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