BufferQueueCore.h revision 289ade165e60b5f71734d30e535f16eb1f4313ad
1/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_GUI_BUFFERQUEUECORE_H
18#define ANDROID_GUI_BUFFERQUEUECORE_H
19
20#include <gui/BufferSlot.h>
21
22#include <utils/Condition.h>
23#include <utils/Mutex.h>
24#include <utils/RefBase.h>
25#include <utils/String8.h>
26#include <utils/StrongPointer.h>
27#include <utils/Trace.h>
28#include <utils/Vector.h>
29
30#define BQ_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
31#define BQ_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
32#define BQ_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
33#define BQ_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
34#define BQ_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
35
36#define ATRACE_BUFFER_INDEX(index)                                   \
37    if (ATRACE_ENABLED()) {                                          \
38        char ___traceBuf[1024];                                      \
39        snprintf(___traceBuf, 1024, "%s: %d",                        \
40                mCore->mConsumerName.string(), (index));             \
41        android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf);  \
42    }
43
44namespace android {
45
46class BufferItem;
47class IBinder;
48class IConsumerListener;
49class IGraphicBufferAlloc;
50
51class BufferQueueCore : public virtual RefBase {
52
53    friend class BufferQueueProducer;
54    friend class BufferQueueConsumer;
55
56public:
57    // BufferQueue will keep track of at most this value of buffers. Attempts
58    // at runtime to increase the number of buffers past this will fail.
59    enum { NUM_BUFFER_SLOTS = 32 };
60
61    // Used as a placeholder slot number when the value isn't pointing to an
62    // existing buffer.
63    enum { INVALID_BUFFER_SLOT = -1 }; // TODO: Extract from IGBC::BufferItem
64
65    // We reserve two slots in order to guarantee that the producer and
66    // consumer can run asynchronously.
67    enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
68
69    // The default API number used to indicate that no producer is connected
70    enum { NO_CONNECTED_API = 0 };
71
72    typedef BufferSlot SlotsType[NUM_BUFFER_SLOTS];
73    typedef Vector<BufferItem> Fifo;
74
75    // BufferQueueCore manages a pool of gralloc memory slots to be used by
76    // producers and consumers. allocator is used to allocate all the needed
77    // gralloc buffers.
78    BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL);
79    virtual ~BufferQueueCore();
80
81private:
82    void dump(String8& result, const char* prefix) const;
83
84    int getMinUndequeuedBufferCountLocked(bool async) const;
85    int getMinMaxBufferCountLocked(bool async) const;
86    int getMaxBufferCountLocked(bool async) const;
87    status_t setDefaultMaxBufferCountLocked(int count);
88    void freeBufferLocked(int slot);
89    void freeAllBuffersLocked();
90    bool stillTracking(const BufferItem* item) const;
91
92    const sp<IGraphicBufferAlloc>& mAllocator;
93    mutable Mutex mMutex;
94    bool mIsAbandoned;
95    bool mConsumerControlledByApp;
96    String8 mConsumerName;
97    sp<IConsumerListener> mConsumerListener;
98    uint32_t mConsumerUsageBits;
99    int mConnectedApi;
100    sp<IBinder> mConnectedProducerToken;
101    BufferSlot mSlots[NUM_BUFFER_SLOTS];
102    Fifo mQueue;
103    int mOverrideMaxBufferCount;
104    mutable Condition mDequeueCondition;
105    bool mUseAsyncBuffer;
106    bool mDequeueBufferCannotBlock;
107    uint32_t mDefaultBufferFormat;
108    int mDefaultWidth;
109    int mDefaultHeight;
110    int mDefaultMaxBufferCount;
111    int mMaxAcquiredBufferCount;
112    bool mBufferHasBeenQueued;
113    uint64_t mFrameCounter;
114    uint32_t mTransformHint;
115
116}; // class BufferQueueCore
117
118} // namespace android
119
120#endif
121