1 /* 2 * Copyright 2014 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 ANDROID_GUI_BUFFERQUEUEPRODUCER_H 18 #define ANDROID_GUI_BUFFERQUEUEPRODUCER_H 19 20 #include <gui/AdditionalOptions.h> 21 #include <gui/BufferQueueDefs.h> 22 23 #include <gui/IGraphicBufferProducer.h> 24 25 namespace android { 26 27 class IBinder; 28 struct BufferSlot; 29 30 #ifndef NO_BINDER 31 class BufferQueueProducer : public BnGraphicBufferProducer, 32 private IBinder::DeathRecipient { 33 #else 34 class BufferQueueProducer : public BnGraphicBufferProducer { 35 #endif 36 public: 37 friend class BufferQueue; // Needed to access binderDied 38 39 ~BufferQueueProducer() override; 40 41 // requestBuffer returns the GraphicBuffer for slot N. 42 // 43 // In normal operation, this is called the first time slot N is returned 44 // by dequeueBuffer. It must be called again if dequeueBuffer returns 45 // flags indicating that previously-returned buffers are no longer valid. 46 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); 47 48 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) 49 // see IGraphicsBufferProducer::extendSlotCount 50 virtual status_t extendSlotCount(int size) override; 51 #endif 52 53 // see IGraphicsBufferProducer::setMaxDequeuedBufferCount 54 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers); 55 56 // see IGraphicsBufferProducer::setAsyncMode 57 virtual status_t setAsyncMode(bool async); 58 59 // dequeueBuffer gets the next buffer slot index for the producer to use. 60 // If a buffer slot is available then that slot index is written to the 61 // location pointed to by the buf argument and a status of OK is returned. 62 // If no slot is available then a status of -EBUSY is returned and buf is 63 // unmodified. 64 // 65 // The outFence parameter will be updated to hold the fence associated with 66 // the buffer. The contents of the buffer must not be overwritten until the 67 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be 68 // written immediately. 69 // 70 // The width and height parameters must be no greater than the minimum of 71 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). 72 // An error due to invalid dimensions might not be reported until 73 // updateTexImage() is called. If width and height are both zero, the 74 // default values specified by setDefaultBufferSize() are used instead. 75 // 76 // If the format is 0, the default format will be used. 77 // 78 // The usage argument specifies gralloc buffer usage flags. The values 79 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These 80 // will be merged with the usage flags specified by setConsumerUsageBits. 81 // 82 // The return value may be a negative error value or a non-negative 83 // collection of flags. If the flags are set, the return values are 84 // valid, but additional actions must be performed. 85 // 86 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the 87 // producer must discard cached GraphicBuffer references for the slot 88 // returned in buf. 89 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer 90 // must discard cached GraphicBuffer references for all slots. 91 // 92 // In both cases, the producer will need to call requestBuffer to get a 93 // GraphicBuffer handle for the returned slot. 94 virtual status_t dequeueBuffer(int* outSlot, sp<Fence>* outFence, uint32_t width, 95 uint32_t height, PixelFormat format, uint64_t usage, 96 uint64_t* outBufferAge, 97 FrameEventHistoryDelta* outTimestamps) override; 98 99 // See IGraphicBufferProducer::detachBuffer 100 virtual status_t detachBuffer(int slot); 101 102 // See IGraphicBufferProducer::detachNextBuffer 103 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, 104 sp<Fence>* outFence); 105 106 // See IGraphicBufferProducer::attachBuffer 107 virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer); 108 109 // queueBuffer returns a filled buffer to the BufferQueue. 110 // 111 // Additional data is provided in the QueueBufferInput struct. Notably, 112 // a timestamp must be provided for the buffer. The timestamp is in 113 // nanoseconds, and must be monotonically increasing. Its other semantics 114 // (zero point, etc) are producer-specific and should be documented by the 115 // producer. 116 // 117 // The caller may provide a fence that signals when all rendering 118 // operations have completed. Alternatively, NO_FENCE may be used, 119 // indicating that the buffer is ready immediately. 120 // 121 // Some values are returned in the output struct: the current settings 122 // for default width and height, the current transform hint, and the 123 // number of queued buffers. 124 virtual status_t queueBuffer(int slot, 125 const QueueBufferInput& input, QueueBufferOutput* output); 126 127 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't 128 // queue it for use by the consumer. 129 // 130 // The buffer will not be overwritten until the fence signals. The fence 131 // will usually be the one obtained from dequeueBuffer. 132 virtual status_t cancelBuffer(int slot, const sp<Fence>& fence); 133 134 // Query native window attributes. The "what" values are enumerated in 135 // window.h (e.g. NATIVE_WINDOW_FORMAT). 136 virtual int query(int what, int* outValue); 137 138 // connect attempts to connect a producer API to the BufferQueue. This 139 // must be called before any other IGraphicBufferProducer methods are 140 // called except for getAllocator. A consumer must already be connected. 141 // 142 // This method will fail if connect was previously called on the 143 // BufferQueue and no corresponding disconnect call was made (i.e. if 144 // it's still connected to a producer). 145 // 146 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU). 147 virtual status_t connect(const sp<IProducerListener>& listener, 148 int api, bool producerControlledByApp, QueueBufferOutput* output); 149 150 // See IGraphicBufferProducer::disconnect 151 virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api); 152 153 // Attaches a sideband buffer stream to the IGraphicBufferProducer. 154 // 155 // A sideband stream is a device-specific mechanism for passing buffers 156 // from the producer to the consumer without using dequeueBuffer/ 157 // queueBuffer. If a sideband stream is present, the consumer can choose 158 // whether to acquire buffers from the sideband stream or from the queued 159 // buffers. 160 // 161 // Passing NULL or a different stream handle will detach the previous 162 // handle if any. 163 virtual status_t setSidebandStream(const sp<NativeHandle>& stream); 164 165 // See IGraphicBufferProducer::allocateBuffers 166 virtual void allocateBuffers(uint32_t width, uint32_t height, 167 PixelFormat format, uint64_t usage) override; 168 169 // See IGraphicBufferProducer::allowAllocation 170 virtual status_t allowAllocation(bool allow); 171 172 // See IGraphicBufferProducer::setGenerationNumber 173 virtual status_t setGenerationNumber(uint32_t generationNumber); 174 175 // See IGraphicBufferProducer::getConsumerName 176 virtual String8 getConsumerName() const override; 177 178 // See IGraphicBufferProducer::setSharedBufferMode 179 virtual status_t setSharedBufferMode(bool sharedBufferMode) override; 180 181 // See IGraphicBufferProducer::setAutoRefresh 182 virtual status_t setAutoRefresh(bool autoRefresh) override; 183 184 // See IGraphicBufferProducer::setDequeueTimeout 185 virtual status_t setDequeueTimeout(nsecs_t timeout) override; 186 187 // see IGraphicBufferProducer::setLegacyBufferDrop 188 virtual status_t setLegacyBufferDrop(bool drop); 189 190 // See IGraphicBufferProducer::getLastQueuedBuffer 191 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, 192 sp<Fence>* outFence, float outTransformMatrix[16]) override; 193 194 // See IGraphicBufferProducer::getLastQueuedBuffer 195 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence, 196 Rect* outRect, uint32_t* outTransform) override; 197 198 // See IGraphicBufferProducer::getFrameTimestamps 199 virtual void getFrameTimestamps(FrameEventHistoryDelta* outDelta) override; 200 201 // See IGraphicBufferProducer::getUniqueId 202 virtual status_t getUniqueId(uint64_t* outId) const override; 203 204 // See IGraphicBufferProducer::getConsumerUsage 205 virtual status_t getConsumerUsage(uint64_t* outUsage) const override; 206 207 // See IGraphicBufferProducer::setAutoPrerotation 208 virtual status_t setAutoPrerotation(bool autoPrerotation); 209 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE) 210 // See IGraphicBufferProducer::setFrameRate 211 status_t setFrameRate(float frameRate, int8_t compatibility, 212 int8_t changeFrameRateStrategy) override; 213 #endif 214 215 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) 216 status_t setAdditionalOptions(const std::vector<gui::AdditionalOptions>& options) override; 217 #endif 218 219 protected: 220 explicit BufferQueueProducer(const sp<BufferQueueCore>& core, 221 bool consumerIsSurfaceFlinger = false); 222 friend class sp<BufferQueueProducer>; 223 // see IGraphicsBufferProducer::setMaxDequeuedBufferCount, but with the ability to retrieve the 224 // total maximum buffer count for the buffer queue (dequeued AND acquired) 225 status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers, int* maxBufferCount); 226 227 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) 228 // Wait until a buffer has been released. The method may spuriously return OK when no buffer has 229 // been released. The BufferQueue mutex is passed in the locked state. It must be unlocked 230 // before waiting for a release and locked before returning. 231 virtual status_t waitForBufferRelease(std::unique_lock<std::mutex>& lock, 232 nsecs_t timeout) const; 233 #endif 234 235 private: 236 // This is required by the IBinder::DeathRecipient interface 237 virtual void binderDied(const wp<IBinder>& who); 238 239 // Returns the slot of the next free buffer if one is available or 240 // BufferQueueCore::INVALID_BUFFER_SLOT otherwise 241 int getFreeBufferLocked() const; 242 243 // Returns the next free slot if one is available or 244 // BufferQueueCore::INVALID_BUFFER_SLOT otherwise 245 int getFreeSlotLocked() const; 246 247 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, 248 FrameEventHistoryDelta* outDelta); 249 250 // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may 251 // block if there are no available slots and we are not in non-blocking 252 // mode (producer and consumer controlled by the application). If it blocks, 253 // it will release mCore->mMutex while blocked so that other operations on 254 // the BufferQueue may succeed. 255 enum class FreeSlotCaller { 256 Dequeue, 257 Attach, 258 }; 259 status_t waitForFreeSlotThenRelock(FreeSlotCaller caller, std::unique_lock<std::mutex>& lock, 260 int* found) const; 261 262 sp<BufferQueueCore> mCore; 263 264 // This references mCore->mSlots. Lock mCore->mMutex while accessing. 265 BufferQueueDefs::SlotsType& mSlots; 266 267 // This is a cached copy of the name stored in the BufferQueueCore. 268 // It's updated during connect and dequeueBuffer (which should catch 269 // most updates). 270 String8 mConsumerName; 271 272 uint32_t mStickyTransform; 273 274 // This controls whether the GraphicBuffer pointer in the BufferItem is 275 // cleared after being queued 276 bool mConsumerIsSurfaceFlinger; 277 278 // This saves the fence from the last queueBuffer, such that the 279 // next queueBuffer call can throttle buffer production. The prior 280 // queueBuffer's fence is not nessessarily available elsewhere, 281 // since the previous buffer might have already been acquired. 282 sp<Fence> mLastQueueBufferFence; 283 284 Rect mLastQueuedCrop; 285 uint32_t mLastQueuedTransform; 286 287 // Take-a-ticket system for ensuring that onFrame* callbacks are called in 288 // the order that frames are queued. While the BufferQueue lock 289 // (mCore->mMutex) is held, a ticket is retained by the producer. After 290 // dropping the BufferQueue lock, the producer must wait on the condition 291 // variable until the current callback ticket matches its retained ticket. 292 std::mutex mCallbackMutex; 293 int mNextCallbackTicket; // Protected by mCore->mMutex 294 int mCurrentCallbackTicket; // Protected by mCallbackMutex 295 std::condition_variable mCallbackCondition; 296 297 // Sets how long dequeueBuffer or attachBuffer will block if a buffer or 298 // slot is not yet available. 299 nsecs_t mDequeueTimeout; 300 301 // If set to true, dequeueBuffer() is currently waiting for buffer allocation to complete. 302 bool mDequeueWaitingForAllocation; 303 304 // Condition variable to signal allocateBuffers() that dequeueBuffer() is no longer waiting for 305 // allocation to complete. 306 std::condition_variable mDequeueWaitingForAllocationCondition; 307 308 }; // class BufferQueueProducer 309 310 } // namespace android 311 312 #endif 313