BufferQueue.h revision fa5b40ebb8923133df12dc70591bfe35b3f1c9b3
1/*
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
22#include <gui/IGraphicBufferAlloc.h>
23#include <gui/ISurfaceTexture.h>
24
25#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 };
43    enum { INVALID_BUFFER_SLOT = -1 };
44
45    // ConsumerListener is the interface through which the BufferQueue notifies
46    // the consumer of events that the consumer may wish to react to.  Because
47    // the consumer will generally have a mutex that is locked during calls from
48    // teh consumer to the BufferQueue, these calls from the BufferQueue to the
49    // consumer *MUST* be called only when the BufferQueue mutex is NOT locked.
50    struct ConsumerListener : public virtual RefBase {
51        // onFrameAvailable is called from queueBuffer each time an additional
52        // frame becomes available for consumption. This means that frames that
53        // are queued while in asynchronous mode only trigger the callback if no
54        // previous frames are pending. Frames queued while in synchronous mode
55        // always trigger the callback.
56        //
57        // This is called without any lock held and can be called concurrently
58        // by multiple threads.
59        virtual void onFrameAvailable() = 0;
60
61        // onBuffersReleased is called to notify the buffer consumer that the
62        // BufferQueue has released its references to one or more GraphicBuffers
63        // contained in its slots.  The buffer consumer should then call
64        // BufferQueue::getReleasedBuffers to retrieve the list of buffers
65        //
66        // This is called without any lock held and can be called concurrently
67        // by multiple threads.
68        virtual void onBuffersReleased() = 0;
69    };
70
71    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
72    // reference to the actual consumer object.  It forwards all calls to that
73    // consumer object so long as it exists.
74    //
75    // This class exists to avoid having a circular reference between the
76    // BufferQueue object and the consumer object.  The reason this can't be a weak
77    // reference in the BufferQueue class is because we're planning to expose the
78    // consumer side of a BufferQueue as a binder interface, which doesn't support
79    // weak references.
80    class ProxyConsumerListener : public BufferQueue::ConsumerListener {
81    public:
82
83        ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener);
84        virtual ~ProxyConsumerListener();
85        virtual void onFrameAvailable();
86        virtual void onBuffersReleased();
87
88    private:
89
90        // mConsumerListener is a weak reference to the ConsumerListener.  This is
91        // the raison d'etre of ProxyConsumerListener.
92        wp<BufferQueue::ConsumerListener> mConsumerListener;
93    };
94
95
96    // BufferQueue manages a pool of gralloc memory slots to be used
97    // by producers and consumers.
98    // allowSynchronousMode specifies whether or not synchronous mode can be
99    // enabled.
100    BufferQueue(bool allowSynchronousMode = true);
101    virtual ~BufferQueue();
102
103    virtual int query(int what, int* value);
104
105    // setBufferCount updates the number of available buffer slots.  After
106    // calling this all buffer slots are both unallocated and owned by the
107    // BufferQueue object (i.e. they are not owned by the client).
108    virtual status_t setBufferCount(int bufferCount);
109
110    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
111
112    // dequeueBuffer gets the next buffer slot index for the client to use. If a
113    // buffer slot is available then that slot index is written to the location
114    // pointed to by the buf argument and a status of OK is returned.  If no
115    // slot is available then a status of -EBUSY is returned and buf is
116    // unmodified.
117    // The width and height parameters must be no greater than the minimum of
118    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
119    // An error due to invalid dimensions might not be reported until
120    // updateTexImage() is called.
121    virtual status_t dequeueBuffer(int *buf, uint32_t width, uint32_t height,
122            uint32_t format, uint32_t usage);
123
124    // queueBuffer returns a filled buffer to the BufferQueue. In addition, a
125    // timestamp must be provided for the buffer. The timestamp is in
126    // nanoseconds, and must be monotonically increasing. Its other semantics
127    // (zero point, etc) are client-dependent and should be documented by the
128    // client.
129    virtual status_t queueBuffer(int buf, int64_t timestamp,
130            uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
131    virtual void cancelBuffer(int buf);
132    virtual status_t setCrop(const Rect& reg);
133    virtual status_t setTransform(uint32_t transform);
134    virtual status_t setScalingMode(int mode);
135
136    // setSynchronousMode set whether dequeueBuffer is synchronous or
137    // asynchronous. In synchronous mode, dequeueBuffer blocks until
138    // a buffer is available, the currently bound buffer can be dequeued and
139    // queued buffers will be retired in order.
140    // The default mode is asynchronous.
141    virtual status_t setSynchronousMode(bool enabled);
142
143    // connect attempts to connect a producer client API to the BufferQueue.
144    // This must be called before any other ISurfaceTexture methods are called
145    // except for getAllocator.
146    //
147    // This method will fail if the connect was previously called on the
148    // BufferQueue and no corresponding disconnect call was made.
149    virtual status_t connect(int api,
150            uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
151
152    // disconnect attempts to disconnect a producer client API from the
153    // BufferQueue. Calling this method will cause any subsequent calls to other
154    // ISurfaceTexture methods to fail except for getAllocator and connect.
155    // Successfully calling connect after this will allow the other methods to
156    // succeed again.
157    //
158    // This method will fail if the the BufferQueue is not currently
159    // connected to the specified client API.
160    virtual status_t disconnect(int api);
161
162    // dump our state in a String
163    virtual void dump(String8& result) const;
164    virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
165
166    // public facing structure for BufferSlot
167    struct BufferItem {
168
169        BufferItem()
170         :
171           mTransform(0),
172           mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
173           mTimestamp(0),
174           mFrameNumber(0),
175           mBuf(INVALID_BUFFER_SLOT) {
176             mCrop.makeInvalid();
177         }
178        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
179        // if no buffer has been allocated.
180        sp<GraphicBuffer> mGraphicBuffer;
181
182        // mCrop is the current crop rectangle for this buffer slot. This gets
183        // set to mNextCrop each time queueBuffer gets called for this buffer.
184        Rect mCrop;
185
186        // mTransform is the current transform flags for this buffer slot. This
187        // gets set to mNextTransform each time queueBuffer gets called for this
188        // slot.
189        uint32_t mTransform;
190
191        // mScalingMode is the current scaling mode for this buffer slot. This
192        // gets set to mNextScalingMode each time queueBuffer gets called for
193        // this slot.
194        uint32_t mScalingMode;
195
196        // mTimestamp is the current timestamp for this buffer slot. This gets
197        // to set by queueBuffer each time this slot is queued.
198        int64_t mTimestamp;
199
200        // mFrameNumber is the number of the queued frame for this slot.
201        uint64_t mFrameNumber;
202
203        // buf is the slot index of this buffer
204        int mBuf;
205
206    };
207
208    // The following public functions is the consumer facing interface
209
210    // acquireBuffer attempts to acquire ownership of the next pending buffer in
211    // the BufferQueue.  If no buffer is pending then it returns -EINVAL.  If a
212    // buffer is successfully acquired, the information about the buffer is
213    // returned in BufferItem.  If the buffer returned had previously been
214    // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
215    // NULL and it is assumed that the consumer still holds a reference to the
216    // buffer.
217    status_t acquireBuffer(BufferItem *buffer);
218
219    // releaseBuffer releases a buffer slot from the consumer back to the
220    // BufferQueue pending a fence sync.
221    //
222    // Note that the dependencies on EGL will be removed once we switch to using
223    // the Android HW Sync HAL.
224    status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence);
225
226    // consumerConnect connects a consumer to the BufferQueue.  Only one
227    // consumer may be connected, and when that consumer disconnects the
228    // BufferQueue is placed into the "abandoned" state, causing most
229    // interactions with the BufferQueue by the producer to fail.
230    status_t consumerConnect(const sp<ConsumerListener>& consumer);
231
232    // consumerDisconnect disconnects a consumer from the BufferQueue. All
233    // buffers will be freed and the BufferQueue is placed in the "abandoned"
234    // state, causing most interactions with the BufferQueue by the producer to
235    // fail.
236    status_t consumerDisconnect();
237
238    // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
239    // indicating which buffer slots the have been released by the BufferQueue
240    // but have not yet been released by the consumer.
241    status_t getReleasedBuffers(uint32_t* slotMask);
242
243    // setDefaultBufferSize is used to set the size of buffers returned by
244    // requestBuffers when a with and height of zero is requested.
245    status_t setDefaultBufferSize(uint32_t w, uint32_t h);
246
247    // setBufferCountServer set the buffer count. If the client has requested
248    // a buffer count using setBufferCount, the server-buffer count will
249    // take effect once the client sets the count back to zero.
250    status_t setBufferCountServer(int bufferCount);
251
252    // isSynchronousMode returns whether the SurfaceTexture is currently in
253    // synchronous mode.
254    bool isSynchronousMode() const;
255
256    // setConsumerName sets the name used in logging
257    void setConsumerName(const String8& name);
258
259    // setDefaultBufferFormat allows the BufferQueue to create
260    // GraphicBuffers of a defaultFormat if no format is specified
261    // in dequeueBuffer
262    status_t setDefaultBufferFormat(uint32_t defaultFormat);
263
264    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
265    status_t setConsumerUsageBits(uint32_t usage);
266
267    // setTransformHint bakes in rotation to buffers so overlays can be used
268    status_t setTransformHint(uint32_t hint);
269
270private:
271    // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
272    // for the given slot.
273    void freeBufferLocked(int index);
274
275    // freeAllBuffersLocked frees the resources (both GraphicBuffer and
276    // EGLImage) for all slots.
277    void freeAllBuffersLocked();
278
279    // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
280    // and EGLImage) for all slots except the head of mQueue
281    void freeAllBuffersExceptHeadLocked();
282
283    // drainQueueLocked drains the buffer queue if we're in synchronous mode
284    // returns immediately otherwise. It returns NO_INIT if the BufferQueue
285    // became abandoned or disconnected during this call.
286    status_t drainQueueLocked();
287
288    // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
289    // synchronous mode and free all buffers. In asynchronous mode, all buffers
290    // are freed except the current buffer.
291    status_t drainQueueAndFreeBuffersLocked();
292
293    status_t setBufferCountServerLocked(int bufferCount);
294
295    struct BufferSlot {
296
297        BufferSlot()
298        : mEglDisplay(EGL_NO_DISPLAY),
299          mBufferState(BufferSlot::FREE),
300          mRequestBufferCalled(false),
301          mTransform(0),
302          mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
303          mTimestamp(0),
304          mFrameNumber(0),
305          mFence(EGL_NO_SYNC_KHR),
306          mAcquireCalled(false) {
307            mCrop.makeInvalid();
308        }
309
310        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
311        // if no buffer has been allocated.
312        sp<GraphicBuffer> mGraphicBuffer;
313
314        // mEglDisplay is the EGLDisplay used to create mEglImage.
315        EGLDisplay mEglDisplay;
316
317        // BufferState represents the different states in which a buffer slot
318        // can be.
319        enum BufferState {
320            // FREE indicates that the buffer is not currently being used and
321            // will not be used in the future until it gets dequeued and
322            // subsequently queued by the client.
323            // aka "owned by BufferQueue, ready to be dequeued"
324            FREE = 0,
325
326            // DEQUEUED indicates that the buffer has been dequeued by the
327            // client, but has not yet been queued or canceled. The buffer is
328            // considered 'owned' by the client, and the server should not use
329            // it for anything.
330            //
331            // Note that when in synchronous-mode (mSynchronousMode == true),
332            // the buffer that's currently attached to the texture may be
333            // dequeued by the client.  That means that the current buffer can
334            // be in either the DEQUEUED or QUEUED state.  In asynchronous mode,
335            // however, the current buffer is always in the QUEUED state.
336            // aka "owned by producer, ready to be queued"
337            DEQUEUED = 1,
338
339            // QUEUED indicates that the buffer has been queued by the client,
340            // and has not since been made available for the client to dequeue.
341            // Attaching the buffer to the texture does NOT transition the
342            // buffer away from the QUEUED state. However, in Synchronous mode
343            // the current buffer may be dequeued by the client under some
344            // circumstances. See the note about the current buffer in the
345            // documentation for DEQUEUED.
346            // aka "owned by BufferQueue, ready to be acquired"
347            QUEUED = 2,
348
349            // aka "owned by consumer, ready to be released"
350            ACQUIRED = 3
351        };
352
353        // mBufferState is the current state of this buffer slot.
354        BufferState mBufferState;
355
356        // mRequestBufferCalled is used for validating that the client did
357        // call requestBuffer() when told to do so. Technically this is not
358        // needed but useful for debugging and catching client bugs.
359        bool mRequestBufferCalled;
360
361        // mCrop is the current crop rectangle for this buffer slot. This gets
362        // set to mNextCrop each time queueBuffer gets called for this buffer.
363        Rect mCrop;
364
365        // mTransform is the current transform flags for this buffer slot. This
366        // gets set to mNextTransform each time queueBuffer gets called for this
367        // slot.
368        uint32_t mTransform;
369
370        // mScalingMode is the current scaling mode for this buffer slot. This
371        // gets set to mNextScalingMode each time queueBuffer gets called for
372        // this slot.
373        uint32_t mScalingMode;
374
375        // mTimestamp is the current timestamp for this buffer slot. This gets
376        // to set by queueBuffer each time this slot is queued.
377        int64_t mTimestamp;
378
379        // mFrameNumber is the number of the queued frame for this slot.
380        uint64_t mFrameNumber;
381
382        // mFence is the EGL sync object that must signal before the buffer
383        // associated with this buffer slot may be dequeued. It is initialized
384        // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
385        // on a compile-time option) set to a new sync object in updateTexImage.
386        EGLSyncKHR mFence;
387
388        // Indicates whether this buffer has been seen by a consumer yet
389        bool mAcquireCalled;
390    };
391
392    // mSlots is the array of buffer slots that must be mirrored on the client
393    // side. This allows buffer ownership to be transferred between the client
394    // and server without sending a GraphicBuffer over binder. The entire array
395    // is initialized to NULL at construction time, and buffers are allocated
396    // for a slot when requestBuffer is called with that slot's index.
397    BufferSlot mSlots[NUM_BUFFER_SLOTS];
398
399    // mDefaultWidth holds the default width of allocated buffers. It is used
400    // in requestBuffers() if a width and height of zero is specified.
401    uint32_t mDefaultWidth;
402
403    // mDefaultHeight holds the default height of allocated buffers. It is used
404    // in requestBuffers() if a width and height of zero is specified.
405    uint32_t mDefaultHeight;
406
407    // mPixelFormat holds the pixel format of allocated buffers. It is used
408    // in requestBuffers() if a format of zero is specified.
409    uint32_t mPixelFormat;
410
411    // mBufferCount is the number of buffer slots that the client and server
412    // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
413    // by calling setBufferCount or setBufferCountServer
414    int mBufferCount;
415
416    // mClientBufferCount is the number of buffer slots requested by the client.
417    // The default is zero, which means the client doesn't care how many buffers
418    // there is.
419    int mClientBufferCount;
420
421    // mServerBufferCount buffer count requested by the server-side
422    int mServerBufferCount;
423
424    // mNextCrop is the crop rectangle that will be used for the next buffer
425    // that gets queued. It is set by calling setCrop.
426    Rect mNextCrop;
427
428    // mNextTransform is the transform identifier that will be used for the next
429    // buffer that gets queued. It is set by calling setTransform.
430    uint32_t mNextTransform;
431
432    // mNextScalingMode is the scaling mode that will be used for the next
433    // buffers that get queued. It is set by calling setScalingMode.
434    int mNextScalingMode;
435
436    // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
437    // allocate new GraphicBuffer objects.
438    sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
439
440    // mConsumerListener is used to notify the connected consumer of
441    // asynchronous events that it may wish to react to.  It is initially set
442    // to NULL and is written by consumerConnect and consumerDisconnect.
443    sp<ConsumerListener> mConsumerListener;
444
445    // mSynchronousMode whether we're in synchronous mode or not
446    bool mSynchronousMode;
447
448    // mAllowSynchronousMode whether we allow synchronous mode or not
449    const bool mAllowSynchronousMode;
450
451    // mConnectedApi indicates the API that is currently connected to this
452    // BufferQueue.  It defaults to NO_CONNECTED_API (= 0), and gets updated
453    // by the connect and disconnect methods.
454    int mConnectedApi;
455
456    // mDequeueCondition condition used for dequeueBuffer in synchronous mode
457    mutable Condition mDequeueCondition;
458
459    // mQueue is a FIFO of queued buffers used in synchronous mode
460    typedef Vector<int> Fifo;
461    Fifo mQueue;
462
463    // mAbandoned indicates that the BufferQueue will no longer be used to
464    // consume images buffers pushed to it using the ISurfaceTexture interface.
465    // It is initialized to false, and set to true in the abandon method.  A
466    // BufferQueue that has been abandoned will return the NO_INIT error from
467    // all ISurfaceTexture methods capable of returning an error.
468    bool mAbandoned;
469
470    // mName is a string used to identify the BufferQueue in log messages.
471    // It is set by the setName method.
472    String8 mConsumerName;
473
474    // mMutex is the mutex used to prevent concurrent access to the member
475    // variables of BufferQueue objects. It must be locked whenever the
476    // member variables are accessed.
477    mutable Mutex mMutex;
478
479    // mFrameCounter is the free running counter, incremented for every buffer queued
480    // with the surface Texture.
481    uint64_t mFrameCounter;
482
483    // mBufferHasBeenQueued is true once a buffer has been queued.  It is reset
484    // by changing the buffer count.
485    bool mBufferHasBeenQueued;
486
487    // mDefaultBufferFormat can be set so it will override
488    // the buffer format when it isn't specified in dequeueBuffer
489    uint32_t mDefaultBufferFormat;
490
491    // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
492    uint32_t mConsumerUsageBits;
493
494    // mTransformHint is used to optimize for screen rotations
495    uint32_t mTransformHint;
496};
497
498// ----------------------------------------------------------------------------
499}; // namespace android
500
501#endif // ANDROID_GUI_BUFFERQUEUE_H
502