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 | |
| 25 | class BufferSlot; |
| 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 | |
| 42 | // setBufferCount updates the number of available buffer slots. If this |
| 43 | // method succeeds, buffer slots will be both unallocated and owned by |
| 44 | // the BufferQueue object (i.e. they are not owned by the producer or |
| 45 | // consumer). |
| 46 | // |
| 47 | // This will fail if the producer has dequeued any buffers, or if |
| 48 | // bufferCount is invalid. bufferCount must generally be a value |
| 49 | // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS |
| 50 | // (inclusive). It may also be set to zero (the default) to indicate |
| 51 | // that the producer does not wish to set a value. The minimum value |
| 52 | // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 53 | // ...). |
| 54 | // |
| 55 | // This may only be called by the producer. The consumer will be told |
| 56 | // to discard buffers through the onBuffersReleased callback. |
| 57 | virtual status_t setBufferCount(int bufferCount); |
| 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 | // |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 76 | // If the format is 0, the default format will be used. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 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. |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 94 | virtual status_t dequeueBuffer(int *outSlot, sp<Fence>* outFence, |
| 95 | bool async, uint32_t width, uint32_t height, PixelFormat format, |
| 96 | uint32_t usage); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 97 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 98 | // See IGraphicBufferProducer::detachBuffer |
| 99 | virtual status_t detachBuffer(int slot); |
| 100 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 101 | // See IGraphicBufferProducer::detachNextBuffer |
| 102 | virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
| 103 | sp<Fence>* outFence); |
| 104 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 105 | // See IGraphicBufferProducer::attachBuffer |
| 106 | virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer); |
| 107 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 108 | // queueBuffer returns a filled buffer to the BufferQueue. |
| 109 | // |
| 110 | // Additional data is provided in the QueueBufferInput struct. Notably, |
| 111 | // a timestamp must be provided for the buffer. The timestamp is in |
| 112 | // nanoseconds, and must be monotonically increasing. Its other semantics |
| 113 | // (zero point, etc) are producer-specific and should be documented by the |
| 114 | // producer. |
| 115 | // |
| 116 | // The caller may provide a fence that signals when all rendering |
| 117 | // operations have completed. Alternatively, NO_FENCE may be used, |
| 118 | // indicating that the buffer is ready immediately. |
| 119 | // |
| 120 | // Some values are returned in the output struct: the current settings |
| 121 | // for default width and height, the current transform hint, and the |
| 122 | // number of queued buffers. |
| 123 | virtual status_t queueBuffer(int slot, |
| 124 | const QueueBufferInput& input, QueueBufferOutput* output); |
| 125 | |
| 126 | // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't |
| 127 | // queue it for use by the consumer. |
| 128 | // |
| 129 | // The buffer will not be overwritten until the fence signals. The fence |
| 130 | // will usually be the one obtained from dequeueBuffer. |
| 131 | virtual void cancelBuffer(int slot, const sp<Fence>& fence); |
| 132 | |
| 133 | // Query native window attributes. The "what" values are enumerated in |
| 134 | // window.h (e.g. NATIVE_WINDOW_FORMAT). |
| 135 | virtual int query(int what, int* outValue); |
| 136 | |
| 137 | // connect attempts to connect a producer API to the BufferQueue. This |
| 138 | // must be called before any other IGraphicBufferProducer methods are |
| 139 | // called except for getAllocator. A consumer must already be connected. |
| 140 | // |
| 141 | // This method will fail if connect was previously called on the |
| 142 | // BufferQueue and no corresponding disconnect call was made (i.e. if |
| 143 | // it's still connected to a producer). |
| 144 | // |
| 145 | // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU). |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 146 | virtual status_t connect(const sp<IProducerListener>& listener, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 147 | int api, bool producerControlledByApp, QueueBufferOutput* output); |
| 148 | |
| 149 | // disconnect attempts to disconnect a producer API from the BufferQueue. |
| 150 | // Calling this method will cause any subsequent calls to other |
| 151 | // IGraphicBufferProducer methods to fail except for getAllocator and connect. |
| 152 | // Successfully calling connect after this will allow the other methods to |
| 153 | // succeed again. |
| 154 | // |
| 155 | // This method will fail if the the BufferQueue is not currently |
| 156 | // connected to the specified producer API. |
| 157 | virtual status_t disconnect(int api); |
| 158 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 159 | // Attaches a sideband buffer stream to the IGraphicBufferProducer. |
| 160 | // |
| 161 | // A sideband stream is a device-specific mechanism for passing buffers |
| 162 | // from the producer to the consumer without using dequeueBuffer/ |
| 163 | // queueBuffer. If a sideband stream is present, the consumer can choose |
| 164 | // whether to acquire buffers from the sideband stream or from the queued |
| 165 | // buffers. |
| 166 | // |
| 167 | // Passing NULL or a different stream handle will detach the previous |
| 168 | // handle if any. |
| 169 | virtual status_t setSidebandStream(const sp<NativeHandle>& stream); |
| 170 | |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 171 | // See IGraphicBufferProducer::allocateBuffers |
| 172 | virtual void allocateBuffers(bool async, uint32_t width, uint32_t height, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 173 | PixelFormat format, uint32_t usage); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 174 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 175 | // See IGraphicBufferProducer::allowAllocation |
| 176 | virtual status_t allowAllocation(bool allow); |
| 177 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 178 | private: |
| 179 | // This is required by the IBinder::DeathRecipient interface |
| 180 | virtual void binderDied(const wp<IBinder>& who); |
| 181 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 182 | // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may |
| 183 | // block if there are no available slots and we are not in non-blocking |
| 184 | // mode (producer and consumer controlled by the application). If it blocks, |
| 185 | // it will release mCore->mMutex while blocked so that other operations on |
| 186 | // the BufferQueue may succeed. |
| 187 | status_t waitForFreeSlotThenRelock(const char* caller, bool async, |
| 188 | int* found, status_t* returnFlags) const; |
| 189 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 190 | sp<BufferQueueCore> mCore; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 191 | |
| 192 | // This references mCore->mSlots. Lock mCore->mMutex while accessing. |
| 193 | BufferQueueDefs::SlotsType& mSlots; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 194 | |
| 195 | // This is a cached copy of the name stored in the BufferQueueCore. |
| 196 | // It's updated during connect and dequeueBuffer (which should catch |
| 197 | // most updates). |
| 198 | String8 mConsumerName; |
| 199 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 200 | uint32_t mStickyTransform; |
| 201 | |
Eric Penner | 99a0afb | 2014-09-30 11:28:30 -0700 | [diff] [blame] | 202 | // This saves the fence from the last queueBuffer, such that the |
| 203 | // next queueBuffer call can throttle buffer production. The prior |
| 204 | // queueBuffer's fence is not nessessarily available elsewhere, |
| 205 | // since the previous buffer might have already been acquired. |
| 206 | sp<Fence> mLastQueueBufferFence; |
| 207 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 208 | // Take-a-ticket system for ensuring that onFrame* callbacks are called in |
| 209 | // the order that frames are queued. While the BufferQueue lock |
| 210 | // (mCore->mMutex) is held, a ticket is retained by the producer. After |
| 211 | // dropping the BufferQueue lock, the producer must wait on the condition |
| 212 | // variable until the current callback ticket matches its retained ticket. |
| 213 | Mutex mCallbackMutex; |
| 214 | int mNextCallbackTicket; // Protected by mCore->mMutex |
| 215 | int mCurrentCallbackTicket; // Protected by mCallbackMutex |
| 216 | Condition mCallbackCondition; |
| 217 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 218 | }; // class BufferQueueProducer |
| 219 | |
| 220 | } // namespace android |
| 221 | |
| 222 | #endif |