blob: 408956b6e7f3dff85adebf1ba6e4700506827baf [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 Agopian365857d2013-09-11 19:35:45 -070023#include <binder/IBinder.h>
24
Mathias Agopiana4e19522013-07-31 20:09:53 -070025#include <gui/IConsumerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/IGraphicBufferAlloc.h>
Andy McFadden2adaf042012-12-18 09:49:45 -080027#include <gui/IGraphicBufferProducer.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070028#include <gui/IGraphicBufferConsumer.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080029
Jesse Hallef194142012-06-14 14:45:17 -070030#include <ui/Fence.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080031#include <ui/GraphicBuffer.h>
32
33#include <utils/String8.h>
34#include <utils/Vector.h>
35#include <utils/threads.h>
36
37namespace android {
38// ----------------------------------------------------------------------------
39
Mathias Agopian365857d2013-09-11 19:35:45 -070040class BufferQueue : public BnGraphicBufferProducer,
41 public BnGraphicBufferConsumer,
42 private IBinder::DeathRecipient {
Daniel Lam6b091c52012-01-22 15:26:27 -080043public:
44 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
Daniel Lam6b091c52012-01-22 15:26:27 -080045 enum { NUM_BUFFER_SLOTS = 32 };
46 enum { NO_CONNECTED_API = 0 };
Daniel Lameae59d22012-01-22 15:26:27 -080047 enum { INVALID_BUFFER_SLOT = -1 };
Andy McFadden1585c4d2013-06-28 13:52:40 -070048 enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE, PRESENT_LATER };
Daniel Lam6b091c52012-01-22 15:26:27 -080049
Jamie Gennisc68f2ec2012-08-30 18:36:22 -070050 // When in async mode we reserve two slots in order to guarantee that the
51 // producer and consumer can run asynchronously.
52 enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
53
Mathias Agopiana4e19522013-07-31 20:09:53 -070054 // for backward source compatibility
55 typedef ::android::ConsumerListener ConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -080056
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070057 // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
58 // reference to the actual consumer object. It forwards all calls to that
59 // consumer object so long as it exists.
60 //
61 // This class exists to avoid having a circular reference between the
62 // BufferQueue object and the consumer object. The reason this can't be a weak
63 // reference in the BufferQueue class is because we're planning to expose the
64 // consumer side of a BufferQueue as a binder interface, which doesn't support
65 // weak references.
Mathias Agopiana4e19522013-07-31 20:09:53 -070066 class ProxyConsumerListener : public BnConsumerListener {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070067 public:
Mathias Agopiana4e19522013-07-31 20:09:53 -070068 ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070069 virtual ~ProxyConsumerListener();
70 virtual void onFrameAvailable();
71 virtual void onBuffersReleased();
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070072 private:
Mathias Agopiana4e19522013-07-31 20:09:53 -070073 // mConsumerListener is a weak reference to the IConsumerListener. This is
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070074 // the raison d'etre of ProxyConsumerListener.
Mathias Agopiana4e19522013-07-31 20:09:53 -070075 wp<ConsumerListener> mConsumerListener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070076 };
77
78
Jamie Gennis72f096f2012-08-27 18:48:37 -070079 // BufferQueue manages a pool of gralloc memory slots to be used by
Mathias Agopian595264f2013-07-16 22:56:09 -070080 // producers and consumers. allocator is used to allocate all the
81 // needed gralloc buffers.
82 BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL);
Daniel Lam6b091c52012-01-22 15:26:27 -080083 virtual ~BufferQueue();
84
Mathias Agopiana4e19522013-07-31 20:09:53 -070085 /*
Mathias Agopian365857d2013-09-11 19:35:45 -070086 * IBinder::DeathRecipient interface
87 */
88
89 virtual void binderDied(const wp<IBinder>& who);
90
91 /*
Mathias Agopiana4e19522013-07-31 20:09:53 -070092 * IGraphicBufferProducer interface
93 */
94
Andy McFadden753e3412013-04-04 17:09:03 -070095 // Query native window attributes. The "what" values are enumerated in
96 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -080097 virtual int query(int what, int* value);
98
Andy McFadden753e3412013-04-04 17:09:03 -070099 // setBufferCount updates the number of available buffer slots. If this
100 // method succeeds, buffer slots will be both unallocated and owned by
101 // the BufferQueue object (i.e. they are not owned by the producer or
102 // consumer).
103 //
104 // This will fail if the producer has dequeued any buffers, or if
105 // bufferCount is invalid. bufferCount must generally be a value
106 // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
107 // (inclusive). It may also be set to zero (the default) to indicate
108 // that the producer does not wish to set a value. The minimum value
109 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
110 // ...).
111 //
112 // This may only be called by the producer. The consumer will be told
113 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800114 virtual status_t setBufferCount(int bufferCount);
115
Andy McFadden753e3412013-04-04 17:09:03 -0700116 // requestBuffer returns the GraphicBuffer for slot N.
117 //
118 // In normal operation, this is called the first time slot N is returned
119 // by dequeueBuffer. It must be called again if dequeueBuffer returns
120 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800121 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
122
Andy McFadden753e3412013-04-04 17:09:03 -0700123 // dequeueBuffer gets the next buffer slot index for the producer to use.
124 // If a buffer slot is available then that slot index is written to the
125 // location pointed to by the buf argument and a status of OK is returned.
126 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800127 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700128 //
129 // The fence parameter will be updated to hold the fence associated with
130 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700131 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
132 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700133 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800134 // The width and height parameters must be no greater than the minimum of
135 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
136 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700137 // updateTexImage() is called. If width and height are both zero, the
138 // default values specified by setDefaultBufferSize() are used instead.
139 //
140 // The pixel formats are enumerated in graphics.h, e.g.
141 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
142 // will be used.
143 //
144 // The usage argument specifies gralloc buffer usage flags. The values
145 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
146 // will be merged with the usage flags specified by setConsumerUsageBits.
147 //
148 // The return value may be a negative error value or a non-negative
149 // collection of flags. If the flags are set, the return values are
150 // valid, but additional actions must be performed.
151 //
152 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
153 // producer must discard cached GraphicBuffer references for the slot
154 // returned in buf.
155 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
156 // must discard cached GraphicBuffer references for all slots.
157 //
158 // In both cases, the producer will need to call requestBuffer to get a
159 // GraphicBuffer handle for the returned slot.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700160 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700161 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800162
Andy McFadden753e3412013-04-04 17:09:03 -0700163 // queueBuffer returns a filled buffer to the BufferQueue.
164 //
165 // Additional data is provided in the QueueBufferInput struct. Notably,
166 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800167 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700168 // (zero point, etc) are producer-specific and should be documented by the
169 // producer.
170 //
171 // The caller may provide a fence that signals when all rendering
172 // operations have completed. Alternatively, NO_FENCE may be used,
173 // indicating that the buffer is ready immediately.
174 //
175 // Some values are returned in the output struct: the current settings
176 // for default width and height, the current transform hint, and the
177 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700178 virtual status_t queueBuffer(int buf,
179 const QueueBufferInput& input, QueueBufferOutput* output);
180
Andy McFadden753e3412013-04-04 17:09:03 -0700181 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
182 // queue it for use by the consumer.
183 //
184 // The buffer will not be overwritten until the fence signals. The fence
185 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700186 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800187
Andy McFadden753e3412013-04-04 17:09:03 -0700188 // connect attempts to connect a producer API to the BufferQueue. This
189 // must be called before any other IGraphicBufferProducer methods are
190 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800191 //
Andy McFadden753e3412013-04-04 17:09:03 -0700192 // This method will fail if connect was previously called on the
193 // BufferQueue and no corresponding disconnect call was made (i.e. if
194 // it's still connected to a producer).
195 //
196 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian365857d2013-09-11 19:35:45 -0700197 virtual status_t connect(const sp<IBinder>& token,
198 int api, bool producerControlledByApp, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800199
Andy McFadden753e3412013-04-04 17:09:03 -0700200 // disconnect attempts to disconnect a producer API from the BufferQueue.
201 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800202 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800203 // Successfully calling connect after this will allow the other methods to
204 // succeed again.
205 //
206 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700207 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800208 virtual status_t disconnect(int api);
209
Mathias Agopiana4e19522013-07-31 20:09:53 -0700210 /*
211 * IGraphicBufferConsumer interface
212 */
Daniel Lameae59d22012-01-22 15:26:27 -0800213
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700214 // acquireBuffer attempts to acquire ownership of the next pending buffer in
215 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
216 // buffer is successfully acquired, the information about the buffer is
217 // returned in BufferItem. If the buffer returned had previously been
218 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
219 // NULL and it is assumed that the consumer still holds a reference to the
220 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700221 //
222 // If presentWhen is nonzero, it indicates the time when the buffer will
223 // be displayed on screen. If the buffer's timestamp is farther in the
224 // future, the buffer won't be acquired, and PRESENT_LATER will be
225 // returned. The presentation time is in nanoseconds, and the time base
226 // is CLOCK_MONOTONIC.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700227 virtual status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800228
229 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700230 // BufferQueue. This may be done while the buffer's contents are still
231 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700232 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700233 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700234 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
235 // any references to the just-released buffer that it might have, as if it
236 // had received a onBuffersReleased() call with a mask set for the released
237 // buffer.
238 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700239 // Note that the dependencies on EGL will be removed once we switch to using
240 // the Android HW Sync HAL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700241 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700242 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700243 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800244
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700245 // consumerConnect connects a consumer to the BufferQueue. Only one
246 // consumer may be connected, and when that consumer disconnects the
247 // BufferQueue is placed into the "abandoned" state, causing most
248 // interactions with the BufferQueue by the producer to fail.
Mathias Agopian595264f2013-07-16 22:56:09 -0700249 // controlledByApp indicates whether the consumer is controlled by
250 // the application.
Andy McFadden753e3412013-04-04 17:09:03 -0700251 //
252 // consumer may not be NULL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700253 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700254
Daniel Lameae59d22012-01-22 15:26:27 -0800255 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700256 // buffers will be freed and the BufferQueue is placed in the "abandoned"
257 // state, causing most interactions with the BufferQueue by the producer to
258 // fail.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700259 virtual status_t consumerDisconnect();
Daniel Lameae59d22012-01-22 15:26:27 -0800260
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700261 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700262 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700263 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700264 //
265 // This should be called from the onBuffersReleased() callback.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700266 virtual status_t getReleasedBuffers(uint32_t* slotMask);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700267
Daniel Lameae59d22012-01-22 15:26:27 -0800268 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700269 // dequeueBuffer when a width and height of zero is requested. Default
270 // is 1x1.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700271 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h);
Daniel Lameae59d22012-01-22 15:26:27 -0800272
Andy McFadden753e3412013-04-04 17:09:03 -0700273 // setDefaultMaxBufferCount sets the default value for the maximum buffer
274 // count (the initial default is 2). If the producer has requested a
275 // buffer count using setBufferCount, the default buffer count will only
276 // take effect if the producer sets the count back to zero.
277 //
278 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700279 virtual status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800280
Mathias Agopianad678e12013-07-23 17:28:53 -0700281 // disableAsyncBuffer disables the extra buffer used in async mode
282 // (when both producer and consumer have set their "isControlledByApp"
283 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
284 //
285 // This can only be called before consumerConnect().
Mathias Agopiana4e19522013-07-31 20:09:53 -0700286 virtual status_t disableAsyncBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700287
Jamie Gennis72f096f2012-08-27 18:48:37 -0700288 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700289 // be acquired by the consumer at one time (default 1). This call will
290 // fail if a producer is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700291 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700292
Daniel Lameae59d22012-01-22 15:26:27 -0800293 // setConsumerName sets the name used in logging
Mathias Agopiana4e19522013-07-31 20:09:53 -0700294 virtual void setConsumerName(const String8& name);
Daniel Lameae59d22012-01-22 15:26:27 -0800295
Daniel Lamb2675792012-02-23 14:35:13 -0800296 // setDefaultBufferFormat allows the BufferQueue to create
297 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700298 // in dequeueBuffer. Formats are enumerated in graphics.h; the
299 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700300 virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800301
Andy McFadden753e3412013-04-04 17:09:03 -0700302 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
303 // These are merged with the bits passed to dequeueBuffer. The values are
304 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700305 virtual status_t setConsumerUsageBits(uint32_t usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800306
Andy McFadden753e3412013-04-04 17:09:03 -0700307 // setTransformHint bakes in rotation to buffers so overlays can be used.
308 // The values are enumerated in window.h, e.g.
309 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700310 virtual status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800311
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700312 // dump our state in a String
313 virtual void dump(String8& result, const char* prefix) const;
314
Mathias Agopianad678e12013-07-23 17:28:53 -0700315
Daniel Lameae59d22012-01-22 15:26:27 -0800316private:
Andy McFadden753e3412013-04-04 17:09:03 -0700317 // freeBufferLocked frees the GraphicBuffer and sync resources for the
318 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800319 void freeBufferLocked(int index);
320
Andy McFadden753e3412013-04-04 17:09:03 -0700321 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
322 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800323 void freeAllBuffersLocked();
324
Jamie Gennis31a353d2012-08-24 17:25:13 -0700325 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
326 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700327 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
328 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700329 status_t setDefaultMaxBufferCountLocked(int count);
330
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700331 // getMinUndequeuedBufferCount returns the minimum number of buffers
332 // that must remain in a state other than DEQUEUED.
333 // The async parameter tells whether we're in asynchronous mode.
334 int getMinUndequeuedBufferCount(bool async) const;
335
Jamie Gennis31a353d2012-08-24 17:25:13 -0700336 // getMinBufferCountLocked returns the minimum number of buffers allowed
337 // given the current BufferQueue state.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700338 // The async parameter tells whether we're in asynchronous mode.
339 int getMinMaxBufferCountLocked(bool async) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800340
Jamie Gennise191e6c2012-08-24 20:26:34 -0700341 // getMaxBufferCountLocked returns the maximum number of buffers that can
342 // be allocated at once. This value depends upon the following member
343 // variables:
344 //
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700345 // mDequeueBufferCannotBlock
Jamie Gennis72f096f2012-08-27 18:48:37 -0700346 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700347 // mDefaultMaxBufferCount
348 // mOverrideMaxBufferCount
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700349 // async parameter
Jamie Gennise191e6c2012-08-24 20:26:34 -0700350 //
351 // Any time one of these member variables is changed while a producer is
352 // connected, mDequeueCondition must be broadcast.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700353 int getMaxBufferCountLocked(bool async) const;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700354
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700355 // stillTracking returns true iff the buffer item is still being tracked
356 // in one of the slots.
357 bool stillTracking(const BufferItem *item) const;
358
Daniel Lam6b091c52012-01-22 15:26:27 -0800359 struct BufferSlot {
360
361 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800362 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800363 mBufferState(BufferSlot::FREE),
364 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800365 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700366 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700367 mAcquireCalled(false),
368 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800369 }
370
371 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
372 // if no buffer has been allocated.
373 sp<GraphicBuffer> mGraphicBuffer;
374
Andy McFadden753e3412013-04-04 17:09:03 -0700375 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800376 EGLDisplay mEglDisplay;
377
378 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700379 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800380 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700381 // FREE indicates that the buffer is available to be dequeued
382 // by the producer. The buffer may be in use by the consumer for
383 // a finite time, so the buffer must not be modified until the
384 // associated fence is signaled.
385 //
386 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
387 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800388 FREE = 0,
389
390 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700391 // producer, but has not yet been queued or canceled. The
392 // producer may modify the buffer's contents as soon as the
393 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800394 //
Andy McFadden753e3412013-04-04 17:09:03 -0700395 // The slot is "owned" by the producer. It can transition to
396 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800397 DEQUEUED = 1,
398
Andy McFadden753e3412013-04-04 17:09:03 -0700399 // QUEUED indicates that the buffer has been filled by the
400 // producer and queued for use by the consumer. The buffer
401 // contents may continue to be modified for a finite time, so
402 // the contents must not be accessed until the associated fence
403 // is signaled.
404 //
405 // The slot is "owned" by BufferQueue. It can transition to
406 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
407 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800408 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800409
Andy McFadden753e3412013-04-04 17:09:03 -0700410 // ACQUIRED indicates that the buffer has been acquired by the
411 // consumer. As with QUEUED, the contents must not be accessed
412 // by the consumer until the fence is signaled.
413 //
414 // The slot is "owned" by the consumer. It transitions to FREE
415 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800416 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800417 };
418
419 // mBufferState is the current state of this buffer slot.
420 BufferState mBufferState;
421
Andy McFadden753e3412013-04-04 17:09:03 -0700422 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800423 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700424 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800425 bool mRequestBufferCalled;
426
Andy McFadden753e3412013-04-04 17:09:03 -0700427 // mFrameNumber is the number of the queued frame for this slot. This
428 // is used to dequeue buffers in LRU order (useful because buffers
429 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800430 uint64_t mFrameNumber;
431
Jesse Hallc777b0b2012-06-28 12:52:05 -0700432 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800433 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700434 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
435 // new sync object in releaseBuffer. (This is deprecated in favor of
436 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700437 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800438
Jesse Hallc777b0b2012-06-28 12:52:05 -0700439 // mFence is a fence which will signal when work initiated by the
440 // previous owner of the buffer is finished. When the buffer is FREE,
441 // the fence indicates when the consumer has finished reading
442 // from the buffer, or when the producer has finished writing if it
443 // called cancelBuffer after queueing some writes. When the buffer is
444 // QUEUED, it indicates when the producer has finished filling the
445 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
446 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700447 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700448 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700449
Daniel Lameae59d22012-01-22 15:26:27 -0800450 // Indicates whether this buffer has been seen by a consumer yet
451 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700452
Andy McFadden753e3412013-04-04 17:09:03 -0700453 // Indicates whether this buffer needs to be cleaned up by the
454 // consumer. This is set when a buffer in ACQUIRED state is freed.
455 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700456 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800457 };
458
Andy McFadden753e3412013-04-04 17:09:03 -0700459 // mSlots is the array of buffer slots that must be mirrored on the
460 // producer side. This allows buffer ownership to be transferred between
461 // the producer and consumer without sending a GraphicBuffer over binder.
462 // The entire array is initialized to NULL at construction time, and
463 // buffers are allocated for a slot when requestBuffer is called with
464 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800465 BufferSlot mSlots[NUM_BUFFER_SLOTS];
466
Daniel Lam6b091c52012-01-22 15:26:27 -0800467 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700468 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800469 uint32_t mDefaultWidth;
470
471 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700472 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800473 uint32_t mDefaultHeight;
474
Jamie Gennis72f096f2012-08-27 18:48:37 -0700475 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
476 // acquire at one time. It defaults to 1 and can be changed by the
477 // consumer via the setMaxAcquiredBufferCount method, but this may only be
478 // done when no producer is connected to the BufferQueue.
479 //
480 // This value is used to derive the value returned for the
481 // MIN_UNDEQUEUED_BUFFERS query by the producer.
482 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700483
Jamie Gennis31a353d2012-08-24 17:25:13 -0700484 // mDefaultMaxBufferCount is the default limit on the number of buffers
485 // that will be allocated at one time. This default limit is set by the
486 // consumer. The limit (as opposed to the default limit) may be
487 // overridden by the producer.
488 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700489
Jamie Gennis31a353d2012-08-24 17:25:13 -0700490 // mOverrideMaxBufferCount is the limit on the number of buffers that will
491 // be allocated at one time. This value is set by the image producer by
492 // calling setBufferCount. The default is zero, which means the producer
493 // doesn't care about the number of buffers in the pool. In that case
494 // mDefaultMaxBufferCount is used as the limit.
495 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800496
Daniel Lam6b091c52012-01-22 15:26:27 -0800497 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
498 // allocate new GraphicBuffer objects.
499 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
500
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700501 // mConsumerListener is used to notify the connected consumer of
502 // asynchronous events that it may wish to react to. It is initially set
503 // to NULL and is written by consumerConnect and consumerDisconnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700504 sp<IConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800505
Mathias Agopian595264f2013-07-16 22:56:09 -0700506 // mConsumerControlledByApp whether the connected consumer is controlled by the
507 // application.
508 bool mConsumerControlledByApp;
509
510 // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
Mathias Agopianad678e12013-07-23 17:28:53 -0700511 // this flag is set during connect() when both consumer and producer are controlled
Mathias Agopian595264f2013-07-16 22:56:09 -0700512 // by the application.
513 bool mDequeueBufferCannotBlock;
514
Mathias Agopianad678e12013-07-23 17:28:53 -0700515 // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent
516 // dequeueBuffer() from ever blocking.
517 bool mUseAsyncBuffer;
518
Andy McFadden753e3412013-04-04 17:09:03 -0700519 // mConnectedApi indicates the producer API that is currently connected
520 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
521 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800522 int mConnectedApi;
523
524 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
525 mutable Condition mDequeueCondition;
526
527 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700528 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800529 Fifo mQueue;
530
531 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700532 // consume image buffers pushed to it using the IGraphicBufferProducer
533 // interface. It is initialized to false, and set to true in the
534 // consumerDisconnect method. A BufferQueue that has been abandoned will
535 // return the NO_INIT error from all IGraphicBufferProducer methods
536 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800537 bool mAbandoned;
538
Andy McFadden753e3412013-04-04 17:09:03 -0700539 // mConsumerName is a string used to identify the BufferQueue in log
540 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800541 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800542
543 // mMutex is the mutex used to prevent concurrent access to the member
544 // variables of BufferQueue objects. It must be locked whenever the
545 // member variables are accessed.
546 mutable Mutex mMutex;
547
Andy McFadden753e3412013-04-04 17:09:03 -0700548 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700549 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800550 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800551
Andy McFadden753e3412013-04-04 17:09:03 -0700552 // mBufferHasBeenQueued is true once a buffer has been queued. It is
553 // reset when something causes all buffers to be freed (e.g. changing the
554 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800555 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800556
557 // mDefaultBufferFormat can be set so it will override
558 // the buffer format when it isn't specified in dequeueBuffer
559 uint32_t mDefaultBufferFormat;
560
561 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
562 uint32_t mConsumerUsageBits;
563
564 // mTransformHint is used to optimize for screen rotations
565 uint32_t mTransformHint;
Mathias Agopian365857d2013-09-11 19:35:45 -0700566
567 // mConnectedProducerToken is used to set a binder death notification on the producer
568 sp<IBinder> mConnectedProducerToken;
Daniel Lam6b091c52012-01-22 15:26:27 -0800569};
570
571// ----------------------------------------------------------------------------
572}; // namespace android
573
574#endif // ANDROID_GUI_BUFFERQUEUE_H