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