blob: f0125868ae7ec0f4d2ffce8cae8ca15a190751d3 [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
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
Yifan Hong65799c32017-07-26 10:47:14 -070018#include <pwd.h>
19#include <sys/types.h>
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070020
Dan Stoza3e96f192014-03-03 10:16:19 -080021#define LOG_TAG "BufferQueueConsumer"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23//#define LOG_NDEBUG 0
24
Pablo Ceballos9e314332016-01-12 13:49:19 -080025#if DEBUG_ONLY_CODE
26#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
27#else
28#define VALIDATE_CONSISTENCY()
29#endif
30
Jim Shargo90842182024-11-14 00:49:27 +000031#define EGL_EGLEXT_PROTOTYPES
32#include <EGL/egl.h>
33#include <EGL/eglext.h>
34
Dan Stoza289ade12014-02-28 11:17:17 -080035#include <gui/BufferItem.h>
36#include <gui/BufferQueueConsumer.h>
37#include <gui/BufferQueueCore.h>
38#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070039#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070040#include <gui/TraceUtils.h>
Dan Stoza289ade12014-02-28 11:17:17 -080041
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080042#include <private/gui/BufferQueueThreadState.h>
Kiyoung Kim5c08e302023-11-10 16:35:13 +090043#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Pablo Ceballos88f69282016-02-11 18:01:49 -080044#include <binder/PermissionCache.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090045#endif
Pablo Ceballos88f69282016-02-11 18:01:49 -080046
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070047#include <system/window.h>
48
Sungtak Leecd217472024-07-19 17:17:40 +000049#include <com_android_graphics_libgui_flags.h>
50
Dan Stoza289ade12014-02-28 11:17:17 -080051namespace android {
52
Iris Chang430193f2019-12-04 16:25:46 +080053// Macros for include BufferQueueCore information in log messages
54#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000055 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080056 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
57 ##__VA_ARGS__)
58#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000059 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080060 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
61 ##__VA_ARGS__)
62#define BQ_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000063 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080064 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
65 ##__VA_ARGS__)
66#define BQ_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000067 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080068 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
69 ##__VA_ARGS__)
70#define BQ_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000071 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080072 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
73 ##__VA_ARGS__)
74
Chong Zhang62493092020-01-15 16:04:47 -080075ConsumerListener::~ConsumerListener() = default;
76
Dan Stoza289ade12014-02-28 11:17:17 -080077BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
78 mCore(core),
79 mSlots(core->mSlots),
80 mConsumerName() {}
81
82BufferQueueConsumer::~BufferQueueConsumer() {}
83
84status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070085 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080086 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080087
Lajos Molnar5f920c12015-07-13 16:04:24 -070088 int numDroppedBuffers = 0;
89 sp<IProducerListener> listener;
90 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020091 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070092
93 // Check that the consumer doesn't currently have the maximum number of
94 // buffers acquired. We allow the max buffer count to be exceeded by one
95 // buffer so that the consumer can successfully set up the newly acquired
96 // buffer before releasing the old one.
97 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080098 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070099 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700100 ++numAcquiredBuffers;
101 }
Dan Stoza289ade12014-02-28 11:17:17 -0800102 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800103 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
104 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
105 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
106 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700107 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
108 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
109 return INVALID_OPERATION;
110 }
Dan Stoza289ade12014-02-28 11:17:17 -0800111
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700112 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
113 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700114 BufferQueueCore::INVALID_BUFFER_SLOT;
115
Lajos Molnar5f920c12015-07-13 16:04:24 -0700116 // In asynchronous mode the list is guaranteed to be one buffer deep,
117 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700118 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700119 return NO_BUFFER_AVAILABLE;
120 }
Dan Stoza289ade12014-02-28 11:17:17 -0800121
Lajos Molnar5f920c12015-07-13 16:04:24 -0700122 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800123
Lajos Molnar5f920c12015-07-13 16:04:24 -0700124 // If expectedPresent is specified, we may not want to return a buffer yet.
125 // If it's specified and there's more than one buffer queued, we may want
126 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700127 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700128 // since in that case we'll just return the shared buffer.
129 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700130 // The 'expectedPresent' argument indicates when the buffer is expected
131 // to be presented on-screen. If the buffer's desired present time is
132 // earlier (less) than expectedPresent -- meaning it will be displayed
133 // on time or possibly late if we show it as soon as possible -- we
134 // acquire and return it. If we don't want to display it until after the
135 // expectedPresent time, we return PRESENT_LATER without acquiring it.
136 //
137 // To be safe, we don't defer acquisition if expectedPresent is more
138 // than one second in the future beyond the desired present time
139 // (i.e., we'd be holding the buffer for a long time).
140 //
141 // NOTE: Code assumes monotonic time values from the system clock
142 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800143
Lajos Molnar5f920c12015-07-13 16:04:24 -0700144 // Start by checking to see if we can drop frames. We skip this check if
145 // the timestamps are being auto-generated by Surface. If the app isn't
146 // generating timestamps explicitly, it probably doesn't want frames to
147 // be discarded based on them.
148 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
149 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700150
Lajos Molnar5f920c12015-07-13 16:04:24 -0700151 // If dropping entry[0] would leave us with a buffer that the
152 // consumer is not yet ready for, don't drop it.
153 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
154 break;
155 }
156
157 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
158 // additional criterion here: we only drop the earlier buffer if our
159 // desiredPresent falls within +/- 1 second of the expected present.
160 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
161 // relative timestamp), which normally mean "ignore the timestamp
162 // and acquire immediately", would cause us to drop frames.
163 //
164 // We may want to add an additional criterion: don't drop the
165 // earlier buffer if entry[1]'s fence hasn't signaled yet.
166 nsecs_t desiredPresent = bufferItem.mTimestamp;
167 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
168 desiredPresent > expectedPresent) {
169 // This buffer is set to display in the near future, or
170 // desiredPresent is garbage. Either way we don't want to drop
171 // the previous buffer just to get this on the screen sooner.
172 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
173 PRId64 " (%" PRId64 ") now=%" PRId64,
174 desiredPresent, expectedPresent,
175 desiredPresent - expectedPresent,
176 systemTime(CLOCK_MONOTONIC));
177 break;
178 }
179
180 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
181 " size=%zu",
182 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800183
184 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700185 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700186 mSlots[front->mSlot].mBufferState.freeQueued();
187
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700188 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700189 // still be around. Mark it as no longer shared if this
190 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700191 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700192 mSlots[front->mSlot].mBufferState.isFree()) {
193 mSlots[front->mSlot].mBufferState.mShared = false;
194 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800195
196 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700197 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800198 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700199 mCore->mFreeBuffers.push_back(front->mSlot);
200 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800201
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700202 if (mCore->mBufferReleasedCbEnabled) {
203 listener = mCore->mConnectedProducerListener;
204 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700205 ++numDroppedBuffers;
206 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800207
Lajos Molnar5f920c12015-07-13 16:04:24 -0700208 mCore->mQueue.erase(front);
209 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700210 }
211
Lajos Molnar5f920c12015-07-13 16:04:24 -0700212 // See if the front buffer is ready to be acquired
213 nsecs_t desiredPresent = front->mTimestamp;
214 bool bufferIsDue = desiredPresent <= expectedPresent ||
215 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
216 bool consumerIsReady = maxFrameNumber > 0 ?
217 front->mFrameNumber <= maxFrameNumber : true;
218 if (!bufferIsDue || !consumerIsReady) {
219 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
220 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
221 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700222 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800223 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700224 systemTime(CLOCK_MONOTONIC),
225 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700226 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700227 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800228 }
229
Lajos Molnar5f920c12015-07-13 16:04:24 -0700230 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
231 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800232 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700233 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800234 }
235
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700236 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
237
238 if (sharedBufferAvailable && mCore->mQueue.empty()) {
239 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200240 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700241
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700242 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700243
244 // Recreate the BufferItem for the shared buffer from the data that
245 // was cached when it was last queued.
246 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
247 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700248 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700249 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
250 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700251 ~static_cast<uint32_t>(
252 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700253 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
254 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700255 outBuffer->mFrameNumber = mCore->mFrameCounter;
256 outBuffer->mSlot = slot;
257 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
258 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700259 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700260 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
261 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700262 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800263 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700264 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800265 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800266 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
267 BQ_LOGV("acquireBuffer: front buffer is not droppable");
268 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700269 } else {
270 slot = front->mSlot;
271 *outBuffer = *front;
272 }
273
Lajos Molnar5f920c12015-07-13 16:04:24 -0700274 ATRACE_BUFFER_INDEX(slot);
275
276 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700277 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800278
279 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700280 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700281 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700282 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700283 // the queue is empty and the BufferItem is created above.
284 if (mCore->mQueue.empty()) {
285 mSlots[slot].mBufferState.acquireNotInQueue();
286 } else {
287 mSlots[slot].mBufferState.acquire();
288 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700289 mSlots[slot].mFence = Fence::NO_FENCE;
290 }
291
292 // If the buffer has previously been acquired by the consumer, set
293 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
294 // on the consumer side
295 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700296 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700297 }
298
299 mCore->mQueue.erase(front);
300
301 // We might have freed a slot while dropping old buffers, or the producer
302 // may be blocked waiting for the number of buffers in the queue to
303 // decrease.
Patrick Williams078d7362024-08-27 10:20:39 -0500304#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
305 mCore->notifyBufferReleased();
306#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200307 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500308#endif
Lajos Molnar5f920c12015-07-13 16:04:24 -0700309
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000310 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800311#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700312 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800313#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800314 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800315 }
316
Yi Kong48a619f2018-06-05 16:34:59 -0700317 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700318 for (int i = 0; i < numDroppedBuffers; ++i) {
319 listener->onBufferReleased();
320 }
Dan Stoza289ade12014-02-28 11:17:17 -0800321 }
322
Dan Stoza289ade12014-02-28 11:17:17 -0800323 return NO_ERROR;
324}
325
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326status_t BufferQueueConsumer::detachBuffer(int slot) {
327 ATRACE_CALL();
328 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700329 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000330 sp<IProducerListener> listener;
331 {
332 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800333
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000334 if (mCore->mIsAbandoned) {
335 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
336 return NO_INIT;
337 }
338
339 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
340 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
341 return BAD_VALUE;
342 }
343
Jim Shargo2e614a42024-10-02 19:31:53 +0000344 const int totalSlotCount = mCore->getTotalSlotCountLocked();
345 if (slot < 0 || slot >= totalSlotCount) {
346 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000347 return BAD_VALUE;
348 } else if (!mSlots[slot].mBufferState.isAcquired()) {
349 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
350 "(state = %s)", slot, mSlots[slot].mBufferState.string());
351 return BAD_VALUE;
352 }
353 if (mCore->mBufferReleasedCbEnabled) {
354 listener = mCore->mConnectedProducerListener;
355 }
356
357 mSlots[slot].mBufferState.detachConsumer();
358 mCore->mActiveBuffers.erase(slot);
359 mCore->mFreeSlots.insert(slot);
360 mCore->clearBufferSlotLocked(slot);
Patrick Williams078d7362024-08-27 10:20:39 -0500361#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
362 mCore->notifyBufferReleased();
363#else
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000364 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500365#endif
366
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000367 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 }
369
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000370 if (listener) {
371 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 return NO_ERROR;
374}
375
376status_t BufferQueueConsumer::attachBuffer(int* outSlot,
377 const sp<android::GraphicBuffer>& buffer) {
378 ATRACE_CALL();
379
Yi Kong48a619f2018-06-05 16:34:59 -0700380 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700381 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700383 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700384 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800385 return BAD_VALUE;
386 }
387
Sungtak Leecd217472024-07-19 17:17:40 +0000388 sp<IProducerListener> listener;
389 {
390 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391
Sungtak Leecd217472024-07-19 17:17:40 +0000392 if (mCore->mSharedBufferMode) {
393 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
394 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800395 }
Sungtak Leecd217472024-07-19 17:17:40 +0000396
397 // Make sure we don't have too many acquired buffers
398 int numAcquiredBuffers = 0;
399 for (int s : mCore->mActiveBuffers) {
400 if (mSlots[s].mBufferState.isAcquired()) {
401 ++numAcquiredBuffers;
402 }
403 }
404
405 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
406 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
407 "(max %d)", numAcquiredBuffers,
408 mCore->mMaxAcquiredBufferCount);
409 return INVALID_OPERATION;
410 }
411
412 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
413 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
414 "[queue %u]", buffer->getGenerationNumber(),
415 mCore->mGenerationNumber);
416 return BAD_VALUE;
417 }
418
419 // Find a free slot to put the buffer into
420 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
421 if (!mCore->mFreeSlots.empty()) {
422 auto slot = mCore->mFreeSlots.begin();
423 found = *slot;
424 mCore->mFreeSlots.erase(slot);
425 } else if (!mCore->mFreeBuffers.empty()) {
426 found = mCore->mFreeBuffers.front();
427 mCore->mFreeBuffers.remove(found);
428 }
429 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
430 BQ_LOGE("attachBuffer: could not find free buffer slot");
431 return NO_MEMORY;
432 }
433
434#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
435 if (mCore->mBufferAttachedCbEnabled) {
436 listener = mCore->mConnectedProducerListener;
437 }
438#endif
439
440 mCore->mActiveBuffers.insert(found);
441 *outSlot = found;
442 ATRACE_BUFFER_INDEX(*outSlot);
443 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
444
445 mSlots[*outSlot].mGraphicBuffer = buffer;
446 mSlots[*outSlot].mBufferState.attachConsumer();
447 mSlots[*outSlot].mNeedsReallocation = true;
448 mSlots[*outSlot].mFence = Fence::NO_FENCE;
449 mSlots[*outSlot].mFrameNumber = 0;
450
451 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
452 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
453 // Binder traffic by not un/flattening the GraphicBuffer. However, it
454 // requires that the consumer maintain a cached copy of the slot <--> buffer
455 // mappings, which is why the consumer doesn't need the valid pointer on
456 // acquire.
457 //
458 // The StreamSplitter is one of the primary users of the attach/detach
459 // logic, and while it is running, all buffers it acquires are immediately
460 // detached, and all buffers it eventually releases are ones that were
461 // attached (as opposed to having been obtained from acquireBuffer), so it
462 // doesn't make sense to maintain the slot/buffer mappings, which would
463 // become invalid for every buffer during detach/attach. By setting this to
464 // false, the valid GraphicBuffer pointer will always be sent with acquire
465 // for attached buffers.
466 mSlots[*outSlot].mAcquireCalled = false;
467
468 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800469 }
470
Sungtak Leecd217472024-07-19 17:17:40 +0000471#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
472 if (listener != nullptr) {
473 listener->onBufferAttached();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800474 }
Sungtak Leecd217472024-07-19 17:17:40 +0000475#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -0700476
Dan Stoza9f3053d2014-03-06 15:14:33 -0800477 return NO_ERROR;
478}
479
Dan Stoza289ade12014-02-28 11:17:17 -0800480status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
481 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
482 EGLSyncKHR eglFence) {
483 ATRACE_CALL();
484 ATRACE_BUFFER_INDEX(slot);
485
Jim Shargo2e614a42024-10-02 19:31:53 +0000486 const int totalSlotCount = mCore->getTotalSlotCountLocked();
487 if (slot < 0 || slot >= totalSlotCount) {
488 BQ_LOGE("releaseBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
489 return BAD_VALUE;
490 }
491 if (releaseFence == nullptr) {
492 BQ_LOGE("releaseBuffer: slot %d fence %p NULL", slot, releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800493 return BAD_VALUE;
494 }
495
Jim Shargo90842182024-11-14 00:49:27 +0000496#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
497 if (eglFence != EGL_NO_SYNC_KHR) {
498 // Most platforms will be using native fences, so it's unlikely that we'll ever have to
499 // process an eglFence. Ideally we can remove this code eventually. In the mean time, do our
500 // best to wait for it so the buffer stays valid, otherwise return an error to the caller.
501 //
502 // EGL_SYNC_FLUSH_COMMANDS_BIT_KHR so that we don't wait forever on a fence that hasn't
503 // shown up on the GPU yet.
504 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
505 1000000000);
506 if (result == EGL_FALSE) {
507 BQ_LOGE("releaseBuffer: error %#x waiting for fence", eglGetError());
508 return UNKNOWN_ERROR;
509 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
510 BQ_LOGE("releaseBuffer: timeout waiting for fence");
511 return UNKNOWN_ERROR;
512 }
513 eglDestroySyncKHR(eglDisplay, eglFence);
514 }
515#endif
516
Dan Stozad1c10362014-03-28 15:19:08 -0700517 sp<IProducerListener> listener;
518 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200519 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800520
Jim Shargo2e614a42024-10-02 19:31:53 +0000521 const int totalSlotCount = mCore->getTotalSlotCountLocked();
522 if (slot < 0 || slot >= totalSlotCount || releaseFence == nullptr) {
523 BQ_LOGE("releaseBuffer: slot %d out of range [0, %d) or fence %p NULL", slot,
524 totalSlotCount, releaseFence.get());
525 return BAD_VALUE;
526 }
527
Dan Stozad1c10362014-03-28 15:19:08 -0700528 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700529 // we can ignore this releaseBuffer for the old buffer.
530 // Ignore this for the shared buffer where the frame number can easily
531 // get out of sync due to the buffer being queued and acquired at the
532 // same time.
533 if (frameNumber != mSlots[slot].mFrameNumber &&
534 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700535 return STALE_BUFFER_SLOT;
536 }
Dan Stoza289ade12014-02-28 11:17:17 -0800537
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800538 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700539 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700540 "but its state was %s", slot,
541 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800542 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800543 }
Dan Stoza289ade12014-02-28 11:17:17 -0800544
Jim Shargo90842182024-11-14 00:49:27 +0000545#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800546 mSlots[slot].mEglDisplay = eglDisplay;
547 mSlots[slot].mEglFence = eglFence;
Jim Shargo90842182024-11-14 00:49:27 +0000548#endif
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800549 mSlots[slot].mFence = releaseFence;
550 mSlots[slot].mBufferState.release();
551
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700552 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800553 // still be around. Mark it as no longer shared if this
554 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700555 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800556 mSlots[slot].mBufferState.mShared = false;
557 }
558 // Don't put the shared buffer on the free list.
559 if (!mSlots[slot].mBufferState.isShared()) {
560 mCore->mActiveBuffers.erase(slot);
561 mCore->mFreeBuffers.push_back(slot);
562 }
563
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700564 if (mCore->mBufferReleasedCbEnabled) {
565 listener = mCore->mConnectedProducerListener;
566 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800567 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
568
Patrick Williams078d7362024-08-27 10:20:39 -0500569#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
570 mCore->notifyBufferReleased();
571#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200572 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500573#endif
574
Pablo Ceballos9e314332016-01-12 13:49:19 -0800575 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700576 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800577
Dan Stozad1c10362014-03-28 15:19:08 -0700578 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700579 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700580 listener->onBufferReleased();
581 }
Dan Stoza289ade12014-02-28 11:17:17 -0800582
583 return NO_ERROR;
584}
585
586status_t BufferQueueConsumer::connect(
587 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
588 ATRACE_CALL();
589
Yi Kong48a619f2018-06-05 16:34:59 -0700590 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700591 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800592 return BAD_VALUE;
593 }
594
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700595 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800596 controlledByApp ? "true" : "false");
597
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200598 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800599
600 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700601 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800602 return NO_INIT;
603 }
604
605 mCore->mConsumerListener = consumerListener;
606 mCore->mConsumerControlledByApp = controlledByApp;
607
608 return NO_ERROR;
609}
610
611status_t BufferQueueConsumer::disconnect() {
612 ATRACE_CALL();
613
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700614 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800615
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200616 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800617
Yi Kong48a619f2018-06-05 16:34:59 -0700618 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700619 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800620 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800621 }
622
623 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700624 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800625 mCore->mQueue.clear();
626 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700627 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Patrick Williams078d7362024-08-27 10:20:39 -0500628#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
629 mCore->notifyBufferReleased();
630#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200631 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500632#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800633 return NO_ERROR;
634}
635
Dan Stozafebd4f42014-04-09 16:14:51 -0700636status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800637 ATRACE_CALL();
638
Yi Kong48a619f2018-06-05 16:34:59 -0700639 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800640 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
641 return BAD_VALUE;
642 }
643
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200644 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800645
646 if (mCore->mIsAbandoned) {
647 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
648 return NO_INIT;
649 }
650
Dan Stozafebd4f42014-04-09 16:14:51 -0700651 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800652 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800653 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700654 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800655 }
656 }
657
658 // Remove from the mask queued buffers for which acquire has been called,
659 // since the consumer will not receive their buffer addresses and so must
660 // retain their cached information
661 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
662 while (current != mCore->mQueue.end()) {
663 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700664 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800665 }
666 ++current;
667 }
668
Dan Stozafebd4f42014-04-09 16:14:51 -0700669 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800670 *outSlotMask = mask;
671 return NO_ERROR;
672}
673
Jim Shargo2e614a42024-10-02 19:31:53 +0000674#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
675status_t BufferQueueConsumer::getReleasedBuffersExtended(std::vector<bool>* outSlotMask) {
676 ATRACE_CALL();
677
678 if (outSlotMask == nullptr) {
679 BQ_LOGE("getReleasedBuffersExtended: outSlotMask may not be NULL");
680 return BAD_VALUE;
681 }
682
683 std::lock_guard<std::mutex> lock(mCore->mMutex);
684
685 if (mCore->mIsAbandoned) {
686 BQ_LOGE("getReleasedBuffersExtended: BufferQueue has been abandoned");
687 return NO_INIT;
688 }
689
690 const int totalSlotCount = mCore->getTotalSlotCountLocked();
691 outSlotMask->resize(totalSlotCount);
692 for (int s = 0; s < totalSlotCount; ++s) {
693 (*outSlotMask)[s] = !mSlots[s].mAcquireCalled;
694 }
695
696 // Remove from the mask queued buffers for which acquire has been called,
697 // since the consumer will not receive their buffer addresses and so must
698 // retain their cached information
699 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
700 while (current != mCore->mQueue.end()) {
701 if (current->mAcquireCalled) {
702 (*outSlotMask)[current->mSlot] = false;
703 }
704 ++current;
705 }
706
707 return NO_ERROR;
708}
709#endif
710
Dan Stoza289ade12014-02-28 11:17:17 -0800711status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
712 uint32_t height) {
713 ATRACE_CALL();
714
715 if (width == 0 || height == 0) {
716 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
717 "height=%u)", width, height);
718 return BAD_VALUE;
719 }
720
721 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
722
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200723 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800724 mCore->mDefaultWidth = width;
725 mCore->mDefaultHeight = height;
726 return NO_ERROR;
727}
728
Jim Shargo2e614a42024-10-02 19:31:53 +0000729#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
730status_t BufferQueueConsumer::allowUnlimitedSlots(bool allowUnlimitedSlots) {
731 ATRACE_CALL();
732 BQ_LOGV("allowUnlimitedSlots: %d", allowUnlimitedSlots);
733 std::lock_guard<std::mutex> lock(mCore->mMutex);
734
735 if (mCore->mIsAbandoned) {
736 BQ_LOGE("allowUnlimitedSlots: BufferQueue has been abandoned");
737 return NO_INIT;
738 }
739
740 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
741 BQ_LOGE("allowUnlimitedSlots: BufferQueue already connected");
742 return INVALID_OPERATION;
743 }
744
745 mCore->mAllowExtendedSlotCount = allowUnlimitedSlots;
746
747 return OK;
748}
749#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
750
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700751status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800752 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800753
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700754 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
755 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
756 return BAD_VALUE;
757 }
Dan Stoza289ade12014-02-28 11:17:17 -0800758
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200759 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800760
Pablo Ceballos38273792016-03-02 01:38:10 +0000761 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
762 BQ_LOGE("setMaxBufferCount: producer is already connected");
763 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800764 }
765
Pablo Ceballos38273792016-03-02 01:38:10 +0000766 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
767 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
768 "mMaxAcquiredBufferCount (%d)", bufferCount,
769 mCore->mMaxAcquiredBufferCount);
770 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700771 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000772
773 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
774 mCore->mDequeueBufferCannotBlock, bufferCount) -
775 mCore->getMaxBufferCountLocked();
776 if (!mCore->adjustAvailableSlotsLocked(delta)) {
777 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
778 "available slots. Delta = %d", delta);
779 return BAD_VALUE;
780 }
781
782 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800783 return NO_ERROR;
784}
785
786status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
787 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700788 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800789
Pablo Ceballos38273792016-03-02 01:38:10 +0000790 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800791 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200792 std::unique_lock<std::mutex> lock(mCore->mMutex);
Jim Shargo2e614a42024-10-02 19:31:53 +0000793
794 // We reserve two slots in order to guarantee that the producer and
795 // consumer can run asynchronously.
796 int maxMaxAcquiredBuffers =
797#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
798 mCore->getTotalSlotCountLocked() - 2;
799#else
800 BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS;
801#endif
802 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > maxMaxAcquiredBuffers) {
803 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d", maxAcquiredBuffers);
804 return BAD_VALUE;
805 }
806
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200807 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800808
Pablo Ceballos72daab62015-12-07 16:38:43 -0800809 if (mCore->mIsAbandoned) {
810 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
811 return NO_INIT;
812 }
813
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700814 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
815 return NO_ERROR;
816 }
817
Pablo Ceballos72daab62015-12-07 16:38:43 -0800818 // The new maxAcquiredBuffers count should not be violated by the number
819 // of currently acquired buffers
820 int acquiredCount = 0;
821 for (int slot : mCore->mActiveBuffers) {
822 if (mSlots[slot].mBufferState.isAcquired()) {
823 acquiredCount++;
824 }
825 }
826 if (acquiredCount > maxAcquiredBuffers) {
827 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
828 "count (%d) exceeds the current acquired buffer count (%d)",
829 maxAcquiredBuffers, acquiredCount);
830 return BAD_VALUE;
831 }
832
833 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
834 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
835 > mCore->mMaxBufferCount) {
836 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
837 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
838 maxAcquiredBuffers, mCore->mMaxBufferCount,
839 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
840 mCore->mDequeueBufferCannotBlock);
841 return BAD_VALUE;
842 }
843
844 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000845 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800846 return BAD_VALUE;
847 }
848
849 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
850 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
851 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700852 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000853 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800854 }
855 }
856 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700857 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000858 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800859 }
860
Dan Stoza289ade12014-02-28 11:17:17 -0800861 return NO_ERROR;
862}
863
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700864status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800865 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000866 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200867 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800868 mCore->mConsumerName = name;
869 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700870 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800871}
872
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800873status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800874 ATRACE_CALL();
875 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200876 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800877 mCore->mDefaultBufferFormat = defaultFormat;
878 return NO_ERROR;
879}
880
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800881status_t BufferQueueConsumer::setDefaultBufferDataSpace(
882 android_dataspace defaultDataSpace) {
883 ATRACE_CALL();
884 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200885 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800886 mCore->mDefaultBufferDataSpace = defaultDataSpace;
887 return NO_ERROR;
888}
889
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700890status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800891 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700892 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200893 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800894 mCore->mConsumerUsageBits = usage;
895 return NO_ERROR;
896}
897
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700898status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
899 ATRACE_CALL();
900 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200901 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700902 mCore->mConsumerIsProtected = isProtected;
903 return NO_ERROR;
904}
905
Dan Stoza289ade12014-02-28 11:17:17 -0800906status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
907 ATRACE_CALL();
908 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200909 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800910 mCore->mTransformHint = hint;
911 return NO_ERROR;
912}
913
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700914status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200915 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700916 *outStream = mCore->mSidebandStream;
917 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800918}
919
Dan Stozae77c7662016-05-13 11:37:28 -0700920status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
921 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200922 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800923#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700924 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800925#else
926 (void)forceFlush;
927 outHistory->clear();
928#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700929 return NO_ERROR;
930}
931
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700932status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200933 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700934 mCore->discardFreeBuffersLocked();
935 return NO_ERROR;
936}
937
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700938status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700939 struct passwd* pwd = getpwnam("shell");
940 uid_t shellUid = pwd ? pwd->pw_uid : 0;
941 if (!shellUid) {
942 int savedErrno = errno;
943 BQ_LOGE("Cannot get AID_SHELL");
944 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
945 }
946
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800947 bool denied = false;
948 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800949#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900950 // permission check can't be done for vendors as vendors have no access to
Kiyoung Kim3f2638b2023-12-06 16:22:30 +0900951 // the PermissionController.
952 const pid_t pid = BufferQueueThreadState::getCallingPid();
953 if ((uid != shellUid) &&
954 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
955 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
956 "from pid=%d, uid=%d\n",
957 pid, uid);
958 denied = true;
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800959 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900960#else
961 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800962 denied = true;
963 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900964#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800965 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700966 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700967 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700968 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800969 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700970
971 mCore->dumpState(prefix, outResult);
972 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800973}
974
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800975void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
976 std::lock_guard<std::mutex> lock(mCore->mMutex);
977 mCore->mAllowExtraAcquire = allow;
978}
979
Dan Stoza289ade12014-02-28 11:17:17 -0800980} // namespace android