blob: 15dc64524d696152a18717d78492b012f43ad80e [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:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080044 // BufferQueue will keep track of at most this value of buffers.
45 // Attempts at runtime to increase the number of buffers past this will fail.
Daniel Lam6b091c52012-01-22 15:26:27 -080046 enum { NUM_BUFFER_SLOTS = 32 };
Igor Murashkin7d2d1602013-11-12 18:02:20 -080047 // Used as a placeholder slot# when the value isn't pointing to an existing buffer.
48 enum { INVALID_BUFFER_SLOT = IGraphicBufferConsumer::BufferItem::INVALID_BUFFER_SLOT };
49 // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code!
50 enum {
51 NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE,
52 PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER,
53 };
Daniel Lam6b091c52012-01-22 15:26:27 -080054
Jamie Gennisc68f2ec2012-08-30 18:36:22 -070055 // When in async mode we reserve two slots in order to guarantee that the
56 // producer and consumer can run asynchronously.
57 enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
58
Mathias Agopiana4e19522013-07-31 20:09:53 -070059 // for backward source compatibility
60 typedef ::android::ConsumerListener ConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -080061
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070062 // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
63 // reference to the actual consumer object. It forwards all calls to that
64 // consumer object so long as it exists.
65 //
66 // This class exists to avoid having a circular reference between the
67 // BufferQueue object and the consumer object. The reason this can't be a weak
68 // reference in the BufferQueue class is because we're planning to expose the
69 // consumer side of a BufferQueue as a binder interface, which doesn't support
70 // weak references.
Mathias Agopiana4e19522013-07-31 20:09:53 -070071 class ProxyConsumerListener : public BnConsumerListener {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070072 public:
Mathias Agopiana4e19522013-07-31 20:09:53 -070073 ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070074 virtual ~ProxyConsumerListener();
75 virtual void onFrameAvailable();
76 virtual void onBuffersReleased();
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070077 private:
Mathias Agopiana4e19522013-07-31 20:09:53 -070078 // mConsumerListener is a weak reference to the IConsumerListener. This is
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070079 // the raison d'etre of ProxyConsumerListener.
Mathias Agopiana4e19522013-07-31 20:09:53 -070080 wp<ConsumerListener> mConsumerListener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070081 };
82
Jamie Gennis72f096f2012-08-27 18:48:37 -070083 // BufferQueue manages a pool of gralloc memory slots to be used by
Mathias Agopian595264f2013-07-16 22:56:09 -070084 // producers and consumers. allocator is used to allocate all the
85 // needed gralloc buffers.
86 BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL);
Daniel Lam6b091c52012-01-22 15:26:27 -080087 virtual ~BufferQueue();
88
Mathias Agopiana4e19522013-07-31 20:09:53 -070089 /*
Mathias Agopian365857d2013-09-11 19:35:45 -070090 * IBinder::DeathRecipient interface
91 */
92
93 virtual void binderDied(const wp<IBinder>& who);
94
95 /*
Mathias Agopiana4e19522013-07-31 20:09:53 -070096 * IGraphicBufferProducer interface
97 */
98
Andy McFadden753e3412013-04-04 17:09:03 -070099 // Query native window attributes. The "what" values are enumerated in
100 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -0800101 virtual int query(int what, int* value);
102
Andy McFadden753e3412013-04-04 17:09:03 -0700103 // setBufferCount updates the number of available buffer slots. If this
104 // method succeeds, buffer slots will be both unallocated and owned by
105 // the BufferQueue object (i.e. they are not owned by the producer or
106 // consumer).
107 //
108 // This will fail if the producer has dequeued any buffers, or if
109 // bufferCount is invalid. bufferCount must generally be a value
Igor Murashkin7ea777f2013-11-18 16:58:36 -0800110 // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS
Andy McFadden753e3412013-04-04 17:09:03 -0700111 // (inclusive). It may also be set to zero (the default) to indicate
112 // that the producer does not wish to set a value. The minimum value
113 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
114 // ...).
115 //
116 // This may only be called by the producer. The consumer will be told
117 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800118 virtual status_t setBufferCount(int bufferCount);
119
Andy McFadden753e3412013-04-04 17:09:03 -0700120 // requestBuffer returns the GraphicBuffer for slot N.
121 //
122 // In normal operation, this is called the first time slot N is returned
123 // by dequeueBuffer. It must be called again if dequeueBuffer returns
124 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800125 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
126
Andy McFadden753e3412013-04-04 17:09:03 -0700127 // dequeueBuffer gets the next buffer slot index for the producer to use.
128 // If a buffer slot is available then that slot index is written to the
129 // location pointed to by the buf argument and a status of OK is returned.
130 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800131 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700132 //
133 // The fence parameter will be updated to hold the fence associated with
134 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700135 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
136 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700137 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800138 // The width and height parameters must be no greater than the minimum of
139 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
140 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700141 // updateTexImage() is called. If width and height are both zero, the
142 // default values specified by setDefaultBufferSize() are used instead.
143 //
144 // The pixel formats are enumerated in graphics.h, e.g.
145 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
146 // will be used.
147 //
148 // The usage argument specifies gralloc buffer usage flags. The values
149 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
150 // will be merged with the usage flags specified by setConsumerUsageBits.
151 //
152 // The return value may be a negative error value or a non-negative
153 // collection of flags. If the flags are set, the return values are
154 // valid, but additional actions must be performed.
155 //
156 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
157 // producer must discard cached GraphicBuffer references for the slot
158 // returned in buf.
159 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
160 // must discard cached GraphicBuffer references for all slots.
161 //
162 // In both cases, the producer will need to call requestBuffer to get a
163 // GraphicBuffer handle for the returned slot.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700164 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700165 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800166
Andy McFadden753e3412013-04-04 17:09:03 -0700167 // queueBuffer returns a filled buffer to the BufferQueue.
168 //
169 // Additional data is provided in the QueueBufferInput struct. Notably,
170 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800171 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700172 // (zero point, etc) are producer-specific and should be documented by the
173 // producer.
174 //
175 // The caller may provide a fence that signals when all rendering
176 // operations have completed. Alternatively, NO_FENCE may be used,
177 // indicating that the buffer is ready immediately.
178 //
179 // Some values are returned in the output struct: the current settings
180 // for default width and height, the current transform hint, and the
181 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700182 virtual status_t queueBuffer(int buf,
183 const QueueBufferInput& input, QueueBufferOutput* output);
184
Andy McFadden753e3412013-04-04 17:09:03 -0700185 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
186 // queue it for use by the consumer.
187 //
188 // The buffer will not be overwritten until the fence signals. The fence
189 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700190 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800191
Andy McFadden753e3412013-04-04 17:09:03 -0700192 // connect attempts to connect a producer API to the BufferQueue. This
193 // must be called before any other IGraphicBufferProducer methods are
194 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800195 //
Andy McFadden753e3412013-04-04 17:09:03 -0700196 // This method will fail if connect was previously called on the
197 // BufferQueue and no corresponding disconnect call was made (i.e. if
198 // it's still connected to a producer).
199 //
200 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian365857d2013-09-11 19:35:45 -0700201 virtual status_t connect(const sp<IBinder>& token,
202 int api, bool producerControlledByApp, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800203
Andy McFadden753e3412013-04-04 17:09:03 -0700204 // disconnect attempts to disconnect a producer API from the BufferQueue.
205 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800206 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800207 // Successfully calling connect after this will allow the other methods to
208 // succeed again.
209 //
210 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700211 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800212 virtual status_t disconnect(int api);
213
Mathias Agopiana4e19522013-07-31 20:09:53 -0700214 /*
215 * IGraphicBufferConsumer interface
216 */
Daniel Lameae59d22012-01-22 15:26:27 -0800217
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700218 // acquireBuffer attempts to acquire ownership of the next pending buffer in
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800219 // the BufferQueue. If no buffer is pending then it returns NO_BUFFER_AVAILABLE. If a
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700220 // buffer is successfully acquired, the information about the buffer is
221 // returned in BufferItem. If the buffer returned had previously been
222 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
223 // NULL and it is assumed that the consumer still holds a reference to the
224 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700225 //
226 // If presentWhen is nonzero, it indicates the time when the buffer will
227 // be displayed on screen. If the buffer's timestamp is farther in the
228 // future, the buffer won't be acquired, and PRESENT_LATER will be
229 // returned. The presentation time is in nanoseconds, and the time base
230 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800231 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800232
233 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700234 // BufferQueue. This may be done while the buffer's contents are still
235 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700236 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700237 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700238 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
239 // any references to the just-released buffer that it might have, as if it
240 // had received a onBuffersReleased() call with a mask set for the released
241 // buffer.
242 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700243 // Note that the dependencies on EGL will be removed once we switch to using
244 // the Android HW Sync HAL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700245 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700246 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700247 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800248
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700249 // consumerConnect connects a consumer to the BufferQueue. Only one
250 // consumer may be connected, and when that consumer disconnects the
251 // BufferQueue is placed into the "abandoned" state, causing most
252 // interactions with the BufferQueue by the producer to fail.
Mathias Agopian595264f2013-07-16 22:56:09 -0700253 // controlledByApp indicates whether the consumer is controlled by
254 // the application.
Andy McFadden753e3412013-04-04 17:09:03 -0700255 //
256 // consumer may not be NULL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700257 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700258
Daniel Lameae59d22012-01-22 15:26:27 -0800259 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700260 // buffers will be freed and the BufferQueue is placed in the "abandoned"
261 // state, causing most interactions with the BufferQueue by the producer to
262 // fail.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700263 virtual status_t consumerDisconnect();
Daniel Lameae59d22012-01-22 15:26:27 -0800264
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700265 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700266 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700267 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700268 //
269 // This should be called from the onBuffersReleased() callback.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700270 virtual status_t getReleasedBuffers(uint32_t* slotMask);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700271
Daniel Lameae59d22012-01-22 15:26:27 -0800272 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700273 // dequeueBuffer when a width and height of zero is requested. Default
274 // is 1x1.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700275 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h);
Daniel Lameae59d22012-01-22 15:26:27 -0800276
Andy McFadden753e3412013-04-04 17:09:03 -0700277 // setDefaultMaxBufferCount sets the default value for the maximum buffer
278 // count (the initial default is 2). If the producer has requested a
279 // buffer count using setBufferCount, the default buffer count will only
280 // take effect if the producer sets the count back to zero.
281 //
282 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700283 virtual status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800284
Mathias Agopianad678e12013-07-23 17:28:53 -0700285 // disableAsyncBuffer disables the extra buffer used in async mode
286 // (when both producer and consumer have set their "isControlledByApp"
287 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
288 //
289 // This can only be called before consumerConnect().
Mathias Agopiana4e19522013-07-31 20:09:53 -0700290 virtual status_t disableAsyncBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700291
Jamie Gennis72f096f2012-08-27 18:48:37 -0700292 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700293 // be acquired by the consumer at one time (default 1). This call will
294 // fail if a producer is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700295 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700296
Daniel Lameae59d22012-01-22 15:26:27 -0800297 // setConsumerName sets the name used in logging
Mathias Agopiana4e19522013-07-31 20:09:53 -0700298 virtual void setConsumerName(const String8& name);
Daniel Lameae59d22012-01-22 15:26:27 -0800299
Daniel Lamb2675792012-02-23 14:35:13 -0800300 // setDefaultBufferFormat allows the BufferQueue to create
301 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700302 // in dequeueBuffer. Formats are enumerated in graphics.h; the
303 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700304 virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800305
Andy McFadden753e3412013-04-04 17:09:03 -0700306 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
307 // These are merged with the bits passed to dequeueBuffer. The values are
308 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700309 virtual status_t setConsumerUsageBits(uint32_t usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800310
Andy McFadden753e3412013-04-04 17:09:03 -0700311 // setTransformHint bakes in rotation to buffers so overlays can be used.
312 // The values are enumerated in window.h, e.g.
313 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700314 virtual status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800315
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700316 // dump our state in a String
317 virtual void dump(String8& result, const char* prefix) const;
318
Daniel Lameae59d22012-01-22 15:26:27 -0800319private:
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800320 // The default API number used to indicate no producer client is connected.
321 enum { NO_CONNECTED_API = 0 };
322
323 // Aliases for using enums from <IGraphicBufferConsumer.h>
324 enum { STALE_BUFFER_SLOT = IGraphicBufferConsumer::STALE_BUFFER_SLOT };
325
Andy McFadden753e3412013-04-04 17:09:03 -0700326 // freeBufferLocked frees the GraphicBuffer and sync resources for the
327 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800328 void freeBufferLocked(int index);
329
Andy McFadden753e3412013-04-04 17:09:03 -0700330 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
331 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800332 void freeAllBuffersLocked();
333
Jamie Gennis31a353d2012-08-24 17:25:13 -0700334 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
335 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700336 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
337 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700338 status_t setDefaultMaxBufferCountLocked(int count);
339
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700340 // getMinUndequeuedBufferCount returns the minimum number of buffers
341 // that must remain in a state other than DEQUEUED.
342 // The async parameter tells whether we're in asynchronous mode.
343 int getMinUndequeuedBufferCount(bool async) const;
344
Jamie Gennis31a353d2012-08-24 17:25:13 -0700345 // getMinBufferCountLocked returns the minimum number of buffers allowed
346 // given the current BufferQueue state.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700347 // The async parameter tells whether we're in asynchronous mode.
348 int getMinMaxBufferCountLocked(bool async) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800349
Jamie Gennise191e6c2012-08-24 20:26:34 -0700350 // getMaxBufferCountLocked returns the maximum number of buffers that can
351 // be allocated at once. This value depends upon the following member
352 // variables:
353 //
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700354 // mDequeueBufferCannotBlock
Jamie Gennis72f096f2012-08-27 18:48:37 -0700355 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700356 // mDefaultMaxBufferCount
357 // mOverrideMaxBufferCount
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700358 // async parameter
Jamie Gennise191e6c2012-08-24 20:26:34 -0700359 //
360 // Any time one of these member variables is changed while a producer is
361 // connected, mDequeueCondition must be broadcast.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700362 int getMaxBufferCountLocked(bool async) const;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700363
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700364 // stillTracking returns true iff the buffer item is still being tracked
365 // in one of the slots.
366 bool stillTracking(const BufferItem *item) const;
367
Daniel Lam6b091c52012-01-22 15:26:27 -0800368 struct BufferSlot {
369
370 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800371 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800372 mBufferState(BufferSlot::FREE),
373 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800374 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700375 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700376 mAcquireCalled(false),
377 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800378 }
379
380 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
381 // if no buffer has been allocated.
382 sp<GraphicBuffer> mGraphicBuffer;
383
Andy McFadden753e3412013-04-04 17:09:03 -0700384 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800385 EGLDisplay mEglDisplay;
386
387 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700388 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800389 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700390 // FREE indicates that the buffer is available to be dequeued
391 // by the producer. The buffer may be in use by the consumer for
392 // a finite time, so the buffer must not be modified until the
393 // associated fence is signaled.
394 //
395 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
396 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800397 FREE = 0,
398
399 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700400 // producer, but has not yet been queued or canceled. The
401 // producer may modify the buffer's contents as soon as the
402 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800403 //
Andy McFadden753e3412013-04-04 17:09:03 -0700404 // The slot is "owned" by the producer. It can transition to
405 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800406 DEQUEUED = 1,
407
Andy McFadden753e3412013-04-04 17:09:03 -0700408 // QUEUED indicates that the buffer has been filled by the
409 // producer and queued for use by the consumer. The buffer
410 // contents may continue to be modified for a finite time, so
411 // the contents must not be accessed until the associated fence
412 // is signaled.
413 //
414 // The slot is "owned" by BufferQueue. It can transition to
415 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
416 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800417 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800418
Andy McFadden753e3412013-04-04 17:09:03 -0700419 // ACQUIRED indicates that the buffer has been acquired by the
420 // consumer. As with QUEUED, the contents must not be accessed
421 // by the consumer until the fence is signaled.
422 //
423 // The slot is "owned" by the consumer. It transitions to FREE
424 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800425 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800426 };
427
428 // mBufferState is the current state of this buffer slot.
429 BufferState mBufferState;
430
Andy McFadden753e3412013-04-04 17:09:03 -0700431 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800432 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700433 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800434 bool mRequestBufferCalled;
435
Andy McFadden753e3412013-04-04 17:09:03 -0700436 // mFrameNumber is the number of the queued frame for this slot. This
437 // is used to dequeue buffers in LRU order (useful because buffers
438 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800439 uint64_t mFrameNumber;
440
Jesse Hallc777b0b2012-06-28 12:52:05 -0700441 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800442 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700443 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
444 // new sync object in releaseBuffer. (This is deprecated in favor of
445 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700446 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800447
Jesse Hallc777b0b2012-06-28 12:52:05 -0700448 // mFence is a fence which will signal when work initiated by the
449 // previous owner of the buffer is finished. When the buffer is FREE,
450 // the fence indicates when the consumer has finished reading
451 // from the buffer, or when the producer has finished writing if it
452 // called cancelBuffer after queueing some writes. When the buffer is
453 // QUEUED, it indicates when the producer has finished filling the
454 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
455 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700456 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700457 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700458
Daniel Lameae59d22012-01-22 15:26:27 -0800459 // Indicates whether this buffer has been seen by a consumer yet
460 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700461
Andy McFadden753e3412013-04-04 17:09:03 -0700462 // Indicates whether this buffer needs to be cleaned up by the
463 // consumer. This is set when a buffer in ACQUIRED state is freed.
464 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700465 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800466 };
467
Andy McFadden753e3412013-04-04 17:09:03 -0700468 // mSlots is the array of buffer slots that must be mirrored on the
469 // producer side. This allows buffer ownership to be transferred between
470 // the producer and consumer without sending a GraphicBuffer over binder.
471 // The entire array is initialized to NULL at construction time, and
472 // buffers are allocated for a slot when requestBuffer is called with
473 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800474 BufferSlot mSlots[NUM_BUFFER_SLOTS];
475
Daniel Lam6b091c52012-01-22 15:26:27 -0800476 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700477 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800478 uint32_t mDefaultWidth;
479
480 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700481 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800482 uint32_t mDefaultHeight;
483
Jamie Gennis72f096f2012-08-27 18:48:37 -0700484 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
485 // acquire at one time. It defaults to 1 and can be changed by the
486 // consumer via the setMaxAcquiredBufferCount method, but this may only be
487 // done when no producer is connected to the BufferQueue.
488 //
489 // This value is used to derive the value returned for the
490 // MIN_UNDEQUEUED_BUFFERS query by the producer.
491 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700492
Jamie Gennis31a353d2012-08-24 17:25:13 -0700493 // mDefaultMaxBufferCount is the default limit on the number of buffers
494 // that will be allocated at one time. This default limit is set by the
495 // consumer. The limit (as opposed to the default limit) may be
496 // overridden by the producer.
497 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700498
Jamie Gennis31a353d2012-08-24 17:25:13 -0700499 // mOverrideMaxBufferCount is the limit on the number of buffers that will
500 // be allocated at one time. This value is set by the image producer by
501 // calling setBufferCount. The default is zero, which means the producer
502 // doesn't care about the number of buffers in the pool. In that case
503 // mDefaultMaxBufferCount is used as the limit.
504 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800505
Daniel Lam6b091c52012-01-22 15:26:27 -0800506 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
507 // allocate new GraphicBuffer objects.
508 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
509
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700510 // mConsumerListener is used to notify the connected consumer of
511 // asynchronous events that it may wish to react to. It is initially set
512 // to NULL and is written by consumerConnect and consumerDisconnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700513 sp<IConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800514
Mathias Agopian595264f2013-07-16 22:56:09 -0700515 // mConsumerControlledByApp whether the connected consumer is controlled by the
516 // application.
517 bool mConsumerControlledByApp;
518
519 // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
Mathias Agopianad678e12013-07-23 17:28:53 -0700520 // this flag is set during connect() when both consumer and producer are controlled
Mathias Agopian595264f2013-07-16 22:56:09 -0700521 // by the application.
522 bool mDequeueBufferCannotBlock;
523
Mathias Agopianad678e12013-07-23 17:28:53 -0700524 // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent
525 // dequeueBuffer() from ever blocking.
526 bool mUseAsyncBuffer;
527
Andy McFadden753e3412013-04-04 17:09:03 -0700528 // mConnectedApi indicates the producer API that is currently connected
529 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
530 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800531 int mConnectedApi;
532
533 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
534 mutable Condition mDequeueCondition;
535
536 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700537 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800538 Fifo mQueue;
539
540 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700541 // consume image buffers pushed to it using the IGraphicBufferProducer
542 // interface. It is initialized to false, and set to true in the
543 // consumerDisconnect method. A BufferQueue that has been abandoned will
544 // return the NO_INIT error from all IGraphicBufferProducer methods
545 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800546 bool mAbandoned;
547
Andy McFadden753e3412013-04-04 17:09:03 -0700548 // mConsumerName is a string used to identify the BufferQueue in log
549 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800550 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800551
552 // mMutex is the mutex used to prevent concurrent access to the member
553 // variables of BufferQueue objects. It must be locked whenever the
554 // member variables are accessed.
555 mutable Mutex mMutex;
556
Andy McFadden753e3412013-04-04 17:09:03 -0700557 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700558 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800559 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800560
Andy McFadden753e3412013-04-04 17:09:03 -0700561 // mBufferHasBeenQueued is true once a buffer has been queued. It is
562 // reset when something causes all buffers to be freed (e.g. changing the
563 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800564 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800565
566 // mDefaultBufferFormat can be set so it will override
567 // the buffer format when it isn't specified in dequeueBuffer
568 uint32_t mDefaultBufferFormat;
569
570 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
571 uint32_t mConsumerUsageBits;
572
573 // mTransformHint is used to optimize for screen rotations
574 uint32_t mTransformHint;
Mathias Agopian365857d2013-09-11 19:35:45 -0700575
576 // mConnectedProducerToken is used to set a binder death notification on the producer
577 sp<IBinder> mConnectedProducerToken;
Daniel Lam6b091c52012-01-22 15:26:27 -0800578};
579
580// ----------------------------------------------------------------------------
581}; // namespace android
582
583#endif // ANDROID_GUI_BUFFERQUEUE_H