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