blob: c59e00e0da2350c9691cec3e003c2db633787774 [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>
Andy McFadden2adaf042012-12-18 09:49:45 -080024#include <gui/IGraphicBufferProducer.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080025
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
Andy McFadden2adaf042012-12-18 09:49:45 -080036class BufferQueue : public BnGraphicBufferProducer {
Daniel Lam6b091c52012-01-22 15:26:27 -080037public:
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.
Andy McFadden2adaf042012-12-18 09:49:45 -0800152 // This must be called before any other IGraphicBufferProducer methods are called
Daniel Lam6b091c52012-01-22 15:26:27 -0800153 // 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
Andy McFadden2adaf042012-12-18 09:49:45 -0800161 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800162 // 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 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800185 // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
186 // if the buffer in this slot has been acquired in the past (see
187 // BufferSlot.mAcquireCalled).
Daniel Lameae59d22012-01-22 15:26:27 -0800188 sp<GraphicBuffer> mGraphicBuffer;
189
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700190 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800191 Rect mCrop;
192
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700193 // mTransform is the current transform flags for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800194 uint32_t mTransform;
195
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700196 // mScalingMode is the current scaling mode for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800197 uint32_t mScalingMode;
198
199 // mTimestamp is the current timestamp for this buffer slot. This gets
200 // to set by queueBuffer each time this slot is queued.
201 int64_t mTimestamp;
202
203 // mFrameNumber is the number of the queued frame for this slot.
204 uint64_t mFrameNumber;
205
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700206 // mBuf is the slot index of this buffer
Daniel Lameae59d22012-01-22 15:26:27 -0800207 int mBuf;
Jesse Hallb42b1ac2012-06-28 14:27:53 -0700208
209 // mFence is a fence that will signal when the buffer is idle.
210 sp<Fence> mFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800211 };
212
213 // The following public functions is the consumer facing interface
214
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700215 // acquireBuffer attempts to acquire ownership of the next pending buffer in
216 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
217 // buffer is successfully acquired, the information about the buffer is
218 // returned in BufferItem. If the buffer returned had previously been
219 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
220 // NULL and it is assumed that the consumer still holds a reference to the
221 // buffer.
222 status_t acquireBuffer(BufferItem *buffer);
Daniel Lameae59d22012-01-22 15:26:27 -0800223
224 // releaseBuffer releases a buffer slot from the consumer back to the
225 // BufferQueue pending a fence sync.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700226 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700227 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
228 // any references to the just-released buffer that it might have, as if it
229 // had received a onBuffersReleased() call with a mask set for the released
230 // buffer.
231 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700232 // Note that the dependencies on EGL will be removed once we switch to using
233 // the Android HW Sync HAL.
Jesse Hallef194142012-06-14 14:45:17 -0700234 status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700235 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800236
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700237 // consumerConnect connects a consumer to the BufferQueue. Only one
238 // consumer may be connected, and when that consumer disconnects the
239 // BufferQueue is placed into the "abandoned" state, causing most
240 // interactions with the BufferQueue by the producer to fail.
241 status_t consumerConnect(const sp<ConsumerListener>& consumer);
242
Daniel Lameae59d22012-01-22 15:26:27 -0800243 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700244 // buffers will be freed and the BufferQueue is placed in the "abandoned"
245 // state, causing most interactions with the BufferQueue by the producer to
246 // fail.
Daniel Lameae59d22012-01-22 15:26:27 -0800247 status_t consumerDisconnect();
248
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700249 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
250 // indicating which buffer slots the have been released by the BufferQueue
251 // but have not yet been released by the consumer.
252 status_t getReleasedBuffers(uint32_t* slotMask);
253
Daniel Lameae59d22012-01-22 15:26:27 -0800254 // setDefaultBufferSize is used to set the size of buffers returned by
255 // requestBuffers when a with and height of zero is requested.
256 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
257
Jamie Gennis31a353d2012-08-24 17:25:13 -0700258 // setDefaultBufferCount set the buffer count. If the client has requested
Daniel Lameae59d22012-01-22 15:26:27 -0800259 // a buffer count using setBufferCount, the server-buffer count will
260 // take effect once the client sets the count back to zero.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700261 status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800262
Jamie Gennis72f096f2012-08-27 18:48:37 -0700263 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
264 // be acquired by the consumer at one time. This call will fail if a
265 // producer is connected to the BufferQueue.
266 status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
267
Andy McFadden2adaf042012-12-18 09:49:45 -0800268 // isSynchronousMode returns whether the BufferQueue is currently in
Daniel Lameae59d22012-01-22 15:26:27 -0800269 // synchronous mode.
270 bool isSynchronousMode() const;
271
272 // setConsumerName sets the name used in logging
273 void setConsumerName(const String8& name);
274
Daniel Lamb2675792012-02-23 14:35:13 -0800275 // setDefaultBufferFormat allows the BufferQueue to create
276 // GraphicBuffers of a defaultFormat if no format is specified
277 // in dequeueBuffer
278 status_t setDefaultBufferFormat(uint32_t defaultFormat);
279
280 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
281 status_t setConsumerUsageBits(uint32_t usage);
282
283 // setTransformHint bakes in rotation to buffers so overlays can be used
284 status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800285
286private:
Daniel Lam6b091c52012-01-22 15:26:27 -0800287 // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
288 // for the given slot.
289 void freeBufferLocked(int index);
290
291 // freeAllBuffersLocked frees the resources (both GraphicBuffer and
292 // EGLImage) for all slots.
293 void freeAllBuffersLocked();
294
295 // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
296 // and EGLImage) for all slots except the head of mQueue
297 void freeAllBuffersExceptHeadLocked();
298
299 // drainQueueLocked drains the buffer queue if we're in synchronous mode
300 // returns immediately otherwise. It returns NO_INIT if the BufferQueue
301 // became abandoned or disconnected during this call.
302 status_t drainQueueLocked();
303
304 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
305 // synchronous mode and free all buffers. In asynchronous mode, all buffers
306 // are freed except the current buffer.
307 status_t drainQueueAndFreeBuffersLocked();
308
Jamie Gennis31a353d2012-08-24 17:25:13 -0700309 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
310 // that will be used if the producer does not override the buffer slot
311 // count.
312 status_t setDefaultMaxBufferCountLocked(int count);
313
314 // getMinBufferCountLocked returns the minimum number of buffers allowed
315 // given the current BufferQueue state.
316 int getMinMaxBufferCountLocked() const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800317
Jamie Gennis72f096f2012-08-27 18:48:37 -0700318 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
319 // that must remain in a state other than DEQUEUED.
320 int getMinUndequeuedBufferCountLocked() const;
321
Jamie Gennise191e6c2012-08-24 20:26:34 -0700322 // getMaxBufferCountLocked returns the maximum number of buffers that can
323 // be allocated at once. This value depends upon the following member
324 // variables:
325 //
326 // mSynchronousMode
Jamie Gennis72f096f2012-08-27 18:48:37 -0700327 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700328 // mDefaultMaxBufferCount
329 // mOverrideMaxBufferCount
330 //
331 // Any time one of these member variables is changed while a producer is
332 // connected, mDequeueCondition must be broadcast.
333 int getMaxBufferCountLocked() const;
334
Daniel Lam6b091c52012-01-22 15:26:27 -0800335 struct BufferSlot {
336
337 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800338 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800339 mBufferState(BufferSlot::FREE),
340 mRequestBufferCalled(false),
341 mTransform(0),
342 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
343 mTimestamp(0),
344 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700345 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700346 mAcquireCalled(false),
347 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800348 mCrop.makeInvalid();
349 }
350
351 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
352 // if no buffer has been allocated.
353 sp<GraphicBuffer> mGraphicBuffer;
354
Daniel Lam6b091c52012-01-22 15:26:27 -0800355 // mEglDisplay is the EGLDisplay used to create mEglImage.
356 EGLDisplay mEglDisplay;
357
358 // BufferState represents the different states in which a buffer slot
359 // can be.
360 enum BufferState {
361 // FREE indicates that the buffer is not currently being used and
362 // will not be used in the future until it gets dequeued and
363 // subsequently queued by the client.
Daniel Lameae59d22012-01-22 15:26:27 -0800364 // aka "owned by BufferQueue, ready to be dequeued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800365 FREE = 0,
366
367 // DEQUEUED indicates that the buffer has been dequeued by the
368 // client, but has not yet been queued or canceled. The buffer is
369 // considered 'owned' by the client, and the server should not use
370 // it for anything.
371 //
372 // Note that when in synchronous-mode (mSynchronousMode == true),
373 // the buffer that's currently attached to the texture may be
374 // dequeued by the client. That means that the current buffer can
375 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
376 // however, the current buffer is always in the QUEUED state.
Daniel Lameae59d22012-01-22 15:26:27 -0800377 // aka "owned by producer, ready to be queued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800378 DEQUEUED = 1,
379
380 // QUEUED indicates that the buffer has been queued by the client,
381 // and has not since been made available for the client to dequeue.
382 // Attaching the buffer to the texture does NOT transition the
383 // buffer away from the QUEUED state. However, in Synchronous mode
384 // the current buffer may be dequeued by the client under some
385 // circumstances. See the note about the current buffer in the
386 // documentation for DEQUEUED.
Daniel Lameae59d22012-01-22 15:26:27 -0800387 // aka "owned by BufferQueue, ready to be acquired"
Daniel Lam6b091c52012-01-22 15:26:27 -0800388 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800389
390 // aka "owned by consumer, ready to be released"
391 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800392 };
393
394 // mBufferState is the current state of this buffer slot.
395 BufferState mBufferState;
396
397 // mRequestBufferCalled is used for validating that the client did
398 // call requestBuffer() when told to do so. Technically this is not
399 // needed but useful for debugging and catching client bugs.
400 bool mRequestBufferCalled;
401
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700402 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800403 Rect mCrop;
404
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700405 // mTransform is the current transform flags for this buffer slot.
Andy McFadden69052052012-09-14 16:10:11 -0700406 // (example: NATIVE_WINDOW_TRANSFORM_ROT_90)
Daniel Lam6b091c52012-01-22 15:26:27 -0800407 uint32_t mTransform;
408
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700409 // mScalingMode is the current scaling mode for this buffer slot.
Andy McFadden69052052012-09-14 16:10:11 -0700410 // (example: NATIVE_WINDOW_SCALING_MODE_FREEZE)
Daniel Lam6b091c52012-01-22 15:26:27 -0800411 uint32_t mScalingMode;
412
413 // mTimestamp is the current timestamp for this buffer slot. This gets
414 // to set by queueBuffer each time this slot is queued.
415 int64_t mTimestamp;
416
417 // mFrameNumber is the number of the queued frame for this slot.
418 uint64_t mFrameNumber;
419
Jesse Hallc777b0b2012-06-28 12:52:05 -0700420 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800421 // associated with this buffer slot may be dequeued. It is initialized
422 // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
423 // on a compile-time option) set to a new sync object in updateTexImage.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700424 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800425
Jesse Hallc777b0b2012-06-28 12:52:05 -0700426 // mFence is a fence which will signal when work initiated by the
427 // previous owner of the buffer is finished. When the buffer is FREE,
428 // the fence indicates when the consumer has finished reading
429 // from the buffer, or when the producer has finished writing if it
430 // called cancelBuffer after queueing some writes. When the buffer is
431 // QUEUED, it indicates when the producer has finished filling the
432 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
433 // passed to the consumer or producer along with ownership of the
434 // buffer, and mFence is empty.
435 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700436
Daniel Lameae59d22012-01-22 15:26:27 -0800437 // Indicates whether this buffer has been seen by a consumer yet
438 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700439
440 // Indicates whether this buffer needs to be cleaned up by consumer
441 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800442 };
443
444 // mSlots is the array of buffer slots that must be mirrored on the client
445 // side. This allows buffer ownership to be transferred between the client
446 // and server without sending a GraphicBuffer over binder. The entire array
447 // is initialized to NULL at construction time, and buffers are allocated
448 // for a slot when requestBuffer is called with that slot's index.
449 BufferSlot mSlots[NUM_BUFFER_SLOTS];
450
Daniel Lam6b091c52012-01-22 15:26:27 -0800451 // mDefaultWidth holds the default width of allocated buffers. It is used
452 // in requestBuffers() if a width and height of zero is specified.
453 uint32_t mDefaultWidth;
454
455 // mDefaultHeight holds the default height of allocated buffers. It is used
456 // in requestBuffers() if a width and height of zero is specified.
457 uint32_t mDefaultHeight;
458
Jamie Gennis72f096f2012-08-27 18:48:37 -0700459 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
460 // acquire at one time. It defaults to 1 and can be changed by the
461 // consumer via the setMaxAcquiredBufferCount method, but this may only be
462 // done when no producer is connected to the BufferQueue.
463 //
464 // This value is used to derive the value returned for the
465 // MIN_UNDEQUEUED_BUFFERS query by the producer.
466 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700467
Jamie Gennis31a353d2012-08-24 17:25:13 -0700468 // mDefaultMaxBufferCount is the default limit on the number of buffers
469 // that will be allocated at one time. This default limit is set by the
470 // consumer. The limit (as opposed to the default limit) may be
471 // overridden by the producer.
472 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700473
Jamie Gennis31a353d2012-08-24 17:25:13 -0700474 // mOverrideMaxBufferCount is the limit on the number of buffers that will
475 // be allocated at one time. This value is set by the image producer by
476 // calling setBufferCount. The default is zero, which means the producer
477 // doesn't care about the number of buffers in the pool. In that case
478 // mDefaultMaxBufferCount is used as the limit.
479 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800480
Daniel Lam6b091c52012-01-22 15:26:27 -0800481 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
482 // allocate new GraphicBuffer objects.
483 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
484
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700485 // mConsumerListener is used to notify the connected consumer of
486 // asynchronous events that it may wish to react to. It is initially set
487 // to NULL and is written by consumerConnect and consumerDisconnect.
488 sp<ConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800489
490 // mSynchronousMode whether we're in synchronous mode or not
491 bool mSynchronousMode;
492
493 // mAllowSynchronousMode whether we allow synchronous mode or not
494 const bool mAllowSynchronousMode;
495
496 // mConnectedApi indicates the API that is currently connected to this
497 // BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets updated
498 // by the connect and disconnect methods.
499 int mConnectedApi;
500
501 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
502 mutable Condition mDequeueCondition;
503
504 // mQueue is a FIFO of queued buffers used in synchronous mode
505 typedef Vector<int> Fifo;
506 Fifo mQueue;
507
508 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden2adaf042012-12-18 09:49:45 -0800509 // consume images buffers pushed to it using the IGraphicBufferProducer interface.
Daniel Lam6b091c52012-01-22 15:26:27 -0800510 // It is initialized to false, and set to true in the abandon method. A
511 // BufferQueue that has been abandoned will return the NO_INIT error from
Andy McFadden2adaf042012-12-18 09:49:45 -0800512 // all IGraphicBufferProducer methods capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800513 bool mAbandoned;
514
515 // mName is a string used to identify the BufferQueue in log messages.
516 // It is set by the setName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800517 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800518
519 // mMutex is the mutex used to prevent concurrent access to the member
520 // variables of BufferQueue objects. It must be locked whenever the
521 // member variables are accessed.
522 mutable Mutex mMutex;
523
524 // mFrameCounter is the free running counter, incremented for every buffer queued
525 // with the surface Texture.
526 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800527
Daniel Lamb2675792012-02-23 14:35:13 -0800528 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
529 // by changing the buffer count.
Daniel Lameae59d22012-01-22 15:26:27 -0800530 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800531
532 // mDefaultBufferFormat can be set so it will override
533 // the buffer format when it isn't specified in dequeueBuffer
534 uint32_t mDefaultBufferFormat;
535
536 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
537 uint32_t mConsumerUsageBits;
538
539 // mTransformHint is used to optimize for screen rotations
540 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800541};
542
543// ----------------------------------------------------------------------------
544}; // namespace android
545
546#endif // ANDROID_GUI_BUFFERQUEUE_H