BufferQueue.h revision 399184a4cd728ea1421fb0bc1722274a29e38f4a
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 <gui/BufferQueueProducer.h> 21#include <gui/BufferQueueConsumer.h> 22#include <gui/IConsumerListener.h> 23 24// These are only required to keep other parts of the framework with incomplete 25// dependencies building successfully 26#include <gui/IGraphicBufferAlloc.h> 27 28#include <binder/IBinder.h> 29 30namespace android { 31// ---------------------------------------------------------------------------- 32 33class BufferQueue : public BnGraphicBufferProducer, 34 public BnGraphicBufferConsumer, 35 private IBinder::DeathRecipient { 36public: 37 // BufferQueue will keep track of at most this value of buffers. 38 // Attempts at runtime to increase the number of buffers past this will fail. 39 enum { NUM_BUFFER_SLOTS = 32 }; 40 // Used as a placeholder slot# when the value isn't pointing to an existing buffer. 41 enum { INVALID_BUFFER_SLOT = IGraphicBufferConsumer::BufferItem::INVALID_BUFFER_SLOT }; 42 // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code! 43 enum { 44 NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE, 45 PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER, 46 }; 47 48 // When in async mode we reserve two slots in order to guarantee that the 49 // producer and consumer can run asynchronously. 50 enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 }; 51 52 // for backward source compatibility 53 typedef ::android::ConsumerListener ConsumerListener; 54 55 // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak 56 // reference to the actual consumer object. It forwards all calls to that 57 // consumer object so long as it exists. 58 // 59 // This class exists to avoid having a circular reference between the 60 // BufferQueue object and the consumer object. The reason this can't be a weak 61 // reference in the BufferQueue class is because we're planning to expose the 62 // consumer side of a BufferQueue as a binder interface, which doesn't support 63 // weak references. 64 class ProxyConsumerListener : public BnConsumerListener { 65 public: 66 ProxyConsumerListener(const wp<ConsumerListener>& consumerListener); 67 virtual ~ProxyConsumerListener(); 68 virtual void onFrameAvailable(); 69 virtual void onBuffersReleased(); 70 virtual void onSidebandStreamChanged(); 71 private: 72 // mConsumerListener is a weak reference to the IConsumerListener. This is 73 // the raison d'etre of ProxyConsumerListener. 74 wp<ConsumerListener> mConsumerListener; 75 }; 76 77 // BufferQueue manages a pool of gralloc memory slots to be used by 78 // producers and consumers. allocator is used to allocate all the 79 // needed gralloc buffers. 80 BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL); 81 virtual ~BufferQueue(); 82 83 /* 84 * IBinder::DeathRecipient interface 85 */ 86 87 virtual void binderDied(const wp<IBinder>& who); 88 89 /* 90 * IGraphicBufferProducer interface 91 */ 92 93 // Query native window attributes. The "what" values are enumerated in 94 // window.h (e.g. NATIVE_WINDOW_FORMAT). 95 virtual int query(int what, int* value); 96 97 // setBufferCount updates the number of available buffer slots. If this 98 // method succeeds, buffer slots will be both unallocated and owned by 99 // the BufferQueue object (i.e. they are not owned by the producer or 100 // consumer). 101 // 102 // This will fail if the producer has dequeued any buffers, or if 103 // bufferCount is invalid. bufferCount must generally be a value 104 // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS 105 // (inclusive). It may also be set to zero (the default) to indicate 106 // that the producer does not wish to set a value. The minimum value 107 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, 108 // ...). 109 // 110 // This may only be called by the producer. The consumer will be told 111 // to discard buffers through the onBuffersReleased callback. 112 virtual status_t setBufferCount(int bufferCount); 113 114 // requestBuffer returns the GraphicBuffer for slot N. 115 // 116 // In normal operation, this is called the first time slot N is returned 117 // by dequeueBuffer. It must be called again if dequeueBuffer returns 118 // flags indicating that previously-returned buffers are no longer valid. 119 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); 120 121 // dequeueBuffer gets the next buffer slot index for the producer to use. 122 // If a buffer slot is available then that slot index is written to the 123 // location pointed to by the buf argument and a status of OK is returned. 124 // If no slot is available then a status of -EBUSY is returned and buf is 125 // unmodified. 126 // 127 // The fence parameter will be updated to hold the fence associated with 128 // the buffer. The contents of the buffer must not be overwritten until the 129 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be 130 // written immediately. 131 // 132 // The width and height parameters must be no greater than the minimum of 133 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). 134 // An error due to invalid dimensions might not be reported until 135 // updateTexImage() is called. If width and height are both zero, the 136 // default values specified by setDefaultBufferSize() are used instead. 137 // 138 // The pixel formats are enumerated in graphics.h, e.g. 139 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format 140 // will be used. 141 // 142 // The usage argument specifies gralloc buffer usage flags. The values 143 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These 144 // will be merged with the usage flags specified by setConsumerUsageBits. 145 // 146 // The return value may be a negative error value or a non-negative 147 // collection of flags. If the flags are set, the return values are 148 // valid, but additional actions must be performed. 149 // 150 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the 151 // producer must discard cached GraphicBuffer references for the slot 152 // returned in buf. 153 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer 154 // must discard cached GraphicBuffer references for all slots. 155 // 156 // In both cases, the producer will need to call requestBuffer to get a 157 // GraphicBuffer handle for the returned slot. 158 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async, 159 uint32_t width, uint32_t height, uint32_t format, uint32_t usage); 160 161 // queueBuffer returns a filled buffer to the BufferQueue. 162 // 163 // Additional data is provided in the QueueBufferInput struct. Notably, 164 // a timestamp must be provided for the buffer. The timestamp is in 165 // nanoseconds, and must be monotonically increasing. Its other semantics 166 // (zero point, etc) are producer-specific and should be documented by the 167 // producer. 168 // 169 // The caller may provide a fence that signals when all rendering 170 // operations have completed. Alternatively, NO_FENCE may be used, 171 // indicating that the buffer is ready immediately. 172 // 173 // Some values are returned in the output struct: the current settings 174 // for default width and height, the current transform hint, and the 175 // number of queued buffers. 176 virtual status_t queueBuffer(int buf, 177 const QueueBufferInput& input, QueueBufferOutput* output); 178 179 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't 180 // queue it for use by the consumer. 181 // 182 // The buffer will not be overwritten until the fence signals. The fence 183 // will usually be the one obtained from dequeueBuffer. 184 virtual void cancelBuffer(int buf, const sp<Fence>& fence); 185 186 // connect attempts to connect a producer API to the BufferQueue. This 187 // must be called before any other IGraphicBufferProducer methods are 188 // called except for getAllocator. A consumer must already be connected. 189 // 190 // This method will fail if connect was previously called on the 191 // BufferQueue and no corresponding disconnect call was made (i.e. if 192 // it's still connected to a producer). 193 // 194 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU). 195 virtual status_t connect(const sp<IBinder>& token, 196 int api, bool producerControlledByApp, QueueBufferOutput* output); 197 198 // disconnect attempts to disconnect a producer API from the BufferQueue. 199 // Calling this method will cause any subsequent calls to other 200 // IGraphicBufferProducer methods to fail except for getAllocator and connect. 201 // Successfully calling connect after this will allow the other methods to 202 // succeed again. 203 // 204 // This method will fail if the the BufferQueue is not currently 205 // connected to the specified producer API. 206 virtual status_t disconnect(int api); 207 208 // Attaches a sideband buffer stream to the BufferQueue. 209 // 210 // A sideband stream is a device-specific mechanism for passing buffers 211 // from the producer to the consumer without using dequeueBuffer/ 212 // queueBuffer. If a sideband stream is present, the consumer can choose 213 // whether to acquire buffers from the sideband stream or from the queued 214 // buffers. 215 // 216 // Passing NULL or a different stream handle will detach the previous 217 // handle if any. 218 virtual status_t setSidebandStream(const sp<NativeHandle>& stream); 219 220 /* 221 * IGraphicBufferConsumer interface 222 */ 223 224 // acquireBuffer attempts to acquire ownership of the next pending buffer in 225 // the BufferQueue. If no buffer is pending then it returns NO_BUFFER_AVAILABLE. If a 226 // buffer is successfully acquired, the information about the buffer is 227 // returned in BufferItem. If the buffer returned had previously been 228 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to 229 // NULL and it is assumed that the consumer still holds a reference to the 230 // buffer. 231 // 232 // If presentWhen is nonzero, it indicates the time when the buffer will 233 // be displayed on screen. If the buffer's timestamp is farther in the 234 // future, the buffer won't be acquired, and PRESENT_LATER will be 235 // returned. The presentation time is in nanoseconds, and the time base 236 // is CLOCK_MONOTONIC. 237 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen); 238 239 // releaseBuffer releases a buffer slot from the consumer back to the 240 // BufferQueue. This may be done while the buffer's contents are still 241 // being accessed. The fence will signal when the buffer is no longer 242 // in use. frameNumber is used to indentify the exact buffer returned. 243 // 244 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free 245 // any references to the just-released buffer that it might have, as if it 246 // had received a onBuffersReleased() call with a mask set for the released 247 // buffer. 248 // 249 // Note that the dependencies on EGL will be removed once we switch to using 250 // the Android HW Sync HAL. 251 virtual status_t releaseBuffer(int buf, uint64_t frameNumber, 252 EGLDisplay display, EGLSyncKHR fence, 253 const sp<Fence>& releaseFence); 254 255 // consumerConnect connects a consumer to the BufferQueue. Only one 256 // consumer may be connected, and when that consumer disconnects the 257 // BufferQueue is placed into the "abandoned" state, causing most 258 // interactions with the BufferQueue by the producer to fail. 259 // controlledByApp indicates whether the consumer is controlled by 260 // the application. 261 // 262 // consumer may not be NULL. 263 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp); 264 265 // consumerDisconnect disconnects a consumer from the BufferQueue. All 266 // buffers will be freed and the BufferQueue is placed in the "abandoned" 267 // state, causing most interactions with the BufferQueue by the producer to 268 // fail. 269 virtual status_t consumerDisconnect(); 270 271 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask 272 // indicating which buffer slots have been released by the BufferQueue 273 // but have not yet been released by the consumer. 274 // 275 // This should be called from the onBuffersReleased() callback. 276 virtual status_t getReleasedBuffers(uint32_t* slotMask); 277 278 // setDefaultBufferSize is used to set the size of buffers returned by 279 // dequeueBuffer when a width and height of zero is requested. Default 280 // is 1x1. 281 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h); 282 283 // setDefaultMaxBufferCount sets the default value for the maximum buffer 284 // count (the initial default is 2). If the producer has requested a 285 // buffer count using setBufferCount, the default buffer count will only 286 // take effect if the producer sets the count back to zero. 287 // 288 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. 289 virtual status_t setDefaultMaxBufferCount(int bufferCount); 290 291 // disableAsyncBuffer disables the extra buffer used in async mode 292 // (when both producer and consumer have set their "isControlledByApp" 293 // flag) and has dequeueBuffer() return WOULD_BLOCK instead. 294 // 295 // This can only be called before consumerConnect(). 296 virtual status_t disableAsyncBuffer(); 297 298 // setMaxAcquiredBufferCount sets the maximum number of buffers that can 299 // be acquired by the consumer at one time (default 1). This call will 300 // fail if a producer is connected to the BufferQueue. 301 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers); 302 303 // setConsumerName sets the name used in logging 304 virtual void setConsumerName(const String8& name); 305 306 // setDefaultBufferFormat allows the BufferQueue to create 307 // GraphicBuffers of a defaultFormat if no format is specified 308 // in dequeueBuffer. Formats are enumerated in graphics.h; the 309 // initial default is HAL_PIXEL_FORMAT_RGBA_8888. 310 virtual status_t setDefaultBufferFormat(uint32_t defaultFormat); 311 312 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. 313 // These are merged with the bits passed to dequeueBuffer. The values are 314 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0. 315 virtual status_t setConsumerUsageBits(uint32_t usage); 316 317 // setTransformHint bakes in rotation to buffers so overlays can be used. 318 // The values are enumerated in window.h, e.g. 319 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform). 320 virtual status_t setTransformHint(uint32_t hint); 321 322 // Retrieve the BufferQueue's sideband stream, if any. 323 virtual sp<NativeHandle> getSidebandStream() const; 324 325 // dump our state in a String 326 virtual void dump(String8& result, const char* prefix) const; 327 328private: 329 sp<BufferQueueProducer> mProducer; 330 sp<BufferQueueConsumer> mConsumer; 331}; 332 333// ---------------------------------------------------------------------------- 334}; // namespace android 335 336#endif // ANDROID_GUI_BUFFERQUEUE_H 337