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