BufferQueue.h revision e191e6c34829aec406a9cfe3e95211f884a311ff
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2012 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
9ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See the License for the specific language governing permissions and
149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch * limitations under the License.
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) */
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#ifndef ANDROID_GUI_BUFFERQUEUE_H
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ANDROID_GUI_BUFFERQUEUE_H
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <EGL/egl.h>
21ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include <EGL/eglext.h>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <gui/IGraphicBufferAlloc.h>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <gui/ISurfaceTexture.h>
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ui/Fence.h>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ui/GraphicBuffer.h>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/String8.h>
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/Vector.h>
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/threads.h>
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace android {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------------
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BufferQueue : public BnSurfaceTexture {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum { MIN_UNDEQUEUED_BUFFERS = 2 };
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum { NUM_BUFFER_SLOTS = 32 };
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum { NO_CONNECTED_API = 0 };
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum { INVALID_BUFFER_SLOT = -1 };
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE };
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ConsumerListener is the interface through which the BufferQueue notifies
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the consumer of events that the consumer may wish to react to.  Because
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the consumer will generally have a mutex that is locked during calls from
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // teh consumer to the BufferQueue, these calls from the BufferQueue to the
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // consumer *MUST* be called only when the BufferQueue mutex is NOT locked.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    struct ConsumerListener : public virtual RefBase {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // onFrameAvailable is called from queueBuffer each time an additional
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // frame becomes available for consumption. This means that frames that
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // are queued while in asynchronous mode only trigger the callback if no
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // previous frames are pending. Frames queued while in synchronous mode
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // always trigger the callback.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // This is called without any lock held and can be called concurrently
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // by multiple threads.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        virtual void onFrameAvailable() = 0;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // onBuffersReleased is called to notify the buffer consumer that the
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // BufferQueue has released its references to one or more GraphicBuffers
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // contained in its slots.  The buffer consumer should then call
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // BufferQueue::getReleasedBuffers to retrieve the list of buffers
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        //
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // This is called without any lock held and can be called concurrently
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // by multiple threads.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        virtual void onBuffersReleased() = 0;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // reference to the actual consumer object.  It forwards all calls to that
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // consumer object so long as it exists.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This class exists to avoid having a circular reference between the
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue object and the consumer object.  The reason this can't be a weak
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // reference in the BufferQueue class is because we're planning to expose the
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // consumer side of a BufferQueue as a binder interface, which doesn't support
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // weak references.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    class ProxyConsumerListener : public BufferQueue::ConsumerListener {
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public:
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        virtual ~ProxyConsumerListener();
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        virtual void onFrameAvailable();
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        virtual void onBuffersReleased();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private:
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mConsumerListener is a weak reference to the ConsumerListener.  This is
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // the raison d'etre of ProxyConsumerListener.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        wp<BufferQueue::ConsumerListener> mConsumerListener;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue manages a pool of gralloc memory slots to be used
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // by producers and consumers.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // allowSynchronousMode specifies whether or not synchronous mode can be
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // enabled.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // bufferCount sets the minimum number of undequeued buffers for this queue
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BufferQueue(bool allowSynchronousMode = true,
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            int bufferCount = MIN_UNDEQUEUED_BUFFERS,
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            const sp<IGraphicBufferAlloc>& allocator = NULL);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~BufferQueue();
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual int query(int what, int* value);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setBufferCount updates the number of available buffer slots.  After
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // calling this all buffer slots are both unallocated and owned by the
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue object (i.e. they are not owned by the client).
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t setBufferCount(int bufferCount);
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // dequeueBuffer gets the next buffer slot index for the client to use. If a
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // buffer slot is available then that slot index is written to the location
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // pointed to by the buf argument and a status of OK is returned.  If no
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // slot is available then a status of -EBUSY is returned and buf is
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // unmodified.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The fence parameter will be updated to hold the fence associated with
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the buffer. The contents of the buffer must not be overwritten until the
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // fence signals. If the fence is NULL, the buffer may be written
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // immediately.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The width and height parameters must be no greater than the minimum of
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // An error due to invalid dimensions might not be reported until
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // updateTexImage() is called.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence,
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // queueBuffer returns a filled buffer to the BufferQueue. In addition, a
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // timestamp must be provided for the buffer. The timestamp is in
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // nanoseconds, and must be monotonically increasing. Its other semantics
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // (zero point, etc) are client-dependent and should be documented by the
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // client.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t queueBuffer(int buf,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            const QueueBufferInput& input, QueueBufferOutput* output);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void cancelBuffer(int buf, sp<Fence> fence);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setSynchronousMode set whether dequeueBuffer is synchronous or
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // asynchronous. In synchronous mode, dequeueBuffer blocks until
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // a buffer is available, the currently bound buffer can be dequeued and
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // queued buffers will be retired in order.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The default mode is asynchronous.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t setSynchronousMode(bool enabled);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // connect attempts to connect a producer client API to the BufferQueue.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This must be called before any other ISurfaceTexture methods are called
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // except for getAllocator.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This method will fail if the connect was previously called on the
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue and no corresponding disconnect call was made.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t connect(int api, QueueBufferOutput* output);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // disconnect attempts to disconnect a producer client API from the
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue. Calling this method will cause any subsequent calls to other
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ISurfaceTexture methods to fail except for getAllocator and connect.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Successfully calling connect after this will allow the other methods to
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // succeed again.
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This method will fail if the the BufferQueue is not currently
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // connected to the specified client API.
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual status_t disconnect(int api);
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // dump our state in a String
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void dump(String8& result) const;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // public facing structure for BufferSlot
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    struct BufferItem {
173b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        BufferItem()
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         :
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           mTransform(0),
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           mTimestamp(0),
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           mFrameNumber(0),
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           mBuf(INVALID_BUFFER_SLOT) {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             mCrop.makeInvalid();
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // if no buffer has been allocated.
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        sp<GraphicBuffer> mGraphicBuffer;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mCrop is the current crop rectangle for this buffer slot.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Rect mCrop;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mTransform is the current transform flags for this buffer slot.
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        uint32_t mTransform;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mScalingMode is the current scaling mode for this buffer slot.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        uint32_t mScalingMode;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mTimestamp is the current timestamp for this buffer slot. This gets
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // to set by queueBuffer each time this slot is queued.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        int64_t mTimestamp;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mFrameNumber is the number of the queued frame for this slot.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        uint64_t mFrameNumber;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mBuf is the slot index of this buffer
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        int mBuf;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // mFence is a fence that will signal when the buffer is idle.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        sp<Fence> mFence;
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The following public functions is the consumer facing interface
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // acquireBuffer attempts to acquire ownership of the next pending buffer in
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the BufferQueue.  If no buffer is pending then it returns -EINVAL.  If a
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // buffer is successfully acquired, the information about the buffer is
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // returned in BufferItem.  If the buffer returned had previously been
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // NULL and it is assumed that the consumer still holds a reference to the
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // buffer.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t acquireBuffer(BufferItem *buffer);
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // releaseBuffer releases a buffer slot from the consumer back to the
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue pending a fence sync.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // any references to the just-released buffer that it might have, as if it
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // had received a onBuffersReleased() call with a mask set for the released
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // buffer.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Note that the dependencies on EGL will be removed once we switch to using
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the Android HW Sync HAL.
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence,
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            const sp<Fence>& releaseFence);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // consumerConnect connects a consumer to the BufferQueue.  Only one
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // consumer may be connected, and when that consumer disconnects the
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // BufferQueue is placed into the "abandoned" state, causing most
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // interactions with the BufferQueue by the producer to fail.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t consumerConnect(const sp<ConsumerListener>& consumer);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // consumerDisconnect disconnects a consumer from the BufferQueue. All
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // buffers will be freed and the BufferQueue is placed in the "abandoned"
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // state, causing most interactions with the BufferQueue by the producer to
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // fail.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t consumerDisconnect();
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // indicating which buffer slots the have been released by the BufferQueue
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // but have not yet been released by the consumer.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t getReleasedBuffers(uint32_t* slotMask);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setDefaultBufferSize is used to set the size of buffers returned by
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // requestBuffers when a with and height of zero is requested.
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t setDefaultBufferSize(uint32_t w, uint32_t h);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setDefaultBufferCount set the buffer count. If the client has requested
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // a buffer count using setBufferCount, the server-buffer count will
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // take effect once the client sets the count back to zero.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t setDefaultMaxBufferCount(int bufferCount);
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // isSynchronousMode returns whether the SurfaceTexture is currently in
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // synchronous mode.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isSynchronousMode() const;
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setConsumerName sets the name used in logging
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void setConsumerName(const String8& name);
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setDefaultBufferFormat allows the BufferQueue to create
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // GraphicBuffers of a defaultFormat if no format is specified
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // in dequeueBuffer
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t setDefaultBufferFormat(uint32_t defaultFormat);
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t setConsumerUsageBits(uint32_t usage);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // setTransformHint bakes in rotation to buffers so overlays can be used
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t setTransformHint(uint32_t hint);
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)private:
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // for the given slot.
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void freeBufferLocked(int index);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // freeAllBuffersLocked frees the resources (both GraphicBuffer and
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // EGLImage) for all slots.
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void freeAllBuffersLocked();
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
287ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
288    // and EGLImage) for all slots except the head of mQueue
289    void freeAllBuffersExceptHeadLocked();
290
291    // drainQueueLocked drains the buffer queue if we're in synchronous mode
292    // returns immediately otherwise. It returns NO_INIT if the BufferQueue
293    // became abandoned or disconnected during this call.
294    status_t drainQueueLocked();
295
296    // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
297    // synchronous mode and free all buffers. In asynchronous mode, all buffers
298    // are freed except the current buffer.
299    status_t drainQueueAndFreeBuffersLocked();
300
301    // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
302    // that will be used if the producer does not override the buffer slot
303    // count.
304    status_t setDefaultMaxBufferCountLocked(int count);
305
306    // getMinBufferCountLocked returns the minimum number of buffers allowed
307    // given the current BufferQueue state.
308    int getMinMaxBufferCountLocked() const;
309
310    // getMaxBufferCountLocked returns the maximum number of buffers that can
311    // be allocated at once.  This value depends upon the following member
312    // variables:
313    //
314    //      mSynchronousMode
315    //      mMinUndequeuedBuffers
316    //      mDefaultMaxBufferCount
317    //      mOverrideMaxBufferCount
318    //
319    // Any time one of these member variables is changed while a producer is
320    // connected, mDequeueCondition must be broadcast.
321    int getMaxBufferCountLocked() const;
322
323    struct BufferSlot {
324
325        BufferSlot()
326        : mEglDisplay(EGL_NO_DISPLAY),
327          mBufferState(BufferSlot::FREE),
328          mRequestBufferCalled(false),
329          mTransform(0),
330          mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
331          mTimestamp(0),
332          mFrameNumber(0),
333          mEglFence(EGL_NO_SYNC_KHR),
334          mAcquireCalled(false),
335          mNeedsCleanupOnRelease(false) {
336            mCrop.makeInvalid();
337        }
338
339        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
340        // if no buffer has been allocated.
341        sp<GraphicBuffer> mGraphicBuffer;
342
343        // mEglDisplay is the EGLDisplay used to create mEglImage.
344        EGLDisplay mEglDisplay;
345
346        // BufferState represents the different states in which a buffer slot
347        // can be.
348        enum BufferState {
349            // FREE indicates that the buffer is not currently being used and
350            // will not be used in the future until it gets dequeued and
351            // subsequently queued by the client.
352            // aka "owned by BufferQueue, ready to be dequeued"
353            FREE = 0,
354
355            // DEQUEUED indicates that the buffer has been dequeued by the
356            // client, but has not yet been queued or canceled. The buffer is
357            // considered 'owned' by the client, and the server should not use
358            // it for anything.
359            //
360            // Note that when in synchronous-mode (mSynchronousMode == true),
361            // the buffer that's currently attached to the texture may be
362            // dequeued by the client.  That means that the current buffer can
363            // be in either the DEQUEUED or QUEUED state.  In asynchronous mode,
364            // however, the current buffer is always in the QUEUED state.
365            // aka "owned by producer, ready to be queued"
366            DEQUEUED = 1,
367
368            // QUEUED indicates that the buffer has been queued by the client,
369            // and has not since been made available for the client to dequeue.
370            // Attaching the buffer to the texture does NOT transition the
371            // buffer away from the QUEUED state. However, in Synchronous mode
372            // the current buffer may be dequeued by the client under some
373            // circumstances. See the note about the current buffer in the
374            // documentation for DEQUEUED.
375            // aka "owned by BufferQueue, ready to be acquired"
376            QUEUED = 2,
377
378            // aka "owned by consumer, ready to be released"
379            ACQUIRED = 3
380        };
381
382        // mBufferState is the current state of this buffer slot.
383        BufferState mBufferState;
384
385        // mRequestBufferCalled is used for validating that the client did
386        // call requestBuffer() when told to do so. Technically this is not
387        // needed but useful for debugging and catching client bugs.
388        bool mRequestBufferCalled;
389
390        // mCrop is the current crop rectangle for this buffer slot.
391        Rect mCrop;
392
393        // mTransform is the current transform flags for this buffer slot.
394        uint32_t mTransform;
395
396        // mScalingMode is the current scaling mode for this buffer slot.
397        uint32_t mScalingMode;
398
399        // mTimestamp is the current timestamp for this buffer slot. This gets
400        // to set by queueBuffer each time this slot is queued.
401        int64_t mTimestamp;
402
403        // mFrameNumber is the number of the queued frame for this slot.
404        uint64_t mFrameNumber;
405
406        // mEglFence is the EGL sync object that must signal before the buffer
407        // associated with this buffer slot may be dequeued. It is initialized
408        // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
409        // on a compile-time option) set to a new sync object in updateTexImage.
410        EGLSyncKHR mEglFence;
411
412        // mFence is a fence which will signal when work initiated by the
413        // previous owner of the buffer is finished. When the buffer is FREE,
414        // the fence indicates when the consumer has finished reading
415        // from the buffer, or when the producer has finished writing if it
416        // called cancelBuffer after queueing some writes. When the buffer is
417        // QUEUED, it indicates when the producer has finished filling the
418        // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
419        // passed to the consumer or producer along with ownership of the
420        // buffer, and mFence is empty.
421        sp<Fence> mFence;
422
423        // Indicates whether this buffer has been seen by a consumer yet
424        bool mAcquireCalled;
425
426        // Indicates whether this buffer needs to be cleaned up by consumer
427        bool mNeedsCleanupOnRelease;
428    };
429
430    // mSlots is the array of buffer slots that must be mirrored on the client
431    // side. This allows buffer ownership to be transferred between the client
432    // and server without sending a GraphicBuffer over binder. The entire array
433    // is initialized to NULL at construction time, and buffers are allocated
434    // for a slot when requestBuffer is called with that slot's index.
435    BufferSlot mSlots[NUM_BUFFER_SLOTS];
436
437    // mDefaultWidth holds the default width of allocated buffers. It is used
438    // in requestBuffers() if a width and height of zero is specified.
439    uint32_t mDefaultWidth;
440
441    // mDefaultHeight holds the default height of allocated buffers. It is used
442    // in requestBuffers() if a width and height of zero is specified.
443    uint32_t mDefaultHeight;
444
445    // mMinUndequeuedBuffers is a constraint on the number of buffers
446    // not dequeued at any time
447    int mMinUndequeuedBuffers;
448
449    // mDefaultMaxBufferCount is the default limit on the number of buffers
450    // that will be allocated at one time.  This default limit is set by the
451    // consumer.  The limit (as opposed to the default limit) may be
452    // overridden by the producer.
453    int mDefaultMaxBufferCount;
454
455    // mOverrideMaxBufferCount is the limit on the number of buffers that will
456    // be allocated at one time. This value is set by the image producer by
457    // calling setBufferCount. The default is zero, which means the producer
458    // doesn't care about the number of buffers in the pool. In that case
459    // mDefaultMaxBufferCount is used as the limit.
460    int mOverrideMaxBufferCount;
461
462    // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
463    // allocate new GraphicBuffer objects.
464    sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
465
466    // mConsumerListener is used to notify the connected consumer of
467    // asynchronous events that it may wish to react to.  It is initially set
468    // to NULL and is written by consumerConnect and consumerDisconnect.
469    sp<ConsumerListener> mConsumerListener;
470
471    // mSynchronousMode whether we're in synchronous mode or not
472    bool mSynchronousMode;
473
474    // mAllowSynchronousMode whether we allow synchronous mode or not
475    const bool mAllowSynchronousMode;
476
477    // mConnectedApi indicates the API that is currently connected to this
478    // BufferQueue.  It defaults to NO_CONNECTED_API (= 0), and gets updated
479    // by the connect and disconnect methods.
480    int mConnectedApi;
481
482    // mDequeueCondition condition used for dequeueBuffer in synchronous mode
483    mutable Condition mDequeueCondition;
484
485    // mQueue is a FIFO of queued buffers used in synchronous mode
486    typedef Vector<int> Fifo;
487    Fifo mQueue;
488
489    // mAbandoned indicates that the BufferQueue will no longer be used to
490    // consume images buffers pushed to it using the ISurfaceTexture interface.
491    // It is initialized to false, and set to true in the abandon method.  A
492    // BufferQueue that has been abandoned will return the NO_INIT error from
493    // all ISurfaceTexture methods capable of returning an error.
494    bool mAbandoned;
495
496    // mName is a string used to identify the BufferQueue in log messages.
497    // It is set by the setName method.
498    String8 mConsumerName;
499
500    // mMutex is the mutex used to prevent concurrent access to the member
501    // variables of BufferQueue objects. It must be locked whenever the
502    // member variables are accessed.
503    mutable Mutex mMutex;
504
505    // mFrameCounter is the free running counter, incremented for every buffer queued
506    // with the surface Texture.
507    uint64_t mFrameCounter;
508
509    // mBufferHasBeenQueued is true once a buffer has been queued.  It is reset
510    // by changing the buffer count.
511    bool mBufferHasBeenQueued;
512
513    // mDefaultBufferFormat can be set so it will override
514    // the buffer format when it isn't specified in dequeueBuffer
515    uint32_t mDefaultBufferFormat;
516
517    // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
518    uint32_t mConsumerUsageBits;
519
520    // mTransformHint is used to optimize for screen rotations
521    uint32_t mTransformHint;
522};
523
524// ----------------------------------------------------------------------------
525}; // namespace android
526
527#endif // ANDROID_GUI_BUFFERQUEUE_H
528