blob: 7e404fe84287236948f25dfc0186f6bb5aec6231 [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 /*
82 * IGraphicBufferProducer interface
83 */
84
Andy McFadden753e3412013-04-04 17:09:03 -070085 // Query native window attributes. The "what" values are enumerated in
86 // window.h (e.g. NATIVE_WINDOW_FORMAT).
Daniel Lamb8560522012-01-30 15:51:27 -080087 virtual int query(int what, int* value);
88
Andy McFadden753e3412013-04-04 17:09:03 -070089 // setBufferCount updates the number of available buffer slots. If this
90 // method succeeds, buffer slots will be both unallocated and owned by
91 // the BufferQueue object (i.e. they are not owned by the producer or
92 // consumer).
93 //
94 // This will fail if the producer has dequeued any buffers, or if
95 // bufferCount is invalid. bufferCount must generally be a value
96 // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
97 // (inclusive). It may also be set to zero (the default) to indicate
98 // that the producer does not wish to set a value. The minimum value
99 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
100 // ...).
101 //
102 // This may only be called by the producer. The consumer will be told
103 // to discard buffers through the onBuffersReleased callback.
Daniel Lam6b091c52012-01-22 15:26:27 -0800104 virtual status_t setBufferCount(int bufferCount);
105
Andy McFadden753e3412013-04-04 17:09:03 -0700106 // requestBuffer returns the GraphicBuffer for slot N.
107 //
108 // In normal operation, this is called the first time slot N is returned
109 // by dequeueBuffer. It must be called again if dequeueBuffer returns
110 // flags indicating that previously-returned buffers are no longer valid.
Daniel Lam6b091c52012-01-22 15:26:27 -0800111 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
112
Andy McFadden753e3412013-04-04 17:09:03 -0700113 // dequeueBuffer gets the next buffer slot index for the producer to use.
114 // If a buffer slot is available then that slot index is written to the
115 // location pointed to by the buf argument and a status of OK is returned.
116 // If no slot is available then a status of -EBUSY is returned and buf is
Daniel Lam6b091c52012-01-22 15:26:27 -0800117 // unmodified.
Jesse Hallf7857542012-06-14 15:26:33 -0700118 //
119 // The fence parameter will be updated to hold the fence associated with
120 // the buffer. The contents of the buffer must not be overwritten until the
Andy McFadden753e3412013-04-04 17:09:03 -0700121 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
122 // written immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700123 //
Daniel Lam6b091c52012-01-22 15:26:27 -0800124 // The width and height parameters must be no greater than the minimum of
125 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
126 // An error due to invalid dimensions might not be reported until
Andy McFadden753e3412013-04-04 17:09:03 -0700127 // updateTexImage() is called. If width and height are both zero, the
128 // default values specified by setDefaultBufferSize() are used instead.
129 //
130 // The pixel formats are enumerated in graphics.h, e.g.
131 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
132 // will be used.
133 //
134 // The usage argument specifies gralloc buffer usage flags. The values
135 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
136 // will be merged with the usage flags specified by setConsumerUsageBits.
137 //
138 // The return value may be a negative error value or a non-negative
139 // collection of flags. If the flags are set, the return values are
140 // valid, but additional actions must be performed.
141 //
142 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
143 // producer must discard cached GraphicBuffer references for the slot
144 // returned in buf.
145 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
146 // must discard cached GraphicBuffer references for all slots.
147 //
148 // In both cases, the producer will need to call requestBuffer to get a
149 // GraphicBuffer handle for the returned slot.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700150 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700151 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
Daniel Lam6b091c52012-01-22 15:26:27 -0800152
Andy McFadden753e3412013-04-04 17:09:03 -0700153 // queueBuffer returns a filled buffer to the BufferQueue.
154 //
155 // Additional data is provided in the QueueBufferInput struct. Notably,
156 // a timestamp must be provided for the buffer. The timestamp is in
Daniel Lam6b091c52012-01-22 15:26:27 -0800157 // nanoseconds, and must be monotonically increasing. Its other semantics
Andy McFadden753e3412013-04-04 17:09:03 -0700158 // (zero point, etc) are producer-specific and should be documented by the
159 // producer.
160 //
161 // The caller may provide a fence that signals when all rendering
162 // operations have completed. Alternatively, NO_FENCE may be used,
163 // indicating that the buffer is ready immediately.
164 //
165 // Some values are returned in the output struct: the current settings
166 // for default width and height, the current transform hint, and the
167 // number of queued buffers.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700168 virtual status_t queueBuffer(int buf,
169 const QueueBufferInput& input, QueueBufferOutput* output);
170
Andy McFadden753e3412013-04-04 17:09:03 -0700171 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
172 // queue it for use by the consumer.
173 //
174 // The buffer will not be overwritten until the fence signals. The fence
175 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700176 virtual void cancelBuffer(int buf, const sp<Fence>& fence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800177
Andy McFadden753e3412013-04-04 17:09:03 -0700178 // connect attempts to connect a producer API to the BufferQueue. This
179 // must be called before any other IGraphicBufferProducer methods are
180 // called except for getAllocator. A consumer must already be connected.
Daniel Lam6b091c52012-01-22 15:26:27 -0800181 //
Andy McFadden753e3412013-04-04 17:09:03 -0700182 // This method will fail if connect was previously called on the
183 // BufferQueue and no corresponding disconnect call was made (i.e. if
184 // it's still connected to a producer).
185 //
186 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Mathias Agopian595264f2013-07-16 22:56:09 -0700187 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output);
Daniel Lam6b091c52012-01-22 15:26:27 -0800188
Andy McFadden753e3412013-04-04 17:09:03 -0700189 // disconnect attempts to disconnect a producer API from the BufferQueue.
190 // Calling this method will cause any subsequent calls to other
Andy McFadden2adaf042012-12-18 09:49:45 -0800191 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
Daniel Lam6b091c52012-01-22 15:26:27 -0800192 // Successfully calling connect after this will allow the other methods to
193 // succeed again.
194 //
195 // This method will fail if the the BufferQueue is not currently
Andy McFadden753e3412013-04-04 17:09:03 -0700196 // connected to the specified producer API.
Daniel Lam6b091c52012-01-22 15:26:27 -0800197 virtual status_t disconnect(int api);
198
Mathias Agopiana4e19522013-07-31 20:09:53 -0700199 /*
200 * IGraphicBufferConsumer interface
201 */
Daniel Lameae59d22012-01-22 15:26:27 -0800202
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700203 // acquireBuffer attempts to acquire ownership of the next pending buffer in
204 // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a
205 // buffer is successfully acquired, the information about the buffer is
206 // returned in BufferItem. If the buffer returned had previously been
207 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
208 // NULL and it is assumed that the consumer still holds a reference to the
209 // buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700210 //
211 // If presentWhen is nonzero, it indicates the time when the buffer will
212 // be displayed on screen. If the buffer's timestamp is farther in the
213 // future, the buffer won't be acquired, and PRESENT_LATER will be
214 // returned. The presentation time is in nanoseconds, and the time base
215 // is CLOCK_MONOTONIC.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700216 virtual status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen);
Daniel Lameae59d22012-01-22 15:26:27 -0800217
218 // releaseBuffer releases a buffer slot from the consumer back to the
Andy McFadden753e3412013-04-04 17:09:03 -0700219 // BufferQueue. This may be done while the buffer's contents are still
220 // being accessed. The fence will signal when the buffer is no longer
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700221 // in use. frameNumber is used to indentify the exact buffer returned.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700222 //
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700223 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
224 // any references to the just-released buffer that it might have, as if it
225 // had received a onBuffersReleased() call with a mask set for the released
226 // buffer.
227 //
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700228 // Note that the dependencies on EGL will be removed once we switch to using
229 // the Android HW Sync HAL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700230 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700231 EGLDisplay display, EGLSyncKHR fence,
Jesse Hallf7857542012-06-14 15:26:33 -0700232 const sp<Fence>& releaseFence);
Daniel Lameae59d22012-01-22 15:26:27 -0800233
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700234 // consumerConnect connects a consumer to the BufferQueue. Only one
235 // consumer may be connected, and when that consumer disconnects the
236 // BufferQueue is placed into the "abandoned" state, causing most
237 // interactions with the BufferQueue by the producer to fail.
Mathias Agopian595264f2013-07-16 22:56:09 -0700238 // controlledByApp indicates whether the consumer is controlled by
239 // the application.
Andy McFadden753e3412013-04-04 17:09:03 -0700240 //
241 // consumer may not be NULL.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700242 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700243
Daniel Lameae59d22012-01-22 15:26:27 -0800244 // consumerDisconnect disconnects a consumer from the BufferQueue. All
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700245 // buffers will be freed and the BufferQueue is placed in the "abandoned"
246 // state, causing most interactions with the BufferQueue by the producer to
247 // fail.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700248 virtual status_t consumerDisconnect();
Daniel Lameae59d22012-01-22 15:26:27 -0800249
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700250 // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
Andy McFadden753e3412013-04-04 17:09:03 -0700251 // indicating which buffer slots have been released by the BufferQueue
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700252 // but have not yet been released by the consumer.
Andy McFadden753e3412013-04-04 17:09:03 -0700253 //
254 // This should be called from the onBuffersReleased() callback.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700255 virtual status_t getReleasedBuffers(uint32_t* slotMask);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700256
Daniel Lameae59d22012-01-22 15:26:27 -0800257 // setDefaultBufferSize is used to set the size of buffers returned by
Andy McFadden753e3412013-04-04 17:09:03 -0700258 // dequeueBuffer when a width and height of zero is requested. Default
259 // is 1x1.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700260 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h);
Daniel Lameae59d22012-01-22 15:26:27 -0800261
Andy McFadden753e3412013-04-04 17:09:03 -0700262 // setDefaultMaxBufferCount sets the default value for the maximum buffer
263 // count (the initial default is 2). If the producer has requested a
264 // buffer count using setBufferCount, the default buffer count will only
265 // take effect if the producer sets the count back to zero.
266 //
267 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700268 virtual status_t setDefaultMaxBufferCount(int bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -0800269
Mathias Agopianad678e12013-07-23 17:28:53 -0700270 // disableAsyncBuffer disables the extra buffer used in async mode
271 // (when both producer and consumer have set their "isControlledByApp"
272 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
273 //
274 // This can only be called before consumerConnect().
Mathias Agopiana4e19522013-07-31 20:09:53 -0700275 virtual status_t disableAsyncBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700276
Jamie Gennis72f096f2012-08-27 18:48:37 -0700277 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Andy McFadden753e3412013-04-04 17:09:03 -0700278 // be acquired by the consumer at one time (default 1). This call will
279 // fail if a producer is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700280 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700281
Daniel Lameae59d22012-01-22 15:26:27 -0800282 // setConsumerName sets the name used in logging
Mathias Agopiana4e19522013-07-31 20:09:53 -0700283 virtual void setConsumerName(const String8& name);
Daniel Lameae59d22012-01-22 15:26:27 -0800284
Daniel Lamb2675792012-02-23 14:35:13 -0800285 // setDefaultBufferFormat allows the BufferQueue to create
286 // GraphicBuffers of a defaultFormat if no format is specified
Andy McFadden753e3412013-04-04 17:09:03 -0700287 // in dequeueBuffer. Formats are enumerated in graphics.h; the
288 // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700289 virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800290
Andy McFadden753e3412013-04-04 17:09:03 -0700291 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
292 // These are merged with the bits passed to dequeueBuffer. The values are
293 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700294 virtual status_t setConsumerUsageBits(uint32_t usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800295
Andy McFadden753e3412013-04-04 17:09:03 -0700296 // setTransformHint bakes in rotation to buffers so overlays can be used.
297 // The values are enumerated in window.h, e.g.
298 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700299 virtual status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800300
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700301 // dump our state in a String
302 virtual void dump(String8& result, const char* prefix) const;
303
Mathias Agopianad678e12013-07-23 17:28:53 -0700304
Daniel Lameae59d22012-01-22 15:26:27 -0800305private:
Andy McFadden753e3412013-04-04 17:09:03 -0700306 // freeBufferLocked frees the GraphicBuffer and sync resources for the
307 // given slot.
Daniel Lam6b091c52012-01-22 15:26:27 -0800308 void freeBufferLocked(int index);
309
Andy McFadden753e3412013-04-04 17:09:03 -0700310 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
311 // all slots.
Daniel Lam6b091c52012-01-22 15:26:27 -0800312 void freeAllBuffersLocked();
313
Jamie Gennis31a353d2012-08-24 17:25:13 -0700314 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
315 // that will be used if the producer does not override the buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700316 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
317 // The initial default is 2.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700318 status_t setDefaultMaxBufferCountLocked(int count);
319
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700320 // getMinUndequeuedBufferCount returns the minimum number of buffers
321 // that must remain in a state other than DEQUEUED.
322 // The async parameter tells whether we're in asynchronous mode.
323 int getMinUndequeuedBufferCount(bool async) const;
324
Jamie Gennis31a353d2012-08-24 17:25:13 -0700325 // getMinBufferCountLocked returns the minimum number of buffers allowed
326 // given the current BufferQueue state.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700327 // The async parameter tells whether we're in asynchronous mode.
328 int getMinMaxBufferCountLocked(bool async) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800329
Jamie Gennise191e6c2012-08-24 20:26:34 -0700330 // getMaxBufferCountLocked returns the maximum number of buffers that can
331 // be allocated at once. This value depends upon the following member
332 // variables:
333 //
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700334 // mDequeueBufferCannotBlock
Jamie Gennis72f096f2012-08-27 18:48:37 -0700335 // mMaxAcquiredBufferCount
Jamie Gennise191e6c2012-08-24 20:26:34 -0700336 // mDefaultMaxBufferCount
337 // mOverrideMaxBufferCount
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700338 // async parameter
Jamie Gennise191e6c2012-08-24 20:26:34 -0700339 //
340 // Any time one of these member variables is changed while a producer is
341 // connected, mDequeueCondition must be broadcast.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700342 int getMaxBufferCountLocked(bool async) const;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700343
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700344 // stillTracking returns true iff the buffer item is still being tracked
345 // in one of the slots.
346 bool stillTracking(const BufferItem *item) const;
347
Daniel Lam6b091c52012-01-22 15:26:27 -0800348 struct BufferSlot {
349
350 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800351 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800352 mBufferState(BufferSlot::FREE),
353 mRequestBufferCalled(false),
Daniel Lam6b091c52012-01-22 15:26:27 -0800354 mFrameNumber(0),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700355 mEglFence(EGL_NO_SYNC_KHR),
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700356 mAcquireCalled(false),
357 mNeedsCleanupOnRelease(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800358 }
359
360 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
361 // if no buffer has been allocated.
362 sp<GraphicBuffer> mGraphicBuffer;
363
Andy McFadden753e3412013-04-04 17:09:03 -0700364 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
Daniel Lam6b091c52012-01-22 15:26:27 -0800365 EGLDisplay mEglDisplay;
366
367 // BufferState represents the different states in which a buffer slot
Andy McFadden753e3412013-04-04 17:09:03 -0700368 // can be. All slots are initially FREE.
Daniel Lam6b091c52012-01-22 15:26:27 -0800369 enum BufferState {
Andy McFadden753e3412013-04-04 17:09:03 -0700370 // FREE indicates that the buffer is available to be dequeued
371 // by the producer. The buffer may be in use by the consumer for
372 // a finite time, so the buffer must not be modified until the
373 // associated fence is signaled.
374 //
375 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
376 // when dequeueBuffer is called.
Daniel Lam6b091c52012-01-22 15:26:27 -0800377 FREE = 0,
378
379 // DEQUEUED indicates that the buffer has been dequeued by the
Andy McFadden753e3412013-04-04 17:09:03 -0700380 // producer, but has not yet been queued or canceled. The
381 // producer may modify the buffer's contents as soon as the
382 // associated ready fence is signaled.
Daniel Lam6b091c52012-01-22 15:26:27 -0800383 //
Andy McFadden753e3412013-04-04 17:09:03 -0700384 // The slot is "owned" by the producer. It can transition to
385 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
Daniel Lam6b091c52012-01-22 15:26:27 -0800386 DEQUEUED = 1,
387
Andy McFadden753e3412013-04-04 17:09:03 -0700388 // QUEUED indicates that the buffer has been filled by the
389 // producer and queued for use by the consumer. The buffer
390 // contents may continue to be modified for a finite time, so
391 // the contents must not be accessed until the associated fence
392 // is signaled.
393 //
394 // The slot is "owned" by BufferQueue. It can transition to
395 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
396 // queued in asynchronous mode).
Daniel Lam6b091c52012-01-22 15:26:27 -0800397 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800398
Andy McFadden753e3412013-04-04 17:09:03 -0700399 // ACQUIRED indicates that the buffer has been acquired by the
400 // consumer. As with QUEUED, the contents must not be accessed
401 // by the consumer until the fence is signaled.
402 //
403 // The slot is "owned" by the consumer. It transitions to FREE
404 // when releaseBuffer is called.
Daniel Lameae59d22012-01-22 15:26:27 -0800405 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800406 };
407
408 // mBufferState is the current state of this buffer slot.
409 BufferState mBufferState;
410
Andy McFadden753e3412013-04-04 17:09:03 -0700411 // mRequestBufferCalled is used for validating that the producer did
Daniel Lam6b091c52012-01-22 15:26:27 -0800412 // call requestBuffer() when told to do so. Technically this is not
Andy McFadden753e3412013-04-04 17:09:03 -0700413 // needed but useful for debugging and catching producer bugs.
Daniel Lam6b091c52012-01-22 15:26:27 -0800414 bool mRequestBufferCalled;
415
Andy McFadden753e3412013-04-04 17:09:03 -0700416 // mFrameNumber is the number of the queued frame for this slot. This
417 // is used to dequeue buffers in LRU order (useful because buffers
418 // may be released before their release fence is signaled).
Daniel Lam6b091c52012-01-22 15:26:27 -0800419 uint64_t mFrameNumber;
420
Jesse Hallc777b0b2012-06-28 12:52:05 -0700421 // mEglFence is the EGL sync object that must signal before the buffer
Daniel Lam6b091c52012-01-22 15:26:27 -0800422 // associated with this buffer slot may be dequeued. It is initialized
Andy McFadden753e3412013-04-04 17:09:03 -0700423 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
424 // new sync object in releaseBuffer. (This is deprecated in favor of
425 // mFence, below.)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700426 EGLSyncKHR mEglFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800427
Jesse Hallc777b0b2012-06-28 12:52:05 -0700428 // mFence is a fence which will signal when work initiated by the
429 // previous owner of the buffer is finished. When the buffer is FREE,
430 // the fence indicates when the consumer has finished reading
431 // from the buffer, or when the producer has finished writing if it
432 // called cancelBuffer after queueing some writes. When the buffer is
433 // QUEUED, it indicates when the producer has finished filling the
434 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
435 // passed to the consumer or producer along with ownership of the
Andy McFadden753e3412013-04-04 17:09:03 -0700436 // buffer, and mFence is set to NO_FENCE.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700437 sp<Fence> mFence;
Jesse Hallef194142012-06-14 14:45:17 -0700438
Daniel Lameae59d22012-01-22 15:26:27 -0800439 // Indicates whether this buffer has been seen by a consumer yet
440 bool mAcquireCalled;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700441
Andy McFadden753e3412013-04-04 17:09:03 -0700442 // Indicates whether this buffer needs to be cleaned up by the
443 // consumer. This is set when a buffer in ACQUIRED state is freed.
444 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700445 bool mNeedsCleanupOnRelease;
Daniel Lam6b091c52012-01-22 15:26:27 -0800446 };
447
Andy McFadden753e3412013-04-04 17:09:03 -0700448 // mSlots is the array of buffer slots that must be mirrored on the
449 // producer side. This allows buffer ownership to be transferred between
450 // the producer and consumer without sending a GraphicBuffer over binder.
451 // The entire array is initialized to NULL at construction time, and
452 // buffers are allocated for a slot when requestBuffer is called with
453 // that slot's index.
Daniel Lam6b091c52012-01-22 15:26:27 -0800454 BufferSlot mSlots[NUM_BUFFER_SLOTS];
455
Daniel Lam6b091c52012-01-22 15:26:27 -0800456 // mDefaultWidth holds the default width of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700457 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800458 uint32_t mDefaultWidth;
459
460 // mDefaultHeight holds the default height of allocated buffers. It is used
Andy McFadden753e3412013-04-04 17:09:03 -0700461 // in dequeueBuffer() if a width and height of zero is specified.
Daniel Lam6b091c52012-01-22 15:26:27 -0800462 uint32_t mDefaultHeight;
463
Jamie Gennis72f096f2012-08-27 18:48:37 -0700464 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
465 // acquire at one time. It defaults to 1 and can be changed by the
466 // consumer via the setMaxAcquiredBufferCount method, but this may only be
467 // done when no producer is connected to the BufferQueue.
468 //
469 // This value is used to derive the value returned for the
470 // MIN_UNDEQUEUED_BUFFERS query by the producer.
471 int mMaxAcquiredBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700472
Jamie Gennis31a353d2012-08-24 17:25:13 -0700473 // mDefaultMaxBufferCount is the default limit on the number of buffers
474 // that will be allocated at one time. This default limit is set by the
475 // consumer. The limit (as opposed to the default limit) may be
476 // overridden by the producer.
477 int mDefaultMaxBufferCount;
Daniel Lamabe61bf2012-03-26 20:37:15 -0700478
Jamie Gennis31a353d2012-08-24 17:25:13 -0700479 // mOverrideMaxBufferCount is the limit on the number of buffers that will
480 // be allocated at one time. This value is set by the image producer by
481 // calling setBufferCount. The default is zero, which means the producer
482 // doesn't care about the number of buffers in the pool. In that case
483 // mDefaultMaxBufferCount is used as the limit.
484 int mOverrideMaxBufferCount;
Daniel Lam6b091c52012-01-22 15:26:27 -0800485
Daniel Lam6b091c52012-01-22 15:26:27 -0800486 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
487 // allocate new GraphicBuffer objects.
488 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
489
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700490 // mConsumerListener is used to notify the connected consumer of
491 // asynchronous events that it may wish to react to. It is initially set
492 // to NULL and is written by consumerConnect and consumerDisconnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700493 sp<IConsumerListener> mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800494
Mathias Agopian595264f2013-07-16 22:56:09 -0700495 // mConsumerControlledByApp whether the connected consumer is controlled by the
496 // application.
497 bool mConsumerControlledByApp;
498
499 // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
Mathias Agopianad678e12013-07-23 17:28:53 -0700500 // this flag is set during connect() when both consumer and producer are controlled
Mathias Agopian595264f2013-07-16 22:56:09 -0700501 // by the application.
502 bool mDequeueBufferCannotBlock;
503
Mathias Agopianad678e12013-07-23 17:28:53 -0700504 // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent
505 // dequeueBuffer() from ever blocking.
506 bool mUseAsyncBuffer;
507
Andy McFadden753e3412013-04-04 17:09:03 -0700508 // mConnectedApi indicates the producer API that is currently connected
509 // to this BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets
510 // updated by the connect and disconnect methods.
Daniel Lam6b091c52012-01-22 15:26:27 -0800511 int mConnectedApi;
512
513 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
514 mutable Condition mDequeueCondition;
515
516 // mQueue is a FIFO of queued buffers used in synchronous mode
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700517 typedef Vector<BufferItem> Fifo;
Daniel Lam6b091c52012-01-22 15:26:27 -0800518 Fifo mQueue;
519
520 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden753e3412013-04-04 17:09:03 -0700521 // consume image buffers pushed to it using the IGraphicBufferProducer
522 // interface. It is initialized to false, and set to true in the
523 // consumerDisconnect method. A BufferQueue that has been abandoned will
524 // return the NO_INIT error from all IGraphicBufferProducer methods
525 // capable of returning an error.
Daniel Lam6b091c52012-01-22 15:26:27 -0800526 bool mAbandoned;
527
Andy McFadden753e3412013-04-04 17:09:03 -0700528 // mConsumerName is a string used to identify the BufferQueue in log
529 // messages. It is set by the setConsumerName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800530 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800531
532 // mMutex is the mutex used to prevent concurrent access to the member
533 // variables of BufferQueue objects. It must be locked whenever the
534 // member variables are accessed.
535 mutable Mutex mMutex;
536
Andy McFadden753e3412013-04-04 17:09:03 -0700537 // mFrameCounter is the free running counter, incremented on every
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700538 // successful queueBuffer call, and buffer allocation.
Daniel Lam6b091c52012-01-22 15:26:27 -0800539 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800540
Andy McFadden753e3412013-04-04 17:09:03 -0700541 // mBufferHasBeenQueued is true once a buffer has been queued. It is
542 // reset when something causes all buffers to be freed (e.g. changing the
543 // buffer count).
Daniel Lameae59d22012-01-22 15:26:27 -0800544 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800545
546 // mDefaultBufferFormat can be set so it will override
547 // the buffer format when it isn't specified in dequeueBuffer
548 uint32_t mDefaultBufferFormat;
549
550 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
551 uint32_t mConsumerUsageBits;
552
553 // mTransformHint is used to optimize for screen rotations
554 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800555};
556
557// ----------------------------------------------------------------------------
558}; // namespace android
559
560#endif // ANDROID_GUI_BUFFERQUEUE_H