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