blob: 46d18541395d94d70889731b47c84cc045fbde8f [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>
21
Mathias Agopian90ac7992012-02-25 18:48:35 -080022#include <gui/IGraphicBufferAlloc.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080023#include <gui/ISurfaceTexture.h>
24
Daniel Lam6b091c52012-01-22 15:26:27 -080025#include <ui/GraphicBuffer.h>
26
27#include <utils/String8.h>
28#include <utils/Vector.h>
29#include <utils/threads.h>
30
31namespace android {
32// ----------------------------------------------------------------------------
33
34class BufferQueue : public BnSurfaceTexture {
35public:
36 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
37 enum {
38 MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1,
39 MIN_SYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS
40 };
41 enum { NUM_BUFFER_SLOTS = 32 };
42 enum { NO_CONNECTED_API = 0 };
Daniel Lameae59d22012-01-22 15:26:27 -080043 enum { INVALID_BUFFER_SLOT = -1 };
Daniel Lam6b091c52012-01-22 15:26:27 -080044
45 struct FrameAvailableListener : public virtual RefBase {
46 // onFrameAvailable() is called from queueBuffer() each time an
47 // additional frame becomes available for consumption. This means that
48 // frames that are queued while in asynchronous mode only trigger the
49 // callback if no previous frames are pending. Frames queued while in
50 // synchronous mode always trigger the callback.
51 //
52 // This is called without any lock held and can be called concurrently
53 // by multiple threads.
54 virtual void onFrameAvailable() = 0;
55 };
56
57 // BufferQueue manages a pool of gralloc memory slots to be used
58 // by producers and consumers.
59 // allowSynchronousMode specifies whether or not synchronous mode can be
60 // enabled.
61 BufferQueue(bool allowSynchronousMode = true);
62 virtual ~BufferQueue();
63
Daniel Lamb8560522012-01-30 15:51:27 -080064 virtual int query(int what, int* value);
65
Daniel Lam6b091c52012-01-22 15:26:27 -080066 // setBufferCount updates the number of available buffer slots. After
67 // calling this all buffer slots are both unallocated and owned by the
68 // BufferQueue object (i.e. they are not owned by the client).
69 virtual status_t setBufferCount(int bufferCount);
70
71 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
72
73 // dequeueBuffer gets the next buffer slot index for the client to use. If a
74 // buffer slot is available then that slot index is written to the location
75 // pointed to by the buf argument and a status of OK is returned. If no
76 // slot is available then a status of -EBUSY is returned and buf is
77 // unmodified.
78 // The width and height parameters must be no greater than the minimum of
79 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
80 // An error due to invalid dimensions might not be reported until
81 // updateTexImage() is called.
82 virtual status_t dequeueBuffer(int *buf, uint32_t width, uint32_t height,
83 uint32_t format, uint32_t usage);
84
85 // queueBuffer returns a filled buffer to the BufferQueue. In addition, a
86 // timestamp must be provided for the buffer. The timestamp is in
87 // nanoseconds, and must be monotonically increasing. Its other semantics
88 // (zero point, etc) are client-dependent and should be documented by the
89 // client.
90 virtual status_t queueBuffer(int buf, int64_t timestamp,
91 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
92 virtual void cancelBuffer(int buf);
93 virtual status_t setCrop(const Rect& reg);
94 virtual status_t setTransform(uint32_t transform);
95 virtual status_t setScalingMode(int mode);
96
97 // setSynchronousMode set whether dequeueBuffer is synchronous or
98 // asynchronous. In synchronous mode, dequeueBuffer blocks until
99 // a buffer is available, the currently bound buffer can be dequeued and
100 // queued buffers will be retired in order.
101 // The default mode is asynchronous.
102 virtual status_t setSynchronousMode(bool enabled);
103
104 // connect attempts to connect a producer client API to the BufferQueue.
105 // This must be called before any other ISurfaceTexture methods are called
106 // except for getAllocator.
107 //
108 // This method will fail if the connect was previously called on the
109 // BufferQueue and no corresponding disconnect call was made.
110 virtual status_t connect(int api,
111 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
112
113 // disconnect attempts to disconnect a producer client API from the
114 // BufferQueue. Calling this method will cause any subsequent calls to other
115 // ISurfaceTexture methods to fail except for getAllocator and connect.
116 // Successfully calling connect after this will allow the other methods to
117 // succeed again.
118 //
119 // This method will fail if the the BufferQueue is not currently
120 // connected to the specified client API.
121 virtual status_t disconnect(int api);
122
Daniel Lameae59d22012-01-22 15:26:27 -0800123 // dump our state in a String
124 virtual void dump(String8& result) const;
125 virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
Daniel Lam6b091c52012-01-22 15:26:27 -0800126
Daniel Lameae59d22012-01-22 15:26:27 -0800127 // public facing structure for BufferSlot
128 struct BufferItem {
129
130 BufferItem()
131 :
132 mTransform(0),
133 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
134 mTimestamp(0),
135 mFrameNumber(0),
136 mBuf(INVALID_BUFFER_SLOT) {
137 mCrop.makeInvalid();
138 }
139 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
140 // if no buffer has been allocated.
141 sp<GraphicBuffer> mGraphicBuffer;
142
143 // mCrop is the current crop rectangle for this buffer slot. This gets
144 // set to mNextCrop each time queueBuffer gets called for this buffer.
145 Rect mCrop;
146
147 // mTransform is the current transform flags for this buffer slot. This
148 // gets set to mNextTransform each time queueBuffer gets called for this
149 // slot.
150 uint32_t mTransform;
151
152 // mScalingMode is the current scaling mode for this buffer slot. This
153 // gets set to mNextScalingMode each time queueBuffer gets called for
154 // this slot.
155 uint32_t mScalingMode;
156
157 // mTimestamp is the current timestamp for this buffer slot. This gets
158 // to set by queueBuffer each time this slot is queued.
159 int64_t mTimestamp;
160
161 // mFrameNumber is the number of the queued frame for this slot.
162 uint64_t mFrameNumber;
163
164 // buf is the slot index of this buffer
165 int mBuf;
166
167 };
168
169 // The following public functions is the consumer facing interface
170
171 // acquire consumes a buffer by transferring its ownership to a consumer.
172 // buffer contains the GraphicBuffer and its corresponding information.
173 // buffer.mGraphicsBuffer will be NULL when the buffer has been already
174 // acquired by the consumer.
175
176 status_t acquire(BufferItem *buffer);
177
178 // releaseBuffer releases a buffer slot from the consumer back to the
179 // BufferQueue pending a fence sync.
180 status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence);
181
182 // consumerDisconnect disconnects a consumer from the BufferQueue. All
183 // buffers will be freed.
184 status_t consumerDisconnect();
185
186 // setDefaultBufferSize is used to set the size of buffers returned by
187 // requestBuffers when a with and height of zero is requested.
188 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
189
190 // setBufferCountServer set the buffer count. If the client has requested
191 // a buffer count using setBufferCount, the server-buffer count will
192 // take effect once the client sets the count back to zero.
193 status_t setBufferCountServer(int bufferCount);
194
195 // isSynchronousMode returns whether the SurfaceTexture is currently in
196 // synchronous mode.
197 bool isSynchronousMode() const;
198
199 // setConsumerName sets the name used in logging
200 void setConsumerName(const String8& name);
201
202 // setFrameAvailableListener sets the listener object that will be notified
203 // when a new frame becomes available.
204 void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
205
Daniel Lamb2675792012-02-23 14:35:13 -0800206 // setDefaultBufferFormat allows the BufferQueue to create
207 // GraphicBuffers of a defaultFormat if no format is specified
208 // in dequeueBuffer
209 status_t setDefaultBufferFormat(uint32_t defaultFormat);
210
211 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
212 status_t setConsumerUsageBits(uint32_t usage);
213
214 // setTransformHint bakes in rotation to buffers so overlays can be used
215 status_t setTransformHint(uint32_t hint);
Daniel Lameae59d22012-01-22 15:26:27 -0800216
217private:
Daniel Lam6b091c52012-01-22 15:26:27 -0800218 // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
219 // for the given slot.
220 void freeBufferLocked(int index);
221
222 // freeAllBuffersLocked frees the resources (both GraphicBuffer and
223 // EGLImage) for all slots.
224 void freeAllBuffersLocked();
225
226 // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
227 // and EGLImage) for all slots except the head of mQueue
228 void freeAllBuffersExceptHeadLocked();
229
230 // drainQueueLocked drains the buffer queue if we're in synchronous mode
231 // returns immediately otherwise. It returns NO_INIT if the BufferQueue
232 // became abandoned or disconnected during this call.
233 status_t drainQueueLocked();
234
235 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
236 // synchronous mode and free all buffers. In asynchronous mode, all buffers
237 // are freed except the current buffer.
238 status_t drainQueueAndFreeBuffersLocked();
239
240 status_t setBufferCountServerLocked(int bufferCount);
241
Daniel Lam6b091c52012-01-22 15:26:27 -0800242 struct BufferSlot {
243
244 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800245 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800246 mBufferState(BufferSlot::FREE),
247 mRequestBufferCalled(false),
248 mTransform(0),
249 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
250 mTimestamp(0),
251 mFrameNumber(0),
Daniel Lameae59d22012-01-22 15:26:27 -0800252 mFence(EGL_NO_SYNC_KHR),
253 mAcquireCalled(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800254 mCrop.makeInvalid();
255 }
256
257 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
258 // if no buffer has been allocated.
259 sp<GraphicBuffer> mGraphicBuffer;
260
Daniel Lam6b091c52012-01-22 15:26:27 -0800261 // mEglDisplay is the EGLDisplay used to create mEglImage.
262 EGLDisplay mEglDisplay;
263
264 // BufferState represents the different states in which a buffer slot
265 // can be.
266 enum BufferState {
267 // FREE indicates that the buffer is not currently being used and
268 // will not be used in the future until it gets dequeued and
269 // subsequently queued by the client.
Daniel Lameae59d22012-01-22 15:26:27 -0800270 // aka "owned by BufferQueue, ready to be dequeued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800271 FREE = 0,
272
273 // DEQUEUED indicates that the buffer has been dequeued by the
274 // client, but has not yet been queued or canceled. The buffer is
275 // considered 'owned' by the client, and the server should not use
276 // it for anything.
277 //
278 // Note that when in synchronous-mode (mSynchronousMode == true),
279 // the buffer that's currently attached to the texture may be
280 // dequeued by the client. That means that the current buffer can
281 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
282 // however, the current buffer is always in the QUEUED state.
Daniel Lameae59d22012-01-22 15:26:27 -0800283 // aka "owned by producer, ready to be queued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800284 DEQUEUED = 1,
285
286 // QUEUED indicates that the buffer has been queued by the client,
287 // and has not since been made available for the client to dequeue.
288 // Attaching the buffer to the texture does NOT transition the
289 // buffer away from the QUEUED state. However, in Synchronous mode
290 // the current buffer may be dequeued by the client under some
291 // circumstances. See the note about the current buffer in the
292 // documentation for DEQUEUED.
Daniel Lameae59d22012-01-22 15:26:27 -0800293 // aka "owned by BufferQueue, ready to be acquired"
Daniel Lam6b091c52012-01-22 15:26:27 -0800294 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800295
296 // aka "owned by consumer, ready to be released"
297 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800298 };
299
300 // mBufferState is the current state of this buffer slot.
301 BufferState mBufferState;
302
303 // mRequestBufferCalled is used for validating that the client did
304 // call requestBuffer() when told to do so. Technically this is not
305 // needed but useful for debugging and catching client bugs.
306 bool mRequestBufferCalled;
307
308 // mCrop is the current crop rectangle for this buffer slot. This gets
309 // set to mNextCrop each time queueBuffer gets called for this buffer.
310 Rect mCrop;
311
312 // mTransform is the current transform flags for this buffer slot. This
313 // gets set to mNextTransform each time queueBuffer gets called for this
314 // slot.
315 uint32_t mTransform;
316
317 // mScalingMode is the current scaling mode for this buffer slot. This
318 // gets set to mNextScalingMode each time queueBuffer gets called for
319 // this slot.
320 uint32_t mScalingMode;
321
322 // mTimestamp is the current timestamp for this buffer slot. This gets
323 // to set by queueBuffer each time this slot is queued.
324 int64_t mTimestamp;
325
326 // mFrameNumber is the number of the queued frame for this slot.
327 uint64_t mFrameNumber;
328
329 // mFence is the EGL sync object that must signal before the buffer
330 // associated with this buffer slot may be dequeued. It is initialized
331 // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
332 // on a compile-time option) set to a new sync object in updateTexImage.
333 EGLSyncKHR mFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800334
335 // Indicates whether this buffer has been seen by a consumer yet
336 bool mAcquireCalled;
Daniel Lam6b091c52012-01-22 15:26:27 -0800337 };
338
339 // mSlots is the array of buffer slots that must be mirrored on the client
340 // side. This allows buffer ownership to be transferred between the client
341 // and server without sending a GraphicBuffer over binder. The entire array
342 // is initialized to NULL at construction time, and buffers are allocated
343 // for a slot when requestBuffer is called with that slot's index.
344 BufferSlot mSlots[NUM_BUFFER_SLOTS];
345
Daniel Lam6b091c52012-01-22 15:26:27 -0800346 // mDefaultWidth holds the default width of allocated buffers. It is used
347 // in requestBuffers() if a width and height of zero is specified.
348 uint32_t mDefaultWidth;
349
350 // mDefaultHeight holds the default height of allocated buffers. It is used
351 // in requestBuffers() if a width and height of zero is specified.
352 uint32_t mDefaultHeight;
353
354 // mPixelFormat holds the pixel format of allocated buffers. It is used
355 // in requestBuffers() if a format of zero is specified.
356 uint32_t mPixelFormat;
357
358 // mBufferCount is the number of buffer slots that the client and server
359 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
360 // by calling setBufferCount or setBufferCountServer
361 int mBufferCount;
362
363 // mClientBufferCount is the number of buffer slots requested by the client.
364 // The default is zero, which means the client doesn't care how many buffers
365 // there is.
366 int mClientBufferCount;
367
368 // mServerBufferCount buffer count requested by the server-side
369 int mServerBufferCount;
370
Daniel Lam6b091c52012-01-22 15:26:27 -0800371 // mNextCrop is the crop rectangle that will be used for the next buffer
372 // that gets queued. It is set by calling setCrop.
373 Rect mNextCrop;
374
375 // mNextTransform is the transform identifier that will be used for the next
376 // buffer that gets queued. It is set by calling setTransform.
377 uint32_t mNextTransform;
378
379 // mNextScalingMode is the scaling mode that will be used for the next
380 // buffers that get queued. It is set by calling setScalingMode.
381 int mNextScalingMode;
382
383 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
384 // allocate new GraphicBuffer objects.
385 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
386
387 // mFrameAvailableListener is the listener object that will be called when a
388 // new frame becomes available. If it is not NULL it will be called from
389 // queueBuffer.
390 sp<FrameAvailableListener> mFrameAvailableListener;
391
392 // mSynchronousMode whether we're in synchronous mode or not
393 bool mSynchronousMode;
394
395 // mAllowSynchronousMode whether we allow synchronous mode or not
396 const bool mAllowSynchronousMode;
397
398 // mConnectedApi indicates the API that is currently connected to this
399 // BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets updated
400 // by the connect and disconnect methods.
401 int mConnectedApi;
402
403 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
404 mutable Condition mDequeueCondition;
405
406 // mQueue is a FIFO of queued buffers used in synchronous mode
407 typedef Vector<int> Fifo;
408 Fifo mQueue;
409
410 // mAbandoned indicates that the BufferQueue will no longer be used to
411 // consume images buffers pushed to it using the ISurfaceTexture interface.
412 // It is initialized to false, and set to true in the abandon method. A
413 // BufferQueue that has been abandoned will return the NO_INIT error from
414 // all ISurfaceTexture methods capable of returning an error.
415 bool mAbandoned;
416
417 // mName is a string used to identify the BufferQueue in log messages.
418 // It is set by the setName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800419 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800420
421 // mMutex is the mutex used to prevent concurrent access to the member
422 // variables of BufferQueue objects. It must be locked whenever the
423 // member variables are accessed.
424 mutable Mutex mMutex;
425
426 // mFrameCounter is the free running counter, incremented for every buffer queued
427 // with the surface Texture.
428 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800429
Daniel Lamb2675792012-02-23 14:35:13 -0800430 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
431 // by changing the buffer count.
Daniel Lameae59d22012-01-22 15:26:27 -0800432 bool mBufferHasBeenQueued;
Daniel Lamb2675792012-02-23 14:35:13 -0800433
434 // mDefaultBufferFormat can be set so it will override
435 // the buffer format when it isn't specified in dequeueBuffer
436 uint32_t mDefaultBufferFormat;
437
438 // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
439 uint32_t mConsumerUsageBits;
440
441 // mTransformHint is used to optimize for screen rotations
442 uint32_t mTransformHint;
Daniel Lam6b091c52012-01-22 15:26:27 -0800443};
444
445// ----------------------------------------------------------------------------
446}; // namespace android
447
448#endif // ANDROID_GUI_BUFFERQUEUE_H