blob: 17ff24983cb9d3f9f6684fc8788c83d57cb996df [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 Hongfc8d8852017-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
Pablo Ceballos88f69282016-02-11 18:01:49 -080037#include <binder/IPCThreadState.h>
38#include <binder/PermissionCache.h>
Pablo Ceballos88f69282016-02-11 18:01:49 -080039
Dan Stoza289ade12014-02-28 11:17:17 -080040namespace android {
41
42BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
43 mCore(core),
44 mSlots(core->mSlots),
45 mConsumerName() {}
46
47BufferQueueConsumer::~BufferQueueConsumer() {}
48
49status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070050 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080051 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080052
Lajos Molnar5f920c12015-07-13 16:04:24 -070053 int numDroppedBuffers = 0;
54 sp<IProducerListener> listener;
55 {
56 Mutex::Autolock lock(mCore->mMutex);
57
58 // Check that the consumer doesn't currently have the maximum number of
59 // buffers acquired. We allow the max buffer count to be exceeded by one
60 // buffer so that the consumer can successfully set up the newly acquired
61 // buffer before releasing the old one.
62 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080063 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070064 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070065 ++numAcquiredBuffers;
66 }
Dan Stoza289ade12014-02-28 11:17:17 -080067 }
Lajos Molnar5f920c12015-07-13 16:04:24 -070068 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
69 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
70 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
71 return INVALID_OPERATION;
72 }
Dan Stoza289ade12014-02-28 11:17:17 -080073
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070074 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
75 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -070076 BufferQueueCore::INVALID_BUFFER_SLOT;
77
Lajos Molnar5f920c12015-07-13 16:04:24 -070078 // In asynchronous mode the list is guaranteed to be one buffer deep,
79 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070080 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070081 return NO_BUFFER_AVAILABLE;
82 }
Dan Stoza289ade12014-02-28 11:17:17 -080083
Lajos Molnar5f920c12015-07-13 16:04:24 -070084 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080085
Lajos Molnar5f920c12015-07-13 16:04:24 -070086 // If expectedPresent is specified, we may not want to return a buffer yet.
87 // If it's specified and there's more than one buffer queued, we may want
88 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070089 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -070090 // since in that case we'll just return the shared buffer.
91 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070092 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080093
Lajos Molnar5f920c12015-07-13 16:04:24 -070094 // The 'expectedPresent' argument indicates when the buffer is expected
95 // to be presented on-screen. If the buffer's desired present time is
96 // earlier (less) than expectedPresent -- meaning it will be displayed
97 // on time or possibly late if we show it as soon as possible -- we
98 // acquire and return it. If we don't want to display it until after the
99 // expectedPresent time, we return PRESENT_LATER without acquiring it.
100 //
101 // To be safe, we don't defer acquisition if expectedPresent is more
102 // than one second in the future beyond the desired present time
103 // (i.e., we'd be holding the buffer for a long time).
104 //
105 // NOTE: Code assumes monotonic time values from the system clock
106 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800107
Lajos Molnar5f920c12015-07-13 16:04:24 -0700108 // Start by checking to see if we can drop frames. We skip this check if
109 // the timestamps are being auto-generated by Surface. If the app isn't
110 // generating timestamps explicitly, it probably doesn't want frames to
111 // be discarded based on them.
112 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
113 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700114
Lajos Molnar5f920c12015-07-13 16:04:24 -0700115 // If dropping entry[0] would leave us with a buffer that the
116 // consumer is not yet ready for, don't drop it.
117 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
118 break;
119 }
120
121 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
122 // additional criterion here: we only drop the earlier buffer if our
123 // desiredPresent falls within +/- 1 second of the expected present.
124 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
125 // relative timestamp), which normally mean "ignore the timestamp
126 // and acquire immediately", would cause us to drop frames.
127 //
128 // We may want to add an additional criterion: don't drop the
129 // earlier buffer if entry[1]'s fence hasn't signaled yet.
130 nsecs_t desiredPresent = bufferItem.mTimestamp;
131 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
132 desiredPresent > expectedPresent) {
133 // This buffer is set to display in the near future, or
134 // desiredPresent is garbage. Either way we don't want to drop
135 // the previous buffer just to get this on the screen sooner.
136 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
137 PRId64 " (%" PRId64 ") now=%" PRId64,
138 desiredPresent, expectedPresent,
139 desiredPresent - expectedPresent,
140 systemTime(CLOCK_MONOTONIC));
141 break;
142 }
143
144 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
145 " size=%zu",
146 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800147
148 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700149 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700150 mSlots[front->mSlot].mBufferState.freeQueued();
151
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700152 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700153 // still be around. Mark it as no longer shared if this
154 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700155 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700156 mSlots[front->mSlot].mBufferState.isFree()) {
157 mSlots[front->mSlot].mBufferState.mShared = false;
158 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800159
160 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700161 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800162 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700163 mCore->mFreeBuffers.push_back(front->mSlot);
164 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800165
Lajos Molnar5f920c12015-07-13 16:04:24 -0700166 listener = mCore->mConnectedProducerListener;
167 ++numDroppedBuffers;
168 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800169
Lajos Molnar5f920c12015-07-13 16:04:24 -0700170 mCore->mQueue.erase(front);
171 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700172 }
173
Lajos Molnar5f920c12015-07-13 16:04:24 -0700174 // See if the front buffer is ready to be acquired
175 nsecs_t desiredPresent = front->mTimestamp;
176 bool bufferIsDue = desiredPresent <= expectedPresent ||
177 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
178 bool consumerIsReady = maxFrameNumber > 0 ?
179 front->mFrameNumber <= maxFrameNumber : true;
180 if (!bufferIsDue || !consumerIsReady) {
181 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
182 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
183 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700184 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800185 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700186 systemTime(CLOCK_MONOTONIC),
187 front->mFrameNumber, maxFrameNumber);
188 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800189 }
190
Lajos Molnar5f920c12015-07-13 16:04:24 -0700191 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
192 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800193 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700194 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800195 }
196
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700197 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
198
199 if (sharedBufferAvailable && mCore->mQueue.empty()) {
200 // make sure the buffer has finished allocating before acquiring it
201 mCore->waitWhileAllocatingLocked();
202
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700203 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700204
205 // Recreate the BufferItem for the shared buffer from the data that
206 // was cached when it was last queued.
207 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
208 outBuffer->mFence = Fence::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700209 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
210 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700211 ~static_cast<uint32_t>(
212 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700213 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
214 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700215 outBuffer->mFrameNumber = mCore->mFrameCounter;
216 outBuffer->mSlot = slot;
217 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
218 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700219 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700220 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
221 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700222 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800223 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700224 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800225 mCore->mAutoRefresh;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700226 } else {
227 slot = front->mSlot;
228 *outBuffer = *front;
229 }
230
Lajos Molnar5f920c12015-07-13 16:04:24 -0700231 ATRACE_BUFFER_INDEX(slot);
232
233 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700234 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800235
236 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700237 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700238 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700239 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700240 // the queue is empty and the BufferItem is created above.
241 if (mCore->mQueue.empty()) {
242 mSlots[slot].mBufferState.acquireNotInQueue();
243 } else {
244 mSlots[slot].mBufferState.acquire();
245 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700246 mSlots[slot].mFence = Fence::NO_FENCE;
247 }
248
249 // If the buffer has previously been acquired by the consumer, set
250 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
251 // on the consumer side
252 if (outBuffer->mAcquireCalled) {
253 outBuffer->mGraphicBuffer = NULL;
254 }
255
256 mCore->mQueue.erase(front);
257
258 // We might have freed a slot while dropping old buffers, or the producer
259 // may be blocked waiting for the number of buffers in the queue to
260 // decrease.
261 mCore->mDequeueCondition.broadcast();
262
Colin Cross6e7e2b42016-09-27 14:08:19 -0700263 ATRACE_INT(mCore->mConsumerName.string(),
264 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700265 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Lajos Molnar5f920c12015-07-13 16:04:24 -0700266
Pablo Ceballos9e314332016-01-12 13:49:19 -0800267 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800268 }
269
Lajos Molnar5f920c12015-07-13 16:04:24 -0700270 if (listener != NULL) {
271 for (int i = 0; i < numDroppedBuffers; ++i) {
272 listener->onBufferReleased();
273 }
Dan Stoza289ade12014-02-28 11:17:17 -0800274 }
275
Dan Stoza289ade12014-02-28 11:17:17 -0800276 return NO_ERROR;
277}
278
Dan Stoza9f3053d2014-03-06 15:14:33 -0800279status_t BufferQueueConsumer::detachBuffer(int slot) {
280 ATRACE_CALL();
281 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700282 BQ_LOGV("detachBuffer: slot %d", slot);
Pablo Ceballos38273792016-03-02 01:38:10 +0000283 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800284
Pablo Ceballos38273792016-03-02 01:38:10 +0000285 if (mCore->mIsAbandoned) {
286 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
287 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800288 }
289
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700290 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
291 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000292 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 }
294
Pablo Ceballos38273792016-03-02 01:38:10 +0000295 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
296 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
297 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
298 return BAD_VALUE;
299 } else if (!mSlots[slot].mBufferState.isAcquired()) {
300 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
301 "(state = %s)", slot, mSlots[slot].mBufferState.string());
302 return BAD_VALUE;
303 }
304
305 mSlots[slot].mBufferState.detachConsumer();
306 mCore->mActiveBuffers.erase(slot);
307 mCore->mFreeSlots.insert(slot);
308 mCore->clearBufferSlotLocked(slot);
309 mCore->mDequeueCondition.broadcast();
310 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800311
312 return NO_ERROR;
313}
314
315status_t BufferQueueConsumer::attachBuffer(int* outSlot,
316 const sp<android::GraphicBuffer>& buffer) {
317 ATRACE_CALL();
318
319 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700320 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800321 return BAD_VALUE;
322 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700323 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324 return BAD_VALUE;
325 }
326
327 Mutex::Autolock lock(mCore->mMutex);
328
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700329 if (mCore->mSharedBufferMode) {
330 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700331 return BAD_VALUE;
332 }
333
Dan Stoza0de7ea72015-04-23 13:20:51 -0700334 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800335 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800336 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700337 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800338 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800339 }
340 }
341
342 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700343 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800344 "(max %d)", numAcquiredBuffers,
345 mCore->mMaxAcquiredBufferCount);
346 return INVALID_OPERATION;
347 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700348
Dan Stoza812ed062015-06-02 15:45:22 -0700349 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
350 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
351 "[queue %u]", buffer->getGenerationNumber(),
352 mCore->mGenerationNumber);
353 return BAD_VALUE;
354 }
355
Dan Stoza0de7ea72015-04-23 13:20:51 -0700356 // Find a free slot to put the buffer into
357 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
358 if (!mCore->mFreeSlots.empty()) {
359 auto slot = mCore->mFreeSlots.begin();
360 found = *slot;
361 mCore->mFreeSlots.erase(slot);
362 } else if (!mCore->mFreeBuffers.empty()) {
363 found = mCore->mFreeBuffers.front();
364 mCore->mFreeBuffers.remove(found);
365 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800366 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700367 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 return NO_MEMORY;
369 }
370
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800371 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 *outSlot = found;
373 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700374 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800375
376 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700377 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800378 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800379 mSlots[*outSlot].mFence = Fence::NO_FENCE;
380 mSlots[*outSlot].mFrameNumber = 0;
381
Dan Stoza99b18b42014-03-28 15:34:33 -0700382 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
383 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
384 // Binder traffic by not un/flattening the GraphicBuffer. However, it
385 // requires that the consumer maintain a cached copy of the slot <--> buffer
386 // mappings, which is why the consumer doesn't need the valid pointer on
387 // acquire.
388 //
389 // The StreamSplitter is one of the primary users of the attach/detach
390 // logic, and while it is running, all buffers it acquires are immediately
391 // detached, and all buffers it eventually releases are ones that were
392 // attached (as opposed to having been obtained from acquireBuffer), so it
393 // doesn't make sense to maintain the slot/buffer mappings, which would
394 // become invalid for every buffer during detach/attach. By setting this to
395 // false, the valid GraphicBuffer pointer will always be sent with acquire
396 // for attached buffers.
397 mSlots[*outSlot].mAcquireCalled = false;
398
Pablo Ceballos9e314332016-01-12 13:49:19 -0800399 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700400
Dan Stoza9f3053d2014-03-06 15:14:33 -0800401 return NO_ERROR;
402}
403
Dan Stoza289ade12014-02-28 11:17:17 -0800404status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
405 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
406 EGLSyncKHR eglFence) {
407 ATRACE_CALL();
408 ATRACE_BUFFER_INDEX(slot);
409
Dan Stoza9f3053d2014-03-06 15:14:33 -0800410 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
411 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700412 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
413 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800414 return BAD_VALUE;
415 }
416
Dan Stozad1c10362014-03-28 15:19:08 -0700417 sp<IProducerListener> listener;
418 { // Autolock scope
419 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800420
Dan Stozad1c10362014-03-28 15:19:08 -0700421 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700422 // we can ignore this releaseBuffer for the old buffer.
423 // Ignore this for the shared buffer where the frame number can easily
424 // get out of sync due to the buffer being queued and acquired at the
425 // same time.
426 if (frameNumber != mSlots[slot].mFrameNumber &&
427 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700428 return STALE_BUFFER_SLOT;
429 }
Dan Stoza289ade12014-02-28 11:17:17 -0800430
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800431 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700432 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700433 "but its state was %s", slot,
434 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800435 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800436 }
Dan Stoza289ade12014-02-28 11:17:17 -0800437
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800438 mSlots[slot].mEglDisplay = eglDisplay;
439 mSlots[slot].mEglFence = eglFence;
440 mSlots[slot].mFence = releaseFence;
441 mSlots[slot].mBufferState.release();
442
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700443 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800444 // still be around. Mark it as no longer shared if this
445 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700446 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800447 mSlots[slot].mBufferState.mShared = false;
448 }
449 // Don't put the shared buffer on the free list.
450 if (!mSlots[slot].mBufferState.isShared()) {
451 mCore->mActiveBuffers.erase(slot);
452 mCore->mFreeBuffers.push_back(slot);
453 }
454
455 listener = mCore->mConnectedProducerListener;
456 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
457
Dan Stozad1c10362014-03-28 15:19:08 -0700458 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800459 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700460 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800461
Dan Stozad1c10362014-03-28 15:19:08 -0700462 // Call back without lock held
463 if (listener != NULL) {
464 listener->onBufferReleased();
465 }
Dan Stoza289ade12014-02-28 11:17:17 -0800466
467 return NO_ERROR;
468}
469
470status_t BufferQueueConsumer::connect(
471 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
472 ATRACE_CALL();
473
474 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700475 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800476 return BAD_VALUE;
477 }
478
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700479 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800480 controlledByApp ? "true" : "false");
481
482 Mutex::Autolock lock(mCore->mMutex);
483
484 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700485 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800486 return NO_INIT;
487 }
488
489 mCore->mConsumerListener = consumerListener;
490 mCore->mConsumerControlledByApp = controlledByApp;
491
492 return NO_ERROR;
493}
494
495status_t BufferQueueConsumer::disconnect() {
496 ATRACE_CALL();
497
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700498 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800499
500 Mutex::Autolock lock(mCore->mMutex);
501
502 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700503 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800504 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800505 }
506
507 mCore->mIsAbandoned = true;
508 mCore->mConsumerListener = NULL;
509 mCore->mQueue.clear();
510 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700511 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800512 mCore->mDequeueCondition.broadcast();
513 return NO_ERROR;
514}
515
Dan Stozafebd4f42014-04-09 16:14:51 -0700516status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800517 ATRACE_CALL();
518
519 if (outSlotMask == NULL) {
520 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
521 return BAD_VALUE;
522 }
523
524 Mutex::Autolock lock(mCore->mMutex);
525
526 if (mCore->mIsAbandoned) {
527 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
528 return NO_INIT;
529 }
530
Dan Stozafebd4f42014-04-09 16:14:51 -0700531 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800532 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800533 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700534 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800535 }
536 }
537
538 // Remove from the mask queued buffers for which acquire has been called,
539 // since the consumer will not receive their buffer addresses and so must
540 // retain their cached information
541 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
542 while (current != mCore->mQueue.end()) {
543 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700544 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800545 }
546 ++current;
547 }
548
Dan Stozafebd4f42014-04-09 16:14:51 -0700549 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800550 *outSlotMask = mask;
551 return NO_ERROR;
552}
553
554status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
555 uint32_t height) {
556 ATRACE_CALL();
557
558 if (width == 0 || height == 0) {
559 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
560 "height=%u)", width, height);
561 return BAD_VALUE;
562 }
563
564 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
565
566 Mutex::Autolock lock(mCore->mMutex);
567 mCore->mDefaultWidth = width;
568 mCore->mDefaultHeight = height;
569 return NO_ERROR;
570}
571
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700572status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800573 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800574
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700575 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
576 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
577 return BAD_VALUE;
578 }
Dan Stoza289ade12014-02-28 11:17:17 -0800579
Pablo Ceballos38273792016-03-02 01:38:10 +0000580 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800581
Pablo Ceballos38273792016-03-02 01:38:10 +0000582 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
583 BQ_LOGE("setMaxBufferCount: producer is already connected");
584 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800585 }
586
Pablo Ceballos38273792016-03-02 01:38:10 +0000587 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
588 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
589 "mMaxAcquiredBufferCount (%d)", bufferCount,
590 mCore->mMaxAcquiredBufferCount);
591 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700592 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000593
594 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
595 mCore->mDequeueBufferCannotBlock, bufferCount) -
596 mCore->getMaxBufferCountLocked();
597 if (!mCore->adjustAvailableSlotsLocked(delta)) {
598 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
599 "available slots. Delta = %d", delta);
600 return BAD_VALUE;
601 }
602
603 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800604 return NO_ERROR;
605}
606
607status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
608 int maxAcquiredBuffers) {
609 ATRACE_CALL();
610
611 if (maxAcquiredBuffers < 1 ||
612 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
613 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
614 maxAcquiredBuffers);
615 return BAD_VALUE;
616 }
617
Pablo Ceballos38273792016-03-02 01:38:10 +0000618 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800619 { // Autolock scope
620 Mutex::Autolock lock(mCore->mMutex);
621 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800622
Pablo Ceballos72daab62015-12-07 16:38:43 -0800623 if (mCore->mIsAbandoned) {
624 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
625 return NO_INIT;
626 }
627
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700628 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
629 return NO_ERROR;
630 }
631
Pablo Ceballos72daab62015-12-07 16:38:43 -0800632 // The new maxAcquiredBuffers count should not be violated by the number
633 // of currently acquired buffers
634 int acquiredCount = 0;
635 for (int slot : mCore->mActiveBuffers) {
636 if (mSlots[slot].mBufferState.isAcquired()) {
637 acquiredCount++;
638 }
639 }
640 if (acquiredCount > maxAcquiredBuffers) {
641 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
642 "count (%d) exceeds the current acquired buffer count (%d)",
643 maxAcquiredBuffers, acquiredCount);
644 return BAD_VALUE;
645 }
646
647 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
648 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
649 > mCore->mMaxBufferCount) {
650 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
651 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
652 maxAcquiredBuffers, mCore->mMaxBufferCount,
653 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
654 mCore->mDequeueBufferCannotBlock);
655 return BAD_VALUE;
656 }
657
658 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000659 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800660 return BAD_VALUE;
661 }
662
663 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
664 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
665 VALIDATE_CONSISTENCY();
666 if (delta < 0) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000667 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800668 }
669 }
670 // Call back without lock held
Pablo Ceballos38273792016-03-02 01:38:10 +0000671 if (listener != NULL) {
672 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800673 }
674
Dan Stoza289ade12014-02-28 11:17:17 -0800675 return NO_ERROR;
676}
677
678void BufferQueueConsumer::setConsumerName(const String8& name) {
679 ATRACE_CALL();
680 BQ_LOGV("setConsumerName: '%s'", name.string());
681 Mutex::Autolock lock(mCore->mMutex);
682 mCore->mConsumerName = name;
683 mConsumerName = name;
684}
685
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800686status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800687 ATRACE_CALL();
688 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
689 Mutex::Autolock lock(mCore->mMutex);
690 mCore->mDefaultBufferFormat = defaultFormat;
691 return NO_ERROR;
692}
693
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800694status_t BufferQueueConsumer::setDefaultBufferDataSpace(
695 android_dataspace defaultDataSpace) {
696 ATRACE_CALL();
697 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
698 Mutex::Autolock lock(mCore->mMutex);
699 mCore->mDefaultBufferDataSpace = defaultDataSpace;
700 return NO_ERROR;
701}
702
Dan Stoza289ade12014-02-28 11:17:17 -0800703status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
704 ATRACE_CALL();
705 BQ_LOGV("setConsumerUsageBits: %#x", usage);
706 Mutex::Autolock lock(mCore->mMutex);
707 mCore->mConsumerUsageBits = usage;
708 return NO_ERROR;
709}
710
711status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
712 ATRACE_CALL();
713 BQ_LOGV("setTransformHint: %#x", hint);
714 Mutex::Autolock lock(mCore->mMutex);
715 mCore->mTransformHint = hint;
716 return NO_ERROR;
717}
718
Jesse Hall399184a2014-03-03 15:42:54 -0800719sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
Fabien Sanglard2d8a2432016-11-08 15:31:32 -0800720 Mutex::Autolock lock(mCore->mMutex);
Jesse Hall399184a2014-03-03 15:42:54 -0800721 return mCore->mSidebandStream;
722}
723
Dan Stozae77c7662016-05-13 11:37:28 -0700724status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
725 std::vector<OccupancyTracker::Segment>* outHistory) {
726 Mutex::Autolock lock(mCore->mMutex);
727 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
728 return NO_ERROR;
729}
730
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700731status_t BufferQueueConsumer::discardFreeBuffers() {
732 Mutex::Autolock lock(mCore->mMutex);
733 mCore->discardFreeBuffersLocked();
734 return NO_ERROR;
735}
736
Colin Crossdc782512016-09-26 18:10:16 -0700737void BufferQueueConsumer::dumpState(String8& result, const char* prefix) const {
Yifan Hongfc8d8852017-07-26 10:47:14 -0700738 struct passwd* pwd = getpwnam("shell");
739 uid_t shellUid = pwd ? pwd->pw_uid : 0;
740 if (!shellUid) {
741 BQ_LOGE("Cannot get AID_SHELL");
742 return;
743 }
744
Pablo Ceballos88f69282016-02-11 18:01:49 -0800745 const IPCThreadState* ipc = IPCThreadState::self();
746 const pid_t pid = ipc->getCallingPid();
747 const uid_t uid = ipc->getCallingUid();
Yifan Hongfc8d8852017-07-26 10:47:14 -0700748 if ((uid != shellUid) &&
749 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
Pablo Ceballos88f69282016-02-11 18:01:49 -0800750 result.appendFormat("Permission Denial: can't dump BufferQueueConsumer "
751 "from pid=%d, uid=%d\n", pid, uid);
Colin Cross6e7e2b42016-09-27 14:08:19 -0700752 android_errorWriteWithInfoLog(0x534e4554, "27046057",
753 static_cast<int32_t>(uid), NULL, 0);
Pablo Ceballos88f69282016-02-11 18:01:49 -0800754 } else {
Colin Crossdc782512016-09-26 18:10:16 -0700755 mCore->dumpState(result, prefix);
Pablo Ceballos88f69282016-02-11 18:01:49 -0800756 }
Dan Stoza289ade12014-02-28 11:17:17 -0800757}
758
759} // namespace android