| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 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 |  | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 20 | #include <gui/BufferQueueDefs.h> | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 21 | #include <gui/IGraphicBufferProducer.h> | 
|  | 22 |  | 
|  | 23 | namespace android { | 
|  | 24 |  | 
| Colin Cross | 8970247 | 2016-09-29 17:46:51 -0700 | [diff] [blame] | 25 | struct BufferSlot; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 26 |  | 
|  | 27 | class BufferQueueProducer : public BnGraphicBufferProducer, | 
|  | 28 | private IBinder::DeathRecipient { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 29 | public: | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 30 | friend class BufferQueue; // Needed to access binderDied | 
|  | 31 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 32 | BufferQueueProducer(const sp<BufferQueueCore>& core); | 
|  | 33 | virtual ~BufferQueueProducer(); | 
|  | 34 |  | 
|  | 35 | // requestBuffer returns the GraphicBuffer for slot N. | 
|  | 36 | // | 
|  | 37 | // In normal operation, this is called the first time slot N is returned | 
|  | 38 | // by dequeueBuffer.  It must be called again if dequeueBuffer returns | 
|  | 39 | // flags indicating that previously-returned buffers are no longer valid. | 
|  | 40 | virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); | 
|  | 41 |  | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 42 | // see IGraphicsBufferProducer::setMaxDequeuedBufferCount | 
|  | 43 | virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers); | 
|  | 44 |  | 
|  | 45 | // see IGraphicsBufferProducer::setAsyncMode | 
|  | 46 | virtual status_t setAsyncMode(bool async); | 
|  | 47 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 48 | // dequeueBuffer gets the next buffer slot index for the producer to use. | 
|  | 49 | // If a buffer slot is available then that slot index is written to the | 
|  | 50 | // location pointed to by the buf argument and a status of OK is returned. | 
|  | 51 | // If no slot is available then a status of -EBUSY is returned and buf is | 
|  | 52 | // unmodified. | 
|  | 53 | // | 
|  | 54 | // The outFence parameter will be updated to hold the fence associated with | 
|  | 55 | // the buffer. The contents of the buffer must not be overwritten until the | 
|  | 56 | // fence signals. If the fence is Fence::NO_FENCE, the buffer may be | 
|  | 57 | // written immediately. | 
|  | 58 | // | 
|  | 59 | // The width and height parameters must be no greater than the minimum of | 
|  | 60 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). | 
|  | 61 | // An error due to invalid dimensions might not be reported until | 
|  | 62 | // updateTexImage() is called.  If width and height are both zero, the | 
|  | 63 | // default values specified by setDefaultBufferSize() are used instead. | 
|  | 64 | // | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 65 | // If the format is 0, the default format will be used. | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 66 | // | 
|  | 67 | // The usage argument specifies gralloc buffer usage flags.  The values | 
|  | 68 | // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER.  These | 
|  | 69 | // will be merged with the usage flags specified by setConsumerUsageBits. | 
|  | 70 | // | 
|  | 71 | // The return value may be a negative error value or a non-negative | 
|  | 72 | // collection of flags.  If the flags are set, the return values are | 
|  | 73 | // valid, but additional actions must be performed. | 
|  | 74 | // | 
|  | 75 | // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the | 
|  | 76 | // producer must discard cached GraphicBuffer references for the slot | 
|  | 77 | // returned in buf. | 
|  | 78 | // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer | 
|  | 79 | // must discard cached GraphicBuffer references for all slots. | 
|  | 80 | // | 
|  | 81 | // In both cases, the producer will need to call requestBuffer to get a | 
|  | 82 | // GraphicBuffer handle for the returned slot. | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 83 | virtual status_t dequeueBuffer(int *outSlot, sp<Fence>* outFence, | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 84 | uint32_t width, uint32_t height, PixelFormat format, | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 85 | uint32_t usage); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 86 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 87 | // See IGraphicBufferProducer::detachBuffer | 
|  | 88 | virtual status_t detachBuffer(int slot); | 
|  | 89 |  | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 90 | // See IGraphicBufferProducer::detachNextBuffer | 
|  | 91 | virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, | 
|  | 92 | sp<Fence>* outFence); | 
|  | 93 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 94 | // See IGraphicBufferProducer::attachBuffer | 
|  | 95 | virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer); | 
|  | 96 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 97 | // queueBuffer returns a filled buffer to the BufferQueue. | 
|  | 98 | // | 
|  | 99 | // Additional data is provided in the QueueBufferInput struct.  Notably, | 
|  | 100 | // a timestamp must be provided for the buffer. The timestamp is in | 
|  | 101 | // nanoseconds, and must be monotonically increasing. Its other semantics | 
|  | 102 | // (zero point, etc) are producer-specific and should be documented by the | 
|  | 103 | // producer. | 
|  | 104 | // | 
|  | 105 | // The caller may provide a fence that signals when all rendering | 
|  | 106 | // operations have completed.  Alternatively, NO_FENCE may be used, | 
|  | 107 | // indicating that the buffer is ready immediately. | 
|  | 108 | // | 
|  | 109 | // Some values are returned in the output struct: the current settings | 
|  | 110 | // for default width and height, the current transform hint, and the | 
|  | 111 | // number of queued buffers. | 
|  | 112 | virtual status_t queueBuffer(int slot, | 
|  | 113 | const QueueBufferInput& input, QueueBufferOutput* output); | 
|  | 114 |  | 
|  | 115 | // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't | 
|  | 116 | // queue it for use by the consumer. | 
|  | 117 | // | 
|  | 118 | // The buffer will not be overwritten until the fence signals.  The fence | 
|  | 119 | // will usually be the one obtained from dequeueBuffer. | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 120 | virtual status_t cancelBuffer(int slot, const sp<Fence>& fence); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | // Query native window attributes.  The "what" values are enumerated in | 
|  | 123 | // window.h (e.g. NATIVE_WINDOW_FORMAT). | 
|  | 124 | virtual int query(int what, int* outValue); | 
|  | 125 |  | 
|  | 126 | // connect attempts to connect a producer API to the BufferQueue.  This | 
|  | 127 | // must be called before any other IGraphicBufferProducer methods are | 
|  | 128 | // called except for getAllocator.  A consumer must already be connected. | 
|  | 129 | // | 
|  | 130 | // This method will fail if connect was previously called on the | 
|  | 131 | // BufferQueue and no corresponding disconnect call was made (i.e. if | 
|  | 132 | // it's still connected to a producer). | 
|  | 133 | // | 
|  | 134 | // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU). | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 135 | virtual status_t connect(const sp<IProducerListener>& listener, | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 136 | int api, bool producerControlledByApp, QueueBufferOutput* output); | 
|  | 137 |  | 
|  | 138 | // disconnect attempts to disconnect a producer API from the BufferQueue. | 
|  | 139 | // Calling this method will cause any subsequent calls to other | 
|  | 140 | // IGraphicBufferProducer methods to fail except for getAllocator and connect. | 
|  | 141 | // Successfully calling connect after this will allow the other methods to | 
|  | 142 | // succeed again. | 
|  | 143 | // | 
|  | 144 | // This method will fail if the the BufferQueue is not currently | 
|  | 145 | // connected to the specified producer API. | 
|  | 146 | virtual status_t disconnect(int api); | 
|  | 147 |  | 
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 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 |  | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 160 | // See IGraphicBufferProducer::allocateBuffers | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 161 | virtual void allocateBuffers(uint32_t width, uint32_t height, | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 162 | PixelFormat format, uint32_t usage); | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 163 |  | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 164 | // See IGraphicBufferProducer::allowAllocation | 
|  | 165 | virtual status_t allowAllocation(bool allow); | 
|  | 166 |  | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 167 | // See IGraphicBufferProducer::setGenerationNumber | 
|  | 168 | virtual status_t setGenerationNumber(uint32_t generationNumber); | 
|  | 169 |  | 
| Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 170 | // See IGraphicBufferProducer::getConsumerName | 
|  | 171 | virtual String8 getConsumerName() const override; | 
|  | 172 |  | 
| Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 173 | // See IGraphicBufferProducer::getNextFrameNumber | 
|  | 174 | virtual uint64_t getNextFrameNumber() const override; | 
|  | 175 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 176 | // See IGraphicBufferProducer::setSharedBufferMode | 
|  | 177 | virtual status_t setSharedBufferMode(bool sharedBufferMode) override; | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 178 |  | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 179 | // See IGraphicBufferProducer::setAutoRefresh | 
|  | 180 | virtual status_t setAutoRefresh(bool autoRefresh) override; | 
|  | 181 |  | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 182 | // See IGraphicBufferProducer::setDequeueTimeout | 
|  | 183 | virtual status_t setDequeueTimeout(nsecs_t timeout) override; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 184 |  | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 185 | // See IGraphicBufferProducer::getLastQueuedBuffer | 
|  | 186 | virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 187 | sp<Fence>* outFence, float outTransformMatrix[16]) override; | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 188 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 189 | private: | 
|  | 190 | // This is required by the IBinder::DeathRecipient interface | 
|  | 191 | virtual void binderDied(const wp<IBinder>& who); | 
|  | 192 |  | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 193 | // Returns the slot of the next free buffer if one is available or | 
|  | 194 | // BufferQueueCore::INVALID_BUFFER_SLOT otherwise | 
|  | 195 | int getFreeBufferLocked() const; | 
|  | 196 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 197 | // Returns the next free slot if one is available or | 
|  | 198 | // BufferQueueCore::INVALID_BUFFER_SLOT otherwise | 
|  | 199 | int getFreeSlotLocked() const; | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 200 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 201 | // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may | 
|  | 202 | // block if there are no available slots and we are not in non-blocking | 
|  | 203 | // mode (producer and consumer controlled by the application). If it blocks, | 
|  | 204 | // it will release mCore->mMutex while blocked so that other operations on | 
|  | 205 | // the BufferQueue may succeed. | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 206 | enum class FreeSlotCaller { | 
|  | 207 | Dequeue, | 
|  | 208 | Attach, | 
|  | 209 | }; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 210 | status_t waitForFreeSlotThenRelock(FreeSlotCaller caller, int* found) const; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 211 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 212 | sp<BufferQueueCore> mCore; | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 213 |  | 
|  | 214 | // This references mCore->mSlots. Lock mCore->mMutex while accessing. | 
|  | 215 | BufferQueueDefs::SlotsType& mSlots; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 216 |  | 
|  | 217 | // This is a cached copy of the name stored in the BufferQueueCore. | 
|  | 218 | // It's updated during connect and dequeueBuffer (which should catch | 
|  | 219 | // most updates). | 
|  | 220 | String8 mConsumerName; | 
|  | 221 |  | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 222 | uint32_t mStickyTransform; | 
|  | 223 |  | 
| Eric Penner | 99a0afb | 2014-09-30 11:28:30 -0700 | [diff] [blame] | 224 | // This saves the fence from the last queueBuffer, such that the | 
|  | 225 | // next queueBuffer call can throttle buffer production. The prior | 
|  | 226 | // queueBuffer's fence is not nessessarily available elsewhere, | 
|  | 227 | // since the previous buffer might have already been acquired. | 
|  | 228 | sp<Fence> mLastQueueBufferFence; | 
|  | 229 |  | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 230 | Rect mLastQueuedCrop; | 
|  | 231 | uint32_t mLastQueuedTransform; | 
|  | 232 |  | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 233 | // Take-a-ticket system for ensuring that onFrame* callbacks are called in | 
|  | 234 | // the order that frames are queued. While the BufferQueue lock | 
|  | 235 | // (mCore->mMutex) is held, a ticket is retained by the producer. After | 
|  | 236 | // dropping the BufferQueue lock, the producer must wait on the condition | 
|  | 237 | // variable until the current callback ticket matches its retained ticket. | 
|  | 238 | Mutex mCallbackMutex; | 
|  | 239 | int mNextCallbackTicket; // Protected by mCore->mMutex | 
|  | 240 | int mCurrentCallbackTicket; // Protected by mCallbackMutex | 
|  | 241 | Condition mCallbackCondition; | 
|  | 242 |  | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 243 | // Sets how long dequeueBuffer or attachBuffer will block if a buffer or | 
|  | 244 | // slot is not yet available. | 
|  | 245 | nsecs_t mDequeueTimeout; | 
|  | 246 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 247 | }; // class BufferQueueProducer | 
|  | 248 |  | 
|  | 249 | } // namespace android | 
|  | 250 |  | 
|  | 251 | #endif |