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     ~AImageReader();
61 
62     // Inintialize AImageReader, uninitialized or failed to initialize AImageReader
63     // should never be passed to application
64     media_status_t init();
65 
66     media_status_t setImageListener(AImageReader_ImageListener* listener);
67     media_status_t setBufferRemovedListener(AImageReader_BufferRemovedListener* listener);
68 
69     media_status_t acquireNextImage(/*out*/AImage** image, /*out*/int* fenceFd);
70     media_status_t acquireLatestImage(/*out*/AImage** image, /*out*/int* fenceFd);
71 
72     media_status_t getWindowNativeHandle(/*out*/native_handle_t **handle);
73 
getWindowAImageReader74     ANativeWindow* getWindow()    const { return mWindow.get(); };
getWidthAImageReader75     int32_t        getWidth()     const { return mWidth; };
getHeightAImageReader76     int32_t        getHeight()    const { return mHeight; };
getFormatAImageReader77     int32_t        getFormat()    const { return mFormat; };
getMaxImagesAImageReader78     int32_t        getMaxImages() const { return mMaxImages; };
79     void           close();
80 
81   private:
82 
83     friend struct AImage; // for grabing reader lock
84 
85     BufferItem* getBufferItemLocked();
86     void returnBufferItemLocked(BufferItem* buffer);
87 
88     // Called by AImageReader_acquireXXX to acquire a Buffer and setup AImage.
89     media_status_t acquireImageLocked(/*out*/AImage** image, /*out*/int* fenceFd);
90 
91     // Called by AImage/~AImageReader to close image. Caller is responsible to grab AImage::mLock
92     void releaseImageLocked(AImage* image, int releaseFenceFd, bool clearCache = true);
93 
94     static int getBufferWidth(BufferItem* buffer);
95     static int getBufferHeight(BufferItem* buffer);
96 
97     media_status_t setImageListenerLocked(AImageReader_ImageListener* listener);
98     media_status_t setBufferRemovedListenerLocked(AImageReader_BufferRemovedListener* listener);
99 
100     // definition of handler and message
101     enum {
102         kWhatBufferRemoved,
103         kWhatImageAvailable,
104     };
105     static const char* kCallbackFpKey;
106     static const char* kContextKey;
107     static const char* kGraphicBufferKey;
108     class CallbackHandler : public AHandler {
109       public:
CallbackHandlerAImageReader110         CallbackHandler(AImageReader* reader) : mReader(reader) {}
111         void onMessageReceived(const sp<AMessage> &msg) override;
112       private:
113         AImageReader* mReader;
114     };
115     sp<CallbackHandler> mHandler;
116     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
117     List<BufferItem*>   mBuffers;
118 
119     const int32_t mWidth;
120     const int32_t mHeight;
121     const int32_t mFormat;
122     const uint64_t mUsage;  // AHARDWAREBUFFER_USAGE_* flags.
123     const int32_t mMaxImages;
124 
125     // TODO(jwcai) Seems completely unused in AImageReader class.
126     const int32_t mNumPlanes;
127 
128     struct FrameListener : public ConsumerBase::FrameAvailableListener {
129       public:
FrameListenerAImageReader::FrameListener130         explicit FrameListener(AImageReader* parent) : mReader(parent) {}
131 
132         void onFrameAvailable(const BufferItem& item) override;
133 
134         media_status_t setImageListener(AImageReader_ImageListener* listener);
135 
136       private:
137         AImageReader_ImageListener mListener = {nullptr, nullptr};
138         const wp<AImageReader>     mReader;
139         Mutex                      mLock;
140     };
141     sp<FrameListener> mFrameListener;
142 
143     struct BufferRemovedListener : public BufferItemConsumer::BufferFreedListener {
144       public:
BufferRemovedListenerAImageReader::BufferRemovedListener145         explicit BufferRemovedListener(AImageReader* parent) : mReader(parent) {}
146 
147         void onBufferFreed(const wp<GraphicBuffer>& graphicBuffer) override;
148 
149         media_status_t setBufferRemovedListener(AImageReader_BufferRemovedListener* listener);
150 
151        private:
152         AImageReader_BufferRemovedListener mListener = {nullptr, nullptr};
153         const wp<AImageReader>     mReader;
154         Mutex                      mLock;
155     };
156     sp<BufferRemovedListener> mBufferRemovedListener;
157 
158     int mHalFormat;
159     android_dataspace mHalDataSpace;
160     uint64_t mHalUsage;
161 
162     sp<IGraphicBufferProducer> mProducer;
163     sp<Surface>                mSurface;
164     sp<BufferItemConsumer>     mBufferItemConsumer;
165     sp<ANativeWindow>          mWindow;
166     native_handle_t*           mWindowHandle = nullptr;
167 
168     List<AImage*>              mAcquiredImages;
169     bool                       mIsClosed = false;
170 
171     Mutex                      mLock;
172 };
173 
174 #endif // _NDK_IMAGE_READER_PRIV_H
175