blob: 89f277996880ebf445565faf4e9ac12dbdd22c8d [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
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
Dan Stoza3e96f192014-03-03 10:16:19 -080020#include <gui/BufferQueueDefs.h>
Dan Stoza289ade12014-02-28 11:17:17 -080021#include <gui/BufferSlot.h>
22
23#include <utils/Condition.h>
24#include <utils/Mutex.h>
Jesse Hall399184a2014-03-03 15:42:54 -080025#include <utils/NativeHandle.h>
Dan Stoza289ade12014-02-28 11:17:17 -080026#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/StrongPointer.h>
29#include <utils/Trace.h>
30#include <utils/Vector.h>
31
32#define BQ_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
33#define BQ_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
34#define BQ_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
35#define BQ_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
36#define BQ_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
37
38#define ATRACE_BUFFER_INDEX(index) \
39 if (ATRACE_ENABLED()) { \
40 char ___traceBuf[1024]; \
41 snprintf(___traceBuf, 1024, "%s: %d", \
42 mCore->mConsumerName.string(), (index)); \
43 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
44 }
45
46namespace android {
47
48class BufferItem;
49class IBinder;
50class IConsumerListener;
51class IGraphicBufferAlloc;
52
53class BufferQueueCore : public virtual RefBase {
54
55 friend class BufferQueueProducer;
56 friend class BufferQueueConsumer;
57
58public:
Dan Stoza289ade12014-02-28 11:17:17 -080059 // Used as a placeholder slot number when the value isn't pointing to an
60 // existing buffer.
61 enum { INVALID_BUFFER_SLOT = -1 }; // TODO: Extract from IGBC::BufferItem
62
63 // We reserve two slots in order to guarantee that the producer and
64 // consumer can run asynchronously.
Dan Stoza3e96f192014-03-03 10:16:19 -080065 enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 };
Dan Stoza289ade12014-02-28 11:17:17 -080066
67 // The default API number used to indicate that no producer is connected
68 enum { NO_CONNECTED_API = 0 };
69
Dan Stoza289ade12014-02-28 11:17:17 -080070 typedef Vector<BufferItem> Fifo;
71
72 // BufferQueueCore manages a pool of gralloc memory slots to be used by
73 // producers and consumers. allocator is used to allocate all the needed
74 // gralloc buffers.
75 BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL);
76 virtual ~BufferQueueCore();
77
78private:
Dan Stoza3e96f192014-03-03 10:16:19 -080079 // Dump our state in a string
Dan Stoza289ade12014-02-28 11:17:17 -080080 void dump(String8& result, const char* prefix) const;
81
Dan Stoza3e96f192014-03-03 10:16:19 -080082 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
83 // that must remain in a state other than DEQUEUED. The async parameter
84 // tells whether we're in asynchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -080085 int getMinUndequeuedBufferCountLocked(bool async) const;
Dan Stoza3e96f192014-03-03 10:16:19 -080086
87 // getMinMaxBufferCountLocked returns the minimum number of buffers allowed
88 // given the current BufferQueue state. The async parameter tells whether
89 // we're in asynchonous mode.
Dan Stoza289ade12014-02-28 11:17:17 -080090 int getMinMaxBufferCountLocked(bool async) const;
Dan Stoza3e96f192014-03-03 10:16:19 -080091
92 // getMaxBufferCountLocked returns the maximum number of buffers that can be
93 // allocated at once. This value depends on the following member variables:
94 //
95 // mDequeueBufferCannotBlock
96 // mMaxAcquiredBufferCount
97 // mDefaultMaxBufferCount
98 // mOverrideMaxBufferCount
99 // async parameter
100 //
101 // Any time one of these member variables is changed while a producer is
102 // connected, mDequeueCondition must be broadcast.
Dan Stoza289ade12014-02-28 11:17:17 -0800103 int getMaxBufferCountLocked(bool async) const;
Dan Stoza3e96f192014-03-03 10:16:19 -0800104
105 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
106 // that will be used if the producer does not override the buffer slot
107 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. The
108 // initial default is 2.
Dan Stoza289ade12014-02-28 11:17:17 -0800109 status_t setDefaultMaxBufferCountLocked(int count);
Dan Stoza3e96f192014-03-03 10:16:19 -0800110
111 // freeBufferLocked frees the GraphicBuffer and sync resources for the
112 // given slot.
Dan Stoza289ade12014-02-28 11:17:17 -0800113 void freeBufferLocked(int slot);
Dan Stoza3e96f192014-03-03 10:16:19 -0800114
115 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
116 // all slots.
Dan Stoza289ade12014-02-28 11:17:17 -0800117 void freeAllBuffersLocked();
Dan Stoza3e96f192014-03-03 10:16:19 -0800118
119 // stillTracking returns true iff the buffer item is still being tracked
120 // in one of the slots.
Dan Stoza289ade12014-02-28 11:17:17 -0800121 bool stillTracking(const BufferItem* item) const;
122
Dan Stoza3e96f192014-03-03 10:16:19 -0800123 // mAllocator is the connection to SurfaceFlinger that is used to allocate
124 // new GraphicBuffer objects.
125 sp<IGraphicBufferAlloc> mAllocator;
126
127 // mMutex is the mutex used to prevent concurrent access to the member
128 // variables of BufferQueueCore objects. It must be locked whenever any
129 // member variable is accessed.
Dan Stoza289ade12014-02-28 11:17:17 -0800130 mutable Mutex mMutex;
Dan Stoza3e96f192014-03-03 10:16:19 -0800131
132 // mIsAbandoned indicates that the BufferQueue will no longer be used to
133 // consume image buffers pushed to it using the IGraphicBufferProducer
134 // interface. It is initialized to false, and set to true in the
135 // consumerDisconnect method. A BufferQueue that is abandoned will return
136 // the NO_INIT error from all IGraphicBufferProducer methods capable of
137 // returning an error.
Dan Stoza289ade12014-02-28 11:17:17 -0800138 bool mIsAbandoned;
Dan Stoza3e96f192014-03-03 10:16:19 -0800139
140 // mConsumerControlledByApp indicates whether the connected consumer is
141 // controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800142 bool mConsumerControlledByApp;
Dan Stoza3e96f192014-03-03 10:16:19 -0800143
144 // mConsumerName is a string used to identify the BufferQueue in log
145 // messages. It is set by the IGraphicBufferConsumer::setConsumerName
146 // method.
Dan Stoza289ade12014-02-28 11:17:17 -0800147 String8 mConsumerName;
Dan Stoza3e96f192014-03-03 10:16:19 -0800148
149 // mConsumerListener is used to notify the connected consumer of
150 // asynchronous events that it may wish to react to. It is initially
151 // set to NULL and is written by consumerConnect and consumerDisconnect.
Dan Stoza289ade12014-02-28 11:17:17 -0800152 sp<IConsumerListener> mConsumerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800153
154 // mConsumerUsageBits contains flags that the consumer wants for
155 // GraphicBuffers.
Dan Stoza289ade12014-02-28 11:17:17 -0800156 uint32_t mConsumerUsageBits;
Dan Stoza3e96f192014-03-03 10:16:19 -0800157
158 // mConnectedApi indicates the producer API that is currently connected
159 // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
160 // by the connect and disconnect methods.
Dan Stoza289ade12014-02-28 11:17:17 -0800161 int mConnectedApi;
Dan Stoza3e96f192014-03-03 10:16:19 -0800162
163 // mConnectedProducerToken is used to set a binder death notification on
164 // the producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800165 sp<IBinder> mConnectedProducerToken;
Dan Stoza3e96f192014-03-03 10:16:19 -0800166
167 // mSlots is an array of buffer slots that must be mirrored on the producer
168 // side. This allows buffer ownership to be transferred between the producer
169 // and consumer without sending a GraphicBuffer over Binder. The entire
170 // array is initialized to NULL at construction time, and buffers are
171 // allocated for a slot when requestBuffer is called with that slot's index.
172 BufferQueueDefs::SlotsType mSlots;
173
174 // mQueue is a FIFO of queued buffers used in synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800175 Fifo mQueue;
Dan Stoza3e96f192014-03-03 10:16:19 -0800176
177 // mOverrideMaxBufferCount is the limit on the number of buffers that will
178 // be allocated at one time. This value is set by the producer by calling
179 // setBufferCount. The default is 0, which means that the producer doesn't
180 // care about the number of buffers in the pool. In that case,
181 // mDefaultMaxBufferCount is used as the limit.
Dan Stoza289ade12014-02-28 11:17:17 -0800182 int mOverrideMaxBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800183
184 // mDequeueCondition is a condition variable used for dequeueBuffer in
185 // synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800186 mutable Condition mDequeueCondition;
Dan Stoza3e96f192014-03-03 10:16:19 -0800187
188 // mUseAsyncBuffer indicates whether an extra buffer is used in async mode
189 // to prevent dequeueBuffer from blocking.
Dan Stoza289ade12014-02-28 11:17:17 -0800190 bool mUseAsyncBuffer;
Dan Stoza3e96f192014-03-03 10:16:19 -0800191
192 // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
193 // block. This flag is set during connect when both the producer and
194 // consumer are controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800195 bool mDequeueBufferCannotBlock;
Dan Stoza3e96f192014-03-03 10:16:19 -0800196
197 // mDefaultBufferFormat can be set so it will override the buffer format
198 // when it isn't specified in dequeueBuffer.
Dan Stoza289ade12014-02-28 11:17:17 -0800199 uint32_t mDefaultBufferFormat;
Dan Stoza3e96f192014-03-03 10:16:19 -0800200
201 // mDefaultWidth holds the default width of allocated buffers. It is used
202 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza289ade12014-02-28 11:17:17 -0800203 int mDefaultWidth;
Dan Stoza3e96f192014-03-03 10:16:19 -0800204
205 // mDefaultHeight holds the default height of allocated buffers. It is used
206 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza289ade12014-02-28 11:17:17 -0800207 int mDefaultHeight;
Dan Stoza3e96f192014-03-03 10:16:19 -0800208
209 // mDefaultMaxBufferCount is the default limit on the number of buffers that
210 // will be allocated at one time. This default limit is set by the consumer.
211 // The limit (as opposed to the default limit) may be overriden by the
212 // producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800213 int mDefaultMaxBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800214
215 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
216 // acquire at one time. It defaults to 1, and can be changed by the consumer
217 // via setMaxAcquiredBufferCount, but this may only be done while no
218 // producer is connected to the BufferQueue. This value is used to derive
219 // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800220 int mMaxAcquiredBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800221
222 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
223 // when something causes all buffers to be freed (e.g., changing the buffer
224 // count).
Dan Stoza289ade12014-02-28 11:17:17 -0800225 bool mBufferHasBeenQueued;
Dan Stoza3e96f192014-03-03 10:16:19 -0800226
227 // mFrameCounter is the free running counter, incremented on every
228 // successful queueBuffer call and buffer allocation.
Dan Stoza289ade12014-02-28 11:17:17 -0800229 uint64_t mFrameCounter;
Dan Stoza3e96f192014-03-03 10:16:19 -0800230
231 // mTransformHint is used to optimize for screen rotations.
Dan Stoza289ade12014-02-28 11:17:17 -0800232 uint32_t mTransformHint;
233
Jesse Hall399184a2014-03-03 15:42:54 -0800234 // mSidebandStream is a handle to the sideband buffer stream, if any
235 sp<NativeHandle> mSidebandStream;
236
Dan Stoza289ade12014-02-28 11:17:17 -0800237}; // class BufferQueueCore
238
239} // namespace android
240
241#endif