blob: b025280382439a7cfa327d923ef3655279f0dcb3 [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
35namespace android {
36
37BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
38 mCore(core),
39 mSlots(core->mSlots),
40 mConsumerName() {}
41
42BufferQueueConsumer::~BufferQueueConsumer() {}
43
44status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070045 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080046 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080047
Lajos Molnar5f920c12015-07-13 16:04:24 -070048 int numDroppedBuffers = 0;
49 sp<IProducerListener> listener;
50 {
51 Mutex::Autolock lock(mCore->mMutex);
52
53 // Check that the consumer doesn't currently have the maximum number of
54 // buffers acquired. We allow the max buffer count to be exceeded by one
55 // buffer so that the consumer can successfully set up the newly acquired
56 // buffer before releasing the old one.
57 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080058 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070059 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070060 ++numAcquiredBuffers;
61 }
Dan Stoza289ade12014-02-28 11:17:17 -080062 }
Lajos Molnar5f920c12015-07-13 16:04:24 -070063 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
64 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
65 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
66 return INVALID_OPERATION;
67 }
Dan Stoza289ade12014-02-28 11:17:17 -080068
Pablo Ceballosccdfd602015-10-07 15:05:45 -070069 bool sharedBufferAvailable = mCore->mSingleBufferMode &&
70 mCore->mSingleBufferSlot !=
71 BufferQueueCore::INVALID_BUFFER_SLOT;
72
Lajos Molnar5f920c12015-07-13 16:04:24 -070073 // In asynchronous mode the list is guaranteed to be one buffer deep,
74 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070075 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070076 return NO_BUFFER_AVAILABLE;
77 }
Dan Stoza289ade12014-02-28 11:17:17 -080078
Lajos Molnar5f920c12015-07-13 16:04:24 -070079 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080080
Lajos Molnar5f920c12015-07-13 16:04:24 -070081 // If expectedPresent is specified, we may not want to return a buffer yet.
82 // If it's specified and there's more than one buffer queued, we may want
83 // to drop a buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070084 // Skip this if we're in single buffer mode and the queue is empty,
85 // since in that case we'll just return the shared buffer.
86 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070087 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080088
Lajos Molnar5f920c12015-07-13 16:04:24 -070089 // The 'expectedPresent' argument indicates when the buffer is expected
90 // to be presented on-screen. If the buffer's desired present time is
91 // earlier (less) than expectedPresent -- meaning it will be displayed
92 // on time or possibly late if we show it as soon as possible -- we
93 // acquire and return it. If we don't want to display it until after the
94 // expectedPresent time, we return PRESENT_LATER without acquiring it.
95 //
96 // To be safe, we don't defer acquisition if expectedPresent is more
97 // than one second in the future beyond the desired present time
98 // (i.e., we'd be holding the buffer for a long time).
99 //
100 // NOTE: Code assumes monotonic time values from the system clock
101 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800102
Lajos Molnar5f920c12015-07-13 16:04:24 -0700103 // Start by checking to see if we can drop frames. We skip this check if
104 // the timestamps are being auto-generated by Surface. If the app isn't
105 // generating timestamps explicitly, it probably doesn't want frames to
106 // be discarded based on them.
107 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
108 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700109
Lajos Molnar5f920c12015-07-13 16:04:24 -0700110 // If dropping entry[0] would leave us with a buffer that the
111 // consumer is not yet ready for, don't drop it.
112 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
113 break;
114 }
115
116 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
117 // additional criterion here: we only drop the earlier buffer if our
118 // desiredPresent falls within +/- 1 second of the expected present.
119 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
120 // relative timestamp), which normally mean "ignore the timestamp
121 // and acquire immediately", would cause us to drop frames.
122 //
123 // We may want to add an additional criterion: don't drop the
124 // earlier buffer if entry[1]'s fence hasn't signaled yet.
125 nsecs_t desiredPresent = bufferItem.mTimestamp;
126 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
127 desiredPresent > expectedPresent) {
128 // This buffer is set to display in the near future, or
129 // desiredPresent is garbage. Either way we don't want to drop
130 // the previous buffer just to get this on the screen sooner.
131 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
132 PRId64 " (%" PRId64 ") now=%" PRId64,
133 desiredPresent, expectedPresent,
134 desiredPresent - expectedPresent,
135 systemTime(CLOCK_MONOTONIC));
136 break;
137 }
138
139 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
140 " size=%zu",
141 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800142
143 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700144 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700145 mSlots[front->mSlot].mBufferState.freeQueued();
146
147 // After leaving single buffer mode, the shared buffer will
148 // still be around. Mark it as no longer shared if this
149 // operation causes it to be free.
150 if (!mCore->mSingleBufferMode &&
151 mSlots[front->mSlot].mBufferState.isFree()) {
152 mSlots[front->mSlot].mBufferState.mShared = false;
153 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800154
155 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700156 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800157 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700158 mCore->mFreeBuffers.push_back(front->mSlot);
159 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800160
Lajos Molnar5f920c12015-07-13 16:04:24 -0700161 listener = mCore->mConnectedProducerListener;
162 ++numDroppedBuffers;
163 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800164
Lajos Molnar5f920c12015-07-13 16:04:24 -0700165 mCore->mQueue.erase(front);
166 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700167 }
168
Lajos Molnar5f920c12015-07-13 16:04:24 -0700169 // See if the front buffer is ready to be acquired
170 nsecs_t desiredPresent = front->mTimestamp;
171 bool bufferIsDue = desiredPresent <= expectedPresent ||
172 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
173 bool consumerIsReady = maxFrameNumber > 0 ?
174 front->mFrameNumber <= maxFrameNumber : true;
175 if (!bufferIsDue || !consumerIsReady) {
176 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
177 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
178 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700179 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800180 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700181 systemTime(CLOCK_MONOTONIC),
182 front->mFrameNumber, maxFrameNumber);
183 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800184 }
185
Lajos Molnar5f920c12015-07-13 16:04:24 -0700186 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
187 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800188 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700189 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800190 }
191
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700192 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
193
194 if (sharedBufferAvailable && mCore->mQueue.empty()) {
195 // make sure the buffer has finished allocating before acquiring it
196 mCore->waitWhileAllocatingLocked();
197
198 slot = mCore->mSingleBufferSlot;
199
200 // Recreate the BufferItem for the shared buffer from the data that
201 // was cached when it was last queued.
202 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
203 outBuffer->mFence = Fence::NO_FENCE;
204 outBuffer->mCrop = mCore->mSingleBufferCache.crop;
205 outBuffer->mTransform = mCore->mSingleBufferCache.transform &
206 ~static_cast<uint32_t>(
207 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
208 outBuffer->mScalingMode = mCore->mSingleBufferCache.scalingMode;
209 outBuffer->mDataSpace = mCore->mSingleBufferCache.dataspace;
210 outBuffer->mFrameNumber = mCore->mFrameCounter;
211 outBuffer->mSlot = slot;
212 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
213 outBuffer->mTransformToDisplayInverse =
214 (mCore->mSingleBufferCache.transform &
215 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
216 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700217 outBuffer->mSingleBufferMode = true;
218 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800219 outBuffer->mIsStale = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700220 } else {
221 slot = front->mSlot;
222 *outBuffer = *front;
223 }
224
Pablo Ceballos06312182015-10-07 16:32:12 -0700225 outBuffer->mSingleBufferMode = mCore->mSingleBufferMode;
226
Lajos Molnar5f920c12015-07-13 16:04:24 -0700227 ATRACE_BUFFER_INDEX(slot);
228
229 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700230 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800231
232 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700233 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700234 // Don't decrease the queue count if the BufferItem wasn't
235 // previously in the queue. This happens in single buffer mode when
236 // the queue is empty and the BufferItem is created above.
237 if (mCore->mQueue.empty()) {
238 mSlots[slot].mBufferState.acquireNotInQueue();
239 } else {
240 mSlots[slot].mBufferState.acquire();
241 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700242 mSlots[slot].mFence = Fence::NO_FENCE;
243 }
244
245 // If the buffer has previously been acquired by the consumer, set
246 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
247 // on the consumer side
248 if (outBuffer->mAcquireCalled) {
249 outBuffer->mGraphicBuffer = NULL;
250 }
251
252 mCore->mQueue.erase(front);
253
254 // We might have freed a slot while dropping old buffers, or the producer
255 // may be blocked waiting for the number of buffers in the queue to
256 // decrease.
257 mCore->mDequeueCondition.broadcast();
258
259 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
260
Pablo Ceballos9e314332016-01-12 13:49:19 -0800261 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800262 }
263
Lajos Molnar5f920c12015-07-13 16:04:24 -0700264 if (listener != NULL) {
265 for (int i = 0; i < numDroppedBuffers; ++i) {
266 listener->onBufferReleased();
267 }
Dan Stoza289ade12014-02-28 11:17:17 -0800268 }
269
Dan Stoza289ade12014-02-28 11:17:17 -0800270 return NO_ERROR;
271}
272
Dan Stoza9f3053d2014-03-06 15:14:33 -0800273status_t BufferQueueConsumer::detachBuffer(int slot) {
274 ATRACE_CALL();
275 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700276 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800278 sp<IConsumerListener> consumerListener;
279 sp<IProducerListener> producerListener;
280 {
281 Mutex::Autolock lock(mCore->mMutex);
282
283 if (mCore->mIsAbandoned) {
284 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
285 return NO_INIT;
286 }
287
288 if (mCore->mSingleBufferMode || slot == mCore->mSingleBufferSlot) {
289 BQ_LOGE("detachBuffer: detachBuffer not allowed in single buffer"
290 "mode");
291 return BAD_VALUE;
292 }
293
294 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
295 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
296 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
297 return BAD_VALUE;
298 } else if (!mSlots[slot].mBufferState.isAcquired()) {
299 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
300 "(state = %s)", slot, mSlots[slot].mBufferState.string());
301 return BAD_VALUE;
302 }
303
304 mSlots[slot].mBufferState.detachConsumer();
305 mCore->mActiveBuffers.erase(slot);
306 mCore->mFreeSlots.insert(slot);
307 mCore->clearBufferSlotLocked(slot);
308 mCore->mDequeueCondition.broadcast();
309 VALIDATE_CONSISTENCY();
310 producerListener = mCore->mConnectedProducerListener;
311 consumerListener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800312 }
313
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800314 // Call back without lock held
315 if (producerListener != NULL) {
316 producerListener->onSlotFreed(slot);
317 }
318 if (consumerListener != NULL) {
319 consumerListener->onBuffersReleased();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800320 }
321
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322
323 return NO_ERROR;
324}
325
326status_t BufferQueueConsumer::attachBuffer(int* outSlot,
327 const sp<android::GraphicBuffer>& buffer) {
328 ATRACE_CALL();
329
330 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700331 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800332 return BAD_VALUE;
333 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700334 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800335 return BAD_VALUE;
336 }
337
338 Mutex::Autolock lock(mCore->mMutex);
339
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700340 if (mCore->mSingleBufferMode) {
341 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer"
342 "mode");
343 return BAD_VALUE;
344 }
345
Dan Stoza0de7ea72015-04-23 13:20:51 -0700346 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800347 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800348 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700349 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800350 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800351 }
352 }
353
354 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700355 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800356 "(max %d)", numAcquiredBuffers,
357 mCore->mMaxAcquiredBufferCount);
358 return INVALID_OPERATION;
359 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700360
Dan Stoza812ed062015-06-02 15:45:22 -0700361 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
362 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
363 "[queue %u]", buffer->getGenerationNumber(),
364 mCore->mGenerationNumber);
365 return BAD_VALUE;
366 }
367
Dan Stoza0de7ea72015-04-23 13:20:51 -0700368 // Find a free slot to put the buffer into
369 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
370 if (!mCore->mFreeSlots.empty()) {
371 auto slot = mCore->mFreeSlots.begin();
372 found = *slot;
373 mCore->mFreeSlots.erase(slot);
374 } else if (!mCore->mFreeBuffers.empty()) {
375 found = mCore->mFreeBuffers.front();
376 mCore->mFreeBuffers.remove(found);
377 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800378 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700379 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380 return NO_MEMORY;
381 }
382
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800383 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800384 *outSlot = found;
385 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700386 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800387
388 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700389 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800390 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391 mSlots[*outSlot].mFence = Fence::NO_FENCE;
392 mSlots[*outSlot].mFrameNumber = 0;
393
Dan Stoza99b18b42014-03-28 15:34:33 -0700394 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
395 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
396 // Binder traffic by not un/flattening the GraphicBuffer. However, it
397 // requires that the consumer maintain a cached copy of the slot <--> buffer
398 // mappings, which is why the consumer doesn't need the valid pointer on
399 // acquire.
400 //
401 // The StreamSplitter is one of the primary users of the attach/detach
402 // logic, and while it is running, all buffers it acquires are immediately
403 // detached, and all buffers it eventually releases are ones that were
404 // attached (as opposed to having been obtained from acquireBuffer), so it
405 // doesn't make sense to maintain the slot/buffer mappings, which would
406 // become invalid for every buffer during detach/attach. By setting this to
407 // false, the valid GraphicBuffer pointer will always be sent with acquire
408 // for attached buffers.
409 mSlots[*outSlot].mAcquireCalled = false;
410
Pablo Ceballos9e314332016-01-12 13:49:19 -0800411 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700412
Dan Stoza9f3053d2014-03-06 15:14:33 -0800413 return NO_ERROR;
414}
415
Dan Stoza289ade12014-02-28 11:17:17 -0800416status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
417 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
418 EGLSyncKHR eglFence) {
419 ATRACE_CALL();
420 ATRACE_BUFFER_INDEX(slot);
421
Dan Stoza9f3053d2014-03-06 15:14:33 -0800422 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
423 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700424 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
425 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800426 return BAD_VALUE;
427 }
428
Dan Stozad1c10362014-03-28 15:19:08 -0700429 sp<IProducerListener> listener;
430 { // Autolock scope
431 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800432
Dan Stozad1c10362014-03-28 15:19:08 -0700433 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700434 // we can ignore this releaseBuffer for the old buffer.
435 // Ignore this for the shared buffer where the frame number can easily
436 // get out of sync due to the buffer being queued and acquired at the
437 // same time.
438 if (frameNumber != mSlots[slot].mFrameNumber &&
439 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700440 return STALE_BUFFER_SLOT;
441 }
Dan Stoza289ade12014-02-28 11:17:17 -0800442
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800443 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700444 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700445 "but its state was %s", slot,
446 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800447 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800448 }
Dan Stoza289ade12014-02-28 11:17:17 -0800449
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800450 mSlots[slot].mEglDisplay = eglDisplay;
451 mSlots[slot].mEglFence = eglFence;
452 mSlots[slot].mFence = releaseFence;
453 mSlots[slot].mBufferState.release();
454
455 // After leaving single buffer mode, the shared buffer will
456 // still be around. Mark it as no longer shared if this
457 // operation causes it to be free.
458 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
459 mSlots[slot].mBufferState.mShared = false;
460 }
461 // Don't put the shared buffer on the free list.
462 if (!mSlots[slot].mBufferState.isShared()) {
463 mCore->mActiveBuffers.erase(slot);
464 mCore->mFreeBuffers.push_back(slot);
465 }
466
467 listener = mCore->mConnectedProducerListener;
468 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
469
Dan Stozad1c10362014-03-28 15:19:08 -0700470 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800471 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700472 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800473
Dan Stozad1c10362014-03-28 15:19:08 -0700474 // Call back without lock held
475 if (listener != NULL) {
476 listener->onBufferReleased();
477 }
Dan Stoza289ade12014-02-28 11:17:17 -0800478
479 return NO_ERROR;
480}
481
482status_t BufferQueueConsumer::connect(
483 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
484 ATRACE_CALL();
485
486 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700487 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800488 return BAD_VALUE;
489 }
490
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700491 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800492 controlledByApp ? "true" : "false");
493
494 Mutex::Autolock lock(mCore->mMutex);
495
496 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700497 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800498 return NO_INIT;
499 }
500
501 mCore->mConsumerListener = consumerListener;
502 mCore->mConsumerControlledByApp = controlledByApp;
503
504 return NO_ERROR;
505}
506
507status_t BufferQueueConsumer::disconnect() {
508 ATRACE_CALL();
509
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700510 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800511
512 Mutex::Autolock lock(mCore->mMutex);
513
514 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700515 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800516 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800517 }
518
519 mCore->mIsAbandoned = true;
520 mCore->mConsumerListener = NULL;
521 mCore->mQueue.clear();
522 mCore->freeAllBuffersLocked();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800523 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800524 mCore->mDequeueCondition.broadcast();
525 return NO_ERROR;
526}
527
Dan Stozafebd4f42014-04-09 16:14:51 -0700528status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800529 ATRACE_CALL();
530
531 if (outSlotMask == NULL) {
532 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
533 return BAD_VALUE;
534 }
535
536 Mutex::Autolock lock(mCore->mMutex);
537
538 if (mCore->mIsAbandoned) {
539 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
540 return NO_INIT;
541 }
542
Dan Stozafebd4f42014-04-09 16:14:51 -0700543 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800544 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800545 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700546 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800547 }
548 }
549
550 // Remove from the mask queued buffers for which acquire has been called,
551 // since the consumer will not receive their buffer addresses and so must
552 // retain their cached information
553 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
554 while (current != mCore->mQueue.end()) {
555 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700556 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800557 }
558 ++current;
559 }
560
Dan Stozafebd4f42014-04-09 16:14:51 -0700561 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800562 *outSlotMask = mask;
563 return NO_ERROR;
564}
565
566status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
567 uint32_t height) {
568 ATRACE_CALL();
569
570 if (width == 0 || height == 0) {
571 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
572 "height=%u)", width, height);
573 return BAD_VALUE;
574 }
575
576 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
577
578 Mutex::Autolock lock(mCore->mMutex);
579 mCore->mDefaultWidth = width;
580 mCore->mDefaultHeight = height;
581 return NO_ERROR;
582}
583
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700584status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800585 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800586
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700587 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
588 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
589 return BAD_VALUE;
590 }
Dan Stoza289ade12014-02-28 11:17:17 -0800591
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800592 sp<IConsumerListener> listener;
593 {
594 Mutex::Autolock lock(mCore->mMutex);
595 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
596 BQ_LOGE("setMaxBufferCount: producer is already connected");
597 return INVALID_OPERATION;
598 }
Dan Stoza289ade12014-02-28 11:17:17 -0800599
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800600 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
601 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
602 "mMaxAcquiredBufferCount (%d)", bufferCount,
603 mCore->mMaxAcquiredBufferCount);
604 return BAD_VALUE;
605 }
606
607 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
608 mCore->mDequeueBufferCannotBlock, bufferCount) -
609 mCore->getMaxBufferCountLocked();
610 if (!mCore->adjustAvailableSlotsLocked(delta, nullptr)) {
611 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number"
612 " of available slots. Delta = %d", delta);
613 return BAD_VALUE;
614 }
615
616 mCore->mMaxBufferCount = bufferCount;
617 if (delta < 0) {
618 listener = mCore->mConsumerListener;
619 }
Dan Stoza289ade12014-02-28 11:17:17 -0800620 }
621
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800622 // Call back without lock held
623 if (listener != NULL) {
624 listener->onBuffersReleased();
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700625 }
Dan Stoza289ade12014-02-28 11:17:17 -0800626 return NO_ERROR;
627}
628
629status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
630 int maxAcquiredBuffers) {
631 ATRACE_CALL();
632
633 if (maxAcquiredBuffers < 1 ||
634 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
635 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
636 maxAcquiredBuffers);
637 return BAD_VALUE;
638 }
639
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800640 sp<IConsumerListener> consumerListener;
641 sp<IProducerListener> producerListener;
642 std::vector<int> freedSlots;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800643 { // Autolock scope
644 Mutex::Autolock lock(mCore->mMutex);
645 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800646
Pablo Ceballos72daab62015-12-07 16:38:43 -0800647 if (mCore->mIsAbandoned) {
648 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
649 return NO_INIT;
650 }
651
652 // The new maxAcquiredBuffers count should not be violated by the number
653 // of currently acquired buffers
654 int acquiredCount = 0;
655 for (int slot : mCore->mActiveBuffers) {
656 if (mSlots[slot].mBufferState.isAcquired()) {
657 acquiredCount++;
658 }
659 }
660 if (acquiredCount > maxAcquiredBuffers) {
661 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
662 "count (%d) exceeds the current acquired buffer count (%d)",
663 maxAcquiredBuffers, acquiredCount);
664 return BAD_VALUE;
665 }
666
667 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
668 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
669 > mCore->mMaxBufferCount) {
670 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
671 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
672 maxAcquiredBuffers, mCore->mMaxBufferCount,
673 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
674 mCore->mDequeueBufferCannotBlock);
675 return BAD_VALUE;
676 }
677
678 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800679 if (!mCore->adjustAvailableSlotsLocked(delta, &freedSlots)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800680 return BAD_VALUE;
681 }
682
683 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
684 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
685 VALIDATE_CONSISTENCY();
686 if (delta < 0) {
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800687 consumerListener = mCore->mConsumerListener;
688 producerListener = mCore->mConnectedProducerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800689 }
690 }
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800691
Pablo Ceballos72daab62015-12-07 16:38:43 -0800692 // Call back without lock held
Pablo Ceballos16c9c302016-02-18 15:25:36 -0800693 if (consumerListener != NULL) {
694 consumerListener->onBuffersReleased();
695 }
696 if (producerListener != NULL) {
697 for (int i : freedSlots) {
698 producerListener->onSlotFreed(i);
699 }
Dan Stoza289ade12014-02-28 11:17:17 -0800700 }
701
Dan Stoza289ade12014-02-28 11:17:17 -0800702 return NO_ERROR;
703}
704
705void BufferQueueConsumer::setConsumerName(const String8& name) {
706 ATRACE_CALL();
707 BQ_LOGV("setConsumerName: '%s'", name.string());
708 Mutex::Autolock lock(mCore->mMutex);
709 mCore->mConsumerName = name;
710 mConsumerName = name;
711}
712
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800713status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800714 ATRACE_CALL();
715 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
716 Mutex::Autolock lock(mCore->mMutex);
717 mCore->mDefaultBufferFormat = defaultFormat;
718 return NO_ERROR;
719}
720
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800721status_t BufferQueueConsumer::setDefaultBufferDataSpace(
722 android_dataspace defaultDataSpace) {
723 ATRACE_CALL();
724 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
725 Mutex::Autolock lock(mCore->mMutex);
726 mCore->mDefaultBufferDataSpace = defaultDataSpace;
727 return NO_ERROR;
728}
729
Dan Stoza289ade12014-02-28 11:17:17 -0800730status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
731 ATRACE_CALL();
732 BQ_LOGV("setConsumerUsageBits: %#x", usage);
733 Mutex::Autolock lock(mCore->mMutex);
734 mCore->mConsumerUsageBits = usage;
735 return NO_ERROR;
736}
737
738status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
739 ATRACE_CALL();
740 BQ_LOGV("setTransformHint: %#x", hint);
741 Mutex::Autolock lock(mCore->mMutex);
742 mCore->mTransformHint = hint;
743 return NO_ERROR;
744}
745
Jesse Hall399184a2014-03-03 15:42:54 -0800746sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
747 return mCore->mSidebandStream;
748}
749
Dan Stoza289ade12014-02-28 11:17:17 -0800750void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
751 mCore->dump(result, prefix);
752}
753
754} // namespace android