blob: 11f5174d7629e60a39a32dc0433a83672db398f2 [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
Dan Stoza289ade12014-02-28 11:17:17 -080045namespace android {
46
Iris Chang430193f2019-12-04 16:25:46 +080047// Macros for include BufferQueueCore information in log messages
48#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000049 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080050 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
51 ##__VA_ARGS__)
52#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000053 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080054 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
55 ##__VA_ARGS__)
56#define BQ_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000057 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080058 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
59 ##__VA_ARGS__)
60#define BQ_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000061 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080062 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
63 ##__VA_ARGS__)
64#define BQ_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000065 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080066 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
67 ##__VA_ARGS__)
68
Chong Zhang62493092020-01-15 16:04:47 -080069ConsumerListener::~ConsumerListener() = default;
70
Dan Stoza289ade12014-02-28 11:17:17 -080071BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
72 mCore(core),
73 mSlots(core->mSlots),
74 mConsumerName() {}
75
76BufferQueueConsumer::~BufferQueueConsumer() {}
77
78status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070079 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080080 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080081
Lajos Molnar5f920c12015-07-13 16:04:24 -070082 int numDroppedBuffers = 0;
83 sp<IProducerListener> listener;
84 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020085 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070086
87 // Check that the consumer doesn't currently have the maximum number of
88 // buffers acquired. We allow the max buffer count to be exceeded by one
89 // buffer so that the consumer can successfully set up the newly acquired
90 // buffer before releasing the old one.
91 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080092 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070093 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070094 ++numAcquiredBuffers;
95 }
Dan Stoza289ade12014-02-28 11:17:17 -080096 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -080097 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
98 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
99 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
100 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700101 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
102 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
103 return INVALID_OPERATION;
104 }
Dan Stoza289ade12014-02-28 11:17:17 -0800105
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700106 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
107 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700108 BufferQueueCore::INVALID_BUFFER_SLOT;
109
Lajos Molnar5f920c12015-07-13 16:04:24 -0700110 // In asynchronous mode the list is guaranteed to be one buffer deep,
111 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700113 return NO_BUFFER_AVAILABLE;
114 }
Dan Stoza289ade12014-02-28 11:17:17 -0800115
Lajos Molnar5f920c12015-07-13 16:04:24 -0700116 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800117
Lajos Molnar5f920c12015-07-13 16:04:24 -0700118 // If expectedPresent is specified, we may not want to return a buffer yet.
119 // If it's specified and there's more than one buffer queued, we may want
120 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700121 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700122 // since in that case we'll just return the shared buffer.
123 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700124 // The 'expectedPresent' argument indicates when the buffer is expected
125 // to be presented on-screen. If the buffer's desired present time is
126 // earlier (less) than expectedPresent -- meaning it will be displayed
127 // on time or possibly late if we show it as soon as possible -- we
128 // acquire and return it. If we don't want to display it until after the
129 // expectedPresent time, we return PRESENT_LATER without acquiring it.
130 //
131 // To be safe, we don't defer acquisition if expectedPresent is more
132 // than one second in the future beyond the desired present time
133 // (i.e., we'd be holding the buffer for a long time).
134 //
135 // NOTE: Code assumes monotonic time values from the system clock
136 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800137
Lajos Molnar5f920c12015-07-13 16:04:24 -0700138 // Start by checking to see if we can drop frames. We skip this check if
139 // the timestamps are being auto-generated by Surface. If the app isn't
140 // generating timestamps explicitly, it probably doesn't want frames to
141 // be discarded based on them.
142 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
143 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700144
Lajos Molnar5f920c12015-07-13 16:04:24 -0700145 // If dropping entry[0] would leave us with a buffer that the
146 // consumer is not yet ready for, don't drop it.
147 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
148 break;
149 }
150
151 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
152 // additional criterion here: we only drop the earlier buffer if our
153 // desiredPresent falls within +/- 1 second of the expected present.
154 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
155 // relative timestamp), which normally mean "ignore the timestamp
156 // and acquire immediately", would cause us to drop frames.
157 //
158 // We may want to add an additional criterion: don't drop the
159 // earlier buffer if entry[1]'s fence hasn't signaled yet.
160 nsecs_t desiredPresent = bufferItem.mTimestamp;
161 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
162 desiredPresent > expectedPresent) {
163 // This buffer is set to display in the near future, or
164 // desiredPresent is garbage. Either way we don't want to drop
165 // the previous buffer just to get this on the screen sooner.
166 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
167 PRId64 " (%" PRId64 ") now=%" PRId64,
168 desiredPresent, expectedPresent,
169 desiredPresent - expectedPresent,
170 systemTime(CLOCK_MONOTONIC));
171 break;
172 }
173
174 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
175 " size=%zu",
176 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800177
178 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700179 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700180 mSlots[front->mSlot].mBufferState.freeQueued();
181
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700182 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700183 // still be around. Mark it as no longer shared if this
184 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700185 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700186 mSlots[front->mSlot].mBufferState.isFree()) {
187 mSlots[front->mSlot].mBufferState.mShared = false;
188 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800189
190 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700191 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800192 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700193 mCore->mFreeBuffers.push_back(front->mSlot);
194 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800195
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700196 if (mCore->mBufferReleasedCbEnabled) {
197 listener = mCore->mConnectedProducerListener;
198 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700199 ++numDroppedBuffers;
200 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800201
Lajos Molnar5f920c12015-07-13 16:04:24 -0700202 mCore->mQueue.erase(front);
203 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700204 }
205
Lajos Molnar5f920c12015-07-13 16:04:24 -0700206 // See if the front buffer is ready to be acquired
207 nsecs_t desiredPresent = front->mTimestamp;
208 bool bufferIsDue = desiredPresent <= expectedPresent ||
209 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
210 bool consumerIsReady = maxFrameNumber > 0 ?
211 front->mFrameNumber <= maxFrameNumber : true;
212 if (!bufferIsDue || !consumerIsReady) {
213 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
214 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
215 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700216 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800217 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700218 systemTime(CLOCK_MONOTONIC),
219 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700220 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700221 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800222 }
223
Lajos Molnar5f920c12015-07-13 16:04:24 -0700224 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
225 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800226 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700227 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800228 }
229
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700230 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
231
232 if (sharedBufferAvailable && mCore->mQueue.empty()) {
233 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200234 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700235
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700236 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700237
238 // Recreate the BufferItem for the shared buffer from the data that
239 // was cached when it was last queued.
240 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
241 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700242 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700243 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
244 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700245 ~static_cast<uint32_t>(
246 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700247 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
248 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700249 outBuffer->mFrameNumber = mCore->mFrameCounter;
250 outBuffer->mSlot = slot;
251 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
252 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700253 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700254 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
255 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700256 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800257 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700258 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800259 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800260 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
261 BQ_LOGV("acquireBuffer: front buffer is not droppable");
262 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700263 } else {
264 slot = front->mSlot;
265 *outBuffer = *front;
266 }
267
Lajos Molnar5f920c12015-07-13 16:04:24 -0700268 ATRACE_BUFFER_INDEX(slot);
269
270 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700271 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800272
273 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700274 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700275 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700276 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700277 // the queue is empty and the BufferItem is created above.
278 if (mCore->mQueue.empty()) {
279 mSlots[slot].mBufferState.acquireNotInQueue();
280 } else {
281 mSlots[slot].mBufferState.acquire();
282 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700283 mSlots[slot].mFence = Fence::NO_FENCE;
284 }
285
286 // If the buffer has previously been acquired by the consumer, set
287 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
288 // on the consumer side
289 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700290 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700291 }
292
293 mCore->mQueue.erase(front);
294
295 // We might have freed a slot while dropping old buffers, or the producer
296 // may be blocked waiting for the number of buffers in the queue to
297 // decrease.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200298 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700299
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000300 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800301#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700302 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800303#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800304 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800305 }
306
Yi Kong48a619f2018-06-05 16:34:59 -0700307 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700308 for (int i = 0; i < numDroppedBuffers; ++i) {
309 listener->onBufferReleased();
310 }
Dan Stoza289ade12014-02-28 11:17:17 -0800311 }
312
Dan Stoza289ade12014-02-28 11:17:17 -0800313 return NO_ERROR;
314}
315
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316status_t BufferQueueConsumer::detachBuffer(int slot) {
317 ATRACE_CALL();
318 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700319 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000320 sp<IProducerListener> listener;
321 {
322 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800323
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000324 if (mCore->mIsAbandoned) {
325 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
326 return NO_INIT;
327 }
328
329 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
330 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
331 return BAD_VALUE;
332 }
333
334 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
335 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
336 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
337 return BAD_VALUE;
338 } else if (!mSlots[slot].mBufferState.isAcquired()) {
339 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
340 "(state = %s)", slot, mSlots[slot].mBufferState.string());
341 return BAD_VALUE;
342 }
343 if (mCore->mBufferReleasedCbEnabled) {
344 listener = mCore->mConnectedProducerListener;
345 }
346
347 mSlots[slot].mBufferState.detachConsumer();
348 mCore->mActiveBuffers.erase(slot);
349 mCore->mFreeSlots.insert(slot);
350 mCore->clearBufferSlotLocked(slot);
351 mCore->mDequeueCondition.notify_all();
352 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800353 }
354
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000355 if (listener) {
356 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800357 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800358 return NO_ERROR;
359}
360
361status_t BufferQueueConsumer::attachBuffer(int* outSlot,
362 const sp<android::GraphicBuffer>& buffer) {
363 ATRACE_CALL();
364
Yi Kong48a619f2018-06-05 16:34:59 -0700365 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700366 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800367 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700368 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700369 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800370 return BAD_VALUE;
371 }
372
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200373 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800374
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700375 if (mCore->mSharedBufferMode) {
376 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700377 return BAD_VALUE;
378 }
379
Dan Stoza0de7ea72015-04-23 13:20:51 -0700380 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800382 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700383 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800384 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800385 }
386 }
387
388 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700389 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800390 "(max %d)", numAcquiredBuffers,
391 mCore->mMaxAcquiredBufferCount);
392 return INVALID_OPERATION;
393 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700394
Dan Stoza812ed062015-06-02 15:45:22 -0700395 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
396 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
397 "[queue %u]", buffer->getGenerationNumber(),
398 mCore->mGenerationNumber);
399 return BAD_VALUE;
400 }
401
Dan Stoza0de7ea72015-04-23 13:20:51 -0700402 // Find a free slot to put the buffer into
403 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
404 if (!mCore->mFreeSlots.empty()) {
405 auto slot = mCore->mFreeSlots.begin();
406 found = *slot;
407 mCore->mFreeSlots.erase(slot);
408 } else if (!mCore->mFreeBuffers.empty()) {
409 found = mCore->mFreeBuffers.front();
410 mCore->mFreeBuffers.remove(found);
411 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800412 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700413 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800414 return NO_MEMORY;
415 }
416
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800417 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800418 *outSlot = found;
419 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700420 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800421
422 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700423 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800424 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800425 mSlots[*outSlot].mFence = Fence::NO_FENCE;
426 mSlots[*outSlot].mFrameNumber = 0;
427
Dan Stoza99b18b42014-03-28 15:34:33 -0700428 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
429 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
430 // Binder traffic by not un/flattening the GraphicBuffer. However, it
431 // requires that the consumer maintain a cached copy of the slot <--> buffer
432 // mappings, which is why the consumer doesn't need the valid pointer on
433 // acquire.
434 //
435 // The StreamSplitter is one of the primary users of the attach/detach
436 // logic, and while it is running, all buffers it acquires are immediately
437 // detached, and all buffers it eventually releases are ones that were
438 // attached (as opposed to having been obtained from acquireBuffer), so it
439 // doesn't make sense to maintain the slot/buffer mappings, which would
440 // become invalid for every buffer during detach/attach. By setting this to
441 // false, the valid GraphicBuffer pointer will always be sent with acquire
442 // for attached buffers.
443 mSlots[*outSlot].mAcquireCalled = false;
444
Pablo Ceballos9e314332016-01-12 13:49:19 -0800445 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700446
Dan Stoza9f3053d2014-03-06 15:14:33 -0800447 return NO_ERROR;
448}
449
Dan Stoza289ade12014-02-28 11:17:17 -0800450status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
451 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
452 EGLSyncKHR eglFence) {
453 ATRACE_CALL();
454 ATRACE_BUFFER_INDEX(slot);
455
Dan Stoza9f3053d2014-03-06 15:14:33 -0800456 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700457 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700458 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
459 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800460 return BAD_VALUE;
461 }
462
Dan Stozad1c10362014-03-28 15:19:08 -0700463 sp<IProducerListener> listener;
464 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200465 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800466
Dan Stozad1c10362014-03-28 15:19:08 -0700467 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700468 // we can ignore this releaseBuffer for the old buffer.
469 // Ignore this for the shared buffer where the frame number can easily
470 // get out of sync due to the buffer being queued and acquired at the
471 // same time.
472 if (frameNumber != mSlots[slot].mFrameNumber &&
473 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700474 return STALE_BUFFER_SLOT;
475 }
Dan Stoza289ade12014-02-28 11:17:17 -0800476
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800477 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700478 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700479 "but its state was %s", slot,
480 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800481 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800482 }
Dan Stoza289ade12014-02-28 11:17:17 -0800483
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800484 mSlots[slot].mEglDisplay = eglDisplay;
485 mSlots[slot].mEglFence = eglFence;
486 mSlots[slot].mFence = releaseFence;
487 mSlots[slot].mBufferState.release();
488
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700489 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800490 // still be around. Mark it as no longer shared if this
491 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700492 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800493 mSlots[slot].mBufferState.mShared = false;
494 }
495 // Don't put the shared buffer on the free list.
496 if (!mSlots[slot].mBufferState.isShared()) {
497 mCore->mActiveBuffers.erase(slot);
498 mCore->mFreeBuffers.push_back(slot);
499 }
500
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700501 if (mCore->mBufferReleasedCbEnabled) {
502 listener = mCore->mConnectedProducerListener;
503 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800504 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
505
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200506 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800507 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700508 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800509
Dan Stozad1c10362014-03-28 15:19:08 -0700510 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700511 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700512 listener->onBufferReleased();
513 }
Dan Stoza289ade12014-02-28 11:17:17 -0800514
515 return NO_ERROR;
516}
517
518status_t BufferQueueConsumer::connect(
519 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
520 ATRACE_CALL();
521
Yi Kong48a619f2018-06-05 16:34:59 -0700522 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700523 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800524 return BAD_VALUE;
525 }
526
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700527 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800528 controlledByApp ? "true" : "false");
529
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200530 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800531
532 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700533 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800534 return NO_INIT;
535 }
536
537 mCore->mConsumerListener = consumerListener;
538 mCore->mConsumerControlledByApp = controlledByApp;
539
540 return NO_ERROR;
541}
542
543status_t BufferQueueConsumer::disconnect() {
544 ATRACE_CALL();
545
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700546 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800547
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200548 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800549
Yi Kong48a619f2018-06-05 16:34:59 -0700550 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700551 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800552 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800553 }
554
555 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700556 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800557 mCore->mQueue.clear();
558 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700559 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200560 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800561 return NO_ERROR;
562}
563
Dan Stozafebd4f42014-04-09 16:14:51 -0700564status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800565 ATRACE_CALL();
566
Yi Kong48a619f2018-06-05 16:34:59 -0700567 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800568 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
569 return BAD_VALUE;
570 }
571
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200572 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800573
574 if (mCore->mIsAbandoned) {
575 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
576 return NO_INIT;
577 }
578
Dan Stozafebd4f42014-04-09 16:14:51 -0700579 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800580 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800581 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700582 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800583 }
584 }
585
586 // Remove from the mask queued buffers for which acquire has been called,
587 // since the consumer will not receive their buffer addresses and so must
588 // retain their cached information
589 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
590 while (current != mCore->mQueue.end()) {
591 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700592 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800593 }
594 ++current;
595 }
596
Dan Stozafebd4f42014-04-09 16:14:51 -0700597 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800598 *outSlotMask = mask;
599 return NO_ERROR;
600}
601
602status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
603 uint32_t height) {
604 ATRACE_CALL();
605
606 if (width == 0 || height == 0) {
607 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
608 "height=%u)", width, height);
609 return BAD_VALUE;
610 }
611
612 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
613
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200614 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800615 mCore->mDefaultWidth = width;
616 mCore->mDefaultHeight = height;
617 return NO_ERROR;
618}
619
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700620status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800621 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800622
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700623 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
624 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
625 return BAD_VALUE;
626 }
Dan Stoza289ade12014-02-28 11:17:17 -0800627
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200628 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800629
Pablo Ceballos38273792016-03-02 01:38:10 +0000630 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
631 BQ_LOGE("setMaxBufferCount: producer is already connected");
632 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800633 }
634
Pablo Ceballos38273792016-03-02 01:38:10 +0000635 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
636 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
637 "mMaxAcquiredBufferCount (%d)", bufferCount,
638 mCore->mMaxAcquiredBufferCount);
639 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700640 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000641
642 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
643 mCore->mDequeueBufferCannotBlock, bufferCount) -
644 mCore->getMaxBufferCountLocked();
645 if (!mCore->adjustAvailableSlotsLocked(delta)) {
646 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
647 "available slots. Delta = %d", delta);
648 return BAD_VALUE;
649 }
650
651 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800652 return NO_ERROR;
653}
654
655status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
656 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700657 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800658
659 if (maxAcquiredBuffers < 1 ||
660 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
661 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
662 maxAcquiredBuffers);
663 return BAD_VALUE;
664 }
665
Pablo Ceballos38273792016-03-02 01:38:10 +0000666 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800667 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200668 std::unique_lock<std::mutex> lock(mCore->mMutex);
669 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800670
Pablo Ceballos72daab62015-12-07 16:38:43 -0800671 if (mCore->mIsAbandoned) {
672 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
673 return NO_INIT;
674 }
675
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700676 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
677 return NO_ERROR;
678 }
679
Pablo Ceballos72daab62015-12-07 16:38:43 -0800680 // The new maxAcquiredBuffers count should not be violated by the number
681 // of currently acquired buffers
682 int acquiredCount = 0;
683 for (int slot : mCore->mActiveBuffers) {
684 if (mSlots[slot].mBufferState.isAcquired()) {
685 acquiredCount++;
686 }
687 }
688 if (acquiredCount > maxAcquiredBuffers) {
689 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
690 "count (%d) exceeds the current acquired buffer count (%d)",
691 maxAcquiredBuffers, acquiredCount);
692 return BAD_VALUE;
693 }
694
695 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
696 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
697 > mCore->mMaxBufferCount) {
698 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
699 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
700 maxAcquiredBuffers, mCore->mMaxBufferCount,
701 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
702 mCore->mDequeueBufferCannotBlock);
703 return BAD_VALUE;
704 }
705
706 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000707 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800708 return BAD_VALUE;
709 }
710
711 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
712 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
713 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700714 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000715 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800716 }
717 }
718 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700719 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000720 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800721 }
722
Dan Stoza289ade12014-02-28 11:17:17 -0800723 return NO_ERROR;
724}
725
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700726status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800727 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000728 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200729 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800730 mCore->mConsumerName = name;
731 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700732 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800733}
734
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800735status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800736 ATRACE_CALL();
737 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200738 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800739 mCore->mDefaultBufferFormat = defaultFormat;
740 return NO_ERROR;
741}
742
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800743status_t BufferQueueConsumer::setDefaultBufferDataSpace(
744 android_dataspace defaultDataSpace) {
745 ATRACE_CALL();
746 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200747 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800748 mCore->mDefaultBufferDataSpace = defaultDataSpace;
749 return NO_ERROR;
750}
751
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700752status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800753 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700754 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
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->mConsumerUsageBits = usage;
757 return NO_ERROR;
758}
759
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700760status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
761 ATRACE_CALL();
762 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200763 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700764 mCore->mConsumerIsProtected = isProtected;
765 return NO_ERROR;
766}
767
Dan Stoza289ade12014-02-28 11:17:17 -0800768status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
769 ATRACE_CALL();
770 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200771 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800772 mCore->mTransformHint = hint;
773 return NO_ERROR;
774}
775
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700776status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200777 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700778 *outStream = mCore->mSidebandStream;
779 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800780}
781
Dan Stozae77c7662016-05-13 11:37:28 -0700782status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
783 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200784 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800785#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700786 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800787#else
788 (void)forceFlush;
789 outHistory->clear();
790#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700791 return NO_ERROR;
792}
793
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700794status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200795 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700796 mCore->discardFreeBuffersLocked();
797 return NO_ERROR;
798}
799
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700800status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700801 struct passwd* pwd = getpwnam("shell");
802 uid_t shellUid = pwd ? pwd->pw_uid : 0;
803 if (!shellUid) {
804 int savedErrno = errno;
805 BQ_LOGE("Cannot get AID_SHELL");
806 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
807 }
808
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800809 bool denied = false;
810 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800811#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900812 // permission check can't be done for vendors as vendors have no access to
Kiyoung Kim3f2638b2023-12-06 16:22:30 +0900813 // the PermissionController.
814 const pid_t pid = BufferQueueThreadState::getCallingPid();
815 if ((uid != shellUid) &&
816 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
817 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
818 "from pid=%d, uid=%d\n",
819 pid, uid);
820 denied = true;
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800821 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900822#else
823 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800824 denied = true;
825 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900826#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800827 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700828 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700829 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700830 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800831 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700832
833 mCore->dumpState(prefix, outResult);
834 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800835}
836
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800837void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
838 std::lock_guard<std::mutex> lock(mCore->mMutex);
839 mCore->mAllowExtraAcquire = allow;
840}
841
Dan Stoza289ade12014-02-28 11:17:17 -0800842} // namespace android