blob: 8c21a28cff5e7c3a2837d75c64710b18262a0f1d [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
206
207private:
Daniel Lam6b091c52012-01-22 15:26:27 -0800208 // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
209 // for the given slot.
210 void freeBufferLocked(int index);
211
212 // freeAllBuffersLocked frees the resources (both GraphicBuffer and
213 // EGLImage) for all slots.
214 void freeAllBuffersLocked();
215
216 // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
217 // and EGLImage) for all slots except the head of mQueue
218 void freeAllBuffersExceptHeadLocked();
219
220 // drainQueueLocked drains the buffer queue if we're in synchronous mode
221 // returns immediately otherwise. It returns NO_INIT if the BufferQueue
222 // became abandoned or disconnected during this call.
223 status_t drainQueueLocked();
224
225 // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
226 // synchronous mode and free all buffers. In asynchronous mode, all buffers
227 // are freed except the current buffer.
228 status_t drainQueueAndFreeBuffersLocked();
229
230 status_t setBufferCountServerLocked(int bufferCount);
231
Daniel Lam6b091c52012-01-22 15:26:27 -0800232 struct BufferSlot {
233
234 BufferSlot()
Daniel Lameae59d22012-01-22 15:26:27 -0800235 : mEglDisplay(EGL_NO_DISPLAY),
Daniel Lam6b091c52012-01-22 15:26:27 -0800236 mBufferState(BufferSlot::FREE),
237 mRequestBufferCalled(false),
238 mTransform(0),
239 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
240 mTimestamp(0),
241 mFrameNumber(0),
Daniel Lameae59d22012-01-22 15:26:27 -0800242 mFence(EGL_NO_SYNC_KHR),
243 mAcquireCalled(false) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800244 mCrop.makeInvalid();
245 }
246
247 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
248 // if no buffer has been allocated.
249 sp<GraphicBuffer> mGraphicBuffer;
250
Daniel Lam6b091c52012-01-22 15:26:27 -0800251 // mEglDisplay is the EGLDisplay used to create mEglImage.
252 EGLDisplay mEglDisplay;
253
254 // BufferState represents the different states in which a buffer slot
255 // can be.
256 enum BufferState {
257 // FREE indicates that the buffer is not currently being used and
258 // will not be used in the future until it gets dequeued and
259 // subsequently queued by the client.
Daniel Lameae59d22012-01-22 15:26:27 -0800260 // aka "owned by BufferQueue, ready to be dequeued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800261 FREE = 0,
262
263 // DEQUEUED indicates that the buffer has been dequeued by the
264 // client, but has not yet been queued or canceled. The buffer is
265 // considered 'owned' by the client, and the server should not use
266 // it for anything.
267 //
268 // Note that when in synchronous-mode (mSynchronousMode == true),
269 // the buffer that's currently attached to the texture may be
270 // dequeued by the client. That means that the current buffer can
271 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
272 // however, the current buffer is always in the QUEUED state.
Daniel Lameae59d22012-01-22 15:26:27 -0800273 // aka "owned by producer, ready to be queued"
Daniel Lam6b091c52012-01-22 15:26:27 -0800274 DEQUEUED = 1,
275
276 // QUEUED indicates that the buffer has been queued by the client,
277 // and has not since been made available for the client to dequeue.
278 // Attaching the buffer to the texture does NOT transition the
279 // buffer away from the QUEUED state. However, in Synchronous mode
280 // the current buffer may be dequeued by the client under some
281 // circumstances. See the note about the current buffer in the
282 // documentation for DEQUEUED.
Daniel Lameae59d22012-01-22 15:26:27 -0800283 // aka "owned by BufferQueue, ready to be acquired"
Daniel Lam6b091c52012-01-22 15:26:27 -0800284 QUEUED = 2,
Daniel Lameae59d22012-01-22 15:26:27 -0800285
286 // aka "owned by consumer, ready to be released"
287 ACQUIRED = 3
Daniel Lam6b091c52012-01-22 15:26:27 -0800288 };
289
290 // mBufferState is the current state of this buffer slot.
291 BufferState mBufferState;
292
293 // mRequestBufferCalled is used for validating that the client did
294 // call requestBuffer() when told to do so. Technically this is not
295 // needed but useful for debugging and catching client bugs.
296 bool mRequestBufferCalled;
297
298 // mCrop is the current crop rectangle for this buffer slot. This gets
299 // set to mNextCrop each time queueBuffer gets called for this buffer.
300 Rect mCrop;
301
302 // mTransform is the current transform flags for this buffer slot. This
303 // gets set to mNextTransform each time queueBuffer gets called for this
304 // slot.
305 uint32_t mTransform;
306
307 // mScalingMode is the current scaling mode for this buffer slot. This
308 // gets set to mNextScalingMode each time queueBuffer gets called for
309 // this slot.
310 uint32_t mScalingMode;
311
312 // mTimestamp is the current timestamp for this buffer slot. This gets
313 // to set by queueBuffer each time this slot is queued.
314 int64_t mTimestamp;
315
316 // mFrameNumber is the number of the queued frame for this slot.
317 uint64_t mFrameNumber;
318
319 // mFence is the EGL sync object that must signal before the buffer
320 // associated with this buffer slot may be dequeued. It is initialized
321 // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
322 // on a compile-time option) set to a new sync object in updateTexImage.
323 EGLSyncKHR mFence;
Daniel Lameae59d22012-01-22 15:26:27 -0800324
325 // Indicates whether this buffer has been seen by a consumer yet
326 bool mAcquireCalled;
Daniel Lam6b091c52012-01-22 15:26:27 -0800327 };
328
329 // mSlots is the array of buffer slots that must be mirrored on the client
330 // side. This allows buffer ownership to be transferred between the client
331 // and server without sending a GraphicBuffer over binder. The entire array
332 // is initialized to NULL at construction time, and buffers are allocated
333 // for a slot when requestBuffer is called with that slot's index.
334 BufferSlot mSlots[NUM_BUFFER_SLOTS];
335
Daniel Lam6b091c52012-01-22 15:26:27 -0800336 // mDefaultWidth holds the default width of allocated buffers. It is used
337 // in requestBuffers() if a width and height of zero is specified.
338 uint32_t mDefaultWidth;
339
340 // mDefaultHeight holds the default height of allocated buffers. It is used
341 // in requestBuffers() if a width and height of zero is specified.
342 uint32_t mDefaultHeight;
343
344 // mPixelFormat holds the pixel format of allocated buffers. It is used
345 // in requestBuffers() if a format of zero is specified.
346 uint32_t mPixelFormat;
347
348 // mBufferCount is the number of buffer slots that the client and server
349 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
350 // by calling setBufferCount or setBufferCountServer
351 int mBufferCount;
352
353 // mClientBufferCount is the number of buffer slots requested by the client.
354 // The default is zero, which means the client doesn't care how many buffers
355 // there is.
356 int mClientBufferCount;
357
358 // mServerBufferCount buffer count requested by the server-side
359 int mServerBufferCount;
360
Daniel Lam6b091c52012-01-22 15:26:27 -0800361 // mNextCrop is the crop rectangle that will be used for the next buffer
362 // that gets queued. It is set by calling setCrop.
363 Rect mNextCrop;
364
365 // mNextTransform is the transform identifier that will be used for the next
366 // buffer that gets queued. It is set by calling setTransform.
367 uint32_t mNextTransform;
368
369 // mNextScalingMode is the scaling mode that will be used for the next
370 // buffers that get queued. It is set by calling setScalingMode.
371 int mNextScalingMode;
372
373 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
374 // allocate new GraphicBuffer objects.
375 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
376
377 // mFrameAvailableListener is the listener object that will be called when a
378 // new frame becomes available. If it is not NULL it will be called from
379 // queueBuffer.
380 sp<FrameAvailableListener> mFrameAvailableListener;
381
382 // mSynchronousMode whether we're in synchronous mode or not
383 bool mSynchronousMode;
384
385 // mAllowSynchronousMode whether we allow synchronous mode or not
386 const bool mAllowSynchronousMode;
387
388 // mConnectedApi indicates the API that is currently connected to this
389 // BufferQueue. It defaults to NO_CONNECTED_API (= 0), and gets updated
390 // by the connect and disconnect methods.
391 int mConnectedApi;
392
393 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
394 mutable Condition mDequeueCondition;
395
396 // mQueue is a FIFO of queued buffers used in synchronous mode
397 typedef Vector<int> Fifo;
398 Fifo mQueue;
399
400 // mAbandoned indicates that the BufferQueue will no longer be used to
401 // consume images buffers pushed to it using the ISurfaceTexture interface.
402 // It is initialized to false, and set to true in the abandon method. A
403 // BufferQueue that has been abandoned will return the NO_INIT error from
404 // all ISurfaceTexture methods capable of returning an error.
405 bool mAbandoned;
406
407 // mName is a string used to identify the BufferQueue in log messages.
408 // It is set by the setName method.
Daniel Lameae59d22012-01-22 15:26:27 -0800409 String8 mConsumerName;
Daniel Lam6b091c52012-01-22 15:26:27 -0800410
411 // mMutex is the mutex used to prevent concurrent access to the member
412 // variables of BufferQueue objects. It must be locked whenever the
413 // member variables are accessed.
414 mutable Mutex mMutex;
415
416 // mFrameCounter is the free running counter, incremented for every buffer queued
417 // with the surface Texture.
418 uint64_t mFrameCounter;
Daniel Lameae59d22012-01-22 15:26:27 -0800419
420 bool mBufferHasBeenQueued;
Daniel Lam6b091c52012-01-22 15:26:27 -0800421};
422
423// ----------------------------------------------------------------------------
424}; // namespace android
425
426#endif // ANDROID_GUI_BUFFERQUEUE_H