blob: 744201a5df0db5e4144dd78106f935af80c34536 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
Yifan Hong65799c32017-07-26 10:47:14 -070018#include <pwd.h>
19#include <sys/types.h>
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070020
Dan Stoza3e96f192014-03-03 10:16:19 -080021#define LOG_TAG "BufferQueueConsumer"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23//#define LOG_NDEBUG 0
24
Pablo Ceballos9e314332016-01-12 13:49:19 -080025#if DEBUG_ONLY_CODE
26#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
27#else
28#define VALIDATE_CONSISTENCY()
29#endif
30
Dan Stoza289ade12014-02-28 11:17:17 -080031#include <gui/BufferItem.h>
32#include <gui/BufferQueueConsumer.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070035#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070036#include <gui/TraceUtils.h>
Dan Stoza289ade12014-02-28 11:17:17 -080037
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080038#include <private/gui/BufferQueueThreadState.h>
Kiyoung Kim5c08e302023-11-10 16:35:13 +090039#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Pablo Ceballos88f69282016-02-11 18:01:49 -080040#include <binder/PermissionCache.h>
Kiyoung Kim5c08e302023-11-10 16:35:13 +090041#include <selinux/android.h>
42#include <selinux/selinux.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090043#endif
Pablo Ceballos88f69282016-02-11 18:01:49 -080044
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070045#include <system/window.h>
46
Kiyoung Kim5c08e302023-11-10 16:35:13 +090047namespace {
48#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
49int selinux_log_suppress_callback(int, const char*, ...) { // NOLINT
50 // DO NOTHING
51 return 0;
52}
53
54bool hasAccessToPermissionService() {
55 char* ctx;
56
57 if (getcon(&ctx) == -1) {
58 // Failed to get current selinux context
59 return false;
60 }
61
62 union selinux_callback cb;
63
64 cb.func_log = selinux_log_suppress_callback;
65 selinux_set_callback(SELINUX_CB_LOG, cb);
66
67 bool hasAccess = selinux_check_access(ctx, "u:object_r:permission_service:s0",
68 "service_manager", "find", NULL) == 0;
69 freecon(ctx);
70 cb.func_log = hasAccess ? selinux_log_callback : selinux_vendor_log_callback;
71 selinux_set_callback(SELINUX_CB_LOG, cb);
72
73 return hasAccess;
74}
75#endif
76} // namespace
77
Dan Stoza289ade12014-02-28 11:17:17 -080078namespace android {
79
Iris Chang430193f2019-12-04 16:25:46 +080080// Macros for include BufferQueueCore information in log messages
81#define BQ_LOGV(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000082 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080083 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
84 ##__VA_ARGS__)
85#define BQ_LOGD(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000086 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080087 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
88 ##__VA_ARGS__)
89#define BQ_LOGI(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000090 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080091 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
92 ##__VA_ARGS__)
93#define BQ_LOGW(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000094 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080095 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
96 ##__VA_ARGS__)
97#define BQ_LOGE(x, ...) \
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +000098 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080099 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
100 ##__VA_ARGS__)
101
Chong Zhang62493092020-01-15 16:04:47 -0800102ConsumerListener::~ConsumerListener() = default;
103
Dan Stoza289ade12014-02-28 11:17:17 -0800104BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
105 mCore(core),
106 mSlots(core->mSlots),
107 mConsumerName() {}
108
109BufferQueueConsumer::~BufferQueueConsumer() {}
110
111status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -0700112 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -0800113 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800114
Lajos Molnar5f920c12015-07-13 16:04:24 -0700115 int numDroppedBuffers = 0;
116 sp<IProducerListener> listener;
117 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200118 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -0700119
120 // Check that the consumer doesn't currently have the maximum number of
121 // buffers acquired. We allow the max buffer count to be exceeded by one
122 // buffer so that the consumer can successfully set up the newly acquired
123 // buffer before releasing the old one.
124 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800125 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700126 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700127 ++numAcquiredBuffers;
128 }
Dan Stoza289ade12014-02-28 11:17:17 -0800129 }
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800130 const bool acquireNonDroppableBuffer = mCore->mAllowExtraAcquire &&
131 numAcquiredBuffers == mCore->mMaxAcquiredBufferCount + 1;
132 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1 &&
133 !acquireNonDroppableBuffer) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700134 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
135 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
136 return INVALID_OPERATION;
137 }
Dan Stoza289ade12014-02-28 11:17:17 -0800138
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700139 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
140 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700141 BufferQueueCore::INVALID_BUFFER_SLOT;
142
Lajos Molnar5f920c12015-07-13 16:04:24 -0700143 // In asynchronous mode the list is guaranteed to be one buffer deep,
144 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700145 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700146 return NO_BUFFER_AVAILABLE;
147 }
Dan Stoza289ade12014-02-28 11:17:17 -0800148
Lajos Molnar5f920c12015-07-13 16:04:24 -0700149 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -0800150
Lajos Molnar5f920c12015-07-13 16:04:24 -0700151 // If expectedPresent is specified, we may not want to return a buffer yet.
152 // If it's specified and there's more than one buffer queued, we may want
153 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700154 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700155 // since in that case we'll just return the shared buffer.
156 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700157 // The 'expectedPresent' argument indicates when the buffer is expected
158 // to be presented on-screen. If the buffer's desired present time is
159 // earlier (less) than expectedPresent -- meaning it will be displayed
160 // on time or possibly late if we show it as soon as possible -- we
161 // acquire and return it. If we don't want to display it until after the
162 // expectedPresent time, we return PRESENT_LATER without acquiring it.
163 //
164 // To be safe, we don't defer acquisition if expectedPresent is more
165 // than one second in the future beyond the desired present time
166 // (i.e., we'd be holding the buffer for a long time).
167 //
168 // NOTE: Code assumes monotonic time values from the system clock
169 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800170
Lajos Molnar5f920c12015-07-13 16:04:24 -0700171 // Start by checking to see if we can drop frames. We skip this check if
172 // the timestamps are being auto-generated by Surface. If the app isn't
173 // generating timestamps explicitly, it probably doesn't want frames to
174 // be discarded based on them.
175 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
176 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700177
Lajos Molnar5f920c12015-07-13 16:04:24 -0700178 // If dropping entry[0] would leave us with a buffer that the
179 // consumer is not yet ready for, don't drop it.
180 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
181 break;
182 }
183
184 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
185 // additional criterion here: we only drop the earlier buffer if our
186 // desiredPresent falls within +/- 1 second of the expected present.
187 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
188 // relative timestamp), which normally mean "ignore the timestamp
189 // and acquire immediately", would cause us to drop frames.
190 //
191 // We may want to add an additional criterion: don't drop the
192 // earlier buffer if entry[1]'s fence hasn't signaled yet.
193 nsecs_t desiredPresent = bufferItem.mTimestamp;
194 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
195 desiredPresent > expectedPresent) {
196 // This buffer is set to display in the near future, or
197 // desiredPresent is garbage. Either way we don't want to drop
198 // the previous buffer just to get this on the screen sooner.
199 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
200 PRId64 " (%" PRId64 ") now=%" PRId64,
201 desiredPresent, expectedPresent,
202 desiredPresent - expectedPresent,
203 systemTime(CLOCK_MONOTONIC));
204 break;
205 }
206
207 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
208 " size=%zu",
209 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800210
211 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700212 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700213 mSlots[front->mSlot].mBufferState.freeQueued();
214
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700215 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700216 // still be around. Mark it as no longer shared if this
217 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700218 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700219 mSlots[front->mSlot].mBufferState.isFree()) {
220 mSlots[front->mSlot].mBufferState.mShared = false;
221 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800222
223 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700224 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800225 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700226 mCore->mFreeBuffers.push_back(front->mSlot);
227 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800228
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700229 if (mCore->mBufferReleasedCbEnabled) {
230 listener = mCore->mConnectedProducerListener;
231 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700232 ++numDroppedBuffers;
233 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800234
Lajos Molnar5f920c12015-07-13 16:04:24 -0700235 mCore->mQueue.erase(front);
236 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700237 }
238
Lajos Molnar5f920c12015-07-13 16:04:24 -0700239 // See if the front buffer is ready to be acquired
240 nsecs_t desiredPresent = front->mTimestamp;
241 bool bufferIsDue = desiredPresent <= expectedPresent ||
242 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
243 bool consumerIsReady = maxFrameNumber > 0 ?
244 front->mFrameNumber <= maxFrameNumber : true;
245 if (!bufferIsDue || !consumerIsReady) {
246 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
247 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
248 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700249 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800250 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700251 systemTime(CLOCK_MONOTONIC),
252 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700253 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700254 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800255 }
256
Lajos Molnar5f920c12015-07-13 16:04:24 -0700257 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
258 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800259 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700260 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800261 }
262
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700263 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
264
265 if (sharedBufferAvailable && mCore->mQueue.empty()) {
266 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200267 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700268
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700269 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700270
271 // Recreate the BufferItem for the shared buffer from the data that
272 // was cached when it was last queued.
273 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
274 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700275 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700276 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
277 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700278 ~static_cast<uint32_t>(
279 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700280 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
281 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700282 outBuffer->mFrameNumber = mCore->mFrameCounter;
283 outBuffer->mSlot = slot;
284 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
285 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700286 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700287 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
288 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700289 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800290 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700291 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800292 mCore->mAutoRefresh;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800293 } else if (acquireNonDroppableBuffer && front->mIsDroppable) {
294 BQ_LOGV("acquireBuffer: front buffer is not droppable");
295 return NO_BUFFER_AVAILABLE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296 } else {
297 slot = front->mSlot;
298 *outBuffer = *front;
299 }
300
Lajos Molnar5f920c12015-07-13 16:04:24 -0700301 ATRACE_BUFFER_INDEX(slot);
302
303 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700304 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800305
306 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700307 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700308 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700309 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700310 // the queue is empty and the BufferItem is created above.
311 if (mCore->mQueue.empty()) {
312 mSlots[slot].mBufferState.acquireNotInQueue();
313 } else {
314 mSlots[slot].mBufferState.acquire();
315 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700316 mSlots[slot].mFence = Fence::NO_FENCE;
317 }
318
319 // If the buffer has previously been acquired by the consumer, set
320 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
321 // on the consumer side
322 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700323 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700324 }
325
326 mCore->mQueue.erase(front);
327
328 // We might have freed a slot while dropping old buffers, or the producer
329 // may be blocked waiting for the number of buffers in the queue to
330 // decrease.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200331 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700332
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000333 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -0800334#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700335 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -0800336#endif
Pablo Ceballos9e314332016-01-12 13:49:19 -0800337 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800338 }
339
Yi Kong48a619f2018-06-05 16:34:59 -0700340 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700341 for (int i = 0; i < numDroppedBuffers; ++i) {
342 listener->onBufferReleased();
343 }
Dan Stoza289ade12014-02-28 11:17:17 -0800344 }
345
Dan Stoza289ade12014-02-28 11:17:17 -0800346 return NO_ERROR;
347}
348
Dan Stoza9f3053d2014-03-06 15:14:33 -0800349status_t BufferQueueConsumer::detachBuffer(int slot) {
350 ATRACE_CALL();
351 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700352 BQ_LOGV("detachBuffer: slot %d", slot);
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000353 sp<IProducerListener> listener;
354 {
355 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800356
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000357 if (mCore->mIsAbandoned) {
358 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
359 return NO_INIT;
360 }
361
362 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
363 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
364 return BAD_VALUE;
365 }
366
367 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
368 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
369 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
370 return BAD_VALUE;
371 } else if (!mSlots[slot].mBufferState.isAcquired()) {
372 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
373 "(state = %s)", slot, mSlots[slot].mBufferState.string());
374 return BAD_VALUE;
375 }
376 if (mCore->mBufferReleasedCbEnabled) {
377 listener = mCore->mConnectedProducerListener;
378 }
379
380 mSlots[slot].mBufferState.detachConsumer();
381 mCore->mActiveBuffers.erase(slot);
382 mCore->mFreeSlots.insert(slot);
383 mCore->clearBufferSlotLocked(slot);
384 mCore->mDequeueCondition.notify_all();
385 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386 }
387
Sungtak Lee1e7c09b2023-10-26 08:44:21 +0000388 if (listener) {
389 listener->onBufferDetached(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800390 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391 return NO_ERROR;
392}
393
394status_t BufferQueueConsumer::attachBuffer(int* outSlot,
395 const sp<android::GraphicBuffer>& buffer) {
396 ATRACE_CALL();
397
Yi Kong48a619f2018-06-05 16:34:59 -0700398 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700399 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800400 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700401 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700402 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800403 return BAD_VALUE;
404 }
405
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200406 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800407
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700408 if (mCore->mSharedBufferMode) {
409 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700410 return BAD_VALUE;
411 }
412
Dan Stoza0de7ea72015-04-23 13:20:51 -0700413 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800414 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800415 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700416 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800417 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800418 }
419 }
420
421 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700422 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800423 "(max %d)", numAcquiredBuffers,
424 mCore->mMaxAcquiredBufferCount);
425 return INVALID_OPERATION;
426 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700427
Dan Stoza812ed062015-06-02 15:45:22 -0700428 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
429 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
430 "[queue %u]", buffer->getGenerationNumber(),
431 mCore->mGenerationNumber);
432 return BAD_VALUE;
433 }
434
Dan Stoza0de7ea72015-04-23 13:20:51 -0700435 // Find a free slot to put the buffer into
436 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
437 if (!mCore->mFreeSlots.empty()) {
438 auto slot = mCore->mFreeSlots.begin();
439 found = *slot;
440 mCore->mFreeSlots.erase(slot);
441 } else if (!mCore->mFreeBuffers.empty()) {
442 found = mCore->mFreeBuffers.front();
443 mCore->mFreeBuffers.remove(found);
444 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800445 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700446 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800447 return NO_MEMORY;
448 }
449
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800450 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800451 *outSlot = found;
452 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700453 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800454
455 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700456 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800457 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800458 mSlots[*outSlot].mFence = Fence::NO_FENCE;
459 mSlots[*outSlot].mFrameNumber = 0;
460
Dan Stoza99b18b42014-03-28 15:34:33 -0700461 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
462 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
463 // Binder traffic by not un/flattening the GraphicBuffer. However, it
464 // requires that the consumer maintain a cached copy of the slot <--> buffer
465 // mappings, which is why the consumer doesn't need the valid pointer on
466 // acquire.
467 //
468 // The StreamSplitter is one of the primary users of the attach/detach
469 // logic, and while it is running, all buffers it acquires are immediately
470 // detached, and all buffers it eventually releases are ones that were
471 // attached (as opposed to having been obtained from acquireBuffer), so it
472 // doesn't make sense to maintain the slot/buffer mappings, which would
473 // become invalid for every buffer during detach/attach. By setting this to
474 // false, the valid GraphicBuffer pointer will always be sent with acquire
475 // for attached buffers.
476 mSlots[*outSlot].mAcquireCalled = false;
477
Pablo Ceballos9e314332016-01-12 13:49:19 -0800478 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700479
Dan Stoza9f3053d2014-03-06 15:14:33 -0800480 return NO_ERROR;
481}
482
Dan Stoza289ade12014-02-28 11:17:17 -0800483status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
484 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
485 EGLSyncKHR eglFence) {
486 ATRACE_CALL();
487 ATRACE_BUFFER_INDEX(slot);
488
Dan Stoza9f3053d2014-03-06 15:14:33 -0800489 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700490 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700491 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
492 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800493 return BAD_VALUE;
494 }
495
Dan Stozad1c10362014-03-28 15:19:08 -0700496 sp<IProducerListener> listener;
497 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200498 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800499
Dan Stozad1c10362014-03-28 15:19:08 -0700500 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700501 // we can ignore this releaseBuffer for the old buffer.
502 // Ignore this for the shared buffer where the frame number can easily
503 // get out of sync due to the buffer being queued and acquired at the
504 // same time.
505 if (frameNumber != mSlots[slot].mFrameNumber &&
506 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700507 return STALE_BUFFER_SLOT;
508 }
Dan Stoza289ade12014-02-28 11:17:17 -0800509
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800510 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700511 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700512 "but its state was %s", slot,
513 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800514 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800515 }
Dan Stoza289ade12014-02-28 11:17:17 -0800516
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800517 mSlots[slot].mEglDisplay = eglDisplay;
518 mSlots[slot].mEglFence = eglFence;
519 mSlots[slot].mFence = releaseFence;
520 mSlots[slot].mBufferState.release();
521
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700522 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800523 // still be around. Mark it as no longer shared if this
524 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700525 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800526 mSlots[slot].mBufferState.mShared = false;
527 }
528 // Don't put the shared buffer on the free list.
529 if (!mSlots[slot].mBufferState.isShared()) {
530 mCore->mActiveBuffers.erase(slot);
531 mCore->mFreeBuffers.push_back(slot);
532 }
533
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700534 if (mCore->mBufferReleasedCbEnabled) {
535 listener = mCore->mConnectedProducerListener;
536 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800537 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
538
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200539 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800540 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700541 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800542
Dan Stozad1c10362014-03-28 15:19:08 -0700543 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700544 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700545 listener->onBufferReleased();
546 }
Dan Stoza289ade12014-02-28 11:17:17 -0800547
548 return NO_ERROR;
549}
550
551status_t BufferQueueConsumer::connect(
552 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
553 ATRACE_CALL();
554
Yi Kong48a619f2018-06-05 16:34:59 -0700555 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700556 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800557 return BAD_VALUE;
558 }
559
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700560 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800561 controlledByApp ? "true" : "false");
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) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700566 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800567 return NO_INIT;
568 }
569
570 mCore->mConsumerListener = consumerListener;
571 mCore->mConsumerControlledByApp = controlledByApp;
572
573 return NO_ERROR;
574}
575
576status_t BufferQueueConsumer::disconnect() {
577 ATRACE_CALL();
578
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700579 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800580
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200581 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800582
Yi Kong48a619f2018-06-05 16:34:59 -0700583 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700584 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800585 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800586 }
587
588 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700589 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800590 mCore->mQueue.clear();
591 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700592 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200593 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800594 return NO_ERROR;
595}
596
Dan Stozafebd4f42014-04-09 16:14:51 -0700597status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800598 ATRACE_CALL();
599
Yi Kong48a619f2018-06-05 16:34:59 -0700600 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800601 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
602 return BAD_VALUE;
603 }
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
607 if (mCore->mIsAbandoned) {
608 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
609 return NO_INIT;
610 }
611
Dan Stozafebd4f42014-04-09 16:14:51 -0700612 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800613 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800614 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700615 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800616 }
617 }
618
619 // Remove from the mask queued buffers for which acquire has been called,
620 // since the consumer will not receive their buffer addresses and so must
621 // retain their cached information
622 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
623 while (current != mCore->mQueue.end()) {
624 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700625 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800626 }
627 ++current;
628 }
629
Dan Stozafebd4f42014-04-09 16:14:51 -0700630 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800631 *outSlotMask = mask;
632 return NO_ERROR;
633}
634
635status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
636 uint32_t height) {
637 ATRACE_CALL();
638
639 if (width == 0 || height == 0) {
640 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
641 "height=%u)", width, height);
642 return BAD_VALUE;
643 }
644
645 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
646
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200647 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800648 mCore->mDefaultWidth = width;
649 mCore->mDefaultHeight = height;
650 return NO_ERROR;
651}
652
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700653status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800654 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800655
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700656 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
657 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
658 return BAD_VALUE;
659 }
Dan Stoza289ade12014-02-28 11:17:17 -0800660
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200661 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800662
Pablo Ceballos38273792016-03-02 01:38:10 +0000663 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
664 BQ_LOGE("setMaxBufferCount: producer is already connected");
665 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800666 }
667
Pablo Ceballos38273792016-03-02 01:38:10 +0000668 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
669 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
670 "mMaxAcquiredBufferCount (%d)", bufferCount,
671 mCore->mMaxAcquiredBufferCount);
672 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700673 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000674
675 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
676 mCore->mDequeueBufferCannotBlock, bufferCount) -
677 mCore->getMaxBufferCountLocked();
678 if (!mCore->adjustAvailableSlotsLocked(delta)) {
679 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
680 "available slots. Delta = %d", delta);
681 return BAD_VALUE;
682 }
683
684 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800685 return NO_ERROR;
686}
687
688status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
689 int maxAcquiredBuffers) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700690 ATRACE_FORMAT("%s(%d)", __func__, maxAcquiredBuffers);
Dan Stoza289ade12014-02-28 11:17:17 -0800691
692 if (maxAcquiredBuffers < 1 ||
693 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
694 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
695 maxAcquiredBuffers);
696 return BAD_VALUE;
697 }
698
Pablo Ceballos38273792016-03-02 01:38:10 +0000699 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800700 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200701 std::unique_lock<std::mutex> lock(mCore->mMutex);
702 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800703
Pablo Ceballos72daab62015-12-07 16:38:43 -0800704 if (mCore->mIsAbandoned) {
705 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
706 return NO_INIT;
707 }
708
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700709 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
710 return NO_ERROR;
711 }
712
Pablo Ceballos72daab62015-12-07 16:38:43 -0800713 // The new maxAcquiredBuffers count should not be violated by the number
714 // of currently acquired buffers
715 int acquiredCount = 0;
716 for (int slot : mCore->mActiveBuffers) {
717 if (mSlots[slot].mBufferState.isAcquired()) {
718 acquiredCount++;
719 }
720 }
721 if (acquiredCount > maxAcquiredBuffers) {
722 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
723 "count (%d) exceeds the current acquired buffer count (%d)",
724 maxAcquiredBuffers, acquiredCount);
725 return BAD_VALUE;
726 }
727
728 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
729 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
730 > mCore->mMaxBufferCount) {
731 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
732 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
733 maxAcquiredBuffers, mCore->mMaxBufferCount,
734 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
735 mCore->mDequeueBufferCannotBlock);
736 return BAD_VALUE;
737 }
738
739 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000740 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800741 return BAD_VALUE;
742 }
743
744 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
745 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
746 VALIDATE_CONSISTENCY();
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700747 if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000748 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800749 }
750 }
751 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700752 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000753 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800754 }
755
Dan Stoza289ade12014-02-28 11:17:17 -0800756 return NO_ERROR;
757}
758
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700759status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800760 ATRACE_CALL();
Tomasz Wasilczyk0e1adf42023-08-11 00:06:51 +0000761 BQ_LOGV("setConsumerName: '%s'", name.c_str());
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->mConsumerName = name;
764 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700765 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800766}
767
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800768status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800769 ATRACE_CALL();
770 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200771 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800772 mCore->mDefaultBufferFormat = defaultFormat;
773 return NO_ERROR;
774}
775
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800776status_t BufferQueueConsumer::setDefaultBufferDataSpace(
777 android_dataspace defaultDataSpace) {
778 ATRACE_CALL();
779 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200780 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800781 mCore->mDefaultBufferDataSpace = defaultDataSpace;
782 return NO_ERROR;
783}
784
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700785status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800786 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700787 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200788 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800789 mCore->mConsumerUsageBits = usage;
790 return NO_ERROR;
791}
792
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700793status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
794 ATRACE_CALL();
795 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200796 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700797 mCore->mConsumerIsProtected = isProtected;
798 return NO_ERROR;
799}
800
Dan Stoza289ade12014-02-28 11:17:17 -0800801status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
802 ATRACE_CALL();
803 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200804 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800805 mCore->mTransformHint = hint;
806 return NO_ERROR;
807}
808
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700809status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200810 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700811 *outStream = mCore->mSidebandStream;
812 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800813}
814
Dan Stozae77c7662016-05-13 11:37:28 -0700815status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
816 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200817 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chong Zhang62493092020-01-15 16:04:47 -0800818#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -0700819 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
Chong Zhang62493092020-01-15 16:04:47 -0800820#else
821 (void)forceFlush;
822 outHistory->clear();
823#endif
Dan Stozae77c7662016-05-13 11:37:28 -0700824 return NO_ERROR;
825}
826
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700827status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200828 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700829 mCore->discardFreeBuffersLocked();
830 return NO_ERROR;
831}
832
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700833status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700834 struct passwd* pwd = getpwnam("shell");
835 uid_t shellUid = pwd ? pwd->pw_uid : 0;
836 if (!shellUid) {
837 int savedErrno = errno;
838 BQ_LOGE("Cannot get AID_SHELL");
839 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
840 }
841
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800842 bool denied = false;
843 const uid_t uid = BufferQueueThreadState::getCallingUid();
Chong Zhang62493092020-01-15 16:04:47 -0800844#if !defined(__ANDROID_VNDK__) && !defined(NO_BINDER)
Jiyong Park47f876b2018-04-17 13:56:46 +0900845 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800846 // the PermissionController. We need to do a runtime check as well, since
847 // the system variant of libgui can be loaded in a vendor process. For eg:
848 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
Kiyoung Kim5c08e302023-11-10 16:35:13 +0900849 if (hasAccessToPermissionService()) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800850 const pid_t pid = BufferQueueThreadState::getCallingPid();
851 if ((uid != shellUid) &&
852 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
853 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
854 "from pid=%d, uid=%d\n",
855 pid, uid);
856 denied = true;
857 }
858 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900859#else
860 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800861 denied = true;
862 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900863#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800864 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700865 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700866 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700867 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800868 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700869
870 mCore->dumpState(prefix, outResult);
871 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800872}
873
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800874void BufferQueueConsumer::setAllowExtraAcquire(bool allow) {
875 std::lock_guard<std::mutex> lock(mCore->mMutex);
876 mCore->mAllowExtraAcquire = allow;
877}
878
Dan Stoza289ade12014-02-28 11:17:17 -0800879} // namespace android