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