• 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     ~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 
getWindowAImageReader72     ANativeWindow* getWindow()    const { return mWindow.get(); };
getWidthAImageReader73     int32_t        getWidth()     const { return mWidth; };
getHeightAImageReader74     int32_t        getHeight()    const { return mHeight; };
getFormatAImageReader75     int32_t        getFormat()    const { return mFormat; };
getMaxImagesAImageReader76     int32_t        getMaxImages() const { return mMaxImages; };
77 
78   private:
79 
80     friend struct AImage; // for grabing reader lock
81 
82     BufferItem* getBufferItemLocked();
83     void returnBufferItemLocked(BufferItem* buffer);
84 
85     // Called by AImageReader_acquireXXX to acquire a Buffer and setup AImage.
86     media_status_t acquireImageLocked(/*out*/AImage** image, /*out*/int* fenceFd);
87 
88     // Called by AImage/~AImageReader to close image. Caller is responsible to grab AImage::mLock
89     void releaseImageLocked(AImage* image, int releaseFenceFd);
90 
91     static int getBufferWidth(BufferItem* buffer);
92     static int getBufferHeight(BufferItem* buffer);
93 
94     media_status_t setImageListenerLocked(AImageReader_ImageListener* listener);
95     media_status_t setBufferRemovedListenerLocked(AImageReader_BufferRemovedListener* listener);
96 
97     // definition of handler and message
98     enum {
99         kWhatBufferRemoved,
100         kWhatImageAvailable,
101     };
102     static const char* kCallbackFpKey;
103     static const char* kContextKey;
104     static const char* kGraphicBufferKey;
105     class CallbackHandler : public AHandler {
106       public:
CallbackHandlerAImageReader107         CallbackHandler(AImageReader* reader) : mReader(reader) {}
108         void onMessageReceived(const sp<AMessage> &msg) override;
109       private:
110         AImageReader* mReader;
111     };
112     sp<CallbackHandler> mHandler;
113     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
114     List<BufferItem*>   mBuffers;
115 
116     const int32_t mWidth;
117     const int32_t mHeight;
118     const int32_t mFormat;
119     const uint64_t mUsage;  // AHARDWAREBUFFER_USAGE_* flags.
120     const int32_t mMaxImages;
121 
122     // TODO(jwcai) Seems completely unused in AImageReader class.
123     const int32_t mNumPlanes;
124 
125     struct FrameListener : public ConsumerBase::FrameAvailableListener {
126       public:
FrameListenerAImageReader::FrameListener127         explicit FrameListener(AImageReader* parent) : mReader(parent) {}
128 
129         void onFrameAvailable(const BufferItem& item) override;
130 
131         media_status_t setImageListener(AImageReader_ImageListener* listener);
132 
133       private:
134         AImageReader_ImageListener mListener = {nullptr, nullptr};
135         wp<AImageReader>           mReader;
136         Mutex                      mLock;
137     };
138     sp<FrameListener> mFrameListener;
139 
140     struct BufferRemovedListener : public BufferItemConsumer::BufferFreedListener {
141       public:
BufferRemovedListenerAImageReader::BufferRemovedListener142         explicit BufferRemovedListener(AImageReader* parent) : mReader(parent) {}
143 
144         void onBufferFreed(const wp<GraphicBuffer>& graphicBuffer) override;
145 
146         media_status_t setBufferRemovedListener(AImageReader_BufferRemovedListener* listener);
147 
148        private:
149         AImageReader_BufferRemovedListener mListener = {nullptr, nullptr};
150         wp<AImageReader>           mReader;
151         Mutex                      mLock;
152     };
153     sp<BufferRemovedListener> mBufferRemovedListener;
154 
155     int mHalFormat;
156     android_dataspace mHalDataSpace;
157     uint64_t mHalUsage;
158 
159     sp<IGraphicBufferProducer> mProducer;
160     sp<Surface>                mSurface;
161     sp<BufferItemConsumer>     mBufferItemConsumer;
162     sp<ANativeWindow>          mWindow;
163 
164     List<AImage*>              mAcquiredImages;
165 
166     Mutex                      mLock;
167 };
168 
169 #endif // _NDK_IMAGE_READER_PRIV_H
170