blob: db513561fbe6e743dd49e26c06de0e85a1710763 [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>
Dan Stoza289ade12014-02-28 11:17:17 -080036
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080037#include <private/gui/BufferQueueThreadState.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090038#ifndef __ANDROID_VNDK__
Pablo Ceballos88f69282016-02-11 18:01:49 -080039#include <binder/PermissionCache.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080040#include <vndksupport/linker.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090041#endif
Pablo Ceballos88f69282016-02-11 18:01:49 -080042
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070043#include <system/window.h>
44
Dan Stoza289ade12014-02-28 11:17:17 -080045namespace android {
46
Iris Chang430193f2019-12-04 16:25:46 +080047// Macros for include BufferQueueCore information in log messages
48#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000049 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080050 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
51 ##__VA_ARGS__)
52#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000053 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080054 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
55 ##__VA_ARGS__)
56#define BQ_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000057 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080058 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
59 ##__VA_ARGS__)
60#define BQ_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000061 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080062 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
63 ##__VA_ARGS__)
64#define BQ_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000065 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080066 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
67 ##__VA_ARGS__)
68
Chong Zhang62493092020-01-15 16:04:47 -080069ConsumerListener::~ConsumerListener() = default;
70
Dan Stoza289ade12014-02-28 11:17:17 -080071BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
72 mCore(core),
73 mSlots(core->mSlots),
74 mConsumerName() {}
75
76BufferQueueConsumer::~BufferQueueConsumer() {}
77
78status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070079 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080080 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080081
Lajos Molnar5f920c12015-07-13 16:04:24 -070082 int numDroppedBuffers = 0;
83 sp<IProducerListener> listener;
84 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020085 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070086
87 // Check that the consumer doesn't currently have the maximum number of
88 // buffers acquired. We allow the max buffer count to be exceeded by one
89 // buffer so that the consumer can successfully set up the newly acquired
90 // buffer before releasing the old one.
91 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080092 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070093 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070094 ++numAcquiredBuffers;
95 }
Dan Stoza289ade12014-02-28 11:17:17 -080096 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -080097 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
98 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
99 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
100 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700101 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
102 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
103 return INVALID_OPERATION;
104 }
Dan Stoza289ade12014-02-28 11:17:17 -0800105
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700106 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
107 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700108 BufferQueueCore::INVALID_BUFFER_SLOT;
109
Lajos Molnar5f920c12015-07-13 16:04:24 -0700110 // In asynchronous mode the list is guaranteed to be one buffer deep,
111 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700113 return NO_BUFFER_AVAILABLE;
114 }
Dan Stoza289ade12014-02-28 11:17:17 -0800115
Lajos Molnar5f920c12015-07-13 16:04:24 -0700116 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800117
Lajos Molnar5f920c12015-07-13 16:04:24 -0700118 // If expectedPresent is specified, we may not want to return a buffer yet.
119 // If it's specified and there's more than one buffer queued, we may want
120 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700121 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700122 // since in that case we'll just return the shared buffer.
123 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700124 // The 'expectedPresent' argument indicates when the buffer is expected
125 // to be presented on-screen. If the buffer's desired present time is
126 // earlier (less) than expectedPresent -- meaning it will be displayed
127 // on time or possibly late if we show it as soon as possible -- we
128 // acquire and return it. If we don't want to display it until after the
129 // expectedPresent time, we return PRESENT_LATER without acquiring it.
130 //
131 // To be safe, we don't defer acquisition if expectedPresent is more
132 // than one second in the future beyond the desired present time
133 // (i.e., we'd be holding the buffer for a long time).
134 //
135 // NOTE: Code assumes monotonic time values from the system clock
136 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800137
Lajos Molnar5f920c12015-07-13 16:04:24 -0700138 // Start by checking to see if we can drop frames. We skip this check if
139 // the timestamps are being auto-generated by Surface. If the app isn't
140 // generating timestamps explicitly, it probably doesn't want frames to
141 // be discarded based on them.
142 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
143 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700144
Lajos Molnar5f920c12015-07-13 16:04:24 -0700145 // If dropping entry[0] would leave us with a buffer that the
146 // consumer is not yet ready for, don't drop it.
147 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
148 break;
149 }
150
151 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
152 // additional criterion here: we only drop the earlier buffer if our
153 // desiredPresent falls within +/- 1 second of the expected present.
154 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
155 // relative timestamp), which normally mean "ignore the timestamp
156 // and acquire immediately", would cause us to drop frames.
157 //
158 // We may want to add an additional criterion: don't drop the
159 // earlier buffer if entry[1]'s fence hasn't signaled yet.
160 nsecs_t desiredPresent = bufferItem.mTimestamp;
161 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
162 desiredPresent > expectedPresent) {
163 // This buffer is set to display in the near future, or
164 // desiredPresent is garbage. Either way we don't want to drop
165 // the previous buffer just to get this on the screen sooner.
166 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
167 PRId64 " (%" PRId64 ") now=%" PRId64,
168 desiredPresent, expectedPresent,
169 desiredPresent - expectedPresent,
170 systemTime(CLOCK_MONOTONIC));
171 break;
172 }
173
174 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
175 " size=%zu",
176 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800177
178 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700179 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700180 mSlots[front->mSlot].mBufferState.freeQueued();
181
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700182 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700183 // still be around. Mark it as no longer shared if this
184 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700185 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700186 mSlots[front->mSlot].mBufferState.isFree()) {
187 mSlots[front->mSlot].mBufferState.mShared = false;
188 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800189
190 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700191 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800192 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700193 mCore->mFreeBuffers.push_back(front->mSlot);
194 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800195
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700196 if (mCore->mBufferReleasedCbEnabled) {
197 listener = mCore->mConnectedProducerListener;
198 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700199 ++numDroppedBuffers;
200 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800201
Lajos Molnar5f920c12015-07-13 16:04:24 -0700202 mCore->mQueue.erase(front);
203 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700204 }
205
Lajos Molnar5f920c12015-07-13 16:04:24 -0700206 // See if the front buffer is ready to be acquired
207 nsecs_t desiredPresent = front->mTimestamp;
208 bool bufferIsDue = desiredPresent <= expectedPresent ||
209 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
210 bool consumerIsReady = maxFrameNumber > 0 ?
211 front->mFrameNumber <= maxFrameNumber : true;
212 if (!bufferIsDue || !consumerIsReady) {
213 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
214 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
215 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700216 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800217 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700218 systemTime(CLOCK_MONOTONIC),
219 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700220 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700221 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800222 }
223
Lajos Molnar5f920c12015-07-13 16:04:24 -0700224 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
225 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800226 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700227 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800228 }
229
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700230 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
231
232 if (sharedBufferAvailable && mCore->mQueue.empty()) {
233 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200234 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700235
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700236 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700237
238 // Recreate the BufferItem for the shared buffer from the data that
239 // was cached when it was last queued.
240 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
241 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700242 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700243 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
244 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700245 ~static_cast<uint32_t>(
246 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700247 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
248 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700249 outBuffer->mFrameNumber = mCore->mFrameCounter;
250 outBuffer->mSlot = slot;
251 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
252 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700253 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700254 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
255 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700256 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800257 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700258 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800259 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800260 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
261 BQ_LOGV("acquireBuffer: front buffer is not droppable");
262 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700263 } else {
264 slot = front->mSlot;
265 *outBuffer = *front;
266 }
267
Lajos Molnar5f920c12015-07-13 16:04:24 -0700268 ATRACE_BUFFER_INDEX(slot);
269
270 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700271 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800272
273 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700274 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700275 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700276 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700277 // the queue is empty and the BufferItem is created above.
278 if (mCore->mQueue.empty()) {
279 mSlots[slot].mBufferState.acquireNotInQueue();
280 } else {
281 mSlots[slot].mBufferState.acquire();
282 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700283 mSlots[slot].mFence = Fence::NO_FENCE;
284 }
285
286 // If the buffer has previously been acquired by the consumer, set
287 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
288 // on the consumer side
289 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700290 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700291 }
292
293 mCore->mQueue.erase(front);
294
295 // We might have freed a slot while dropping old buffers, or the producer
296 // may be blocked waiting for the number of buffers in the queue to
297 // decrease.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200298 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700299
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000300 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800301#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700302 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800303#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800304 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800305 }
306
Yi Kong48a619f2018-06-05 16:34:59 -0700307 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700308 for (int i = 0; i < numDroppedBuffers; ++i) {
309 listener->onBufferReleased();
310 }
Dan Stoza289ade12014-02-28 11:17:17 -0800311 }
312
Dan Stoza289ade12014-02-28 11:17:17 -0800313 return NO_ERROR;
314}
315
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316status_t BufferQueueConsumer::detachBuffer(int slot) {
317 ATRACE_CALL();
318 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700319 BQ_LOGV("detachBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200320 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800321
Pablo Ceballos38273792016-03-02 01:38:10 +0000322 if (mCore->mIsAbandoned) {
323 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
324 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 }
326
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700327 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
328 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000329 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800330 }
331
Pablo Ceballos38273792016-03-02 01:38:10 +0000332 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
333 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
334 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
335 return BAD_VALUE;
336 } else if (!mSlots[slot].mBufferState.isAcquired()) {
337 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
338 "(state = %s)", slot, mSlots[slot].mBufferState.string());
339 return BAD_VALUE;
340 }
341
342 mSlots[slot].mBufferState.detachConsumer();
343 mCore->mActiveBuffers.erase(slot);
344 mCore->mFreeSlots.insert(slot);
345 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200346 mCore->mDequeueCondition.notify_all();
Pablo Ceballos38273792016-03-02 01:38:10 +0000347 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800348
349 return NO_ERROR;
350}
351
352status_t BufferQueueConsumer::attachBuffer(int* outSlot,
353 const sp<android::GraphicBuffer>& buffer) {
354 ATRACE_CALL();
355
Yi Kong48a619f2018-06-05 16:34:59 -0700356 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700357 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800358 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700359 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700360 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800361 return BAD_VALUE;
362 }
363
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200364 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800365
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700366 if (mCore->mSharedBufferMode) {
367 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700368 return BAD_VALUE;
369 }
370
Dan Stoza0de7ea72015-04-23 13:20:51 -0700371 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800373 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700374 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800375 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800376 }
377 }
378
379 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700380 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 "(max %d)", numAcquiredBuffers,
382 mCore->mMaxAcquiredBufferCount);
383 return INVALID_OPERATION;
384 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700385
Dan Stoza812ed062015-06-02 15:45:22 -0700386 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
387 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
388 "[queue %u]", buffer->getGenerationNumber(),
389 mCore->mGenerationNumber);
390 return BAD_VALUE;
391 }
392
Dan Stoza0de7ea72015-04-23 13:20:51 -0700393 // Find a free slot to put the buffer into
394 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
395 if (!mCore->mFreeSlots.empty()) {
396 auto slot = mCore->mFreeSlots.begin();
397 found = *slot;
398 mCore->mFreeSlots.erase(slot);
399 } else if (!mCore->mFreeBuffers.empty()) {
400 found = mCore->mFreeBuffers.front();
401 mCore->mFreeBuffers.remove(found);
402 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800403 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700404 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800405 return NO_MEMORY;
406 }
407
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800408 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800409 *outSlot = found;
410 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700411 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800412
413 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700414 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800415 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800416 mSlots[*outSlot].mFence = Fence::NO_FENCE;
417 mSlots[*outSlot].mFrameNumber = 0;
418
Dan Stoza99b18b42014-03-28 15:34:33 -0700419 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
420 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
421 // Binder traffic by not un/flattening the GraphicBuffer. However, it
422 // requires that the consumer maintain a cached copy of the slot <--> buffer
423 // mappings, which is why the consumer doesn't need the valid pointer on
424 // acquire.
425 //
426 // The StreamSplitter is one of the primary users of the attach/detach
427 // logic, and while it is running, all buffers it acquires are immediately
428 // detached, and all buffers it eventually releases are ones that were
429 // attached (as opposed to having been obtained from acquireBuffer), so it
430 // doesn't make sense to maintain the slot/buffer mappings, which would
431 // become invalid for every buffer during detach/attach. By setting this to
432 // false, the valid GraphicBuffer pointer will always be sent with acquire
433 // for attached buffers.
434 mSlots[*outSlot].mAcquireCalled = false;
435
Pablo Ceballos9e314332016-01-12 13:49:19 -0800436 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700437
Dan Stoza9f3053d2014-03-06 15:14:33 -0800438 return NO_ERROR;
439}
440
Dan Stoza289ade12014-02-28 11:17:17 -0800441status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
442 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
443 EGLSyncKHR eglFence) {
444 ATRACE_CALL();
445 ATRACE_BUFFER_INDEX(slot);
446
Dan Stoza9f3053d2014-03-06 15:14:33 -0800447 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700448 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700449 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
450 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800451 return BAD_VALUE;
452 }
453
Dan Stozad1c10362014-03-28 15:19:08 -0700454 sp<IProducerListener> listener;
455 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200456 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800457
Dan Stozad1c10362014-03-28 15:19:08 -0700458 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700459 // we can ignore this releaseBuffer for the old buffer.
460 // Ignore this for the shared buffer where the frame number can easily
461 // get out of sync due to the buffer being queued and acquired at the
462 // same time.
463 if (frameNumber != mSlots[slot].mFrameNumber &&
464 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700465 return STALE_BUFFER_SLOT;
466 }
Dan Stoza289ade12014-02-28 11:17:17 -0800467
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800468 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700469 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700470 "but its state was %s", slot,
471 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800472 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800473 }
Dan Stoza289ade12014-02-28 11:17:17 -0800474
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800475 mSlots[slot].mEglDisplay = eglDisplay;
476 mSlots[slot].mEglFence = eglFence;
477 mSlots[slot].mFence = releaseFence;
478 mSlots[slot].mBufferState.release();
479
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700480 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800481 // still be around. Mark it as no longer shared if this
482 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700483 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800484 mSlots[slot].mBufferState.mShared = false;
485 }
486 // Don't put the shared buffer on the free list.
487 if (!mSlots[slot].mBufferState.isShared()) {
488 mCore->mActiveBuffers.erase(slot);
489 mCore->mFreeBuffers.push_back(slot);
490 }
491
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700492 if (mCore->mBufferReleasedCbEnabled) {
493 listener = mCore->mConnectedProducerListener;
494 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800495 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
496
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200497 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800498 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700499 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800500
Dan Stozad1c10362014-03-28 15:19:08 -0700501 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700502 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700503 listener->onBufferReleased();
504 }
Dan Stoza289ade12014-02-28 11:17:17 -0800505
506 return NO_ERROR;
507}
508
509status_t BufferQueueConsumer::connect(
510 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
511 ATRACE_CALL();
512
Yi Kong48a619f2018-06-05 16:34:59 -0700513 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700514 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800515 return BAD_VALUE;
516 }
517
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700518 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800519 controlledByApp ? "true" : "false");
520
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200521 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800522
523 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700524 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800525 return NO_INIT;
526 }
527
528 mCore->mConsumerListener = consumerListener;
529 mCore->mConsumerControlledByApp = controlledByApp;
530
531 return NO_ERROR;
532}
533
534status_t BufferQueueConsumer::disconnect() {
535 ATRACE_CALL();
536
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700537 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800538
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200539 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800540
Yi Kong48a619f2018-06-05 16:34:59 -0700541 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700542 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800543 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800544 }
545
546 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700547 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800548 mCore->mQueue.clear();
549 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700550 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200551 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800552 return NO_ERROR;
553}
554
Dan Stozafebd4f42014-04-09 16:14:51 -0700555status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800556 ATRACE_CALL();
557
Yi Kong48a619f2018-06-05 16:34:59 -0700558 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800559 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
560 return BAD_VALUE;
561 }
562
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200563 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800564
565 if (mCore->mIsAbandoned) {
566 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
567 return NO_INIT;
568 }
569
Dan Stozafebd4f42014-04-09 16:14:51 -0700570 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800571 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800572 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700573 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800574 }
575 }
576
577 // Remove from the mask queued buffers for which acquire has been called,
578 // since the consumer will not receive their buffer addresses and so must
579 // retain their cached information
580 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
581 while (current != mCore->mQueue.end()) {
582 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700583 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800584 }
585 ++current;
586 }
587
Dan Stozafebd4f42014-04-09 16:14:51 -0700588 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800589 *outSlotMask = mask;
590 return NO_ERROR;
591}
592
593status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
594 uint32_t height) {
595 ATRACE_CALL();
596
597 if (width == 0 || height == 0) {
598 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
599 "height=%u)", width, height);
600 return BAD_VALUE;
601 }
602
603 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
604
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200605 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800606 mCore->mDefaultWidth = width;
607 mCore->mDefaultHeight = height;
608 return NO_ERROR;
609}
610
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700611status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800612 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800613
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700614 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
615 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
616 return BAD_VALUE;
617 }
Dan Stoza289ade12014-02-28 11:17:17 -0800618
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200619 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800620
Pablo Ceballos38273792016-03-02 01:38:10 +0000621 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
622 BQ_LOGE("setMaxBufferCount: producer is already connected");
623 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800624 }
625
Pablo Ceballos38273792016-03-02 01:38:10 +0000626 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
627 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
628 "mMaxAcquiredBufferCount (%d)", bufferCount,
629 mCore->mMaxAcquiredBufferCount);
630 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700631 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000632
633 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
634 mCore->mDequeueBufferCannotBlock, bufferCount) -
635 mCore->getMaxBufferCountLocked();
636 if (!mCore->adjustAvailableSlotsLocked(delta)) {
637 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
638 "available slots. Delta = %d", delta);
639 return BAD_VALUE;
640 }
641
642 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800643 return NO_ERROR;
644}
645
646status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
647 int maxAcquiredBuffers) {
648 ATRACE_CALL();
649
650 if (maxAcquiredBuffers < 1 ||
651 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
652 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
653 maxAcquiredBuffers);
654 return BAD_VALUE;
655 }
656
Pablo Ceballos38273792016-03-02 01:38:10 +0000657 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800658 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200659 std::unique_lock<std::mutex> lock(mCore->mMutex);
660 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800661
Pablo Ceballos72daab62015-12-07 16:38:43 -0800662 if (mCore->mIsAbandoned) {
663 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
664 return NO_INIT;
665 }
666
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700667 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
668 return NO_ERROR;
669 }
670
Pablo Ceballos72daab62015-12-07 16:38:43 -0800671 // The new maxAcquiredBuffers count should not be violated by the number
672 // of currently acquired buffers
673 int acquiredCount = 0;
674 for (int slot : mCore->mActiveBuffers) {
675 if (mSlots[slot].mBufferState.isAcquired()) {
676 acquiredCount++;
677 }
678 }
679 if (acquiredCount > maxAcquiredBuffers) {
680 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
681 "count (%d) exceeds the current acquired buffer count (%d)",
682 maxAcquiredBuffers, acquiredCount);
683 return BAD_VALUE;
684 }
685
686 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
687 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
688 > mCore->mMaxBufferCount) {
689 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
690 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
691 maxAcquiredBuffers, mCore->mMaxBufferCount,
692 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
693 mCore->mDequeueBufferCannotBlock);
694 return BAD_VALUE;
695 }
696
697 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000698 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800699 return BAD_VALUE;
700 }
701
702 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
703 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
704 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700705 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000706 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800707 }
708 }
709 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700710 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000711 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800712 }
713
Dan Stoza289ade12014-02-28 11:17:17 -0800714 return NO_ERROR;
715}
716
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700717status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800718 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000719 BQ_LOGV("setConsumerName: '%s'", name.c_str());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200720 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800721 mCore->mConsumerName = name;
722 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700723 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800724}
725
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800726status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800727 ATRACE_CALL();
728 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200729 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800730 mCore->mDefaultBufferFormat = defaultFormat;
731 return NO_ERROR;
732}
733
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800734status_t BufferQueueConsumer::setDefaultBufferDataSpace(
735 android_dataspace defaultDataSpace) {
736 ATRACE_CALL();
737 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200738 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800739 mCore->mDefaultBufferDataSpace = defaultDataSpace;
740 return NO_ERROR;
741}
742
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700743status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800744 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700745 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200746 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800747 mCore->mConsumerUsageBits = usage;
748 return NO_ERROR;
749}
750
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700751status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
752 ATRACE_CALL();
753 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200754 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700755 mCore->mConsumerIsProtected = isProtected;
756 return NO_ERROR;
757}
758
Dan Stoza289ade12014-02-28 11:17:17 -0800759status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
760 ATRACE_CALL();
761 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200762 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800763 mCore->mTransformHint = hint;
764 return NO_ERROR;
765}
766
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700767status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200768 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700769 *outStream = mCore->mSidebandStream;
770 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800771}
772
Dan Stozae77c7662016-05-13 11:37:28 -0700773status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
774 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200775 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800776#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700777 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800778#else
779 (void)forceFlush;
780 outHistory->clear();
781#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700782 return NO_ERROR;
783}
784
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700785status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200786 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700787 mCore->discardFreeBuffersLocked();
788 return NO_ERROR;
789}
790
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700791status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700792 struct passwd* pwd = getpwnam("shell");
793 uid_t shellUid = pwd ? pwd->pw_uid : 0;
794 if (!shellUid) {
795 int savedErrno = errno;
796 BQ_LOGE("Cannot get AID_SHELL");
797 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
798 }
799
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800800 bool denied = false;
801 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800802#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900803 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800804 // the PermissionController. We need to do a runtime check as well, since
805 // the system variant of libgui can be loaded in a vendor process. For eg:
806 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
807 if (!android_is_in_vendor_process()) {
808 const pid_t pid = BufferQueueThreadState::getCallingPid();
809 if ((uid != shellUid) &&
810 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
811 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
812 "from pid=%d, uid=%d\n",
813 pid, uid);
814 denied = true;
815 }
816 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900817#else
818 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800819 denied = true;
820 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900821#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800822 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700823 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700824 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700825 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800826 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700827
828 mCore->dumpState(prefix, outResult);
829 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800830}
831
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800832void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
833 std::lock_guard<std::mutex> lock(mCore->mMutex);
834 mCore->mAllowExtraAcquire = allow;
835}
836
Dan Stoza289ade12014-02-28 11:17:17 -0800837} // namespace android