blob: 0143be3fd32b857e676aa541cb0989663b45c6d8 [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
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
Andy McFadden753e3412013-04-04 17:09:03 -0700107 // Query native window attributes. The "what" values are enumerated in
108 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -0800109 virtual int query(int what, int* value);
110
Andy McFadden753e3412013-04-04 17:09:03 -0700111 // setBufferCount updates the number of available buffer slots. If this
112 // method succeeds, buffer slots will be both unallocated and owned by
113 // the BufferQueue object (i.e. they are not owned by the producer or
114 // consumer).
115 //
116 // This will fail if the producer has dequeued any buffers, or if
117 // bufferCount is invalid. bufferCount must generally be a value
118 // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
119 // (inclusive). It may also be set to zero (the default) to indicate
120 // that the producer does not wish to set a value. The minimum value
121 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
122 // ...).
123 //
124 // This may only be called by the producer. The consumer will be told
125 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800126 virtual status_t setBufferCount(int bufferCount);
127
Andy McFadden753e3412013-04-04 17:09:03 -0700128 // requestBuffer returns the GraphicBuffer for slot N.
129 //
130 // In normal operation, this is called the first time slot N is returned
131 // by dequeueBuffer. It must be called again if dequeueBuffer returns
132 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800133 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
134
Andy McFadden753e3412013-04-04 17:09:03 -0700135 // dequeueBuffer gets the next buffer slot index for the producer to use.
136 // If a buffer slot is available then that slot index is written to the
137 // location pointed to by the buf argument and a status of OK is returned.
138 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800139 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700140 //
141 // The fence parameter will be updated to hold the fence associated with
142 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700143 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
144 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700145 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800146 // The width and height parameters must be no greater than the minimum of
147 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
148 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700149 // updateTexImage() is called. If width and height are both zero, the
150 // default values specified by setDefaultBufferSize() are used instead.
151 //
152 // The pixel formats are enumerated in graphics.h, e.g.
153 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
154 // will be used.
155 //
156 // The usage argument specifies gralloc buffer usage flags. The values
157 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
158 // will be merged with the usage flags specified by setConsumerUsageBits.
159 //
160 // The return value may be a negative error value or a non-negative
161 // collection of flags. If the flags are set, the return values are
162 // valid, but additional actions must be performed.
163 //
164 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
165 // producer must discard cached GraphicBuffer references for the slot
166 // returned in buf.
167 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
168 // must discard cached GraphicBuffer references for all slots.
169 //
170 // In both cases, the producer will need to call requestBuffer to get a
171 // GraphicBuffer handle for the returned slot.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700172 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700173 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800174
Andy McFadden753e3412013-04-04 17:09:03 -0700175 // queueBuffer returns a filled buffer to the BufferQueue.
176 //
177 // Additional data is provided in the QueueBufferInput struct. Notably,
178 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800179 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700180 // (zero point, etc) are producer-specific and should be documented by the
181 // producer.
182 //
183 // The caller may provide a fence that signals when all rendering
184 // operations have completed. Alternatively, NO_FENCE may be used,
185 // indicating that the buffer is ready immediately.
186 //
187 // Some values are returned in the output struct: the current settings
188 // for default width and height, the current transform hint, and the
189 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700190 virtual status_t queueBuffer(int buf,
191 const QueueBufferInput& input, QueueBufferOutput* output);
192
Andy McFadden753e3412013-04-04 17:09:03 -0700193 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
194 // queue it for use by the consumer.
195 //
196 // The buffer will not be overwritten until the fence signals. The fence
197 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700198 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800199
Andy McFadden753e3412013-04-04 17:09:03 -0700200 // setSynchronousMode sets whether dequeueBuffer is synchronous or
Daniel Lam6b091c52012-01-22 15:26:27 -0800201 // asynchronous. In synchronous mode, dequeueBuffer blocks until
202 // a buffer is available, the currently bound buffer can be dequeued and
Andy McFadden753e3412013-04-04 17:09:03 -0700203 // queued buffers will be acquired in order. In asynchronous mode,
204 // a queued buffer may be replaced by a subsequently queued buffer.
205 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800206 // The default mode is asynchronous.
207 virtual status_t setSynchronousMode(bool enabled);
208
Andy McFadden753e3412013-04-04 17:09:03 -0700209 // connect attempts to connect a producer API to the BufferQueue. This
210 // must be called before any other IGraphicBufferProducer methods are
211 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800212 //
Andy McFadden753e3412013-04-04 17:09:03 -0700213 // This method will fail if connect was previously called on the
214 // BufferQueue and no corresponding disconnect call was made (i.e. if
215 // it's still connected to a producer).
216 //
217 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian24202f52012-04-23 14:28:58 -0700218 virtual status_t connect(int api, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800219
Andy McFadden753e3412013-04-04 17:09:03 -0700220 // disconnect attempts to disconnect a producer API from the BufferQueue.
221 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800222 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800223 // Successfully calling connect after this will allow the other methods to
224 // succeed again.
225 //
226 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700227 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800228 virtual status_t disconnect(int api);
229
Daniel Lameae59d22012-01-22 15:26:27 -0800230 // dump our state in a String
231 virtual void dump(String8& result) const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200232 virtual void dump(String8& result, const char* prefix) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800233
Daniel Lameae59d22012-01-22 15:26:27 -0800234 // public facing structure for BufferSlot
235 struct BufferItem {
236
237 BufferItem()
238 :
239 mTransform(0),
240 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
241 mTimestamp(0),
242 mFrameNumber(0),
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700243 mBuf(INVALID_BUFFER_SLOT),
244 mAcquireCalled(false) {
Daniel Lameae59d22012-01-22 15:26:27 -0800245 mCrop.makeInvalid();
Andy McFadden753e3412013-04-04 17:09:03 -0700246 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800247 // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
248 // if the buffer in this slot has been acquired in the past (see
249 // BufferSlot.mAcquireCalled).
Daniel Lameae59d22012-01-22 15:26:27 -0800250 sp<GraphicBuffer> mGraphicBuffer;
251
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700252 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800253 Rect mCrop;
254
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700255 // mTransform is the current transform flags for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800256 uint32_t mTransform;
257
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700258 // mScalingMode is the current scaling mode for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800259 uint32_t mScalingMode;
260
261 // mTimestamp is the current timestamp for this buffer slot. This gets
262 // to set by queueBuffer each time this slot is queued.
263 int64_t mTimestamp;
264
265 // mFrameNumber is the number of the queued frame for this slot.
266 uint64_t mFrameNumber;
267
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700268 // mBuf is the slot index of this buffer
Daniel Lameae59d22012-01-22 15:26:27 -0800269 int mBuf;
Jesse Hallb42b1ac2012-06-28 14:27:53 -0700270
271 // mFence is a fence that will signal when the buffer is idle.
272 sp<Fence> mFence;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700273
274 // Indicates whether this buffer has been seen by a consumer yet
275 bool mAcquireCalled;
Daniel Lameae59d22012-01-22 15:26:27 -0800276 };
277
Andy McFadden753e3412013-04-04 17:09:03 -0700278 // The following public functions are the consumer-facing interface
Daniel Lameae59d22012-01-22 15:26:27 -0800279
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700280 // acquireBuffer attempts to acquire ownership of the next pending buffer in
281 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
282 // buffer is successfully acquired, the information about the buffer is
283 // returned in BufferItem. If the buffer returned had previously been
284 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
285 // NULL and it is assumed that the consumer still holds a reference to the
286 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700287 //
288 // If presentWhen is nonzero, it indicates the time when the buffer will
289 // be displayed on screen. If the buffer's timestamp is farther in the
290 // future, the buffer won't be acquired, and PRESENT_LATER will be
291 // returned. The presentation time is in nanoseconds, and the time base
292 // is CLOCK_MONOTONIC.
293 status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800294
295 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700296 // BufferQueue. This may be done while the buffer's contents are still
297 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700298 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700299 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700300 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
301 // any references to the just-released buffer that it might have, as if it
302 // had received a onBuffersReleased() call with a mask set for the released
303 // buffer.
304 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700305 // Note that the dependencies on EGL will be removed once we switch to using
306 // the Android HW Sync HAL.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700307 status_t releaseBuffer(int buf, uint64_t frameNumber,
308 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700309 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800310
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700311 // consumerConnect connects a consumer to the BufferQueue. Only one
312 // consumer may be connected, and when that consumer disconnects the
313 // BufferQueue is placed into the "abandoned" state, causing most
314 // interactions with the BufferQueue by the producer to fail.
Andy McFadden753e3412013-04-04 17:09:03 -0700315 //
316 // consumer may not be NULL.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700317 status_t consumerConnect(const sp<ConsumerListener>& consumer);
318
Daniel Lameae59d22012-01-22 15:26:27 -0800319 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700320 // buffers will be freed and the BufferQueue is placed in the "abandoned"
321 // state, causing most interactions with the BufferQueue by the producer to
322 // fail.
Daniel Lameae59d22012-01-22 15:26:27 -0800323 status_t consumerDisconnect();
324
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700325 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700326 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700327 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700328 //
329 // This should be called from the onBuffersReleased() callback.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700330 status_t getReleasedBuffers(uint32_t* slotMask);
331
Daniel Lameae59d22012-01-22 15:26:27 -0800332 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700333 // dequeueBuffer when a width and height of zero is requested. Default
334 // is 1x1.
Daniel Lameae59d22012-01-22 15:26:27 -0800335 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
336
Andy McFadden753e3412013-04-04 17:09:03 -0700337 // setDefaultMaxBufferCount sets the default value for the maximum buffer
338 // count (the initial default is 2). If the producer has requested a
339 // buffer count using setBufferCount, the default buffer count will only
340 // take effect if the producer sets the count back to zero.
341 //
342 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700343 status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800344
Jamie Gennis72f096f2012-08-27 18:48:37 -0700345 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700346 // be acquired by the consumer at one time (default 1). This call will
347 // fail if a producer is connected to the BufferQueue.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700348 status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
349
Andy McFadden2adaf042012-12-18 09:49:45 -0800350 // isSynchronousMode returns whether the BufferQueue is currently in
Daniel Lameae59d22012-01-22 15:26:27 -0800351 // synchronous mode.
352 bool isSynchronousMode() const;
353
354 // setConsumerName sets the name used in logging
355 void setConsumerName(const String8& name);
356
Daniel Lamb2675792012-02-23 14:35:13 -0800357 // setDefaultBufferFormat allows the BufferQueue to create
358 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700359 // in dequeueBuffer. Formats are enumerated in graphics.h; the
360 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Daniel Lamb2675792012-02-23 14:35:13 -0800361 status_t setDefaultBufferFormat(uint32_t defaultFormat);
362
Andy McFadden753e3412013-04-04 17:09:03 -0700363 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
364 // These are merged with the bits passed to dequeueBuffer. The values are
365 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Daniel Lamb2675792012-02-23 14:35:13 -0800366 status_t setConsumerUsageBits(uint32_t usage);
367
Andy McFadden753e3412013-04-04 17:09:03 -0700368 // setTransformHint bakes in rotation to buffers so overlays can be used.
369 // The values are enumerated in window.h, e.g.
370 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Daniel Lamb2675792012-02-23 14:35:13 -0800371 status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800372
373private:
Andy McFadden753e3412013-04-04 17:09:03 -0700374 // freeBufferLocked frees the GraphicBuffer and sync resources for the
375 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800376 void freeBufferLocked(int index);
377
Andy McFadden753e3412013-04-04 17:09:03 -0700378 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
379 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800380 void freeAllBuffersLocked();
381
Andy McFadden753e3412013-04-04 17:09:03 -0700382 // drainQueueLocked waits for the buffer queue to empty if we're in
383 // synchronous mode, or returns immediately otherwise. It returns NO_INIT
384 // if the BufferQueue is abandoned (consumer disconnected) or disconnected
385 // (producer disconnected) during the call.
Daniel Lam6b091c52012-01-22 15:26:27 -0800386 status_t drainQueueLocked();
387
388 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
389 // synchronous mode and free all buffers. In asynchronous mode, all buffers
Andy McFadden753e3412013-04-04 17:09:03 -0700390 // are freed except the currently queued buffer (if it exists).
Daniel Lam6b091c52012-01-22 15:26:27 -0800391 status_t drainQueueAndFreeBuffersLocked();
392
Jamie Gennis31a353d2012-08-24 17:25:13 -0700393 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
394 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700395 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
396 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700397 status_t setDefaultMaxBufferCountLocked(int count);
398
399 // getMinBufferCountLocked returns the minimum number of buffers allowed
400 // given the current BufferQueue state.
401 int getMinMaxBufferCountLocked() const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800402
Jamie Gennis72f096f2012-08-27 18:48:37 -0700403 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
404 // that must remain in a state other than DEQUEUED.
405 int getMinUndequeuedBufferCountLocked() const;
406
Jamie Gennise191e6c2012-08-24 20:26:34 -0700407 // getMaxBufferCountLocked returns the maximum number of buffers that can
408 // be allocated at once. This value depends upon the following member
409 // variables:
410 //
411 // mSynchronousMode
Jamie Gennis72f096f2012-08-27 18:48:37 -0700412 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700413 // mDefaultMaxBufferCount
414 // mOverrideMaxBufferCount
415 //
416 // Any time one of these member variables is changed while a producer is
417 // connected, mDequeueCondition must be broadcast.
418 int getMaxBufferCountLocked() const;
419
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700420 // stillTracking returns true iff the buffer item is still being tracked
421 // in one of the slots.
422 bool stillTracking(const BufferItem *item) const;
423
Daniel Lam6b091c52012-01-22 15:26:27 -0800424 struct BufferSlot {
425
426 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800427 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800428 mBufferState(BufferSlot::FREE),
429 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800430 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700431 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700432 mAcquireCalled(false),
433 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800434 }
435
436 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
437 // if no buffer has been allocated.
438 sp<GraphicBuffer> mGraphicBuffer;
439
Andy McFadden753e3412013-04-04 17:09:03 -0700440 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800441 EGLDisplay mEglDisplay;
442
443 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700444 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800445 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700446 // FREE indicates that the buffer is available to be dequeued
447 // by the producer. The buffer may be in use by the consumer for
448 // a finite time, so the buffer must not be modified until the
449 // associated fence is signaled.
450 //
451 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
452 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800453 FREE = 0,
454
455 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700456 // producer, but has not yet been queued or canceled. The
457 // producer may modify the buffer's contents as soon as the
458 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800459 //
Andy McFadden753e3412013-04-04 17:09:03 -0700460 // The slot is "owned" by the producer. It can transition to
461 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800462 DEQUEUED = 1,
463
Andy McFadden753e3412013-04-04 17:09:03 -0700464 // QUEUED indicates that the buffer has been filled by the
465 // producer and queued for use by the consumer. The buffer
466 // contents may continue to be modified for a finite time, so
467 // the contents must not be accessed until the associated fence
468 // is signaled.
469 //
470 // The slot is "owned" by BufferQueue. It can transition to
471 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
472 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800473 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800474
Andy McFadden753e3412013-04-04 17:09:03 -0700475 // ACQUIRED indicates that the buffer has been acquired by the
476 // consumer. As with QUEUED, the contents must not be accessed
477 // by the consumer until the fence is signaled.
478 //
479 // The slot is "owned" by the consumer. It transitions to FREE
480 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800481 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800482 };
483
484 // mBufferState is the current state of this buffer slot.
485 BufferState mBufferState;
486
Andy McFadden753e3412013-04-04 17:09:03 -0700487 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800488 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700489 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800490 bool mRequestBufferCalled;
491
Andy McFadden753e3412013-04-04 17:09:03 -0700492 // mFrameNumber is the number of the queued frame for this slot. This
493 // is used to dequeue buffers in LRU order (useful because buffers
494 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800495 uint64_t mFrameNumber;
496
Jesse Hallc777b0b2012-06-28 12:52:05 -0700497 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800498 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700499 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
500 // new sync object in releaseBuffer. (This is deprecated in favor of
501 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700502 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800503
Jesse Hallc777b0b2012-06-28 12:52:05 -0700504 // mFence is a fence which will signal when work initiated by the
505 // previous owner of the buffer is finished. When the buffer is FREE,
506 // the fence indicates when the consumer has finished reading
507 // from the buffer, or when the producer has finished writing if it
508 // called cancelBuffer after queueing some writes. When the buffer is
509 // QUEUED, it indicates when the producer has finished filling the
510 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
511 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700512 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700513 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700514
Daniel Lameae59d22012-01-22 15:26:27 -0800515 // Indicates whether this buffer has been seen by a consumer yet
516 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700517
Andy McFadden753e3412013-04-04 17:09:03 -0700518 // Indicates whether this buffer needs to be cleaned up by the
519 // consumer. This is set when a buffer in ACQUIRED state is freed.
520 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700521 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800522 };
523
Andy McFadden753e3412013-04-04 17:09:03 -0700524 // mSlots is the array of buffer slots that must be mirrored on the
525 // producer side. This allows buffer ownership to be transferred between
526 // the producer and consumer without sending a GraphicBuffer over binder.
527 // The entire array is initialized to NULL at construction time, and
528 // buffers are allocated for a slot when requestBuffer is called with
529 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800530 BufferSlot mSlots[NUM_BUFFER_SLOTS];
531
Daniel Lam6b091c52012-01-22 15:26:27 -0800532 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700533 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800534 uint32_t mDefaultWidth;
535
536 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700537 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800538 uint32_t mDefaultHeight;
539
Jamie Gennis72f096f2012-08-27 18:48:37 -0700540 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
541 // acquire at one time. It defaults to 1 and can be changed by the
542 // consumer via the setMaxAcquiredBufferCount method, but this may only be
543 // done when no producer is connected to the BufferQueue.
544 //
545 // This value is used to derive the value returned for the
546 // MIN_UNDEQUEUED_BUFFERS query by the producer.
547 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700548
Jamie Gennis31a353d2012-08-24 17:25:13 -0700549 // mDefaultMaxBufferCount is the default limit on the number of buffers
550 // that will be allocated at one time. This default limit is set by the
551 // consumer. The limit (as opposed to the default limit) may be
552 // overridden by the producer.
553 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700554
Jamie Gennis31a353d2012-08-24 17:25:13 -0700555 // mOverrideMaxBufferCount is the limit on the number of buffers that will
556 // be allocated at one time. This value is set by the image producer by
557 // calling setBufferCount. The default is zero, which means the producer
558 // doesn't care about the number of buffers in the pool. In that case
559 // mDefaultMaxBufferCount is used as the limit.
560 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800561
Daniel Lam6b091c52012-01-22 15:26:27 -0800562 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
563 // allocate new GraphicBuffer objects.
564 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
565
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700566 // mConsumerListener is used to notify the connected consumer of
567 // asynchronous events that it may wish to react to. It is initially set
568 // to NULL and is written by consumerConnect and consumerDisconnect.
569 sp<ConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800570
571 // mSynchronousMode whether we're in synchronous mode or not
572 bool mSynchronousMode;
573
Andy McFadden753e3412013-04-04 17:09:03 -0700574 // mAllowSynchronousMode whether we allow synchronous mode or not. Set
575 // when the BufferQueue is created (by the consumer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800576 const bool mAllowSynchronousMode;
577
Andy McFadden753e3412013-04-04 17:09:03 -0700578 // mConnectedApi indicates the producer API that is currently connected
579 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
580 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800581 int mConnectedApi;
582
583 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
584 mutable Condition mDequeueCondition;
585
586 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700587 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800588 Fifo mQueue;
589
590 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700591 // consume image buffers pushed to it using the IGraphicBufferProducer
592 // interface. It is initialized to false, and set to true in the
593 // consumerDisconnect method. A BufferQueue that has been abandoned will
594 // return the NO_INIT error from all IGraphicBufferProducer methods
595 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800596 bool mAbandoned;
597
Andy McFadden753e3412013-04-04 17:09:03 -0700598 // mConsumerName is a string used to identify the BufferQueue in log
599 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800600 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800601
602 // mMutex is the mutex used to prevent concurrent access to the member
603 // variables of BufferQueue objects. It must be locked whenever the
604 // member variables are accessed.
605 mutable Mutex mMutex;
606
Andy McFadden753e3412013-04-04 17:09:03 -0700607 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700608 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800609 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800610
Andy McFadden753e3412013-04-04 17:09:03 -0700611 // mBufferHasBeenQueued is true once a buffer has been queued. It is
612 // reset when something causes all buffers to be freed (e.g. changing the
613 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800614 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800615
616 // mDefaultBufferFormat can be set so it will override
617 // the buffer format when it isn't specified in dequeueBuffer
618 uint32_t mDefaultBufferFormat;
619
620 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
621 uint32_t mConsumerUsageBits;
622
623 // mTransformHint is used to optimize for screen rotations
624 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800625};
626
627// ----------------------------------------------------------------------------
628}; // namespace android
629
630#endif // ANDROID_GUI_BUFFERQUEUE_H