blob: 5b34ba12c8368f2c123ebb29374f7a7f608c5c74 [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);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200321 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322
Pablo Ceballos38273792016-03-02 01:38:10 +0000323 if (mCore->mIsAbandoned) {
324 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
325 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 }
327
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700328 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
329 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000330 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800331 }
332
Pablo Ceballos38273792016-03-02 01:38:10 +0000333 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
334 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
335 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
336 return BAD_VALUE;
337 } else if (!mSlots[slot].mBufferState.isAcquired()) {
338 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
339 "(state = %s)", slot, mSlots[slot].mBufferState.string());
340 return BAD_VALUE;
341 }
342
343 mSlots[slot].mBufferState.detachConsumer();
344 mCore->mActiveBuffers.erase(slot);
345 mCore->mFreeSlots.insert(slot);
346 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200347 mCore->mDequeueCondition.notify_all();
Pablo Ceballos38273792016-03-02 01:38:10 +0000348 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800349
350 return NO_ERROR;
351}
352
353status_t BufferQueueConsumer::attachBuffer(int* outSlot,
354 const sp<android::GraphicBuffer>& buffer) {
355 ATRACE_CALL();
356
Yi Kong48a619f2018-06-05 16:34:59 -0700357 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700358 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800359 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700360 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700361 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800362 return BAD_VALUE;
363 }
364
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200365 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800366
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700367 if (mCore->mSharedBufferMode) {
368 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700369 return BAD_VALUE;
370 }
371
Dan Stoza0de7ea72015-04-23 13:20:51 -0700372 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800374 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700375 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800376 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 }
378 }
379
380 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700381 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 "(max %d)", numAcquiredBuffers,
383 mCore->mMaxAcquiredBufferCount);
384 return INVALID_OPERATION;
385 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700386
Dan Stoza812ed062015-06-02 15:45:22 -0700387 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
388 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
389 "[queue %u]", buffer->getGenerationNumber(),
390 mCore->mGenerationNumber);
391 return BAD_VALUE;
392 }
393
Dan Stoza0de7ea72015-04-23 13:20:51 -0700394 // Find a free slot to put the buffer into
395 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
396 if (!mCore->mFreeSlots.empty()) {
397 auto slot = mCore->mFreeSlots.begin();
398 found = *slot;
399 mCore->mFreeSlots.erase(slot);
400 } else if (!mCore->mFreeBuffers.empty()) {
401 found = mCore->mFreeBuffers.front();
402 mCore->mFreeBuffers.remove(found);
403 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800404 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700405 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800406 return NO_MEMORY;
407 }
408
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800409 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800410 *outSlot = found;
411 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700412 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800413
414 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700415 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800416 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800417 mSlots[*outSlot].mFence = Fence::NO_FENCE;
418 mSlots[*outSlot].mFrameNumber = 0;
419
Dan Stoza99b18b42014-03-28 15:34:33 -0700420 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
421 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
422 // Binder traffic by not un/flattening the GraphicBuffer. However, it
423 // requires that the consumer maintain a cached copy of the slot <--> buffer
424 // mappings, which is why the consumer doesn't need the valid pointer on
425 // acquire.
426 //
427 // The StreamSplitter is one of the primary users of the attach/detach
428 // logic, and while it is running, all buffers it acquires are immediately
429 // detached, and all buffers it eventually releases are ones that were
430 // attached (as opposed to having been obtained from acquireBuffer), so it
431 // doesn't make sense to maintain the slot/buffer mappings, which would
432 // become invalid for every buffer during detach/attach. By setting this to
433 // false, the valid GraphicBuffer pointer will always be sent with acquire
434 // for attached buffers.
435 mSlots[*outSlot].mAcquireCalled = false;
436
Pablo Ceballos9e314332016-01-12 13:49:19 -0800437 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700438
Dan Stoza9f3053d2014-03-06 15:14:33 -0800439 return NO_ERROR;
440}
441
Dan Stoza289ade12014-02-28 11:17:17 -0800442status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
443 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
444 EGLSyncKHR eglFence) {
445 ATRACE_CALL();
446 ATRACE_BUFFER_INDEX(slot);
447
Dan Stoza9f3053d2014-03-06 15:14:33 -0800448 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700449 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700450 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
451 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800452 return BAD_VALUE;
453 }
454
Dan Stozad1c10362014-03-28 15:19:08 -0700455 sp<IProducerListener> listener;
456 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200457 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800458
Dan Stozad1c10362014-03-28 15:19:08 -0700459 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700460 // we can ignore this releaseBuffer for the old buffer.
461 // Ignore this for the shared buffer where the frame number can easily
462 // get out of sync due to the buffer being queued and acquired at the
463 // same time.
464 if (frameNumber != mSlots[slot].mFrameNumber &&
465 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700466 return STALE_BUFFER_SLOT;
467 }
Dan Stoza289ade12014-02-28 11:17:17 -0800468
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800469 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700470 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700471 "but its state was %s", slot,
472 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800473 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800474 }
Dan Stoza289ade12014-02-28 11:17:17 -0800475
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800476 mSlots[slot].mEglDisplay = eglDisplay;
477 mSlots[slot].mEglFence = eglFence;
478 mSlots[slot].mFence = releaseFence;
479 mSlots[slot].mBufferState.release();
480
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700481 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800482 // still be around. Mark it as no longer shared if this
483 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700484 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800485 mSlots[slot].mBufferState.mShared = false;
486 }
487 // Don't put the shared buffer on the free list.
488 if (!mSlots[slot].mBufferState.isShared()) {
489 mCore->mActiveBuffers.erase(slot);
490 mCore->mFreeBuffers.push_back(slot);
491 }
492
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700493 if (mCore->mBufferReleasedCbEnabled) {
494 listener = mCore->mConnectedProducerListener;
495 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800496 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
497
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200498 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800499 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700500 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800501
Dan Stozad1c10362014-03-28 15:19:08 -0700502 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700503 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700504 listener->onBufferReleased();
505 }
Dan Stoza289ade12014-02-28 11:17:17 -0800506
507 return NO_ERROR;
508}
509
510status_t BufferQueueConsumer::connect(
511 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
512 ATRACE_CALL();
513
Yi Kong48a619f2018-06-05 16:34:59 -0700514 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700515 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800516 return BAD_VALUE;
517 }
518
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700519 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800520 controlledByApp ? "true" : "false");
521
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200522 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800523
524 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700525 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800526 return NO_INIT;
527 }
528
529 mCore->mConsumerListener = consumerListener;
530 mCore->mConsumerControlledByApp = controlledByApp;
531
532 return NO_ERROR;
533}
534
535status_t BufferQueueConsumer::disconnect() {
536 ATRACE_CALL();
537
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700538 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800539
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200540 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800541
Yi Kong48a619f2018-06-05 16:34:59 -0700542 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700543 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800544 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800545 }
546
547 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700548 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800549 mCore->mQueue.clear();
550 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700551 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200552 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800553 return NO_ERROR;
554}
555
Dan Stozafebd4f42014-04-09 16:14:51 -0700556status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800557 ATRACE_CALL();
558
Yi Kong48a619f2018-06-05 16:34:59 -0700559 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800560 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
561 return BAD_VALUE;
562 }
563
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200564 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800565
566 if (mCore->mIsAbandoned) {
567 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
568 return NO_INIT;
569 }
570
Dan Stozafebd4f42014-04-09 16:14:51 -0700571 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800572 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800573 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700574 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800575 }
576 }
577
578 // Remove from the mask queued buffers for which acquire has been called,
579 // since the consumer will not receive their buffer addresses and so must
580 // retain their cached information
581 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
582 while (current != mCore->mQueue.end()) {
583 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700584 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800585 }
586 ++current;
587 }
588
Dan Stozafebd4f42014-04-09 16:14:51 -0700589 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800590 *outSlotMask = mask;
591 return NO_ERROR;
592}
593
594status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
595 uint32_t height) {
596 ATRACE_CALL();
597
598 if (width == 0 || height == 0) {
599 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
600 "height=%u)", width, height);
601 return BAD_VALUE;
602 }
603
604 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
605
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200606 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800607 mCore->mDefaultWidth = width;
608 mCore->mDefaultHeight = height;
609 return NO_ERROR;
610}
611
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700612status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800613 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800614
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700615 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
616 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
617 return BAD_VALUE;
618 }
Dan Stoza289ade12014-02-28 11:17:17 -0800619
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200620 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800621
Pablo Ceballos38273792016-03-02 01:38:10 +0000622 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
623 BQ_LOGE("setMaxBufferCount: producer is already connected");
624 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800625 }
626
Pablo Ceballos38273792016-03-02 01:38:10 +0000627 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
628 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
629 "mMaxAcquiredBufferCount (%d)", bufferCount,
630 mCore->mMaxAcquiredBufferCount);
631 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700632 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000633
634 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
635 mCore->mDequeueBufferCannotBlock, bufferCount) -
636 mCore->getMaxBufferCountLocked();
637 if (!mCore->adjustAvailableSlotsLocked(delta)) {
638 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
639 "available slots. Delta = %d", delta);
640 return BAD_VALUE;
641 }
642
643 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800644 return NO_ERROR;
645}
646
647status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
648 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700649 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800650
651 if (maxAcquiredBuffers < 1 ||
652 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
653 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
654 maxAcquiredBuffers);
655 return BAD_VALUE;
656 }
657
Pablo Ceballos38273792016-03-02 01:38:10 +0000658 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800659 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200660 std::unique_lock<std::mutex> lock(mCore->mMutex);
661 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800662
Pablo Ceballos72daab62015-12-07 16:38:43 -0800663 if (mCore->mIsAbandoned) {
664 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
665 return NO_INIT;
666 }
667
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700668 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
669 return NO_ERROR;
670 }
671
Pablo Ceballos72daab62015-12-07 16:38:43 -0800672 // The new maxAcquiredBuffers count should not be violated by the number
673 // of currently acquired buffers
674 int acquiredCount = 0;
675 for (int slot : mCore->mActiveBuffers) {
676 if (mSlots[slot].mBufferState.isAcquired()) {
677 acquiredCount++;
678 }
679 }
680 if (acquiredCount > maxAcquiredBuffers) {
681 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
682 "count (%d) exceeds the current acquired buffer count (%d)",
683 maxAcquiredBuffers, acquiredCount);
684 return BAD_VALUE;
685 }
686
687 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
688 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
689 > mCore->mMaxBufferCount) {
690 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
691 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
692 maxAcquiredBuffers, mCore->mMaxBufferCount,
693 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
694 mCore->mDequeueBufferCannotBlock);
695 return BAD_VALUE;
696 }
697
698 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000699 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800700 return BAD_VALUE;
701 }
702
703 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
704 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
705 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700706 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000707 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800708 }
709 }
710 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700711 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000712 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800713 }
714
Dan Stoza289ade12014-02-28 11:17:17 -0800715 return NO_ERROR;
716}
717
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700718status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800719 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000720 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200721 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800722 mCore->mConsumerName = name;
723 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700724 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800725}
726
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800727status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800728 ATRACE_CALL();
729 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
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->mDefaultBufferFormat = defaultFormat;
732 return NO_ERROR;
733}
734
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800735status_t BufferQueueConsumer::setDefaultBufferDataSpace(
736 android_dataspace defaultDataSpace) {
737 ATRACE_CALL();
738 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200739 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800740 mCore->mDefaultBufferDataSpace = defaultDataSpace;
741 return NO_ERROR;
742}
743
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700744status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800745 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700746 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200747 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800748 mCore->mConsumerUsageBits = usage;
749 return NO_ERROR;
750}
751
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700752status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
753 ATRACE_CALL();
754 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200755 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700756 mCore->mConsumerIsProtected = isProtected;
757 return NO_ERROR;
758}
759
Dan Stoza289ade12014-02-28 11:17:17 -0800760status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
761 ATRACE_CALL();
762 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200763 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800764 mCore->mTransformHint = hint;
765 return NO_ERROR;
766}
767
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700768status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200769 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700770 *outStream = mCore->mSidebandStream;
771 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800772}
773
Dan Stozae77c7662016-05-13 11:37:28 -0700774status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
775 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200776 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800777#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700778 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800779#else
780 (void)forceFlush;
781 outHistory->clear();
782#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700783 return NO_ERROR;
784}
785
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700786status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200787 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700788 mCore->discardFreeBuffersLocked();
789 return NO_ERROR;
790}
791
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700792status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700793 struct passwd* pwd = getpwnam("shell");
794 uid_t shellUid = pwd ? pwd->pw_uid : 0;
795 if (!shellUid) {
796 int savedErrno = errno;
797 BQ_LOGE("Cannot get AID_SHELL");
798 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
799 }
800
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800801 bool denied = false;
802 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800803#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900804 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800805 // the PermissionController. We need to do a runtime check as well, since
806 // the system variant of libgui can be loaded in a vendor process. For eg:
807 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
808 if (!android_is_in_vendor_process()) {
809 const pid_t pid = BufferQueueThreadState::getCallingPid();
810 if ((uid != shellUid) &&
811 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
812 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
813 "from pid=%d, uid=%d\n",
814 pid, uid);
815 denied = true;
816 }
817 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900818#else
819 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800820 denied = true;
821 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900822#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800823 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700824 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700825 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700826 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800827 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700828
829 mCore->dumpState(prefix, outResult);
830 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800831}
832
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800833void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
834 std::lock_guard<std::mutex> lock(mCore->mMutex);
835 mCore->mAllowExtraAcquire = allow;
836}
837
Dan Stoza289ade12014-02-28 11:17:17 -0800838} // namespace android