blob: e2261360b6fa06994df469b5b3c62dc5a00f6036 [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
47BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
48 mCore(core),
49 mSlots(core->mSlots),
50 mConsumerName() {}
51
52BufferQueueConsumer::~BufferQueueConsumer() {}
53
54status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070055 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080056 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080057
Lajos Molnar5f920c12015-07-13 16:04:24 -070058 int numDroppedBuffers = 0;
59 sp<IProducerListener> listener;
60 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +020061 std::unique_lock<std::mutex> lock(mCore->mMutex);
Lajos Molnar5f920c12015-07-13 16:04:24 -070062
63 // Check that the consumer doesn't currently have the maximum number of
64 // buffers acquired. We allow the max buffer count to be exceeded by one
65 // buffer so that the consumer can successfully set up the newly acquired
66 // buffer before releasing the old one.
67 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080068 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070069 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070070 ++numAcquiredBuffers;
71 }
Dan Stoza289ade12014-02-28 11:17:17 -080072 }
Lajos Molnar5f920c12015-07-13 16:04:24 -070073 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
74 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
75 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
76 return INVALID_OPERATION;
77 }
Dan Stoza289ade12014-02-28 11:17:17 -080078
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070079 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
80 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -070081 BufferQueueCore::INVALID_BUFFER_SLOT;
82
Lajos Molnar5f920c12015-07-13 16:04:24 -070083 // In asynchronous mode the list is guaranteed to be one buffer deep,
84 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070085 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070086 return NO_BUFFER_AVAILABLE;
87 }
Dan Stoza289ade12014-02-28 11:17:17 -080088
Lajos Molnar5f920c12015-07-13 16:04:24 -070089 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080090
Lajos Molnar5f920c12015-07-13 16:04:24 -070091 // If expectedPresent is specified, we may not want to return a buffer yet.
92 // If it's specified and there's more than one buffer queued, we may want
93 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070094 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -070095 // since in that case we'll just return the shared buffer.
96 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070097 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080098
Lajos Molnar5f920c12015-07-13 16:04:24 -070099 // The 'expectedPresent' argument indicates when the buffer is expected
100 // to be presented on-screen. If the buffer's desired present time is
101 // earlier (less) than expectedPresent -- meaning it will be displayed
102 // on time or possibly late if we show it as soon as possible -- we
103 // acquire and return it. If we don't want to display it until after the
104 // expectedPresent time, we return PRESENT_LATER without acquiring it.
105 //
106 // To be safe, we don't defer acquisition if expectedPresent is more
107 // than one second in the future beyond the desired present time
108 // (i.e., we'd be holding the buffer for a long time).
109 //
110 // NOTE: Code assumes monotonic time values from the system clock
111 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800112
Lajos Molnar5f920c12015-07-13 16:04:24 -0700113 // Start by checking to see if we can drop frames. We skip this check if
114 // the timestamps are being auto-generated by Surface. If the app isn't
115 // generating timestamps explicitly, it probably doesn't want frames to
116 // be discarded based on them.
117 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
118 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700119
Lajos Molnar5f920c12015-07-13 16:04:24 -0700120 // If dropping entry[0] would leave us with a buffer that the
121 // consumer is not yet ready for, don't drop it.
122 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
123 break;
124 }
125
126 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
127 // additional criterion here: we only drop the earlier buffer if our
128 // desiredPresent falls within +/- 1 second of the expected present.
129 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
130 // relative timestamp), which normally mean "ignore the timestamp
131 // and acquire immediately", would cause us to drop frames.
132 //
133 // We may want to add an additional criterion: don't drop the
134 // earlier buffer if entry[1]'s fence hasn't signaled yet.
135 nsecs_t desiredPresent = bufferItem.mTimestamp;
136 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
137 desiredPresent > expectedPresent) {
138 // This buffer is set to display in the near future, or
139 // desiredPresent is garbage. Either way we don't want to drop
140 // the previous buffer just to get this on the screen sooner.
141 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
142 PRId64 " (%" PRId64 ") now=%" PRId64,
143 desiredPresent, expectedPresent,
144 desiredPresent - expectedPresent,
145 systemTime(CLOCK_MONOTONIC));
146 break;
147 }
148
149 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
150 " size=%zu",
151 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800152
153 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700154 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700155 mSlots[front->mSlot].mBufferState.freeQueued();
156
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700157 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700158 // still be around. Mark it as no longer shared if this
159 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700160 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700161 mSlots[front->mSlot].mBufferState.isFree()) {
162 mSlots[front->mSlot].mBufferState.mShared = false;
163 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800164
165 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700166 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800167 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700168 mCore->mFreeBuffers.push_back(front->mSlot);
169 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800170
Lajos Molnar5f920c12015-07-13 16:04:24 -0700171 listener = mCore->mConnectedProducerListener;
172 ++numDroppedBuffers;
173 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800174
Lajos Molnar5f920c12015-07-13 16:04:24 -0700175 mCore->mQueue.erase(front);
176 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700177 }
178
Lajos Molnar5f920c12015-07-13 16:04:24 -0700179 // See if the front buffer is ready to be acquired
180 nsecs_t desiredPresent = front->mTimestamp;
181 bool bufferIsDue = desiredPresent <= expectedPresent ||
182 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
183 bool consumerIsReady = maxFrameNumber > 0 ?
184 front->mFrameNumber <= maxFrameNumber : true;
185 if (!bufferIsDue || !consumerIsReady) {
186 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
187 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
188 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700189 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800190 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700191 systemTime(CLOCK_MONOTONIC),
192 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700193 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700194 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800195 }
196
Lajos Molnar5f920c12015-07-13 16:04:24 -0700197 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
198 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800199 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700200 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800201 }
202
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700203 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
204
205 if (sharedBufferAvailable && mCore->mQueue.empty()) {
206 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200207 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700208
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700209 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700210
211 // Recreate the BufferItem for the shared buffer from the data that
212 // was cached when it was last queued.
213 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
214 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700215 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700216 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
217 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700218 ~static_cast<uint32_t>(
219 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700220 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
221 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700222 outBuffer->mFrameNumber = mCore->mFrameCounter;
223 outBuffer->mSlot = slot;
224 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
225 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700226 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700227 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
228 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700229 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800230 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700231 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800232 mCore->mAutoRefresh;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700233 } else {
234 slot = front->mSlot;
235 *outBuffer = *front;
236 }
237
Lajos Molnar5f920c12015-07-13 16:04:24 -0700238 ATRACE_BUFFER_INDEX(slot);
239
240 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700241 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800242
243 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700244 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700245 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700246 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700247 // the queue is empty and the BufferItem is created above.
248 if (mCore->mQueue.empty()) {
249 mSlots[slot].mBufferState.acquireNotInQueue();
250 } else {
251 mSlots[slot].mBufferState.acquire();
252 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700253 mSlots[slot].mFence = Fence::NO_FENCE;
254 }
255
256 // If the buffer has previously been acquired by the consumer, set
257 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
258 // on the consumer side
259 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700260 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700261 }
262
263 mCore->mQueue.erase(front);
264
265 // We might have freed a slot while dropping old buffers, or the producer
266 // may be blocked waiting for the number of buffers in the queue to
267 // decrease.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200268 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700269
Colin Cross6e7e2b42016-09-27 14:08:19 -0700270 ATRACE_INT(mCore->mConsumerName.string(),
271 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700272 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Lajos Molnar5f920c12015-07-13 16:04:24 -0700273
Pablo Ceballos9e314332016-01-12 13:49:19 -0800274 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800275 }
276
Yi Kong48a619f2018-06-05 16:34:59 -0700277 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700278 for (int i = 0; i < numDroppedBuffers; ++i) {
279 listener->onBufferReleased();
280 }
Dan Stoza289ade12014-02-28 11:17:17 -0800281 }
282
Dan Stoza289ade12014-02-28 11:17:17 -0800283 return NO_ERROR;
284}
285
Dan Stoza9f3053d2014-03-06 15:14:33 -0800286status_t BufferQueueConsumer::detachBuffer(int slot) {
287 ATRACE_CALL();
288 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700289 BQ_LOGV("detachBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200290 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800291
Pablo Ceballos38273792016-03-02 01:38:10 +0000292 if (mCore->mIsAbandoned) {
293 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
294 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800295 }
296
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700297 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
298 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000299 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800300 }
301
Pablo Ceballos38273792016-03-02 01:38:10 +0000302 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
303 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
304 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
305 return BAD_VALUE;
306 } else if (!mSlots[slot].mBufferState.isAcquired()) {
307 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
308 "(state = %s)", slot, mSlots[slot].mBufferState.string());
309 return BAD_VALUE;
310 }
311
312 mSlots[slot].mBufferState.detachConsumer();
313 mCore->mActiveBuffers.erase(slot);
314 mCore->mFreeSlots.insert(slot);
315 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200316 mCore->mDequeueCondition.notify_all();
Pablo Ceballos38273792016-03-02 01:38:10 +0000317 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318
319 return NO_ERROR;
320}
321
322status_t BufferQueueConsumer::attachBuffer(int* outSlot,
323 const sp<android::GraphicBuffer>& buffer) {
324 ATRACE_CALL();
325
Yi Kong48a619f2018-06-05 16:34:59 -0700326 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700327 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800328 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700329 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700330 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800331 return BAD_VALUE;
332 }
333
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200334 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800335
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700336 if (mCore->mSharedBufferMode) {
337 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700338 return BAD_VALUE;
339 }
340
Dan Stoza0de7ea72015-04-23 13:20:51 -0700341 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800342 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800343 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700344 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800345 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800346 }
347 }
348
349 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700350 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800351 "(max %d)", numAcquiredBuffers,
352 mCore->mMaxAcquiredBufferCount);
353 return INVALID_OPERATION;
354 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700355
Dan Stoza812ed062015-06-02 15:45:22 -0700356 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
357 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
358 "[queue %u]", buffer->getGenerationNumber(),
359 mCore->mGenerationNumber);
360 return BAD_VALUE;
361 }
362
Dan Stoza0de7ea72015-04-23 13:20:51 -0700363 // Find a free slot to put the buffer into
364 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
365 if (!mCore->mFreeSlots.empty()) {
366 auto slot = mCore->mFreeSlots.begin();
367 found = *slot;
368 mCore->mFreeSlots.erase(slot);
369 } else if (!mCore->mFreeBuffers.empty()) {
370 found = mCore->mFreeBuffers.front();
371 mCore->mFreeBuffers.remove(found);
372 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700374 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800375 return NO_MEMORY;
376 }
377
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800378 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800379 *outSlot = found;
380 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700381 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382
383 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700384 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800385 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386 mSlots[*outSlot].mFence = Fence::NO_FENCE;
387 mSlots[*outSlot].mFrameNumber = 0;
388
Dan Stoza99b18b42014-03-28 15:34:33 -0700389 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
390 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
391 // Binder traffic by not un/flattening the GraphicBuffer. However, it
392 // requires that the consumer maintain a cached copy of the slot <--> buffer
393 // mappings, which is why the consumer doesn't need the valid pointer on
394 // acquire.
395 //
396 // The StreamSplitter is one of the primary users of the attach/detach
397 // logic, and while it is running, all buffers it acquires are immediately
398 // detached, and all buffers it eventually releases are ones that were
399 // attached (as opposed to having been obtained from acquireBuffer), so it
400 // doesn't make sense to maintain the slot/buffer mappings, which would
401 // become invalid for every buffer during detach/attach. By setting this to
402 // false, the valid GraphicBuffer pointer will always be sent with acquire
403 // for attached buffers.
404 mSlots[*outSlot].mAcquireCalled = false;
405
Pablo Ceballos9e314332016-01-12 13:49:19 -0800406 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700407
Dan Stoza9f3053d2014-03-06 15:14:33 -0800408 return NO_ERROR;
409}
410
Dan Stoza289ade12014-02-28 11:17:17 -0800411status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
412 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
413 EGLSyncKHR eglFence) {
414 ATRACE_CALL();
415 ATRACE_BUFFER_INDEX(slot);
416
Dan Stoza9f3053d2014-03-06 15:14:33 -0800417 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700418 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700419 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
420 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800421 return BAD_VALUE;
422 }
423
Dan Stozad1c10362014-03-28 15:19:08 -0700424 sp<IProducerListener> listener;
425 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200426 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800427
Dan Stozad1c10362014-03-28 15:19:08 -0700428 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700429 // we can ignore this releaseBuffer for the old buffer.
430 // Ignore this for the shared buffer where the frame number can easily
431 // get out of sync due to the buffer being queued and acquired at the
432 // same time.
433 if (frameNumber != mSlots[slot].mFrameNumber &&
434 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700435 return STALE_BUFFER_SLOT;
436 }
Dan Stoza289ade12014-02-28 11:17:17 -0800437
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800438 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700439 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700440 "but its state was %s", slot,
441 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800442 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800443 }
Dan Stoza289ade12014-02-28 11:17:17 -0800444
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800445 mSlots[slot].mEglDisplay = eglDisplay;
446 mSlots[slot].mEglFence = eglFence;
447 mSlots[slot].mFence = releaseFence;
448 mSlots[slot].mBufferState.release();
449
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700450 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800451 // still be around. Mark it as no longer shared if this
452 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700453 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800454 mSlots[slot].mBufferState.mShared = false;
455 }
456 // Don't put the shared buffer on the free list.
457 if (!mSlots[slot].mBufferState.isShared()) {
458 mCore->mActiveBuffers.erase(slot);
459 mCore->mFreeBuffers.push_back(slot);
460 }
461
462 listener = mCore->mConnectedProducerListener;
463 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
464
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200465 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800466 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700467 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800468
Dan Stozad1c10362014-03-28 15:19:08 -0700469 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700470 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700471 listener->onBufferReleased();
472 }
Dan Stoza289ade12014-02-28 11:17:17 -0800473
474 return NO_ERROR;
475}
476
477status_t BufferQueueConsumer::connect(
478 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
479 ATRACE_CALL();
480
Yi Kong48a619f2018-06-05 16:34:59 -0700481 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700482 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800483 return BAD_VALUE;
484 }
485
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700486 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800487 controlledByApp ? "true" : "false");
488
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200489 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800490
491 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700492 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800493 return NO_INIT;
494 }
495
496 mCore->mConsumerListener = consumerListener;
497 mCore->mConsumerControlledByApp = controlledByApp;
498
499 return NO_ERROR;
500}
501
502status_t BufferQueueConsumer::disconnect() {
503 ATRACE_CALL();
504
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700505 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800506
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200507 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800508
Yi Kong48a619f2018-06-05 16:34:59 -0700509 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700510 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800511 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800512 }
513
514 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700515 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800516 mCore->mQueue.clear();
517 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700518 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200519 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800520 return NO_ERROR;
521}
522
Dan Stozafebd4f42014-04-09 16:14:51 -0700523status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800524 ATRACE_CALL();
525
Yi Kong48a619f2018-06-05 16:34:59 -0700526 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800527 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
528 return BAD_VALUE;
529 }
530
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200531 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800532
533 if (mCore->mIsAbandoned) {
534 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
535 return NO_INIT;
536 }
537
Dan Stozafebd4f42014-04-09 16:14:51 -0700538 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800539 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800540 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700541 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800542 }
543 }
544
545 // Remove from the mask queued buffers for which acquire has been called,
546 // since the consumer will not receive their buffer addresses and so must
547 // retain their cached information
548 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
549 while (current != mCore->mQueue.end()) {
550 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700551 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800552 }
553 ++current;
554 }
555
Dan Stozafebd4f42014-04-09 16:14:51 -0700556 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800557 *outSlotMask = mask;
558 return NO_ERROR;
559}
560
561status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
562 uint32_t height) {
563 ATRACE_CALL();
564
565 if (width == 0 || height == 0) {
566 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
567 "height=%u)", width, height);
568 return BAD_VALUE;
569 }
570
571 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
572
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200573 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800574 mCore->mDefaultWidth = width;
575 mCore->mDefaultHeight = height;
576 return NO_ERROR;
577}
578
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700579status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800580 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800581
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700582 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
583 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
584 return BAD_VALUE;
585 }
Dan Stoza289ade12014-02-28 11:17:17 -0800586
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200587 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800588
Pablo Ceballos38273792016-03-02 01:38:10 +0000589 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
590 BQ_LOGE("setMaxBufferCount: producer is already connected");
591 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800592 }
593
Pablo Ceballos38273792016-03-02 01:38:10 +0000594 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
595 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
596 "mMaxAcquiredBufferCount (%d)", bufferCount,
597 mCore->mMaxAcquiredBufferCount);
598 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700599 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000600
601 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
602 mCore->mDequeueBufferCannotBlock, bufferCount) -
603 mCore->getMaxBufferCountLocked();
604 if (!mCore->adjustAvailableSlotsLocked(delta)) {
605 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
606 "available slots. Delta = %d", delta);
607 return BAD_VALUE;
608 }
609
610 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800611 return NO_ERROR;
612}
613
614status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
615 int maxAcquiredBuffers) {
616 ATRACE_CALL();
617
618 if (maxAcquiredBuffers < 1 ||
619 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
620 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
621 maxAcquiredBuffers);
622 return BAD_VALUE;
623 }
624
Pablo Ceballos38273792016-03-02 01:38:10 +0000625 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800626 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200627 std::unique_lock<std::mutex> lock(mCore->mMutex);
628 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800629
Pablo Ceballos72daab62015-12-07 16:38:43 -0800630 if (mCore->mIsAbandoned) {
631 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
632 return NO_INIT;
633 }
634
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700635 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
636 return NO_ERROR;
637 }
638
Pablo Ceballos72daab62015-12-07 16:38:43 -0800639 // The new maxAcquiredBuffers count should not be violated by the number
640 // of currently acquired buffers
641 int acquiredCount = 0;
642 for (int slot : mCore->mActiveBuffers) {
643 if (mSlots[slot].mBufferState.isAcquired()) {
644 acquiredCount++;
645 }
646 }
647 if (acquiredCount > maxAcquiredBuffers) {
648 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
649 "count (%d) exceeds the current acquired buffer count (%d)",
650 maxAcquiredBuffers, acquiredCount);
651 return BAD_VALUE;
652 }
653
654 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
655 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
656 > mCore->mMaxBufferCount) {
657 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
658 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
659 maxAcquiredBuffers, mCore->mMaxBufferCount,
660 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
661 mCore->mDequeueBufferCannotBlock);
662 return BAD_VALUE;
663 }
664
665 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000666 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800667 return BAD_VALUE;
668 }
669
670 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
671 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
672 VALIDATE_CONSISTENCY();
673 if (delta < 0) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000674 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800675 }
676 }
677 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700678 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000679 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800680 }
681
Dan Stoza289ade12014-02-28 11:17:17 -0800682 return NO_ERROR;
683}
684
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700685status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800686 ATRACE_CALL();
687 BQ_LOGV("setConsumerName: '%s'", name.string());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200688 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800689 mCore->mConsumerName = name;
690 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700691 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800692}
693
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800694status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800695 ATRACE_CALL();
696 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200697 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800698 mCore->mDefaultBufferFormat = defaultFormat;
699 return NO_ERROR;
700}
701
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800702status_t BufferQueueConsumer::setDefaultBufferDataSpace(
703 android_dataspace defaultDataSpace) {
704 ATRACE_CALL();
705 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200706 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800707 mCore->mDefaultBufferDataSpace = defaultDataSpace;
708 return NO_ERROR;
709}
710
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700711status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800712 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700713 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200714 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800715 mCore->mConsumerUsageBits = usage;
716 return NO_ERROR;
717}
718
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700719status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
720 ATRACE_CALL();
721 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200722 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700723 mCore->mConsumerIsProtected = isProtected;
724 return NO_ERROR;
725}
726
Dan Stoza289ade12014-02-28 11:17:17 -0800727status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
728 ATRACE_CALL();
729 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200730 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800731 mCore->mTransformHint = hint;
732 return NO_ERROR;
733}
734
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700735status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200736 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700737 *outStream = mCore->mSidebandStream;
738 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800739}
740
Dan Stozae77c7662016-05-13 11:37:28 -0700741status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
742 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200743 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozae77c7662016-05-13 11:37:28 -0700744 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
745 return NO_ERROR;
746}
747
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700748status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200749 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700750 mCore->discardFreeBuffersLocked();
751 return NO_ERROR;
752}
753
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700754status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700755 struct passwd* pwd = getpwnam("shell");
756 uid_t shellUid = pwd ? pwd->pw_uid : 0;
757 if (!shellUid) {
758 int savedErrno = errno;
759 BQ_LOGE("Cannot get AID_SHELL");
760 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
761 }
762
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800763 bool denied = false;
764 const uid_t uid = BufferQueueThreadState::getCallingUid();
Jiyong Park47f876b2018-04-17 13:56:46 +0900765#ifndef __ANDROID_VNDK__
766 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800767 // the PermissionController. We need to do a runtime check as well, since
768 // the system variant of libgui can be loaded in a vendor process. For eg:
769 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
770 if (!android_is_in_vendor_process()) {
771 const pid_t pid = BufferQueueThreadState::getCallingPid();
772 if ((uid != shellUid) &&
773 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
774 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
775 "from pid=%d, uid=%d\n",
776 pid, uid);
777 denied = true;
778 }
779 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900780#else
781 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800782 denied = true;
783 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900784#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800785 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700786 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700787 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700788 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800789 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700790
791 mCore->dumpState(prefix, outResult);
792 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800793}
794
795} // namespace android