blob: 2457eba090e7f8345c27e9eed5d56e45bcf09c90 [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 };
Igor Murashkin7d2d1602013-11-12 18:02:20 -080045 // BufferQueue will keep track of at most this value of buffers.
46 // Attempts at runtime to increase the number of buffers past this will fail.
Daniel Lam6b091c52012-01-22 15:26:27 -080047 enum { NUM_BUFFER_SLOTS = 32 };
Igor Murashkin7d2d1602013-11-12 18:02:20 -080048 // Used as a placeholder slot# when the value isn't pointing to an existing buffer.
49 enum { INVALID_BUFFER_SLOT = IGraphicBufferConsumer::BufferItem::INVALID_BUFFER_SLOT };
50 // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code!
51 enum {
52 NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE,
53 PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER,
54 };
Daniel Lam6b091c52012-01-22 15:26:27 -080055
Jamie Gennisc68f2ec2012-08-30 18:36:22 -070056 // When in async mode we reserve two slots in order to guarantee that the
57 // producer and consumer can run asynchronously.
58 enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
59
Mathias Agopiana4e19522013-07-31 20:09:53 -070060 // for backward source compatibility
61 typedef ::android::ConsumerListener ConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -080062
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070063 // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
64 // reference to the actual consumer object. It forwards all calls to that
65 // consumer object so long as it exists.
66 //
67 // This class exists to avoid having a circular reference between the
68 // BufferQueue object and the consumer object. The reason this can't be a weak
69 // reference in the BufferQueue class is because we're planning to expose the
70 // consumer side of a BufferQueue as a binder interface, which doesn't support
71 // weak references.
Mathias Agopiana4e19522013-07-31 20:09:53 -070072 class ProxyConsumerListener : public BnConsumerListener {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070073 public:
Mathias Agopiana4e19522013-07-31 20:09:53 -070074 ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070075 virtual ~ProxyConsumerListener();
76 virtual void onFrameAvailable();
77 virtual void onBuffersReleased();
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070078 private:
Mathias Agopiana4e19522013-07-31 20:09:53 -070079 // mConsumerListener is a weak reference to the IConsumerListener. This is
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070080 // the raison d'etre of ProxyConsumerListener.
Mathias Agopiana4e19522013-07-31 20:09:53 -070081 wp<ConsumerListener> mConsumerListener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070082 };
83
Jamie Gennis72f096f2012-08-27 18:48:37 -070084 // BufferQueue manages a pool of gralloc memory slots to be used by
Mathias Agopian595264f2013-07-16 22:56:09 -070085 // producers and consumers. allocator is used to allocate all the
86 // needed gralloc buffers.
87 BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL);
Daniel Lam6b091c52012-01-22 15:26:27 -080088 virtual ~BufferQueue();
89
Mathias Agopiana4e19522013-07-31 20:09:53 -070090 /*
Mathias Agopian365857d2013-09-11 19:35:45 -070091 * IBinder::DeathRecipient interface
92 */
93
94 virtual void binderDied(const wp<IBinder>& who);
95
96 /*
Mathias Agopiana4e19522013-07-31 20:09:53 -070097 * IGraphicBufferProducer interface
98 */
99
Andy McFadden753e3412013-04-04 17:09:03 -0700100 // Query native window attributes. The "what" values are enumerated in
101 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -0800102 virtual int query(int what, int* value);
103
Andy McFadden753e3412013-04-04 17:09:03 -0700104 // setBufferCount updates the number of available buffer slots. If this
105 // method succeeds, buffer slots will be both unallocated and owned by
106 // the BufferQueue object (i.e. they are not owned by the producer or
107 // consumer).
108 //
109 // This will fail if the producer has dequeued any buffers, or if
110 // bufferCount is invalid. bufferCount must generally be a value
111 // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
112 // (inclusive). It may also be set to zero (the default) to indicate
113 // that the producer does not wish to set a value. The minimum value
114 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
115 // ...).
116 //
117 // This may only be called by the producer. The consumer will be told
118 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800119 virtual status_t setBufferCount(int bufferCount);
120
Andy McFadden753e3412013-04-04 17:09:03 -0700121 // requestBuffer returns the GraphicBuffer for slot N.
122 //
123 // In normal operation, this is called the first time slot N is returned
124 // by dequeueBuffer. It must be called again if dequeueBuffer returns
125 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800126 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
127
Andy McFadden753e3412013-04-04 17:09:03 -0700128 // dequeueBuffer gets the next buffer slot index for the producer to use.
129 // If a buffer slot is available then that slot index is written to the
130 // location pointed to by the buf argument and a status of OK is returned.
131 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800132 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700133 //
134 // The fence parameter will be updated to hold the fence associated with
135 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700136 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
137 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700138 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800139 // The width and height parameters must be no greater than the minimum of
140 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
141 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700142 // updateTexImage() is called. If width and height are both zero, the
143 // default values specified by setDefaultBufferSize() are used instead.
144 //
145 // The pixel formats are enumerated in graphics.h, e.g.
146 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
147 // will be used.
148 //
149 // The usage argument specifies gralloc buffer usage flags. The values
150 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
151 // will be merged with the usage flags specified by setConsumerUsageBits.
152 //
153 // The return value may be a negative error value or a non-negative
154 // collection of flags. If the flags are set, the return values are
155 // valid, but additional actions must be performed.
156 //
157 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
158 // producer must discard cached GraphicBuffer references for the slot
159 // returned in buf.
160 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
161 // must discard cached GraphicBuffer references for all slots.
162 //
163 // In both cases, the producer will need to call requestBuffer to get a
164 // GraphicBuffer handle for the returned slot.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700165 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700166 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800167
Andy McFadden753e3412013-04-04 17:09:03 -0700168 // queueBuffer returns a filled buffer to the BufferQueue.
169 //
170 // Additional data is provided in the QueueBufferInput struct. Notably,
171 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800172 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700173 // (zero point, etc) are producer-specific and should be documented by the
174 // producer.
175 //
176 // The caller may provide a fence that signals when all rendering
177 // operations have completed. Alternatively, NO_FENCE may be used,
178 // indicating that the buffer is ready immediately.
179 //
180 // Some values are returned in the output struct: the current settings
181 // for default width and height, the current transform hint, and the
182 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700183 virtual status_t queueBuffer(int buf,
184 const QueueBufferInput& input, QueueBufferOutput* output);
185
Andy McFadden753e3412013-04-04 17:09:03 -0700186 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
187 // queue it for use by the consumer.
188 //
189 // The buffer will not be overwritten until the fence signals. The fence
190 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700191 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800192
Andy McFadden753e3412013-04-04 17:09:03 -0700193 // connect attempts to connect a producer API to the BufferQueue. This
194 // must be called before any other IGraphicBufferProducer methods are
195 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800196 //
Andy McFadden753e3412013-04-04 17:09:03 -0700197 // This method will fail if connect was previously called on the
198 // BufferQueue and no corresponding disconnect call was made (i.e. if
199 // it's still connected to a producer).
200 //
201 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian365857d2013-09-11 19:35:45 -0700202 virtual status_t connect(const sp<IBinder>& token,
203 int api, bool producerControlledByApp, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800204
Andy McFadden753e3412013-04-04 17:09:03 -0700205 // disconnect attempts to disconnect a producer API from the BufferQueue.
206 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800207 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800208 // Successfully calling connect after this will allow the other methods to
209 // succeed again.
210 //
211 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700212 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800213 virtual status_t disconnect(int api);
214
Mathias Agopiana4e19522013-07-31 20:09:53 -0700215 /*
216 * IGraphicBufferConsumer interface
217 */
Daniel Lameae59d22012-01-22 15:26:27 -0800218
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700219 // acquireBuffer attempts to acquire ownership of the next pending buffer in
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800220 // the BufferQueue. If no buffer is pending then it returns NO_BUFFER_AVAILABLE. If a
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700221 // buffer is successfully acquired, the information about the buffer is
222 // returned in BufferItem. If the buffer returned had previously been
223 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
224 // NULL and it is assumed that the consumer still holds a reference to the
225 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700226 //
227 // If presentWhen is nonzero, it indicates the time when the buffer will
228 // be displayed on screen. If the buffer's timestamp is farther in the
229 // future, the buffer won't be acquired, and PRESENT_LATER will be
230 // returned. The presentation time is in nanoseconds, and the time base
231 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800232 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800233
234 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700235 // BufferQueue. This may be done while the buffer's contents are still
236 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700237 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700238 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700239 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
240 // any references to the just-released buffer that it might have, as if it
241 // had received a onBuffersReleased() call with a mask set for the released
242 // buffer.
243 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700244 // Note that the dependencies on EGL will be removed once we switch to using
245 // the Android HW Sync HAL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700246 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700247 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700248 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800249
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700250 // consumerConnect connects a consumer to the BufferQueue. Only one
251 // consumer may be connected, and when that consumer disconnects the
252 // BufferQueue is placed into the "abandoned" state, causing most
253 // interactions with the BufferQueue by the producer to fail.
Mathias Agopian595264f2013-07-16 22:56:09 -0700254 // controlledByApp indicates whether the consumer is controlled by
255 // the application.
Andy McFadden753e3412013-04-04 17:09:03 -0700256 //
257 // consumer may not be NULL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700258 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700259
Daniel Lameae59d22012-01-22 15:26:27 -0800260 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700261 // buffers will be freed and the BufferQueue is placed in the "abandoned"
262 // state, causing most interactions with the BufferQueue by the producer to
263 // fail.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700264 virtual status_t consumerDisconnect();
Daniel Lameae59d22012-01-22 15:26:27 -0800265
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700266 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700267 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700268 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700269 //
270 // This should be called from the onBuffersReleased() callback.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700271 virtual status_t getReleasedBuffers(uint32_t* slotMask);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700272
Daniel Lameae59d22012-01-22 15:26:27 -0800273 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700274 // dequeueBuffer when a width and height of zero is requested. Default
275 // is 1x1.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700276 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h);
Daniel Lameae59d22012-01-22 15:26:27 -0800277
Andy McFadden753e3412013-04-04 17:09:03 -0700278 // setDefaultMaxBufferCount sets the default value for the maximum buffer
279 // count (the initial default is 2). If the producer has requested a
280 // buffer count using setBufferCount, the default buffer count will only
281 // take effect if the producer sets the count back to zero.
282 //
283 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700284 virtual status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800285
Mathias Agopianad678e12013-07-23 17:28:53 -0700286 // disableAsyncBuffer disables the extra buffer used in async mode
287 // (when both producer and consumer have set their "isControlledByApp"
288 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
289 //
290 // This can only be called before consumerConnect().
Mathias Agopiana4e19522013-07-31 20:09:53 -0700291 virtual status_t disableAsyncBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700292
Jamie Gennis72f096f2012-08-27 18:48:37 -0700293 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700294 // be acquired by the consumer at one time (default 1). This call will
295 // fail if a producer is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700296 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700297
Daniel Lameae59d22012-01-22 15:26:27 -0800298 // setConsumerName sets the name used in logging
Mathias Agopiana4e19522013-07-31 20:09:53 -0700299 virtual void setConsumerName(const String8& name);
Daniel Lameae59d22012-01-22 15:26:27 -0800300
Daniel Lamb2675792012-02-23 14:35:13 -0800301 // setDefaultBufferFormat allows the BufferQueue to create
302 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700303 // in dequeueBuffer. Formats are enumerated in graphics.h; the
304 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700305 virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800306
Andy McFadden753e3412013-04-04 17:09:03 -0700307 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
308 // These are merged with the bits passed to dequeueBuffer. The values are
309 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700310 virtual status_t setConsumerUsageBits(uint32_t usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800311
Andy McFadden753e3412013-04-04 17:09:03 -0700312 // setTransformHint bakes in rotation to buffers so overlays can be used.
313 // The values are enumerated in window.h, e.g.
314 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700315 virtual status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800316
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700317 // dump our state in a String
318 virtual void dump(String8& result, const char* prefix) const;
319
Daniel Lameae59d22012-01-22 15:26:27 -0800320private:
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800321 // The default API number used to indicate no producer client is connected.
322 enum { NO_CONNECTED_API = 0 };
323
324 // Aliases for using enums from <IGraphicBufferConsumer.h>
325 enum { STALE_BUFFER_SLOT = IGraphicBufferConsumer::STALE_BUFFER_SLOT };
326
Andy McFadden753e3412013-04-04 17:09:03 -0700327 // freeBufferLocked frees the GraphicBuffer and sync resources for the
328 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800329 void freeBufferLocked(int index);
330
Andy McFadden753e3412013-04-04 17:09:03 -0700331 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
332 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800333 void freeAllBuffersLocked();
334
Jamie Gennis31a353d2012-08-24 17:25:13 -0700335 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
336 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700337 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
338 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700339 status_t setDefaultMaxBufferCountLocked(int count);
340
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700341 // getMinUndequeuedBufferCount returns the minimum number of buffers
342 // that must remain in a state other than DEQUEUED.
343 // The async parameter tells whether we're in asynchronous mode.
344 int getMinUndequeuedBufferCount(bool async) const;
345
Jamie Gennis31a353d2012-08-24 17:25:13 -0700346 // getMinBufferCountLocked returns the minimum number of buffers allowed
347 // given the current BufferQueue state.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700348 // The async parameter tells whether we're in asynchronous mode.
349 int getMinMaxBufferCountLocked(bool async) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800350
Jamie Gennise191e6c2012-08-24 20:26:34 -0700351 // getMaxBufferCountLocked returns the maximum number of buffers that can
352 // be allocated at once. This value depends upon the following member
353 // variables:
354 //
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700355 // mDequeueBufferCannotBlock
Jamie Gennis72f096f2012-08-27 18:48:37 -0700356 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700357 // mDefaultMaxBufferCount
358 // mOverrideMaxBufferCount
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700359 // async parameter
Jamie Gennise191e6c2012-08-24 20:26:34 -0700360 //
361 // Any time one of these member variables is changed while a producer is
362 // connected, mDequeueCondition must be broadcast.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700363 int getMaxBufferCountLocked(bool async) const;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700364
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700365 // stillTracking returns true iff the buffer item is still being tracked
366 // in one of the slots.
367 bool stillTracking(const BufferItem *item) const;
368
Daniel Lam6b091c52012-01-22 15:26:27 -0800369 struct BufferSlot {
370
371 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800372 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800373 mBufferState(BufferSlot::FREE),
374 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800375 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700376 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700377 mAcquireCalled(false),
378 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800379 }
380
381 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
382 // if no buffer has been allocated.
383 sp<GraphicBuffer> mGraphicBuffer;
384
Andy McFadden753e3412013-04-04 17:09:03 -0700385 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800386 EGLDisplay mEglDisplay;
387
388 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700389 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800390 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700391 // FREE indicates that the buffer is available to be dequeued
392 // by the producer. The buffer may be in use by the consumer for
393 // a finite time, so the buffer must not be modified until the
394 // associated fence is signaled.
395 //
396 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
397 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800398 FREE = 0,
399
400 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700401 // producer, but has not yet been queued or canceled. The
402 // producer may modify the buffer's contents as soon as the
403 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800404 //
Andy McFadden753e3412013-04-04 17:09:03 -0700405 // The slot is "owned" by the producer. It can transition to
406 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800407 DEQUEUED = 1,
408
Andy McFadden753e3412013-04-04 17:09:03 -0700409 // QUEUED indicates that the buffer has been filled by the
410 // producer and queued for use by the consumer. The buffer
411 // contents may continue to be modified for a finite time, so
412 // the contents must not be accessed until the associated fence
413 // is signaled.
414 //
415 // The slot is "owned" by BufferQueue. It can transition to
416 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
417 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800418 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800419
Andy McFadden753e3412013-04-04 17:09:03 -0700420 // ACQUIRED indicates that the buffer has been acquired by the
421 // consumer. As with QUEUED, the contents must not be accessed
422 // by the consumer until the fence is signaled.
423 //
424 // The slot is "owned" by the consumer. It transitions to FREE
425 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800426 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800427 };
428
429 // mBufferState is the current state of this buffer slot.
430 BufferState mBufferState;
431
Andy McFadden753e3412013-04-04 17:09:03 -0700432 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800433 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700434 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800435 bool mRequestBufferCalled;
436
Andy McFadden753e3412013-04-04 17:09:03 -0700437 // mFrameNumber is the number of the queued frame for this slot. This
438 // is used to dequeue buffers in LRU order (useful because buffers
439 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800440 uint64_t mFrameNumber;
441
Jesse Hallc777b0b2012-06-28 12:52:05 -0700442 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800443 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700444 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
445 // new sync object in releaseBuffer. (This is deprecated in favor of
446 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700447 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800448
Jesse Hallc777b0b2012-06-28 12:52:05 -0700449 // mFence is a fence which will signal when work initiated by the
450 // previous owner of the buffer is finished. When the buffer is FREE,
451 // the fence indicates when the consumer has finished reading
452 // from the buffer, or when the producer has finished writing if it
453 // called cancelBuffer after queueing some writes. When the buffer is
454 // QUEUED, it indicates when the producer has finished filling the
455 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
456 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700457 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700458 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700459
Daniel Lameae59d22012-01-22 15:26:27 -0800460 // Indicates whether this buffer has been seen by a consumer yet
461 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700462
Andy McFadden753e3412013-04-04 17:09:03 -0700463 // Indicates whether this buffer needs to be cleaned up by the
464 // consumer. This is set when a buffer in ACQUIRED state is freed.
465 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700466 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800467 };
468
Andy McFadden753e3412013-04-04 17:09:03 -0700469 // mSlots is the array of buffer slots that must be mirrored on the
470 // producer side. This allows buffer ownership to be transferred between
471 // the producer and consumer without sending a GraphicBuffer over binder.
472 // The entire array is initialized to NULL at construction time, and
473 // buffers are allocated for a slot when requestBuffer is called with
474 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800475 BufferSlot mSlots[NUM_BUFFER_SLOTS];
476
Daniel Lam6b091c52012-01-22 15:26:27 -0800477 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700478 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800479 uint32_t mDefaultWidth;
480
481 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700482 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800483 uint32_t mDefaultHeight;
484
Jamie Gennis72f096f2012-08-27 18:48:37 -0700485 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
486 // acquire at one time. It defaults to 1 and can be changed by the
487 // consumer via the setMaxAcquiredBufferCount method, but this may only be
488 // done when no producer is connected to the BufferQueue.
489 //
490 // This value is used to derive the value returned for the
491 // MIN_UNDEQUEUED_BUFFERS query by the producer.
492 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700493
Jamie Gennis31a353d2012-08-24 17:25:13 -0700494 // mDefaultMaxBufferCount is the default limit on the number of buffers
495 // that will be allocated at one time. This default limit is set by the
496 // consumer. The limit (as opposed to the default limit) may be
497 // overridden by the producer.
498 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700499
Jamie Gennis31a353d2012-08-24 17:25:13 -0700500 // mOverrideMaxBufferCount is the limit on the number of buffers that will
501 // be allocated at one time. This value is set by the image producer by
502 // calling setBufferCount. The default is zero, which means the producer
503 // doesn't care about the number of buffers in the pool. In that case
504 // mDefaultMaxBufferCount is used as the limit.
505 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800506
Daniel Lam6b091c52012-01-22 15:26:27 -0800507 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
508 // allocate new GraphicBuffer objects.
509 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
510
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700511 // mConsumerListener is used to notify the connected consumer of
512 // asynchronous events that it may wish to react to. It is initially set
513 // to NULL and is written by consumerConnect and consumerDisconnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700514 sp<IConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800515
Mathias Agopian595264f2013-07-16 22:56:09 -0700516 // mConsumerControlledByApp whether the connected consumer is controlled by the
517 // application.
518 bool mConsumerControlledByApp;
519
520 // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
Mathias Agopianad678e12013-07-23 17:28:53 -0700521 // this flag is set during connect() when both consumer and producer are controlled
Mathias Agopian595264f2013-07-16 22:56:09 -0700522 // by the application.
523 bool mDequeueBufferCannotBlock;
524
Mathias Agopianad678e12013-07-23 17:28:53 -0700525 // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent
526 // dequeueBuffer() from ever blocking.
527 bool mUseAsyncBuffer;
528
Andy McFadden753e3412013-04-04 17:09:03 -0700529 // mConnectedApi indicates the producer API that is currently connected
530 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
531 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800532 int mConnectedApi;
533
534 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
535 mutable Condition mDequeueCondition;
536
537 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700538 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800539 Fifo mQueue;
540
541 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700542 // consume image buffers pushed to it using the IGraphicBufferProducer
543 // interface. It is initialized to false, and set to true in the
544 // consumerDisconnect method. A BufferQueue that has been abandoned will
545 // return the NO_INIT error from all IGraphicBufferProducer methods
546 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800547 bool mAbandoned;
548
Andy McFadden753e3412013-04-04 17:09:03 -0700549 // mConsumerName is a string used to identify the BufferQueue in log
550 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800551 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800552
553 // mMutex is the mutex used to prevent concurrent access to the member
554 // variables of BufferQueue objects. It must be locked whenever the
555 // member variables are accessed.
556 mutable Mutex mMutex;
557
Andy McFadden753e3412013-04-04 17:09:03 -0700558 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700559 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800560 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800561
Andy McFadden753e3412013-04-04 17:09:03 -0700562 // mBufferHasBeenQueued is true once a buffer has been queued. It is
563 // reset when something causes all buffers to be freed (e.g. changing the
564 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800565 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800566
567 // mDefaultBufferFormat can be set so it will override
568 // the buffer format when it isn't specified in dequeueBuffer
569 uint32_t mDefaultBufferFormat;
570
571 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
572 uint32_t mConsumerUsageBits;
573
574 // mTransformHint is used to optimize for screen rotations
575 uint32_t mTransformHint;
Mathias Agopian365857d2013-09-11 19:35:45 -0700576
577 // mConnectedProducerToken is used to set a binder death notification on the producer
578 sp<IBinder> mConnectedProducerToken;
Daniel Lam6b091c52012-01-22 15:26:27 -0800579};
580
581// ----------------------------------------------------------------------------
582}; // namespace android
583
584#endif // ANDROID_GUI_BUFFERQUEUE_H