blob: d0607bfab8c75659034bea117655e6a45a71ab31 [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
Dan Stoza289ade12014-02-28 11:17:17 -080031#include <gui/BufferItem.h>
32#include <gui/BufferQueueConsumer.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070035#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070036#include <gui/TraceUtils.h>
Dan Stoza289ade12014-02-28 11:17:17 -080037
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080038#include <private/gui/BufferQueueThreadState.h>
Kiyoung Kim5c08e302023-11-10 16:35:13 +090039#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Pablo Ceballos88f69282016-02-11 18:01:49 -080040#include <binder/PermissionCache.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090041#endif
Pablo Ceballos88f69282016-02-11 18:01:49 -080042
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070043#include <system/window.h>
44
Sungtak Leecd217472024-07-19 17:17:40 +000045#include <com_android_graphics_libgui_flags.h>
46
Dan Stoza289ade12014-02-28 11:17:17 -080047namespace android {
48
Iris Chang430193f2019-12-04 16:25:46 +080049// Macros for include BufferQueueCore information in log messages
50#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000051 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080052 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
53 ##__VA_ARGS__)
54#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000055 ALOGD("[%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_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000059 ALOGI("[%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_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000063 ALOGW("[%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_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000067 ALOGE("[%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
Chong Zhang62493092020-01-15 16:04:47 -080071ConsumerListener::~ConsumerListener() = default;
72
Dan Stoza289ade12014-02-28 11:17:17 -080073BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
74 mCore(core),
75 mSlots(core->mSlots),
76 mConsumerName() {}
77
78BufferQueueConsumer::~BufferQueueConsumer() {}
79
80status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070081 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080082 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080083
Lajos Molnar5f920c12015-07-13 16:04:24 -070084 int numDroppedBuffers = 0;
85 sp<IProducerListener> listener;
86 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020087 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070088
89 // Check that the consumer doesn't currently have the maximum number of
90 // buffers acquired. We allow the max buffer count to be exceeded by one
91 // buffer so that the consumer can successfully set up the newly acquired
92 // buffer before releasing the old one.
93 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080094 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070095 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070096 ++numAcquiredBuffers;
97 }
Dan Stoza289ade12014-02-28 11:17:17 -080098 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -080099 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
100 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
101 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
102 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700103 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
104 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
105 return INVALID_OPERATION;
106 }
Dan Stoza289ade12014-02-28 11:17:17 -0800107
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700108 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
109 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700110 BufferQueueCore::INVALID_BUFFER_SLOT;
111
Lajos Molnar5f920c12015-07-13 16:04:24 -0700112 // In asynchronous mode the list is guaranteed to be one buffer deep,
113 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700114 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700115 return NO_BUFFER_AVAILABLE;
116 }
Dan Stoza289ade12014-02-28 11:17:17 -0800117
Lajos Molnar5f920c12015-07-13 16:04:24 -0700118 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800119
Lajos Molnar5f920c12015-07-13 16:04:24 -0700120 // If expectedPresent is specified, we may not want to return a buffer yet.
121 // If it's specified and there's more than one buffer queued, we may want
122 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700123 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700124 // since in that case we'll just return the shared buffer.
125 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700126 // The 'expectedPresent' argument indicates when the buffer is expected
127 // to be presented on-screen. If the buffer's desired present time is
128 // earlier (less) than expectedPresent -- meaning it will be displayed
129 // on time or possibly late if we show it as soon as possible -- we
130 // acquire and return it. If we don't want to display it until after the
131 // expectedPresent time, we return PRESENT_LATER without acquiring it.
132 //
133 // To be safe, we don't defer acquisition if expectedPresent is more
134 // than one second in the future beyond the desired present time
135 // (i.e., we'd be holding the buffer for a long time).
136 //
137 // NOTE: Code assumes monotonic time values from the system clock
138 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800139
Lajos Molnar5f920c12015-07-13 16:04:24 -0700140 // Start by checking to see if we can drop frames. We skip this check if
141 // the timestamps are being auto-generated by Surface. If the app isn't
142 // generating timestamps explicitly, it probably doesn't want frames to
143 // be discarded based on them.
144 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
145 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700146
Lajos Molnar5f920c12015-07-13 16:04:24 -0700147 // If dropping entry[0] would leave us with a buffer that the
148 // consumer is not yet ready for, don't drop it.
149 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
150 break;
151 }
152
153 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
154 // additional criterion here: we only drop the earlier buffer if our
155 // desiredPresent falls within +/- 1 second of the expected present.
156 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
157 // relative timestamp), which normally mean "ignore the timestamp
158 // and acquire immediately", would cause us to drop frames.
159 //
160 // We may want to add an additional criterion: don't drop the
161 // earlier buffer if entry[1]'s fence hasn't signaled yet.
162 nsecs_t desiredPresent = bufferItem.mTimestamp;
163 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
164 desiredPresent > expectedPresent) {
165 // This buffer is set to display in the near future, or
166 // desiredPresent is garbage. Either way we don't want to drop
167 // the previous buffer just to get this on the screen sooner.
168 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
169 PRId64 " (%" PRId64 ") now=%" PRId64,
170 desiredPresent, expectedPresent,
171 desiredPresent - expectedPresent,
172 systemTime(CLOCK_MONOTONIC));
173 break;
174 }
175
176 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
177 " size=%zu",
178 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800179
180 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700181 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700182 mSlots[front->mSlot].mBufferState.freeQueued();
183
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700184 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700185 // still be around. Mark it as no longer shared if this
186 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700187 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700188 mSlots[front->mSlot].mBufferState.isFree()) {
189 mSlots[front->mSlot].mBufferState.mShared = false;
190 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800191
192 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700193 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800194 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700195 mCore->mFreeBuffers.push_back(front->mSlot);
196 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800197
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700198 if (mCore->mBufferReleasedCbEnabled) {
199 listener = mCore->mConnectedProducerListener;
200 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700201 ++numDroppedBuffers;
202 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800203
Lajos Molnar5f920c12015-07-13 16:04:24 -0700204 mCore->mQueue.erase(front);
205 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700206 }
207
Lajos Molnar5f920c12015-07-13 16:04:24 -0700208 // See if the front buffer is ready to be acquired
209 nsecs_t desiredPresent = front->mTimestamp;
210 bool bufferIsDue = desiredPresent <= expectedPresent ||
211 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
212 bool consumerIsReady = maxFrameNumber > 0 ?
213 front->mFrameNumber <= maxFrameNumber : true;
214 if (!bufferIsDue || !consumerIsReady) {
215 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
216 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
217 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700218 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800219 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700220 systemTime(CLOCK_MONOTONIC),
221 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700222 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700223 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800224 }
225
Lajos Molnar5f920c12015-07-13 16:04:24 -0700226 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
227 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800228 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700229 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800230 }
231
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700232 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
233
234 if (sharedBufferAvailable && mCore->mQueue.empty()) {
235 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200236 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700237
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700238 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700239
240 // Recreate the BufferItem for the shared buffer from the data that
241 // was cached when it was last queued.
242 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
243 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700244 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700245 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
246 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700247 ~static_cast<uint32_t>(
248 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700249 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
250 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700251 outBuffer->mFrameNumber = mCore->mFrameCounter;
252 outBuffer->mSlot = slot;
253 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
254 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700255 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700256 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
257 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700258 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800259 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700260 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800261 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800262 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
263 BQ_LOGV("acquireBuffer: front buffer is not droppable");
264 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700265 } else {
266 slot = front->mSlot;
267 *outBuffer = *front;
268 }
269
Lajos Molnar5f920c12015-07-13 16:04:24 -0700270 ATRACE_BUFFER_INDEX(slot);
271
272 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700273 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800274
275 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700276 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700277 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700278 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700279 // the queue is empty and the BufferItem is created above.
280 if (mCore->mQueue.empty()) {
281 mSlots[slot].mBufferState.acquireNotInQueue();
282 } else {
283 mSlots[slot].mBufferState.acquire();
284 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700285 mSlots[slot].mFence = Fence::NO_FENCE;
286 }
287
288 // If the buffer has previously been acquired by the consumer, set
289 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
290 // on the consumer side
291 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700292 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700293 }
294
295 mCore->mQueue.erase(front);
296
297 // We might have freed a slot while dropping old buffers, or the producer
298 // may be blocked waiting for the number of buffers in the queue to
299 // decrease.
Patrick Williams078d7362024-08-27 10:20:39 -0500300#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
301 mCore->notifyBufferReleased();
302#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200303 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500304#endif
Lajos Molnar5f920c12015-07-13 16:04:24 -0700305
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000306 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800307#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700308 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800309#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800310 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800311 }
312
Yi Kong48a619f2018-06-05 16:34:59 -0700313 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700314 for (int i = 0; i < numDroppedBuffers; ++i) {
315 listener->onBufferReleased();
316 }
Dan Stoza289ade12014-02-28 11:17:17 -0800317 }
318
Dan Stoza289ade12014-02-28 11:17:17 -0800319 return NO_ERROR;
320}
321
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322status_t BufferQueueConsumer::detachBuffer(int slot) {
323 ATRACE_CALL();
324 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700325 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000326 sp<IProducerListener> listener;
327 {
328 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800329
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000330 if (mCore->mIsAbandoned) {
331 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
332 return NO_INIT;
333 }
334
335 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
336 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
337 return BAD_VALUE;
338 }
339
340 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
341 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
342 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
343 return BAD_VALUE;
344 } else if (!mSlots[slot].mBufferState.isAcquired()) {
345 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
346 "(state = %s)", slot, mSlots[slot].mBufferState.string());
347 return BAD_VALUE;
348 }
349 if (mCore->mBufferReleasedCbEnabled) {
350 listener = mCore->mConnectedProducerListener;
351 }
352
353 mSlots[slot].mBufferState.detachConsumer();
354 mCore->mActiveBuffers.erase(slot);
355 mCore->mFreeSlots.insert(slot);
356 mCore->clearBufferSlotLocked(slot);
Patrick Williams078d7362024-08-27 10:20:39 -0500357#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
358 mCore->notifyBufferReleased();
359#else
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000360 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500361#endif
362
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000363 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800364 }
365
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000366 if (listener) {
367 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 return NO_ERROR;
370}
371
372status_t BufferQueueConsumer::attachBuffer(int* outSlot,
373 const sp<android::GraphicBuffer>& buffer) {
374 ATRACE_CALL();
375
Yi Kong48a619f2018-06-05 16:34:59 -0700376 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700377 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800378 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700379 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700380 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 return BAD_VALUE;
382 }
383
Sungtak Leecd217472024-07-19 17:17:40 +0000384 sp<IProducerListener> listener;
385 {
386 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800387
Sungtak Leecd217472024-07-19 17:17:40 +0000388 if (mCore->mSharedBufferMode) {
389 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
390 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391 }
Sungtak Leecd217472024-07-19 17:17:40 +0000392
393 // Make sure we don't have too many acquired buffers
394 int numAcquiredBuffers = 0;
395 for (int s : mCore->mActiveBuffers) {
396 if (mSlots[s].mBufferState.isAcquired()) {
397 ++numAcquiredBuffers;
398 }
399 }
400
401 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
402 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
403 "(max %d)", numAcquiredBuffers,
404 mCore->mMaxAcquiredBufferCount);
405 return INVALID_OPERATION;
406 }
407
408 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
409 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
410 "[queue %u]", buffer->getGenerationNumber(),
411 mCore->mGenerationNumber);
412 return BAD_VALUE;
413 }
414
415 // Find a free slot to put the buffer into
416 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
417 if (!mCore->mFreeSlots.empty()) {
418 auto slot = mCore->mFreeSlots.begin();
419 found = *slot;
420 mCore->mFreeSlots.erase(slot);
421 } else if (!mCore->mFreeBuffers.empty()) {
422 found = mCore->mFreeBuffers.front();
423 mCore->mFreeBuffers.remove(found);
424 }
425 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
426 BQ_LOGE("attachBuffer: could not find free buffer slot");
427 return NO_MEMORY;
428 }
429
430#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
431 if (mCore->mBufferAttachedCbEnabled) {
432 listener = mCore->mConnectedProducerListener;
433 }
434#endif
435
436 mCore->mActiveBuffers.insert(found);
437 *outSlot = found;
438 ATRACE_BUFFER_INDEX(*outSlot);
439 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
440
441 mSlots[*outSlot].mGraphicBuffer = buffer;
442 mSlots[*outSlot].mBufferState.attachConsumer();
443 mSlots[*outSlot].mNeedsReallocation = true;
444 mSlots[*outSlot].mFence = Fence::NO_FENCE;
445 mSlots[*outSlot].mFrameNumber = 0;
446
447 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
448 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
449 // Binder traffic by not un/flattening the GraphicBuffer. However, it
450 // requires that the consumer maintain a cached copy of the slot <--> buffer
451 // mappings, which is why the consumer doesn't need the valid pointer on
452 // acquire.
453 //
454 // The StreamSplitter is one of the primary users of the attach/detach
455 // logic, and while it is running, all buffers it acquires are immediately
456 // detached, and all buffers it eventually releases are ones that were
457 // attached (as opposed to having been obtained from acquireBuffer), so it
458 // doesn't make sense to maintain the slot/buffer mappings, which would
459 // become invalid for every buffer during detach/attach. By setting this to
460 // false, the valid GraphicBuffer pointer will always be sent with acquire
461 // for attached buffers.
462 mSlots[*outSlot].mAcquireCalled = false;
463
464 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800465 }
466
Sungtak Leecd217472024-07-19 17:17:40 +0000467#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
468 if (listener != nullptr) {
469 listener->onBufferAttached();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800470 }
Sungtak Leecd217472024-07-19 17:17:40 +0000471#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -0700472
Dan Stoza9f3053d2014-03-06 15:14:33 -0800473 return NO_ERROR;
474}
475
Dan Stoza289ade12014-02-28 11:17:17 -0800476status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
477 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
478 EGLSyncKHR eglFence) {
479 ATRACE_CALL();
480 ATRACE_BUFFER_INDEX(slot);
481
Dan Stoza9f3053d2014-03-06 15:14:33 -0800482 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700483 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700484 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
485 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800486 return BAD_VALUE;
487 }
488
Dan Stozad1c10362014-03-28 15:19:08 -0700489 sp<IProducerListener> listener;
490 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200491 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800492
Dan Stozad1c10362014-03-28 15:19:08 -0700493 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700494 // we can ignore this releaseBuffer for the old buffer.
495 // Ignore this for the shared buffer where the frame number can easily
496 // get out of sync due to the buffer being queued and acquired at the
497 // same time.
498 if (frameNumber != mSlots[slot].mFrameNumber &&
499 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700500 return STALE_BUFFER_SLOT;
501 }
Dan Stoza289ade12014-02-28 11:17:17 -0800502
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800503 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700504 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700505 "but its state was %s", slot,
506 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800507 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800508 }
Dan Stoza289ade12014-02-28 11:17:17 -0800509
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800510 mSlots[slot].mEglDisplay = eglDisplay;
511 mSlots[slot].mEglFence = eglFence;
512 mSlots[slot].mFence = releaseFence;
513 mSlots[slot].mBufferState.release();
514
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700515 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800516 // still be around. Mark it as no longer shared if this
517 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700518 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800519 mSlots[slot].mBufferState.mShared = false;
520 }
521 // Don't put the shared buffer on the free list.
522 if (!mSlots[slot].mBufferState.isShared()) {
523 mCore->mActiveBuffers.erase(slot);
524 mCore->mFreeBuffers.push_back(slot);
525 }
526
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700527 if (mCore->mBufferReleasedCbEnabled) {
528 listener = mCore->mConnectedProducerListener;
529 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800530 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
531
Patrick Williams078d7362024-08-27 10:20:39 -0500532#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
533 mCore->notifyBufferReleased();
534#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200535 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500536#endif
537
Pablo Ceballos9e314332016-01-12 13:49:19 -0800538 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700539 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800540
Dan Stozad1c10362014-03-28 15:19:08 -0700541 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700542 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700543 listener->onBufferReleased();
544 }
Dan Stoza289ade12014-02-28 11:17:17 -0800545
546 return NO_ERROR;
547}
548
549status_t BufferQueueConsumer::connect(
550 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
551 ATRACE_CALL();
552
Yi Kong48a619f2018-06-05 16:34:59 -0700553 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700554 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800555 return BAD_VALUE;
556 }
557
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700558 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800559 controlledByApp ? "true" : "false");
560
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200561 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800562
563 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700564 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800565 return NO_INIT;
566 }
567
568 mCore->mConsumerListener = consumerListener;
569 mCore->mConsumerControlledByApp = controlledByApp;
570
571 return NO_ERROR;
572}
573
574status_t BufferQueueConsumer::disconnect() {
575 ATRACE_CALL();
576
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700577 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800578
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200579 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800580
Yi Kong48a619f2018-06-05 16:34:59 -0700581 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700582 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800583 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800584 }
585
586 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700587 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800588 mCore->mQueue.clear();
589 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700590 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Patrick Williams078d7362024-08-27 10:20:39 -0500591#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
592 mCore->notifyBufferReleased();
593#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200594 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500595#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800596 return NO_ERROR;
597}
598
Dan Stozafebd4f42014-04-09 16:14:51 -0700599status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800600 ATRACE_CALL();
601
Yi Kong48a619f2018-06-05 16:34:59 -0700602 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800603 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
604 return BAD_VALUE;
605 }
606
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200607 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800608
609 if (mCore->mIsAbandoned) {
610 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
611 return NO_INIT;
612 }
613
Dan Stozafebd4f42014-04-09 16:14:51 -0700614 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800615 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800616 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700617 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800618 }
619 }
620
621 // Remove from the mask queued buffers for which acquire has been called,
622 // since the consumer will not receive their buffer addresses and so must
623 // retain their cached information
624 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
625 while (current != mCore->mQueue.end()) {
626 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700627 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800628 }
629 ++current;
630 }
631
Dan Stozafebd4f42014-04-09 16:14:51 -0700632 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800633 *outSlotMask = mask;
634 return NO_ERROR;
635}
636
637status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
638 uint32_t height) {
639 ATRACE_CALL();
640
641 if (width == 0 || height == 0) {
642 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
643 "height=%u)", width, height);
644 return BAD_VALUE;
645 }
646
647 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
648
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200649 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800650 mCore->mDefaultWidth = width;
651 mCore->mDefaultHeight = height;
652 return NO_ERROR;
653}
654
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700655status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800656 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800657
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700658 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
659 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
660 return BAD_VALUE;
661 }
Dan Stoza289ade12014-02-28 11:17:17 -0800662
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200663 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800664
Pablo Ceballos38273792016-03-02 01:38:10 +0000665 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
666 BQ_LOGE("setMaxBufferCount: producer is already connected");
667 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800668 }
669
Pablo Ceballos38273792016-03-02 01:38:10 +0000670 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
671 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
672 "mMaxAcquiredBufferCount (%d)", bufferCount,
673 mCore->mMaxAcquiredBufferCount);
674 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700675 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000676
677 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
678 mCore->mDequeueBufferCannotBlock, bufferCount) -
679 mCore->getMaxBufferCountLocked();
680 if (!mCore->adjustAvailableSlotsLocked(delta)) {
681 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
682 "available slots. Delta = %d", delta);
683 return BAD_VALUE;
684 }
685
686 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800687 return NO_ERROR;
688}
689
690status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
691 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700692 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800693
694 if (maxAcquiredBuffers < 1 ||
695 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
696 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
697 maxAcquiredBuffers);
698 return BAD_VALUE;
699 }
700
Pablo Ceballos38273792016-03-02 01:38:10 +0000701 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800702 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200703 std::unique_lock<std::mutex> lock(mCore->mMutex);
704 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800705
Pablo Ceballos72daab62015-12-07 16:38:43 -0800706 if (mCore->mIsAbandoned) {
707 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
708 return NO_INIT;
709 }
710
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700711 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
712 return NO_ERROR;
713 }
714
Pablo Ceballos72daab62015-12-07 16:38:43 -0800715 // The new maxAcquiredBuffers count should not be violated by the number
716 // of currently acquired buffers
717 int acquiredCount = 0;
718 for (int slot : mCore->mActiveBuffers) {
719 if (mSlots[slot].mBufferState.isAcquired()) {
720 acquiredCount++;
721 }
722 }
723 if (acquiredCount > maxAcquiredBuffers) {
724 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
725 "count (%d) exceeds the current acquired buffer count (%d)",
726 maxAcquiredBuffers, acquiredCount);
727 return BAD_VALUE;
728 }
729
730 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
731 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
732 > mCore->mMaxBufferCount) {
733 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
734 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
735 maxAcquiredBuffers, mCore->mMaxBufferCount,
736 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
737 mCore->mDequeueBufferCannotBlock);
738 return BAD_VALUE;
739 }
740
741 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000742 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800743 return BAD_VALUE;
744 }
745
746 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
747 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
748 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700749 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000750 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800751 }
752 }
753 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700754 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000755 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800756 }
757
Dan Stoza289ade12014-02-28 11:17:17 -0800758 return NO_ERROR;
759}
760
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700761status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800762 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000763 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200764 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800765 mCore->mConsumerName = name;
766 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700767 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800768}
769
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800770status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800771 ATRACE_CALL();
772 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200773 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800774 mCore->mDefaultBufferFormat = defaultFormat;
775 return NO_ERROR;
776}
777
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800778status_t BufferQueueConsumer::setDefaultBufferDataSpace(
779 android_dataspace defaultDataSpace) {
780 ATRACE_CALL();
781 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200782 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800783 mCore->mDefaultBufferDataSpace = defaultDataSpace;
784 return NO_ERROR;
785}
786
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700787status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800788 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700789 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200790 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800791 mCore->mConsumerUsageBits = usage;
792 return NO_ERROR;
793}
794
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700795status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
796 ATRACE_CALL();
797 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200798 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700799 mCore->mConsumerIsProtected = isProtected;
800 return NO_ERROR;
801}
802
Dan Stoza289ade12014-02-28 11:17:17 -0800803status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
804 ATRACE_CALL();
805 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200806 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800807 mCore->mTransformHint = hint;
808 return NO_ERROR;
809}
810
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700811status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200812 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700813 *outStream = mCore->mSidebandStream;
814 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800815}
816
Dan Stozae77c7662016-05-13 11:37:28 -0700817status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
818 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200819 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800820#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700821 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800822#else
823 (void)forceFlush;
824 outHistory->clear();
825#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700826 return NO_ERROR;
827}
828
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700829status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200830 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700831 mCore->discardFreeBuffersLocked();
832 return NO_ERROR;
833}
834
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700835status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700836 struct passwd* pwd = getpwnam("shell");
837 uid_t shellUid = pwd ? pwd->pw_uid : 0;
838 if (!shellUid) {
839 int savedErrno = errno;
840 BQ_LOGE("Cannot get AID_SHELL");
841 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
842 }
843
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800844 bool denied = false;
845 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800846#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900847 // permission check can't be done for vendors as vendors have no access to
Kiyoung Kim3f2638b2023-12-06 16:22:30 +0900848 // the PermissionController.
849 const pid_t pid = BufferQueueThreadState::getCallingPid();
850 if ((uid != shellUid) &&
851 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
852 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
853 "from pid=%d, uid=%d\n",
854 pid, uid);
855 denied = true;
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800856 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900857#else
858 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800859 denied = true;
860 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900861#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800862 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700863 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700864 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700865 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800866 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700867
868 mCore->dumpState(prefix, outResult);
869 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800870}
871
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800872void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
873 std::lock_guard<std::mutex> lock(mCore->mMutex);
874 mCore->mAllowExtraAcquire = allow;
875}
876
Dan Stoza289ade12014-02-28 11:17:17 -0800877} // namespace android