blob: 9e265bab72aa88ef71d137c832565a4485f48e4c [file] [log] [blame]
Daniel Lam6b091c52012-01-22 15:26:27 -08001/*
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 Lamf71c4ae2012-03-23 18:12:04 -070021#include <EGL/eglext.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080022
Mathias Agopian90ac7992012-02-25 18:48:35 -080023#include <gui/IGraphicBufferAlloc.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080024#include <gui/ISurfaceTexture.h>
25
Jesse Hallef194142012-06-14 14:45:17 -070026#include <ui/Fence.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080027#include <ui/GraphicBuffer.h>
28
29#include <utils/String8.h>
30#include <utils/Vector.h>
31#include <utils/threads.h>
32
33namespace android {
34// ----------------------------------------------------------------------------
35
36class BufferQueue : public BnSurfaceTexture {
37public:
38 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
Daniel Lam6b091c52012-01-22 15:26:27 -080039 enum { NUM_BUFFER_SLOTS = 32 };
40 enum { NO_CONNECTED_API = 0 };
Daniel Lameae59d22012-01-22 15:26:27 -080041 enum { INVALID_BUFFER_SLOT = -1 };
Daniel Lamfbcda932012-04-09 22:51:52 -070042 enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE };
Daniel Lam6b091c52012-01-22 15:26:27 -080043
Jamie Gennisc68f2ec2012-08-30 18:36:22 -070044 // 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 Gennisfa5b40e2012-03-15 14:01:24 -070048 // 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 Lam6b091c52012-01-22 15:26:27 -080059 //
60 // This is called without any lock held and can be called concurrently
61 // by multiple threads.
62 virtual void onFrameAvailable() = 0;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070063
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 Lam6b091c52012-01-22 15:26:27 -080072 };
73
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070074 // 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 Gennis72f096f2012-08-27 18:48:37 -070099 // 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 Agopian3e876012012-06-07 17:52:54 -0700103 BufferQueue(bool allowSynchronousMode = true,
Mathias Agopian3e876012012-06-07 17:52:54 -0700104 const sp<IGraphicBufferAlloc>& allocator = NULL);
Daniel Lam6b091c52012-01-22 15:26:27 -0800105 virtual ~BufferQueue();
106
Daniel Lamb8560522012-01-30 15:51:27 -0800107 virtual int query(int what, int* value);
108
Daniel Lam6b091c52012-01-22 15:26:27 -0800109 // 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 Hallf7857542012-06-14 15:26:33 -0700121 //
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 Lam6b091c52012-01-22 15:26:27 -0800127 // 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 Hallf7857542012-06-14 15:26:33 -0700131 virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence,
132 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800133
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 Agopianf0bc2f12012-04-09 16:14:01 -0700139 virtual status_t queueBuffer(int buf,
140 const QueueBufferInput& input, QueueBufferOutput* output);
141
Jesse Hallc777b0b2012-06-28 12:52:05 -0700142 virtual void cancelBuffer(int buf, sp<Fence> fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800143
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.
152 // This must be called before any other ISurfaceTexture methods are called
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 Agopian24202f52012-04-23 14:28:58 -0700157 virtual status_t connect(int api, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800158
159 // disconnect attempts to disconnect a producer client API from the
160 // BufferQueue. Calling this method will cause any subsequent calls to other
161 // ISurfaceTexture methods to fail except for getAllocator and connect.
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 Lameae59d22012-01-22 15:26:27 -0800169 // 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 Lam6b091c52012-01-22 15:26:27 -0800172
Daniel Lameae59d22012-01-22 15:26:27 -0800173 // 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 }
185 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
186 // if no buffer has been allocated.
187 sp<GraphicBuffer> mGraphicBuffer;
188
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700189 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800190 Rect mCrop;
191
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700192 // mTransform is the current transform flags for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800193 uint32_t mTransform;
194
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700195 // mScalingMode is the current scaling mode for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800196 uint32_t mScalingMode;
197
198 // mTimestamp is the current timestamp for this buffer slot. This gets
199 // to set by queueBuffer each time this slot is queued.
200 int64_t mTimestamp;
201
202 // mFrameNumber is the number of the queued frame for this slot.
203 uint64_t mFrameNumber;
204
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700205 // mBuf is the slot index of this buffer
Daniel Lameae59d22012-01-22 15:26:27 -0800206 int mBuf;
Jesse Hallb42b1ac2012-06-28 14:27:53 -0700207
208 // mFence is a fence that will signal when the buffer is idle.
209 sp<Fence> mFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800210 };
211
212 // The following public functions is the consumer facing interface
213
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700214 // acquireBuffer attempts to acquire ownership of the next pending buffer in
215 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
216 // buffer is successfully acquired, the information about the buffer is
217 // returned in BufferItem. If the buffer returned had previously been
218 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
219 // NULL and it is assumed that the consumer still holds a reference to the
220 // buffer.
221 status_t acquireBuffer(BufferItem *buffer);
Daniel Lameae59d22012-01-22 15:26:27 -0800222
223 // releaseBuffer releases a buffer slot from the consumer back to the
224 // BufferQueue pending a fence sync.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700225 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700226 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
227 // any references to the just-released buffer that it might have, as if it
228 // had received a onBuffersReleased() call with a mask set for the released
229 // buffer.
230 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700231 // Note that the dependencies on EGL will be removed once we switch to using
232 // the Android HW Sync HAL.
Jesse Hallef194142012-06-14 14:45:17 -0700233 status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700234 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800235
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700236 // consumerConnect connects a consumer to the BufferQueue. Only one
237 // consumer may be connected, and when that consumer disconnects the
238 // BufferQueue is placed into the "abandoned" state, causing most
239 // interactions with the BufferQueue by the producer to fail.
240 status_t consumerConnect(const sp<ConsumerListener>& consumer);
241
Daniel Lameae59d22012-01-22 15:26:27 -0800242 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700243 // buffers will be freed and the BufferQueue is placed in the "abandoned"
244 // state, causing most interactions with the BufferQueue by the producer to
245 // fail.
Daniel Lameae59d22012-01-22 15:26:27 -0800246 status_t consumerDisconnect();
247
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700248 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
249 // indicating which buffer slots the have been released by the BufferQueue
250 // but have not yet been released by the consumer.
251 status_t getReleasedBuffers(uint32_t* slotMask);
252
Daniel Lameae59d22012-01-22 15:26:27 -0800253 // setDefaultBufferSize is used to set the size of buffers returned by
254 // requestBuffers when a with and height of zero is requested.
255 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
256
Jamie Gennis31a353d2012-08-24 17:25:13 -0700257 // setDefaultBufferCount set the buffer count. If the client has requested
Daniel Lameae59d22012-01-22 15:26:27 -0800258 // a buffer count using setBufferCount, the server-buffer count will
259 // take effect once the client sets the count back to zero.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700260 status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800261
Jamie Gennis72f096f2012-08-27 18:48:37 -0700262 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
263 // be acquired by the consumer at one time. This call will fail if a
264 // producer is connected to the BufferQueue.
265 status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
266
Daniel Lameae59d22012-01-22 15:26:27 -0800267 // isSynchronousMode returns whether the SurfaceTexture is currently in
268 // synchronous mode.
269 bool isSynchronousMode() const;
270
271 // setConsumerName sets the name used in logging
272 void setConsumerName(const String8& name);
273
Daniel Lamb2675792012-02-23 14:35:13 -0800274 // setDefaultBufferFormat allows the BufferQueue to create
275 // GraphicBuffers of a defaultFormat if no format is specified
276 // in dequeueBuffer
277 status_t setDefaultBufferFormat(uint32_t defaultFormat);
278
279 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
280 status_t setConsumerUsageBits(uint32_t usage);
281
282 // setTransformHint bakes in rotation to buffers so overlays can be used
283 status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800284
285private:
Daniel Lam6b091c52012-01-22 15:26:27 -0800286 // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
287 // for the given slot.
288 void freeBufferLocked(int index);
289
290 // freeAllBuffersLocked frees the resources (both GraphicBuffer and
291 // EGLImage) for all slots.
292 void freeAllBuffersLocked();
293
294 // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
295 // and EGLImage) for all slots except the head of mQueue
296 void freeAllBuffersExceptHeadLocked();
297
298 // drainQueueLocked drains the buffer queue if we're in synchronous mode
299 // returns immediately otherwise. It returns NO_INIT if the BufferQueue
300 // became abandoned or disconnected during this call.
301 status_t drainQueueLocked();
302
303 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
304 // synchronous mode and free all buffers. In asynchronous mode, all buffers
305 // are freed except the current buffer.
306 status_t drainQueueAndFreeBuffersLocked();
307
Jamie Gennis31a353d2012-08-24 17:25:13 -0700308 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
309 // that will be used if the producer does not override the buffer slot
310 // count.
311 status_t setDefaultMaxBufferCountLocked(int count);
312
313 // getMinBufferCountLocked returns the minimum number of buffers allowed
314 // given the current BufferQueue state.
315 int getMinMaxBufferCountLocked() const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800316
Jamie Gennis72f096f2012-08-27 18:48:37 -0700317 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
318 // that must remain in a state other than DEQUEUED.
319 int getMinUndequeuedBufferCountLocked() const;
320
Jamie Gennise191e6c2012-08-24 20:26:34 -0700321 // getMaxBufferCountLocked returns the maximum number of buffers that can
322 // be allocated at once. This value depends upon the following member
323 // variables:
324 //
325 // mSynchronousMode
Jamie Gennis72f096f2012-08-27 18:48:37 -0700326 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700327 // mDefaultMaxBufferCount
328 // mOverrideMaxBufferCount
329 //
330 // Any time one of these member variables is changed while a producer is
331 // connected, mDequeueCondition must be broadcast.
332 int getMaxBufferCountLocked() const;
333
Daniel Lam6b091c52012-01-22 15:26:27 -0800334 struct BufferSlot {
335
336 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800337 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800338 mBufferState(BufferSlot::FREE),
339 mRequestBufferCalled(false),
340 mTransform(0),
341 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
342 mTimestamp(0),
343 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700344 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700345 mAcquireCalled(false),
346 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800347 mCrop.makeInvalid();
348 }
349
350 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
351 // if no buffer has been allocated.
352 sp<GraphicBuffer> mGraphicBuffer;
353
Daniel Lam6b091c52012-01-22 15:26:27 -0800354 // mEglDisplay is the EGLDisplay used to create mEglImage.
355 EGLDisplay mEglDisplay;
356
357 // BufferState represents the different states in which a buffer slot
358 // can be.
359 enum BufferState {
360 // FREE indicates that the buffer is not currently being used and
361 // will not be used in the future until it gets dequeued and
362 // subsequently queued by the client.
Daniel Lameae59d22012-01-22 15:26:27 -0800363 // aka "owned by BufferQueue, ready to be dequeued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800364 FREE = 0,
365
366 // DEQUEUED indicates that the buffer has been dequeued by the
367 // client, but has not yet been queued or canceled. The buffer is
368 // considered 'owned' by the client, and the server should not use
369 // it for anything.
370 //
371 // Note that when in synchronous-mode (mSynchronousMode == true),
372 // the buffer that's currently attached to the texture may be
373 // dequeued by the client. That means that the current buffer can
374 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
375 // however, the current buffer is always in the QUEUED state.
Daniel Lameae59d22012-01-22 15:26:27 -0800376 // aka "owned by producer, ready to be queued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800377 DEQUEUED = 1,
378
379 // QUEUED indicates that the buffer has been queued by the client,
380 // and has not since been made available for the client to dequeue.
381 // Attaching the buffer to the texture does NOT transition the
382 // buffer away from the QUEUED state. However, in Synchronous mode
383 // the current buffer may be dequeued by the client under some
384 // circumstances. See the note about the current buffer in the
385 // documentation for DEQUEUED.
Daniel Lameae59d22012-01-22 15:26:27 -0800386 // aka "owned by BufferQueue, ready to be acquired"
Daniel Lam6b091c52012-01-22 15:26:27 -0800387 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800388
389 // aka "owned by consumer, ready to be released"
390 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800391 };
392
393 // mBufferState is the current state of this buffer slot.
394 BufferState mBufferState;
395
396 // mRequestBufferCalled is used for validating that the client did
397 // call requestBuffer() when told to do so. Technically this is not
398 // needed but useful for debugging and catching client bugs.
399 bool mRequestBufferCalled;
400
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700401 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800402 Rect mCrop;
403
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700404 // mTransform is the current transform flags for this buffer slot.
Andy McFadden69052052012-09-14 16:10:11 -0700405 // (example: NATIVE_WINDOW_TRANSFORM_ROT_90)
Daniel Lam6b091c52012-01-22 15:26:27 -0800406 uint32_t mTransform;
407
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700408 // mScalingMode is the current scaling mode for this buffer slot.
Andy McFadden69052052012-09-14 16:10:11 -0700409 // (example: NATIVE_WINDOW_SCALING_MODE_FREEZE)
Daniel Lam6b091c52012-01-22 15:26:27 -0800410 uint32_t mScalingMode;
411
412 // mTimestamp is the current timestamp for this buffer slot. This gets
413 // to set by queueBuffer each time this slot is queued.
414 int64_t mTimestamp;
415
416 // mFrameNumber is the number of the queued frame for this slot.
417 uint64_t mFrameNumber;
418
Jesse Hallc777b0b2012-06-28 12:52:05 -0700419 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800420 // associated with this buffer slot may be dequeued. It is initialized
421 // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
422 // on a compile-time option) set to a new sync object in updateTexImage.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700423 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800424
Jesse Hallc777b0b2012-06-28 12:52:05 -0700425 // mFence is a fence which will signal when work initiated by the
426 // previous owner of the buffer is finished. When the buffer is FREE,
427 // the fence indicates when the consumer has finished reading
428 // from the buffer, or when the producer has finished writing if it
429 // called cancelBuffer after queueing some writes. When the buffer is
430 // QUEUED, it indicates when the producer has finished filling the
431 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
432 // passed to the consumer or producer along with ownership of the
433 // buffer, and mFence is empty.
434 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700435
Daniel Lameae59d22012-01-22 15:26:27 -0800436 // Indicates whether this buffer has been seen by a consumer yet
437 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700438
439 // Indicates whether this buffer needs to be cleaned up by consumer
440 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800441 };
442
443 // mSlots is the array of buffer slots that must be mirrored on the client
444 // side. This allows buffer ownership to be transferred between the client
445 // and server without sending a GraphicBuffer over binder. The entire array
446 // is initialized to NULL at construction time, and buffers are allocated
447 // for a slot when requestBuffer is called with that slot's index.
448 BufferSlot mSlots[NUM_BUFFER_SLOTS];
449
Daniel Lam6b091c52012-01-22 15:26:27 -0800450 // mDefaultWidth holds the default width of allocated buffers. It is used
451 // in requestBuffers() if a width and height of zero is specified.
452 uint32_t mDefaultWidth;
453
454 // mDefaultHeight holds the default height of allocated buffers. It is used
455 // in requestBuffers() if a width and height of zero is specified.
456 uint32_t mDefaultHeight;
457
Jamie Gennis72f096f2012-08-27 18:48:37 -0700458 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
459 // acquire at one time. It defaults to 1 and can be changed by the
460 // consumer via the setMaxAcquiredBufferCount method, but this may only be
461 // done when no producer is connected to the BufferQueue.
462 //
463 // This value is used to derive the value returned for the
464 // MIN_UNDEQUEUED_BUFFERS query by the producer.
465 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700466
Jamie Gennis31a353d2012-08-24 17:25:13 -0700467 // mDefaultMaxBufferCount is the default limit on the number of buffers
468 // that will be allocated at one time. This default limit is set by the
469 // consumer. The limit (as opposed to the default limit) may be
470 // overridden by the producer.
471 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700472
Jamie Gennis31a353d2012-08-24 17:25:13 -0700473 // mOverrideMaxBufferCount is the limit on the number of buffers that will
474 // be allocated at one time. This value is set by the image producer by
475 // calling setBufferCount. The default is zero, which means the producer
476 // doesn't care about the number of buffers in the pool. In that case
477 // mDefaultMaxBufferCount is used as the limit.
478 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800479
Daniel Lam6b091c52012-01-22 15:26:27 -0800480 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
481 // allocate new GraphicBuffer objects.
482 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
483
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700484 // mConsumerListener is used to notify the connected consumer of
485 // asynchronous events that it may wish to react to. It is initially set
486 // to NULL and is written by consumerConnect and consumerDisconnect.
487 sp<ConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800488
489 // mSynchronousMode whether we're in synchronous mode or not
490 bool mSynchronousMode;
491
492 // mAllowSynchronousMode whether we allow synchronous mode or not
493 const bool mAllowSynchronousMode;
494
495 // mConnectedApi indicates the API that is currently connected to this
496 // BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets updated
497 // by the connect and disconnect methods.
498 int mConnectedApi;
499
500 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
501 mutable Condition mDequeueCondition;
502
503 // mQueue is a FIFO of queued buffers used in synchronous mode
504 typedef Vector<int> Fifo;
505 Fifo mQueue;
506
507 // mAbandoned indicates that the BufferQueue will no longer be used to
508 // consume images buffers pushed to it using the ISurfaceTexture interface.
509 // It is initialized to false, and set to true in the abandon method. A
510 // BufferQueue that has been abandoned will return the NO_INIT error from
511 // all ISurfaceTexture methods capable of returning an error.
512 bool mAbandoned;
513
514 // mName is a string used to identify the BufferQueue in log messages.
515 // It is set by the setName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800516 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800517
518 // mMutex is the mutex used to prevent concurrent access to the member
519 // variables of BufferQueue objects. It must be locked whenever the
520 // member variables are accessed.
521 mutable Mutex mMutex;
522
523 // mFrameCounter is the free running counter, incremented for every buffer queued
524 // with the surface Texture.
525 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800526
Daniel Lamb2675792012-02-23 14:35:13 -0800527 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
528 // by changing the buffer count.
Daniel Lameae59d22012-01-22 15:26:27 -0800529 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800530
531 // mDefaultBufferFormat can be set so it will override
532 // the buffer format when it isn't specified in dequeueBuffer
533 uint32_t mDefaultBufferFormat;
534
535 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
536 uint32_t mConsumerUsageBits;
537
538 // mTransformHint is used to optimize for screen rotations
539 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800540};
541
542// ----------------------------------------------------------------------------
543}; // namespace android
544
545#endif // ANDROID_GUI_BUFFERQUEUE_H