Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_BUFFERQUEUE_H |
| 18 | #define ANDROID_GUI_BUFFERQUEUE_H |
| 19 | |
| 20 | #include <EGL/egl.h> |
Daniel Lam | f71c4ae | 2012-03-23 18:12:04 -0700 | [diff] [blame] | 21 | #include <EGL/eglext.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 22 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 23 | #include <gui/IGraphicBufferAlloc.h> |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 24 | #include <gui/IGraphicBufferProducer.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 25 | |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 26 | #include <ui/Fence.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 27 | #include <ui/GraphicBuffer.h> |
| 28 | |
| 29 | #include <utils/String8.h> |
| 30 | #include <utils/Vector.h> |
| 31 | #include <utils/threads.h> |
| 32 | |
| 33 | namespace android { |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 36 | class BufferQueue : public BnGraphicBufferProducer { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 37 | public: |
| 38 | enum { MIN_UNDEQUEUED_BUFFERS = 2 }; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 39 | enum { NUM_BUFFER_SLOTS = 32 }; |
| 40 | enum { NO_CONNECTED_API = 0 }; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 41 | enum { INVALID_BUFFER_SLOT = -1 }; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 42 | enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE, PRESENT_LATER }; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 43 | |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 44 | // When in async mode we reserve two slots in order to guarantee that the |
| 45 | // producer and consumer can run asynchronously. |
| 46 | enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 }; |
| 47 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 48 | // ConsumerListener is the interface through which the BufferQueue notifies |
| 49 | // the consumer of events that the consumer may wish to react to. Because |
| 50 | // the consumer will generally have a mutex that is locked during calls from |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 51 | // the consumer to the BufferQueue, these calls from the BufferQueue to the |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 52 | // consumer *MUST* be called only when the BufferQueue mutex is NOT locked. |
| 53 | struct ConsumerListener : public virtual RefBase { |
| 54 | // onFrameAvailable is called from queueBuffer each time an additional |
| 55 | // frame becomes available for consumption. This means that frames that |
| 56 | // are queued while in asynchronous mode only trigger the callback if no |
| 57 | // previous frames are pending. Frames queued while in synchronous mode |
| 58 | // always trigger the callback. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 59 | // |
| 60 | // This is called without any lock held and can be called concurrently |
| 61 | // by multiple threads. |
| 62 | virtual void onFrameAvailable() = 0; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 63 | |
| 64 | // onBuffersReleased is called to notify the buffer consumer that the |
| 65 | // BufferQueue has released its references to one or more GraphicBuffers |
| 66 | // contained in its slots. The buffer consumer should then call |
| 67 | // BufferQueue::getReleasedBuffers to retrieve the list of buffers |
| 68 | // |
| 69 | // This is called without any lock held and can be called concurrently |
| 70 | // by multiple threads. |
| 71 | virtual void onBuffersReleased() = 0; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 74 | // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak |
| 75 | // reference to the actual consumer object. It forwards all calls to that |
| 76 | // consumer object so long as it exists. |
| 77 | // |
| 78 | // This class exists to avoid having a circular reference between the |
| 79 | // BufferQueue object and the consumer object. The reason this can't be a weak |
| 80 | // reference in the BufferQueue class is because we're planning to expose the |
| 81 | // consumer side of a BufferQueue as a binder interface, which doesn't support |
| 82 | // weak references. |
| 83 | class ProxyConsumerListener : public BufferQueue::ConsumerListener { |
| 84 | public: |
| 85 | |
| 86 | ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener); |
| 87 | virtual ~ProxyConsumerListener(); |
| 88 | virtual void onFrameAvailable(); |
| 89 | virtual void onBuffersReleased(); |
| 90 | |
| 91 | private: |
| 92 | |
| 93 | // mConsumerListener is a weak reference to the ConsumerListener. This is |
| 94 | // the raison d'etre of ProxyConsumerListener. |
| 95 | wp<BufferQueue::ConsumerListener> mConsumerListener; |
| 96 | }; |
| 97 | |
| 98 | |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 99 | // BufferQueue manages a pool of gralloc memory slots to be used by |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 100 | // producers and consumers. allocator is used to allocate all the |
| 101 | // needed gralloc buffers. |
| 102 | BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 103 | virtual ~BufferQueue(); |
| 104 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 105 | // Query native window attributes. The "what" values are enumerated in |
| 106 | // window.h (e.g. NATIVE_WINDOW_FORMAT). |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 107 | virtual int query(int what, int* value); |
| 108 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 109 | // setBufferCount updates the number of available buffer slots. If this |
| 110 | // method succeeds, buffer slots will be both unallocated and owned by |
| 111 | // the BufferQueue object (i.e. they are not owned by the producer or |
| 112 | // consumer). |
| 113 | // |
| 114 | // This will fail if the producer has dequeued any buffers, or if |
| 115 | // bufferCount is invalid. bufferCount must generally be a value |
| 116 | // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS |
| 117 | // (inclusive). It may also be set to zero (the default) to indicate |
| 118 | // that the producer does not wish to set a value. The minimum value |
| 119 | // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 120 | // ...). |
| 121 | // |
| 122 | // This may only be called by the producer. The consumer will be told |
| 123 | // to discard buffers through the onBuffersReleased callback. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 124 | virtual status_t setBufferCount(int bufferCount); |
| 125 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 126 | // requestBuffer returns the GraphicBuffer for slot N. |
| 127 | // |
| 128 | // In normal operation, this is called the first time slot N is returned |
| 129 | // by dequeueBuffer. It must be called again if dequeueBuffer returns |
| 130 | // flags indicating that previously-returned buffers are no longer valid. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 131 | virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); |
| 132 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 133 | // dequeueBuffer gets the next buffer slot index for the producer to use. |
| 134 | // If a buffer slot is available then that slot index is written to the |
| 135 | // location pointed to by the buf argument and a status of OK is returned. |
| 136 | // If no slot is available then a status of -EBUSY is returned and buf is |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 137 | // unmodified. |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 138 | // |
| 139 | // The fence parameter will be updated to hold the fence associated with |
| 140 | // the buffer. The contents of the buffer must not be overwritten until the |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 141 | // fence signals. If the fence is Fence::NO_FENCE, the buffer may be |
| 142 | // written immediately. |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 143 | // |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 144 | // The width and height parameters must be no greater than the minimum of |
| 145 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
| 146 | // An error due to invalid dimensions might not be reported until |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 147 | // updateTexImage() is called. If width and height are both zero, the |
| 148 | // default values specified by setDefaultBufferSize() are used instead. |
| 149 | // |
| 150 | // The pixel formats are enumerated in graphics.h, e.g. |
| 151 | // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format |
| 152 | // will be used. |
| 153 | // |
| 154 | // The usage argument specifies gralloc buffer usage flags. The values |
| 155 | // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These |
| 156 | // will be merged with the usage flags specified by setConsumerUsageBits. |
| 157 | // |
| 158 | // The return value may be a negative error value or a non-negative |
| 159 | // collection of flags. If the flags are set, the return values are |
| 160 | // valid, but additional actions must be performed. |
| 161 | // |
| 162 | // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the |
| 163 | // producer must discard cached GraphicBuffer references for the slot |
| 164 | // returned in buf. |
| 165 | // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer |
| 166 | // must discard cached GraphicBuffer references for all slots. |
| 167 | // |
| 168 | // In both cases, the producer will need to call requestBuffer to get a |
| 169 | // GraphicBuffer handle for the returned slot. |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 170 | virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async, |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 171 | uint32_t width, uint32_t height, uint32_t format, uint32_t usage); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 172 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 173 | // queueBuffer returns a filled buffer to the BufferQueue. |
| 174 | // |
| 175 | // Additional data is provided in the QueueBufferInput struct. Notably, |
| 176 | // a timestamp must be provided for the buffer. The timestamp is in |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 177 | // nanoseconds, and must be monotonically increasing. Its other semantics |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 178 | // (zero point, etc) are producer-specific and should be documented by the |
| 179 | // producer. |
| 180 | // |
| 181 | // The caller may provide a fence that signals when all rendering |
| 182 | // operations have completed. Alternatively, NO_FENCE may be used, |
| 183 | // indicating that the buffer is ready immediately. |
| 184 | // |
| 185 | // Some values are returned in the output struct: the current settings |
| 186 | // for default width and height, the current transform hint, and the |
| 187 | // number of queued buffers. |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 188 | virtual status_t queueBuffer(int buf, |
| 189 | const QueueBufferInput& input, QueueBufferOutput* output); |
| 190 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 191 | // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't |
| 192 | // queue it for use by the consumer. |
| 193 | // |
| 194 | // The buffer will not be overwritten until the fence signals. The fence |
| 195 | // will usually be the one obtained from dequeueBuffer. |
Jesse Hall | 4c00cc1 | 2013-03-15 21:34:30 -0700 | [diff] [blame] | 196 | virtual void cancelBuffer(int buf, const sp<Fence>& fence); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 197 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 198 | // connect attempts to connect a producer API to the BufferQueue. This |
| 199 | // must be called before any other IGraphicBufferProducer methods are |
| 200 | // called except for getAllocator. A consumer must already be connected. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 201 | // |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 202 | // This method will fail if connect was previously called on the |
| 203 | // BufferQueue and no corresponding disconnect call was made (i.e. if |
| 204 | // it's still connected to a producer). |
| 205 | // |
| 206 | // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU). |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 207 | virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 208 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 209 | // disconnect attempts to disconnect a producer API from the BufferQueue. |
| 210 | // Calling this method will cause any subsequent calls to other |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 211 | // IGraphicBufferProducer methods to fail except for getAllocator and connect. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 212 | // Successfully calling connect after this will allow the other methods to |
| 213 | // succeed again. |
| 214 | // |
| 215 | // This method will fail if the the BufferQueue is not currently |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 216 | // connected to the specified producer API. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 217 | virtual status_t disconnect(int api); |
| 218 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 219 | // dump our state in a String |
| 220 | virtual void dump(String8& result) const; |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 221 | virtual void dump(String8& result, const char* prefix) const; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 222 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 223 | // public facing structure for BufferSlot |
| 224 | struct BufferItem { |
| 225 | |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 226 | BufferItem() : |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 227 | mTransform(0), |
| 228 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 229 | mTimestamp(0), |
| 230 | mFrameNumber(0), |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 231 | mBuf(INVALID_BUFFER_SLOT), |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 232 | mIsDroppable(false), |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 233 | mAcquireCalled(false) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 234 | mCrop.makeInvalid(); |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 235 | } |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 236 | // mGraphicBuffer points to the buffer allocated for this slot, or is NULL |
| 237 | // if the buffer in this slot has been acquired in the past (see |
| 238 | // BufferSlot.mAcquireCalled). |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 239 | sp<GraphicBuffer> mGraphicBuffer; |
| 240 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 241 | // mCrop is the current crop rectangle for this buffer slot. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 242 | Rect mCrop; |
| 243 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 244 | // mTransform is the current transform flags for this buffer slot. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 245 | uint32_t mTransform; |
| 246 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 247 | // mScalingMode is the current scaling mode for this buffer slot. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 248 | uint32_t mScalingMode; |
| 249 | |
| 250 | // mTimestamp is the current timestamp for this buffer slot. This gets |
| 251 | // to set by queueBuffer each time this slot is queued. |
| 252 | int64_t mTimestamp; |
| 253 | |
| 254 | // mFrameNumber is the number of the queued frame for this slot. |
| 255 | uint64_t mFrameNumber; |
| 256 | |
Jamie Gennis | efc7ab6 | 2012-04-17 19:36:18 -0700 | [diff] [blame] | 257 | // mBuf is the slot index of this buffer |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 258 | int mBuf; |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 259 | |
| 260 | // mFence is a fence that will signal when the buffer is idle. |
| 261 | sp<Fence> mFence; |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 262 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 263 | // mIsDroppable whether this buffer was queued with the |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 264 | // property that it can be replaced by a new buffer for the purpose of |
| 265 | // making sure dequeueBuffer() won't block. |
| 266 | // i.e.: was the BufferQueue in "mDequeueBufferCannotBlock" when this buffer |
| 267 | // was queued. |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 268 | bool mIsDroppable; |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 269 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 270 | // Indicates whether this buffer has been seen by a consumer yet |
| 271 | bool mAcquireCalled; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 272 | }; |
| 273 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 274 | // The following public functions are the consumer-facing interface |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 275 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 276 | // acquireBuffer attempts to acquire ownership of the next pending buffer in |
| 277 | // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a |
| 278 | // buffer is successfully acquired, the information about the buffer is |
| 279 | // returned in BufferItem. If the buffer returned had previously been |
| 280 | // acquired then the BufferItem::mGraphicBuffer field of buffer is set to |
| 281 | // NULL and it is assumed that the consumer still holds a reference to the |
| 282 | // buffer. |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 283 | // |
| 284 | // If presentWhen is nonzero, it indicates the time when the buffer will |
| 285 | // be displayed on screen. If the buffer's timestamp is farther in the |
| 286 | // future, the buffer won't be acquired, and PRESENT_LATER will be |
| 287 | // returned. The presentation time is in nanoseconds, and the time base |
| 288 | // is CLOCK_MONOTONIC. |
| 289 | status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 290 | |
| 291 | // releaseBuffer releases a buffer slot from the consumer back to the |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 292 | // BufferQueue. This may be done while the buffer's contents are still |
| 293 | // being accessed. The fence will signal when the buffer is no longer |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 294 | // in use. frameNumber is used to indentify the exact buffer returned. |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 295 | // |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 296 | // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free |
| 297 | // any references to the just-released buffer that it might have, as if it |
| 298 | // had received a onBuffersReleased() call with a mask set for the released |
| 299 | // buffer. |
| 300 | // |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 301 | // Note that the dependencies on EGL will be removed once we switch to using |
| 302 | // the Android HW Sync HAL. |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 303 | status_t releaseBuffer(int buf, uint64_t frameNumber, |
| 304 | EGLDisplay display, EGLSyncKHR fence, |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 305 | const sp<Fence>& releaseFence); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 306 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 307 | // consumerConnect connects a consumer to the BufferQueue. Only one |
| 308 | // consumer may be connected, and when that consumer disconnects the |
| 309 | // BufferQueue is placed into the "abandoned" state, causing most |
| 310 | // interactions with the BufferQueue by the producer to fail. |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 311 | // controlledByApp indicates whether the consumer is controlled by |
| 312 | // the application. |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 313 | // |
| 314 | // consumer may not be NULL. |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 315 | status_t consumerConnect(const sp<ConsumerListener>& consumer, bool controlledByApp); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 316 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 317 | // consumerDisconnect disconnects a consumer from the BufferQueue. All |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 318 | // buffers will be freed and the BufferQueue is placed in the "abandoned" |
| 319 | // state, causing most interactions with the BufferQueue by the producer to |
| 320 | // fail. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 321 | status_t consumerDisconnect(); |
| 322 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 323 | // getReleasedBuffers sets the value pointed to by slotMask to a bit mask |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 324 | // indicating which buffer slots have been released by the BufferQueue |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 325 | // but have not yet been released by the consumer. |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 326 | // |
| 327 | // This should be called from the onBuffersReleased() callback. |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 328 | status_t getReleasedBuffers(uint32_t* slotMask); |
| 329 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 330 | // setDefaultBufferSize is used to set the size of buffers returned by |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 331 | // dequeueBuffer when a width and height of zero is requested. Default |
| 332 | // is 1x1. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 333 | status_t setDefaultBufferSize(uint32_t w, uint32_t h); |
| 334 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 335 | // setDefaultMaxBufferCount sets the default value for the maximum buffer |
| 336 | // count (the initial default is 2). If the producer has requested a |
| 337 | // buffer count using setBufferCount, the default buffer count will only |
| 338 | // take effect if the producer sets the count back to zero. |
| 339 | // |
| 340 | // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 341 | status_t setDefaultMaxBufferCount(int bufferCount); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 342 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame^] | 343 | // disableAsyncBuffer disables the extra buffer used in async mode |
| 344 | // (when both producer and consumer have set their "isControlledByApp" |
| 345 | // flag) and has dequeueBuffer() return WOULD_BLOCK instead. |
| 346 | // |
| 347 | // This can only be called before consumerConnect(). |
| 348 | status_t disableAsyncBuffer(); |
| 349 | |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 350 | // setMaxAcquiredBufferCount sets the maximum number of buffers that can |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 351 | // be acquired by the consumer at one time (default 1). This call will |
| 352 | // fail if a producer is connected to the BufferQueue. |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 353 | status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers); |
| 354 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 355 | // setConsumerName sets the name used in logging |
| 356 | void setConsumerName(const String8& name); |
| 357 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 358 | // setDefaultBufferFormat allows the BufferQueue to create |
| 359 | // GraphicBuffers of a defaultFormat if no format is specified |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 360 | // in dequeueBuffer. Formats are enumerated in graphics.h; the |
| 361 | // initial default is HAL_PIXEL_FORMAT_RGBA_8888. |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 362 | status_t setDefaultBufferFormat(uint32_t defaultFormat); |
| 363 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 364 | // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. |
| 365 | // These are merged with the bits passed to dequeueBuffer. The values are |
| 366 | // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0. |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 367 | status_t setConsumerUsageBits(uint32_t usage); |
| 368 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 369 | // setTransformHint bakes in rotation to buffers so overlays can be used. |
| 370 | // The values are enumerated in window.h, e.g. |
| 371 | // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform). |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 372 | status_t setTransformHint(uint32_t hint); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 373 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame^] | 374 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 375 | private: |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 376 | // freeBufferLocked frees the GraphicBuffer and sync resources for the |
| 377 | // given slot. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 378 | void freeBufferLocked(int index); |
| 379 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 380 | // freeAllBuffersLocked frees the GraphicBuffer and sync resources for |
| 381 | // all slots. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 382 | void freeAllBuffersLocked(); |
| 383 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 384 | // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots |
| 385 | // that will be used if the producer does not override the buffer slot |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 386 | // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. |
| 387 | // The initial default is 2. |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 388 | status_t setDefaultMaxBufferCountLocked(int count); |
| 389 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 390 | // getMinUndequeuedBufferCount returns the minimum number of buffers |
| 391 | // that must remain in a state other than DEQUEUED. |
| 392 | // The async parameter tells whether we're in asynchronous mode. |
| 393 | int getMinUndequeuedBufferCount(bool async) const; |
| 394 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 395 | // getMinBufferCountLocked returns the minimum number of buffers allowed |
| 396 | // given the current BufferQueue state. |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 397 | // The async parameter tells whether we're in asynchronous mode. |
| 398 | int getMinMaxBufferCountLocked(bool async) const; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 399 | |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 400 | // getMaxBufferCountLocked returns the maximum number of buffers that can |
| 401 | // be allocated at once. This value depends upon the following member |
| 402 | // variables: |
| 403 | // |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 404 | // mDequeueBufferCannotBlock |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 405 | // mMaxAcquiredBufferCount |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 406 | // mDefaultMaxBufferCount |
| 407 | // mOverrideMaxBufferCount |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 408 | // async parameter |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 409 | // |
| 410 | // Any time one of these member variables is changed while a producer is |
| 411 | // connected, mDequeueCondition must be broadcast. |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 412 | int getMaxBufferCountLocked(bool async) const; |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 413 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 414 | // stillTracking returns true iff the buffer item is still being tracked |
| 415 | // in one of the slots. |
| 416 | bool stillTracking(const BufferItem *item) const; |
| 417 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 418 | struct BufferSlot { |
| 419 | |
| 420 | BufferSlot() |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 421 | : mEglDisplay(EGL_NO_DISPLAY), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 422 | mBufferState(BufferSlot::FREE), |
| 423 | mRequestBufferCalled(false), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 424 | mFrameNumber(0), |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 425 | mEglFence(EGL_NO_SYNC_KHR), |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 426 | mAcquireCalled(false), |
| 427 | mNeedsCleanupOnRelease(false) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
| 431 | // if no buffer has been allocated. |
| 432 | sp<GraphicBuffer> mGraphicBuffer; |
| 433 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 434 | // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 435 | EGLDisplay mEglDisplay; |
| 436 | |
| 437 | // BufferState represents the different states in which a buffer slot |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 438 | // can be. All slots are initially FREE. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 439 | enum BufferState { |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 440 | // FREE indicates that the buffer is available to be dequeued |
| 441 | // by the producer. The buffer may be in use by the consumer for |
| 442 | // a finite time, so the buffer must not be modified until the |
| 443 | // associated fence is signaled. |
| 444 | // |
| 445 | // The slot is "owned" by BufferQueue. It transitions to DEQUEUED |
| 446 | // when dequeueBuffer is called. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 447 | FREE = 0, |
| 448 | |
| 449 | // DEQUEUED indicates that the buffer has been dequeued by the |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 450 | // producer, but has not yet been queued or canceled. The |
| 451 | // producer may modify the buffer's contents as soon as the |
| 452 | // associated ready fence is signaled. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 453 | // |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 454 | // The slot is "owned" by the producer. It can transition to |
| 455 | // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer). |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 456 | DEQUEUED = 1, |
| 457 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 458 | // QUEUED indicates that the buffer has been filled by the |
| 459 | // producer and queued for use by the consumer. The buffer |
| 460 | // contents may continue to be modified for a finite time, so |
| 461 | // the contents must not be accessed until the associated fence |
| 462 | // is signaled. |
| 463 | // |
| 464 | // The slot is "owned" by BufferQueue. It can transition to |
| 465 | // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is |
| 466 | // queued in asynchronous mode). |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 467 | QUEUED = 2, |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 468 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 469 | // ACQUIRED indicates that the buffer has been acquired by the |
| 470 | // consumer. As with QUEUED, the contents must not be accessed |
| 471 | // by the consumer until the fence is signaled. |
| 472 | // |
| 473 | // The slot is "owned" by the consumer. It transitions to FREE |
| 474 | // when releaseBuffer is called. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 475 | ACQUIRED = 3 |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | // mBufferState is the current state of this buffer slot. |
| 479 | BufferState mBufferState; |
| 480 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 481 | // mRequestBufferCalled is used for validating that the producer did |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 482 | // call requestBuffer() when told to do so. Technically this is not |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 483 | // needed but useful for debugging and catching producer bugs. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 484 | bool mRequestBufferCalled; |
| 485 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 486 | // mFrameNumber is the number of the queued frame for this slot. This |
| 487 | // is used to dequeue buffers in LRU order (useful because buffers |
| 488 | // may be released before their release fence is signaled). |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 489 | uint64_t mFrameNumber; |
| 490 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 491 | // mEglFence is the EGL sync object that must signal before the buffer |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 492 | // associated with this buffer slot may be dequeued. It is initialized |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 493 | // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a |
| 494 | // new sync object in releaseBuffer. (This is deprecated in favor of |
| 495 | // mFence, below.) |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 496 | EGLSyncKHR mEglFence; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 497 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 498 | // mFence is a fence which will signal when work initiated by the |
| 499 | // previous owner of the buffer is finished. When the buffer is FREE, |
| 500 | // the fence indicates when the consumer has finished reading |
| 501 | // from the buffer, or when the producer has finished writing if it |
| 502 | // called cancelBuffer after queueing some writes. When the buffer is |
| 503 | // QUEUED, it indicates when the producer has finished filling the |
| 504 | // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been |
| 505 | // passed to the consumer or producer along with ownership of the |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 506 | // buffer, and mFence is set to NO_FENCE. |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 507 | sp<Fence> mFence; |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 508 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 509 | // Indicates whether this buffer has been seen by a consumer yet |
| 510 | bool mAcquireCalled; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 511 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 512 | // Indicates whether this buffer needs to be cleaned up by the |
| 513 | // consumer. This is set when a buffer in ACQUIRED state is freed. |
| 514 | // It causes releaseBuffer to return STALE_BUFFER_SLOT. |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 515 | bool mNeedsCleanupOnRelease; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 516 | }; |
| 517 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 518 | // mSlots is the array of buffer slots that must be mirrored on the |
| 519 | // producer side. This allows buffer ownership to be transferred between |
| 520 | // the producer and consumer without sending a GraphicBuffer over binder. |
| 521 | // The entire array is initialized to NULL at construction time, and |
| 522 | // buffers are allocated for a slot when requestBuffer is called with |
| 523 | // that slot's index. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 524 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
| 525 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 526 | // mDefaultWidth holds the default width of allocated buffers. It is used |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 527 | // in dequeueBuffer() if a width and height of zero is specified. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 528 | uint32_t mDefaultWidth; |
| 529 | |
| 530 | // mDefaultHeight holds the default height of allocated buffers. It is used |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 531 | // in dequeueBuffer() if a width and height of zero is specified. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 532 | uint32_t mDefaultHeight; |
| 533 | |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 534 | // mMaxAcquiredBufferCount is the number of buffers that the consumer may |
| 535 | // acquire at one time. It defaults to 1 and can be changed by the |
| 536 | // consumer via the setMaxAcquiredBufferCount method, but this may only be |
| 537 | // done when no producer is connected to the BufferQueue. |
| 538 | // |
| 539 | // This value is used to derive the value returned for the |
| 540 | // MIN_UNDEQUEUED_BUFFERS query by the producer. |
| 541 | int mMaxAcquiredBufferCount; |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 542 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 543 | // mDefaultMaxBufferCount is the default limit on the number of buffers |
| 544 | // that will be allocated at one time. This default limit is set by the |
| 545 | // consumer. The limit (as opposed to the default limit) may be |
| 546 | // overridden by the producer. |
| 547 | int mDefaultMaxBufferCount; |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 548 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 549 | // mOverrideMaxBufferCount is the limit on the number of buffers that will |
| 550 | // be allocated at one time. This value is set by the image producer by |
| 551 | // calling setBufferCount. The default is zero, which means the producer |
| 552 | // doesn't care about the number of buffers in the pool. In that case |
| 553 | // mDefaultMaxBufferCount is used as the limit. |
| 554 | int mOverrideMaxBufferCount; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 555 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 556 | // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to |
| 557 | // allocate new GraphicBuffer objects. |
| 558 | sp<IGraphicBufferAlloc> mGraphicBufferAlloc; |
| 559 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 560 | // mConsumerListener is used to notify the connected consumer of |
| 561 | // asynchronous events that it may wish to react to. It is initially set |
| 562 | // to NULL and is written by consumerConnect and consumerDisconnect. |
| 563 | sp<ConsumerListener> mConsumerListener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 564 | |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 565 | // mConsumerControlledByApp whether the connected consumer is controlled by the |
| 566 | // application. |
| 567 | bool mConsumerControlledByApp; |
| 568 | |
| 569 | // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block. |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame^] | 570 | // this flag is set during connect() when both consumer and producer are controlled |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 571 | // by the application. |
| 572 | bool mDequeueBufferCannotBlock; |
| 573 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame^] | 574 | // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent |
| 575 | // dequeueBuffer() from ever blocking. |
| 576 | bool mUseAsyncBuffer; |
| 577 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 578 | // mConnectedApi indicates the producer API that is currently connected |
| 579 | // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets |
| 580 | // updated by the connect and disconnect methods. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 581 | int mConnectedApi; |
| 582 | |
| 583 | // mDequeueCondition condition used for dequeueBuffer in synchronous mode |
| 584 | mutable Condition mDequeueCondition; |
| 585 | |
| 586 | // mQueue is a FIFO of queued buffers used in synchronous mode |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 587 | typedef Vector<BufferItem> Fifo; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 588 | Fifo mQueue; |
| 589 | |
| 590 | // mAbandoned indicates that the BufferQueue will no longer be used to |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 591 | // consume image buffers pushed to it using the IGraphicBufferProducer |
| 592 | // interface. It is initialized to false, and set to true in the |
| 593 | // consumerDisconnect method. A BufferQueue that has been abandoned will |
| 594 | // return the NO_INIT error from all IGraphicBufferProducer methods |
| 595 | // capable of returning an error. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 596 | bool mAbandoned; |
| 597 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 598 | // mConsumerName is a string used to identify the BufferQueue in log |
| 599 | // messages. It is set by the setConsumerName method. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 600 | String8 mConsumerName; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 601 | |
| 602 | // mMutex is the mutex used to prevent concurrent access to the member |
| 603 | // variables of BufferQueue objects. It must be locked whenever the |
| 604 | // member variables are accessed. |
| 605 | mutable Mutex mMutex; |
| 606 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 607 | // mFrameCounter is the free running counter, incremented on every |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 608 | // successful queueBuffer call, and buffer allocation. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 609 | uint64_t mFrameCounter; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 610 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 611 | // mBufferHasBeenQueued is true once a buffer has been queued. It is |
| 612 | // reset when something causes all buffers to be freed (e.g. changing the |
| 613 | // buffer count). |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 614 | bool mBufferHasBeenQueued; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 615 | |
| 616 | // mDefaultBufferFormat can be set so it will override |
| 617 | // the buffer format when it isn't specified in dequeueBuffer |
| 618 | uint32_t mDefaultBufferFormat; |
| 619 | |
| 620 | // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers |
| 621 | uint32_t mConsumerUsageBits; |
| 622 | |
| 623 | // mTransformHint is used to optimize for screen rotations |
| 624 | uint32_t mTransformHint; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | // ---------------------------------------------------------------------------- |
| 628 | }; // namespace android |
| 629 | |
| 630 | #endif // ANDROID_GUI_BUFFERQUEUE_H |