blob: b1c730a5877d370cd0795969c8dfea592f855a89 [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>
Dan Stozae77c7662016-05-13 11:37:28 -070023#include <gui/OccupancyTracker.h>
Dan Stoza289ade12014-02-28 11:17:17 -080024
25#include <utils/Condition.h>
26#include <utils/Mutex.h>
Jesse Hall399184a2014-03-03 15:42:54 -080027#include <utils/NativeHandle.h>
Dan Stoza289ade12014-02-28 11:17:17 -080028#include <utils/RefBase.h>
29#include <utils/String8.h>
30#include <utils/StrongPointer.h>
31#include <utils/Trace.h>
32#include <utils/Vector.h>
33
Dan Stoza0de7ea72015-04-23 13:20:51 -070034#include <list>
35#include <set>
36
Dan Albert7d831872014-09-09 09:21:28 -070037#define BQ_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
38#define BQ_LOGD(x, ...) ALOGD("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
39#define BQ_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
40#define BQ_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
41#define BQ_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
Dan Stoza289ade12014-02-28 11:17:17 -080042
43#define ATRACE_BUFFER_INDEX(index) \
44 if (ATRACE_ENABLED()) { \
45 char ___traceBuf[1024]; \
46 snprintf(___traceBuf, 1024, "%s: %d", \
47 mCore->mConsumerName.string(), (index)); \
48 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
49 }
50
51namespace android {
52
Dan Stoza289ade12014-02-28 11:17:17 -080053class IConsumerListener;
54class IGraphicBufferAlloc;
Dan Stozaf0eaf252014-03-21 13:05:51 -070055class IProducerListener;
Dan Stoza289ade12014-02-28 11:17:17 -080056
57class BufferQueueCore : public virtual RefBase {
58
59 friend class BufferQueueProducer;
60 friend class BufferQueueConsumer;
61
62public:
Dan Stoza289ade12014-02-28 11:17:17 -080063 // Used as a placeholder slot number when the value isn't pointing to an
64 // existing buffer.
Dan Stoza1c87e472015-03-13 14:40:34 -070065 enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
Dan Stoza289ade12014-02-28 11:17:17 -080066
67 // We reserve two slots in order to guarantee that the producer and
68 // consumer can run asynchronously.
Dan Stoza3e96f192014-03-03 10:16:19 -080069 enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 };
Dan Stoza289ade12014-02-28 11:17:17 -080070
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080071 enum {
72 // The API number used to indicate the currently connected producer
73 CURRENTLY_CONNECTED_API = -1,
74
75 // The API number used to indicate that no producer is connected
76 NO_CONNECTED_API = 0,
77 };
Dan Stoza289ade12014-02-28 11:17:17 -080078
Dan Stoza289ade12014-02-28 11:17:17 -080079 typedef Vector<BufferItem> Fifo;
80
81 // BufferQueueCore manages a pool of gralloc memory slots to be used by
82 // producers and consumers. allocator is used to allocate all the needed
83 // gralloc buffers.
Dan Stoza70982a52016-01-11 23:40:44 +000084 BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL);
Dan Stoza289ade12014-02-28 11:17:17 -080085 virtual ~BufferQueueCore();
86
87private:
Dan Stoza3e96f192014-03-03 10:16:19 -080088 // Dump our state in a string
Colin Crossdc782512016-09-26 18:10:16 -070089 void dumpState(String8& result, const char* prefix) const;
Dan Stoza289ade12014-02-28 11:17:17 -080090
Dan Stoza3e96f192014-03-03 10:16:19 -080091 // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
92 // that must remain in a state other than DEQUEUED. The async parameter
93 // tells whether we're in asynchronous mode.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070094 int getMinUndequeuedBufferCountLocked() const;
Dan Stoza3e96f192014-03-03 10:16:19 -080095
96 // getMinMaxBufferCountLocked returns the minimum number of buffers allowed
97 // given the current BufferQueue state. The async parameter tells whether
98 // we're in asynchonous mode.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070099 int getMinMaxBufferCountLocked() const;
Dan Stoza3e96f192014-03-03 10:16:19 -0800100
101 // getMaxBufferCountLocked returns the maximum number of buffers that can be
102 // allocated at once. This value depends on the following member variables:
103 //
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700104 // mMaxDequeuedBufferCount
Dan Stoza3e96f192014-03-03 10:16:19 -0800105 // mMaxAcquiredBufferCount
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700106 // mMaxBufferCount
107 // mAsyncMode
108 // mDequeueBufferCannotBlock
Dan Stoza3e96f192014-03-03 10:16:19 -0800109 //
110 // Any time one of these member variables is changed while a producer is
111 // connected, mDequeueCondition must be broadcast.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700112 int getMaxBufferCountLocked() const;
Dan Stoza3e96f192014-03-03 10:16:19 -0800113
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800114 // This performs the same computation but uses the given arguments instead
115 // of the member variables for mMaxBufferCount, mAsyncMode, and
116 // mDequeueBufferCannotBlock.
117 int getMaxBufferCountLocked(bool asyncMode,
118 bool dequeueBufferCannotBlock, int maxBufferCount) const;
119
120 // clearBufferSlotLocked frees the GraphicBuffer and sync resources for the
Dan Stoza3e96f192014-03-03 10:16:19 -0800121 // given slot.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800122 void clearBufferSlotLocked(int slot);
Dan Stoza3e96f192014-03-03 10:16:19 -0800123
124 // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700125 // all slots, even if they're currently dequeued, queued, or acquired.
Dan Stoza289ade12014-02-28 11:17:17 -0800126 void freeAllBuffersLocked();
Dan Stoza3e96f192014-03-03 10:16:19 -0800127
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700128 // discardFreeBuffersLocked releases all currently-free buffers held by the
129 // queue, in order to reduce the memory consumption of the queue to the
130 // minimum possible without discarding data.
131 void discardFreeBuffersLocked();
132
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800133 // If delta is positive, makes more slots available. If negative, takes
134 // away slots. Returns false if the request can't be met.
135 bool adjustAvailableSlotsLocked(int delta);
Dan Stoza289ade12014-02-28 11:17:17 -0800136
Antoine Labour78014f32014-07-15 21:17:03 -0700137 // waitWhileAllocatingLocked blocks until mIsAllocating is false.
138 void waitWhileAllocatingLocked() const;
139
Pablo Ceballos9e314332016-01-12 13:49:19 -0800140#if DEBUG_ONLY_CODE
Dan Stoza0de7ea72015-04-23 13:20:51 -0700141 // validateConsistencyLocked ensures that the free lists are in sync with
142 // the information stored in mSlots
143 void validateConsistencyLocked() const;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800144#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -0700145
Dan Stoza3e96f192014-03-03 10:16:19 -0800146 // mAllocator is the connection to SurfaceFlinger that is used to allocate
147 // new GraphicBuffer objects.
148 sp<IGraphicBufferAlloc> mAllocator;
149
150 // mMutex is the mutex used to prevent concurrent access to the member
151 // variables of BufferQueueCore objects. It must be locked whenever any
152 // member variable is accessed.
Dan Stoza289ade12014-02-28 11:17:17 -0800153 mutable Mutex mMutex;
Dan Stoza3e96f192014-03-03 10:16:19 -0800154
155 // mIsAbandoned indicates that the BufferQueue will no longer be used to
156 // consume image buffers pushed to it using the IGraphicBufferProducer
157 // interface. It is initialized to false, and set to true in the
158 // consumerDisconnect method. A BufferQueue that is abandoned will return
159 // the NO_INIT error from all IGraphicBufferProducer methods capable of
160 // returning an error.
Dan Stoza289ade12014-02-28 11:17:17 -0800161 bool mIsAbandoned;
Dan Stoza3e96f192014-03-03 10:16:19 -0800162
163 // mConsumerControlledByApp indicates whether the connected consumer is
164 // controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800165 bool mConsumerControlledByApp;
Dan Stoza3e96f192014-03-03 10:16:19 -0800166
167 // mConsumerName is a string used to identify the BufferQueue in log
168 // messages. It is set by the IGraphicBufferConsumer::setConsumerName
169 // method.
Dan Stoza289ade12014-02-28 11:17:17 -0800170 String8 mConsumerName;
Dan Stoza3e96f192014-03-03 10:16:19 -0800171
172 // mConsumerListener is used to notify the connected consumer of
173 // asynchronous events that it may wish to react to. It is initially
174 // set to NULL and is written by consumerConnect and consumerDisconnect.
Dan Stoza289ade12014-02-28 11:17:17 -0800175 sp<IConsumerListener> mConsumerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800176
177 // mConsumerUsageBits contains flags that the consumer wants for
178 // GraphicBuffers.
Dan Stoza289ade12014-02-28 11:17:17 -0800179 uint32_t mConsumerUsageBits;
Dan Stoza3e96f192014-03-03 10:16:19 -0800180
181 // mConnectedApi indicates the producer API that is currently connected
182 // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
183 // by the connect and disconnect methods.
Dan Stoza289ade12014-02-28 11:17:17 -0800184 int mConnectedApi;
Robert Carr97b9c862016-09-08 13:54:35 -0700185 // PID of the process which last successfully called connect(...)
186 pid_t mConnectedPid;
Dan Stoza3e96f192014-03-03 10:16:19 -0800187
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -0700188 // mLinkedToDeath is used to set a binder death notification on
Dan Stoza3e96f192014-03-03 10:16:19 -0800189 // the producer.
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -0700190 sp<IProducerListener> mLinkedToDeath;
191
192 // mConnectedProducerListener is used to handle the onBufferReleased
193 // notification.
Dan Stozaf0eaf252014-03-21 13:05:51 -0700194 sp<IProducerListener> mConnectedProducerListener;
Dan Stoza3e96f192014-03-03 10:16:19 -0800195
196 // mSlots is an array of buffer slots that must be mirrored on the producer
197 // side. This allows buffer ownership to be transferred between the producer
198 // and consumer without sending a GraphicBuffer over Binder. The entire
199 // array is initialized to NULL at construction time, and buffers are
200 // allocated for a slot when requestBuffer is called with that slot's index.
201 BufferQueueDefs::SlotsType mSlots;
202
203 // mQueue is a FIFO of queued buffers used in synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800204 Fifo mQueue;
Dan Stoza3e96f192014-03-03 10:16:19 -0800205
Dan Stoza0de7ea72015-04-23 13:20:51 -0700206 // mFreeSlots contains all of the slots which are FREE and do not currently
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800207 // have a buffer attached.
Dan Stoza0de7ea72015-04-23 13:20:51 -0700208 std::set<int> mFreeSlots;
209
210 // mFreeBuffers contains all of the slots which are FREE and currently have
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800211 // a buffer attached.
Dan Stoza0de7ea72015-04-23 13:20:51 -0700212 std::list<int> mFreeBuffers;
213
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800214 // mUnusedSlots contains all slots that are currently unused. They should be
215 // free and not have a buffer attached.
216 std::list<int> mUnusedSlots;
217
218 // mActiveBuffers contains all slots which have a non-FREE buffer attached.
219 std::set<int> mActiveBuffers;
220
Dan Stoza3e96f192014-03-03 10:16:19 -0800221 // mDequeueCondition is a condition variable used for dequeueBuffer in
222 // synchronous mode.
Dan Stoza289ade12014-02-28 11:17:17 -0800223 mutable Condition mDequeueCondition;
Dan Stoza3e96f192014-03-03 10:16:19 -0800224
Dan Stoza3e96f192014-03-03 10:16:19 -0800225 // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
226 // block. This flag is set during connect when both the producer and
227 // consumer are controlled by the application.
Dan Stoza289ade12014-02-28 11:17:17 -0800228 bool mDequeueBufferCannotBlock;
Dan Stoza3e96f192014-03-03 10:16:19 -0800229
230 // mDefaultBufferFormat can be set so it will override the buffer format
231 // when it isn't specified in dequeueBuffer.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800232 PixelFormat mDefaultBufferFormat;
Dan Stoza3e96f192014-03-03 10:16:19 -0800233
234 // mDefaultWidth holds the default width of allocated buffers. It is used
235 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800236 uint32_t mDefaultWidth;
Dan Stoza3e96f192014-03-03 10:16:19 -0800237
238 // mDefaultHeight holds the default height of allocated buffers. It is used
239 // in dequeueBuffer if a width and height of 0 are specified.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800240 uint32_t mDefaultHeight;
Dan Stoza3e96f192014-03-03 10:16:19 -0800241
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800242 // mDefaultBufferDataSpace holds the default dataSpace of queued buffers.
243 // It is used in queueBuffer if a dataspace of 0 (HAL_DATASPACE_UNKNOWN)
244 // is specified.
245 android_dataspace mDefaultBufferDataSpace;
246
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700247 // mMaxBufferCount is the limit on the number of buffers that will be
248 // allocated at one time. This limit can be set by the consumer.
249 int mMaxBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800250
251 // mMaxAcquiredBufferCount is the number of buffers that the consumer may
252 // acquire at one time. It defaults to 1, and can be changed by the consumer
253 // via setMaxAcquiredBufferCount, but this may only be done while no
254 // producer is connected to the BufferQueue. This value is used to derive
255 // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
Dan Stoza289ade12014-02-28 11:17:17 -0800256 int mMaxAcquiredBufferCount;
Dan Stoza3e96f192014-03-03 10:16:19 -0800257
Pablo Ceballosfa455352015-08-12 17:47:47 -0700258 // mMaxDequeuedBufferCount is the number of buffers that the producer may
259 // dequeue at one time. It defaults to 1, and can be changed by the producer
260 // via setMaxDequeuedBufferCount.
261 int mMaxDequeuedBufferCount;
262
Dan Stoza3e96f192014-03-03 10:16:19 -0800263 // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
264 // when something causes all buffers to be freed (e.g., changing the buffer
265 // count).
Dan Stoza289ade12014-02-28 11:17:17 -0800266 bool mBufferHasBeenQueued;
Dan Stoza3e96f192014-03-03 10:16:19 -0800267
268 // mFrameCounter is the free running counter, incremented on every
269 // successful queueBuffer call and buffer allocation.
Dan Stoza289ade12014-02-28 11:17:17 -0800270 uint64_t mFrameCounter;
Dan Stoza3e96f192014-03-03 10:16:19 -0800271
272 // mTransformHint is used to optimize for screen rotations.
Dan Stoza289ade12014-02-28 11:17:17 -0800273 uint32_t mTransformHint;
274
Jesse Hall399184a2014-03-03 15:42:54 -0800275 // mSidebandStream is a handle to the sideband buffer stream, if any
276 sp<NativeHandle> mSidebandStream;
277
Antoine Labour78014f32014-07-15 21:17:03 -0700278 // mIsAllocating indicates whether a producer is currently trying to allocate buffers (which
279 // releases mMutex while doing the allocation proper). Producers should not modify any of the
280 // FREE slots while this is true. mIsAllocatingCondition is signaled when this value changes to
281 // false.
282 bool mIsAllocating;
283
284 // mIsAllocatingCondition is a condition variable used by producers to wait until mIsAllocating
285 // becomes false.
286 mutable Condition mIsAllocatingCondition;
Dan Stoza9de72932015-04-16 17:28:43 -0700287
288 // mAllowAllocation determines whether dequeueBuffer is allowed to allocate
289 // new buffers
290 bool mAllowAllocation;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800291
292 // mBufferAge tracks the age of the contents of the most recently dequeued
293 // buffer as the number of frames that have elapsed since it was last queued
294 uint64_t mBufferAge;
Dan Stozaecc50402015-04-28 14:42:06 -0700295
Dan Stoza812ed062015-06-02 15:45:22 -0700296 // mGenerationNumber stores the current generation number of the attached
297 // producer. Any attempt to attach a buffer with a different generation
298 // number will fail.
299 uint32_t mGenerationNumber;
300
Pablo Ceballosfa455352015-08-12 17:47:47 -0700301 // mAsyncMode indicates whether or not async mode is enabled.
302 // In async mode an extra buffer will be allocated to allow the producer to
303 // enqueue buffers without blocking.
304 bool mAsyncMode;
305
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700306 // mSharedBufferMode indicates whether or not shared buffer mode is enabled.
307 bool mSharedBufferMode;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700308
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700309 // When shared buffer mode is enabled, this indicates whether the consumer
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800310 // should acquire buffers even if BufferQueue doesn't indicate that they are
311 // available.
312 bool mAutoRefresh;
313
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700314 // When shared buffer mode is enabled, this tracks which slot contains the
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700315 // shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700316 int mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700317
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700318 // Cached data about the shared buffer in shared buffer mode
319 struct SharedBufferCache {
Colin Cross6c5a17d2016-09-27 14:12:48 -0700320 SharedBufferCache(Rect _crop, uint32_t _transform,
321 uint32_t _scalingMode, android_dataspace _dataspace)
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700322 : crop(_crop),
323 transform(_transform),
324 scalingMode(_scalingMode),
325 dataspace(_dataspace) {
Colin Cross17576de2016-09-26 13:07:06 -0700326 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700327
328 Rect crop;
329 uint32_t transform;
330 uint32_t scalingMode;
331 android_dataspace dataspace;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700332 } mSharedBufferCache;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700333
Dan Stoza50101d02016-04-07 16:53:23 -0700334 // The slot of the last queued buffer
335 int mLastQueuedSlot;
336
Dan Stozae77c7662016-05-13 11:37:28 -0700337 OccupancyTracker mOccupancyTracker;
338
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700339 const uint64_t mUniqueId;
340
Dan Stoza289ade12014-02-28 11:17:17 -0800341}; // class BufferQueueCore
342
343} // namespace android
344
345#endif