blob: aacbed58742a0a262bd82128abbd21e5a13d3eab [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>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueConsumer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#include <gui/BufferItem.h>
30#include <gui/BufferQueueConsumer.h>
31#include <gui/BufferQueueCore.h>
32#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070033#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080034
Pablo Ceballos88f69282016-02-11 18:01:49 -080035#include <binder/IPCThreadState.h>
36#include <binder/PermissionCache.h>
37#include <private/android_filesystem_config.h>
38
Dan Stoza289ade12014-02-28 11:17:17 -080039namespace android {
40
41BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
42 mCore(core),
43 mSlots(core->mSlots),
44 mConsumerName() {}
45
46BufferQueueConsumer::~BufferQueueConsumer() {}
47
48status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070049 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080050 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080051
Lajos Molnar5f920c12015-07-13 16:04:24 -070052 int numDroppedBuffers = 0;
53 sp<IProducerListener> listener;
54 {
55 Mutex::Autolock lock(mCore->mMutex);
56
57 // Check that the consumer doesn't currently have the maximum number of
58 // buffers acquired. We allow the max buffer count to be exceeded by one
59 // buffer so that the consumer can successfully set up the newly acquired
60 // buffer before releasing the old one.
61 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080062 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070063 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070064 ++numAcquiredBuffers;
65 }
Dan Stoza289ade12014-02-28 11:17:17 -080066 }
Lajos Molnar5f920c12015-07-13 16:04:24 -070067 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
68 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
69 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
70 return INVALID_OPERATION;
71 }
Dan Stoza289ade12014-02-28 11:17:17 -080072
Pablo Ceballosccdfd602015-10-07 15:05:45 -070073 bool sharedBufferAvailable = mCore->mSingleBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -080074 mCore->mAutoRefresh && mCore->mSingleBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -070075 BufferQueueCore::INVALID_BUFFER_SLOT;
76
Lajos Molnar5f920c12015-07-13 16:04:24 -070077 // In asynchronous mode the list is guaranteed to be one buffer deep,
78 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070079 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070080 return NO_BUFFER_AVAILABLE;
81 }
Dan Stoza289ade12014-02-28 11:17:17 -080082
Lajos Molnar5f920c12015-07-13 16:04:24 -070083 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080084
Lajos Molnar5f920c12015-07-13 16:04:24 -070085 // If expectedPresent is specified, we may not want to return a buffer yet.
86 // If it's specified and there's more than one buffer queued, we may want
87 // to drop a buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070088 // Skip this if we're in single buffer mode and the queue is empty,
89 // since in that case we'll just return the shared buffer.
90 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070091 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080092
Lajos Molnar5f920c12015-07-13 16:04:24 -070093 // The 'expectedPresent' argument indicates when the buffer is expected
94 // to be presented on-screen. If the buffer's desired present time is
95 // earlier (less) than expectedPresent -- meaning it will be displayed
96 // on time or possibly late if we show it as soon as possible -- we
97 // acquire and return it. If we don't want to display it until after the
98 // expectedPresent time, we return PRESENT_LATER without acquiring it.
99 //
100 // To be safe, we don't defer acquisition if expectedPresent is more
101 // than one second in the future beyond the desired present time
102 // (i.e., we'd be holding the buffer for a long time).
103 //
104 // NOTE: Code assumes monotonic time values from the system clock
105 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800106
Lajos Molnar5f920c12015-07-13 16:04:24 -0700107 // Start by checking to see if we can drop frames. We skip this check if
108 // the timestamps are being auto-generated by Surface. If the app isn't
109 // generating timestamps explicitly, it probably doesn't want frames to
110 // be discarded based on them.
111 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
112 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700113
Lajos Molnar5f920c12015-07-13 16:04:24 -0700114 // If dropping entry[0] would leave us with a buffer that the
115 // consumer is not yet ready for, don't drop it.
116 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
117 break;
118 }
119
120 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
121 // additional criterion here: we only drop the earlier buffer if our
122 // desiredPresent falls within +/- 1 second of the expected present.
123 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
124 // relative timestamp), which normally mean "ignore the timestamp
125 // and acquire immediately", would cause us to drop frames.
126 //
127 // We may want to add an additional criterion: don't drop the
128 // earlier buffer if entry[1]'s fence hasn't signaled yet.
129 nsecs_t desiredPresent = bufferItem.mTimestamp;
130 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
131 desiredPresent > expectedPresent) {
132 // This buffer is set to display in the near future, or
133 // desiredPresent is garbage. Either way we don't want to drop
134 // the previous buffer just to get this on the screen sooner.
135 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
136 PRId64 " (%" PRId64 ") now=%" PRId64,
137 desiredPresent, expectedPresent,
138 desiredPresent - expectedPresent,
139 systemTime(CLOCK_MONOTONIC));
140 break;
141 }
142
143 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
144 " size=%zu",
145 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800146
147 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700148 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700149 mSlots[front->mSlot].mBufferState.freeQueued();
150
151 // After leaving single buffer mode, the shared buffer will
152 // still be around. Mark it as no longer shared if this
153 // operation causes it to be free.
154 if (!mCore->mSingleBufferMode &&
155 mSlots[front->mSlot].mBufferState.isFree()) {
156 mSlots[front->mSlot].mBufferState.mShared = false;
157 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800158
159 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700160 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800161 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700162 mCore->mFreeBuffers.push_back(front->mSlot);
163 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800164
Lajos Molnar5f920c12015-07-13 16:04:24 -0700165 listener = mCore->mConnectedProducerListener;
166 ++numDroppedBuffers;
167 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800168
Lajos Molnar5f920c12015-07-13 16:04:24 -0700169 mCore->mQueue.erase(front);
170 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700171 }
172
Lajos Molnar5f920c12015-07-13 16:04:24 -0700173 // See if the front buffer is ready to be acquired
174 nsecs_t desiredPresent = front->mTimestamp;
175 bool bufferIsDue = desiredPresent <= expectedPresent ||
176 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
177 bool consumerIsReady = maxFrameNumber > 0 ?
178 front->mFrameNumber <= maxFrameNumber : true;
179 if (!bufferIsDue || !consumerIsReady) {
180 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
181 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
182 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700183 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800184 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700185 systemTime(CLOCK_MONOTONIC),
186 front->mFrameNumber, maxFrameNumber);
187 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800188 }
189
Lajos Molnar5f920c12015-07-13 16:04:24 -0700190 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
191 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800192 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700193 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800194 }
195
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700196 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
197
198 if (sharedBufferAvailable && mCore->mQueue.empty()) {
199 // make sure the buffer has finished allocating before acquiring it
200 mCore->waitWhileAllocatingLocked();
201
202 slot = mCore->mSingleBufferSlot;
203
204 // Recreate the BufferItem for the shared buffer from the data that
205 // was cached when it was last queued.
206 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
207 outBuffer->mFence = Fence::NO_FENCE;
208 outBuffer->mCrop = mCore->mSingleBufferCache.crop;
209 outBuffer->mTransform = mCore->mSingleBufferCache.transform &
210 ~static_cast<uint32_t>(
211 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
212 outBuffer->mScalingMode = mCore->mSingleBufferCache.scalingMode;
213 outBuffer->mDataSpace = mCore->mSingleBufferCache.dataspace;
214 outBuffer->mFrameNumber = mCore->mFrameCounter;
215 outBuffer->mSlot = slot;
216 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
217 outBuffer->mTransformToDisplayInverse =
218 (mCore->mSingleBufferCache.transform &
219 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
220 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700221 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800222 outBuffer->mIsStale = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800223 outBuffer->mAutoRefresh = mCore->mSingleBufferMode &&
224 mCore->mAutoRefresh;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700225 } else {
226 slot = front->mSlot;
227 *outBuffer = *front;
228 }
229
Lajos Molnar5f920c12015-07-13 16:04:24 -0700230 ATRACE_BUFFER_INDEX(slot);
231
232 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700233 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800234
235 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700236 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700237 // Don't decrease the queue count if the BufferItem wasn't
238 // previously in the queue. This happens in single buffer mode when
239 // the queue is empty and the BufferItem is created above.
240 if (mCore->mQueue.empty()) {
241 mSlots[slot].mBufferState.acquireNotInQueue();
242 } else {
243 mSlots[slot].mBufferState.acquire();
244 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700245 mSlots[slot].mFence = Fence::NO_FENCE;
246 }
247
248 // If the buffer has previously been acquired by the consumer, set
249 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
250 // on the consumer side
251 if (outBuffer->mAcquireCalled) {
252 outBuffer->mGraphicBuffer = NULL;
253 }
254
255 mCore->mQueue.erase(front);
256
257 // We might have freed a slot while dropping old buffers, or the producer
258 // may be blocked waiting for the number of buffers in the queue to
259 // decrease.
260 mCore->mDequeueCondition.broadcast();
261
262 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
263
Pablo Ceballos9e314332016-01-12 13:49:19 -0800264 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800265 }
266
Lajos Molnar5f920c12015-07-13 16:04:24 -0700267 if (listener != NULL) {
268 for (int i = 0; i < numDroppedBuffers; ++i) {
269 listener->onBufferReleased();
270 }
Dan Stoza289ade12014-02-28 11:17:17 -0800271 }
272
Dan Stoza289ade12014-02-28 11:17:17 -0800273 return NO_ERROR;
274}
275
Dan Stoza9f3053d2014-03-06 15:14:33 -0800276status_t BufferQueueConsumer::detachBuffer(int slot) {
277 ATRACE_CALL();
278 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700279 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800280
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800281 sp<IConsumerListener> consumerListener;
282 sp<IProducerListener> producerListener;
283 {
284 Mutex::Autolock lock(mCore->mMutex);
285
286 if (mCore->mIsAbandoned) {
287 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
288 return NO_INIT;
289 }
290
291 if (mCore->mSingleBufferMode || slot == mCore->mSingleBufferSlot) {
292 BQ_LOGE("detachBuffer: detachBuffer not allowed in single buffer"
293 "mode");
294 return BAD_VALUE;
295 }
296
297 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
298 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
299 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
300 return BAD_VALUE;
301 } else if (!mSlots[slot].mBufferState.isAcquired()) {
302 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
303 "(state = %s)", slot, mSlots[slot].mBufferState.string());
304 return BAD_VALUE;
305 }
306
307 mSlots[slot].mBufferState.detachConsumer();
308 mCore->mActiveBuffers.erase(slot);
309 mCore->mFreeSlots.insert(slot);
310 mCore->clearBufferSlotLocked(slot);
311 mCore->mDequeueCondition.broadcast();
312 VALIDATE_CONSISTENCY();
313 producerListener = mCore->mConnectedProducerListener;
314 consumerListener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800315 }
316
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800317 // Call back without lock held
318 if (producerListener != NULL) {
319 producerListener->onSlotFreed(slot);
320 }
321 if (consumerListener != NULL) {
322 consumerListener->onBuffersReleased();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800323 }
324
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325
326 return NO_ERROR;
327}
328
329status_t BufferQueueConsumer::attachBuffer(int* outSlot,
330 const sp<android::GraphicBuffer>& buffer) {
331 ATRACE_CALL();
332
333 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700334 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800335 return BAD_VALUE;
336 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700337 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800338 return BAD_VALUE;
339 }
340
341 Mutex::Autolock lock(mCore->mMutex);
342
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700343 if (mCore->mSingleBufferMode) {
344 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer"
345 "mode");
346 return BAD_VALUE;
347 }
348
Dan Stoza0de7ea72015-04-23 13:20:51 -0700349 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800350 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800351 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700352 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800353 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800354 }
355 }
356
357 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700358 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800359 "(max %d)", numAcquiredBuffers,
360 mCore->mMaxAcquiredBufferCount);
361 return INVALID_OPERATION;
362 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700363
Dan Stoza812ed062015-06-02 15:45:22 -0700364 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
365 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
366 "[queue %u]", buffer->getGenerationNumber(),
367 mCore->mGenerationNumber);
368 return BAD_VALUE;
369 }
370
Dan Stoza0de7ea72015-04-23 13:20:51 -0700371 // Find a free slot to put the buffer into
372 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
373 if (!mCore->mFreeSlots.empty()) {
374 auto slot = mCore->mFreeSlots.begin();
375 found = *slot;
376 mCore->mFreeSlots.erase(slot);
377 } else if (!mCore->mFreeBuffers.empty()) {
378 found = mCore->mFreeBuffers.front();
379 mCore->mFreeBuffers.remove(found);
380 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700382 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800383 return NO_MEMORY;
384 }
385
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800386 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800387 *outSlot = found;
388 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700389 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800390
391 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700392 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800393 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800394 mSlots[*outSlot].mFence = Fence::NO_FENCE;
395 mSlots[*outSlot].mFrameNumber = 0;
396
Dan Stoza99b18b42014-03-28 15:34:33 -0700397 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
398 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
399 // Binder traffic by not un/flattening the GraphicBuffer. However, it
400 // requires that the consumer maintain a cached copy of the slot <--> buffer
401 // mappings, which is why the consumer doesn't need the valid pointer on
402 // acquire.
403 //
404 // The StreamSplitter is one of the primary users of the attach/detach
405 // logic, and while it is running, all buffers it acquires are immediately
406 // detached, and all buffers it eventually releases are ones that were
407 // attached (as opposed to having been obtained from acquireBuffer), so it
408 // doesn't make sense to maintain the slot/buffer mappings, which would
409 // become invalid for every buffer during detach/attach. By setting this to
410 // false, the valid GraphicBuffer pointer will always be sent with acquire
411 // for attached buffers.
412 mSlots[*outSlot].mAcquireCalled = false;
413
Pablo Ceballos9e314332016-01-12 13:49:19 -0800414 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700415
Dan Stoza9f3053d2014-03-06 15:14:33 -0800416 return NO_ERROR;
417}
418
Dan Stoza289ade12014-02-28 11:17:17 -0800419status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
420 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
421 EGLSyncKHR eglFence) {
422 ATRACE_CALL();
423 ATRACE_BUFFER_INDEX(slot);
424
Dan Stoza9f3053d2014-03-06 15:14:33 -0800425 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
426 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700427 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
428 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800429 return BAD_VALUE;
430 }
431
Dan Stozad1c10362014-03-28 15:19:08 -0700432 sp<IProducerListener> listener;
433 { // Autolock scope
434 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800435
Dan Stozad1c10362014-03-28 15:19:08 -0700436 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700437 // we can ignore this releaseBuffer for the old buffer.
438 // Ignore this for the shared buffer where the frame number can easily
439 // get out of sync due to the buffer being queued and acquired at the
440 // same time.
441 if (frameNumber != mSlots[slot].mFrameNumber &&
442 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700443 return STALE_BUFFER_SLOT;
444 }
Dan Stoza289ade12014-02-28 11:17:17 -0800445
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800446 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700447 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700448 "but its state was %s", slot,
449 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800450 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800451 }
Dan Stoza289ade12014-02-28 11:17:17 -0800452
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800453 mSlots[slot].mEglDisplay = eglDisplay;
454 mSlots[slot].mEglFence = eglFence;
455 mSlots[slot].mFence = releaseFence;
456 mSlots[slot].mBufferState.release();
457
458 // After leaving single buffer mode, the shared buffer will
459 // still be around. Mark it as no longer shared if this
460 // operation causes it to be free.
461 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
462 mSlots[slot].mBufferState.mShared = false;
463 }
464 // Don't put the shared buffer on the free list.
465 if (!mSlots[slot].mBufferState.isShared()) {
466 mCore->mActiveBuffers.erase(slot);
467 mCore->mFreeBuffers.push_back(slot);
468 }
469
470 listener = mCore->mConnectedProducerListener;
471 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
472
Dan Stozad1c10362014-03-28 15:19:08 -0700473 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800474 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700475 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800476
Dan Stozad1c10362014-03-28 15:19:08 -0700477 // Call back without lock held
478 if (listener != NULL) {
479 listener->onBufferReleased();
480 }
Dan Stoza289ade12014-02-28 11:17:17 -0800481
482 return NO_ERROR;
483}
484
485status_t BufferQueueConsumer::connect(
486 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
487 ATRACE_CALL();
488
489 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700490 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800491 return BAD_VALUE;
492 }
493
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700494 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800495 controlledByApp ? "true" : "false");
496
497 Mutex::Autolock lock(mCore->mMutex);
498
499 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700500 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800501 return NO_INIT;
502 }
503
504 mCore->mConsumerListener = consumerListener;
505 mCore->mConsumerControlledByApp = controlledByApp;
506
507 return NO_ERROR;
508}
509
510status_t BufferQueueConsumer::disconnect() {
511 ATRACE_CALL();
512
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700513 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800514
515 Mutex::Autolock lock(mCore->mMutex);
516
517 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700518 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800519 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800520 }
521
522 mCore->mIsAbandoned = true;
523 mCore->mConsumerListener = NULL;
524 mCore->mQueue.clear();
525 mCore->freeAllBuffersLocked();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800526 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800527 mCore->mDequeueCondition.broadcast();
528 return NO_ERROR;
529}
530
Dan Stozafebd4f42014-04-09 16:14:51 -0700531status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800532 ATRACE_CALL();
533
534 if (outSlotMask == NULL) {
535 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
536 return BAD_VALUE;
537 }
538
539 Mutex::Autolock lock(mCore->mMutex);
540
541 if (mCore->mIsAbandoned) {
542 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
543 return NO_INIT;
544 }
545
Dan Stozafebd4f42014-04-09 16:14:51 -0700546 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800547 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800548 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700549 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800550 }
551 }
552
553 // Remove from the mask queued buffers for which acquire has been called,
554 // since the consumer will not receive their buffer addresses and so must
555 // retain their cached information
556 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
557 while (current != mCore->mQueue.end()) {
558 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700559 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800560 }
561 ++current;
562 }
563
Dan Stozafebd4f42014-04-09 16:14:51 -0700564 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800565 *outSlotMask = mask;
566 return NO_ERROR;
567}
568
569status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
570 uint32_t height) {
571 ATRACE_CALL();
572
573 if (width == 0 || height == 0) {
574 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
575 "height=%u)", width, height);
576 return BAD_VALUE;
577 }
578
579 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
580
581 Mutex::Autolock lock(mCore->mMutex);
582 mCore->mDefaultWidth = width;
583 mCore->mDefaultHeight = height;
584 return NO_ERROR;
585}
586
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700587status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800588 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800589
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700590 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
591 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
592 return BAD_VALUE;
593 }
Dan Stoza289ade12014-02-28 11:17:17 -0800594
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800595 sp<IConsumerListener> listener;
596 {
597 Mutex::Autolock lock(mCore->mMutex);
598 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
599 BQ_LOGE("setMaxBufferCount: producer is already connected");
600 return INVALID_OPERATION;
601 }
Dan Stoza289ade12014-02-28 11:17:17 -0800602
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800603 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
604 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
605 "mMaxAcquiredBufferCount (%d)", bufferCount,
606 mCore->mMaxAcquiredBufferCount);
607 return BAD_VALUE;
608 }
609
610 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
611 mCore->mDequeueBufferCannotBlock, bufferCount) -
612 mCore->getMaxBufferCountLocked();
613 if (!mCore->adjustAvailableSlotsLocked(delta, nullptr)) {
614 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number"
615 " of available slots. Delta = %d", delta);
616 return BAD_VALUE;
617 }
618
619 mCore->mMaxBufferCount = bufferCount;
620 if (delta < 0) {
621 listener = mCore->mConsumerListener;
622 }
Dan Stoza289ade12014-02-28 11:17:17 -0800623 }
624
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800625 // Call back without lock held
626 if (listener != NULL) {
627 listener->onBuffersReleased();
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700628 }
Dan Stoza289ade12014-02-28 11:17:17 -0800629 return NO_ERROR;
630}
631
632status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
633 int maxAcquiredBuffers) {
634 ATRACE_CALL();
635
636 if (maxAcquiredBuffers < 1 ||
637 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
638 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
639 maxAcquiredBuffers);
640 return BAD_VALUE;
641 }
642
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800643 sp<IConsumerListener> consumerListener;
644 sp<IProducerListener> producerListener;
645 std::vector<int> freedSlots;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800646 { // Autolock scope
647 Mutex::Autolock lock(mCore->mMutex);
648 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800649
Pablo Ceballos72daab62015-12-07 16:38:43 -0800650 if (mCore->mIsAbandoned) {
651 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
652 return NO_INIT;
653 }
654
655 // The new maxAcquiredBuffers count should not be violated by the number
656 // of currently acquired buffers
657 int acquiredCount = 0;
658 for (int slot : mCore->mActiveBuffers) {
659 if (mSlots[slot].mBufferState.isAcquired()) {
660 acquiredCount++;
661 }
662 }
663 if (acquiredCount > maxAcquiredBuffers) {
664 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
665 "count (%d) exceeds the current acquired buffer count (%d)",
666 maxAcquiredBuffers, acquiredCount);
667 return BAD_VALUE;
668 }
669
670 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
671 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
672 > mCore->mMaxBufferCount) {
673 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
674 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
675 maxAcquiredBuffers, mCore->mMaxBufferCount,
676 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
677 mCore->mDequeueBufferCannotBlock);
678 return BAD_VALUE;
679 }
680
681 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800682 if (!mCore->adjustAvailableSlotsLocked(delta, &freedSlots)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800683 return BAD_VALUE;
684 }
685
686 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
687 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
688 VALIDATE_CONSISTENCY();
689 if (delta < 0) {
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800690 consumerListener = mCore->mConsumerListener;
691 producerListener = mCore->mConnectedProducerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800692 }
693 }
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800694
Pablo Ceballos72daab62015-12-07 16:38:43 -0800695 // Call back without lock held
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800696 if (consumerListener != NULL) {
697 consumerListener->onBuffersReleased();
698 }
699 if (producerListener != NULL) {
700 for (int i : freedSlots) {
701 producerListener->onSlotFreed(i);
702 }
Dan Stoza289ade12014-02-28 11:17:17 -0800703 }
704
Dan Stoza289ade12014-02-28 11:17:17 -0800705 return NO_ERROR;
706}
707
708void BufferQueueConsumer::setConsumerName(const String8& name) {
709 ATRACE_CALL();
710 BQ_LOGV("setConsumerName: '%s'", name.string());
711 Mutex::Autolock lock(mCore->mMutex);
712 mCore->mConsumerName = name;
713 mConsumerName = name;
714}
715
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800716status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800717 ATRACE_CALL();
718 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
719 Mutex::Autolock lock(mCore->mMutex);
720 mCore->mDefaultBufferFormat = defaultFormat;
721 return NO_ERROR;
722}
723
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800724status_t BufferQueueConsumer::setDefaultBufferDataSpace(
725 android_dataspace defaultDataSpace) {
726 ATRACE_CALL();
727 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
728 Mutex::Autolock lock(mCore->mMutex);
729 mCore->mDefaultBufferDataSpace = defaultDataSpace;
730 return NO_ERROR;
731}
732
Dan Stoza289ade12014-02-28 11:17:17 -0800733status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
734 ATRACE_CALL();
735 BQ_LOGV("setConsumerUsageBits: %#x", usage);
736 Mutex::Autolock lock(mCore->mMutex);
737 mCore->mConsumerUsageBits = usage;
738 return NO_ERROR;
739}
740
741status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
742 ATRACE_CALL();
743 BQ_LOGV("setTransformHint: %#x", hint);
744 Mutex::Autolock lock(mCore->mMutex);
745 mCore->mTransformHint = hint;
746 return NO_ERROR;
747}
748
Jesse Hall399184a2014-03-03 15:42:54 -0800749sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
750 return mCore->mSidebandStream;
751}
752
Dan Stoza289ade12014-02-28 11:17:17 -0800753void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
Pablo Ceballos88f69282016-02-11 18:01:49 -0800754 const IPCThreadState* ipc = IPCThreadState::self();
755 const pid_t pid = ipc->getCallingPid();
756 const uid_t uid = ipc->getCallingUid();
757 if ((uid != AID_SHELL)
758 && !PermissionCache::checkPermission(String16(
759 "android.permission.DUMP"), pid, uid)) {
760 result.appendFormat("Permission Denial: can't dump BufferQueueConsumer "
761 "from pid=%d, uid=%d\n", pid, uid);
Pablo Ceballosd46695b2016-02-20 11:26:13 -0800762 android_errorWriteWithInfoLog(0x534e4554, "27046057", uid, NULL, 0);
Pablo Ceballos88f69282016-02-11 18:01:49 -0800763 } else {
764 mCore->dump(result, prefix);
765 }
Dan Stoza289ade12014-02-28 11:17:17 -0800766}
767
768} // namespace android