blob: 528bfb194e702ab74a92efa18bf050ee3a2c44c4 [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 // The 'expectedPresent' argument indicates when the buffer is expected
98 // to be presented on-screen. If the buffer's desired present time is
99 // earlier (less) than expectedPresent -- meaning it will be displayed
100 // on time or possibly late if we show it as soon as possible -- we
101 // acquire and return it. If we don't want to display it until after the
102 // expectedPresent time, we return PRESENT_LATER without acquiring it.
103 //
104 // To be safe, we don't defer acquisition if expectedPresent is more
105 // than one second in the future beyond the desired present time
106 // (i.e., we'd be holding the buffer for a long time).
107 //
108 // NOTE: Code assumes monotonic time values from the system clock
109 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800110
Lajos Molnar5f920c12015-07-13 16:04:24 -0700111 // Start by checking to see if we can drop frames. We skip this check if
112 // the timestamps are being auto-generated by Surface. If the app isn't
113 // generating timestamps explicitly, it probably doesn't want frames to
114 // be discarded based on them.
115 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
116 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700117
Lajos Molnar5f920c12015-07-13 16:04:24 -0700118 // If dropping entry[0] would leave us with a buffer that the
119 // consumer is not yet ready for, don't drop it.
120 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
121 break;
122 }
123
124 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
125 // additional criterion here: we only drop the earlier buffer if our
126 // desiredPresent falls within +/- 1 second of the expected present.
127 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
128 // relative timestamp), which normally mean "ignore the timestamp
129 // and acquire immediately", would cause us to drop frames.
130 //
131 // We may want to add an additional criterion: don't drop the
132 // earlier buffer if entry[1]'s fence hasn't signaled yet.
133 nsecs_t desiredPresent = bufferItem.mTimestamp;
134 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
135 desiredPresent > expectedPresent) {
136 // This buffer is set to display in the near future, or
137 // desiredPresent is garbage. Either way we don't want to drop
138 // the previous buffer just to get this on the screen sooner.
139 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
140 PRId64 " (%" PRId64 ") now=%" PRId64,
141 desiredPresent, expectedPresent,
142 desiredPresent - expectedPresent,
143 systemTime(CLOCK_MONOTONIC));
144 break;
145 }
146
147 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
148 " size=%zu",
149 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800150
151 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700152 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700153 mSlots[front->mSlot].mBufferState.freeQueued();
154
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700155 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700156 // still be around. Mark it as no longer shared if this
157 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700158 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700159 mSlots[front->mSlot].mBufferState.isFree()) {
160 mSlots[front->mSlot].mBufferState.mShared = false;
161 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800162
163 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700164 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800165 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700166 mCore->mFreeBuffers.push_back(front->mSlot);
167 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800168
Lajos Molnar5f920c12015-07-13 16:04:24 -0700169 listener = mCore->mConnectedProducerListener;
170 ++numDroppedBuffers;
171 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800172
Lajos Molnar5f920c12015-07-13 16:04:24 -0700173 mCore->mQueue.erase(front);
174 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700175 }
176
Lajos Molnar5f920c12015-07-13 16:04:24 -0700177 // See if the front buffer is ready to be acquired
178 nsecs_t desiredPresent = front->mTimestamp;
179 bool bufferIsDue = desiredPresent <= expectedPresent ||
180 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
181 bool consumerIsReady = maxFrameNumber > 0 ?
182 front->mFrameNumber <= maxFrameNumber : true;
183 if (!bufferIsDue || !consumerIsReady) {
184 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
185 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
186 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700187 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800188 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700189 systemTime(CLOCK_MONOTONIC),
190 front->mFrameNumber, maxFrameNumber);
Ady Abraham09bd3922019-04-08 10:44:56 -0700191 ATRACE_NAME("PRESENT_LATER");
Lajos Molnar5f920c12015-07-13 16:04:24 -0700192 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800193 }
194
Lajos Molnar5f920c12015-07-13 16:04:24 -0700195 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
196 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800197 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700198 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800199 }
200
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700201 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
202
203 if (sharedBufferAvailable && mCore->mQueue.empty()) {
204 // make sure the buffer has finished allocating before acquiring it
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200205 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700206
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700207 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700208
209 // Recreate the BufferItem for the shared buffer from the data that
210 // was cached when it was last queued.
211 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
212 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700213 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700214 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
215 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700216 ~static_cast<uint32_t>(
217 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700218 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
219 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700220 outBuffer->mFrameNumber = mCore->mFrameCounter;
221 outBuffer->mSlot = slot;
222 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
223 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700224 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700225 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
226 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700227 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800228 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700229 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800230 mCore->mAutoRefresh;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700231 } else {
232 slot = front->mSlot;
233 *outBuffer = *front;
234 }
235
Lajos Molnar5f920c12015-07-13 16:04:24 -0700236 ATRACE_BUFFER_INDEX(slot);
237
238 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700239 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800240
241 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700242 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700243 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700244 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700245 // the queue is empty and the BufferItem is created above.
246 if (mCore->mQueue.empty()) {
247 mSlots[slot].mBufferState.acquireNotInQueue();
248 } else {
249 mSlots[slot].mBufferState.acquire();
250 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700251 mSlots[slot].mFence = Fence::NO_FENCE;
252 }
253
254 // If the buffer has previously been acquired by the consumer, set
255 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
256 // on the consumer side
257 if (outBuffer->mAcquireCalled) {
Yi Kong48a619f2018-06-05 16:34:59 -0700258 outBuffer->mGraphicBuffer = nullptr;
Lajos Molnar5f920c12015-07-13 16:04:24 -0700259 }
260
261 mCore->mQueue.erase(front);
262
263 // We might have freed a slot while dropping old buffers, or the producer
264 // may be blocked waiting for the number of buffers in the queue to
265 // decrease.
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200266 mCore->mDequeueCondition.notify_all();
Lajos Molnar5f920c12015-07-13 16:04:24 -0700267
Colin Cross6e7e2b42016-09-27 14:08:19 -0700268 ATRACE_INT(mCore->mConsumerName.string(),
269 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700270 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Lajos Molnar5f920c12015-07-13 16:04:24 -0700271
Pablo Ceballos9e314332016-01-12 13:49:19 -0800272 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800273 }
274
Yi Kong48a619f2018-06-05 16:34:59 -0700275 if (listener != nullptr) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700276 for (int i = 0; i < numDroppedBuffers; ++i) {
277 listener->onBufferReleased();
278 }
Dan Stoza289ade12014-02-28 11:17:17 -0800279 }
280
Dan Stoza289ade12014-02-28 11:17:17 -0800281 return NO_ERROR;
282}
283
Dan Stoza9f3053d2014-03-06 15:14:33 -0800284status_t BufferQueueConsumer::detachBuffer(int slot) {
285 ATRACE_CALL();
286 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700287 BQ_LOGV("detachBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200288 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800289
Pablo Ceballos38273792016-03-02 01:38:10 +0000290 if (mCore->mIsAbandoned) {
291 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
292 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 }
294
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700295 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
296 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000297 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800298 }
299
Pablo Ceballos38273792016-03-02 01:38:10 +0000300 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
301 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
302 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
303 return BAD_VALUE;
304 } else if (!mSlots[slot].mBufferState.isAcquired()) {
305 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
306 "(state = %s)", slot, mSlots[slot].mBufferState.string());
307 return BAD_VALUE;
308 }
309
310 mSlots[slot].mBufferState.detachConsumer();
311 mCore->mActiveBuffers.erase(slot);
312 mCore->mFreeSlots.insert(slot);
313 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200314 mCore->mDequeueCondition.notify_all();
Pablo Ceballos38273792016-03-02 01:38:10 +0000315 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316
317 return NO_ERROR;
318}
319
320status_t BufferQueueConsumer::attachBuffer(int* outSlot,
321 const sp<android::GraphicBuffer>& buffer) {
322 ATRACE_CALL();
323
Yi Kong48a619f2018-06-05 16:34:59 -0700324 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700325 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700327 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700328 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800329 return BAD_VALUE;
330 }
331
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200332 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800333
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 if (mCore->mSharedBufferMode) {
335 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700336 return BAD_VALUE;
337 }
338
Dan Stoza0de7ea72015-04-23 13:20:51 -0700339 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800340 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800341 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700342 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800343 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800344 }
345 }
346
347 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700348 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800349 "(max %d)", numAcquiredBuffers,
350 mCore->mMaxAcquiredBufferCount);
351 return INVALID_OPERATION;
352 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700353
Dan Stoza812ed062015-06-02 15:45:22 -0700354 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
355 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
356 "[queue %u]", buffer->getGenerationNumber(),
357 mCore->mGenerationNumber);
358 return BAD_VALUE;
359 }
360
Dan Stoza0de7ea72015-04-23 13:20:51 -0700361 // Find a free slot to put the buffer into
362 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
363 if (!mCore->mFreeSlots.empty()) {
364 auto slot = mCore->mFreeSlots.begin();
365 found = *slot;
366 mCore->mFreeSlots.erase(slot);
367 } else if (!mCore->mFreeBuffers.empty()) {
368 found = mCore->mFreeBuffers.front();
369 mCore->mFreeBuffers.remove(found);
370 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800371 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700372 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 return NO_MEMORY;
374 }
375
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800376 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 *outSlot = found;
378 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700379 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380
381 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700382 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800383 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800384 mSlots[*outSlot].mFence = Fence::NO_FENCE;
385 mSlots[*outSlot].mFrameNumber = 0;
386
Dan Stoza99b18b42014-03-28 15:34:33 -0700387 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
388 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
389 // Binder traffic by not un/flattening the GraphicBuffer. However, it
390 // requires that the consumer maintain a cached copy of the slot <--> buffer
391 // mappings, which is why the consumer doesn't need the valid pointer on
392 // acquire.
393 //
394 // The StreamSplitter is one of the primary users of the attach/detach
395 // logic, and while it is running, all buffers it acquires are immediately
396 // detached, and all buffers it eventually releases are ones that were
397 // attached (as opposed to having been obtained from acquireBuffer), so it
398 // doesn't make sense to maintain the slot/buffer mappings, which would
399 // become invalid for every buffer during detach/attach. By setting this to
400 // false, the valid GraphicBuffer pointer will always be sent with acquire
401 // for attached buffers.
402 mSlots[*outSlot].mAcquireCalled = false;
403
Pablo Ceballos9e314332016-01-12 13:49:19 -0800404 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700405
Dan Stoza9f3053d2014-03-06 15:14:33 -0800406 return NO_ERROR;
407}
408
Dan Stoza289ade12014-02-28 11:17:17 -0800409status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
410 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
411 EGLSyncKHR eglFence) {
412 ATRACE_CALL();
413 ATRACE_BUFFER_INDEX(slot);
414
Dan Stoza9f3053d2014-03-06 15:14:33 -0800415 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
Yi Kong48a619f2018-06-05 16:34:59 -0700416 releaseFence == nullptr) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700417 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
418 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800419 return BAD_VALUE;
420 }
421
Dan Stozad1c10362014-03-28 15:19:08 -0700422 sp<IProducerListener> listener;
423 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200424 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800425
Dan Stozad1c10362014-03-28 15:19:08 -0700426 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700427 // we can ignore this releaseBuffer for the old buffer.
428 // Ignore this for the shared buffer where the frame number can easily
429 // get out of sync due to the buffer being queued and acquired at the
430 // same time.
431 if (frameNumber != mSlots[slot].mFrameNumber &&
432 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700433 return STALE_BUFFER_SLOT;
434 }
Dan Stoza289ade12014-02-28 11:17:17 -0800435
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800436 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700437 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700438 "but its state was %s", slot,
439 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800440 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800441 }
Dan Stoza289ade12014-02-28 11:17:17 -0800442
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800443 mSlots[slot].mEglDisplay = eglDisplay;
444 mSlots[slot].mEglFence = eglFence;
445 mSlots[slot].mFence = releaseFence;
446 mSlots[slot].mBufferState.release();
447
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700448 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800449 // still be around. Mark it as no longer shared if this
450 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700451 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800452 mSlots[slot].mBufferState.mShared = false;
453 }
454 // Don't put the shared buffer on the free list.
455 if (!mSlots[slot].mBufferState.isShared()) {
456 mCore->mActiveBuffers.erase(slot);
457 mCore->mFreeBuffers.push_back(slot);
458 }
459
460 listener = mCore->mConnectedProducerListener;
461 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
462
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200463 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800464 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700465 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800466
Dan Stozad1c10362014-03-28 15:19:08 -0700467 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700468 if (listener != nullptr) {
Dan Stozad1c10362014-03-28 15:19:08 -0700469 listener->onBufferReleased();
470 }
Dan Stoza289ade12014-02-28 11:17:17 -0800471
472 return NO_ERROR;
473}
474
475status_t BufferQueueConsumer::connect(
476 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
477 ATRACE_CALL();
478
Yi Kong48a619f2018-06-05 16:34:59 -0700479 if (consumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700480 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800481 return BAD_VALUE;
482 }
483
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700484 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800485 controlledByApp ? "true" : "false");
486
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200487 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800488
489 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700490 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800491 return NO_INIT;
492 }
493
494 mCore->mConsumerListener = consumerListener;
495 mCore->mConsumerControlledByApp = controlledByApp;
496
497 return NO_ERROR;
498}
499
500status_t BufferQueueConsumer::disconnect() {
501 ATRACE_CALL();
502
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700503 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800504
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200505 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800506
Yi Kong48a619f2018-06-05 16:34:59 -0700507 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700508 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800509 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800510 }
511
512 mCore->mIsAbandoned = true;
Yi Kong48a619f2018-06-05 16:34:59 -0700513 mCore->mConsumerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800514 mCore->mQueue.clear();
515 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700516 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200517 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800518 return NO_ERROR;
519}
520
Dan Stozafebd4f42014-04-09 16:14:51 -0700521status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800522 ATRACE_CALL();
523
Yi Kong48a619f2018-06-05 16:34:59 -0700524 if (outSlotMask == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800525 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
526 return BAD_VALUE;
527 }
528
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200529 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800530
531 if (mCore->mIsAbandoned) {
532 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
533 return NO_INIT;
534 }
535
Dan Stozafebd4f42014-04-09 16:14:51 -0700536 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800537 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800538 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700539 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800540 }
541 }
542
543 // Remove from the mask queued buffers for which acquire has been called,
544 // since the consumer will not receive their buffer addresses and so must
545 // retain their cached information
546 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
547 while (current != mCore->mQueue.end()) {
548 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700549 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800550 }
551 ++current;
552 }
553
Dan Stozafebd4f42014-04-09 16:14:51 -0700554 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800555 *outSlotMask = mask;
556 return NO_ERROR;
557}
558
559status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
560 uint32_t height) {
561 ATRACE_CALL();
562
563 if (width == 0 || height == 0) {
564 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
565 "height=%u)", width, height);
566 return BAD_VALUE;
567 }
568
569 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
570
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200571 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800572 mCore->mDefaultWidth = width;
573 mCore->mDefaultHeight = height;
574 return NO_ERROR;
575}
576
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700577status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800578 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800579
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700580 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
581 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
582 return BAD_VALUE;
583 }
Dan Stoza289ade12014-02-28 11:17:17 -0800584
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200585 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800586
Pablo Ceballos38273792016-03-02 01:38:10 +0000587 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
588 BQ_LOGE("setMaxBufferCount: producer is already connected");
589 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800590 }
591
Pablo Ceballos38273792016-03-02 01:38:10 +0000592 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
593 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
594 "mMaxAcquiredBufferCount (%d)", bufferCount,
595 mCore->mMaxAcquiredBufferCount);
596 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700597 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000598
599 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
600 mCore->mDequeueBufferCannotBlock, bufferCount) -
601 mCore->getMaxBufferCountLocked();
602 if (!mCore->adjustAvailableSlotsLocked(delta)) {
603 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
604 "available slots. Delta = %d", delta);
605 return BAD_VALUE;
606 }
607
608 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800609 return NO_ERROR;
610}
611
612status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
613 int maxAcquiredBuffers) {
614 ATRACE_CALL();
615
616 if (maxAcquiredBuffers < 1 ||
617 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
618 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
619 maxAcquiredBuffers);
620 return BAD_VALUE;
621 }
622
Pablo Ceballos38273792016-03-02 01:38:10 +0000623 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800624 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200625 std::unique_lock<std::mutex> lock(mCore->mMutex);
626 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -0800627
Pablo Ceballos72daab62015-12-07 16:38:43 -0800628 if (mCore->mIsAbandoned) {
629 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
630 return NO_INIT;
631 }
632
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700633 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
634 return NO_ERROR;
635 }
636
Pablo Ceballos72daab62015-12-07 16:38:43 -0800637 // The new maxAcquiredBuffers count should not be violated by the number
638 // of currently acquired buffers
639 int acquiredCount = 0;
640 for (int slot : mCore->mActiveBuffers) {
641 if (mSlots[slot].mBufferState.isAcquired()) {
642 acquiredCount++;
643 }
644 }
645 if (acquiredCount > maxAcquiredBuffers) {
646 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
647 "count (%d) exceeds the current acquired buffer count (%d)",
648 maxAcquiredBuffers, acquiredCount);
649 return BAD_VALUE;
650 }
651
652 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
653 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
654 > mCore->mMaxBufferCount) {
655 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
656 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
657 maxAcquiredBuffers, mCore->mMaxBufferCount,
658 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
659 mCore->mDequeueBufferCannotBlock);
660 return BAD_VALUE;
661 }
662
663 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000664 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800665 return BAD_VALUE;
666 }
667
668 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
669 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
670 VALIDATE_CONSISTENCY();
671 if (delta < 0) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000672 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800673 }
674 }
675 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700676 if (listener != nullptr) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000677 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800678 }
679
Dan Stoza289ade12014-02-28 11:17:17 -0800680 return NO_ERROR;
681}
682
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700683status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800684 ATRACE_CALL();
685 BQ_LOGV("setConsumerName: '%s'", name.string());
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200686 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800687 mCore->mConsumerName = name;
688 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700689 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800690}
691
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800692status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800693 ATRACE_CALL();
694 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200695 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800696 mCore->mDefaultBufferFormat = defaultFormat;
697 return NO_ERROR;
698}
699
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800700status_t BufferQueueConsumer::setDefaultBufferDataSpace(
701 android_dataspace defaultDataSpace) {
702 ATRACE_CALL();
703 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200704 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800705 mCore->mDefaultBufferDataSpace = defaultDataSpace;
706 return NO_ERROR;
707}
708
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700709status_t BufferQueueConsumer::setConsumerUsageBits(uint64_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800710 ATRACE_CALL();
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700711 BQ_LOGV("setConsumerUsageBits: %#" PRIx64, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200712 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800713 mCore->mConsumerUsageBits = usage;
714 return NO_ERROR;
715}
716
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700717status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
718 ATRACE_CALL();
719 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200720 std::lock_guard<std::mutex> lock(mCore->mMutex);
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700721 mCore->mConsumerIsProtected = isProtected;
722 return NO_ERROR;
723}
724
Dan Stoza289ade12014-02-28 11:17:17 -0800725status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
726 ATRACE_CALL();
727 BQ_LOGV("setTransformHint: %#x", hint);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200728 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800729 mCore->mTransformHint = hint;
730 return NO_ERROR;
731}
732
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700733status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200734 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700735 *outStream = mCore->mSidebandStream;
736 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800737}
738
Dan Stozae77c7662016-05-13 11:37:28 -0700739status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
740 std::vector<OccupancyTracker::Segment>* outHistory) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200741 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozae77c7662016-05-13 11:37:28 -0700742 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
743 return NO_ERROR;
744}
745
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700746status_t BufferQueueConsumer::discardFreeBuffers() {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200747 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700748 mCore->discardFreeBuffersLocked();
749 return NO_ERROR;
750}
751
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700752status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hong65799c32017-07-26 10:47:14 -0700753 struct passwd* pwd = getpwnam("shell");
754 uid_t shellUid = pwd ? pwd->pw_uid : 0;
755 if (!shellUid) {
756 int savedErrno = errno;
757 BQ_LOGE("Cannot get AID_SHELL");
758 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
759 }
760
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800761 bool denied = false;
762 const uid_t uid = BufferQueueThreadState::getCallingUid();
Jiyong Park47f876b2018-04-17 13:56:46 +0900763#ifndef __ANDROID_VNDK__
764 // permission check can't be done for vendors as vendors have no access to
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800765 // the PermissionController. We need to do a runtime check as well, since
766 // the system variant of libgui can be loaded in a vendor process. For eg:
767 // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
768 if (!android_is_in_vendor_process()) {
769 const pid_t pid = BufferQueueThreadState::getCallingPid();
770 if ((uid != shellUid) &&
771 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
772 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
773 "from pid=%d, uid=%d\n",
774 pid, uid);
775 denied = true;
776 }
777 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900778#else
779 if (uid != shellUid) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800780 denied = true;
781 }
Jiyong Park47f876b2018-04-17 13:56:46 +0900782#endif
Jayant Chowdharyad9fe272019-03-07 22:36:06 -0800783 if (denied) {
Colin Cross6e7e2b42016-09-27 14:08:19 -0700784 android_errorWriteWithInfoLog(0x534e4554, "27046057",
Yi Kong48a619f2018-06-05 16:34:59 -0700785 static_cast<int32_t>(uid), nullptr, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700786 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800787 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700788
789 mCore->dumpState(prefix, outResult);
790 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800791}
792
793} // namespace android