blob: 9855b5bca4c12a95ebef48a35995c74d67d3a385 [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
344 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
345 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
346 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
347 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
Dan Stoza9f3053d2014-03-06 15:14:33 -0800486 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700487 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700488 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
489 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800490 return BAD_VALUE;
491 }
492
Jim Shargo90842182024-11-14 00:49:27 +0000493#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
494 if (eglFence != EGL_NO_SYNC_KHR) {
495 // Most platforms will be using native fences, so it's unlikely that we'll ever have to
496 // process an eglFence. Ideally we can remove this code eventually. In the mean time, do our
497 // best to wait for it so the buffer stays valid, otherwise return an error to the caller.
498 //
499 // EGL_SYNC_FLUSH_COMMANDS_BIT_KHR so that we don't wait forever on a fence that hasn't
500 // shown up on the GPU yet.
501 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
502 1000000000);
503 if (result == EGL_FALSE) {
504 BQ_LOGE("releaseBuffer: error %#x waiting for fence", eglGetError());
505 return UNKNOWN_ERROR;
506 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
507 BQ_LOGE("releaseBuffer: timeout waiting for fence");
508 return UNKNOWN_ERROR;
509 }
510 eglDestroySyncKHR(eglDisplay, eglFence);
511 }
512#endif
513
Dan Stozad1c10362014-03-28 15:19:08 -0700514 sp<IProducerListener> listener;
515 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200516 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800517
Dan Stozad1c10362014-03-28 15:19:08 -0700518 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700519 // we can ignore this releaseBuffer for the old buffer.
520 // Ignore this for the shared buffer where the frame number can easily
521 // get out of sync due to the buffer being queued and acquired at the
522 // same time.
523 if (frameNumber != mSlots[slot].mFrameNumber &&
524 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700525 return STALE_BUFFER_SLOT;
526 }
Dan Stoza289ade12014-02-28 11:17:17 -0800527
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800528 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700529 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700530 "but its state was %s", slot,
531 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800532 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800533 }
Dan Stoza289ade12014-02-28 11:17:17 -0800534
Jim Shargo90842182024-11-14 00:49:27 +0000535#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800536 mSlots[slot].mEglDisplay = eglDisplay;
537 mSlots[slot].mEglFence = eglFence;
Jim Shargo90842182024-11-14 00:49:27 +0000538#endif
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800539 mSlots[slot].mFence = releaseFence;
540 mSlots[slot].mBufferState.release();
541
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700542 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800543 // still be around. Mark it as no longer shared if this
544 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700545 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800546 mSlots[slot].mBufferState.mShared = false;
547 }
548 // Don't put the shared buffer on the free list.
549 if (!mSlots[slot].mBufferState.isShared()) {
550 mCore->mActiveBuffers.erase(slot);
551 mCore->mFreeBuffers.push_back(slot);
552 }
553
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700554 if (mCore->mBufferReleasedCbEnabled) {
555 listener = mCore->mConnectedProducerListener;
556 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800557 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
558
Patrick Williams078d7362024-08-27 10:20:39 -0500559#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
560 mCore->notifyBufferReleased();
561#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200562 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500563#endif
564
Pablo Ceballos9e314332016-01-12 13:49:19 -0800565 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700566 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800567
Dan Stozad1c10362014-03-28 15:19:08 -0700568 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700569 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700570 listener->onBufferReleased();
571 }
Dan Stoza289ade12014-02-28 11:17:17 -0800572
573 return NO_ERROR;
574}
575
576status_t BufferQueueConsumer::connect(
577 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
578 ATRACE_CALL();
579
Yi Kong48a619f2018-06-05 16:34:59 -0700580 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700581 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800582 return BAD_VALUE;
583 }
584
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700585 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800586 controlledByApp ? "true" : "false");
587
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200588 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800589
590 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700591 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800592 return NO_INIT;
593 }
594
595 mCore->mConsumerListener = consumerListener;
596 mCore->mConsumerControlledByApp = controlledByApp;
597
598 return NO_ERROR;
599}
600
601status_t BufferQueueConsumer::disconnect() {
602 ATRACE_CALL();
603
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700604 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800605
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200606 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800607
Yi Kong48a619f2018-06-05 16:34:59 -0700608 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700609 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800610 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800611 }
612
613 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700614 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800615 mCore->mQueue.clear();
616 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700617 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Patrick Williams078d7362024-08-27 10:20:39 -0500618#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
619 mCore->notifyBufferReleased();
620#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200621 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500622#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800623 return NO_ERROR;
624}
625
Dan Stozafebd4f42014-04-09 16:14:51 -0700626status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800627 ATRACE_CALL();
628
Yi Kong48a619f2018-06-05 16:34:59 -0700629 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800630 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
631 return BAD_VALUE;
632 }
633
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200634 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800635
636 if (mCore->mIsAbandoned) {
637 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
638 return NO_INIT;
639 }
640
Dan Stozafebd4f42014-04-09 16:14:51 -0700641 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800642 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800643 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700644 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800645 }
646 }
647
648 // Remove from the mask queued buffers for which acquire has been called,
649 // since the consumer will not receive their buffer addresses and so must
650 // retain their cached information
651 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
652 while (current != mCore->mQueue.end()) {
653 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700654 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800655 }
656 ++current;
657 }
658
Dan Stozafebd4f42014-04-09 16:14:51 -0700659 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800660 *outSlotMask = mask;
661 return NO_ERROR;
662}
663
664status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
665 uint32_t height) {
666 ATRACE_CALL();
667
668 if (width == 0 || height == 0) {
669 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
670 "height=%u)", width, height);
671 return BAD_VALUE;
672 }
673
674 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
675
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200676 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800677 mCore->mDefaultWidth = width;
678 mCore->mDefaultHeight = height;
679 return NO_ERROR;
680}
681
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700682status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800683 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800684
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700685 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
686 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
687 return BAD_VALUE;
688 }
Dan Stoza289ade12014-02-28 11:17:17 -0800689
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200690 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800691
Pablo Ceballos38273792016-03-02 01:38:10 +0000692 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
693 BQ_LOGE("setMaxBufferCount: producer is already connected");
694 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800695 }
696
Pablo Ceballos38273792016-03-02 01:38:10 +0000697 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
698 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
699 "mMaxAcquiredBufferCount (%d)", bufferCount,
700 mCore->mMaxAcquiredBufferCount);
701 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700702 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000703
704 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
705 mCore->mDequeueBufferCannotBlock, bufferCount) -
706 mCore->getMaxBufferCountLocked();
707 if (!mCore->adjustAvailableSlotsLocked(delta)) {
708 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
709 "available slots. Delta = %d", delta);
710 return BAD_VALUE;
711 }
712
713 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800714 return NO_ERROR;
715}
716
717status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
718 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700719 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800720
721 if (maxAcquiredBuffers < 1 ||
722 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
723 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
724 maxAcquiredBuffers);
725 return BAD_VALUE;
726 }
727
Pablo Ceballos38273792016-03-02 01:38:10 +0000728 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800729 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200730 std::unique_lock<std::mutex> lock(mCore->mMutex);
731 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800732
Pablo Ceballos72daab62015-12-07 16:38:43 -0800733 if (mCore->mIsAbandoned) {
734 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
735 return NO_INIT;
736 }
737
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700738 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
739 return NO_ERROR;
740 }
741
Pablo Ceballos72daab62015-12-07 16:38:43 -0800742 // The new maxAcquiredBuffers count should not be violated by the number
743 // of currently acquired buffers
744 int acquiredCount = 0;
745 for (int slot : mCore->mActiveBuffers) {
746 if (mSlots[slot].mBufferState.isAcquired()) {
747 acquiredCount++;
748 }
749 }
750 if (acquiredCount > maxAcquiredBuffers) {
751 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
752 "count (%d) exceeds the current acquired buffer count (%d)",
753 maxAcquiredBuffers, acquiredCount);
754 return BAD_VALUE;
755 }
756
757 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
758 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
759 > mCore->mMaxBufferCount) {
760 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
761 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
762 maxAcquiredBuffers, mCore->mMaxBufferCount,
763 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
764 mCore->mDequeueBufferCannotBlock);
765 return BAD_VALUE;
766 }
767
768 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000769 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800770 return BAD_VALUE;
771 }
772
773 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
774 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
775 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700776 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000777 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800778 }
779 }
780 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700781 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000782 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800783 }
784
Dan Stoza289ade12014-02-28 11:17:17 -0800785 return NO_ERROR;
786}
787
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700788status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800789 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000790 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200791 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800792 mCore->mConsumerName = name;
793 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700794 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800795}
796
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800797status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800798 ATRACE_CALL();
799 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200800 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800801 mCore->mDefaultBufferFormat = defaultFormat;
802 return NO_ERROR;
803}
804
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800805status_t BufferQueueConsumer::setDefaultBufferDataSpace(
806 android_dataspace defaultDataSpace) {
807 ATRACE_CALL();
808 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200809 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800810 mCore->mDefaultBufferDataSpace = defaultDataSpace;
811 return NO_ERROR;
812}
813
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700814status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800815 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700816 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200817 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800818 mCore->mConsumerUsageBits = usage;
819 return NO_ERROR;
820}
821
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700822status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
823 ATRACE_CALL();
824 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200825 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700826 mCore->mConsumerIsProtected = isProtected;
827 return NO_ERROR;
828}
829
Dan Stoza289ade12014-02-28 11:17:17 -0800830status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
831 ATRACE_CALL();
832 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200833 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800834 mCore->mTransformHint = hint;
835 return NO_ERROR;
836}
837
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700838status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200839 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700840 *outStream = mCore->mSidebandStream;
841 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800842}
843
Dan Stozae77c7662016-05-13 11:37:28 -0700844status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
845 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200846 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800847#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700848 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800849#else
850 (void)forceFlush;
851 outHistory->clear();
852#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700853 return NO_ERROR;
854}
855
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700856status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200857 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700858 mCore->discardFreeBuffersLocked();
859 return NO_ERROR;
860}
861
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700862status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700863 struct passwd* pwd = getpwnam("shell");
864 uid_t shellUid = pwd ? pwd->pw_uid : 0;
865 if (!shellUid) {
866 int savedErrno = errno;
867 BQ_LOGE("Cannot get AID_SHELL");
868 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
869 }
870
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800871 bool denied = false;
872 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800873#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900874 // permission check can't be done for vendors as vendors have no access to
Kiyoung Kim3f2638b2023-12-06 16:22:30 +0900875 // the PermissionController.
876 const pid_t pid = BufferQueueThreadState::getCallingPid();
877 if ((uid != shellUid) &&
878 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
879 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
880 "from pid=%d, uid=%d\n",
881 pid, uid);
882 denied = true;
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800883 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900884#else
885 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800886 denied = true;
887 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900888#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800889 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700890 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700891 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700892 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800893 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700894
895 mCore->dumpState(prefix, outResult);
896 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800897}
898
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800899void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
900 std::lock_guard<std::mutex> lock(mCore->mMutex);
901 mCore->mAllowExtraAcquire = allow;
902}
903
Dan Stoza289ade12014-02-28 11:17:17 -0800904} // namespace android