Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2012 The Android Open Source Project |
michael@0 | 3 | * Copyright (C) 2013 Mozilla Foundation |
michael@0 | 4 | * |
michael@0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 6 | * you may not use this file except in compliance with the License. |
michael@0 | 7 | * You may obtain a copy of the License at |
michael@0 | 8 | * |
michael@0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 10 | * |
michael@0 | 11 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 14 | * See the License for the specific language governing permissions and |
michael@0 | 15 | * limitations under the License. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #ifndef NATIVEWINDOW_GONKBUFFERQUEUE_JB_H |
michael@0 | 19 | #define NATIVEWINDOW_GONKBUFFERQUEUE_JB_H |
michael@0 | 20 | |
michael@0 | 21 | #include <gui/IGraphicBufferAlloc.h> |
michael@0 | 22 | #if ANDROID_VERSION == 17 |
michael@0 | 23 | #include <gui/ISurfaceTexture.h> |
michael@0 | 24 | #else |
michael@0 | 25 | #include <gui/IGraphicBufferProducer.h> |
michael@0 | 26 | #endif |
michael@0 | 27 | |
michael@0 | 28 | #include <ui/Fence.h> |
michael@0 | 29 | #include <ui/GraphicBuffer.h> |
michael@0 | 30 | |
michael@0 | 31 | #include <utils/String8.h> |
michael@0 | 32 | #include <utils/Vector.h> |
michael@0 | 33 | #include <utils/threads.h> |
michael@0 | 34 | |
michael@0 | 35 | #include "mozilla/layers/LayersSurfaces.h" |
michael@0 | 36 | #include "mozilla/layers/TextureClient.h" |
michael@0 | 37 | |
michael@0 | 38 | #if ANDROID_VERSION == 17 |
michael@0 | 39 | #define IGraphicBufferProducer ISurfaceTexture |
michael@0 | 40 | #endif |
michael@0 | 41 | |
michael@0 | 42 | namespace android { |
michael@0 | 43 | // ---------------------------------------------------------------------------- |
michael@0 | 44 | |
michael@0 | 45 | #if ANDROID_VERSION == 17 |
michael@0 | 46 | class GonkBufferQueue : public BnSurfaceTexture { |
michael@0 | 47 | #else |
michael@0 | 48 | class GonkBufferQueue : public BnGraphicBufferProducer { |
michael@0 | 49 | #endif |
michael@0 | 50 | typedef mozilla::layers::TextureClient TextureClient; |
michael@0 | 51 | |
michael@0 | 52 | public: |
michael@0 | 53 | enum { MIN_UNDEQUEUED_BUFFERS = 2 }; |
michael@0 | 54 | enum { NUM_BUFFER_SLOTS = 32 }; |
michael@0 | 55 | enum { NO_CONNECTED_API = 0 }; |
michael@0 | 56 | enum { INVALID_BUFFER_SLOT = -1 }; |
michael@0 | 57 | enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE }; |
michael@0 | 58 | |
michael@0 | 59 | // When in async mode we reserve two slots in order to guarantee that the |
michael@0 | 60 | // producer and consumer can run asynchronously. |
michael@0 | 61 | enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 }; |
michael@0 | 62 | |
michael@0 | 63 | // ConsumerListener is the interface through which the GonkBufferQueue notifies |
michael@0 | 64 | // the consumer of events that the consumer may wish to react to. Because |
michael@0 | 65 | // the consumer will generally have a mutex that is locked during calls from |
michael@0 | 66 | // the consumer to the GonkBufferQueue, these calls from the GonkBufferQueue to the |
michael@0 | 67 | // consumer *MUST* be called only when the GonkBufferQueue mutex is NOT locked. |
michael@0 | 68 | struct ConsumerListener : public virtual RefBase { |
michael@0 | 69 | // onFrameAvailable is called from queueBuffer each time an additional |
michael@0 | 70 | // frame becomes available for consumption. This means that frames that |
michael@0 | 71 | // are queued while in asynchronous mode only trigger the callback if no |
michael@0 | 72 | // previous frames are pending. Frames queued while in synchronous mode |
michael@0 | 73 | // always trigger the callback. |
michael@0 | 74 | // |
michael@0 | 75 | // This is called without any lock held and can be called concurrently |
michael@0 | 76 | // by multiple threads. |
michael@0 | 77 | virtual void onFrameAvailable() = 0; |
michael@0 | 78 | |
michael@0 | 79 | // onBuffersReleased is called to notify the buffer consumer that the |
michael@0 | 80 | // GonkBufferQueue has released its references to one or more GraphicBuffers |
michael@0 | 81 | // contained in its slots. The buffer consumer should then call |
michael@0 | 82 | // GonkBufferQueue::getReleasedBuffers to retrieve the list of buffers |
michael@0 | 83 | // |
michael@0 | 84 | // This is called without any lock held and can be called concurrently |
michael@0 | 85 | // by multiple threads. |
michael@0 | 86 | virtual void onBuffersReleased() = 0; |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak |
michael@0 | 90 | // reference to the actual consumer object. It forwards all calls to that |
michael@0 | 91 | // consumer object so long as it exists. |
michael@0 | 92 | // |
michael@0 | 93 | // This class exists to avoid having a circular reference between the |
michael@0 | 94 | // GonkBufferQueue object and the consumer object. The reason this can't be a weak |
michael@0 | 95 | // reference in the GonkBufferQueue class is because we're planning to expose the |
michael@0 | 96 | // consumer side of a GonkBufferQueue as a binder interface, which doesn't support |
michael@0 | 97 | // weak references. |
michael@0 | 98 | class ProxyConsumerListener : public GonkBufferQueue::ConsumerListener { |
michael@0 | 99 | public: |
michael@0 | 100 | |
michael@0 | 101 | ProxyConsumerListener(const wp<GonkBufferQueue::ConsumerListener>& consumerListener); |
michael@0 | 102 | virtual ~ProxyConsumerListener(); |
michael@0 | 103 | virtual void onFrameAvailable(); |
michael@0 | 104 | virtual void onBuffersReleased(); |
michael@0 | 105 | |
michael@0 | 106 | private: |
michael@0 | 107 | |
michael@0 | 108 | // mConsumerListener is a weak reference to the ConsumerListener. This is |
michael@0 | 109 | // the raison d'etre of ProxyConsumerListener. |
michael@0 | 110 | wp<GonkBufferQueue::ConsumerListener> mConsumerListener; |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | |
michael@0 | 114 | // GonkBufferQueue manages a pool of gralloc memory slots to be used by |
michael@0 | 115 | // producers and consumers. allowSynchronousMode specifies whether or not |
michael@0 | 116 | // synchronous mode can be enabled by the producer. allocator is used to |
michael@0 | 117 | // allocate all the needed gralloc buffers. |
michael@0 | 118 | GonkBufferQueue(bool allowSynchronousMode = true, |
michael@0 | 119 | const sp<IGraphicBufferAlloc>& allocator = NULL); |
michael@0 | 120 | virtual ~GonkBufferQueue(); |
michael@0 | 121 | |
michael@0 | 122 | // Query native window attributes. The "what" values are enumerated in |
michael@0 | 123 | // window.h (e.g. NATIVE_WINDOW_FORMAT). |
michael@0 | 124 | virtual int query(int what, int* value); |
michael@0 | 125 | |
michael@0 | 126 | // setBufferCount updates the number of available buffer slots. If this |
michael@0 | 127 | // method succeeds, buffer slots will be both unallocated and owned by |
michael@0 | 128 | // the GonkBufferQueue object (i.e. they are not owned by the producer or |
michael@0 | 129 | // consumer). |
michael@0 | 130 | // |
michael@0 | 131 | // This will fail if the producer has dequeued any buffers, or if |
michael@0 | 132 | // bufferCount is invalid. bufferCount must generally be a value |
michael@0 | 133 | // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS |
michael@0 | 134 | // (inclusive). It may also be set to zero (the default) to indicate |
michael@0 | 135 | // that the producer does not wish to set a value. The minimum value |
michael@0 | 136 | // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
michael@0 | 137 | // ...). |
michael@0 | 138 | // |
michael@0 | 139 | // This may only be called by the producer. The consumer will be told |
michael@0 | 140 | // to discard buffers through the onBuffersReleased callback. |
michael@0 | 141 | virtual status_t setBufferCount(int bufferCount); |
michael@0 | 142 | |
michael@0 | 143 | // requestBuffer returns the GraphicBuffer for slot N. |
michael@0 | 144 | // |
michael@0 | 145 | // In normal operation, this is called the first time slot N is returned |
michael@0 | 146 | // by dequeueBuffer. It must be called again if dequeueBuffer returns |
michael@0 | 147 | // flags indicating that previously-returned buffers are no longer valid. |
michael@0 | 148 | virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); |
michael@0 | 149 | |
michael@0 | 150 | // dequeueBuffer gets the next buffer slot index for the producer to use. |
michael@0 | 151 | // If a buffer slot is available then that slot index is written to the |
michael@0 | 152 | // location pointed to by the buf argument and a status of OK is returned. |
michael@0 | 153 | // If no slot is available then a status of -EBUSY is returned and buf is |
michael@0 | 154 | // unmodified. |
michael@0 | 155 | // |
michael@0 | 156 | // The fence parameter will be updated to hold the fence associated with |
michael@0 | 157 | // the buffer. The contents of the buffer must not be overwritten until the |
michael@0 | 158 | // fence signals. If the fence is Fence::NO_FENCE, the buffer may be |
michael@0 | 159 | // written immediately. |
michael@0 | 160 | // |
michael@0 | 161 | // The width and height parameters must be no greater than the minimum of |
michael@0 | 162 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
michael@0 | 163 | // An error due to invalid dimensions might not be reported until |
michael@0 | 164 | // updateTexImage() is called. If width and height are both zero, the |
michael@0 | 165 | // default values specified by setDefaultBufferSize() are used instead. |
michael@0 | 166 | // |
michael@0 | 167 | // The pixel formats are enumerated in graphics.h, e.g. |
michael@0 | 168 | // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format |
michael@0 | 169 | // will be used. |
michael@0 | 170 | // |
michael@0 | 171 | // The usage argument specifies gralloc buffer usage flags. The values |
michael@0 | 172 | // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These |
michael@0 | 173 | // will be merged with the usage flags specified by setConsumerUsageBits. |
michael@0 | 174 | // |
michael@0 | 175 | // The return value may be a negative error value or a non-negative |
michael@0 | 176 | // collection of flags. If the flags are set, the return values are |
michael@0 | 177 | // valid, but additional actions must be performed. |
michael@0 | 178 | // |
michael@0 | 179 | // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the |
michael@0 | 180 | // producer must discard cached GraphicBuffer references for the slot |
michael@0 | 181 | // returned in buf. |
michael@0 | 182 | // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer |
michael@0 | 183 | // must discard cached GraphicBuffer references for all slots. |
michael@0 | 184 | // |
michael@0 | 185 | // In both cases, the producer will need to call requestBuffer to get a |
michael@0 | 186 | // GraphicBuffer handle for the returned slot. |
michael@0 | 187 | #if ANDROID_VERSION == 17 |
michael@0 | 188 | virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence, |
michael@0 | 189 | uint32_t width, uint32_t height, uint32_t format, uint32_t usage) { |
michael@0 | 190 | return dequeueBuffer(buf, &fence, width, height, format, usage); |
michael@0 | 191 | } |
michael@0 | 192 | #endif |
michael@0 | 193 | |
michael@0 | 194 | virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, |
michael@0 | 195 | uint32_t width, uint32_t height, uint32_t format, uint32_t usage); |
michael@0 | 196 | |
michael@0 | 197 | // queueBuffer returns a filled buffer to the GonkBufferQueue. |
michael@0 | 198 | // |
michael@0 | 199 | // Additional data is provided in the QueueBufferInput struct. Notably, |
michael@0 | 200 | // a timestamp must be provided for the buffer. The timestamp is in |
michael@0 | 201 | // nanoseconds, and must be monotonically increasing. Its other semantics |
michael@0 | 202 | // (zero point, etc) are producer-specific and should be documented by the |
michael@0 | 203 | // producer. |
michael@0 | 204 | // |
michael@0 | 205 | // The caller may provide a fence that signals when all rendering |
michael@0 | 206 | // operations have completed. Alternatively, NO_FENCE may be used, |
michael@0 | 207 | // indicating that the buffer is ready immediately. |
michael@0 | 208 | // |
michael@0 | 209 | // Some values are returned in the output struct: the current settings |
michael@0 | 210 | // for default width and height, the current transform hint, and the |
michael@0 | 211 | // number of queued buffers. |
michael@0 | 212 | virtual status_t queueBuffer(int buf, |
michael@0 | 213 | const QueueBufferInput& input, QueueBufferOutput* output); |
michael@0 | 214 | |
michael@0 | 215 | // cancelBuffer returns a dequeued buffer to the GonkBufferQueue, but doesn't |
michael@0 | 216 | // queue it for use by the consumer. |
michael@0 | 217 | // |
michael@0 | 218 | // The buffer will not be overwritten until the fence signals. The fence |
michael@0 | 219 | // will usually be the one obtained from dequeueBuffer. |
michael@0 | 220 | #if ANDROID_VERSION == 17 |
michael@0 | 221 | virtual void cancelBuffer(int buf, sp<Fence> fence); |
michael@0 | 222 | #else |
michael@0 | 223 | virtual void cancelBuffer(int buf, const sp<Fence>& fence); |
michael@0 | 224 | #endif |
michael@0 | 225 | |
michael@0 | 226 | // setSynchronousMode sets whether dequeueBuffer is synchronous or |
michael@0 | 227 | // asynchronous. In synchronous mode, dequeueBuffer blocks until |
michael@0 | 228 | // a buffer is available, the currently bound buffer can be dequeued and |
michael@0 | 229 | // queued buffers will be acquired in order. In asynchronous mode, |
michael@0 | 230 | // a queued buffer may be replaced by a subsequently queued buffer. |
michael@0 | 231 | // |
michael@0 | 232 | // The default mode is asynchronous. |
michael@0 | 233 | virtual status_t setSynchronousMode(bool enabled); |
michael@0 | 234 | |
michael@0 | 235 | // connect attempts to connect a producer API to the GonkBufferQueue. This |
michael@0 | 236 | // must be called before any other IGraphicBufferProducer methods are |
michael@0 | 237 | // called except for getAllocator. A consumer must already be connected. |
michael@0 | 238 | // |
michael@0 | 239 | // This method will fail if connect was previously called on the |
michael@0 | 240 | // GonkBufferQueue and no corresponding disconnect call was made (i.e. if |
michael@0 | 241 | // it's still connected to a producer). |
michael@0 | 242 | // |
michael@0 | 243 | // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU). |
michael@0 | 244 | virtual status_t connect(int api, QueueBufferOutput* output); |
michael@0 | 245 | |
michael@0 | 246 | // disconnect attempts to disconnect a producer API from the GonkBufferQueue. |
michael@0 | 247 | // Calling this method will cause any subsequent calls to other |
michael@0 | 248 | // IGraphicBufferProducer methods to fail except for getAllocator and connect. |
michael@0 | 249 | // Successfully calling connect after this will allow the other methods to |
michael@0 | 250 | // succeed again. |
michael@0 | 251 | // |
michael@0 | 252 | // This method will fail if the the GonkBufferQueue is not currently |
michael@0 | 253 | // connected to the specified producer API. |
michael@0 | 254 | virtual status_t disconnect(int api); |
michael@0 | 255 | |
michael@0 | 256 | // dump our state in a String |
michael@0 | 257 | virtual void dump(String8& result) const; |
michael@0 | 258 | virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const; |
michael@0 | 259 | |
michael@0 | 260 | // public facing structure for BufferSlot |
michael@0 | 261 | struct BufferItem { |
michael@0 | 262 | |
michael@0 | 263 | BufferItem() |
michael@0 | 264 | : |
michael@0 | 265 | mTransform(0), |
michael@0 | 266 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
michael@0 | 267 | mTimestamp(0), |
michael@0 | 268 | mFrameNumber(0), |
michael@0 | 269 | mBuf(INVALID_BUFFER_SLOT) { |
michael@0 | 270 | mCrop.makeInvalid(); |
michael@0 | 271 | } |
michael@0 | 272 | // mGraphicBuffer points to the buffer allocated for this slot, or is NULL |
michael@0 | 273 | // if the buffer in this slot has been acquired in the past (see |
michael@0 | 274 | // BufferSlot.mAcquireCalled). |
michael@0 | 275 | sp<GraphicBuffer> mGraphicBuffer; |
michael@0 | 276 | |
michael@0 | 277 | // mCrop is the current crop rectangle for this buffer slot. |
michael@0 | 278 | Rect mCrop; |
michael@0 | 279 | |
michael@0 | 280 | // mTransform is the current transform flags for this buffer slot. |
michael@0 | 281 | uint32_t mTransform; |
michael@0 | 282 | |
michael@0 | 283 | // mScalingMode is the current scaling mode for this buffer slot. |
michael@0 | 284 | uint32_t mScalingMode; |
michael@0 | 285 | |
michael@0 | 286 | // mTimestamp is the current timestamp for this buffer slot. This gets |
michael@0 | 287 | // to set by queueBuffer each time this slot is queued. |
michael@0 | 288 | int64_t mTimestamp; |
michael@0 | 289 | |
michael@0 | 290 | // mFrameNumber is the number of the queued frame for this slot. |
michael@0 | 291 | uint64_t mFrameNumber; |
michael@0 | 292 | |
michael@0 | 293 | // mBuf is the slot index of this buffer |
michael@0 | 294 | int mBuf; |
michael@0 | 295 | |
michael@0 | 296 | // mFence is a fence that will signal when the buffer is idle. |
michael@0 | 297 | sp<Fence> mFence; |
michael@0 | 298 | }; |
michael@0 | 299 | |
michael@0 | 300 | // The following public functions are the consumer-facing interface |
michael@0 | 301 | |
michael@0 | 302 | // acquireBuffer attempts to acquire ownership of the next pending buffer in |
michael@0 | 303 | // the GonkBufferQueue. If no buffer is pending then it returns -EINVAL. If a |
michael@0 | 304 | // buffer is successfully acquired, the information about the buffer is |
michael@0 | 305 | // returned in BufferItem. If the buffer returned had previously been |
michael@0 | 306 | // acquired then the BufferItem::mGraphicBuffer field of buffer is set to |
michael@0 | 307 | // NULL and it is assumed that the consumer still holds a reference to the |
michael@0 | 308 | // buffer. |
michael@0 | 309 | status_t acquireBuffer(BufferItem *buffer); |
michael@0 | 310 | |
michael@0 | 311 | // releaseBuffer releases a buffer slot from the consumer back to the |
michael@0 | 312 | // GonkBufferQueue. This may be done while the buffer's contents are still |
michael@0 | 313 | // being accessed. The fence will signal when the buffer is no longer |
michael@0 | 314 | // in use. |
michael@0 | 315 | // |
michael@0 | 316 | // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free |
michael@0 | 317 | // any references to the just-released buffer that it might have, as if it |
michael@0 | 318 | // had received a onBuffersReleased() call with a mask set for the released |
michael@0 | 319 | // buffer. |
michael@0 | 320 | // |
michael@0 | 321 | // Note that the dependencies on EGL will be removed once we switch to using |
michael@0 | 322 | // the Android HW Sync HAL. |
michael@0 | 323 | status_t releaseBuffer(int buf, const sp<Fence>& releaseFence); |
michael@0 | 324 | |
michael@0 | 325 | // consumerConnect connects a consumer to the GonkBufferQueue. Only one |
michael@0 | 326 | // consumer may be connected, and when that consumer disconnects the |
michael@0 | 327 | // GonkBufferQueue is placed into the "abandoned" state, causing most |
michael@0 | 328 | // interactions with the GonkBufferQueue by the producer to fail. |
michael@0 | 329 | // |
michael@0 | 330 | // consumer may not be NULL. |
michael@0 | 331 | status_t consumerConnect(const sp<ConsumerListener>& consumer); |
michael@0 | 332 | |
michael@0 | 333 | // consumerDisconnect disconnects a consumer from the GonkBufferQueue. All |
michael@0 | 334 | // buffers will be freed and the GonkBufferQueue is placed in the "abandoned" |
michael@0 | 335 | // state, causing most interactions with the GonkBufferQueue by the producer to |
michael@0 | 336 | // fail. |
michael@0 | 337 | status_t consumerDisconnect(); |
michael@0 | 338 | |
michael@0 | 339 | // getReleasedBuffers sets the value pointed to by slotMask to a bit mask |
michael@0 | 340 | // indicating which buffer slots have been released by the GonkBufferQueue |
michael@0 | 341 | // but have not yet been released by the consumer. |
michael@0 | 342 | // |
michael@0 | 343 | // This should be called from the onBuffersReleased() callback. |
michael@0 | 344 | status_t getReleasedBuffers(uint32_t* slotMask); |
michael@0 | 345 | |
michael@0 | 346 | // setDefaultBufferSize is used to set the size of buffers returned by |
michael@0 | 347 | // dequeueBuffer when a width and height of zero is requested. Default |
michael@0 | 348 | // is 1x1. |
michael@0 | 349 | status_t setDefaultBufferSize(uint32_t w, uint32_t h); |
michael@0 | 350 | |
michael@0 | 351 | // setDefaultMaxBufferCount sets the default value for the maximum buffer |
michael@0 | 352 | // count (the initial default is 2). If the producer has requested a |
michael@0 | 353 | // buffer count using setBufferCount, the default buffer count will only |
michael@0 | 354 | // take effect if the producer sets the count back to zero. |
michael@0 | 355 | // |
michael@0 | 356 | // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. |
michael@0 | 357 | status_t setDefaultMaxBufferCount(int bufferCount); |
michael@0 | 358 | |
michael@0 | 359 | // setMaxAcquiredBufferCount sets the maximum number of buffers that can |
michael@0 | 360 | // be acquired by the consumer at one time (default 1). This call will |
michael@0 | 361 | // fail if a producer is connected to the GonkBufferQueue. |
michael@0 | 362 | status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers); |
michael@0 | 363 | |
michael@0 | 364 | // isSynchronousMode returns whether the GonkBufferQueue is currently in |
michael@0 | 365 | // synchronous mode. |
michael@0 | 366 | bool isSynchronousMode() const; |
michael@0 | 367 | |
michael@0 | 368 | // setConsumerName sets the name used in logging |
michael@0 | 369 | void setConsumerName(const String8& name); |
michael@0 | 370 | |
michael@0 | 371 | // setDefaultBufferFormat allows the GonkBufferQueue to create |
michael@0 | 372 | // GraphicBuffers of a defaultFormat if no format is specified |
michael@0 | 373 | // in dequeueBuffer. Formats are enumerated in graphics.h; the |
michael@0 | 374 | // initial default is HAL_PIXEL_FORMAT_RGBA_8888. |
michael@0 | 375 | status_t setDefaultBufferFormat(uint32_t defaultFormat); |
michael@0 | 376 | |
michael@0 | 377 | // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. |
michael@0 | 378 | // These are merged with the bits passed to dequeueBuffer. The values are |
michael@0 | 379 | // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0. |
michael@0 | 380 | status_t setConsumerUsageBits(uint32_t usage); |
michael@0 | 381 | |
michael@0 | 382 | // setTransformHint bakes in rotation to buffers so overlays can be used. |
michael@0 | 383 | // The values are enumerated in window.h, e.g. |
michael@0 | 384 | // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform). |
michael@0 | 385 | status_t setTransformHint(uint32_t hint); |
michael@0 | 386 | |
michael@0 | 387 | mozilla::TemporaryRef<TextureClient> getTextureClientFromBuffer(ANativeWindowBuffer* buffer); |
michael@0 | 388 | |
michael@0 | 389 | int getSlotFromTextureClientLocked(TextureClient* client) const; |
michael@0 | 390 | |
michael@0 | 391 | private: |
michael@0 | 392 | // freeBufferLocked frees the GraphicBuffer and sync resources for the |
michael@0 | 393 | // given slot. |
michael@0 | 394 | //void freeBufferLocked(int index); |
michael@0 | 395 | |
michael@0 | 396 | // freeAllBuffersLocked frees the GraphicBuffer and sync resources for |
michael@0 | 397 | // all slots. |
michael@0 | 398 | //void freeAllBuffersLocked(); |
michael@0 | 399 | void freeAllBuffersLocked(); |
michael@0 | 400 | |
michael@0 | 401 | // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots |
michael@0 | 402 | // that will be used if the producer does not override the buffer slot |
michael@0 | 403 | // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. |
michael@0 | 404 | // The initial default is 2. |
michael@0 | 405 | status_t setDefaultMaxBufferCountLocked(int count); |
michael@0 | 406 | |
michael@0 | 407 | // getMinBufferCountLocked returns the minimum number of buffers allowed |
michael@0 | 408 | // given the current GonkBufferQueue state. |
michael@0 | 409 | int getMinMaxBufferCountLocked() const; |
michael@0 | 410 | |
michael@0 | 411 | // getMinUndequeuedBufferCountLocked returns the minimum number of buffers |
michael@0 | 412 | // that must remain in a state other than DEQUEUED. |
michael@0 | 413 | int getMinUndequeuedBufferCountLocked() const; |
michael@0 | 414 | |
michael@0 | 415 | // getMaxBufferCountLocked returns the maximum number of buffers that can |
michael@0 | 416 | // be allocated at once. This value depends upon the following member |
michael@0 | 417 | // variables: |
michael@0 | 418 | // |
michael@0 | 419 | // mSynchronousMode |
michael@0 | 420 | // mMaxAcquiredBufferCount |
michael@0 | 421 | // mDefaultMaxBufferCount |
michael@0 | 422 | // mOverrideMaxBufferCount |
michael@0 | 423 | // |
michael@0 | 424 | // Any time one of these member variables is changed while a producer is |
michael@0 | 425 | // connected, mDequeueCondition must be broadcast. |
michael@0 | 426 | int getMaxBufferCountLocked() const; |
michael@0 | 427 | |
michael@0 | 428 | struct BufferSlot { |
michael@0 | 429 | |
michael@0 | 430 | BufferSlot() |
michael@0 | 431 | : mBufferState(BufferSlot::FREE), |
michael@0 | 432 | mRequestBufferCalled(false), |
michael@0 | 433 | mTransform(0), |
michael@0 | 434 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
michael@0 | 435 | mTimestamp(0), |
michael@0 | 436 | mFrameNumber(0), |
michael@0 | 437 | mAcquireCalled(false), |
michael@0 | 438 | mNeedsCleanupOnRelease(false) { |
michael@0 | 439 | mCrop.makeInvalid(); |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
michael@0 | 443 | // if no buffer has been allocated. |
michael@0 | 444 | sp<GraphicBuffer> mGraphicBuffer; |
michael@0 | 445 | |
michael@0 | 446 | // mTextureClient is a thin abstraction over remotely allocated GraphicBuffer. |
michael@0 | 447 | mozilla::RefPtr<TextureClient> mTextureClient; |
michael@0 | 448 | |
michael@0 | 449 | // BufferState represents the different states in which a buffer slot |
michael@0 | 450 | // can be. All slots are initially FREE. |
michael@0 | 451 | enum BufferState { |
michael@0 | 452 | // FREE indicates that the buffer is available to be dequeued |
michael@0 | 453 | // by the producer. The buffer may be in use by the consumer for |
michael@0 | 454 | // a finite time, so the buffer must not be modified until the |
michael@0 | 455 | // associated fence is signaled. |
michael@0 | 456 | // |
michael@0 | 457 | // The slot is "owned" by GonkBufferQueue. It transitions to DEQUEUED |
michael@0 | 458 | // when dequeueBuffer is called. |
michael@0 | 459 | FREE = 0, |
michael@0 | 460 | |
michael@0 | 461 | // DEQUEUED indicates that the buffer has been dequeued by the |
michael@0 | 462 | // producer, but has not yet been queued or canceled. The |
michael@0 | 463 | // producer may modify the buffer's contents as soon as the |
michael@0 | 464 | // associated ready fence is signaled. |
michael@0 | 465 | // |
michael@0 | 466 | // The slot is "owned" by the producer. It can transition to |
michael@0 | 467 | // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer). |
michael@0 | 468 | DEQUEUED = 1, |
michael@0 | 469 | |
michael@0 | 470 | // QUEUED indicates that the buffer has been filled by the |
michael@0 | 471 | // producer and queued for use by the consumer. The buffer |
michael@0 | 472 | // contents may continue to be modified for a finite time, so |
michael@0 | 473 | // the contents must not be accessed until the associated fence |
michael@0 | 474 | // is signaled. |
michael@0 | 475 | // |
michael@0 | 476 | // The slot is "owned" by GonkBufferQueue. It can transition to |
michael@0 | 477 | // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is |
michael@0 | 478 | // queued in asynchronous mode). |
michael@0 | 479 | QUEUED = 2, |
michael@0 | 480 | |
michael@0 | 481 | // ACQUIRED indicates that the buffer has been acquired by the |
michael@0 | 482 | // consumer. As with QUEUED, the contents must not be accessed |
michael@0 | 483 | // by the consumer until the fence is signaled. |
michael@0 | 484 | // |
michael@0 | 485 | // The slot is "owned" by the consumer. It transitions to FREE |
michael@0 | 486 | // when releaseBuffer is called. |
michael@0 | 487 | ACQUIRED = 3 |
michael@0 | 488 | }; |
michael@0 | 489 | |
michael@0 | 490 | // mBufferState is the current state of this buffer slot. |
michael@0 | 491 | BufferState mBufferState; |
michael@0 | 492 | |
michael@0 | 493 | // mRequestBufferCalled is used for validating that the producer did |
michael@0 | 494 | // call requestBuffer() when told to do so. Technically this is not |
michael@0 | 495 | // needed but useful for debugging and catching producer bugs. |
michael@0 | 496 | bool mRequestBufferCalled; |
michael@0 | 497 | |
michael@0 | 498 | // mCrop is the current crop rectangle for this buffer slot. |
michael@0 | 499 | Rect mCrop; |
michael@0 | 500 | |
michael@0 | 501 | // mTransform is the current transform flags for this buffer slot. |
michael@0 | 502 | // (example: NATIVE_WINDOW_TRANSFORM_ROT_90) |
michael@0 | 503 | uint32_t mTransform; |
michael@0 | 504 | |
michael@0 | 505 | // mScalingMode is the current scaling mode for this buffer slot. |
michael@0 | 506 | // (example: NATIVE_WINDOW_SCALING_MODE_FREEZE) |
michael@0 | 507 | uint32_t mScalingMode; |
michael@0 | 508 | |
michael@0 | 509 | // mTimestamp is the current timestamp for this buffer slot. This gets |
michael@0 | 510 | // to set by queueBuffer each time this slot is queued. |
michael@0 | 511 | int64_t mTimestamp; |
michael@0 | 512 | |
michael@0 | 513 | // mFrameNumber is the number of the queued frame for this slot. This |
michael@0 | 514 | // is used to dequeue buffers in LRU order (useful because buffers |
michael@0 | 515 | // may be released before their release fence is signaled). |
michael@0 | 516 | uint64_t mFrameNumber; |
michael@0 | 517 | |
michael@0 | 518 | // mEglFence is the EGL sync object that must signal before the buffer |
michael@0 | 519 | // associated with this buffer slot may be dequeued. It is initialized |
michael@0 | 520 | // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a |
michael@0 | 521 | // new sync object in releaseBuffer. (This is deprecated in favor of |
michael@0 | 522 | // mFence, below.) |
michael@0 | 523 | //EGLSyncKHR mEglFence; |
michael@0 | 524 | |
michael@0 | 525 | // mFence is a fence which will signal when work initiated by the |
michael@0 | 526 | // previous owner of the buffer is finished. When the buffer is FREE, |
michael@0 | 527 | // the fence indicates when the consumer has finished reading |
michael@0 | 528 | // from the buffer, or when the producer has finished writing if it |
michael@0 | 529 | // called cancelBuffer after queueing some writes. When the buffer is |
michael@0 | 530 | // QUEUED, it indicates when the producer has finished filling the |
michael@0 | 531 | // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been |
michael@0 | 532 | // passed to the consumer or producer along with ownership of the |
michael@0 | 533 | // buffer, and mFence is set to NO_FENCE. |
michael@0 | 534 | sp<Fence> mFence; |
michael@0 | 535 | |
michael@0 | 536 | // Indicates whether this buffer has been seen by a consumer yet |
michael@0 | 537 | bool mAcquireCalled; |
michael@0 | 538 | |
michael@0 | 539 | // Indicates whether this buffer needs to be cleaned up by the |
michael@0 | 540 | // consumer. This is set when a buffer in ACQUIRED state is freed. |
michael@0 | 541 | // It causes releaseBuffer to return STALE_BUFFER_SLOT. |
michael@0 | 542 | bool mNeedsCleanupOnRelease; |
michael@0 | 543 | }; |
michael@0 | 544 | |
michael@0 | 545 | // mSlots is the array of buffer slots that must be mirrored on the |
michael@0 | 546 | // producer side. This allows buffer ownership to be transferred between |
michael@0 | 547 | // the producer and consumer without sending a GraphicBuffer over binder. |
michael@0 | 548 | // The entire array is initialized to NULL at construction time, and |
michael@0 | 549 | // buffers are allocated for a slot when requestBuffer is called with |
michael@0 | 550 | // that slot's index. |
michael@0 | 551 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
michael@0 | 552 | |
michael@0 | 553 | // mDefaultWidth holds the default width of allocated buffers. It is used |
michael@0 | 554 | // in dequeueBuffer() if a width and height of zero is specified. |
michael@0 | 555 | uint32_t mDefaultWidth; |
michael@0 | 556 | |
michael@0 | 557 | // mDefaultHeight holds the default height of allocated buffers. It is used |
michael@0 | 558 | // in dequeueBuffer() if a width and height of zero is specified. |
michael@0 | 559 | uint32_t mDefaultHeight; |
michael@0 | 560 | |
michael@0 | 561 | // mMaxAcquiredBufferCount is the number of buffers that the consumer may |
michael@0 | 562 | // acquire at one time. It defaults to 1 and can be changed by the |
michael@0 | 563 | // consumer via the setMaxAcquiredBufferCount method, but this may only be |
michael@0 | 564 | // done when no producer is connected to the GonkBufferQueue. |
michael@0 | 565 | // |
michael@0 | 566 | // This value is used to derive the value returned for the |
michael@0 | 567 | // MIN_UNDEQUEUED_BUFFERS query by the producer. |
michael@0 | 568 | int mMaxAcquiredBufferCount; |
michael@0 | 569 | |
michael@0 | 570 | // mDefaultMaxBufferCount is the default limit on the number of buffers |
michael@0 | 571 | // that will be allocated at one time. This default limit is set by the |
michael@0 | 572 | // consumer. The limit (as opposed to the default limit) may be |
michael@0 | 573 | // overridden by the producer. |
michael@0 | 574 | int mDefaultMaxBufferCount; |
michael@0 | 575 | |
michael@0 | 576 | // mOverrideMaxBufferCount is the limit on the number of buffers that will |
michael@0 | 577 | // be allocated at one time. This value is set by the image producer by |
michael@0 | 578 | // calling setBufferCount. The default is zero, which means the producer |
michael@0 | 579 | // doesn't care about the number of buffers in the pool. In that case |
michael@0 | 580 | // mDefaultMaxBufferCount is used as the limit. |
michael@0 | 581 | int mOverrideMaxBufferCount; |
michael@0 | 582 | |
michael@0 | 583 | // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to |
michael@0 | 584 | // allocate new GraphicBuffer objects. |
michael@0 | 585 | sp<IGraphicBufferAlloc> mGraphicBufferAlloc; |
michael@0 | 586 | |
michael@0 | 587 | // mConsumerListener is used to notify the connected consumer of |
michael@0 | 588 | // asynchronous events that it may wish to react to. It is initially set |
michael@0 | 589 | // to NULL and is written by consumerConnect and consumerDisconnect. |
michael@0 | 590 | sp<ConsumerListener> mConsumerListener; |
michael@0 | 591 | |
michael@0 | 592 | // mSynchronousMode whether we're in synchronous mode or not |
michael@0 | 593 | bool mSynchronousMode; |
michael@0 | 594 | |
michael@0 | 595 | // mAllowSynchronousMode whether we allow synchronous mode or not. Set |
michael@0 | 596 | // when the GonkBufferQueue is created (by the consumer). |
michael@0 | 597 | const bool mAllowSynchronousMode; |
michael@0 | 598 | |
michael@0 | 599 | // mConnectedApi indicates the producer API that is currently connected |
michael@0 | 600 | // to this GonkBufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets |
michael@0 | 601 | // updated by the connect and disconnect methods. |
michael@0 | 602 | int mConnectedApi; |
michael@0 | 603 | |
michael@0 | 604 | // mDequeueCondition condition used for dequeueBuffer in synchronous mode |
michael@0 | 605 | mutable Condition mDequeueCondition; |
michael@0 | 606 | |
michael@0 | 607 | // mQueue is a FIFO of queued buffers used in synchronous mode |
michael@0 | 608 | typedef Vector<int> Fifo; |
michael@0 | 609 | Fifo mQueue; |
michael@0 | 610 | |
michael@0 | 611 | // mAbandoned indicates that the GonkBufferQueue will no longer be used to |
michael@0 | 612 | // consume image buffers pushed to it using the IGraphicBufferProducer |
michael@0 | 613 | // interface. It is initialized to false, and set to true in the |
michael@0 | 614 | // consumerDisconnect method. A GonkBufferQueue that has been abandoned will |
michael@0 | 615 | // return the NO_INIT error from all IGraphicBufferProducer methods |
michael@0 | 616 | // capable of returning an error. |
michael@0 | 617 | bool mAbandoned; |
michael@0 | 618 | |
michael@0 | 619 | // mConsumerName is a string used to identify the GonkBufferQueue in log |
michael@0 | 620 | // messages. It is set by the setConsumerName method. |
michael@0 | 621 | String8 mConsumerName; |
michael@0 | 622 | |
michael@0 | 623 | // mMutex is the mutex used to prevent concurrent access to the member |
michael@0 | 624 | // variables of GonkBufferQueue objects. It must be locked whenever the |
michael@0 | 625 | // member variables are accessed. |
michael@0 | 626 | mutable Mutex mMutex; |
michael@0 | 627 | |
michael@0 | 628 | // mFrameCounter is the free running counter, incremented on every |
michael@0 | 629 | // successful queueBuffer call. |
michael@0 | 630 | uint64_t mFrameCounter; |
michael@0 | 631 | |
michael@0 | 632 | // mBufferHasBeenQueued is true once a buffer has been queued. It is |
michael@0 | 633 | // reset when something causes all buffers to be freed (e.g. changing the |
michael@0 | 634 | // buffer count). |
michael@0 | 635 | bool mBufferHasBeenQueued; |
michael@0 | 636 | |
michael@0 | 637 | // mDefaultBufferFormat can be set so it will override |
michael@0 | 638 | // the buffer format when it isn't specified in dequeueBuffer |
michael@0 | 639 | uint32_t mDefaultBufferFormat; |
michael@0 | 640 | |
michael@0 | 641 | // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers |
michael@0 | 642 | uint32_t mConsumerUsageBits; |
michael@0 | 643 | |
michael@0 | 644 | // mTransformHint is used to optimize for screen rotations |
michael@0 | 645 | uint32_t mTransformHint; |
michael@0 | 646 | |
michael@0 | 647 | }; |
michael@0 | 648 | |
michael@0 | 649 | // ---------------------------------------------------------------------------- |
michael@0 | 650 | }; // namespace android |
michael@0 | 651 | |
michael@0 | 652 | #endif // ANDROID_GUI_BUFFERQUEUE_H |