blob: b6a47fb4e952ec1140bfc0249b3e5bd3cc33f814 [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>
Jiyong Park47f876b2018-04-17 13:56:46 +090039#ifndef __ANDROID_VNDK__
Pablo Ceballos88f69282016-02-11 18:01:49 -080040#include <binder/PermissionCache.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080041#include <vndksupport/linker.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090042#endif
Pablo Ceballos88f69282016-02-11 18:01:49 -080043
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070044#include <system/window.h>
45
Dan Stoza289ade12014-02-28 11:17:17 -080046namespace android {
47
Iris Chang430193f2019-12-04 16:25:46 +080048// Macros for include BufferQueueCore information in log messages
49#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000050 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080051 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
52 ##__VA_ARGS__)
53#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000054 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080055 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
56 ##__VA_ARGS__)
57#define BQ_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000058 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080059 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
60 ##__VA_ARGS__)
61#define BQ_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000062 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080063 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
64 ##__VA_ARGS__)
65#define BQ_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000066 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080067 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
68 ##__VA_ARGS__)
69
Chong Zhang62493092020-01-15 16:04:47 -080070ConsumerListener::~ConsumerListener() = default;
71
Dan Stoza289ade12014-02-28 11:17:17 -080072BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
73 mCore(core),
74 mSlots(core->mSlots),
75 mConsumerName() {}
76
77BufferQueueConsumer::~BufferQueueConsumer() {}
78
79status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070080 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080081 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080082
Lajos Molnar5f920c12015-07-13 16:04:24 -070083 int numDroppedBuffers = 0;
84 sp<IProducerListener> listener;
85 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020086 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070087
88 // Check that the consumer doesn't currently have the maximum number of
89 // buffers acquired. We allow the max buffer count to be exceeded by one
90 // buffer so that the consumer can successfully set up the newly acquired
91 // buffer before releasing the old one.
92 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080093 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070094 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070095 ++numAcquiredBuffers;
96 }
Dan Stoza289ade12014-02-28 11:17:17 -080097 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -080098 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
99 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
100 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
101 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700102 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
103 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
104 return INVALID_OPERATION;
105 }
Dan Stoza289ade12014-02-28 11:17:17 -0800106
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700107 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
108 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700109 BufferQueueCore::INVALID_BUFFER_SLOT;
110
Lajos Molnar5f920c12015-07-13 16:04:24 -0700111 // In asynchronous mode the list is guaranteed to be one buffer deep,
112 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700113 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700114 return NO_BUFFER_AVAILABLE;
115 }
Dan Stoza289ade12014-02-28 11:17:17 -0800116
Lajos Molnar5f920c12015-07-13 16:04:24 -0700117 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800118
Lajos Molnar5f920c12015-07-13 16:04:24 -0700119 // If expectedPresent is specified, we may not want to return a buffer yet.
120 // If it's specified and there's more than one buffer queued, we may want
121 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700122 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700123 // since in that case we'll just return the shared buffer.
124 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700125 // The 'expectedPresent' argument indicates when the buffer is expected
126 // to be presented on-screen. If the buffer's desired present time is
127 // earlier (less) than expectedPresent -- meaning it will be displayed
128 // on time or possibly late if we show it as soon as possible -- we
129 // acquire and return it. If we don't want to display it until after the
130 // expectedPresent time, we return PRESENT_LATER without acquiring it.
131 //
132 // To be safe, we don't defer acquisition if expectedPresent is more
133 // than one second in the future beyond the desired present time
134 // (i.e., we'd be holding the buffer for a long time).
135 //
136 // NOTE: Code assumes monotonic time values from the system clock
137 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800138
Lajos Molnar5f920c12015-07-13 16:04:24 -0700139 // Start by checking to see if we can drop frames. We skip this check if
140 // the timestamps are being auto-generated by Surface. If the app isn't
141 // generating timestamps explicitly, it probably doesn't want frames to
142 // be discarded based on them.
143 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
144 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700145
Lajos Molnar5f920c12015-07-13 16:04:24 -0700146 // If dropping entry[0] would leave us with a buffer that the
147 // consumer is not yet ready for, don't drop it.
148 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
149 break;
150 }
151
152 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
153 // additional criterion here: we only drop the earlier buffer if our
154 // desiredPresent falls within +/- 1 second of the expected present.
155 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
156 // relative timestamp), which normally mean "ignore the timestamp
157 // and acquire immediately", would cause us to drop frames.
158 //
159 // We may want to add an additional criterion: don't drop the
160 // earlier buffer if entry[1]'s fence hasn't signaled yet.
161 nsecs_t desiredPresent = bufferItem.mTimestamp;
162 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
163 desiredPresent > expectedPresent) {
164 // This buffer is set to display in the near future, or
165 // desiredPresent is garbage. Either way we don't want to drop
166 // the previous buffer just to get this on the screen sooner.
167 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
168 PRId64 " (%" PRId64 ") now=%" PRId64,
169 desiredPresent, expectedPresent,
170 desiredPresent - expectedPresent,
171 systemTime(CLOCK_MONOTONIC));
172 break;
173 }
174
175 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
176 " size=%zu",
177 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800178
179 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700180 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700181 mSlots[front->mSlot].mBufferState.freeQueued();
182
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700183 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700184 // still be around. Mark it as no longer shared if this
185 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700186 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700187 mSlots[front->mSlot].mBufferState.isFree()) {
188 mSlots[front->mSlot].mBufferState.mShared = false;
189 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800190
191 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700192 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800193 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700194 mCore->mFreeBuffers.push_back(front->mSlot);
195 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800196
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700197 if (mCore->mBufferReleasedCbEnabled) {
198 listener = mCore->mConnectedProducerListener;
199 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700200 ++numDroppedBuffers;
201 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800202
Lajos Molnar5f920c12015-07-13 16:04:24 -0700203 mCore->mQueue.erase(front);
204 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700205 }
206
Lajos Molnar5f920c12015-07-13 16:04:24 -0700207 // See if the front buffer is ready to be acquired
208 nsecs_t desiredPresent = front->mTimestamp;
209 bool bufferIsDue = desiredPresent <= expectedPresent ||
210 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
211 bool consumerIsReady = maxFrameNumber > 0 ?
212 front->mFrameNumber <= maxFrameNumber : true;
213 if (!bufferIsDue || !consumerIsReady) {
214 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
215 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
216 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700217 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800218 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700219 systemTime(CLOCK_MONOTONIC),
220 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700221 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700222 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800223 }
224
Lajos Molnar5f920c12015-07-13 16:04:24 -0700225 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
226 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800227 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700228 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800229 }
230
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700231 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
232
233 if (sharedBufferAvailable && mCore->mQueue.empty()) {
234 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200235 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700236
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700237 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700238
239 // Recreate the BufferItem for the shared buffer from the data that
240 // was cached when it was last queued.
241 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
242 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700243 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700244 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
245 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700246 ~static_cast<uint32_t>(
247 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700248 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
249 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700250 outBuffer->mFrameNumber = mCore->mFrameCounter;
251 outBuffer->mSlot = slot;
252 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
253 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700254 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700255 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
256 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700257 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800258 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700259 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800260 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800261 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
262 BQ_LOGV("acquireBuffer: front buffer is not droppable");
263 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700264 } else {
265 slot = front->mSlot;
266 *outBuffer = *front;
267 }
268
Lajos Molnar5f920c12015-07-13 16:04:24 -0700269 ATRACE_BUFFER_INDEX(slot);
270
271 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700272 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800273
274 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700275 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700276 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700277 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700278 // the queue is empty and the BufferItem is created above.
279 if (mCore->mQueue.empty()) {
280 mSlots[slot].mBufferState.acquireNotInQueue();
281 } else {
282 mSlots[slot].mBufferState.acquire();
283 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700284 mSlots[slot].mFence = Fence::NO_FENCE;
285 }
286
287 // If the buffer has previously been acquired by the consumer, set
288 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
289 // on the consumer side
290 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700291 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700292 }
293
294 mCore->mQueue.erase(front);
295
296 // We might have freed a slot while dropping old buffers, or the producer
297 // may be blocked waiting for the number of buffers in the queue to
298 // decrease.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200299 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700300
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000301 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800302#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700303 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800304#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800305 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800306 }
307
Yi Kong48a619f2018-06-05 16:34:59 -0700308 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700309 for (int i = 0; i < numDroppedBuffers; ++i) {
310 listener->onBufferReleased();
311 }
Dan Stoza289ade12014-02-28 11:17:17 -0800312 }
313
Dan Stoza289ade12014-02-28 11:17:17 -0800314 return NO_ERROR;
315}
316
Dan Stoza9f3053d2014-03-06 15:14:33 -0800317status_t BufferQueueConsumer::detachBuffer(int slot) {
318 ATRACE_CALL();
319 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700320 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000321 sp<IProducerListener> listener;
322 {
323 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000325 if (mCore->mIsAbandoned) {
326 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
327 return NO_INIT;
328 }
329
330 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
331 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
332 return BAD_VALUE;
333 }
334
335 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
336 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
337 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
338 return BAD_VALUE;
339 } else if (!mSlots[slot].mBufferState.isAcquired()) {
340 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
341 "(state = %s)", slot, mSlots[slot].mBufferState.string());
342 return BAD_VALUE;
343 }
344 if (mCore->mBufferReleasedCbEnabled) {
345 listener = mCore->mConnectedProducerListener;
346 }
347
348 mSlots[slot].mBufferState.detachConsumer();
349 mCore->mActiveBuffers.erase(slot);
350 mCore->mFreeSlots.insert(slot);
351 mCore->clearBufferSlotLocked(slot);
352 mCore->mDequeueCondition.notify_all();
353 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800354 }
355
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000356 if (listener) {
357 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800358 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800359 return NO_ERROR;
360}
361
362status_t BufferQueueConsumer::attachBuffer(int* outSlot,
363 const sp<android::GraphicBuffer>& buffer) {
364 ATRACE_CALL();
365
Yi Kong48a619f2018-06-05 16:34:59 -0700366 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700367 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700369 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700370 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800371 return BAD_VALUE;
372 }
373
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200374 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800375
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700376 if (mCore->mSharedBufferMode) {
377 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700378 return BAD_VALUE;
379 }
380
Dan Stoza0de7ea72015-04-23 13:20:51 -0700381 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800383 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700384 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800385 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386 }
387 }
388
389 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700390 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391 "(max %d)", numAcquiredBuffers,
392 mCore->mMaxAcquiredBufferCount);
393 return INVALID_OPERATION;
394 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700395
Dan Stoza812ed062015-06-02 15:45:22 -0700396 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
397 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
398 "[queue %u]", buffer->getGenerationNumber(),
399 mCore->mGenerationNumber);
400 return BAD_VALUE;
401 }
402
Dan Stoza0de7ea72015-04-23 13:20:51 -0700403 // Find a free slot to put the buffer into
404 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
405 if (!mCore->mFreeSlots.empty()) {
406 auto slot = mCore->mFreeSlots.begin();
407 found = *slot;
408 mCore->mFreeSlots.erase(slot);
409 } else if (!mCore->mFreeBuffers.empty()) {
410 found = mCore->mFreeBuffers.front();
411 mCore->mFreeBuffers.remove(found);
412 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800413 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700414 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800415 return NO_MEMORY;
416 }
417
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800418 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800419 *outSlot = found;
420 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700421 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800422
423 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700424 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800425 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800426 mSlots[*outSlot].mFence = Fence::NO_FENCE;
427 mSlots[*outSlot].mFrameNumber = 0;
428
Dan Stoza99b18b42014-03-28 15:34:33 -0700429 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
430 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
431 // Binder traffic by not un/flattening the GraphicBuffer. However, it
432 // requires that the consumer maintain a cached copy of the slot <--> buffer
433 // mappings, which is why the consumer doesn't need the valid pointer on
434 // acquire.
435 //
436 // The StreamSplitter is one of the primary users of the attach/detach
437 // logic, and while it is running, all buffers it acquires are immediately
438 // detached, and all buffers it eventually releases are ones that were
439 // attached (as opposed to having been obtained from acquireBuffer), so it
440 // doesn't make sense to maintain the slot/buffer mappings, which would
441 // become invalid for every buffer during detach/attach. By setting this to
442 // false, the valid GraphicBuffer pointer will always be sent with acquire
443 // for attached buffers.
444 mSlots[*outSlot].mAcquireCalled = false;
445
Pablo Ceballos9e314332016-01-12 13:49:19 -0800446 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700447
Dan Stoza9f3053d2014-03-06 15:14:33 -0800448 return NO_ERROR;
449}
450
Dan Stoza289ade12014-02-28 11:17:17 -0800451status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
452 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
453 EGLSyncKHR eglFence) {
454 ATRACE_CALL();
455 ATRACE_BUFFER_INDEX(slot);
456
Dan Stoza9f3053d2014-03-06 15:14:33 -0800457 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700458 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700459 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
460 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800461 return BAD_VALUE;
462 }
463
Dan Stozad1c10362014-03-28 15:19:08 -0700464 sp<IProducerListener> listener;
465 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200466 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800467
Dan Stozad1c10362014-03-28 15:19:08 -0700468 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700469 // we can ignore this releaseBuffer for the old buffer.
470 // Ignore this for the shared buffer where the frame number can easily
471 // get out of sync due to the buffer being queued and acquired at the
472 // same time.
473 if (frameNumber != mSlots[slot].mFrameNumber &&
474 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700475 return STALE_BUFFER_SLOT;
476 }
Dan Stoza289ade12014-02-28 11:17:17 -0800477
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800478 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700479 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700480 "but its state was %s", slot,
481 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800482 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800483 }
Dan Stoza289ade12014-02-28 11:17:17 -0800484
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800485 mSlots[slot].mEglDisplay = eglDisplay;
486 mSlots[slot].mEglFence = eglFence;
487 mSlots[slot].mFence = releaseFence;
488 mSlots[slot].mBufferState.release();
489
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700490 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800491 // still be around. Mark it as no longer shared if this
492 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700493 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800494 mSlots[slot].mBufferState.mShared = false;
495 }
496 // Don't put the shared buffer on the free list.
497 if (!mSlots[slot].mBufferState.isShared()) {
498 mCore->mActiveBuffers.erase(slot);
499 mCore->mFreeBuffers.push_back(slot);
500 }
501
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700502 if (mCore->mBufferReleasedCbEnabled) {
503 listener = mCore->mConnectedProducerListener;
504 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800505 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
506
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200507 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800508 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700509 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800510
Dan Stozad1c10362014-03-28 15:19:08 -0700511 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700512 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700513 listener->onBufferReleased();
514 }
Dan Stoza289ade12014-02-28 11:17:17 -0800515
516 return NO_ERROR;
517}
518
519status_t BufferQueueConsumer::connect(
520 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
521 ATRACE_CALL();
522
Yi Kong48a619f2018-06-05 16:34:59 -0700523 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700524 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800525 return BAD_VALUE;
526 }
527
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700528 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800529 controlledByApp ? "true" : "false");
530
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200531 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800532
533 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700534 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800535 return NO_INIT;
536 }
537
538 mCore->mConsumerListener = consumerListener;
539 mCore->mConsumerControlledByApp = controlledByApp;
540
541 return NO_ERROR;
542}
543
544status_t BufferQueueConsumer::disconnect() {
545 ATRACE_CALL();
546
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700547 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800548
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200549 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800550
Yi Kong48a619f2018-06-05 16:34:59 -0700551 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700552 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800553 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800554 }
555
556 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700557 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800558 mCore->mQueue.clear();
559 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700560 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200561 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800562 return NO_ERROR;
563}
564
Dan Stozafebd4f42014-04-09 16:14:51 -0700565status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800566 ATRACE_CALL();
567
Yi Kong48a619f2018-06-05 16:34:59 -0700568 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800569 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
570 return BAD_VALUE;
571 }
572
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200573 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800574
575 if (mCore->mIsAbandoned) {
576 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
577 return NO_INIT;
578 }
579
Dan Stozafebd4f42014-04-09 16:14:51 -0700580 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800581 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800582 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700583 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800584 }
585 }
586
587 // Remove from the mask queued buffers for which acquire has been called,
588 // since the consumer will not receive their buffer addresses and so must
589 // retain their cached information
590 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
591 while (current != mCore->mQueue.end()) {
592 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700593 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800594 }
595 ++current;
596 }
597
Dan Stozafebd4f42014-04-09 16:14:51 -0700598 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800599 *outSlotMask = mask;
600 return NO_ERROR;
601}
602
603status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
604 uint32_t height) {
605 ATRACE_CALL();
606
607 if (width == 0 || height == 0) {
608 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
609 "height=%u)", width, height);
610 return BAD_VALUE;
611 }
612
613 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
614
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200615 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800616 mCore->mDefaultWidth = width;
617 mCore->mDefaultHeight = height;
618 return NO_ERROR;
619}
620
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700621status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800622 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800623
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700624 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
625 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
626 return BAD_VALUE;
627 }
Dan Stoza289ade12014-02-28 11:17:17 -0800628
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200629 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800630
Pablo Ceballos38273792016-03-02 01:38:10 +0000631 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
632 BQ_LOGE("setMaxBufferCount: producer is already connected");
633 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800634 }
635
Pablo Ceballos38273792016-03-02 01:38:10 +0000636 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
637 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
638 "mMaxAcquiredBufferCount (%d)", bufferCount,
639 mCore->mMaxAcquiredBufferCount);
640 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700641 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000642
643 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
644 mCore->mDequeueBufferCannotBlock, bufferCount) -
645 mCore->getMaxBufferCountLocked();
646 if (!mCore->adjustAvailableSlotsLocked(delta)) {
647 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
648 "available slots. Delta = %d", delta);
649 return BAD_VALUE;
650 }
651
652 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800653 return NO_ERROR;
654}
655
656status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
657 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700658 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800659
660 if (maxAcquiredBuffers < 1 ||
661 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
662 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
663 maxAcquiredBuffers);
664 return BAD_VALUE;
665 }
666
Pablo Ceballos38273792016-03-02 01:38:10 +0000667 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800668 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200669 std::unique_lock<std::mutex> lock(mCore->mMutex);
670 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800671
Pablo Ceballos72daab62015-12-07 16:38:43 -0800672 if (mCore->mIsAbandoned) {
673 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
674 return NO_INIT;
675 }
676
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700677 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
678 return NO_ERROR;
679 }
680
Pablo Ceballos72daab62015-12-07 16:38:43 -0800681 // The new maxAcquiredBuffers count should not be violated by the number
682 // of currently acquired buffers
683 int acquiredCount = 0;
684 for (int slot : mCore->mActiveBuffers) {
685 if (mSlots[slot].mBufferState.isAcquired()) {
686 acquiredCount++;
687 }
688 }
689 if (acquiredCount > maxAcquiredBuffers) {
690 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
691 "count (%d) exceeds the current acquired buffer count (%d)",
692 maxAcquiredBuffers, acquiredCount);
693 return BAD_VALUE;
694 }
695
696 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
697 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
698 > mCore->mMaxBufferCount) {
699 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
700 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
701 maxAcquiredBuffers, mCore->mMaxBufferCount,
702 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
703 mCore->mDequeueBufferCannotBlock);
704 return BAD_VALUE;
705 }
706
707 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000708 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800709 return BAD_VALUE;
710 }
711
712 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
713 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
714 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700715 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000716 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800717 }
718 }
719 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700720 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000721 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800722 }
723
Dan Stoza289ade12014-02-28 11:17:17 -0800724 return NO_ERROR;
725}
726
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700727status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800728 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000729 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200730 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800731 mCore->mConsumerName = name;
732 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700733 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800734}
735
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800736status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800737 ATRACE_CALL();
738 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200739 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800740 mCore->mDefaultBufferFormat = defaultFormat;
741 return NO_ERROR;
742}
743
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800744status_t BufferQueueConsumer::setDefaultBufferDataSpace(
745 android_dataspace defaultDataSpace) {
746 ATRACE_CALL();
747 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200748 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800749 mCore->mDefaultBufferDataSpace = defaultDataSpace;
750 return NO_ERROR;
751}
752
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700753status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800754 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700755 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200756 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800757 mCore->mConsumerUsageBits = usage;
758 return NO_ERROR;
759}
760
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700761status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
762 ATRACE_CALL();
763 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200764 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700765 mCore->mConsumerIsProtected = isProtected;
766 return NO_ERROR;
767}
768
Dan Stoza289ade12014-02-28 11:17:17 -0800769status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
770 ATRACE_CALL();
771 BQ_LOGV("setTransformHint: %#x", hint);
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->mTransformHint = hint;
774 return NO_ERROR;
775}
776
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700777status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200778 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700779 *outStream = mCore->mSidebandStream;
780 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800781}
782
Dan Stozae77c7662016-05-13 11:37:28 -0700783status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
784 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200785 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800786#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700787 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800788#else
789 (void)forceFlush;
790 outHistory->clear();
791#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700792 return NO_ERROR;
793}
794
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700795status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200796 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700797 mCore->discardFreeBuffersLocked();
798 return NO_ERROR;
799}
800
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700801status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700802 struct passwd* pwd = getpwnam("shell");
803 uid_t shellUid = pwd ? pwd->pw_uid : 0;
804 if (!shellUid) {
805 int savedErrno = errno;
806 BQ_LOGE("Cannot get AID_SHELL");
807 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
808 }
809
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800810 bool denied = false;
811 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800812#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900813 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800814 // the PermissionController. We need to do a runtime check as well, since
815 // the system variant of libgui can be loaded in a vendor process. For eg:
816 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
817 if (!android_is_in_vendor_process()) {
818 const pid_t pid = BufferQueueThreadState::getCallingPid();
819 if ((uid != shellUid) &&
820 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
821 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
822 "from pid=%d, uid=%d\n",
823 pid, uid);
824 denied = true;
825 }
826 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900827#else
828 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800829 denied = true;
830 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900831#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800832 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700833 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700834 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700835 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800836 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700837
838 mCore->dumpState(prefix, outResult);
839 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800840}
841
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800842void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
843 std::lock_guard<std::mutex> lock(mCore->mMutex);
844 mCore->mAllowExtraAcquire = allow;
845}
846
Dan Stoza289ade12014-02-28 11:17:17 -0800847} // namespace android