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 "NdkImageReader.h"
23 
24 #include <utils/List.h>
25 #include <utils/Mutex.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <gui/CpuConsumer.h>
29 #include <gui/Surface.h>
30 
31 #include <media/stagefright/foundation/ALooper.h>
32 #include <media/stagefright/foundation/AHandler.h>
33 #include <media/stagefright/foundation/AMessage.h>
34 
35 using namespace android;
36 
37 namespace {
38     enum {
39         IMAGE_READER_MAX_NUM_PLANES = 3,
40     };
41 
42     enum {
43         ACQUIRE_SUCCESS = 0,
44         ACQUIRE_NO_BUFFERS = 1,
45         ACQUIRE_MAX_IMAGES = 2,
46     };
47 }
48 
49 struct AImageReader : public RefBase {
50   public:
51 
52     static bool isSupportedFormat(int32_t format);
53     static int getNumPlanesForFormat(int32_t format);
54 
55     AImageReader(int32_t width, int32_t height, int32_t format, int32_t maxImages);
56     ~AImageReader();
57 
58     // Inintialize AImageReader, uninitialized or failed to initialize AImageReader
59     // should never be passed to application
60     media_status_t init();
61 
62     media_status_t setImageListener(AImageReader_ImageListener* listener);
63 
64     media_status_t acquireNextImage(/*out*/AImage** image);
65     media_status_t acquireLatestImage(/*out*/AImage** image);
66 
getWindowAImageReader67     ANativeWindow* getWindow()    const { return mWindow.get(); };
getWidthAImageReader68     int32_t        getWidth()     const { return mWidth; };
getHeightAImageReader69     int32_t        getHeight()    const { return mHeight; };
getFormatAImageReader70     int32_t        getFormat()    const { return mFormat; };
getMaxImagesAImageReader71     int32_t        getMaxImages() const { return mMaxImages; };
72 
73 
74   private:
75 
76     friend struct AImage; // for grabing reader lock
77 
78     media_status_t acquireCpuConsumerImageLocked(/*out*/AImage** image);
79     CpuConsumer::LockedBuffer* getLockedBufferLocked();
80     void returnLockedBufferLocked(CpuConsumer::LockedBuffer* buffer);
81 
82     // Called by AImage to close image
83     void releaseImageLocked(AImage* image);
84 
85     static int getBufferWidth(CpuConsumer::LockedBuffer* buffer);
86     static int getBufferHeight(CpuConsumer::LockedBuffer* buffer);
87 
88     media_status_t setImageListenerLocked(AImageReader_ImageListener* listener);
89 
90     // definition of handler and message
91     enum {
92         kWhatImageAvailable
93     };
94     static const char* kCallbackFpKey;
95     static const char* kContextKey;
96     class CallbackHandler : public AHandler {
97       public:
CallbackHandlerAImageReader98         CallbackHandler(AImageReader* reader) : mReader(reader) {}
99         void onMessageReceived(const sp<AMessage> &msg) override;
100       private:
101         AImageReader* mReader;
102     };
103     sp<CallbackHandler> mHandler;
104     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
105 
106     List<CpuConsumer::LockedBuffer*> mBuffers;
107     const int32_t mWidth;
108     const int32_t mHeight;
109     const int32_t mFormat;
110     const int32_t mMaxImages;
111     const int32_t mNumPlanes;
112 
113     struct FrameListener : public ConsumerBase::FrameAvailableListener {
114       public:
FrameListenerAImageReader::FrameListener115         FrameListener(AImageReader* parent) : mReader(parent) {}
116 
117         void onFrameAvailable(const BufferItem& item) override;
118 
119         media_status_t setImageListener(AImageReader_ImageListener* listener);
120 
121       private:
122         AImageReader_ImageListener mListener = {nullptr, nullptr};
123         wp<AImageReader>           mReader;
124         Mutex                      mLock;
125     };
126     sp<FrameListener> mFrameListener;
127 
128     int mHalFormat;
129     android_dataspace mHalDataSpace;
130 
131     sp<IGraphicBufferProducer> mProducer;
132     sp<Surface>                mSurface;
133     sp<CpuConsumer>            mCpuConsumer;
134     sp<ANativeWindow>          mWindow;
135 
136     List<AImage*>              mAcquiredImages;
137 
138     Mutex                      mLock;
139 };
140 
141 #endif // _NDK_IMAGE_READER_PRIV_H
142