blob: 1fbfc2b4efdcb9b89d47a6ad9b87ffce8b009a01 [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 Agopiana4e19522013-07-31 20:09:53 -070023#include <gui/IConsumerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080024#include <gui/IGraphicBufferAlloc.h>
Andy McFadden2adaf042012-12-18 09:49:45 -080025#include <gui/IGraphicBufferProducer.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070026#include <gui/IGraphicBufferConsumer.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080027
Jesse Hallef194142012-06-14 14:45:17 -070028#include <ui/Fence.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080029#include <ui/GraphicBuffer.h>
30
31#include <utils/String8.h>
32#include <utils/Vector.h>
33#include <utils/threads.h>
34
35namespace android {
36// ----------------------------------------------------------------------------
37
Mathias Agopiana4e19522013-07-31 20:09:53 -070038class BufferQueue : public BnGraphicBufferProducer, public BnGraphicBufferConsumer {
Daniel Lam6b091c52012-01-22 15:26:27 -080039public:
40 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
Daniel Lam6b091c52012-01-22 15:26:27 -080041 enum { NUM_BUFFER_SLOTS = 32 };
42 enum { NO_CONNECTED_API = 0 };
Daniel Lameae59d22012-01-22 15:26:27 -080043 enum { INVALID_BUFFER_SLOT = -1 };
Andy McFadden1585c4d2013-06-28 13:52:40 -070044 enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE, PRESENT_LATER };
Daniel Lam6b091c52012-01-22 15:26:27 -080045
Jamie Gennisc68f2ec2012-08-30 18:36:22 -070046 // When in async mode we reserve two slots in order to guarantee that the
47 // producer and consumer can run asynchronously.
48 enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
49
Mathias Agopiana4e19522013-07-31 20:09:53 -070050 // for backward source compatibility
51 typedef ::android::ConsumerListener ConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -080052
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070053 // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
54 // reference to the actual consumer object. It forwards all calls to that
55 // consumer object so long as it exists.
56 //
57 // This class exists to avoid having a circular reference between the
58 // BufferQueue object and the consumer object. The reason this can't be a weak
59 // reference in the BufferQueue class is because we're planning to expose the
60 // consumer side of a BufferQueue as a binder interface, which doesn't support
61 // weak references.
Mathias Agopiana4e19522013-07-31 20:09:53 -070062 class ProxyConsumerListener : public BnConsumerListener {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070063 public:
Mathias Agopiana4e19522013-07-31 20:09:53 -070064 ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070065 virtual ~ProxyConsumerListener();
66 virtual void onFrameAvailable();
67 virtual void onBuffersReleased();
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070068 private:
Mathias Agopiana4e19522013-07-31 20:09:53 -070069 // mConsumerListener is a weak reference to the IConsumerListener. This is
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070070 // the raison d'etre of ProxyConsumerListener.
Mathias Agopiana4e19522013-07-31 20:09:53 -070071 wp<ConsumerListener> mConsumerListener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070072 };
73
74
Jamie Gennis72f096f2012-08-27 18:48:37 -070075 // BufferQueue manages a pool of gralloc memory slots to be used by
Mathias Agopian595264f2013-07-16 22:56:09 -070076 // producers and consumers. allocator is used to allocate all the
77 // needed gralloc buffers.
78 BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL);
Daniel Lam6b091c52012-01-22 15:26:27 -080079 virtual ~BufferQueue();
80
Mathias Agopiana4e19522013-07-31 20:09:53 -070081 // dump our state in a String
82 virtual void dump(String8& result) const;
83 virtual void dump(String8& result, const char* prefix) const;
84
85 /*
86 * IGraphicBufferProducer interface
87 */
88
Andy McFadden753e3412013-04-04 17:09:03 -070089 // Query native window attributes. The "what" values are enumerated in
90 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -080091 virtual int query(int what, int* value);
92
Andy McFadden753e3412013-04-04 17:09:03 -070093 // setBufferCount updates the number of available buffer slots. If this
94 // method succeeds, buffer slots will be both unallocated and owned by
95 // the BufferQueue object (i.e. they are not owned by the producer or
96 // consumer).
97 //
98 // This will fail if the producer has dequeued any buffers, or if
99 // bufferCount is invalid. bufferCount must generally be a value
100 // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
101 // (inclusive). It may also be set to zero (the default) to indicate
102 // that the producer does not wish to set a value. The minimum value
103 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
104 // ...).
105 //
106 // This may only be called by the producer. The consumer will be told
107 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800108 virtual status_t setBufferCount(int bufferCount);
109
Andy McFadden753e3412013-04-04 17:09:03 -0700110 // requestBuffer returns the GraphicBuffer for slot N.
111 //
112 // In normal operation, this is called the first time slot N is returned
113 // by dequeueBuffer. It must be called again if dequeueBuffer returns
114 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800115 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
116
Andy McFadden753e3412013-04-04 17:09:03 -0700117 // dequeueBuffer gets the next buffer slot index for the producer to use.
118 // If a buffer slot is available then that slot index is written to the
119 // location pointed to by the buf argument and a status of OK is returned.
120 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800121 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700122 //
123 // The fence parameter will be updated to hold the fence associated with
124 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700125 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
126 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700127 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800128 // The width and height parameters must be no greater than the minimum of
129 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
130 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700131 // updateTexImage() is called. If width and height are both zero, the
132 // default values specified by setDefaultBufferSize() are used instead.
133 //
134 // The pixel formats are enumerated in graphics.h, e.g.
135 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
136 // will be used.
137 //
138 // The usage argument specifies gralloc buffer usage flags. The values
139 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
140 // will be merged with the usage flags specified by setConsumerUsageBits.
141 //
142 // The return value may be a negative error value or a non-negative
143 // collection of flags. If the flags are set, the return values are
144 // valid, but additional actions must be performed.
145 //
146 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
147 // producer must discard cached GraphicBuffer references for the slot
148 // returned in buf.
149 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
150 // must discard cached GraphicBuffer references for all slots.
151 //
152 // In both cases, the producer will need to call requestBuffer to get a
153 // GraphicBuffer handle for the returned slot.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700154 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700155 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800156
Andy McFadden753e3412013-04-04 17:09:03 -0700157 // queueBuffer returns a filled buffer to the BufferQueue.
158 //
159 // Additional data is provided in the QueueBufferInput struct. Notably,
160 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800161 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700162 // (zero point, etc) are producer-specific and should be documented by the
163 // producer.
164 //
165 // The caller may provide a fence that signals when all rendering
166 // operations have completed. Alternatively, NO_FENCE may be used,
167 // indicating that the buffer is ready immediately.
168 //
169 // Some values are returned in the output struct: the current settings
170 // for default width and height, the current transform hint, and the
171 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700172 virtual status_t queueBuffer(int buf,
173 const QueueBufferInput& input, QueueBufferOutput* output);
174
Andy McFadden753e3412013-04-04 17:09:03 -0700175 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
176 // queue it for use by the consumer.
177 //
178 // The buffer will not be overwritten until the fence signals. The fence
179 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700180 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800181
Andy McFadden753e3412013-04-04 17:09:03 -0700182 // connect attempts to connect a producer API to the BufferQueue. This
183 // must be called before any other IGraphicBufferProducer methods are
184 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800185 //
Andy McFadden753e3412013-04-04 17:09:03 -0700186 // This method will fail if connect was previously called on the
187 // BufferQueue and no corresponding disconnect call was made (i.e. if
188 // it's still connected to a producer).
189 //
190 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian595264f2013-07-16 22:56:09 -0700191 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800192
Andy McFadden753e3412013-04-04 17:09:03 -0700193 // disconnect attempts to disconnect a producer API from the BufferQueue.
194 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800195 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800196 // Successfully calling connect after this will allow the other methods to
197 // succeed again.
198 //
199 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700200 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800201 virtual status_t disconnect(int api);
202
Mathias Agopiana4e19522013-07-31 20:09:53 -0700203 /*
204 * IGraphicBufferConsumer interface
205 */
Daniel Lameae59d22012-01-22 15:26:27 -0800206
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700207 // acquireBuffer attempts to acquire ownership of the next pending buffer in
208 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
209 // buffer is successfully acquired, the information about the buffer is
210 // returned in BufferItem. If the buffer returned had previously been
211 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
212 // NULL and it is assumed that the consumer still holds a reference to the
213 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700214 //
215 // If presentWhen is nonzero, it indicates the time when the buffer will
216 // be displayed on screen. If the buffer's timestamp is farther in the
217 // future, the buffer won't be acquired, and PRESENT_LATER will be
218 // returned. The presentation time is in nanoseconds, and the time base
219 // is CLOCK_MONOTONIC.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700220 virtual status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800221
222 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700223 // BufferQueue. This may be done while the buffer's contents are still
224 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700225 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700226 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700227 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
228 // any references to the just-released buffer that it might have, as if it
229 // had received a onBuffersReleased() call with a mask set for the released
230 // buffer.
231 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700232 // Note that the dependencies on EGL will be removed once we switch to using
233 // the Android HW Sync HAL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700234 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700235 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700236 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800237
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700238 // consumerConnect connects a consumer to the BufferQueue. Only one
239 // consumer may be connected, and when that consumer disconnects the
240 // BufferQueue is placed into the "abandoned" state, causing most
241 // interactions with the BufferQueue by the producer to fail.
Mathias Agopian595264f2013-07-16 22:56:09 -0700242 // controlledByApp indicates whether the consumer is controlled by
243 // the application.
Andy McFadden753e3412013-04-04 17:09:03 -0700244 //
245 // consumer may not be NULL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700246 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700247
Daniel Lameae59d22012-01-22 15:26:27 -0800248 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700249 // buffers will be freed and the BufferQueue is placed in the "abandoned"
250 // state, causing most interactions with the BufferQueue by the producer to
251 // fail.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700252 virtual status_t consumerDisconnect();
Daniel Lameae59d22012-01-22 15:26:27 -0800253
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700254 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700255 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700256 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700257 //
258 // This should be called from the onBuffersReleased() callback.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700259 virtual status_t getReleasedBuffers(uint32_t* slotMask);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700260
Daniel Lameae59d22012-01-22 15:26:27 -0800261 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700262 // dequeueBuffer when a width and height of zero is requested. Default
263 // is 1x1.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700264 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h);
Daniel Lameae59d22012-01-22 15:26:27 -0800265
Andy McFadden753e3412013-04-04 17:09:03 -0700266 // setDefaultMaxBufferCount sets the default value for the maximum buffer
267 // count (the initial default is 2). If the producer has requested a
268 // buffer count using setBufferCount, the default buffer count will only
269 // take effect if the producer sets the count back to zero.
270 //
271 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700272 virtual status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800273
Mathias Agopianad678e12013-07-23 17:28:53 -0700274 // disableAsyncBuffer disables the extra buffer used in async mode
275 // (when both producer and consumer have set their "isControlledByApp"
276 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
277 //
278 // This can only be called before consumerConnect().
Mathias Agopiana4e19522013-07-31 20:09:53 -0700279 virtual status_t disableAsyncBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700280
Jamie Gennis72f096f2012-08-27 18:48:37 -0700281 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700282 // be acquired by the consumer at one time (default 1). This call will
283 // fail if a producer is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700284 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700285
Daniel Lameae59d22012-01-22 15:26:27 -0800286 // setConsumerName sets the name used in logging
Mathias Agopiana4e19522013-07-31 20:09:53 -0700287 virtual void setConsumerName(const String8& name);
Daniel Lameae59d22012-01-22 15:26:27 -0800288
Daniel Lamb2675792012-02-23 14:35:13 -0800289 // setDefaultBufferFormat allows the BufferQueue to create
290 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700291 // in dequeueBuffer. Formats are enumerated in graphics.h; the
292 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700293 virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800294
Andy McFadden753e3412013-04-04 17:09:03 -0700295 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
296 // These are merged with the bits passed to dequeueBuffer. The values are
297 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700298 virtual status_t setConsumerUsageBits(uint32_t usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800299
Andy McFadden753e3412013-04-04 17:09:03 -0700300 // setTransformHint bakes in rotation to buffers so overlays can be used.
301 // The values are enumerated in window.h, e.g.
302 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700303 virtual status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800304
Mathias Agopianad678e12013-07-23 17:28:53 -0700305
Daniel Lameae59d22012-01-22 15:26:27 -0800306private:
Andy McFadden753e3412013-04-04 17:09:03 -0700307 // freeBufferLocked frees the GraphicBuffer and sync resources for the
308 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800309 void freeBufferLocked(int index);
310
Andy McFadden753e3412013-04-04 17:09:03 -0700311 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
312 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800313 void freeAllBuffersLocked();
314
Jamie Gennis31a353d2012-08-24 17:25:13 -0700315 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
316 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700317 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
318 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700319 status_t setDefaultMaxBufferCountLocked(int count);
320
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700321 // getMinUndequeuedBufferCount returns the minimum number of buffers
322 // that must remain in a state other than DEQUEUED.
323 // The async parameter tells whether we're in asynchronous mode.
324 int getMinUndequeuedBufferCount(bool async) const;
325
Jamie Gennis31a353d2012-08-24 17:25:13 -0700326 // getMinBufferCountLocked returns the minimum number of buffers allowed
327 // given the current BufferQueue state.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700328 // The async parameter tells whether we're in asynchronous mode.
329 int getMinMaxBufferCountLocked(bool async) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800330
Jamie Gennise191e6c2012-08-24 20:26:34 -0700331 // getMaxBufferCountLocked returns the maximum number of buffers that can
332 // be allocated at once. This value depends upon the following member
333 // variables:
334 //
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700335 // mDequeueBufferCannotBlock
Jamie Gennis72f096f2012-08-27 18:48:37 -0700336 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700337 // mDefaultMaxBufferCount
338 // mOverrideMaxBufferCount
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700339 // async parameter
Jamie Gennise191e6c2012-08-24 20:26:34 -0700340 //
341 // Any time one of these member variables is changed while a producer is
342 // connected, mDequeueCondition must be broadcast.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700343 int getMaxBufferCountLocked(bool async) const;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700344
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700345 // stillTracking returns true iff the buffer item is still being tracked
346 // in one of the slots.
347 bool stillTracking(const BufferItem *item) const;
348
Daniel Lam6b091c52012-01-22 15:26:27 -0800349 struct BufferSlot {
350
351 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800352 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800353 mBufferState(BufferSlot::FREE),
354 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800355 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700356 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700357 mAcquireCalled(false),
358 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800359 }
360
361 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
362 // if no buffer has been allocated.
363 sp<GraphicBuffer> mGraphicBuffer;
364
Andy McFadden753e3412013-04-04 17:09:03 -0700365 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800366 EGLDisplay mEglDisplay;
367
368 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700369 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800370 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700371 // FREE indicates that the buffer is available to be dequeued
372 // by the producer. The buffer may be in use by the consumer for
373 // a finite time, so the buffer must not be modified until the
374 // associated fence is signaled.
375 //
376 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
377 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800378 FREE = 0,
379
380 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700381 // producer, but has not yet been queued or canceled. The
382 // producer may modify the buffer's contents as soon as the
383 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800384 //
Andy McFadden753e3412013-04-04 17:09:03 -0700385 // The slot is "owned" by the producer. It can transition to
386 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800387 DEQUEUED = 1,
388
Andy McFadden753e3412013-04-04 17:09:03 -0700389 // QUEUED indicates that the buffer has been filled by the
390 // producer and queued for use by the consumer. The buffer
391 // contents may continue to be modified for a finite time, so
392 // the contents must not be accessed until the associated fence
393 // is signaled.
394 //
395 // The slot is "owned" by BufferQueue. It can transition to
396 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
397 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800398 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800399
Andy McFadden753e3412013-04-04 17:09:03 -0700400 // ACQUIRED indicates that the buffer has been acquired by the
401 // consumer. As with QUEUED, the contents must not be accessed
402 // by the consumer until the fence is signaled.
403 //
404 // The slot is "owned" by the consumer. It transitions to FREE
405 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800406 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800407 };
408
409 // mBufferState is the current state of this buffer slot.
410 BufferState mBufferState;
411
Andy McFadden753e3412013-04-04 17:09:03 -0700412 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800413 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700414 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800415 bool mRequestBufferCalled;
416
Andy McFadden753e3412013-04-04 17:09:03 -0700417 // mFrameNumber is the number of the queued frame for this slot. This
418 // is used to dequeue buffers in LRU order (useful because buffers
419 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800420 uint64_t mFrameNumber;
421
Jesse Hallc777b0b2012-06-28 12:52:05 -0700422 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800423 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700424 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
425 // new sync object in releaseBuffer. (This is deprecated in favor of
426 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700427 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800428
Jesse Hallc777b0b2012-06-28 12:52:05 -0700429 // mFence is a fence which will signal when work initiated by the
430 // previous owner of the buffer is finished. When the buffer is FREE,
431 // the fence indicates when the consumer has finished reading
432 // from the buffer, or when the producer has finished writing if it
433 // called cancelBuffer after queueing some writes. When the buffer is
434 // QUEUED, it indicates when the producer has finished filling the
435 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
436 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700437 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700438 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700439
Daniel Lameae59d22012-01-22 15:26:27 -0800440 // Indicates whether this buffer has been seen by a consumer yet
441 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700442
Andy McFadden753e3412013-04-04 17:09:03 -0700443 // Indicates whether this buffer needs to be cleaned up by the
444 // consumer. This is set when a buffer in ACQUIRED state is freed.
445 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700446 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800447 };
448
Andy McFadden753e3412013-04-04 17:09:03 -0700449 // mSlots is the array of buffer slots that must be mirrored on the
450 // producer side. This allows buffer ownership to be transferred between
451 // the producer and consumer without sending a GraphicBuffer over binder.
452 // The entire array is initialized to NULL at construction time, and
453 // buffers are allocated for a slot when requestBuffer is called with
454 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800455 BufferSlot mSlots[NUM_BUFFER_SLOTS];
456
Daniel Lam6b091c52012-01-22 15:26:27 -0800457 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700458 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800459 uint32_t mDefaultWidth;
460
461 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700462 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800463 uint32_t mDefaultHeight;
464
Jamie Gennis72f096f2012-08-27 18:48:37 -0700465 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
466 // acquire at one time. It defaults to 1 and can be changed by the
467 // consumer via the setMaxAcquiredBufferCount method, but this may only be
468 // done when no producer is connected to the BufferQueue.
469 //
470 // This value is used to derive the value returned for the
471 // MIN_UNDEQUEUED_BUFFERS query by the producer.
472 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700473
Jamie Gennis31a353d2012-08-24 17:25:13 -0700474 // mDefaultMaxBufferCount is the default limit on the number of buffers
475 // that will be allocated at one time. This default limit is set by the
476 // consumer. The limit (as opposed to the default limit) may be
477 // overridden by the producer.
478 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700479
Jamie Gennis31a353d2012-08-24 17:25:13 -0700480 // mOverrideMaxBufferCount is the limit on the number of buffers that will
481 // be allocated at one time. This value is set by the image producer by
482 // calling setBufferCount. The default is zero, which means the producer
483 // doesn't care about the number of buffers in the pool. In that case
484 // mDefaultMaxBufferCount is used as the limit.
485 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800486
Daniel Lam6b091c52012-01-22 15:26:27 -0800487 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
488 // allocate new GraphicBuffer objects.
489 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
490
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700491 // mConsumerListener is used to notify the connected consumer of
492 // asynchronous events that it may wish to react to. It is initially set
493 // to NULL and is written by consumerConnect and consumerDisconnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700494 sp<IConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800495
Mathias Agopian595264f2013-07-16 22:56:09 -0700496 // mConsumerControlledByApp whether the connected consumer is controlled by the
497 // application.
498 bool mConsumerControlledByApp;
499
500 // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
Mathias Agopianad678e12013-07-23 17:28:53 -0700501 // this flag is set during connect() when both consumer and producer are controlled
Mathias Agopian595264f2013-07-16 22:56:09 -0700502 // by the application.
503 bool mDequeueBufferCannotBlock;
504
Mathias Agopianad678e12013-07-23 17:28:53 -0700505 // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent
506 // dequeueBuffer() from ever blocking.
507 bool mUseAsyncBuffer;
508
Andy McFadden753e3412013-04-04 17:09:03 -0700509 // mConnectedApi indicates the producer API that is currently connected
510 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
511 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800512 int mConnectedApi;
513
514 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
515 mutable Condition mDequeueCondition;
516
517 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700518 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800519 Fifo mQueue;
520
521 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700522 // consume image buffers pushed to it using the IGraphicBufferProducer
523 // interface. It is initialized to false, and set to true in the
524 // consumerDisconnect method. A BufferQueue that has been abandoned will
525 // return the NO_INIT error from all IGraphicBufferProducer methods
526 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800527 bool mAbandoned;
528
Andy McFadden753e3412013-04-04 17:09:03 -0700529 // mConsumerName is a string used to identify the BufferQueue in log
530 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800531 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800532
533 // mMutex is the mutex used to prevent concurrent access to the member
534 // variables of BufferQueue objects. It must be locked whenever the
535 // member variables are accessed.
536 mutable Mutex mMutex;
537
Andy McFadden753e3412013-04-04 17:09:03 -0700538 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700539 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800540 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800541
Andy McFadden753e3412013-04-04 17:09:03 -0700542 // mBufferHasBeenQueued is true once a buffer has been queued. It is
543 // reset when something causes all buffers to be freed (e.g. changing the
544 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800545 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800546
547 // mDefaultBufferFormat can be set so it will override
548 // the buffer format when it isn't specified in dequeueBuffer
549 uint32_t mDefaultBufferFormat;
550
551 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
552 uint32_t mConsumerUsageBits;
553
554 // mTransformHint is used to optimize for screen rotations
555 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800556};
557
558// ----------------------------------------------------------------------------
559}; // namespace android
560
561#endif // ANDROID_GUI_BUFFERQUEUE_H