blob: f02e25f2545e1f84fe136e8eb94b6ad60e91606b [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 };
Andy McFadden1585c4d2013-06-28 13:52:40 -070042 enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE, PRESENT_LATER };
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
Andy McFadden753e3412013-04-04 17:09:03 -070051 // the consumer to the BufferQueue, these calls from the BufferQueue to the
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070052 // 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
Mathias Agopian595264f2013-07-16 22:56:09 -0700100 // producers and consumers. allocator is used to allocate all the
101 // needed gralloc buffers.
102 BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL);
Daniel Lam6b091c52012-01-22 15:26:27 -0800103 virtual ~BufferQueue();
104
Andy McFadden753e3412013-04-04 17:09:03 -0700105 // Query native window attributes. The "what" values are enumerated in
106 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -0800107 virtual int query(int what, int* value);
108
Andy McFadden753e3412013-04-04 17:09:03 -0700109 // setBufferCount updates the number of available buffer slots. If this
110 // method succeeds, buffer slots will be both unallocated and owned by
111 // the BufferQueue object (i.e. they are not owned by the producer or
112 // consumer).
113 //
114 // This will fail if the producer has dequeued any buffers, or if
115 // bufferCount is invalid. bufferCount must generally be a value
116 // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
117 // (inclusive). It may also be set to zero (the default) to indicate
118 // that the producer does not wish to set a value. The minimum value
119 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
120 // ...).
121 //
122 // This may only be called by the producer. The consumer will be told
123 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800124 virtual status_t setBufferCount(int bufferCount);
125
Andy McFadden753e3412013-04-04 17:09:03 -0700126 // requestBuffer returns the GraphicBuffer for slot N.
127 //
128 // In normal operation, this is called the first time slot N is returned
129 // by dequeueBuffer. It must be called again if dequeueBuffer returns
130 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800131 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
132
Andy McFadden753e3412013-04-04 17:09:03 -0700133 // dequeueBuffer gets the next buffer slot index for the producer to use.
134 // If a buffer slot is available then that slot index is written to the
135 // location pointed to by the buf argument and a status of OK is returned.
136 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800137 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700138 //
139 // The fence parameter will be updated to hold the fence associated with
140 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700141 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
142 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700143 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800144 // The width and height parameters must be no greater than the minimum of
145 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
146 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700147 // updateTexImage() is called. If width and height are both zero, the
148 // default values specified by setDefaultBufferSize() are used instead.
149 //
150 // The pixel formats are enumerated in graphics.h, e.g.
151 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
152 // will be used.
153 //
154 // The usage argument specifies gralloc buffer usage flags. The values
155 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
156 // will be merged with the usage flags specified by setConsumerUsageBits.
157 //
158 // The return value may be a negative error value or a non-negative
159 // collection of flags. If the flags are set, the return values are
160 // valid, but additional actions must be performed.
161 //
162 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
163 // producer must discard cached GraphicBuffer references for the slot
164 // returned in buf.
165 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
166 // must discard cached GraphicBuffer references for all slots.
167 //
168 // In both cases, the producer will need to call requestBuffer to get a
169 // GraphicBuffer handle for the returned slot.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700170 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700171 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800172
Andy McFadden753e3412013-04-04 17:09:03 -0700173 // queueBuffer returns a filled buffer to the BufferQueue.
174 //
175 // Additional data is provided in the QueueBufferInput struct. Notably,
176 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800177 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700178 // (zero point, etc) are producer-specific and should be documented by the
179 // producer.
180 //
181 // The caller may provide a fence that signals when all rendering
182 // operations have completed. Alternatively, NO_FENCE may be used,
183 // indicating that the buffer is ready immediately.
184 //
185 // Some values are returned in the output struct: the current settings
186 // for default width and height, the current transform hint, and the
187 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700188 virtual status_t queueBuffer(int buf,
189 const QueueBufferInput& input, QueueBufferOutput* output);
190
Andy McFadden753e3412013-04-04 17:09:03 -0700191 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
192 // queue it for use by the consumer.
193 //
194 // The buffer will not be overwritten until the fence signals. The fence
195 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700196 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800197
Andy McFadden753e3412013-04-04 17:09:03 -0700198 // connect attempts to connect a producer API to the BufferQueue. This
199 // must be called before any other IGraphicBufferProducer methods are
200 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800201 //
Andy McFadden753e3412013-04-04 17:09:03 -0700202 // This method will fail if connect was previously called on the
203 // BufferQueue and no corresponding disconnect call was made (i.e. if
204 // it's still connected to a producer).
205 //
206 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian595264f2013-07-16 22:56:09 -0700207 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800208
Andy McFadden753e3412013-04-04 17:09:03 -0700209 // disconnect attempts to disconnect a producer API from the BufferQueue.
210 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800211 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800212 // Successfully calling connect after this will allow the other methods to
213 // succeed again.
214 //
215 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700216 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800217 virtual status_t disconnect(int api);
218
Daniel Lameae59d22012-01-22 15:26:27 -0800219 // dump our state in a String
220 virtual void dump(String8& result) const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200221 virtual void dump(String8& result, const char* prefix) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800222
Daniel Lameae59d22012-01-22 15:26:27 -0800223 // public facing structure for BufferSlot
224 struct BufferItem {
225
226 BufferItem()
227 :
228 mTransform(0),
229 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
230 mTimestamp(0),
231 mFrameNumber(0),
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700232 mBuf(INVALID_BUFFER_SLOT),
233 mAcquireCalled(false) {
Daniel Lameae59d22012-01-22 15:26:27 -0800234 mCrop.makeInvalid();
Andy McFadden753e3412013-04-04 17:09:03 -0700235 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800236 // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
237 // if the buffer in this slot has been acquired in the past (see
238 // BufferSlot.mAcquireCalled).
Daniel Lameae59d22012-01-22 15:26:27 -0800239 sp<GraphicBuffer> mGraphicBuffer;
240
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700241 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800242 Rect mCrop;
243
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700244 // mTransform is the current transform flags for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800245 uint32_t mTransform;
246
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700247 // mScalingMode is the current scaling mode for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800248 uint32_t mScalingMode;
249
250 // mTimestamp is the current timestamp for this buffer slot. This gets
251 // to set by queueBuffer each time this slot is queued.
252 int64_t mTimestamp;
253
254 // mFrameNumber is the number of the queued frame for this slot.
255 uint64_t mFrameNumber;
256
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700257 // mBuf is the slot index of this buffer
Daniel Lameae59d22012-01-22 15:26:27 -0800258 int mBuf;
Jesse Hallb42b1ac2012-06-28 14:27:53 -0700259
260 // mFence is a fence that will signal when the buffer is idle.
261 sp<Fence> mFence;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700262
263 // Indicates whether this buffer has been seen by a consumer yet
264 bool mAcquireCalled;
Daniel Lameae59d22012-01-22 15:26:27 -0800265 };
266
Andy McFadden753e3412013-04-04 17:09:03 -0700267 // The following public functions are the consumer-facing interface
Daniel Lameae59d22012-01-22 15:26:27 -0800268
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700269 // acquireBuffer attempts to acquire ownership of the next pending buffer in
270 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
271 // buffer is successfully acquired, the information about the buffer is
272 // returned in BufferItem. If the buffer returned had previously been
273 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
274 // NULL and it is assumed that the consumer still holds a reference to the
275 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700276 //
277 // If presentWhen is nonzero, it indicates the time when the buffer will
278 // be displayed on screen. If the buffer's timestamp is farther in the
279 // future, the buffer won't be acquired, and PRESENT_LATER will be
280 // returned. The presentation time is in nanoseconds, and the time base
281 // is CLOCK_MONOTONIC.
282 status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800283
284 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700285 // BufferQueue. This may be done while the buffer's contents are still
286 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700287 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700288 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700289 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
290 // any references to the just-released buffer that it might have, as if it
291 // had received a onBuffersReleased() call with a mask set for the released
292 // buffer.
293 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700294 // Note that the dependencies on EGL will be removed once we switch to using
295 // the Android HW Sync HAL.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700296 status_t releaseBuffer(int buf, uint64_t frameNumber,
297 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700298 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800299
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700300 // consumerConnect connects a consumer to the BufferQueue. Only one
301 // consumer may be connected, and when that consumer disconnects the
302 // BufferQueue is placed into the "abandoned" state, causing most
303 // interactions with the BufferQueue by the producer to fail.
Mathias Agopian595264f2013-07-16 22:56:09 -0700304 // controlledByApp indicates whether the consumer is controlled by
305 // the application.
Andy McFadden753e3412013-04-04 17:09:03 -0700306 //
307 // consumer may not be NULL.
Mathias Agopian595264f2013-07-16 22:56:09 -0700308 status_t consumerConnect(const sp<ConsumerListener>& consumer, bool controlledByApp);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700309
Daniel Lameae59d22012-01-22 15:26:27 -0800310 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700311 // buffers will be freed and the BufferQueue is placed in the "abandoned"
312 // state, causing most interactions with the BufferQueue by the producer to
313 // fail.
Daniel Lameae59d22012-01-22 15:26:27 -0800314 status_t consumerDisconnect();
315
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700316 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700317 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700318 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700319 //
320 // This should be called from the onBuffersReleased() callback.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700321 status_t getReleasedBuffers(uint32_t* slotMask);
322
Daniel Lameae59d22012-01-22 15:26:27 -0800323 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700324 // dequeueBuffer when a width and height of zero is requested. Default
325 // is 1x1.
Daniel Lameae59d22012-01-22 15:26:27 -0800326 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
327
Andy McFadden753e3412013-04-04 17:09:03 -0700328 // setDefaultMaxBufferCount sets the default value for the maximum buffer
329 // count (the initial default is 2). If the producer has requested a
330 // buffer count using setBufferCount, the default buffer count will only
331 // take effect if the producer sets the count back to zero.
332 //
333 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700334 status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800335
Jamie Gennis72f096f2012-08-27 18:48:37 -0700336 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700337 // be acquired by the consumer at one time (default 1). This call will
338 // fail if a producer is connected to the BufferQueue.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700339 status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
340
Daniel Lameae59d22012-01-22 15:26:27 -0800341 // setConsumerName sets the name used in logging
342 void setConsumerName(const String8& name);
343
Daniel Lamb2675792012-02-23 14:35:13 -0800344 // setDefaultBufferFormat allows the BufferQueue to create
345 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700346 // in dequeueBuffer. Formats are enumerated in graphics.h; the
347 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Daniel Lamb2675792012-02-23 14:35:13 -0800348 status_t setDefaultBufferFormat(uint32_t defaultFormat);
349
Andy McFadden753e3412013-04-04 17:09:03 -0700350 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
351 // These are merged with the bits passed to dequeueBuffer. The values are
352 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Daniel Lamb2675792012-02-23 14:35:13 -0800353 status_t setConsumerUsageBits(uint32_t usage);
354
Andy McFadden753e3412013-04-04 17:09:03 -0700355 // setTransformHint bakes in rotation to buffers so overlays can be used.
356 // The values are enumerated in window.h, e.g.
357 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Daniel Lamb2675792012-02-23 14:35:13 -0800358 status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800359
360private:
Andy McFadden753e3412013-04-04 17:09:03 -0700361 // freeBufferLocked frees the GraphicBuffer and sync resources for the
362 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800363 void freeBufferLocked(int index);
364
Andy McFadden753e3412013-04-04 17:09:03 -0700365 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
366 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800367 void freeAllBuffersLocked();
368
Andy McFadden753e3412013-04-04 17:09:03 -0700369 // drainQueueLocked waits for the buffer queue to empty if we're in
370 // synchronous mode, or returns immediately otherwise. It returns NO_INIT
371 // if the BufferQueue is abandoned (consumer disconnected) or disconnected
372 // (producer disconnected) during the call.
Daniel Lam6b091c52012-01-22 15:26:27 -0800373 status_t drainQueueLocked();
374
375 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
376 // synchronous mode and free all buffers. In asynchronous mode, all buffers
Andy McFadden753e3412013-04-04 17:09:03 -0700377 // are freed except the currently queued buffer (if it exists).
Daniel Lam6b091c52012-01-22 15:26:27 -0800378 status_t drainQueueAndFreeBuffersLocked();
379
Jamie Gennis31a353d2012-08-24 17:25:13 -0700380 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
381 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700382 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
383 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700384 status_t setDefaultMaxBufferCountLocked(int count);
385
386 // getMinBufferCountLocked returns the minimum number of buffers allowed
387 // given the current BufferQueue state.
388 int getMinMaxBufferCountLocked() const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800389
Jamie Gennis72f096f2012-08-27 18:48:37 -0700390 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
391 // that must remain in a state other than DEQUEUED.
392 int getMinUndequeuedBufferCountLocked() const;
393
Jamie Gennise191e6c2012-08-24 20:26:34 -0700394 // getMaxBufferCountLocked returns the maximum number of buffers that can
395 // be allocated at once. This value depends upon the following member
396 // variables:
397 //
398 // mSynchronousMode
Jamie Gennis72f096f2012-08-27 18:48:37 -0700399 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700400 // mDefaultMaxBufferCount
401 // mOverrideMaxBufferCount
402 //
403 // Any time one of these member variables is changed while a producer is
404 // connected, mDequeueCondition must be broadcast.
405 int getMaxBufferCountLocked() const;
406
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700407 // stillTracking returns true iff the buffer item is still being tracked
408 // in one of the slots.
409 bool stillTracking(const BufferItem *item) const;
410
Daniel Lam6b091c52012-01-22 15:26:27 -0800411 struct BufferSlot {
412
413 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800414 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800415 mBufferState(BufferSlot::FREE),
416 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800417 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700418 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700419 mAcquireCalled(false),
420 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800421 }
422
423 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
424 // if no buffer has been allocated.
425 sp<GraphicBuffer> mGraphicBuffer;
426
Andy McFadden753e3412013-04-04 17:09:03 -0700427 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800428 EGLDisplay mEglDisplay;
429
430 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700431 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800432 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700433 // FREE indicates that the buffer is available to be dequeued
434 // by the producer. The buffer may be in use by the consumer for
435 // a finite time, so the buffer must not be modified until the
436 // associated fence is signaled.
437 //
438 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
439 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800440 FREE = 0,
441
442 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700443 // producer, but has not yet been queued or canceled. The
444 // producer may modify the buffer's contents as soon as the
445 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800446 //
Andy McFadden753e3412013-04-04 17:09:03 -0700447 // The slot is "owned" by the producer. It can transition to
448 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800449 DEQUEUED = 1,
450
Andy McFadden753e3412013-04-04 17:09:03 -0700451 // QUEUED indicates that the buffer has been filled by the
452 // producer and queued for use by the consumer. The buffer
453 // contents may continue to be modified for a finite time, so
454 // the contents must not be accessed until the associated fence
455 // is signaled.
456 //
457 // The slot is "owned" by BufferQueue. It can transition to
458 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
459 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800460 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800461
Andy McFadden753e3412013-04-04 17:09:03 -0700462 // ACQUIRED indicates that the buffer has been acquired by the
463 // consumer. As with QUEUED, the contents must not be accessed
464 // by the consumer until the fence is signaled.
465 //
466 // The slot is "owned" by the consumer. It transitions to FREE
467 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800468 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800469 };
470
471 // mBufferState is the current state of this buffer slot.
472 BufferState mBufferState;
473
Andy McFadden753e3412013-04-04 17:09:03 -0700474 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800475 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700476 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800477 bool mRequestBufferCalled;
478
Andy McFadden753e3412013-04-04 17:09:03 -0700479 // mFrameNumber is the number of the queued frame for this slot. This
480 // is used to dequeue buffers in LRU order (useful because buffers
481 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800482 uint64_t mFrameNumber;
483
Jesse Hallc777b0b2012-06-28 12:52:05 -0700484 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800485 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700486 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
487 // new sync object in releaseBuffer. (This is deprecated in favor of
488 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700489 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800490
Jesse Hallc777b0b2012-06-28 12:52:05 -0700491 // mFence is a fence which will signal when work initiated by the
492 // previous owner of the buffer is finished. When the buffer is FREE,
493 // the fence indicates when the consumer has finished reading
494 // from the buffer, or when the producer has finished writing if it
495 // called cancelBuffer after queueing some writes. When the buffer is
496 // QUEUED, it indicates when the producer has finished filling the
497 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
498 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700499 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700500 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700501
Daniel Lameae59d22012-01-22 15:26:27 -0800502 // Indicates whether this buffer has been seen by a consumer yet
503 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700504
Andy McFadden753e3412013-04-04 17:09:03 -0700505 // Indicates whether this buffer needs to be cleaned up by the
506 // consumer. This is set when a buffer in ACQUIRED state is freed.
507 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700508 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800509 };
510
Andy McFadden753e3412013-04-04 17:09:03 -0700511 // mSlots is the array of buffer slots that must be mirrored on the
512 // producer side. This allows buffer ownership to be transferred between
513 // the producer and consumer without sending a GraphicBuffer over binder.
514 // The entire array is initialized to NULL at construction time, and
515 // buffers are allocated for a slot when requestBuffer is called with
516 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800517 BufferSlot mSlots[NUM_BUFFER_SLOTS];
518
Daniel Lam6b091c52012-01-22 15:26:27 -0800519 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700520 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800521 uint32_t mDefaultWidth;
522
523 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700524 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800525 uint32_t mDefaultHeight;
526
Jamie Gennis72f096f2012-08-27 18:48:37 -0700527 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
528 // acquire at one time. It defaults to 1 and can be changed by the
529 // consumer via the setMaxAcquiredBufferCount method, but this may only be
530 // done when no producer is connected to the BufferQueue.
531 //
532 // This value is used to derive the value returned for the
533 // MIN_UNDEQUEUED_BUFFERS query by the producer.
534 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700535
Jamie Gennis31a353d2012-08-24 17:25:13 -0700536 // mDefaultMaxBufferCount is the default limit on the number of buffers
537 // that will be allocated at one time. This default limit is set by the
538 // consumer. The limit (as opposed to the default limit) may be
539 // overridden by the producer.
540 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700541
Jamie Gennis31a353d2012-08-24 17:25:13 -0700542 // mOverrideMaxBufferCount is the limit on the number of buffers that will
543 // be allocated at one time. This value is set by the image producer by
544 // calling setBufferCount. The default is zero, which means the producer
545 // doesn't care about the number of buffers in the pool. In that case
546 // mDefaultMaxBufferCount is used as the limit.
547 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800548
Daniel Lam6b091c52012-01-22 15:26:27 -0800549 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
550 // allocate new GraphicBuffer objects.
551 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
552
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700553 // mConsumerListener is used to notify the connected consumer of
554 // asynchronous events that it may wish to react to. It is initially set
555 // to NULL and is written by consumerConnect and consumerDisconnect.
556 sp<ConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800557
Mathias Agopian595264f2013-07-16 22:56:09 -0700558 // mConsumerControlledByApp whether the connected consumer is controlled by the
559 // application.
560 bool mConsumerControlledByApp;
561
562 // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
563 // this flag is set durring connect() when both consumer and producer are controlled
564 // by the application.
565 bool mDequeueBufferCannotBlock;
566
Daniel Lam6b091c52012-01-22 15:26:27 -0800567 // mSynchronousMode whether we're in synchronous mode or not
568 bool mSynchronousMode;
569
Andy McFadden753e3412013-04-04 17:09:03 -0700570 // mConnectedApi indicates the producer API that is currently connected
571 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
572 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800573 int mConnectedApi;
574
575 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
576 mutable Condition mDequeueCondition;
577
578 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700579 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800580 Fifo mQueue;
581
582 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700583 // consume image buffers pushed to it using the IGraphicBufferProducer
584 // interface. It is initialized to false, and set to true in the
585 // consumerDisconnect method. A BufferQueue that has been abandoned will
586 // return the NO_INIT error from all IGraphicBufferProducer methods
587 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800588 bool mAbandoned;
589
Andy McFadden753e3412013-04-04 17:09:03 -0700590 // mConsumerName is a string used to identify the BufferQueue in log
591 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800592 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800593
594 // mMutex is the mutex used to prevent concurrent access to the member
595 // variables of BufferQueue objects. It must be locked whenever the
596 // member variables are accessed.
597 mutable Mutex mMutex;
598
Andy McFadden753e3412013-04-04 17:09:03 -0700599 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700600 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800601 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800602
Andy McFadden753e3412013-04-04 17:09:03 -0700603 // mBufferHasBeenQueued is true once a buffer has been queued. It is
604 // reset when something causes all buffers to be freed (e.g. changing the
605 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800606 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800607
608 // mDefaultBufferFormat can be set so it will override
609 // the buffer format when it isn't specified in dequeueBuffer
610 uint32_t mDefaultBufferFormat;
611
612 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
613 uint32_t mConsumerUsageBits;
614
615 // mTransformHint is used to optimize for screen rotations
616 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800617};
618
619// ----------------------------------------------------------------------------
620}; // namespace android
621
622#endif // ANDROID_GUI_BUFFERQUEUE_H