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> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 24 | #include <gui/ISurfaceTexture.h> |
| 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 | |
| 36 | class BufferQueue : public BnSurfaceTexture { |
| 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 | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 44 | // ConsumerListener is the interface through which the BufferQueue notifies |
| 45 | // the consumer of events that the consumer may wish to react to. Because |
| 46 | // the consumer will generally have a mutex that is locked during calls from |
| 47 | // teh consumer to the BufferQueue, these calls from the BufferQueue to the |
| 48 | // consumer *MUST* be called only when the BufferQueue mutex is NOT locked. |
| 49 | struct ConsumerListener : public virtual RefBase { |
| 50 | // onFrameAvailable is called from queueBuffer each time an additional |
| 51 | // frame becomes available for consumption. This means that frames that |
| 52 | // are queued while in asynchronous mode only trigger the callback if no |
| 53 | // previous frames are pending. Frames queued while in synchronous mode |
| 54 | // always trigger the callback. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 55 | // |
| 56 | // This is called without any lock held and can be called concurrently |
| 57 | // by multiple threads. |
| 58 | virtual void onFrameAvailable() = 0; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 59 | |
| 60 | // onBuffersReleased is called to notify the buffer consumer that the |
| 61 | // BufferQueue has released its references to one or more GraphicBuffers |
| 62 | // contained in its slots. The buffer consumer should then call |
| 63 | // BufferQueue::getReleasedBuffers to retrieve the list of buffers |
| 64 | // |
| 65 | // This is called without any lock held and can be called concurrently |
| 66 | // by multiple threads. |
| 67 | virtual void onBuffersReleased() = 0; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 70 | // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak |
| 71 | // reference to the actual consumer object. It forwards all calls to that |
| 72 | // consumer object so long as it exists. |
| 73 | // |
| 74 | // This class exists to avoid having a circular reference between the |
| 75 | // BufferQueue object and the consumer object. The reason this can't be a weak |
| 76 | // reference in the BufferQueue class is because we're planning to expose the |
| 77 | // consumer side of a BufferQueue as a binder interface, which doesn't support |
| 78 | // weak references. |
| 79 | class ProxyConsumerListener : public BufferQueue::ConsumerListener { |
| 80 | public: |
| 81 | |
| 82 | ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener); |
| 83 | virtual ~ProxyConsumerListener(); |
| 84 | virtual void onFrameAvailable(); |
| 85 | virtual void onBuffersReleased(); |
| 86 | |
| 87 | private: |
| 88 | |
| 89 | // mConsumerListener is a weak reference to the ConsumerListener. This is |
| 90 | // the raison d'etre of ProxyConsumerListener. |
| 91 | wp<BufferQueue::ConsumerListener> mConsumerListener; |
| 92 | }; |
| 93 | |
| 94 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 95 | // BufferQueue manages a pool of gralloc memory slots to be used |
| 96 | // by producers and consumers. |
| 97 | // allowSynchronousMode specifies whether or not synchronous mode can be |
| 98 | // enabled. |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 99 | // bufferCount sets the minimum number of undequeued buffers for this queue |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 100 | BufferQueue(bool allowSynchronousMode = true, |
| 101 | int bufferCount = MIN_UNDEQUEUED_BUFFERS, |
| 102 | const sp<IGraphicBufferAlloc>& allocator = NULL); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 103 | virtual ~BufferQueue(); |
| 104 | |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 105 | virtual int query(int what, int* value); |
| 106 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 107 | // setBufferCount updates the number of available buffer slots. After |
| 108 | // calling this all buffer slots are both unallocated and owned by the |
| 109 | // BufferQueue object (i.e. they are not owned by the client). |
| 110 | virtual status_t setBufferCount(int bufferCount); |
| 111 | |
| 112 | virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); |
| 113 | |
| 114 | // dequeueBuffer gets the next buffer slot index for the client to use. If a |
| 115 | // buffer slot is available then that slot index is written to the location |
| 116 | // pointed to by the buf argument and a status of OK is returned. If no |
| 117 | // slot is available then a status of -EBUSY is returned and buf is |
| 118 | // unmodified. |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 119 | // |
| 120 | // The fence parameter will be updated to hold the fence associated with |
| 121 | // the buffer. The contents of the buffer must not be overwritten until the |
| 122 | // fence signals. If the fence is NULL, the buffer may be written |
| 123 | // immediately. |
| 124 | // |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 125 | // The width and height parameters must be no greater than the minimum of |
| 126 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
| 127 | // An error due to invalid dimensions might not be reported until |
| 128 | // updateTexImage() is called. |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 129 | virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence, |
| 130 | uint32_t width, uint32_t height, uint32_t format, uint32_t usage); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 131 | |
| 132 | // queueBuffer returns a filled buffer to the BufferQueue. In addition, a |
| 133 | // timestamp must be provided for the buffer. The timestamp is in |
| 134 | // nanoseconds, and must be monotonically increasing. Its other semantics |
| 135 | // (zero point, etc) are client-dependent and should be documented by the |
| 136 | // client. |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 137 | virtual status_t queueBuffer(int buf, |
| 138 | const QueueBufferInput& input, QueueBufferOutput* output); |
| 139 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 140 | virtual void cancelBuffer(int buf, sp<Fence> fence); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 141 | |
| 142 | // setSynchronousMode set whether dequeueBuffer is synchronous or |
| 143 | // asynchronous. In synchronous mode, dequeueBuffer blocks until |
| 144 | // a buffer is available, the currently bound buffer can be dequeued and |
| 145 | // queued buffers will be retired in order. |
| 146 | // The default mode is asynchronous. |
| 147 | virtual status_t setSynchronousMode(bool enabled); |
| 148 | |
| 149 | // connect attempts to connect a producer client API to the BufferQueue. |
| 150 | // This must be called before any other ISurfaceTexture methods are called |
| 151 | // except for getAllocator. |
| 152 | // |
| 153 | // This method will fail if the connect was previously called on the |
| 154 | // BufferQueue and no corresponding disconnect call was made. |
Mathias Agopian | 24202f5 | 2012-04-23 14:28:58 -0700 | [diff] [blame] | 155 | virtual status_t connect(int api, QueueBufferOutput* output); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 156 | |
| 157 | // disconnect attempts to disconnect a producer client API from the |
| 158 | // BufferQueue. Calling this method will cause any subsequent calls to other |
| 159 | // ISurfaceTexture methods to fail except for getAllocator and connect. |
| 160 | // Successfully calling connect after this will allow the other methods to |
| 161 | // succeed again. |
| 162 | // |
| 163 | // This method will fail if the the BufferQueue is not currently |
| 164 | // connected to the specified client API. |
| 165 | virtual status_t disconnect(int api); |
| 166 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 167 | // dump our state in a String |
| 168 | virtual void dump(String8& result) const; |
| 169 | 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] | 170 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 171 | // public facing structure for BufferSlot |
| 172 | struct BufferItem { |
| 173 | |
| 174 | BufferItem() |
| 175 | : |
| 176 | mTransform(0), |
| 177 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 178 | mTimestamp(0), |
| 179 | mFrameNumber(0), |
| 180 | mBuf(INVALID_BUFFER_SLOT) { |
| 181 | mCrop.makeInvalid(); |
| 182 | } |
| 183 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
| 184 | // if no buffer has been allocated. |
| 185 | sp<GraphicBuffer> mGraphicBuffer; |
| 186 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 187 | // mCrop is the current crop rectangle for this buffer slot. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 188 | Rect mCrop; |
| 189 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 190 | // mTransform is the current transform flags for this buffer slot. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 191 | uint32_t mTransform; |
| 192 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 193 | // mScalingMode is the current scaling mode for this buffer slot. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 194 | uint32_t mScalingMode; |
| 195 | |
| 196 | // mTimestamp is the current timestamp for this buffer slot. This gets |
| 197 | // to set by queueBuffer each time this slot is queued. |
| 198 | int64_t mTimestamp; |
| 199 | |
| 200 | // mFrameNumber is the number of the queued frame for this slot. |
| 201 | uint64_t mFrameNumber; |
| 202 | |
Jamie Gennis | efc7ab6 | 2012-04-17 19:36:18 -0700 | [diff] [blame] | 203 | // mBuf is the slot index of this buffer |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 204 | int mBuf; |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 205 | |
| 206 | // mFence is a fence that will signal when the buffer is idle. |
| 207 | sp<Fence> mFence; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | // The following public functions is the consumer facing interface |
| 211 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 212 | // acquireBuffer attempts to acquire ownership of the next pending buffer in |
| 213 | // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a |
| 214 | // buffer is successfully acquired, the information about the buffer is |
| 215 | // returned in BufferItem. If the buffer returned had previously been |
| 216 | // acquired then the BufferItem::mGraphicBuffer field of buffer is set to |
| 217 | // NULL and it is assumed that the consumer still holds a reference to the |
| 218 | // buffer. |
| 219 | status_t acquireBuffer(BufferItem *buffer); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 220 | |
| 221 | // releaseBuffer releases a buffer slot from the consumer back to the |
| 222 | // BufferQueue pending a fence sync. |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 223 | // |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 224 | // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free |
| 225 | // any references to the just-released buffer that it might have, as if it |
| 226 | // had received a onBuffersReleased() call with a mask set for the released |
| 227 | // buffer. |
| 228 | // |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 229 | // Note that the dependencies on EGL will be removed once we switch to using |
| 230 | // the Android HW Sync HAL. |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 231 | status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence, |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 232 | const sp<Fence>& releaseFence); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 233 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 234 | // consumerConnect connects a consumer to the BufferQueue. Only one |
| 235 | // consumer may be connected, and when that consumer disconnects the |
| 236 | // BufferQueue is placed into the "abandoned" state, causing most |
| 237 | // interactions with the BufferQueue by the producer to fail. |
| 238 | status_t consumerConnect(const sp<ConsumerListener>& consumer); |
| 239 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 240 | // consumerDisconnect disconnects a consumer from the BufferQueue. All |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 241 | // buffers will be freed and the BufferQueue is placed in the "abandoned" |
| 242 | // state, causing most interactions with the BufferQueue by the producer to |
| 243 | // fail. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 244 | status_t consumerDisconnect(); |
| 245 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 246 | // getReleasedBuffers sets the value pointed to by slotMask to a bit mask |
| 247 | // indicating which buffer slots the have been released by the BufferQueue |
| 248 | // but have not yet been released by the consumer. |
| 249 | status_t getReleasedBuffers(uint32_t* slotMask); |
| 250 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 251 | // setDefaultBufferSize is used to set the size of buffers returned by |
| 252 | // requestBuffers when a with and height of zero is requested. |
| 253 | status_t setDefaultBufferSize(uint32_t w, uint32_t h); |
| 254 | |
| 255 | // setBufferCountServer set the buffer count. If the client has requested |
| 256 | // a buffer count using setBufferCount, the server-buffer count will |
| 257 | // take effect once the client sets the count back to zero. |
| 258 | status_t setBufferCountServer(int bufferCount); |
| 259 | |
| 260 | // isSynchronousMode returns whether the SurfaceTexture is currently in |
| 261 | // synchronous mode. |
| 262 | bool isSynchronousMode() const; |
| 263 | |
| 264 | // setConsumerName sets the name used in logging |
| 265 | void setConsumerName(const String8& name); |
| 266 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 267 | // setDefaultBufferFormat allows the BufferQueue to create |
| 268 | // GraphicBuffers of a defaultFormat if no format is specified |
| 269 | // in dequeueBuffer |
| 270 | status_t setDefaultBufferFormat(uint32_t defaultFormat); |
| 271 | |
| 272 | // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer |
| 273 | status_t setConsumerUsageBits(uint32_t usage); |
| 274 | |
| 275 | // setTransformHint bakes in rotation to buffers so overlays can be used |
| 276 | status_t setTransformHint(uint32_t hint); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 277 | |
| 278 | private: |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 279 | // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage) |
| 280 | // for the given slot. |
| 281 | void freeBufferLocked(int index); |
| 282 | |
| 283 | // freeAllBuffersLocked frees the resources (both GraphicBuffer and |
| 284 | // EGLImage) for all slots. |
| 285 | void freeAllBuffersLocked(); |
| 286 | |
| 287 | // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer |
| 288 | // and EGLImage) for all slots except the head of mQueue |
| 289 | void freeAllBuffersExceptHeadLocked(); |
| 290 | |
| 291 | // drainQueueLocked drains the buffer queue if we're in synchronous mode |
| 292 | // returns immediately otherwise. It returns NO_INIT if the BufferQueue |
| 293 | // became abandoned or disconnected during this call. |
| 294 | status_t drainQueueLocked(); |
| 295 | |
| 296 | // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in |
| 297 | // synchronous mode and free all buffers. In asynchronous mode, all buffers |
| 298 | // are freed except the current buffer. |
| 299 | status_t drainQueueAndFreeBuffersLocked(); |
| 300 | |
| 301 | status_t setBufferCountServerLocked(int bufferCount); |
| 302 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 303 | struct BufferSlot { |
| 304 | |
| 305 | BufferSlot() |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 306 | : mEglDisplay(EGL_NO_DISPLAY), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 307 | mBufferState(BufferSlot::FREE), |
| 308 | mRequestBufferCalled(false), |
| 309 | mTransform(0), |
| 310 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 311 | mTimestamp(0), |
| 312 | mFrameNumber(0), |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 313 | mEglFence(EGL_NO_SYNC_KHR), |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 314 | mAcquireCalled(false), |
| 315 | mNeedsCleanupOnRelease(false) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 316 | mCrop.makeInvalid(); |
| 317 | } |
| 318 | |
| 319 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
| 320 | // if no buffer has been allocated. |
| 321 | sp<GraphicBuffer> mGraphicBuffer; |
| 322 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 323 | // mEglDisplay is the EGLDisplay used to create mEglImage. |
| 324 | EGLDisplay mEglDisplay; |
| 325 | |
| 326 | // BufferState represents the different states in which a buffer slot |
| 327 | // can be. |
| 328 | enum BufferState { |
| 329 | // FREE indicates that the buffer is not currently being used and |
| 330 | // will not be used in the future until it gets dequeued and |
| 331 | // subsequently queued by the client. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 332 | // aka "owned by BufferQueue, ready to be dequeued" |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 333 | FREE = 0, |
| 334 | |
| 335 | // DEQUEUED indicates that the buffer has been dequeued by the |
| 336 | // client, but has not yet been queued or canceled. The buffer is |
| 337 | // considered 'owned' by the client, and the server should not use |
| 338 | // it for anything. |
| 339 | // |
| 340 | // Note that when in synchronous-mode (mSynchronousMode == true), |
| 341 | // the buffer that's currently attached to the texture may be |
| 342 | // dequeued by the client. That means that the current buffer can |
| 343 | // be in either the DEQUEUED or QUEUED state. In asynchronous mode, |
| 344 | // however, the current buffer is always in the QUEUED state. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 345 | // aka "owned by producer, ready to be queued" |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 346 | DEQUEUED = 1, |
| 347 | |
| 348 | // QUEUED indicates that the buffer has been queued by the client, |
| 349 | // and has not since been made available for the client to dequeue. |
| 350 | // Attaching the buffer to the texture does NOT transition the |
| 351 | // buffer away from the QUEUED state. However, in Synchronous mode |
| 352 | // the current buffer may be dequeued by the client under some |
| 353 | // circumstances. See the note about the current buffer in the |
| 354 | // documentation for DEQUEUED. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 355 | // aka "owned by BufferQueue, ready to be acquired" |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 356 | QUEUED = 2, |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 357 | |
| 358 | // aka "owned by consumer, ready to be released" |
| 359 | ACQUIRED = 3 |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | // mBufferState is the current state of this buffer slot. |
| 363 | BufferState mBufferState; |
| 364 | |
| 365 | // mRequestBufferCalled is used for validating that the client did |
| 366 | // call requestBuffer() when told to do so. Technically this is not |
| 367 | // needed but useful for debugging and catching client bugs. |
| 368 | bool mRequestBufferCalled; |
| 369 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 370 | // mCrop is the current crop rectangle for this buffer slot. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 371 | Rect mCrop; |
| 372 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 373 | // mTransform is the current transform flags for this buffer slot. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 374 | uint32_t mTransform; |
| 375 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 376 | // mScalingMode is the current scaling mode for this buffer slot. |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 377 | uint32_t mScalingMode; |
| 378 | |
| 379 | // mTimestamp is the current timestamp for this buffer slot. This gets |
| 380 | // to set by queueBuffer each time this slot is queued. |
| 381 | int64_t mTimestamp; |
| 382 | |
| 383 | // mFrameNumber is the number of the queued frame for this slot. |
| 384 | uint64_t mFrameNumber; |
| 385 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 386 | // mEglFence is the EGL sync object that must signal before the buffer |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 387 | // associated with this buffer slot may be dequeued. It is initialized |
| 388 | // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based |
| 389 | // on a compile-time option) set to a new sync object in updateTexImage. |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 390 | EGLSyncKHR mEglFence; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 391 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 392 | // mFence is a fence which will signal when work initiated by the |
| 393 | // previous owner of the buffer is finished. When the buffer is FREE, |
| 394 | // the fence indicates when the consumer has finished reading |
| 395 | // from the buffer, or when the producer has finished writing if it |
| 396 | // called cancelBuffer after queueing some writes. When the buffer is |
| 397 | // QUEUED, it indicates when the producer has finished filling the |
| 398 | // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been |
| 399 | // passed to the consumer or producer along with ownership of the |
| 400 | // buffer, and mFence is empty. |
| 401 | sp<Fence> mFence; |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 402 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 403 | // Indicates whether this buffer has been seen by a consumer yet |
| 404 | bool mAcquireCalled; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 405 | |
| 406 | // Indicates whether this buffer needs to be cleaned up by consumer |
| 407 | bool mNeedsCleanupOnRelease; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | // mSlots is the array of buffer slots that must be mirrored on the client |
| 411 | // side. This allows buffer ownership to be transferred between the client |
| 412 | // and server without sending a GraphicBuffer over binder. The entire array |
| 413 | // is initialized to NULL at construction time, and buffers are allocated |
| 414 | // for a slot when requestBuffer is called with that slot's index. |
| 415 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
| 416 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 417 | // mDefaultWidth holds the default width of allocated buffers. It is used |
| 418 | // in requestBuffers() if a width and height of zero is specified. |
| 419 | uint32_t mDefaultWidth; |
| 420 | |
| 421 | // mDefaultHeight holds the default height of allocated buffers. It is used |
| 422 | // in requestBuffers() if a width and height of zero is specified. |
| 423 | uint32_t mDefaultHeight; |
| 424 | |
| 425 | // mPixelFormat holds the pixel format of allocated buffers. It is used |
| 426 | // in requestBuffers() if a format of zero is specified. |
| 427 | uint32_t mPixelFormat; |
| 428 | |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 429 | // mMinUndequeuedBuffers is a constraint on the number of buffers |
| 430 | // not dequeued at any time |
| 431 | int mMinUndequeuedBuffers; |
| 432 | |
| 433 | // mMinAsyncBufferSlots is a constraint on the minimum mBufferCount |
| 434 | // when this BufferQueue is in asynchronous mode |
| 435 | int mMinAsyncBufferSlots; |
| 436 | |
| 437 | // mMinSyncBufferSlots is a constraint on the minimum mBufferCount |
| 438 | // when this BufferQueue is in synchronous mode |
| 439 | int mMinSyncBufferSlots; |
| 440 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 441 | // mBufferCount is the number of buffer slots that the client and server |
| 442 | // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed |
| 443 | // by calling setBufferCount or setBufferCountServer |
| 444 | int mBufferCount; |
| 445 | |
| 446 | // mClientBufferCount is the number of buffer slots requested by the client. |
| 447 | // The default is zero, which means the client doesn't care how many buffers |
| 448 | // there is. |
| 449 | int mClientBufferCount; |
| 450 | |
| 451 | // mServerBufferCount buffer count requested by the server-side |
| 452 | int mServerBufferCount; |
| 453 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 454 | // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to |
| 455 | // allocate new GraphicBuffer objects. |
| 456 | sp<IGraphicBufferAlloc> mGraphicBufferAlloc; |
| 457 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 458 | // mConsumerListener is used to notify the connected consumer of |
| 459 | // asynchronous events that it may wish to react to. It is initially set |
| 460 | // to NULL and is written by consumerConnect and consumerDisconnect. |
| 461 | sp<ConsumerListener> mConsumerListener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 462 | |
| 463 | // mSynchronousMode whether we're in synchronous mode or not |
| 464 | bool mSynchronousMode; |
| 465 | |
| 466 | // mAllowSynchronousMode whether we allow synchronous mode or not |
| 467 | const bool mAllowSynchronousMode; |
| 468 | |
| 469 | // mConnectedApi indicates the API that is currently connected to this |
| 470 | // BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets updated |
| 471 | // by the connect and disconnect methods. |
| 472 | int mConnectedApi; |
| 473 | |
| 474 | // mDequeueCondition condition used for dequeueBuffer in synchronous mode |
| 475 | mutable Condition mDequeueCondition; |
| 476 | |
| 477 | // mQueue is a FIFO of queued buffers used in synchronous mode |
| 478 | typedef Vector<int> Fifo; |
| 479 | Fifo mQueue; |
| 480 | |
| 481 | // mAbandoned indicates that the BufferQueue will no longer be used to |
| 482 | // consume images buffers pushed to it using the ISurfaceTexture interface. |
| 483 | // It is initialized to false, and set to true in the abandon method. A |
| 484 | // BufferQueue that has been abandoned will return the NO_INIT error from |
| 485 | // all ISurfaceTexture methods capable of returning an error. |
| 486 | bool mAbandoned; |
| 487 | |
| 488 | // mName is a string used to identify the BufferQueue in log messages. |
| 489 | // It is set by the setName method. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 490 | String8 mConsumerName; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 491 | |
| 492 | // mMutex is the mutex used to prevent concurrent access to the member |
| 493 | // variables of BufferQueue objects. It must be locked whenever the |
| 494 | // member variables are accessed. |
| 495 | mutable Mutex mMutex; |
| 496 | |
| 497 | // mFrameCounter is the free running counter, incremented for every buffer queued |
| 498 | // with the surface Texture. |
| 499 | uint64_t mFrameCounter; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 500 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 501 | // mBufferHasBeenQueued is true once a buffer has been queued. It is reset |
| 502 | // by changing the buffer count. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 503 | bool mBufferHasBeenQueued; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 504 | |
| 505 | // mDefaultBufferFormat can be set so it will override |
| 506 | // the buffer format when it isn't specified in dequeueBuffer |
| 507 | uint32_t mDefaultBufferFormat; |
| 508 | |
| 509 | // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers |
| 510 | uint32_t mConsumerUsageBits; |
| 511 | |
| 512 | // mTransformHint is used to optimize for screen rotations |
| 513 | uint32_t mTransformHint; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | // ---------------------------------------------------------------------------- |
| 517 | }; // namespace android |
| 518 | |
| 519 | #endif // ANDROID_GUI_BUFFERQUEUE_H |