1289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza/*
2289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Copyright 2014 The Android Open Source Project
3289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
4289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Licensed under the Apache License, Version 2.0 (the "License");
5289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * you may not use this file except in compliance with the License.
6289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * You may obtain a copy of the License at
7289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
8289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *      http://www.apache.org/licenses/LICENSE-2.0
9289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
10289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Unless required by applicable law or agreed to in writing, software
11289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * distributed under the License is distributed on an "AS IS" BASIS,
12289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * See the License for the specific language governing permissions and
14289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * limitations under the License.
15289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza */
16289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
17289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#ifndef ANDROID_GUI_BUFFERQUEUECORE_H
18289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#define ANDROID_GUI_BUFFERQUEUECORE_H
19289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
201c87e474d87d6d1380fb61d476d606b1a2fda1c1Dan Stoza#include <gui/BufferItem.h>
213e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza#include <gui/BufferQueueDefs.h>
22289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <gui/BufferSlot.h>
23e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza#include <gui/OccupancyTracker.h>
24289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
25289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/Condition.h>
26289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/Mutex.h>
27399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall#include <utils/NativeHandle.h>
28289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/RefBase.h>
29289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/String8.h>
30289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/StrongPointer.h>
31289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/Trace.h>
32289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <utils/Vector.h>
33289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
340de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza#include <list>
350de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza#include <set>
360de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza
377d831871032675e252490b52ddbb29a63e5497bfDan Albert#define BQ_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
387d831871032675e252490b52ddbb29a63e5497bfDan Albert#define BQ_LOGD(x, ...) ALOGD("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
397d831871032675e252490b52ddbb29a63e5497bfDan Albert#define BQ_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
407d831871032675e252490b52ddbb29a63e5497bfDan Albert#define BQ_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
417d831871032675e252490b52ddbb29a63e5497bfDan Albert#define BQ_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
42289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
43289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#define ATRACE_BUFFER_INDEX(index)                                   \
44289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    if (ATRACE_ENABLED()) {                                          \
45289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza        char ___traceBuf[1024];                                      \
46289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza        snprintf(___traceBuf, 1024, "%s: %d",                        \
47289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza                mCore->mConsumerName.string(), (index));             \
48289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza        android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf);  \
49289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    }
50289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
51289ade165e60b5f71734d30e535f16eb1f4313adDan Stozanamespace android {
52289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
53289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaclass IConsumerListener;
54f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stozaclass IProducerListener;
55289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
56289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaclass BufferQueueCore : public virtual RefBase {
57289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
58289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    friend class BufferQueueProducer;
59289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    friend class BufferQueueConsumer;
60289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
61289ade165e60b5f71734d30e535f16eb1f4313adDan Stozapublic:
62289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // Used as a placeholder slot number when the value isn't pointing to an
63289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // existing buffer.
641c87e474d87d6d1380fb61d476d606b1a2fda1c1Dan Stoza    enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
65289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
66289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // We reserve two slots in order to guarantee that the producer and
67289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // consumer can run asynchronously.
683e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 };
69289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
701b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang    enum {
711b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang        // The API number used to indicate the currently connected producer
721b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang        CURRENTLY_CONNECTED_API = -1,
731b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang
741b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang        // The API number used to indicate that no producer is connected
751b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang        NO_CONNECTED_API        = 0,
761b3a9acb77ee12568fa19740d8eeab165a0dcd98Chong Zhang    };
77289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
78289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    typedef Vector<BufferItem> Fifo;
79289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
80289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // BufferQueueCore manages a pool of gralloc memory slots to be used by
810556d79eacbf0c9978080d87aa4075120533c7efMathias Agopian    // producers and consumers.
820556d79eacbf0c9978080d87aa4075120533c7efMathias Agopian    BufferQueueCore();
83289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual ~BufferQueueCore();
84289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
85289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaprivate:
863e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // Dump our state in a string
870c9a1ed91f8e19887ac43eff5af16e59878c8226Dan Stoza    void dumpState(const String8& prefix, String8* outResult) const;
88289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
893e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
903e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // that must remain in a state other than DEQUEUED. The async parameter
913e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // tells whether we're in asynchronous mode.
92567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    int getMinUndequeuedBufferCountLocked() const;
933e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
943e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // getMinMaxBufferCountLocked returns the minimum number of buffers allowed
953e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // given the current BufferQueue state. The async parameter tells whether
963e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // we're in asynchonous mode.
97567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    int getMinMaxBufferCountLocked() const;
983e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
993e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // getMaxBufferCountLocked returns the maximum number of buffers that can be
1003e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // allocated at once. This value depends on the following member variables:
1013e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    //
102567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    //     mMaxDequeuedBufferCount
1033e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    //     mMaxAcquiredBufferCount
104567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    //     mMaxBufferCount
105567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    //     mAsyncMode
106567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    //     mDequeueBufferCannotBlock
1073e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    //
1083e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // Any time one of these member variables is changed while a producer is
1093e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // connected, mDequeueCondition must be broadcast.
110567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    int getMaxBufferCountLocked() const;
1113e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
11223b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // This performs the same computation but uses the given arguments instead
11323b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // of the member variables for mMaxBufferCount, mAsyncMode, and
11423b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // mDequeueBufferCannotBlock.
11523b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    int getMaxBufferCountLocked(bool asyncMode,
11623b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos            bool dequeueBufferCannotBlock, int maxBufferCount) const;
11723b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos
11823b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // clearBufferSlotLocked frees the GraphicBuffer and sync resources for the
1193e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // given slot.
12023b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    void clearBufferSlotLocked(int slot);
1213e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1223e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
123ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos    // all slots, even if they're currently dequeued, queued, or acquired.
124289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    void freeAllBuffersLocked();
1253e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
126bc2df65a3f3f4b8abaaaa2a4e576a3a42c2d30f3Eino-Ville Talvala    // discardFreeBuffersLocked releases all currently-free buffers held by the
127bc2df65a3f3f4b8abaaaa2a4e576a3a42c2d30f3Eino-Ville Talvala    // queue, in order to reduce the memory consumption of the queue to the
128bc2df65a3f3f4b8abaaaa2a4e576a3a42c2d30f3Eino-Ville Talvala    // minimum possible without discarding data.
129bc2df65a3f3f4b8abaaaa2a4e576a3a42c2d30f3Eino-Ville Talvala    void discardFreeBuffersLocked();
130bc2df65a3f3f4b8abaaaa2a4e576a3a42c2d30f3Eino-Ville Talvala
13123b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // If delta is positive, makes more slots available. If negative, takes
13223b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // away slots. Returns false if the request can't be met.
13323b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    bool adjustAvailableSlotsLocked(int delta);
134289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
13578014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // waitWhileAllocatingLocked blocks until mIsAllocating is false.
13678014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    void waitWhileAllocatingLocked() const;
13778014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour
1389e314337cdc65b1fbf52060e9a6a4ddf2215c352Pablo Ceballos#if DEBUG_ONLY_CODE
1390de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    // validateConsistencyLocked ensures that the free lists are in sync with
1400de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    // the information stored in mSlots
1410de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    void validateConsistencyLocked() const;
1429e314337cdc65b1fbf52060e9a6a4ddf2215c352Pablo Ceballos#endif
1430de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza
1443e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mMutex is the mutex used to prevent concurrent access to the member
1453e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // variables of BufferQueueCore objects. It must be locked whenever any
1463e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // member variable is accessed.
147289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    mutable Mutex mMutex;
1483e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1493e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mIsAbandoned indicates that the BufferQueue will no longer be used to
1503e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // consume image buffers pushed to it using the IGraphicBufferProducer
1513e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // interface. It is initialized to false, and set to true in the
1523e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // consumerDisconnect method. A BufferQueue that is abandoned will return
1533e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // the NO_INIT error from all IGraphicBufferProducer methods capable of
1543e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // returning an error.
155289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    bool mIsAbandoned;
1563e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1573e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mConsumerControlledByApp indicates whether the connected consumer is
1583e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // controlled by the application.
159289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    bool mConsumerControlledByApp;
1603e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1613e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mConsumerName is a string used to identify the BufferQueue in log
1623e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // messages. It is set by the IGraphicBufferConsumer::setConsumerName
1633e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // method.
164289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    String8 mConsumerName;
1653e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1663e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mConsumerListener is used to notify the connected consumer of
1673e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // asynchronous events that it may wish to react to. It is initially
1683e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // set to NULL and is written by consumerConnect and consumerDisconnect.
169289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    sp<IConsumerListener> mConsumerListener;
1703e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1713e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mConsumerUsageBits contains flags that the consumer wants for
1723e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // GraphicBuffers.
173289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    uint32_t mConsumerUsageBits;
1743e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1752041913a05b79b96c5c084f30bb8944049a976c8Jiwen 'Steve' Cai    // mConsumerIsProtected indicates the consumer is ready to handle protected
1762041913a05b79b96c5c084f30bb8944049a976c8Jiwen 'Steve' Cai    // buffer.
1772041913a05b79b96c5c084f30bb8944049a976c8Jiwen 'Steve' Cai    bool mConsumerIsProtected;
1782041913a05b79b96c5c084f30bb8944049a976c8Jiwen 'Steve' Cai
1793e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mConnectedApi indicates the producer API that is currently connected
1803e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
1813e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // by the connect and disconnect methods.
182289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    int mConnectedApi;
18397b9c86338e2d364d47ea7522c2d81a8014f0e07Robert Carr    // PID of the process which last successfully called connect(...)
18497b9c86338e2d364d47ea7522c2d81a8014f0e07Robert Carr    pid_t mConnectedPid;
1853e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1863b8e6b2f30af0564538c2a660033d6a97ab8038fMatthew Bouyack    // mLinkedToDeath is used to set a binder death notification on
1873e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // the producer.
1883b8e6b2f30af0564538c2a660033d6a97ab8038fMatthew Bouyack    sp<IProducerListener> mLinkedToDeath;
1893b8e6b2f30af0564538c2a660033d6a97ab8038fMatthew Bouyack
1903b8e6b2f30af0564538c2a660033d6a97ab8038fMatthew Bouyack    // mConnectedProducerListener is used to handle the onBufferReleased
1913b8e6b2f30af0564538c2a660033d6a97ab8038fMatthew Bouyack    // notification.
192f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stoza    sp<IProducerListener> mConnectedProducerListener;
1933e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1943e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mSlots is an array of buffer slots that must be mirrored on the producer
1953e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // side. This allows buffer ownership to be transferred between the producer
1963e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // and consumer without sending a GraphicBuffer over Binder. The entire
1973e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // array is initialized to NULL at construction time, and buffers are
1983e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // allocated for a slot when requestBuffer is called with that slot's index.
1993e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    BufferQueueDefs::SlotsType mSlots;
2003e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2013e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mQueue is a FIFO of queued buffers used in synchronous mode.
202289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    Fifo mQueue;
2033e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2040de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    // mFreeSlots contains all of the slots which are FREE and do not currently
20523b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // have a buffer attached.
2060de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    std::set<int> mFreeSlots;
2070de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza
2080de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    // mFreeBuffers contains all of the slots which are FREE and currently have
20923b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // a buffer attached.
2100de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza    std::list<int> mFreeBuffers;
2110de7ea752900b5da29ad19c2799040235477f3c5Dan Stoza
21223b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // mUnusedSlots contains all slots that are currently unused. They should be
21323b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // free and not have a buffer attached.
21423b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    std::list<int> mUnusedSlots;
21523b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos
21623b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // mActiveBuffers contains all slots which have a non-FREE buffer attached.
21723b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    std::set<int> mActiveBuffers;
21823b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos
2193e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mDequeueCondition is a condition variable used for dequeueBuffer in
2203e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // synchronous mode.
221289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    mutable Condition mDequeueCondition;
2223e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2233e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
2243e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // block. This flag is set during connect when both the producer and
2253e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // consumer are controlled by the application.
226289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    bool mDequeueBufferCannotBlock;
2273e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2283e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mDefaultBufferFormat can be set so it will override the buffer format
2293e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // when it isn't specified in dequeueBuffer.
2303be1c6b60a188dc10025e2ce156c11fac050625dDan Stoza    PixelFormat mDefaultBufferFormat;
2313e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2323e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mDefaultWidth holds the default width of allocated buffers. It is used
2333e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // in dequeueBuffer if a width and height of 0 are specified.
2343be1c6b60a188dc10025e2ce156c11fac050625dDan Stoza    uint32_t mDefaultWidth;
2353e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2363e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mDefaultHeight holds the default height of allocated buffers. It is used
2373e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // in dequeueBuffer if a width and height of 0 are specified.
2383be1c6b60a188dc10025e2ce156c11fac050625dDan Stoza    uint32_t mDefaultHeight;
2393e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
24082c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala    // mDefaultBufferDataSpace holds the default dataSpace of queued buffers.
24182c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala    // It is used in queueBuffer if a dataspace of 0 (HAL_DATASPACE_UNKNOWN)
24282c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala    // is specified.
24382c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala    android_dataspace mDefaultBufferDataSpace;
24482c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala
24519e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos    // mMaxBufferCount is the limit on the number of buffers that will be
24619e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos    // allocated at one time. This limit can be set by the consumer.
24719e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos    int mMaxBufferCount;
2483e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2493e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mMaxAcquiredBufferCount is the number of buffers that the consumer may
2503e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // acquire at one time. It defaults to 1, and can be changed by the consumer
2513e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // via setMaxAcquiredBufferCount, but this may only be done while no
2523e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // producer is connected to the BufferQueue. This value is used to derive
2533e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
254289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    int mMaxAcquiredBufferCount;
2553e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
256fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // mMaxDequeuedBufferCount is the number of buffers that the producer may
257fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // dequeue at one time. It defaults to 1, and can be changed by the producer
258fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // via setMaxDequeuedBufferCount.
259fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    int mMaxDequeuedBufferCount;
260fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
2613e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
2623e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // when something causes all buffers to be freed (e.g., changing the buffer
2633e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // count).
264289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    bool mBufferHasBeenQueued;
2653e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2663e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mFrameCounter is the free running counter, incremented on every
2673e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // successful queueBuffer call and buffer allocation.
268289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    uint64_t mFrameCounter;
2693e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2703e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // mTransformHint is used to optimize for screen rotations.
271289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    uint32_t mTransformHint;
272289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
273399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // mSidebandStream is a handle to the sideband buffer stream, if any
274399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    sp<NativeHandle> mSidebandStream;
275399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall
27678014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // mIsAllocating indicates whether a producer is currently trying to allocate buffers (which
27778014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // releases mMutex while doing the allocation proper). Producers should not modify any of the
27878014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // FREE slots while this is true. mIsAllocatingCondition is signaled when this value changes to
27978014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // false.
28078014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    bool mIsAllocating;
28178014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour
28278014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // mIsAllocatingCondition is a condition variable used by producers to wait until mIsAllocating
28378014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    // becomes false.
28478014f32da6d0ebf52fb34ebb7663863000520a0Antoine Labour    mutable Condition mIsAllocatingCondition;
2859de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza
2869de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza    // mAllowAllocation determines whether dequeueBuffer is allowed to allocate
2879de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza    // new buffers
2889de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza    bool mAllowAllocation;
2894afd8b67f9be307e6c6ed89deab2e85516bb3a0eDan Stoza
2904afd8b67f9be307e6c6ed89deab2e85516bb3a0eDan Stoza    // mBufferAge tracks the age of the contents of the most recently dequeued
2914afd8b67f9be307e6c6ed89deab2e85516bb3a0eDan Stoza    // buffer as the number of frames that have elapsed since it was last queued
2924afd8b67f9be307e6c6ed89deab2e85516bb3a0eDan Stoza    uint64_t mBufferAge;
293ecc504043fddb7a75042ce402c67aedfac04d5e2Dan Stoza
294812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza    // mGenerationNumber stores the current generation number of the attached
295812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza    // producer. Any attempt to attach a buffer with a different generation
296812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza    // number will fail.
297812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza    uint32_t mGenerationNumber;
298812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza
299fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // mAsyncMode indicates whether or not async mode is enabled.
300fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // In async mode an extra buffer will be allocated to allow the producer to
301fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // enqueue buffers without blocking.
302fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    bool mAsyncMode;
303fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
3043559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    // mSharedBufferMode indicates whether or not shared buffer mode is enabled.
3053559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    bool mSharedBufferMode;
306ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos
3073559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    // When shared buffer mode is enabled, this indicates whether the consumer
308ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos    // should acquire buffers even if BufferQueue doesn't indicate that they are
309ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos    // available.
310ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos    bool mAutoRefresh;
311ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos
3123559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    // When shared buffer mode is enabled, this tracks which slot contains the
313ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos    // shared buffer.
3143559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    int mSharedBufferSlot;
315ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos
3163559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    // Cached data about the shared buffer in shared buffer mode
3173559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    struct SharedBufferCache {
3186c5a17dd2603b282cb0800c262857dc0f3d55f7eColin Cross        SharedBufferCache(Rect _crop, uint32_t _transform,
3196c5a17dd2603b282cb0800c262857dc0f3d55f7eColin Cross                uint32_t _scalingMode, android_dataspace _dataspace)
320ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos        : crop(_crop),
321ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos          transform(_transform),
322ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos          scalingMode(_scalingMode),
323ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos          dataspace(_dataspace) {
32417576de056a57753eb4af797545db88ef4f81ef0Colin Cross        }
325ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos
326ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos        Rect crop;
327ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos        uint32_t transform;
328ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos        uint32_t scalingMode;
329ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos        android_dataspace dataspace;
3303559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    } mSharedBufferCache;
331ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos
33250101d02a8eae555887282a5f761fdec57bdaf30Dan Stoza    // The slot of the last queued buffer
33350101d02a8eae555887282a5f761fdec57bdaf30Dan Stoza    int mLastQueuedSlot;
33450101d02a8eae555887282a5f761fdec57bdaf30Dan Stoza
335e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza    OccupancyTracker mOccupancyTracker;
336e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
3378e3e92b906db431c4fa822f21242977d4ee99942Pablo Ceballos    const uint64_t mUniqueId;
3388e3e92b906db431c4fa822f21242977d4ee99942Pablo Ceballos
339289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza}; // class BufferQueueCore
340289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
341289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza} // namespace android
342289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
343289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#endif
344