16b091c53000c843211c218ce40287a7edca9bc63Daniel Lam/*
26b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Copyright (C) 2012 The Android Open Source Project
36b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
46b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Licensed under the Apache License, Version 2.0 (the "License");
56b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * you may not use this file except in compliance with the License.
66b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * You may obtain a copy of the License at
76b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
86b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *      http://www.apache.org/licenses/LICENSE-2.0
96b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Unless required by applicable law or agreed to in writing, software
116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * distributed under the License is distributed on an "AS IS" BASIS,
126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * See the License for the specific language governing permissions and
146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * limitations under the License.
156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam */
166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#ifndef ANDROID_GUI_BUFFERQUEUE_H
186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define ANDROID_GUI_BUFFERQUEUE_H
196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <EGL/egl.h>
21f71c4ae136f7749b9dfdaa2dd64d771868eeeb2dDaniel Lam#include <EGL/eglext.h>
226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2390ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/IGraphicBufferAlloc.h>
246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <gui/ISurfaceTexture.h>
256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
26ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall#include <ui/Fence.h>
276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <ui/GraphicBuffer.h>
286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/String8.h>
306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/Vector.h>
316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/threads.h>
326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
336b091c53000c843211c218ce40287a7edca9bc63Daniel Lamnamespace android {
346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// ----------------------------------------------------------------------------
356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
366b091c53000c843211c218ce40287a7edca9bc63Daniel Lamclass BufferQueue : public BnSurfaceTexture {
376b091c53000c843211c218ce40287a7edca9bc63Daniel Lampublic:
386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    enum { MIN_UNDEQUEUED_BUFFERS = 2 };
396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    enum { NUM_BUFFER_SLOTS = 32 };
406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    enum { NO_CONNECTED_API = 0 };
41eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    enum { INVALID_BUFFER_SLOT = -1 };
42fbcda930dd8b2823cfeb160fd0131f5897b7522fDaniel Lam    enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE };
436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
44c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    // When in async mode we reserve two slots in order to guarantee that the
45c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    // producer and consumer can run asynchronously.
46c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
47c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis
48fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // ConsumerListener is the interface through which the BufferQueue notifies
49fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // the consumer of events that the consumer may wish to react to.  Because
50fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // the consumer will generally have a mutex that is locked during calls from
51fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // teh consumer to the BufferQueue, these calls from the BufferQueue to the
52fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer *MUST* be called only when the BufferQueue mutex is NOT locked.
53fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    struct ConsumerListener : public virtual RefBase {
54fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // onFrameAvailable is called from queueBuffer each time an additional
55fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // frame becomes available for consumption. This means that frames that
56fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // are queued while in asynchronous mode only trigger the callback if no
57fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // previous frames are pending. Frames queued while in synchronous mode
58fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // always trigger the callback.
596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        //
606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // This is called without any lock held and can be called concurrently
616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // by multiple threads.
626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        virtual void onFrameAvailable() = 0;
63fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
64fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // onBuffersReleased is called to notify the buffer consumer that the
65fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // BufferQueue has released its references to one or more GraphicBuffers
66fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // contained in its slots.  The buffer consumer should then call
67fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // BufferQueue::getReleasedBuffers to retrieve the list of buffers
68fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        //
69fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // This is called without any lock held and can be called concurrently
70fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // by multiple threads.
71fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual void onBuffersReleased() = 0;
72fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    };
73fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
74fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
75fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // reference to the actual consumer object.  It forwards all calls to that
76fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer object so long as it exists.
77fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
78fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // This class exists to avoid having a circular reference between the
79fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // BufferQueue object and the consumer object.  The reason this can't be a weak
80fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // reference in the BufferQueue class is because we're planning to expose the
81fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer side of a BufferQueue as a binder interface, which doesn't support
82fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // weak references.
83fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    class ProxyConsumerListener : public BufferQueue::ConsumerListener {
84fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    public:
85fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
86fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener);
87fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual ~ProxyConsumerListener();
88fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual void onFrameAvailable();
89fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual void onBuffersReleased();
90fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
91fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    private:
92fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
93fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // mConsumerListener is a weak reference to the ConsumerListener.  This is
94fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // the raison d'etre of ProxyConsumerListener.
95fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        wp<BufferQueue::ConsumerListener> mConsumerListener;
966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    };
976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
98fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
9972f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // BufferQueue manages a pool of gralloc memory slots to be used by
10072f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // producers and consumers. allowSynchronousMode specifies whether or not
10172f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // synchronous mode can be enabled by the producer. allocator is used to
10272f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // allocate all the needed gralloc buffers.
1033e87601170141229d661df93e2f59e1ced73474bMathias Agopian    BufferQueue(bool allowSynchronousMode = true,
1043e87601170141229d661df93e2f59e1ced73474bMathias Agopian            const sp<IGraphicBufferAlloc>& allocator = NULL);
1056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual ~BufferQueue();
1066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
107b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    virtual int query(int what, int* value);
108b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
1096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // setBufferCount updates the number of available buffer slots.  After
1106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // calling this all buffer slots are both unallocated and owned by the
1116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // BufferQueue object (i.e. they are not owned by the client).
1126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t setBufferCount(int bufferCount);
1136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
1156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // dequeueBuffer gets the next buffer slot index for the client to use. If a
1176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // buffer slot is available then that slot index is written to the location
1186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // pointed to by the buf argument and a status of OK is returned.  If no
1196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // slot is available then a status of -EBUSY is returned and buf is
1206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // unmodified.
121f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    //
122f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    // The fence parameter will be updated to hold the fence associated with
123f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    // the buffer. The contents of the buffer must not be overwritten until the
124f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    // fence signals. If the fence is NULL, the buffer may be written
125f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    // immediately.
126f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    //
1276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // The width and height parameters must be no greater than the minimum of
1286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
1296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // An error due to invalid dimensions might not be reported until
1306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // updateTexImage() is called.
131f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence,
132f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
1336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // queueBuffer returns a filled buffer to the BufferQueue. In addition, a
1356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // timestamp must be provided for the buffer. The timestamp is in
1366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // nanoseconds, and must be monotonically increasing. Its other semantics
1376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // (zero point, etc) are client-dependent and should be documented by the
1386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // client.
139f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian    virtual status_t queueBuffer(int buf,
140f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian            const QueueBufferInput& input, QueueBufferOutput* output);
141f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian
142c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    virtual void cancelBuffer(int buf, sp<Fence> fence);
1436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // setSynchronousMode set whether dequeueBuffer is synchronous or
1456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // asynchronous. In synchronous mode, dequeueBuffer blocks until
1466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // a buffer is available, the currently bound buffer can be dequeued and
1476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // queued buffers will be retired in order.
1486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // The default mode is asynchronous.
1496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t setSynchronousMode(bool enabled);
1506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // connect attempts to connect a producer client API to the BufferQueue.
1526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // This must be called before any other ISurfaceTexture methods are called
1536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // except for getAllocator.
1546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    //
1556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // This method will fail if the connect was previously called on the
1566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // BufferQueue and no corresponding disconnect call was made.
15724202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian    virtual status_t connect(int api, QueueBufferOutput* output);
1586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // disconnect attempts to disconnect a producer client API from the
1606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // BufferQueue. Calling this method will cause any subsequent calls to other
1616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // ISurfaceTexture methods to fail except for getAllocator and connect.
1626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // Successfully calling connect after this will allow the other methods to
1636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // succeed again.
1646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    //
1656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // This method will fail if the the BufferQueue is not currently
1666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // connected to the specified client API.
1676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t disconnect(int api);
1686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
169eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // dump our state in a String
170eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    virtual void dump(String8& result) const;
171eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
172eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
173eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // public facing structure for BufferSlot
174eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    struct BufferItem {
175eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
176eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        BufferItem()
177eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam         :
178eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam           mTransform(0),
179eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam           mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
180eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam           mTimestamp(0),
181eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam           mFrameNumber(0),
182eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam           mBuf(INVALID_BUFFER_SLOT) {
183eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam             mCrop.makeInvalid();
184eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam         }
185eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
186eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // if no buffer has been allocated.
187eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        sp<GraphicBuffer> mGraphicBuffer;
188eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
189851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        // mCrop is the current crop rectangle for this buffer slot.
190eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        Rect mCrop;
191eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
192851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        // mTransform is the current transform flags for this buffer slot.
193eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        uint32_t mTransform;
194eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
195851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        // mScalingMode is the current scaling mode for this buffer slot.
196eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        uint32_t mScalingMode;
197eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
198eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mTimestamp is the current timestamp for this buffer slot. This gets
199eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // to set by queueBuffer each time this slot is queued.
200eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        int64_t mTimestamp;
2016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
202eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mFrameNumber is the number of the queued frame for this slot.
203eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        uint64_t mFrameNumber;
204eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
205efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis        // mBuf is the slot index of this buffer
206eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        int mBuf;
207b42b1ac1587aebda5e2f334d95b620271fafba4eJesse Hall
208b42b1ac1587aebda5e2f334d95b620271fafba4eJesse Hall        // mFence is a fence that will signal when the buffer is idle.
209b42b1ac1587aebda5e2f334d95b620271fafba4eJesse Hall        sp<Fence> mFence;
210eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    };
211eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
212eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // The following public functions is the consumer facing interface
213eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
214fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // acquireBuffer attempts to acquire ownership of the next pending buffer in
215fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // the BufferQueue.  If no buffer is pending then it returns -EINVAL.  If a
216fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // buffer is successfully acquired, the information about the buffer is
217fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // returned in BufferItem.  If the buffer returned had previously been
218fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
219fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // NULL and it is assumed that the consumer still holds a reference to the
220fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // buffer.
221fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    status_t acquireBuffer(BufferItem *buffer);
222eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
223eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // releaseBuffer releases a buffer slot from the consumer back to the
224eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // BufferQueue pending a fence sync.
225fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
226e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
227e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // any references to the just-released buffer that it might have, as if it
228e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // had received a onBuffersReleased() call with a mask set for the released
229e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // buffer.
230e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    //
231fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // Note that the dependencies on EGL will be removed once we switch to using
232fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // the Android HW Sync HAL.
233ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence,
234f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            const sp<Fence>& releaseFence);
235eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
236fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumerConnect connects a consumer to the BufferQueue.  Only one
237fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer may be connected, and when that consumer disconnects the
238fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // BufferQueue is placed into the "abandoned" state, causing most
239fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // interactions with the BufferQueue by the producer to fail.
240fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    status_t consumerConnect(const sp<ConsumerListener>& consumer);
241fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
242eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // consumerDisconnect disconnects a consumer from the BufferQueue. All
243fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // buffers will be freed and the BufferQueue is placed in the "abandoned"
244fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // state, causing most interactions with the BufferQueue by the producer to
245fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // fail.
246eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    status_t consumerDisconnect();
247eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
248fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
249fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // indicating which buffer slots the have been released by the BufferQueue
250fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // but have not yet been released by the consumer.
251fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    status_t getReleasedBuffers(uint32_t* slotMask);
252fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
253eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // setDefaultBufferSize is used to set the size of buffers returned by
254eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // requestBuffers when a with and height of zero is requested.
255eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    status_t setDefaultBufferSize(uint32_t w, uint32_t h);
256eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
25731a353da225af5329735451c761b430d82dfda1bJamie Gennis    // setDefaultBufferCount set the buffer count. If the client has requested
258eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // a buffer count using setBufferCount, the server-buffer count will
259eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // take effect once the client sets the count back to zero.
26031a353da225af5329735451c761b430d82dfda1bJamie Gennis    status_t setDefaultMaxBufferCount(int bufferCount);
261eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
26272f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
26372f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // be acquired by the consumer at one time.  This call will fail if a
26472f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // producer is connected to the BufferQueue.
26572f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
26672f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis
267eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // isSynchronousMode returns whether the SurfaceTexture is currently in
268eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // synchronous mode.
269eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    bool isSynchronousMode() const;
270eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
271eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // setConsumerName sets the name used in logging
272eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    void setConsumerName(const String8& name);
273eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
274b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // setDefaultBufferFormat allows the BufferQueue to create
275b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // GraphicBuffers of a defaultFormat if no format is specified
276b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // in dequeueBuffer
277b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setDefaultBufferFormat(uint32_t defaultFormat);
278b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
279b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
280b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setConsumerUsageBits(uint32_t usage);
281b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
282b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // setTransformHint bakes in rotation to buffers so overlays can be used
283b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setTransformHint(uint32_t hint);
284eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
285eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamprivate:
2866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
2876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // for the given slot.
2886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    void freeBufferLocked(int index);
2896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // freeAllBuffersLocked frees the resources (both GraphicBuffer and
2916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // EGLImage) for all slots.
2926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    void freeAllBuffersLocked();
2936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
2956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // and EGLImage) for all slots except the head of mQueue
2966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    void freeAllBuffersExceptHeadLocked();
2976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // drainQueueLocked drains the buffer queue if we're in synchronous mode
2996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // returns immediately otherwise. It returns NO_INIT if the BufferQueue
3006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // became abandoned or disconnected during this call.
3016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t drainQueueLocked();
3026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
3046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // synchronous mode and free all buffers. In asynchronous mode, all buffers
3056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // are freed except the current buffer.
3066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t drainQueueAndFreeBuffersLocked();
3076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
30831a353da225af5329735451c761b430d82dfda1bJamie Gennis    // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
30931a353da225af5329735451c761b430d82dfda1bJamie Gennis    // that will be used if the producer does not override the buffer slot
31031a353da225af5329735451c761b430d82dfda1bJamie Gennis    // count.
31131a353da225af5329735451c761b430d82dfda1bJamie Gennis    status_t setDefaultMaxBufferCountLocked(int count);
31231a353da225af5329735451c761b430d82dfda1bJamie Gennis
31331a353da225af5329735451c761b430d82dfda1bJamie Gennis    // getMinBufferCountLocked returns the minimum number of buffers allowed
31431a353da225af5329735451c761b430d82dfda1bJamie Gennis    // given the current BufferQueue state.
31531a353da225af5329735451c761b430d82dfda1bJamie Gennis    int getMinMaxBufferCountLocked() const;
3166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
31772f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
31872f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // that must remain in a state other than DEQUEUED.
31972f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    int getMinUndequeuedBufferCountLocked() const;
32072f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis
321e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // getMaxBufferCountLocked returns the maximum number of buffers that can
322e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // be allocated at once.  This value depends upon the following member
323e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // variables:
324e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //
325e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //      mSynchronousMode
32672f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    //      mMaxAcquiredBufferCount
327e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //      mDefaultMaxBufferCount
328e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //      mOverrideMaxBufferCount
329e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //
330e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // Any time one of these member variables is changed while a producer is
331e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // connected, mDequeueCondition must be broadcast.
332e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    int getMaxBufferCountLocked() const;
333e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis
3346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    struct BufferSlot {
3356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        BufferSlot()
337eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        : mEglDisplay(EGL_NO_DISPLAY),
3386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mBufferState(BufferSlot::FREE),
3396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mRequestBufferCalled(false),
3406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mTransform(0),
3416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
3426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mTimestamp(0),
3436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mFrameNumber(0),
344c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall          mEglFence(EGL_NO_SYNC_KHR),
3459abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam          mAcquireCalled(false),
3469abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam          mNeedsCleanupOnRelease(false) {
3476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mCrop.makeInvalid();
3486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
3496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
3516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // if no buffer has been allocated.
3526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        sp<GraphicBuffer> mGraphicBuffer;
3536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mEglDisplay is the EGLDisplay used to create mEglImage.
3556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        EGLDisplay mEglDisplay;
3566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // BufferState represents the different states in which a buffer slot
3586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // can be.
3596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        enum BufferState {
3606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // FREE indicates that the buffer is not currently being used and
3616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // will not be used in the future until it gets dequeued and
3626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // subsequently queued by the client.
363eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            // aka "owned by BufferQueue, ready to be dequeued"
3646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            FREE = 0,
3656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // DEQUEUED indicates that the buffer has been dequeued by the
3676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // client, but has not yet been queued or canceled. The buffer is
3686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // considered 'owned' by the client, and the server should not use
3696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // it for anything.
3706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
3716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // Note that when in synchronous-mode (mSynchronousMode == true),
3726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // the buffer that's currently attached to the texture may be
3736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // dequeued by the client.  That means that the current buffer can
3746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // be in either the DEQUEUED or QUEUED state.  In asynchronous mode,
3756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // however, the current buffer is always in the QUEUED state.
376eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            // aka "owned by producer, ready to be queued"
3776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            DEQUEUED = 1,
3786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // QUEUED indicates that the buffer has been queued by the client,
3806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // and has not since been made available for the client to dequeue.
3816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // Attaching the buffer to the texture does NOT transition the
3826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // buffer away from the QUEUED state. However, in Synchronous mode
3836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // the current buffer may be dequeued by the client under some
3846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // circumstances. See the note about the current buffer in the
3856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // documentation for DEQUEUED.
386eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            // aka "owned by BufferQueue, ready to be acquired"
3876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            QUEUED = 2,
388eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
389eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            // aka "owned by consumer, ready to be released"
390eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            ACQUIRED = 3
3916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        };
3926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mBufferState is the current state of this buffer slot.
3946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        BufferState mBufferState;
3956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mRequestBufferCalled is used for validating that the client did
3976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // call requestBuffer() when told to do so. Technically this is not
3986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // needed but useful for debugging and catching client bugs.
3996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bool mRequestBufferCalled;
4006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
401851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        // mCrop is the current crop rectangle for this buffer slot.
4026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Rect mCrop;
4036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
404851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        // mTransform is the current transform flags for this buffer slot.
4056905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden        // (example: NATIVE_WINDOW_TRANSFORM_ROT_90)
4066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t mTransform;
4076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
408851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        // mScalingMode is the current scaling mode for this buffer slot.
4096905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden        // (example: NATIVE_WINDOW_SCALING_MODE_FREEZE)
4106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t mScalingMode;
4116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mTimestamp is the current timestamp for this buffer slot. This gets
4136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // to set by queueBuffer each time this slot is queued.
4146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int64_t mTimestamp;
4156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mFrameNumber is the number of the queued frame for this slot.
4176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint64_t mFrameNumber;
4186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
419c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // mEglFence is the EGL sync object that must signal before the buffer
4206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // associated with this buffer slot may be dequeued. It is initialized
4216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
4226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // on a compile-time option) set to a new sync object in updateTexImage.
423c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        EGLSyncKHR mEglFence;
424c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
425c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // mFence is a fence which will signal when work initiated by the
426c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // previous owner of the buffer is finished. When the buffer is FREE,
427c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // the fence indicates when the consumer has finished reading
428c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // from the buffer, or when the producer has finished writing if it
429c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // called cancelBuffer after queueing some writes. When the buffer is
430c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // QUEUED, it indicates when the producer has finished filling the
431c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
432c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // passed to the consumer or producer along with ownership of the
433c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // buffer, and mFence is empty.
434c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        sp<Fence> mFence;
435ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall
436eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // Indicates whether this buffer has been seen by a consumer yet
437eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        bool mAcquireCalled;
4389abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
4399abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // Indicates whether this buffer needs to be cleaned up by consumer
4409abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        bool mNeedsCleanupOnRelease;
4416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    };
4426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mSlots is the array of buffer slots that must be mirrored on the client
4446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // side. This allows buffer ownership to be transferred between the client
4456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // and server without sending a GraphicBuffer over binder. The entire array
4466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // is initialized to NULL at construction time, and buffers are allocated
4476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // for a slot when requestBuffer is called with that slot's index.
4486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    BufferSlot mSlots[NUM_BUFFER_SLOTS];
4496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mDefaultWidth holds the default width of allocated buffers. It is used
4516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // in requestBuffers() if a width and height of zero is specified.
4526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    uint32_t mDefaultWidth;
4536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mDefaultHeight holds the default height of allocated buffers. It is used
4556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // in requestBuffers() if a width and height of zero is specified.
4566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    uint32_t mDefaultHeight;
4576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
45872f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // mMaxAcquiredBufferCount is the number of buffers that the consumer may
45972f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // acquire at one time.  It defaults to 1 and can be changed by the
46072f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // consumer via the setMaxAcquiredBufferCount method, but this may only be
46172f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // done when no producer is connected to the BufferQueue.
46272f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    //
46372f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // This value is used to derive the value returned for the
46472f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // MIN_UNDEQUEUED_BUFFERS query by the producer.
46572f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    int mMaxAcquiredBufferCount;
466abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam
46731a353da225af5329735451c761b430d82dfda1bJamie Gennis    // mDefaultMaxBufferCount is the default limit on the number of buffers
46831a353da225af5329735451c761b430d82dfda1bJamie Gennis    // that will be allocated at one time.  This default limit is set by the
46931a353da225af5329735451c761b430d82dfda1bJamie Gennis    // consumer.  The limit (as opposed to the default limit) may be
47031a353da225af5329735451c761b430d82dfda1bJamie Gennis    // overridden by the producer.
47131a353da225af5329735451c761b430d82dfda1bJamie Gennis    int mDefaultMaxBufferCount;
47231a353da225af5329735451c761b430d82dfda1bJamie Gennis
47331a353da225af5329735451c761b430d82dfda1bJamie Gennis    // mOverrideMaxBufferCount is the limit on the number of buffers that will
47431a353da225af5329735451c761b430d82dfda1bJamie Gennis    // be allocated at one time. This value is set by the image producer by
47531a353da225af5329735451c761b430d82dfda1bJamie Gennis    // calling setBufferCount. The default is zero, which means the producer
47631a353da225af5329735451c761b430d82dfda1bJamie Gennis    // doesn't care about the number of buffers in the pool. In that case
47731a353da225af5329735451c761b430d82dfda1bJamie Gennis    // mDefaultMaxBufferCount is used as the limit.
47831a353da225af5329735451c761b430d82dfda1bJamie Gennis    int mOverrideMaxBufferCount;
4796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
4816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // allocate new GraphicBuffer objects.
4826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
4836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
484fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // mConsumerListener is used to notify the connected consumer of
485fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // asynchronous events that it may wish to react to.  It is initially set
486fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // to NULL and is written by consumerConnect and consumerDisconnect.
487fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<ConsumerListener> mConsumerListener;
4886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mSynchronousMode whether we're in synchronous mode or not
4906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    bool mSynchronousMode;
4916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mAllowSynchronousMode whether we allow synchronous mode or not
4936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    const bool mAllowSynchronousMode;
4946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mConnectedApi indicates the API that is currently connected to this
4966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // BufferQueue.  It defaults to NO_CONNECTED_API (= 0), and gets updated
4976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // by the connect and disconnect methods.
4986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int mConnectedApi;
4996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mDequeueCondition condition used for dequeueBuffer in synchronous mode
5016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mutable Condition mDequeueCondition;
5026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mQueue is a FIFO of queued buffers used in synchronous mode
5046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    typedef Vector<int> Fifo;
5056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Fifo mQueue;
5066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mAbandoned indicates that the BufferQueue will no longer be used to
5086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // consume images buffers pushed to it using the ISurfaceTexture interface.
5096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // It is initialized to false, and set to true in the abandon method.  A
5106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // BufferQueue that has been abandoned will return the NO_INIT error from
5116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // all ISurfaceTexture methods capable of returning an error.
5126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    bool mAbandoned;
5136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mName is a string used to identify the BufferQueue in log messages.
5156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // It is set by the setName method.
516eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    String8 mConsumerName;
5176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mMutex is the mutex used to prevent concurrent access to the member
5196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // variables of BufferQueue objects. It must be locked whenever the
5206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // member variables are accessed.
5216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mutable Mutex mMutex;
5226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mFrameCounter is the free running counter, incremented for every buffer queued
5246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // with the surface Texture.
5256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    uint64_t mFrameCounter;
526eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
527b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mBufferHasBeenQueued is true once a buffer has been queued.  It is reset
528b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // by changing the buffer count.
529eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    bool mBufferHasBeenQueued;
530b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
531b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mDefaultBufferFormat can be set so it will override
532b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // the buffer format when it isn't specified in dequeueBuffer
533b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    uint32_t mDefaultBufferFormat;
534b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
535b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
536b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    uint32_t mConsumerUsageBits;
537b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
538b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mTransformHint is used to optimize for screen rotations
539b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    uint32_t mTransformHint;
5406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam};
5416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// ----------------------------------------------------------------------------
5436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}; // namespace android
5446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#endif // ANDROID_GUI_BUFFERQUEUE_H
546