blob: 52172090aff734792e2d5c3daaba29209da32d49 [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, ...) \
50 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
51 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
52 ##__VA_ARGS__)
53#define BQ_LOGD(x, ...) \
54 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
55 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
56 ##__VA_ARGS__)
57#define BQ_LOGI(x, ...) \
58 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
59 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
60 ##__VA_ARGS__)
61#define BQ_LOGW(x, ...) \
62 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
63 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
64 ##__VA_ARGS__)
65#define BQ_LOGE(x, ...) \
66 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
67 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
Colin Cross6e7e2b42016-09-27 14:08:19 -0700301 ATRACE_INT(mCore->mConsumerName.string(),
302 static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800303#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700304 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800305#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800306 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800307 }
308
Yi Kong48a619f2018-06-05 16:34:59 -0700309 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700310 for (int i = 0; i < numDroppedBuffers; ++i) {
311 listener->onBufferReleased();
312 }
Dan Stoza289ade12014-02-28 11:17:17 -0800313 }
314
Dan Stoza289ade12014-02-28 11:17:17 -0800315 return NO_ERROR;
316}
317
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318status_t BufferQueueConsumer::detachBuffer(int slot) {
319 ATRACE_CALL();
320 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700321 BQ_LOGV("detachBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200322 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800323
Pablo Ceballos38273792016-03-02 01:38:10 +0000324 if (mCore->mIsAbandoned) {
325 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
326 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800327 }
328
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700329 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
330 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000331 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800332 }
333
Pablo Ceballos38273792016-03-02 01:38:10 +0000334 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
344 mSlots[slot].mBufferState.detachConsumer();
345 mCore->mActiveBuffers.erase(slot);
346 mCore->mFreeSlots.insert(slot);
347 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200348 mCore->mDequeueCondition.notify_all();
Pablo Ceballos38273792016-03-02 01:38:10 +0000349 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800350
351 return NO_ERROR;
352}
353
354status_t BufferQueueConsumer::attachBuffer(int* outSlot,
355 const sp<android::GraphicBuffer>& buffer) {
356 ATRACE_CALL();
357
Yi Kong48a619f2018-06-05 16:34:59 -0700358 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700359 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800360 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700361 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700362 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800363 return BAD_VALUE;
364 }
365
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200366 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800367
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700368 if (mCore->mSharedBufferMode) {
369 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700370 return BAD_VALUE;
371 }
372
Dan Stoza0de7ea72015-04-23 13:20:51 -0700373 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800374 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800375 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700376 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800378 }
379 }
380
381 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700382 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800383 "(max %d)", numAcquiredBuffers,
384 mCore->mMaxAcquiredBufferCount);
385 return INVALID_OPERATION;
386 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700387
Dan Stoza812ed062015-06-02 15:45:22 -0700388 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
389 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
390 "[queue %u]", buffer->getGenerationNumber(),
391 mCore->mGenerationNumber);
392 return BAD_VALUE;
393 }
394
Dan Stoza0de7ea72015-04-23 13:20:51 -0700395 // Find a free slot to put the buffer into
396 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
397 if (!mCore->mFreeSlots.empty()) {
398 auto slot = mCore->mFreeSlots.begin();
399 found = *slot;
400 mCore->mFreeSlots.erase(slot);
401 } else if (!mCore->mFreeBuffers.empty()) {
402 found = mCore->mFreeBuffers.front();
403 mCore->mFreeBuffers.remove(found);
404 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800405 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700406 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800407 return NO_MEMORY;
408 }
409
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800410 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800411 *outSlot = found;
412 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700413 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800414
415 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700416 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800417 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800418 mSlots[*outSlot].mFence = Fence::NO_FENCE;
419 mSlots[*outSlot].mFrameNumber = 0;
420
Dan Stoza99b18b42014-03-28 15:34:33 -0700421 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
422 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
423 // Binder traffic by not un/flattening the GraphicBuffer. However, it
424 // requires that the consumer maintain a cached copy of the slot <--> buffer
425 // mappings, which is why the consumer doesn't need the valid pointer on
426 // acquire.
427 //
428 // The StreamSplitter is one of the primary users of the attach/detach
429 // logic, and while it is running, all buffers it acquires are immediately
430 // detached, and all buffers it eventually releases are ones that were
431 // attached (as opposed to having been obtained from acquireBuffer), so it
432 // doesn't make sense to maintain the slot/buffer mappings, which would
433 // become invalid for every buffer during detach/attach. By setting this to
434 // false, the valid GraphicBuffer pointer will always be sent with acquire
435 // for attached buffers.
436 mSlots[*outSlot].mAcquireCalled = false;
437
Pablo Ceballos9e314332016-01-12 13:49:19 -0800438 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700439
Dan Stoza9f3053d2014-03-06 15:14:33 -0800440 return NO_ERROR;
441}
442
Dan Stoza289ade12014-02-28 11:17:17 -0800443status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
444 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
445 EGLSyncKHR eglFence) {
446 ATRACE_CALL();
447 ATRACE_BUFFER_INDEX(slot);
448
Dan Stoza9f3053d2014-03-06 15:14:33 -0800449 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700450 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700451 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
452 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800453 return BAD_VALUE;
454 }
455
Dan Stozad1c10362014-03-28 15:19:08 -0700456 sp<IProducerListener> listener;
457 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200458 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800459
Dan Stozad1c10362014-03-28 15:19:08 -0700460 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700461 // we can ignore this releaseBuffer for the old buffer.
462 // Ignore this for the shared buffer where the frame number can easily
463 // get out of sync due to the buffer being queued and acquired at the
464 // same time.
465 if (frameNumber != mSlots[slot].mFrameNumber &&
466 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700467 return STALE_BUFFER_SLOT;
468 }
Dan Stoza289ade12014-02-28 11:17:17 -0800469
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800470 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700471 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700472 "but its state was %s", slot,
473 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800474 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800475 }
Dan Stoza289ade12014-02-28 11:17:17 -0800476
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800477 mSlots[slot].mEglDisplay = eglDisplay;
478 mSlots[slot].mEglFence = eglFence;
479 mSlots[slot].mFence = releaseFence;
480 mSlots[slot].mBufferState.release();
481
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700482 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800483 // still be around. Mark it as no longer shared if this
484 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700485 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800486 mSlots[slot].mBufferState.mShared = false;
487 }
488 // Don't put the shared buffer on the free list.
489 if (!mSlots[slot].mBufferState.isShared()) {
490 mCore->mActiveBuffers.erase(slot);
491 mCore->mFreeBuffers.push_back(slot);
492 }
493
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700494 if (mCore->mBufferReleasedCbEnabled) {
495 listener = mCore->mConnectedProducerListener;
496 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800497 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
498
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200499 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800500 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700501 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800502
Dan Stozad1c10362014-03-28 15:19:08 -0700503 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700504 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700505 listener->onBufferReleased();
506 }
Dan Stoza289ade12014-02-28 11:17:17 -0800507
508 return NO_ERROR;
509}
510
511status_t BufferQueueConsumer::connect(
512 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
513 ATRACE_CALL();
514
Yi Kong48a619f2018-06-05 16:34:59 -0700515 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700516 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800517 return BAD_VALUE;
518 }
519
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700520 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800521 controlledByApp ? "true" : "false");
522
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200523 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800524
525 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700526 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800527 return NO_INIT;
528 }
529
530 mCore->mConsumerListener = consumerListener;
531 mCore->mConsumerControlledByApp = controlledByApp;
532
533 return NO_ERROR;
534}
535
536status_t BufferQueueConsumer::disconnect() {
537 ATRACE_CALL();
538
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700539 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800540
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200541 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800542
Yi Kong48a619f2018-06-05 16:34:59 -0700543 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700544 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800545 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800546 }
547
548 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700549 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800550 mCore->mQueue.clear();
551 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700552 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200553 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800554 return NO_ERROR;
555}
556
Dan Stozafebd4f42014-04-09 16:14:51 -0700557status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800558 ATRACE_CALL();
559
Yi Kong48a619f2018-06-05 16:34:59 -0700560 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800561 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
562 return BAD_VALUE;
563 }
564
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200565 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800566
567 if (mCore->mIsAbandoned) {
568 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
569 return NO_INIT;
570 }
571
Dan Stozafebd4f42014-04-09 16:14:51 -0700572 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800573 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800574 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700575 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800576 }
577 }
578
579 // Remove from the mask queued buffers for which acquire has been called,
580 // since the consumer will not receive their buffer addresses and so must
581 // retain their cached information
582 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
583 while (current != mCore->mQueue.end()) {
584 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700585 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800586 }
587 ++current;
588 }
589
Dan Stozafebd4f42014-04-09 16:14:51 -0700590 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800591 *outSlotMask = mask;
592 return NO_ERROR;
593}
594
595status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
596 uint32_t height) {
597 ATRACE_CALL();
598
599 if (width == 0 || height == 0) {
600 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
601 "height=%u)", width, height);
602 return BAD_VALUE;
603 }
604
605 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
606
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200607 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800608 mCore->mDefaultWidth = width;
609 mCore->mDefaultHeight = height;
610 return NO_ERROR;
611}
612
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700613status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800614 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800615
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700616 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
617 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
618 return BAD_VALUE;
619 }
Dan Stoza289ade12014-02-28 11:17:17 -0800620
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200621 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800622
Pablo Ceballos38273792016-03-02 01:38:10 +0000623 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
624 BQ_LOGE("setMaxBufferCount: producer is already connected");
625 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800626 }
627
Pablo Ceballos38273792016-03-02 01:38:10 +0000628 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
629 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
630 "mMaxAcquiredBufferCount (%d)", bufferCount,
631 mCore->mMaxAcquiredBufferCount);
632 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700633 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000634
635 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
636 mCore->mDequeueBufferCannotBlock, bufferCount) -
637 mCore->getMaxBufferCountLocked();
638 if (!mCore->adjustAvailableSlotsLocked(delta)) {
639 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
640 "available slots. Delta = %d", delta);
641 return BAD_VALUE;
642 }
643
644 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800645 return NO_ERROR;
646}
647
648status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
649 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700650 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800651
652 if (maxAcquiredBuffers < 1 ||
653 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
654 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
655 maxAcquiredBuffers);
656 return BAD_VALUE;
657 }
658
Pablo Ceballos38273792016-03-02 01:38:10 +0000659 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800660 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200661 std::unique_lock<std::mutex> lock(mCore->mMutex);
662 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800663
Pablo Ceballos72daab62015-12-07 16:38:43 -0800664 if (mCore->mIsAbandoned) {
665 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
666 return NO_INIT;
667 }
668
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700669 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
670 return NO_ERROR;
671 }
672
Pablo Ceballos72daab62015-12-07 16:38:43 -0800673 // The new maxAcquiredBuffers count should not be violated by the number
674 // of currently acquired buffers
675 int acquiredCount = 0;
676 for (int slot : mCore->mActiveBuffers) {
677 if (mSlots[slot].mBufferState.isAcquired()) {
678 acquiredCount++;
679 }
680 }
681 if (acquiredCount > maxAcquiredBuffers) {
682 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
683 "count (%d) exceeds the current acquired buffer count (%d)",
684 maxAcquiredBuffers, acquiredCount);
685 return BAD_VALUE;
686 }
687
688 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
689 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
690 > mCore->mMaxBufferCount) {
691 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
692 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
693 maxAcquiredBuffers, mCore->mMaxBufferCount,
694 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
695 mCore->mDequeueBufferCannotBlock);
696 return BAD_VALUE;
697 }
698
699 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000700 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800701 return BAD_VALUE;
702 }
703
704 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
705 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
706 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700707 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000708 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800709 }
710 }
711 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700712 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000713 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800714 }
715
Dan Stoza289ade12014-02-28 11:17:17 -0800716 return NO_ERROR;
717}
718
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700719status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800720 ATRACE_CALL();
721 BQ_LOGV("setConsumerName: '%s'", name.string());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200722 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800723 mCore->mConsumerName = name;
724 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700725 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800726}
727
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800728status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800729 ATRACE_CALL();
730 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200731 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800732 mCore->mDefaultBufferFormat = defaultFormat;
733 return NO_ERROR;
734}
735
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800736status_t BufferQueueConsumer::setDefaultBufferDataSpace(
737 android_dataspace defaultDataSpace) {
738 ATRACE_CALL();
739 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200740 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800741 mCore->mDefaultBufferDataSpace = defaultDataSpace;
742 return NO_ERROR;
743}
744
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700745status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800746 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700747 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200748 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800749 mCore->mConsumerUsageBits = usage;
750 return NO_ERROR;
751}
752
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700753status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
754 ATRACE_CALL();
755 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200756 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700757 mCore->mConsumerIsProtected = isProtected;
758 return NO_ERROR;
759}
760
Dan Stoza289ade12014-02-28 11:17:17 -0800761status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
762 ATRACE_CALL();
763 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200764 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800765 mCore->mTransformHint = hint;
766 return NO_ERROR;
767}
768
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700769status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200770 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700771 *outStream = mCore->mSidebandStream;
772 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800773}
774
Dan Stozae77c7662016-05-13 11:37:28 -0700775status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
776 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200777 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800778#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700779 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800780#else
781 (void)forceFlush;
782 outHistory->clear();
783#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700784 return NO_ERROR;
785}
786
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700787status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200788 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700789 mCore->discardFreeBuffersLocked();
790 return NO_ERROR;
791}
792
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700793status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700794 struct passwd* pwd = getpwnam("shell");
795 uid_t shellUid = pwd ? pwd->pw_uid : 0;
796 if (!shellUid) {
797 int savedErrno = errno;
798 BQ_LOGE("Cannot get AID_SHELL");
799 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
800 }
801
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800802 bool denied = false;
803 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800804#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900805 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800806 // the PermissionController. We need to do a runtime check as well, since
807 // the system variant of libgui can be loaded in a vendor process. For eg:
808 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
809 if (!android_is_in_vendor_process()) {
810 const pid_t pid = BufferQueueThreadState::getCallingPid();
811 if ((uid != shellUid) &&
812 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
813 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
814 "from pid=%d, uid=%d\n",
815 pid, uid);
816 denied = true;
817 }
818 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900819#else
820 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800821 denied = true;
822 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900823#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800824 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700825 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700826 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700827 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800828 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700829
830 mCore->dumpState(prefix, outResult);
831 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800832}
833
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800834void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
835 std::lock_guard<std::mutex> lock(mCore->mMutex);
836 mCore->mAllowExtraAcquire = allow;
837}
838
Dan Stoza289ade12014-02-28 11:17:17 -0800839} // namespace android