blob: 4681c9ecbe9a57ec4e270742ff2e6b5e014acc7f [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
Dan Stoza3e96f192014-03-03 10:16:19 -080017#define LOG_TAG "BufferQueueConsumer"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
Pablo Ceballos9e314332016-01-12 13:49:19 -080021#if DEBUG_ONLY_CODE
22#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
23#else
24#define VALIDATE_CONSISTENCY()
25#endif
26
Jim Shargo90842182024-11-14 00:49:27 +000027#define EGL_EGLEXT_PROTOTYPES
28#include <EGL/egl.h>
29#include <EGL/eglext.h>
30
Dan Stoza289ade12014-02-28 11:17:17 -080031#include <gui/BufferItem.h>
32#include <gui/BufferQueueConsumer.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070035#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070036#include <gui/TraceUtils.h>
Dan Stoza289ade12014-02-28 11:17:17 -080037
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080038#include <private/gui/BufferQueueThreadState.h>
Kiyoung Kim5c08e302023-11-10 16:35:13 +090039#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Pablo Ceballos88f69282016-02-11 18:01:49 -080040#include <binder/PermissionCache.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090041#endif
Pablo Ceballos88f69282016-02-11 18:01:49 -080042
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070043#include <system/window.h>
44
Sungtak Leecd217472024-07-19 17:17:40 +000045#include <com_android_graphics_libgui_flags.h>
46
Jim Shargofd4edb22025-03-05 17:38:19 +000047#include <inttypes.h>
48#include <pwd.h>
49#include <sys/types.h>
50#include <optional>
51
Dan Stoza289ade12014-02-28 11:17:17 -080052namespace android {
53
Iris Chang430193f2019-12-04 16:25:46 +080054// Macros for include BufferQueueCore information in log messages
55#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000056 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080057 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
58 ##__VA_ARGS__)
59#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000060 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080061 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
62 ##__VA_ARGS__)
63#define BQ_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000064 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080065 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
66 ##__VA_ARGS__)
67#define BQ_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000068 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080069 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
70 ##__VA_ARGS__)
71#define BQ_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000072 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080073 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
74 ##__VA_ARGS__)
75
Chong Zhang62493092020-01-15 16:04:47 -080076ConsumerListener::~ConsumerListener() = default;
77
Dan Stoza289ade12014-02-28 11:17:17 -080078BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
79 mCore(core),
80 mSlots(core->mSlots),
81 mConsumerName() {}
82
83BufferQueueConsumer::~BufferQueueConsumer() {}
84
85status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070086 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080087 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080088
Lajos Molnar5f920c12015-07-13 16:04:24 -070089 int numDroppedBuffers = 0;
90 sp<IProducerListener> listener;
91 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020092 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070093
94 // Check that the consumer doesn't currently have the maximum number of
95 // buffers acquired. We allow the max buffer count to be exceeded by one
96 // buffer so that the consumer can successfully set up the newly acquired
97 // buffer before releasing the old one.
98 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080099 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700100 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700101 ++numAcquiredBuffers;
102 }
Dan Stoza289ade12014-02-28 11:17:17 -0800103 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800104 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
105 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
106 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
107 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700108 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
109 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
110 return INVALID_OPERATION;
111 }
Dan Stoza289ade12014-02-28 11:17:17 -0800112
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700113 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
114 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700115 BufferQueueCore::INVALID_BUFFER_SLOT;
116
Lajos Molnar5f920c12015-07-13 16:04:24 -0700117 // In asynchronous mode the list is guaranteed to be one buffer deep,
118 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700119 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700120 return NO_BUFFER_AVAILABLE;
121 }
Dan Stoza289ade12014-02-28 11:17:17 -0800122
Lajos Molnar5f920c12015-07-13 16:04:24 -0700123 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800124
Lajos Molnar5f920c12015-07-13 16:04:24 -0700125 // If expectedPresent is specified, we may not want to return a buffer yet.
126 // If it's specified and there's more than one buffer queued, we may want
127 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700128 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700129 // since in that case we'll just return the shared buffer.
130 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700131 // The 'expectedPresent' argument indicates when the buffer is expected
132 // to be presented on-screen. If the buffer's desired present time is
133 // earlier (less) than expectedPresent -- meaning it will be displayed
134 // on time or possibly late if we show it as soon as possible -- we
135 // acquire and return it. If we don't want to display it until after the
136 // expectedPresent time, we return PRESENT_LATER without acquiring it.
137 //
138 // To be safe, we don't defer acquisition if expectedPresent is more
139 // than one second in the future beyond the desired present time
140 // (i.e., we'd be holding the buffer for a long time).
141 //
142 // NOTE: Code assumes monotonic time values from the system clock
143 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800144
Lajos Molnar5f920c12015-07-13 16:04:24 -0700145 // Start by checking to see if we can drop frames. We skip this check if
146 // the timestamps are being auto-generated by Surface. If the app isn't
147 // generating timestamps explicitly, it probably doesn't want frames to
148 // be discarded based on them.
149 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
150 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700151
Lajos Molnar5f920c12015-07-13 16:04:24 -0700152 // If dropping entry[0] would leave us with a buffer that the
153 // consumer is not yet ready for, don't drop it.
154 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
155 break;
156 }
157
158 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
159 // additional criterion here: we only drop the earlier buffer if our
160 // desiredPresent falls within +/- 1 second of the expected present.
161 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
162 // relative timestamp), which normally mean "ignore the timestamp
163 // and acquire immediately", would cause us to drop frames.
164 //
165 // We may want to add an additional criterion: don't drop the
166 // earlier buffer if entry[1]'s fence hasn't signaled yet.
167 nsecs_t desiredPresent = bufferItem.mTimestamp;
168 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
169 desiredPresent > expectedPresent) {
170 // This buffer is set to display in the near future, or
171 // desiredPresent is garbage. Either way we don't want to drop
172 // the previous buffer just to get this on the screen sooner.
173 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
174 PRId64 " (%" PRId64 ") now=%" PRId64,
175 desiredPresent, expectedPresent,
176 desiredPresent - expectedPresent,
177 systemTime(CLOCK_MONOTONIC));
178 break;
179 }
180
181 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
182 " size=%zu",
183 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800184
185 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700186 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700187 mSlots[front->mSlot].mBufferState.freeQueued();
188
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700189 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700190 // still be around. Mark it as no longer shared if this
191 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700192 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700193 mSlots[front->mSlot].mBufferState.isFree()) {
194 mSlots[front->mSlot].mBufferState.mShared = false;
195 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800196
197 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700198 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800199 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700200 mCore->mFreeBuffers.push_back(front->mSlot);
201 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800202
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700203 if (mCore->mBufferReleasedCbEnabled) {
204 listener = mCore->mConnectedProducerListener;
205 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700206 ++numDroppedBuffers;
207 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800208
Lajos Molnar5f920c12015-07-13 16:04:24 -0700209 mCore->mQueue.erase(front);
210 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700211 }
212
Lajos Molnar5f920c12015-07-13 16:04:24 -0700213 // See if the front buffer is ready to be acquired
214 nsecs_t desiredPresent = front->mTimestamp;
215 bool bufferIsDue = desiredPresent <= expectedPresent ||
216 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
217 bool consumerIsReady = maxFrameNumber > 0 ?
218 front->mFrameNumber <= maxFrameNumber : true;
219 if (!bufferIsDue || !consumerIsReady) {
220 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
221 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
222 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700223 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800224 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700225 systemTime(CLOCK_MONOTONIC),
226 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700227 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700228 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800229 }
230
Lajos Molnar5f920c12015-07-13 16:04:24 -0700231 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
232 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800233 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700234 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800235 }
236
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700237 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
238
239 if (sharedBufferAvailable && mCore->mQueue.empty()) {
240 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200241 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700242
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700243 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700244
245 // Recreate the BufferItem for the shared buffer from the data that
246 // was cached when it was last queued.
247 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
248 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700249 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700250 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
251 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700252 ~static_cast<uint32_t>(
253 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700254 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
255 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700256 outBuffer->mFrameNumber = mCore->mFrameCounter;
257 outBuffer->mSlot = slot;
258 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
259 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700260 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700261 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
262 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700263 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800264 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700265 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800266 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800267 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
268 BQ_LOGV("acquireBuffer: front buffer is not droppable");
269 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700270 } else {
271 slot = front->mSlot;
272 *outBuffer = *front;
273 }
274
Lajos Molnar5f920c12015-07-13 16:04:24 -0700275 ATRACE_BUFFER_INDEX(slot);
276
277 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700278 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800279
280 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700281 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700282 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700283 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700284 // the queue is empty and the BufferItem is created above.
285 if (mCore->mQueue.empty()) {
286 mSlots[slot].mBufferState.acquireNotInQueue();
287 } else {
288 mSlots[slot].mBufferState.acquire();
289 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700290 mSlots[slot].mFence = Fence::NO_FENCE;
291 }
292
293 // If the buffer has previously been acquired by the consumer, set
294 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
295 // on the consumer side
296 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700297 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700298 }
299
300 mCore->mQueue.erase(front);
301
302 // We might have freed a slot while dropping old buffers, or the producer
303 // may be blocked waiting for the number of buffers in the queue to
304 // decrease.
Patrick Williams078d7362024-08-27 10:20:39 -0500305#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
306 mCore->notifyBufferReleased();
307#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200308 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500309#endif
Lajos Molnar5f920c12015-07-13 16:04:24 -0700310
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000311 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800312#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700313 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800314#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800315 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800316 }
317
Yi Kong48a619f2018-06-05 16:34:59 -0700318 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700319 for (int i = 0; i < numDroppedBuffers; ++i) {
320 listener->onBufferReleased();
321 }
Dan Stoza289ade12014-02-28 11:17:17 -0800322 }
323
Dan Stoza289ade12014-02-28 11:17:17 -0800324 return NO_ERROR;
325}
326
Dan Stoza9f3053d2014-03-06 15:14:33 -0800327status_t BufferQueueConsumer::detachBuffer(int slot) {
328 ATRACE_CALL();
329 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700330 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000331 sp<IProducerListener> listener;
332 {
333 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000335 if (mCore->mIsAbandoned) {
336 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
337 return NO_INIT;
338 }
339
340 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
341 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
342 return BAD_VALUE;
343 }
344
Jim Shargo2e614a42024-10-02 19:31:53 +0000345 const int totalSlotCount = mCore->getTotalSlotCountLocked();
346 if (slot < 0 || slot >= totalSlotCount) {
347 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000348 return BAD_VALUE;
349 } else if (!mSlots[slot].mBufferState.isAcquired()) {
350 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
351 "(state = %s)", slot, mSlots[slot].mBufferState.string());
352 return BAD_VALUE;
353 }
354 if (mCore->mBufferReleasedCbEnabled) {
355 listener = mCore->mConnectedProducerListener;
356 }
357
358 mSlots[slot].mBufferState.detachConsumer();
359 mCore->mActiveBuffers.erase(slot);
360 mCore->mFreeSlots.insert(slot);
361 mCore->clearBufferSlotLocked(slot);
Patrick Williams078d7362024-08-27 10:20:39 -0500362#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
363 mCore->notifyBufferReleased();
364#else
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000365 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500366#endif
367
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000368 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 }
370
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000371 if (listener) {
372 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800374 return NO_ERROR;
375}
376
377status_t BufferQueueConsumer::attachBuffer(int* outSlot,
378 const sp<android::GraphicBuffer>& buffer) {
379 ATRACE_CALL();
380
Yi Kong48a619f2018-06-05 16:34:59 -0700381 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700382 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800383 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700384 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700385 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386 return BAD_VALUE;
387 }
388
Sungtak Leecd217472024-07-19 17:17:40 +0000389 sp<IProducerListener> listener;
390 {
391 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800392
Sungtak Leecd217472024-07-19 17:17:40 +0000393 if (mCore->mSharedBufferMode) {
394 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
395 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800396 }
Sungtak Leecd217472024-07-19 17:17:40 +0000397
398 // Make sure we don't have too many acquired buffers
399 int numAcquiredBuffers = 0;
400 for (int s : mCore->mActiveBuffers) {
401 if (mSlots[s].mBufferState.isAcquired()) {
402 ++numAcquiredBuffers;
403 }
404 }
405
406 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
407 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
408 "(max %d)", numAcquiredBuffers,
409 mCore->mMaxAcquiredBufferCount);
410 return INVALID_OPERATION;
411 }
412
413 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
414 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
415 "[queue %u]", buffer->getGenerationNumber(),
416 mCore->mGenerationNumber);
417 return BAD_VALUE;
418 }
419
420 // Find a free slot to put the buffer into
421 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
422 if (!mCore->mFreeSlots.empty()) {
423 auto slot = mCore->mFreeSlots.begin();
424 found = *slot;
425 mCore->mFreeSlots.erase(slot);
426 } else if (!mCore->mFreeBuffers.empty()) {
427 found = mCore->mFreeBuffers.front();
428 mCore->mFreeBuffers.remove(found);
429 }
430 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
431 BQ_LOGE("attachBuffer: could not find free buffer slot");
432 return NO_MEMORY;
433 }
434
435#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
436 if (mCore->mBufferAttachedCbEnabled) {
437 listener = mCore->mConnectedProducerListener;
438 }
439#endif
440
441 mCore->mActiveBuffers.insert(found);
442 *outSlot = found;
443 ATRACE_BUFFER_INDEX(*outSlot);
444 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
445
446 mSlots[*outSlot].mGraphicBuffer = buffer;
447 mSlots[*outSlot].mBufferState.attachConsumer();
448 mSlots[*outSlot].mNeedsReallocation = true;
449 mSlots[*outSlot].mFence = Fence::NO_FENCE;
450 mSlots[*outSlot].mFrameNumber = 0;
451
452 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
453 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
454 // Binder traffic by not un/flattening the GraphicBuffer. However, it
455 // requires that the consumer maintain a cached copy of the slot <--> buffer
456 // mappings, which is why the consumer doesn't need the valid pointer on
457 // acquire.
458 //
459 // The StreamSplitter is one of the primary users of the attach/detach
460 // logic, and while it is running, all buffers it acquires are immediately
461 // detached, and all buffers it eventually releases are ones that were
462 // attached (as opposed to having been obtained from acquireBuffer), so it
463 // doesn't make sense to maintain the slot/buffer mappings, which would
464 // become invalid for every buffer during detach/attach. By setting this to
465 // false, the valid GraphicBuffer pointer will always be sent with acquire
466 // for attached buffers.
467 mSlots[*outSlot].mAcquireCalled = false;
468
469 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800470 }
471
Sungtak Leecd217472024-07-19 17:17:40 +0000472#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
473 if (listener != nullptr) {
474 listener->onBufferAttached();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800475 }
Sungtak Leecd217472024-07-19 17:17:40 +0000476#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -0700477
Dan Stoza9f3053d2014-03-06 15:14:33 -0800478 return NO_ERROR;
479}
480
Jim Shargodefc49e2024-11-15 01:06:41 +0000481#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
482status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
483 const sp<Fence>& releaseFence) {
484#else
Dan Stoza289ade12014-02-28 11:17:17 -0800485status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
486 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
487 EGLSyncKHR eglFence) {
Jim Shargodefc49e2024-11-15 01:06:41 +0000488#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800489 ATRACE_CALL();
490 ATRACE_BUFFER_INDEX(slot);
491
Jim Shargo2e614a42024-10-02 19:31:53 +0000492 const int totalSlotCount = mCore->getTotalSlotCountLocked();
493 if (slot < 0 || slot >= totalSlotCount) {
494 BQ_LOGE("releaseBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
495 return BAD_VALUE;
496 }
497 if (releaseFence == nullptr) {
498 BQ_LOGE("releaseBuffer: slot %d fence %p NULL", slot, releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800499 return BAD_VALUE;
500 }
501
Dan Stozad1c10362014-03-28 15:19:08 -0700502 sp<IProducerListener> listener;
503 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200504 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800505
Jim Shargo2e614a42024-10-02 19:31:53 +0000506 const int totalSlotCount = mCore->getTotalSlotCountLocked();
507 if (slot < 0 || slot >= totalSlotCount || releaseFence == nullptr) {
508 BQ_LOGE("releaseBuffer: slot %d out of range [0, %d) or fence %p NULL", slot,
509 totalSlotCount, releaseFence.get());
510 return BAD_VALUE;
511 }
512
Dan Stozad1c10362014-03-28 15:19:08 -0700513 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700514 // we can ignore this releaseBuffer for the old buffer.
515 // Ignore this for the shared buffer where the frame number can easily
516 // get out of sync due to the buffer being queued and acquired at the
517 // same time.
518 if (frameNumber != mSlots[slot].mFrameNumber &&
519 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700520 return STALE_BUFFER_SLOT;
521 }
Dan Stoza289ade12014-02-28 11:17:17 -0800522
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800523 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700524 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700525 "but its state was %s", slot,
526 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800527 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800528 }
Dan Stoza289ade12014-02-28 11:17:17 -0800529
Jim Shargo90842182024-11-14 00:49:27 +0000530#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800531 mSlots[slot].mEglDisplay = eglDisplay;
532 mSlots[slot].mEglFence = eglFence;
Jim Shargo90842182024-11-14 00:49:27 +0000533#endif
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800534 mSlots[slot].mFence = releaseFence;
535 mSlots[slot].mBufferState.release();
536
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700537 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800538 // still be around. Mark it as no longer shared if this
539 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700540 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800541 mSlots[slot].mBufferState.mShared = false;
542 }
543 // Don't put the shared buffer on the free list.
544 if (!mSlots[slot].mBufferState.isShared()) {
545 mCore->mActiveBuffers.erase(slot);
546 mCore->mFreeBuffers.push_back(slot);
547 }
548
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700549 if (mCore->mBufferReleasedCbEnabled) {
550 listener = mCore->mConnectedProducerListener;
551 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800552 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
553
Patrick Williams078d7362024-08-27 10:20:39 -0500554#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
555 mCore->notifyBufferReleased();
556#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200557 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500558#endif
559
Pablo Ceballos9e314332016-01-12 13:49:19 -0800560 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700561 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800562
Dan Stozad1c10362014-03-28 15:19:08 -0700563 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700564 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700565 listener->onBufferReleased();
566 }
Dan Stoza289ade12014-02-28 11:17:17 -0800567
568 return NO_ERROR;
569}
570
571status_t BufferQueueConsumer::connect(
572 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
573 ATRACE_CALL();
574
Yi Kong48a619f2018-06-05 16:34:59 -0700575 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700576 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800577 return BAD_VALUE;
578 }
579
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700580 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800581 controlledByApp ? "true" : "false");
582
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200583 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800584
585 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700586 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800587 return NO_INIT;
588 }
589
590 mCore->mConsumerListener = consumerListener;
591 mCore->mConsumerControlledByApp = controlledByApp;
592
593 return NO_ERROR;
594}
595
596status_t BufferQueueConsumer::disconnect() {
597 ATRACE_CALL();
598
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700599 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800600
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200601 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800602
Yi Kong48a619f2018-06-05 16:34:59 -0700603 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700604 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800605 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800606 }
607
608 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700609 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800610 mCore->mQueue.clear();
611 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700612 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Patrick Williams078d7362024-08-27 10:20:39 -0500613#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
614 mCore->notifyBufferReleased();
615#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200616 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500617#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800618 return NO_ERROR;
619}
620
Dan Stozafebd4f42014-04-09 16:14:51 -0700621status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800622 ATRACE_CALL();
623
Yi Kong48a619f2018-06-05 16:34:59 -0700624 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800625 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
626 return BAD_VALUE;
627 }
628
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200629 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800630
631 if (mCore->mIsAbandoned) {
632 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
633 return NO_INIT;
634 }
635
Dan Stozafebd4f42014-04-09 16:14:51 -0700636 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800637 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800638 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700639 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800640 }
641 }
642
643 // Remove from the mask queued buffers for which acquire has been called,
644 // since the consumer will not receive their buffer addresses and so must
645 // retain their cached information
646 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
647 while (current != mCore->mQueue.end()) {
648 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700649 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800650 }
651 ++current;
652 }
653
Dan Stozafebd4f42014-04-09 16:14:51 -0700654 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800655 *outSlotMask = mask;
656 return NO_ERROR;
657}
658
Jim Shargo2e614a42024-10-02 19:31:53 +0000659#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
660status_t BufferQueueConsumer::getReleasedBuffersExtended(std::vector<bool>* outSlotMask) {
661 ATRACE_CALL();
662
663 if (outSlotMask == nullptr) {
664 BQ_LOGE("getReleasedBuffersExtended: outSlotMask may not be NULL");
665 return BAD_VALUE;
666 }
667
668 std::lock_guard<std::mutex> lock(mCore->mMutex);
669
670 if (mCore->mIsAbandoned) {
671 BQ_LOGE("getReleasedBuffersExtended: BufferQueue has been abandoned");
672 return NO_INIT;
673 }
674
675 const int totalSlotCount = mCore->getTotalSlotCountLocked();
676 outSlotMask->resize(totalSlotCount);
677 for (int s = 0; s < totalSlotCount; ++s) {
678 (*outSlotMask)[s] = !mSlots[s].mAcquireCalled;
679 }
680
681 // Remove from the mask queued buffers for which acquire has been called,
682 // since the consumer will not receive their buffer addresses and so must
683 // retain their cached information
684 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
685 while (current != mCore->mQueue.end()) {
686 if (current->mAcquireCalled) {
687 (*outSlotMask)[current->mSlot] = false;
688 }
689 ++current;
690 }
691
692 return NO_ERROR;
693}
694#endif
695
Dan Stoza289ade12014-02-28 11:17:17 -0800696status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
697 uint32_t height) {
698 ATRACE_CALL();
699
700 if (width == 0 || height == 0) {
701 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
702 "height=%u)", width, height);
703 return BAD_VALUE;
704 }
705
706 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
707
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200708 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800709 mCore->mDefaultWidth = width;
710 mCore->mDefaultHeight = height;
711 return NO_ERROR;
712}
713
Jim Shargo2e614a42024-10-02 19:31:53 +0000714#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
715status_t BufferQueueConsumer::allowUnlimitedSlots(bool allowUnlimitedSlots) {
716 ATRACE_CALL();
717 BQ_LOGV("allowUnlimitedSlots: %d", allowUnlimitedSlots);
718 std::lock_guard<std::mutex> lock(mCore->mMutex);
719
720 if (mCore->mIsAbandoned) {
721 BQ_LOGE("allowUnlimitedSlots: BufferQueue has been abandoned");
722 return NO_INIT;
723 }
724
725 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
726 BQ_LOGE("allowUnlimitedSlots: BufferQueue already connected");
727 return INVALID_OPERATION;
728 }
729
730 mCore->mAllowExtendedSlotCount = allowUnlimitedSlots;
731
732 return OK;
733}
734#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
735
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700736status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800737 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800738
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700739 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
740 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
741 return BAD_VALUE;
742 }
Dan Stoza289ade12014-02-28 11:17:17 -0800743
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200744 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800745
Pablo Ceballos38273792016-03-02 01:38:10 +0000746 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
747 BQ_LOGE("setMaxBufferCount: producer is already connected");
748 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800749 }
750
Pablo Ceballos38273792016-03-02 01:38:10 +0000751 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
752 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
753 "mMaxAcquiredBufferCount (%d)", bufferCount,
754 mCore->mMaxAcquiredBufferCount);
755 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700756 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000757
758 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
759 mCore->mDequeueBufferCannotBlock, bufferCount) -
760 mCore->getMaxBufferCountLocked();
761 if (!mCore->adjustAvailableSlotsLocked(delta)) {
762 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
763 "available slots. Delta = %d", delta);
764 return BAD_VALUE;
765 }
766
767 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800768 return NO_ERROR;
769}
770
Jim Shargofd4edb22025-03-05 17:38:19 +0000771status_t BufferQueueConsumer::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
772 return setMaxAcquiredBufferCount(maxAcquiredBuffers, std::nullopt);
773}
774
Dan Stoza289ade12014-02-28 11:17:17 -0800775status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
Jim Shargofd4edb22025-03-05 17:38:19 +0000776 int maxAcquiredBuffers, std::optional<OnBufferReleasedCallback> onBuffersReleasedCallback) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700777 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800778
Jim Shargofd4edb22025-03-05 17:38:19 +0000779 std::optional<OnBufferReleasedCallback> callback;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800780 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200781 std::unique_lock<std::mutex> lock(mCore->mMutex);
Jim Shargo2e614a42024-10-02 19:31:53 +0000782
783 // We reserve two slots in order to guarantee that the producer and
784 // consumer can run asynchronously.
785 int maxMaxAcquiredBuffers =
786#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
787 mCore->getTotalSlotCountLocked() - 2;
788#else
789 BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS;
790#endif
791 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > maxMaxAcquiredBuffers) {
792 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d", maxAcquiredBuffers);
793 return BAD_VALUE;
794 }
795
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200796 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800797
Pablo Ceballos72daab62015-12-07 16:38:43 -0800798 if (mCore->mIsAbandoned) {
799 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
800 return NO_INIT;
801 }
802
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700803 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
804 return NO_ERROR;
805 }
806
Pablo Ceballos72daab62015-12-07 16:38:43 -0800807 // The new maxAcquiredBuffers count should not be violated by the number
808 // of currently acquired buffers
809 int acquiredCount = 0;
810 for (int slot : mCore->mActiveBuffers) {
811 if (mSlots[slot].mBufferState.isAcquired()) {
812 acquiredCount++;
813 }
814 }
815 if (acquiredCount > maxAcquiredBuffers) {
816 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
817 "count (%d) exceeds the current acquired buffer count (%d)",
818 maxAcquiredBuffers, acquiredCount);
819 return BAD_VALUE;
820 }
821
822 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
823 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
824 > mCore->mMaxBufferCount) {
825 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
826 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
827 maxAcquiredBuffers, mCore->mMaxBufferCount,
828 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
829 mCore->mDequeueBufferCannotBlock);
830 return BAD_VALUE;
831 }
832
833 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000834 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800835 return BAD_VALUE;
836 }
837
838 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
839 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
840 VALIDATE_CONSISTENCY();
Jim Shargofd4edb22025-03-05 17:38:19 +0000841 if (delta < 0) {
842 if (onBuffersReleasedCallback) {
843 callback = std::move(onBuffersReleasedCallback);
844 } else if (mCore->mBufferReleasedCbEnabled) {
845 callback = [listener = mCore->mConsumerListener]() {
846 listener->onBuffersReleased();
847 };
848 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800849 }
850 }
Jim Shargofd4edb22025-03-05 17:38:19 +0000851
Pablo Ceballos72daab62015-12-07 16:38:43 -0800852 // Call back without lock held
Jim Shargofd4edb22025-03-05 17:38:19 +0000853 if (callback) {
854 (*callback)();
Dan Stoza289ade12014-02-28 11:17:17 -0800855 }
856
Dan Stoza289ade12014-02-28 11:17:17 -0800857 return NO_ERROR;
858}
859
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700860status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800861 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000862 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200863 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800864 mCore->mConsumerName = name;
865 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700866 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800867}
868
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800869status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800870 ATRACE_CALL();
871 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200872 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800873 mCore->mDefaultBufferFormat = defaultFormat;
874 return NO_ERROR;
875}
876
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800877status_t BufferQueueConsumer::setDefaultBufferDataSpace(
878 android_dataspace defaultDataSpace) {
879 ATRACE_CALL();
880 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200881 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800882 mCore->mDefaultBufferDataSpace = defaultDataSpace;
883 return NO_ERROR;
884}
885
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700886status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800887 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700888 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200889 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800890 mCore->mConsumerUsageBits = usage;
891 return NO_ERROR;
892}
893
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700894status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
895 ATRACE_CALL();
896 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200897 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700898 mCore->mConsumerIsProtected = isProtected;
899 return NO_ERROR;
900}
901
Dan Stoza289ade12014-02-28 11:17:17 -0800902status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
903 ATRACE_CALL();
904 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200905 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800906 mCore->mTransformHint = hint;
907 return NO_ERROR;
908}
909
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700910status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200911 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700912 *outStream = mCore->mSidebandStream;
913 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800914}
915
Dan Stozae77c7662016-05-13 11:37:28 -0700916status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
917 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200918 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800919#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700920 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800921#else
922 (void)forceFlush;
923 outHistory->clear();
924#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700925 return NO_ERROR;
926}
927
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700928status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200929 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700930 mCore->discardFreeBuffersLocked();
931 return NO_ERROR;
932}
933
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700934status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700935 struct passwd* pwd = getpwnam("shell");
936 uid_t shellUid = pwd ? pwd->pw_uid : 0;
937 if (!shellUid) {
938 int savedErrno = errno;
939 BQ_LOGE("Cannot get AID_SHELL");
940 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
941 }
942
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800943 bool denied = false;
944 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800945#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900946 // permission check can't be done for vendors as vendors have no access to
Kiyoung Kim3f2638b2023-12-06 16:22:30 +0900947 // the PermissionController.
948 const pid_t pid = BufferQueueThreadState::getCallingPid();
949 if ((uid != shellUid) &&
950 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
951 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
952 "from pid=%d, uid=%d\n",
953 pid, uid);
954 denied = true;
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800955 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900956#else
957 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800958 denied = true;
959 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900960#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800961 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700962 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700963 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700964 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800965 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700966
967 mCore->dumpState(prefix, outResult);
968 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800969}
970
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800971void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
972 std::lock_guard<std::mutex> lock(mCore->mMutex);
973 mCore->mAllowExtraAcquire = allow;
974}
975
Dan Stoza289ade12014-02-28 11:17:17 -0800976} // namespace android