• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _NDK_IMAGE_READER_PRIV_H
18 #define _NDK_IMAGE_READER_PRIV_H
19 
20 #include <inttypes.h>
21 
22 #include <media/NdkImageReader.h>
23 
24 #include <utils/List.h>
25 #include <utils/Mutex.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <gui/BufferItem.h>
29 #include <gui/BufferItemConsumer.h>
30 #include <gui/Surface.h>
31 
32 #include <media/stagefright/foundation/ALooper.h>
33 #include <media/stagefright/foundation/AHandler.h>
34 #include <media/stagefright/foundation/AMessage.h>
35 
36 using namespace android;
37 
38 namespace {
39     enum {
40         IMAGE_READER_MAX_NUM_PLANES = 3,
41     };
42 
43     enum {
44         ACQUIRE_SUCCESS = 0,
45         ACQUIRE_NO_BUFFERS = 1,
46         ACQUIRE_MAX_IMAGES = 2,
47     };
48 }
49 
50 struct AImageReader : public RefBase {
51   public:
52     static bool isSupportedFormatAndUsage(int32_t format, uint64_t usage0);
53     static int getNumPlanesForFormat(int32_t format);
54 
55     AImageReader(int32_t width,
56                  int32_t height,
57                  int32_t format,
58                  uint64_t usage,
59                  int32_t maxImages,
60                  uint32_t hardwareBufferFormat,
61                  android_dataspace dataSpace);
62     ~AImageReader();
63 
64     // Initialize AImageReader, uninitialized or failed to initialize AImageReader
65     // should never be passed to application
66     media_status_t init();
67 
68     media_status_t setImageListener(AImageReader_ImageListener* listener);
69     media_status_t setBufferRemovedListener(AImageReader_BufferRemovedListener* listener);
70 
71     media_status_t acquireNextImage(/*out*/AImage** image, /*out*/int* fenceFd);
72     media_status_t acquireLatestImage(/*out*/AImage** image, /*out*/int* fenceFd);
73 
74     media_status_t getWindowNativeHandle(/*out*/native_handle_t **handle);
75 
getWindowAImageReader76     ANativeWindow* getWindow()    const { return mWindow.get(); };
getWidthAImageReader77     int32_t        getWidth()     const { return mWidth; };
getHeightAImageReader78     int32_t        getHeight()    const { return mHeight; };
getFormatAImageReader79     int32_t        getFormat()    const { return mFormat; };
getMaxImagesAImageReader80     int32_t        getMaxImages() const { return mMaxImages; };
81     void           close();
82 
83   private:
84     friend struct AImage; // for grabing reader lock
85 
86     BufferItem* getBufferItemLocked();
87     void returnBufferItemLocked(BufferItem* buffer);
88 
89     // Called by AImageReader_acquireXXX to acquire a Buffer and setup AImage.
90     media_status_t acquireImageLocked(/*out*/AImage** image, /*out*/int* fenceFd);
91 
92     // Called by AImage/~AImageReader to close image. Caller is responsible to grab AImage::mLock
93     void releaseImageLocked(AImage* image, int releaseFenceFd, bool clearCache = true);
94 
95     static int getBufferWidth(BufferItem* buffer);
96     static int getBufferHeight(BufferItem* buffer);
97 
98     media_status_t setImageListenerLocked(AImageReader_ImageListener* listener);
99     media_status_t setBufferRemovedListenerLocked(AImageReader_BufferRemovedListener* listener);
100 
101     // definition of handler and message
102     enum {
103         kWhatBufferRemoved,
104         kWhatImageAvailable,
105     };
106     static const char* kCallbackFpKey;
107     static const char* kContextKey;
108     static const char* kGraphicBufferKey;
109     class CallbackHandler : public AHandler {
110       public:
CallbackHandlerAImageReader111         CallbackHandler(AImageReader* reader) : mReader(reader) {}
112         void onMessageReceived(const sp<AMessage> &msg) override;
113       private:
114         AImageReader* mReader;
115     };
116     sp<CallbackHandler> mHandler;
117     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
118     List<BufferItem*>   mBuffers;
119 
120     const int32_t mWidth;
121     const int32_t mHeight;
122     int32_t mFormat;
123     const uint64_t mUsage;  // AHARDWAREBUFFER_USAGE_* flags.
124     const int32_t mMaxImages;
125 
126     // TODO(jwcai) Seems completely unused in AImageReader class.
127     const int32_t mNumPlanes;
128 
129     uint32_t mHalFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
130     android_dataspace mHalDataSpace = HAL_DATASPACE_UNKNOWN;
131 
132     struct FrameListener : public ConsumerBase::FrameAvailableListener {
133       public:
FrameListenerAImageReader::FrameListener134         explicit FrameListener(AImageReader* parent) : mReader(parent) {}
135 
136         void onFrameAvailable(const BufferItem& item) override;
137 
138         media_status_t setImageListener(AImageReader_ImageListener* listener);
139 
140       private:
141         AImageReader_ImageListener mListener = {nullptr, nullptr};
142         const wp<AImageReader>     mReader;
143         Mutex                      mLock;
144     };
145     sp<FrameListener> mFrameListener;
146 
147     struct BufferRemovedListener : public BufferItemConsumer::BufferFreedListener {
148       public:
BufferRemovedListenerAImageReader::BufferRemovedListener149         explicit BufferRemovedListener(AImageReader* parent) : mReader(parent) {}
150 
151         void onBufferFreed(const wp<GraphicBuffer>& graphicBuffer) override;
152 
153         media_status_t setBufferRemovedListener(AImageReader_BufferRemovedListener* listener);
154 
155        private:
156         AImageReader_BufferRemovedListener mListener = {nullptr, nullptr};
157         const wp<AImageReader>     mReader;
158         Mutex                      mLock;
159     };
160     sp<BufferRemovedListener> mBufferRemovedListener;
161 
162     uint64_t mHalUsage;
163 
164     sp<IGraphicBufferProducer> mProducer;
165     sp<Surface>                mSurface;
166     sp<BufferItemConsumer>     mBufferItemConsumer;
167     sp<ANativeWindow>          mWindow;
168     native_handle_t*           mWindowHandle = nullptr;
169 
170     List<AImage*>              mAcquiredImages;
171     bool                       mIsOpen = false;
172 
173     Mutex                      mLock;
174 };
175 
176 #endif // _NDK_IMAGE_READER_PRIV_H
177