blob: 69d25be0066c1d7f8f6abb23284942923922d188 [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.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200300 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700301
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000302 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800303#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700304 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800305#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800306 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800307 }
308
Yi Kong48a619f2018-06-05 16:34:59 -0700309 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700310 for (int i = 0; i < numDroppedBuffers; ++i) {
311 listener->onBufferReleased();
312 }
Dan Stoza289ade12014-02-28 11:17:17 -0800313 }
314
Dan Stoza289ade12014-02-28 11:17:17 -0800315 return NO_ERROR;
316}
317
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318status_t BufferQueueConsumer::detachBuffer(int slot) {
319 ATRACE_CALL();
320 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700321 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000322 sp<IProducerListener> listener;
323 {
324 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000326 if (mCore->mIsAbandoned) {
327 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
328 return NO_INIT;
329 }
330
331 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
332 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
333 return BAD_VALUE;
334 }
335
336 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
337 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
338 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
339 return BAD_VALUE;
340 } else if (!mSlots[slot].mBufferState.isAcquired()) {
341 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
342 "(state = %s)", slot, mSlots[slot].mBufferState.string());
343 return BAD_VALUE;
344 }
345 if (mCore->mBufferReleasedCbEnabled) {
346 listener = mCore->mConnectedProducerListener;
347 }
348
349 mSlots[slot].mBufferState.detachConsumer();
350 mCore->mActiveBuffers.erase(slot);
351 mCore->mFreeSlots.insert(slot);
352 mCore->clearBufferSlotLocked(slot);
353 mCore->mDequeueCondition.notify_all();
354 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800355 }
356
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000357 if (listener) {
358 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800359 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800360 return NO_ERROR;
361}
362
363status_t BufferQueueConsumer::attachBuffer(int* outSlot,
364 const sp<android::GraphicBuffer>& buffer) {
365 ATRACE_CALL();
366
Yi Kong48a619f2018-06-05 16:34:59 -0700367 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700368 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700370 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700371 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 return BAD_VALUE;
373 }
374
Sungtak Leecd217472024-07-19 17:17:40 +0000375 sp<IProducerListener> listener;
376 {
377 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800378
Sungtak Leecd217472024-07-19 17:17:40 +0000379 if (mCore->mSharedBufferMode) {
380 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
381 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 }
Sungtak Leecd217472024-07-19 17:17:40 +0000383
384 // Make sure we don't have too many acquired buffers
385 int numAcquiredBuffers = 0;
386 for (int s : mCore->mActiveBuffers) {
387 if (mSlots[s].mBufferState.isAcquired()) {
388 ++numAcquiredBuffers;
389 }
390 }
391
392 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
393 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
394 "(max %d)", numAcquiredBuffers,
395 mCore->mMaxAcquiredBufferCount);
396 return INVALID_OPERATION;
397 }
398
399 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
400 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
401 "[queue %u]", buffer->getGenerationNumber(),
402 mCore->mGenerationNumber);
403 return BAD_VALUE;
404 }
405
406 // Find a free slot to put the buffer into
407 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
408 if (!mCore->mFreeSlots.empty()) {
409 auto slot = mCore->mFreeSlots.begin();
410 found = *slot;
411 mCore->mFreeSlots.erase(slot);
412 } else if (!mCore->mFreeBuffers.empty()) {
413 found = mCore->mFreeBuffers.front();
414 mCore->mFreeBuffers.remove(found);
415 }
416 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
417 BQ_LOGE("attachBuffer: could not find free buffer slot");
418 return NO_MEMORY;
419 }
420
421#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
422 if (mCore->mBufferAttachedCbEnabled) {
423 listener = mCore->mConnectedProducerListener;
424 }
425#endif
426
427 mCore->mActiveBuffers.insert(found);
428 *outSlot = found;
429 ATRACE_BUFFER_INDEX(*outSlot);
430 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
431
432 mSlots[*outSlot].mGraphicBuffer = buffer;
433 mSlots[*outSlot].mBufferState.attachConsumer();
434 mSlots[*outSlot].mNeedsReallocation = true;
435 mSlots[*outSlot].mFence = Fence::NO_FENCE;
436 mSlots[*outSlot].mFrameNumber = 0;
437
438 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
439 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
440 // Binder traffic by not un/flattening the GraphicBuffer. However, it
441 // requires that the consumer maintain a cached copy of the slot <--> buffer
442 // mappings, which is why the consumer doesn't need the valid pointer on
443 // acquire.
444 //
445 // The StreamSplitter is one of the primary users of the attach/detach
446 // logic, and while it is running, all buffers it acquires are immediately
447 // detached, and all buffers it eventually releases are ones that were
448 // attached (as opposed to having been obtained from acquireBuffer), so it
449 // doesn't make sense to maintain the slot/buffer mappings, which would
450 // become invalid for every buffer during detach/attach. By setting this to
451 // false, the valid GraphicBuffer pointer will always be sent with acquire
452 // for attached buffers.
453 mSlots[*outSlot].mAcquireCalled = false;
454
455 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800456 }
457
Sungtak Leecd217472024-07-19 17:17:40 +0000458#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
459 if (listener != nullptr) {
460 listener->onBufferAttached();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800461 }
Sungtak Leecd217472024-07-19 17:17:40 +0000462#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -0700463
Dan Stoza9f3053d2014-03-06 15:14:33 -0800464 return NO_ERROR;
465}
466
Dan Stoza289ade12014-02-28 11:17:17 -0800467status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
468 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
469 EGLSyncKHR eglFence) {
470 ATRACE_CALL();
471 ATRACE_BUFFER_INDEX(slot);
472
Dan Stoza9f3053d2014-03-06 15:14:33 -0800473 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700474 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700475 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
476 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800477 return BAD_VALUE;
478 }
479
Dan Stozad1c10362014-03-28 15:19:08 -0700480 sp<IProducerListener> listener;
481 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200482 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800483
Dan Stozad1c10362014-03-28 15:19:08 -0700484 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700485 // we can ignore this releaseBuffer for the old buffer.
486 // Ignore this for the shared buffer where the frame number can easily
487 // get out of sync due to the buffer being queued and acquired at the
488 // same time.
489 if (frameNumber != mSlots[slot].mFrameNumber &&
490 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700491 return STALE_BUFFER_SLOT;
492 }
Dan Stoza289ade12014-02-28 11:17:17 -0800493
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800494 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700495 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700496 "but its state was %s", slot,
497 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800498 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800499 }
Dan Stoza289ade12014-02-28 11:17:17 -0800500
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800501 mSlots[slot].mEglDisplay = eglDisplay;
502 mSlots[slot].mEglFence = eglFence;
503 mSlots[slot].mFence = releaseFence;
504 mSlots[slot].mBufferState.release();
505
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700506 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800507 // still be around. Mark it as no longer shared if this
508 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700509 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800510 mSlots[slot].mBufferState.mShared = false;
511 }
512 // Don't put the shared buffer on the free list.
513 if (!mSlots[slot].mBufferState.isShared()) {
514 mCore->mActiveBuffers.erase(slot);
515 mCore->mFreeBuffers.push_back(slot);
516 }
517
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700518 if (mCore->mBufferReleasedCbEnabled) {
519 listener = mCore->mConnectedProducerListener;
520 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800521 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
522
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200523 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800524 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700525 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800526
Dan Stozad1c10362014-03-28 15:19:08 -0700527 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700528 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700529 listener->onBufferReleased();
530 }
Dan Stoza289ade12014-02-28 11:17:17 -0800531
532 return NO_ERROR;
533}
534
535status_t BufferQueueConsumer::connect(
536 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
537 ATRACE_CALL();
538
Yi Kong48a619f2018-06-05 16:34:59 -0700539 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700540 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800541 return BAD_VALUE;
542 }
543
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700544 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800545 controlledByApp ? "true" : "false");
546
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200547 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800548
549 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700550 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800551 return NO_INIT;
552 }
553
554 mCore->mConsumerListener = consumerListener;
555 mCore->mConsumerControlledByApp = controlledByApp;
556
557 return NO_ERROR;
558}
559
560status_t BufferQueueConsumer::disconnect() {
561 ATRACE_CALL();
562
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700563 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800564
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200565 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800566
Yi Kong48a619f2018-06-05 16:34:59 -0700567 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700568 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800569 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800570 }
571
572 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700573 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800574 mCore->mQueue.clear();
575 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700576 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200577 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800578 return NO_ERROR;
579}
580
Dan Stozafebd4f42014-04-09 16:14:51 -0700581status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800582 ATRACE_CALL();
583
Yi Kong48a619f2018-06-05 16:34:59 -0700584 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800585 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
586 return BAD_VALUE;
587 }
588
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200589 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800590
591 if (mCore->mIsAbandoned) {
592 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
593 return NO_INIT;
594 }
595
Dan Stozafebd4f42014-04-09 16:14:51 -0700596 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800597 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800598 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700599 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800600 }
601 }
602
603 // Remove from the mask queued buffers for which acquire has been called,
604 // since the consumer will not receive their buffer addresses and so must
605 // retain their cached information
606 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
607 while (current != mCore->mQueue.end()) {
608 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700609 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800610 }
611 ++current;
612 }
613
Dan Stozafebd4f42014-04-09 16:14:51 -0700614 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800615 *outSlotMask = mask;
616 return NO_ERROR;
617}
618
619status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
620 uint32_t height) {
621 ATRACE_CALL();
622
623 if (width == 0 || height == 0) {
624 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
625 "height=%u)", width, height);
626 return BAD_VALUE;
627 }
628
629 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
630
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200631 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800632 mCore->mDefaultWidth = width;
633 mCore->mDefaultHeight = height;
634 return NO_ERROR;
635}
636
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700637status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800638 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800639
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700640 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
641 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
642 return BAD_VALUE;
643 }
Dan Stoza289ade12014-02-28 11:17:17 -0800644
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200645 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800646
Pablo Ceballos38273792016-03-02 01:38:10 +0000647 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
648 BQ_LOGE("setMaxBufferCount: producer is already connected");
649 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800650 }
651
Pablo Ceballos38273792016-03-02 01:38:10 +0000652 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
653 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
654 "mMaxAcquiredBufferCount (%d)", bufferCount,
655 mCore->mMaxAcquiredBufferCount);
656 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700657 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000658
659 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
660 mCore->mDequeueBufferCannotBlock, bufferCount) -
661 mCore->getMaxBufferCountLocked();
662 if (!mCore->adjustAvailableSlotsLocked(delta)) {
663 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
664 "available slots. Delta = %d", delta);
665 return BAD_VALUE;
666 }
667
668 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800669 return NO_ERROR;
670}
671
672status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
673 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700674 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800675
676 if (maxAcquiredBuffers < 1 ||
677 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
678 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
679 maxAcquiredBuffers);
680 return BAD_VALUE;
681 }
682
Pablo Ceballos38273792016-03-02 01:38:10 +0000683 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800684 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200685 std::unique_lock<std::mutex> lock(mCore->mMutex);
686 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800687
Pablo Ceballos72daab62015-12-07 16:38:43 -0800688 if (mCore->mIsAbandoned) {
689 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
690 return NO_INIT;
691 }
692
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700693 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
694 return NO_ERROR;
695 }
696
Pablo Ceballos72daab62015-12-07 16:38:43 -0800697 // The new maxAcquiredBuffers count should not be violated by the number
698 // of currently acquired buffers
699 int acquiredCount = 0;
700 for (int slot : mCore->mActiveBuffers) {
701 if (mSlots[slot].mBufferState.isAcquired()) {
702 acquiredCount++;
703 }
704 }
705 if (acquiredCount > maxAcquiredBuffers) {
706 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
707 "count (%d) exceeds the current acquired buffer count (%d)",
708 maxAcquiredBuffers, acquiredCount);
709 return BAD_VALUE;
710 }
711
712 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
713 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
714 > mCore->mMaxBufferCount) {
715 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
716 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
717 maxAcquiredBuffers, mCore->mMaxBufferCount,
718 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
719 mCore->mDequeueBufferCannotBlock);
720 return BAD_VALUE;
721 }
722
723 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000724 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800725 return BAD_VALUE;
726 }
727
728 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
729 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
730 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700731 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000732 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800733 }
734 }
735 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700736 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000737 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800738 }
739
Dan Stoza289ade12014-02-28 11:17:17 -0800740 return NO_ERROR;
741}
742
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700743status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800744 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000745 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200746 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800747 mCore->mConsumerName = name;
748 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700749 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800750}
751
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800752status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800753 ATRACE_CALL();
754 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200755 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800756 mCore->mDefaultBufferFormat = defaultFormat;
757 return NO_ERROR;
758}
759
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800760status_t BufferQueueConsumer::setDefaultBufferDataSpace(
761 android_dataspace defaultDataSpace) {
762 ATRACE_CALL();
763 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200764 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800765 mCore->mDefaultBufferDataSpace = defaultDataSpace;
766 return NO_ERROR;
767}
768
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700769status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800770 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700771 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200772 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800773 mCore->mConsumerUsageBits = usage;
774 return NO_ERROR;
775}
776
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700777status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
778 ATRACE_CALL();
779 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200780 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700781 mCore->mConsumerIsProtected = isProtected;
782 return NO_ERROR;
783}
784
Dan Stoza289ade12014-02-28 11:17:17 -0800785status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
786 ATRACE_CALL();
787 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200788 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800789 mCore->mTransformHint = hint;
790 return NO_ERROR;
791}
792
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700793status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200794 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700795 *outStream = mCore->mSidebandStream;
796 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800797}
798
Dan Stozae77c7662016-05-13 11:37:28 -0700799status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
800 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200801 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800802#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700803 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800804#else
805 (void)forceFlush;
806 outHistory->clear();
807#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700808 return NO_ERROR;
809}
810
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700811status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200812 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700813 mCore->discardFreeBuffersLocked();
814 return NO_ERROR;
815}
816
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700817status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700818 struct passwd* pwd = getpwnam("shell");
819 uid_t shellUid = pwd ? pwd->pw_uid : 0;
820 if (!shellUid) {
821 int savedErrno = errno;
822 BQ_LOGE("Cannot get AID_SHELL");
823 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
824 }
825
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800826 bool denied = false;
827 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800828#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900829 // permission check can't be done for vendors as vendors have no access to
Kiyoung Kim3f2638b2023-12-06 16:22:30 +0900830 // the PermissionController.
831 const pid_t pid = BufferQueueThreadState::getCallingPid();
832 if ((uid != shellUid) &&
833 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
834 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
835 "from pid=%d, uid=%d\n",
836 pid, uid);
837 denied = true;
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800838 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900839#else
840 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800841 denied = true;
842 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900843#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800844 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700845 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700846 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700847 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800848 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700849
850 mCore->dumpState(prefix, outResult);
851 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800852}
853
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800854void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
855 std::lock_guard<std::mutex> lock(mCore->mMutex);
856 mCore->mAllowExtraAcquire = allow;
857}
858
Dan Stoza289ade12014-02-28 11:17:17 -0800859} // namespace android