blob: 6c1b6913a683d640dfdaa432e5ed87391eb9da5c [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
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;
232 virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) 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),
243 mBuf(INVALID_BUFFER_SLOT) {
244 mCrop.makeInvalid();
Andy McFadden753e3412013-04-04 17:09:03 -0700245 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800246 // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
247 // if the buffer in this slot has been acquired in the past (see
248 // BufferSlot.mAcquireCalled).
Daniel Lameae59d22012-01-22 15:26:27 -0800249 sp<GraphicBuffer> mGraphicBuffer;
250
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700251 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800252 Rect mCrop;
253
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700254 // mTransform is the current transform flags for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800255 uint32_t mTransform;
256
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700257 // mScalingMode is the current scaling mode for this buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800258 uint32_t mScalingMode;
259
260 // mTimestamp is the current timestamp for this buffer slot. This gets
261 // to set by queueBuffer each time this slot is queued.
262 int64_t mTimestamp;
263
264 // mFrameNumber is the number of the queued frame for this slot.
265 uint64_t mFrameNumber;
266
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700267 // mBuf is the slot index of this buffer
Daniel Lameae59d22012-01-22 15:26:27 -0800268 int mBuf;
Jesse Hallb42b1ac2012-06-28 14:27:53 -0700269
270 // mFence is a fence that will signal when the buffer is idle.
271 sp<Fence> mFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800272 };
273
Andy McFadden753e3412013-04-04 17:09:03 -0700274 // The following public functions are the consumer-facing interface
Daniel Lameae59d22012-01-22 15:26:27 -0800275
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700276 // acquireBuffer attempts to acquire ownership of the next pending buffer in
277 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
278 // buffer is successfully acquired, the information about the buffer is
279 // returned in BufferItem. If the buffer returned had previously been
280 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
281 // NULL and it is assumed that the consumer still holds a reference to the
282 // buffer.
283 status_t acquireBuffer(BufferItem *buffer);
Daniel Lameae59d22012-01-22 15:26:27 -0800284
285 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700286 // BufferQueue. This may be done while the buffer's contents are still
287 // being accessed. The fence will signal when the buffer is no longer
288 // in use.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700289 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700290 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
291 // any references to the just-released buffer that it might have, as if it
292 // had received a onBuffersReleased() call with a mask set for the released
293 // buffer.
294 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700295 // Note that the dependencies on EGL will be removed once we switch to using
296 // the Android HW Sync HAL.
Jesse Hallef194142012-06-14 14:45:17 -0700297 status_t releaseBuffer(int buf, 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.
Andy McFadden753e3412013-04-04 17:09:03 -0700304 //
305 // consumer may not be NULL.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700306 status_t consumerConnect(const sp<ConsumerListener>& consumer);
307
Daniel Lameae59d22012-01-22 15:26:27 -0800308 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700309 // buffers will be freed and the BufferQueue is placed in the "abandoned"
310 // state, causing most interactions with the BufferQueue by the producer to
311 // fail.
Daniel Lameae59d22012-01-22 15:26:27 -0800312 status_t consumerDisconnect();
313
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700314 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700315 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700316 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700317 //
318 // This should be called from the onBuffersReleased() callback.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700319 status_t getReleasedBuffers(uint32_t* slotMask);
320
Daniel Lameae59d22012-01-22 15:26:27 -0800321 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700322 // dequeueBuffer when a width and height of zero is requested. Default
323 // is 1x1.
Daniel Lameae59d22012-01-22 15:26:27 -0800324 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
325
Andy McFadden753e3412013-04-04 17:09:03 -0700326 // setDefaultMaxBufferCount sets the default value for the maximum buffer
327 // count (the initial default is 2). If the producer has requested a
328 // buffer count using setBufferCount, the default buffer count will only
329 // take effect if the producer sets the count back to zero.
330 //
331 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700332 status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800333
Jamie Gennis72f096f2012-08-27 18:48:37 -0700334 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700335 // be acquired by the consumer at one time (default 1). This call will
336 // fail if a producer is connected to the BufferQueue.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700337 status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
338
Andy McFadden2adaf042012-12-18 09:49:45 -0800339 // isSynchronousMode returns whether the BufferQueue is currently in
Daniel Lameae59d22012-01-22 15:26:27 -0800340 // synchronous mode.
341 bool isSynchronousMode() const;
342
343 // setConsumerName sets the name used in logging
344 void setConsumerName(const String8& name);
345
Daniel Lamb2675792012-02-23 14:35:13 -0800346 // setDefaultBufferFormat allows the BufferQueue to create
347 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700348 // in dequeueBuffer. Formats are enumerated in graphics.h; the
349 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Daniel Lamb2675792012-02-23 14:35:13 -0800350 status_t setDefaultBufferFormat(uint32_t defaultFormat);
351
Andy McFadden753e3412013-04-04 17:09:03 -0700352 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
353 // These are merged with the bits passed to dequeueBuffer. The values are
354 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Daniel Lamb2675792012-02-23 14:35:13 -0800355 status_t setConsumerUsageBits(uint32_t usage);
356
Andy McFadden753e3412013-04-04 17:09:03 -0700357 // setTransformHint bakes in rotation to buffers so overlays can be used.
358 // The values are enumerated in window.h, e.g.
359 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Daniel Lamb2675792012-02-23 14:35:13 -0800360 status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800361
362private:
Andy McFadden753e3412013-04-04 17:09:03 -0700363 // freeBufferLocked frees the GraphicBuffer and sync resources for the
364 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800365 void freeBufferLocked(int index);
366
Andy McFadden753e3412013-04-04 17:09:03 -0700367 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
368 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800369 void freeAllBuffersLocked();
370
Andy McFadden753e3412013-04-04 17:09:03 -0700371 // freeAllBuffersExceptHeadLocked frees the GraphicBuffer and sync
372 // resources for all slots except the head of mQueue.
Daniel Lam6b091c52012-01-22 15:26:27 -0800373 void freeAllBuffersExceptHeadLocked();
374
Andy McFadden753e3412013-04-04 17:09:03 -0700375 // drainQueueLocked waits for the buffer queue to empty if we're in
376 // synchronous mode, or returns immediately otherwise. It returns NO_INIT
377 // if the BufferQueue is abandoned (consumer disconnected) or disconnected
378 // (producer disconnected) during the call.
Daniel Lam6b091c52012-01-22 15:26:27 -0800379 status_t drainQueueLocked();
380
381 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
382 // synchronous mode and free all buffers. In asynchronous mode, all buffers
Andy McFadden753e3412013-04-04 17:09:03 -0700383 // are freed except the currently queued buffer (if it exists).
Daniel Lam6b091c52012-01-22 15:26:27 -0800384 status_t drainQueueAndFreeBuffersLocked();
385
Jamie Gennis31a353d2012-08-24 17:25:13 -0700386 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
387 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700388 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
389 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700390 status_t setDefaultMaxBufferCountLocked(int count);
391
392 // getMinBufferCountLocked returns the minimum number of buffers allowed
393 // given the current BufferQueue state.
394 int getMinMaxBufferCountLocked() const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800395
Jamie Gennis72f096f2012-08-27 18:48:37 -0700396 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
397 // that must remain in a state other than DEQUEUED.
398 int getMinUndequeuedBufferCountLocked() const;
399
Jamie Gennise191e6c2012-08-24 20:26:34 -0700400 // getMaxBufferCountLocked returns the maximum number of buffers that can
401 // be allocated at once. This value depends upon the following member
402 // variables:
403 //
404 // mSynchronousMode
Jamie Gennis72f096f2012-08-27 18:48:37 -0700405 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700406 // mDefaultMaxBufferCount
407 // mOverrideMaxBufferCount
408 //
409 // Any time one of these member variables is changed while a producer is
410 // connected, mDequeueCondition must be broadcast.
411 int getMaxBufferCountLocked() const;
412
Daniel Lam6b091c52012-01-22 15:26:27 -0800413 struct BufferSlot {
414
415 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800416 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800417 mBufferState(BufferSlot::FREE),
418 mRequestBufferCalled(false),
419 mTransform(0),
420 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
421 mTimestamp(0),
422 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700423 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700424 mAcquireCalled(false),
425 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800426 mCrop.makeInvalid();
427 }
428
429 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
430 // if no buffer has been allocated.
431 sp<GraphicBuffer> mGraphicBuffer;
432
Andy McFadden753e3412013-04-04 17:09:03 -0700433 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800434 EGLDisplay mEglDisplay;
435
436 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700437 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800438 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700439 // FREE indicates that the buffer is available to be dequeued
440 // by the producer. The buffer may be in use by the consumer for
441 // a finite time, so the buffer must not be modified until the
442 // associated fence is signaled.
443 //
444 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
445 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800446 FREE = 0,
447
448 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700449 // producer, but has not yet been queued or canceled. The
450 // producer may modify the buffer's contents as soon as the
451 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800452 //
Andy McFadden753e3412013-04-04 17:09:03 -0700453 // The slot is "owned" by the producer. It can transition to
454 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800455 DEQUEUED = 1,
456
Andy McFadden753e3412013-04-04 17:09:03 -0700457 // QUEUED indicates that the buffer has been filled by the
458 // producer and queued for use by the consumer. The buffer
459 // contents may continue to be modified for a finite time, so
460 // the contents must not be accessed until the associated fence
461 // is signaled.
462 //
463 // The slot is "owned" by BufferQueue. It can transition to
464 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
465 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800466 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800467
Andy McFadden753e3412013-04-04 17:09:03 -0700468 // ACQUIRED indicates that the buffer has been acquired by the
469 // consumer. As with QUEUED, the contents must not be accessed
470 // by the consumer until the fence is signaled.
471 //
472 // The slot is "owned" by the consumer. It transitions to FREE
473 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800474 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800475 };
476
477 // mBufferState is the current state of this buffer slot.
478 BufferState mBufferState;
479
Andy McFadden753e3412013-04-04 17:09:03 -0700480 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800481 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700482 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800483 bool mRequestBufferCalled;
484
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700485 // mCrop is the current crop rectangle for this buffer slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800486 Rect mCrop;
487
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700488 // mTransform is the current transform flags for this buffer slot.
Andy McFadden69052052012-09-14 16:10:11 -0700489 // (example: NATIVE_WINDOW_TRANSFORM_ROT_90)
Daniel Lam6b091c52012-01-22 15:26:27 -0800490 uint32_t mTransform;
491
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700492 // mScalingMode is the current scaling mode for this buffer slot.
Andy McFadden69052052012-09-14 16:10:11 -0700493 // (example: NATIVE_WINDOW_SCALING_MODE_FREEZE)
Daniel Lam6b091c52012-01-22 15:26:27 -0800494 uint32_t mScalingMode;
495
496 // mTimestamp is the current timestamp for this buffer slot. This gets
497 // to set by queueBuffer each time this slot is queued.
498 int64_t mTimestamp;
499
Andy McFadden753e3412013-04-04 17:09:03 -0700500 // mFrameNumber is the number of the queued frame for this slot. This
501 // is used to dequeue buffers in LRU order (useful because buffers
502 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800503 uint64_t mFrameNumber;
504
Jesse Hallc777b0b2012-06-28 12:52:05 -0700505 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800506 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700507 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
508 // new sync object in releaseBuffer. (This is deprecated in favor of
509 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700510 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800511
Jesse Hallc777b0b2012-06-28 12:52:05 -0700512 // mFence is a fence which will signal when work initiated by the
513 // previous owner of the buffer is finished. When the buffer is FREE,
514 // the fence indicates when the consumer has finished reading
515 // from the buffer, or when the producer has finished writing if it
516 // called cancelBuffer after queueing some writes. When the buffer is
517 // QUEUED, it indicates when the producer has finished filling the
518 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
519 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700520 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700521 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700522
Daniel Lameae59d22012-01-22 15:26:27 -0800523 // Indicates whether this buffer has been seen by a consumer yet
524 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700525
Andy McFadden753e3412013-04-04 17:09:03 -0700526 // Indicates whether this buffer needs to be cleaned up by the
527 // consumer. This is set when a buffer in ACQUIRED state is freed.
528 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700529 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800530 };
531
Andy McFadden753e3412013-04-04 17:09:03 -0700532 // mSlots is the array of buffer slots that must be mirrored on the
533 // producer side. This allows buffer ownership to be transferred between
534 // the producer and consumer without sending a GraphicBuffer over binder.
535 // The entire array is initialized to NULL at construction time, and
536 // buffers are allocated for a slot when requestBuffer is called with
537 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800538 BufferSlot mSlots[NUM_BUFFER_SLOTS];
539
Daniel Lam6b091c52012-01-22 15:26:27 -0800540 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700541 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800542 uint32_t mDefaultWidth;
543
544 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700545 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800546 uint32_t mDefaultHeight;
547
Jamie Gennis72f096f2012-08-27 18:48:37 -0700548 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
549 // acquire at one time. It defaults to 1 and can be changed by the
550 // consumer via the setMaxAcquiredBufferCount method, but this may only be
551 // done when no producer is connected to the BufferQueue.
552 //
553 // This value is used to derive the value returned for the
554 // MIN_UNDEQUEUED_BUFFERS query by the producer.
555 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700556
Jamie Gennis31a353d2012-08-24 17:25:13 -0700557 // mDefaultMaxBufferCount is the default limit on the number of buffers
558 // that will be allocated at one time. This default limit is set by the
559 // consumer. The limit (as opposed to the default limit) may be
560 // overridden by the producer.
561 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700562
Jamie Gennis31a353d2012-08-24 17:25:13 -0700563 // mOverrideMaxBufferCount is the limit on the number of buffers that will
564 // be allocated at one time. This value is set by the image producer by
565 // calling setBufferCount. The default is zero, which means the producer
566 // doesn't care about the number of buffers in the pool. In that case
567 // mDefaultMaxBufferCount is used as the limit.
568 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800569
Daniel Lam6b091c52012-01-22 15:26:27 -0800570 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
571 // allocate new GraphicBuffer objects.
572 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
573
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700574 // mConsumerListener is used to notify the connected consumer of
575 // asynchronous events that it may wish to react to. It is initially set
576 // to NULL and is written by consumerConnect and consumerDisconnect.
577 sp<ConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800578
579 // mSynchronousMode whether we're in synchronous mode or not
580 bool mSynchronousMode;
581
Andy McFadden753e3412013-04-04 17:09:03 -0700582 // mAllowSynchronousMode whether we allow synchronous mode or not. Set
583 // when the BufferQueue is created (by the consumer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800584 const bool mAllowSynchronousMode;
585
Andy McFadden753e3412013-04-04 17:09:03 -0700586 // mConnectedApi indicates the producer API that is currently connected
587 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
588 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800589 int mConnectedApi;
590
591 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
592 mutable Condition mDequeueCondition;
593
594 // mQueue is a FIFO of queued buffers used in synchronous mode
595 typedef Vector<int> Fifo;
596 Fifo mQueue;
597
598 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700599 // consume image buffers pushed to it using the IGraphicBufferProducer
600 // interface. It is initialized to false, and set to true in the
601 // consumerDisconnect method. A BufferQueue that has been abandoned will
602 // return the NO_INIT error from all IGraphicBufferProducer methods
603 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800604 bool mAbandoned;
605
Andy McFadden753e3412013-04-04 17:09:03 -0700606 // mConsumerName is a string used to identify the BufferQueue in log
607 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800608 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800609
610 // mMutex is the mutex used to prevent concurrent access to the member
611 // variables of BufferQueue objects. It must be locked whenever the
612 // member variables are accessed.
613 mutable Mutex mMutex;
614
Andy McFadden753e3412013-04-04 17:09:03 -0700615 // mFrameCounter is the free running counter, incremented on every
616 // successful queueBuffer call.
Daniel Lam6b091c52012-01-22 15:26:27 -0800617 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800618
Andy McFadden753e3412013-04-04 17:09:03 -0700619 // mBufferHasBeenQueued is true once a buffer has been queued. It is
620 // reset when something causes all buffers to be freed (e.g. changing the
621 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800622 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800623
624 // mDefaultBufferFormat can be set so it will override
625 // the buffer format when it isn't specified in dequeueBuffer
626 uint32_t mDefaultBufferFormat;
627
628 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
629 uint32_t mConsumerUsageBits;
630
631 // mTransformHint is used to optimize for screen rotations
632 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800633};
634
635// ----------------------------------------------------------------------------
636}; // namespace android
637
638#endif // ANDROID_GUI_BUFFERQUEUE_H