blob: 0371db780b70f432c8ce05cec717e346f5983fd6 [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
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080070 enum {
71 // The API number used to indicate the currently connected producer
72 CURRENTLY_CONNECTED_API = -1,
73
74 // The API number used to indicate that no producer is connected
75 NO_CONNECTED_API = 0,
76 };
Dan Stoza289ade12014-02-28 11:17:17 -080077
Dan Stoza289ade12014-02-28 11:17:17 -080078 typedef Vector<BufferItem> Fifo;
79
80 // BufferQueueCore manages a pool of gralloc memory slots to be used by
81 // producers and consumers. allocator is used to allocate all the needed
82 // gralloc buffers.
Dan Stoza70982a52016-01-11 23:40:44 +000083 BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL);
Dan Stoza289ade12014-02-28 11:17:17 -080084 virtual ~BufferQueueCore();
85
86private:
Dan Stoza3e96f192014-03-03 10:16:19 -080087 // Dump our state in a string
Dan Stoza289ade12014-02-28 11:17:17 -080088 void dump(String8& result, const char* prefix) const;
89
Dan Stoza3e96f192014-03-03 10:16:19 -080090 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
91 // that must remain in a state other than DEQUEUED. The async parameter
92 // tells whether we're in asynchronous mode.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070093 int getMinUndequeuedBufferCountLocked() const;
Dan Stoza3e96f192014-03-03 10:16:19 -080094
95 // getMinMaxBufferCountLocked returns the minimum number of buffers allowed
96 // given the current BufferQueue state. The async parameter tells whether
97 // we're in asynchonous mode.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070098 int getMinMaxBufferCountLocked() const;
Dan Stoza3e96f192014-03-03 10:16:19 -080099
100 // getMaxBufferCountLocked returns the maximum number of buffers that can be
101 // allocated at once. This value depends on the following member variables:
102 //
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700103 // mMaxDequeuedBufferCount
Dan Stoza3e96f192014-03-03 10:16:19 -0800104 // mMaxAcquiredBufferCount
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700105 // mMaxBufferCount
106 // mAsyncMode
107 // mDequeueBufferCannotBlock
Dan Stoza3e96f192014-03-03 10:16:19 -0800108 //
109 // Any time one of these member variables is changed while a producer is
110 // connected, mDequeueCondition must be broadcast.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700111 int getMaxBufferCountLocked() const;
Dan Stoza3e96f192014-03-03 10:16:19 -0800112
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800113 // This performs the same computation but uses the given arguments instead
114 // of the member variables for mMaxBufferCount, mAsyncMode, and
115 // mDequeueBufferCannotBlock.
116 int getMaxBufferCountLocked(bool asyncMode,
117 bool dequeueBufferCannotBlock, int maxBufferCount) const;
118
119 // clearBufferSlotLocked frees the GraphicBuffer and sync resources for the
Dan Stoza3e96f192014-03-03 10:16:19 -0800120 // given slot.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800121 void clearBufferSlotLocked(int slot);
Dan Stoza3e96f192014-03-03 10:16:19 -0800122
123 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700124 // all slots, even if they're currently dequeued, queued, or acquired.
Dan Stoza289ade12014-02-28 11:17:17 -0800125 void freeAllBuffersLocked();
Dan Stoza3e96f192014-03-03 10:16:19 -0800126
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800127 // If delta is positive, makes more slots available. If negative, takes
128 // away slots. Returns false if the request can't be met.
129 bool adjustAvailableSlotsLocked(int delta);
Dan Stoza289ade12014-02-28 11:17:17 -0800130
Antoine Labour78014f32014-07-15 21:17:03 -0700131 // waitWhileAllocatingLocked blocks until mIsAllocating is false.
132 void waitWhileAllocatingLocked() const;
133
Pablo Ceballos9e314332016-01-12 13:49:19 -0800134#if DEBUG_ONLY_CODE
Dan Stoza0de7ea72015-04-23 13:20:51 -0700135 // validateConsistencyLocked ensures that the free lists are in sync with
136 // the information stored in mSlots
137 void validateConsistencyLocked() const;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800138#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -0700139
Dan Stoza3e96f192014-03-03 10:16:19 -0800140 // mAllocator is the connection to SurfaceFlinger that is used to allocate
141 // new GraphicBuffer objects.
142 sp<IGraphicBufferAlloc> mAllocator;
143
144 // mMutex is the mutex used to prevent concurrent access to the member
145 // variables of BufferQueueCore objects. It must be locked whenever any
146 // member variable is accessed.
Dan Stoza289ade12014-02-28 11:17:17 -0800147 mutable Mutex mMutex;
Dan Stoza3e96f192014-03-03 10:16:19 -0800148
149 // mIsAbandoned indicates that the BufferQueue will no longer be used to
150 // consume image buffers pushed to it using the IGraphicBufferProducer
151 // interface. It is initialized to false, and set to true in the
152 // consumerDisconnect method. A BufferQueue that is abandoned will return
153 // the NO_INIT error from all IGraphicBufferProducer methods capable of
154 // returning an error.
Dan Stoza289ade12014-02-28 11:17:17 -0800155 bool mIsAbandoned;
Dan Stoza3e96f192014-03-03 10:16:19 -0800156
157 // mConsumerControlledByApp indicates whether the connected consumer is
158 // controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800159 bool mConsumerControlledByApp;
Dan Stoza3e96f192014-03-03 10:16:19 -0800160
161 // mConsumerName is a string used to identify the BufferQueue in log
162 // messages. It is set by the IGraphicBufferConsumer::setConsumerName
163 // method.
Dan Stoza289ade12014-02-28 11:17:17 -0800164 String8 mConsumerName;
Dan Stoza3e96f192014-03-03 10:16:19 -0800165
166 // mConsumerListener is used to notify the connected consumer of
167 // asynchronous events that it may wish to react to. It is initially
168 // set to NULL and is written by consumerConnect and consumerDisconnect.
Dan Stoza289ade12014-02-28 11:17:17 -0800169 sp<IConsumerListener> mConsumerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800170
171 // mConsumerUsageBits contains flags that the consumer wants for
172 // GraphicBuffers.
Dan Stoza289ade12014-02-28 11:17:17 -0800173 uint32_t mConsumerUsageBits;
Dan Stoza3e96f192014-03-03 10:16:19 -0800174
175 // mConnectedApi indicates the producer API that is currently connected
176 // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
177 // by the connect and disconnect methods.
Dan Stoza289ade12014-02-28 11:17:17 -0800178 int mConnectedApi;
Dan Stoza3e96f192014-03-03 10:16:19 -0800179
180 // mConnectedProducerToken is used to set a binder death notification on
181 // the producer.
Dan Stozaf0eaf252014-03-21 13:05:51 -0700182 sp<IProducerListener> mConnectedProducerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800183
184 // mSlots is an array of buffer slots that must be mirrored on the producer
185 // side. This allows buffer ownership to be transferred between the producer
186 // and consumer without sending a GraphicBuffer over Binder. The entire
187 // array is initialized to NULL at construction time, and buffers are
188 // allocated for a slot when requestBuffer is called with that slot's index.
189 BufferQueueDefs::SlotsType mSlots;
190
191 // mQueue is a FIFO of queued buffers used in synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800192 Fifo mQueue;
Dan Stoza3e96f192014-03-03 10:16:19 -0800193
Dan Stoza0de7ea72015-04-23 13:20:51 -0700194 // mFreeSlots contains all of the slots which are FREE and do not currently
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800195 // have a buffer attached.
Dan Stoza0de7ea72015-04-23 13:20:51 -0700196 std::set<int> mFreeSlots;
197
198 // mFreeBuffers contains all of the slots which are FREE and currently have
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800199 // a buffer attached.
Dan Stoza0de7ea72015-04-23 13:20:51 -0700200 std::list<int> mFreeBuffers;
201
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800202 // mUnusedSlots contains all slots that are currently unused. They should be
203 // free and not have a buffer attached.
204 std::list<int> mUnusedSlots;
205
206 // mActiveBuffers contains all slots which have a non-FREE buffer attached.
207 std::set<int> mActiveBuffers;
208
Dan Stoza3e96f192014-03-03 10:16:19 -0800209 // mDequeueCondition is a condition variable used for dequeueBuffer in
210 // synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800211 mutable Condition mDequeueCondition;
Dan Stoza3e96f192014-03-03 10:16:19 -0800212
Dan Stoza3e96f192014-03-03 10:16:19 -0800213 // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
214 // block. This flag is set during connect when both the producer and
215 // consumer are controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800216 bool mDequeueBufferCannotBlock;
Dan Stoza3e96f192014-03-03 10:16:19 -0800217
218 // mDefaultBufferFormat can be set so it will override the buffer format
219 // when it isn't specified in dequeueBuffer.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800220 PixelFormat mDefaultBufferFormat;
Dan Stoza3e96f192014-03-03 10:16:19 -0800221
222 // mDefaultWidth holds the default width of allocated buffers. It is used
223 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800224 uint32_t mDefaultWidth;
Dan Stoza3e96f192014-03-03 10:16:19 -0800225
226 // mDefaultHeight holds the default height of allocated buffers. It is used
227 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800228 uint32_t mDefaultHeight;
Dan Stoza3e96f192014-03-03 10:16:19 -0800229
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800230 // mDefaultBufferDataSpace holds the default dataSpace of queued buffers.
231 // It is used in queueBuffer if a dataspace of 0 (HAL_DATASPACE_UNKNOWN)
232 // is specified.
233 android_dataspace mDefaultBufferDataSpace;
234
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700235 // mMaxBufferCount is the limit on the number of buffers that will be
236 // allocated at one time. This limit can be set by the consumer.
237 int mMaxBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800238
239 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
240 // acquire at one time. It defaults to 1, and can be changed by the consumer
241 // via setMaxAcquiredBufferCount, but this may only be done while no
242 // producer is connected to the BufferQueue. This value is used to derive
243 // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800244 int mMaxAcquiredBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800245
Pablo Ceballosfa455352015-08-12 17:47:47 -0700246 // mMaxDequeuedBufferCount is the number of buffers that the producer may
247 // dequeue at one time. It defaults to 1, and can be changed by the producer
248 // via setMaxDequeuedBufferCount.
249 int mMaxDequeuedBufferCount;
250
Dan Stoza3e96f192014-03-03 10:16:19 -0800251 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
252 // when something causes all buffers to be freed (e.g., changing the buffer
253 // count).
Dan Stoza289ade12014-02-28 11:17:17 -0800254 bool mBufferHasBeenQueued;
Dan Stoza3e96f192014-03-03 10:16:19 -0800255
256 // mFrameCounter is the free running counter, incremented on every
257 // successful queueBuffer call and buffer allocation.
Dan Stoza289ade12014-02-28 11:17:17 -0800258 uint64_t mFrameCounter;
Dan Stoza3e96f192014-03-03 10:16:19 -0800259
260 // mTransformHint is used to optimize for screen rotations.
Dan Stoza289ade12014-02-28 11:17:17 -0800261 uint32_t mTransformHint;
262
Jesse Hall399184a2014-03-03 15:42:54 -0800263 // mSidebandStream is a handle to the sideband buffer stream, if any
264 sp<NativeHandle> mSidebandStream;
265
Antoine Labour78014f32014-07-15 21:17:03 -0700266 // mIsAllocating indicates whether a producer is currently trying to allocate buffers (which
267 // releases mMutex while doing the allocation proper). Producers should not modify any of the
268 // FREE slots while this is true. mIsAllocatingCondition is signaled when this value changes to
269 // false.
270 bool mIsAllocating;
271
272 // mIsAllocatingCondition is a condition variable used by producers to wait until mIsAllocating
273 // becomes false.
274 mutable Condition mIsAllocatingCondition;
Dan Stoza9de72932015-04-16 17:28:43 -0700275
276 // mAllowAllocation determines whether dequeueBuffer is allowed to allocate
277 // new buffers
278 bool mAllowAllocation;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800279
280 // mBufferAge tracks the age of the contents of the most recently dequeued
281 // buffer as the number of frames that have elapsed since it was last queued
282 uint64_t mBufferAge;
Dan Stozaecc50402015-04-28 14:42:06 -0700283
Dan Stoza812ed062015-06-02 15:45:22 -0700284 // mGenerationNumber stores the current generation number of the attached
285 // producer. Any attempt to attach a buffer with a different generation
286 // number will fail.
287 uint32_t mGenerationNumber;
288
Pablo Ceballosfa455352015-08-12 17:47:47 -0700289 // mAsyncMode indicates whether or not async mode is enabled.
290 // In async mode an extra buffer will be allocated to allow the producer to
291 // enqueue buffers without blocking.
292 bool mAsyncMode;
293
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700294 // mSharedBufferMode indicates whether or not shared buffer mode is enabled.
295 bool mSharedBufferMode;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700297 // When shared buffer mode is enabled, this indicates whether the consumer
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800298 // should acquire buffers even if BufferQueue doesn't indicate that they are
299 // available.
300 bool mAutoRefresh;
301
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700302 // When shared buffer mode is enabled, this tracks which slot contains the
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700303 // shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700304 int mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700305
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700306 // Cached data about the shared buffer in shared buffer mode
307 struct SharedBufferCache {
308 SharedBufferCache(Rect _crop, uint32_t _transform, int _scalingMode,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700309 android_dataspace _dataspace)
310 : crop(_crop),
311 transform(_transform),
312 scalingMode(_scalingMode),
313 dataspace(_dataspace) {
314 };
315
316 Rect crop;
317 uint32_t transform;
318 uint32_t scalingMode;
319 android_dataspace dataspace;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700320 } mSharedBufferCache;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700321
Dan Stoza50101d02016-04-07 16:53:23 -0700322 // The slot of the last queued buffer
323 int mLastQueuedSlot;
324
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700325 const uint64_t mUniqueId;
326
Dan Stoza289ade12014-02-28 11:17:17 -0800327}; // class BufferQueueCore
328
329} // namespace android
330
331#endif