blob: a22015c1aa9131574a53d42656e767ff77cfb5c3 [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 Stoza1c87e472015-03-13 14:40:34 -070020#include <gui/BufferItem.h>
Dan Stoza3e96f192014-03-03 10:16:19 -080021#include <gui/BufferQueueDefs.h>
Dan Stoza289ade12014-02-28 11:17:17 -080022#include <gui/BufferSlot.h>
23
24#include <utils/Condition.h>
25#include <utils/Mutex.h>
Jesse Hall399184a2014-03-03 15:42:54 -080026#include <utils/NativeHandle.h>
Dan Stoza289ade12014-02-28 11:17:17 -080027#include <utils/RefBase.h>
28#include <utils/String8.h>
29#include <utils/StrongPointer.h>
30#include <utils/Trace.h>
31#include <utils/Vector.h>
32
Dan Stoza0de7ea72015-04-23 13:20:51 -070033#include <list>
34#include <set>
35
Dan Albert7d831872014-09-09 09:21:28 -070036#define BQ_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
37#define BQ_LOGD(x, ...) ALOGD("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
38#define BQ_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
39#define BQ_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
40#define BQ_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
Dan Stoza289ade12014-02-28 11:17:17 -080041
42#define ATRACE_BUFFER_INDEX(index) \
43 if (ATRACE_ENABLED()) { \
44 char ___traceBuf[1024]; \
45 snprintf(___traceBuf, 1024, "%s: %d", \
46 mCore->mConsumerName.string(), (index)); \
47 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
48 }
49
50namespace android {
51
Dan Stoza289ade12014-02-28 11:17:17 -080052class IConsumerListener;
53class IGraphicBufferAlloc;
Dan Stozaf0eaf252014-03-21 13:05:51 -070054class IProducerListener;
Dan Stoza289ade12014-02-28 11:17:17 -080055
56class BufferQueueCore : public virtual RefBase {
57
58 friend class BufferQueueProducer;
59 friend class BufferQueueConsumer;
60
61public:
Dan Stoza289ade12014-02-28 11:17:17 -080062 // Used as a placeholder slot number when the value isn't pointing to an
63 // existing buffer.
Dan Stoza1c87e472015-03-13 14:40:34 -070064 enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
Dan Stoza289ade12014-02-28 11:17:17 -080065
66 // We reserve two slots in order to guarantee that the producer and
67 // consumer can run asynchronously.
Dan Stoza3e96f192014-03-03 10:16:19 -080068 enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 };
Dan Stoza289ade12014-02-28 11:17:17 -080069
70 // The default API number used to indicate that no producer is connected
71 enum { NO_CONNECTED_API = 0 };
72
Dan Stoza289ade12014-02-28 11:17:17 -080073 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:
Dan Stoza3e96f192014-03-03 10:16:19 -080082 // Dump our state in a string
Dan Stoza289ade12014-02-28 11:17:17 -080083 void dump(String8& result, const char* prefix) const;
84
Dan Stoza3e96f192014-03-03 10:16:19 -080085 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
86 // that must remain in a state other than DEQUEUED. The async parameter
87 // tells whether we're in asynchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -080088 int getMinUndequeuedBufferCountLocked(bool async) const;
Dan Stoza3e96f192014-03-03 10:16:19 -080089
90 // getMinMaxBufferCountLocked returns the minimum number of buffers allowed
91 // given the current BufferQueue state. The async parameter tells whether
92 // we're in asynchonous mode.
Dan Stoza289ade12014-02-28 11:17:17 -080093 int getMinMaxBufferCountLocked(bool async) const;
Dan Stoza3e96f192014-03-03 10:16:19 -080094
95 // getMaxBufferCountLocked returns the maximum number of buffers that can be
96 // allocated at once. This value depends on the following member variables:
97 //
98 // mDequeueBufferCannotBlock
99 // mMaxAcquiredBufferCount
100 // mDefaultMaxBufferCount
101 // mOverrideMaxBufferCount
102 // async parameter
103 //
104 // Any time one of these member variables is changed while a producer is
105 // connected, mDequeueCondition must be broadcast.
Dan Stoza289ade12014-02-28 11:17:17 -0800106 int getMaxBufferCountLocked(bool async) const;
Dan Stoza3e96f192014-03-03 10:16:19 -0800107
108 // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
109 // that will be used if the producer does not override the buffer slot
110 // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. The
111 // initial default is 2.
Dan Stoza289ade12014-02-28 11:17:17 -0800112 status_t setDefaultMaxBufferCountLocked(int count);
Dan Stoza3e96f192014-03-03 10:16:19 -0800113
114 // freeBufferLocked frees the GraphicBuffer and sync resources for the
115 // given slot.
Dan Stoza289ade12014-02-28 11:17:17 -0800116 void freeBufferLocked(int slot);
Dan Stoza3e96f192014-03-03 10:16:19 -0800117
118 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
119 // all slots.
Dan Stoza289ade12014-02-28 11:17:17 -0800120 void freeAllBuffersLocked();
Dan Stoza3e96f192014-03-03 10:16:19 -0800121
122 // stillTracking returns true iff the buffer item is still being tracked
123 // in one of the slots.
Dan Stoza289ade12014-02-28 11:17:17 -0800124 bool stillTracking(const BufferItem* item) const;
125
Antoine Labour78014f32014-07-15 21:17:03 -0700126 // waitWhileAllocatingLocked blocks until mIsAllocating is false.
127 void waitWhileAllocatingLocked() const;
128
Dan Stoza0de7ea72015-04-23 13:20:51 -0700129 // validateConsistencyLocked ensures that the free lists are in sync with
130 // the information stored in mSlots
131 void validateConsistencyLocked() const;
132
Dan Stoza3e96f192014-03-03 10:16:19 -0800133 // mAllocator is the connection to SurfaceFlinger that is used to allocate
134 // new GraphicBuffer objects.
135 sp<IGraphicBufferAlloc> mAllocator;
136
137 // mMutex is the mutex used to prevent concurrent access to the member
138 // variables of BufferQueueCore objects. It must be locked whenever any
139 // member variable is accessed.
Dan Stoza289ade12014-02-28 11:17:17 -0800140 mutable Mutex mMutex;
Dan Stoza3e96f192014-03-03 10:16:19 -0800141
142 // mIsAbandoned indicates that the BufferQueue will no longer be used to
143 // consume image buffers pushed to it using the IGraphicBufferProducer
144 // interface. It is initialized to false, and set to true in the
145 // consumerDisconnect method. A BufferQueue that is abandoned will return
146 // the NO_INIT error from all IGraphicBufferProducer methods capable of
147 // returning an error.
Dan Stoza289ade12014-02-28 11:17:17 -0800148 bool mIsAbandoned;
Dan Stoza3e96f192014-03-03 10:16:19 -0800149
150 // mConsumerControlledByApp indicates whether the connected consumer is
151 // controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800152 bool mConsumerControlledByApp;
Dan Stoza3e96f192014-03-03 10:16:19 -0800153
154 // mConsumerName is a string used to identify the BufferQueue in log
155 // messages. It is set by the IGraphicBufferConsumer::setConsumerName
156 // method.
Dan Stoza289ade12014-02-28 11:17:17 -0800157 String8 mConsumerName;
Dan Stoza3e96f192014-03-03 10:16:19 -0800158
159 // mConsumerListener is used to notify the connected consumer of
160 // asynchronous events that it may wish to react to. It is initially
161 // set to NULL and is written by consumerConnect and consumerDisconnect.
Dan Stoza289ade12014-02-28 11:17:17 -0800162 sp<IConsumerListener> mConsumerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800163
164 // mConsumerUsageBits contains flags that the consumer wants for
165 // GraphicBuffers.
Dan Stoza289ade12014-02-28 11:17:17 -0800166 uint32_t mConsumerUsageBits;
Dan Stoza3e96f192014-03-03 10:16:19 -0800167
168 // mConnectedApi indicates the producer API that is currently connected
169 // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
170 // by the connect and disconnect methods.
Dan Stoza289ade12014-02-28 11:17:17 -0800171 int mConnectedApi;
Dan Stoza3e96f192014-03-03 10:16:19 -0800172
173 // mConnectedProducerToken is used to set a binder death notification on
174 // the producer.
Dan Stozaf0eaf252014-03-21 13:05:51 -0700175 sp<IProducerListener> mConnectedProducerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800176
177 // mSlots is an array of buffer slots that must be mirrored on the producer
178 // side. This allows buffer ownership to be transferred between the producer
179 // and consumer without sending a GraphicBuffer over Binder. The entire
180 // array is initialized to NULL at construction time, and buffers are
181 // allocated for a slot when requestBuffer is called with that slot's index.
182 BufferQueueDefs::SlotsType mSlots;
183
184 // mQueue is a FIFO of queued buffers used in synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800185 Fifo mQueue;
Dan Stoza3e96f192014-03-03 10:16:19 -0800186
Dan Stoza0de7ea72015-04-23 13:20:51 -0700187 // mFreeSlots contains all of the slots which are FREE and do not currently
188 // have a buffer attached
189 std::set<int> mFreeSlots;
190
191 // mFreeBuffers contains all of the slots which are FREE and currently have
192 // a buffer attached
193 std::list<int> mFreeBuffers;
194
Dan Stoza3e96f192014-03-03 10:16:19 -0800195 // mOverrideMaxBufferCount is the limit on the number of buffers that will
196 // be allocated at one time. This value is set by the producer by calling
197 // setBufferCount. The default is 0, which means that the producer doesn't
198 // care about the number of buffers in the pool. In that case,
199 // mDefaultMaxBufferCount is used as the limit.
Dan Stoza289ade12014-02-28 11:17:17 -0800200 int mOverrideMaxBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800201
202 // mDequeueCondition is a condition variable used for dequeueBuffer in
203 // synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800204 mutable Condition mDequeueCondition;
Dan Stoza3e96f192014-03-03 10:16:19 -0800205
206 // mUseAsyncBuffer indicates whether an extra buffer is used in async mode
207 // to prevent dequeueBuffer from blocking.
Dan Stoza289ade12014-02-28 11:17:17 -0800208 bool mUseAsyncBuffer;
Dan Stoza3e96f192014-03-03 10:16:19 -0800209
210 // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
211 // block. This flag is set during connect when both the producer and
212 // consumer are controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800213 bool mDequeueBufferCannotBlock;
Dan Stoza3e96f192014-03-03 10:16:19 -0800214
215 // mDefaultBufferFormat can be set so it will override the buffer format
216 // when it isn't specified in dequeueBuffer.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800217 PixelFormat mDefaultBufferFormat;
Dan Stoza3e96f192014-03-03 10:16:19 -0800218
219 // mDefaultWidth holds the default width of allocated buffers. It is used
220 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800221 uint32_t mDefaultWidth;
Dan Stoza3e96f192014-03-03 10:16:19 -0800222
223 // mDefaultHeight holds the default height of allocated buffers. It is used
224 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800225 uint32_t mDefaultHeight;
Dan Stoza3e96f192014-03-03 10:16:19 -0800226
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800227 // mDefaultBufferDataSpace holds the default dataSpace of queued buffers.
228 // It is used in queueBuffer if a dataspace of 0 (HAL_DATASPACE_UNKNOWN)
229 // is specified.
230 android_dataspace mDefaultBufferDataSpace;
231
Dan Stoza3e96f192014-03-03 10:16:19 -0800232 // mDefaultMaxBufferCount is the default limit on the number of buffers that
233 // will be allocated at one time. This default limit is set by the consumer.
234 // The limit (as opposed to the default limit) may be overriden by the
235 // producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800236 int mDefaultMaxBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800237
238 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
239 // acquire at one time. It defaults to 1, and can be changed by the consumer
240 // via setMaxAcquiredBufferCount, but this may only be done while no
241 // producer is connected to the BufferQueue. This value is used to derive
242 // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800243 int mMaxAcquiredBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800244
245 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
246 // when something causes all buffers to be freed (e.g., changing the buffer
247 // count).
Dan Stoza289ade12014-02-28 11:17:17 -0800248 bool mBufferHasBeenQueued;
Dan Stoza3e96f192014-03-03 10:16:19 -0800249
250 // mFrameCounter is the free running counter, incremented on every
251 // successful queueBuffer call and buffer allocation.
Dan Stoza289ade12014-02-28 11:17:17 -0800252 uint64_t mFrameCounter;
Dan Stoza3e96f192014-03-03 10:16:19 -0800253
254 // mTransformHint is used to optimize for screen rotations.
Dan Stoza289ade12014-02-28 11:17:17 -0800255 uint32_t mTransformHint;
256
Jesse Hall399184a2014-03-03 15:42:54 -0800257 // mSidebandStream is a handle to the sideband buffer stream, if any
258 sp<NativeHandle> mSidebandStream;
259
Antoine Labour78014f32014-07-15 21:17:03 -0700260 // mIsAllocating indicates whether a producer is currently trying to allocate buffers (which
261 // releases mMutex while doing the allocation proper). Producers should not modify any of the
262 // FREE slots while this is true. mIsAllocatingCondition is signaled when this value changes to
263 // false.
264 bool mIsAllocating;
265
266 // mIsAllocatingCondition is a condition variable used by producers to wait until mIsAllocating
267 // becomes false.
268 mutable Condition mIsAllocatingCondition;
Dan Stoza9de72932015-04-16 17:28:43 -0700269
270 // mAllowAllocation determines whether dequeueBuffer is allowed to allocate
271 // new buffers
272 bool mAllowAllocation;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800273
274 // mBufferAge tracks the age of the contents of the most recently dequeued
275 // buffer as the number of frames that have elapsed since it was last queued
276 uint64_t mBufferAge;
Dan Stozaecc50402015-04-28 14:42:06 -0700277
278 // mConsumerHasShadowQueue determines if acquireBuffer should be more
279 // cautious about dropping buffers so that it always returns a buffer that
280 // is represented in the consumer's shadow queue.
281 bool mConsumerHasShadowQueue;
282 size_t mConsumerShadowQueueSize;
283
Dan Stoza289ade12014-02-28 11:17:17 -0800284}; // class BufferQueueCore
285
286} // namespace android
287
288#endif