blob: 9c1bbe4211721d9880a74f1cfaf2bfd9cbf297b8 [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 Mutex::Autolock lock(mCore->mMutex);
278
279 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700280 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800281 return NO_INIT;
282 }
283
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800284 if (mCore->mSingleBufferMode || slot == mCore->mSingleBufferSlot) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700285 BQ_LOGE("detachBuffer: detachBuffer not allowed in single buffer"
286 "mode");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800287 return BAD_VALUE;
288 }
289
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700290 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
291 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
292 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
293 return BAD_VALUE;
294 } else if (!mSlots[slot].mBufferState.isAcquired()) {
295 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
296 "(state = %s)", slot, mSlots[slot].mBufferState.string());
297 return BAD_VALUE;
298 }
299
300 mSlots[slot].mBufferState.detachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 mCore->mActiveBuffers.erase(slot);
302 mCore->mFreeSlots.insert(slot);
303 mCore->clearBufferSlotLocked(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800304 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800305 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800306
307 return NO_ERROR;
308}
309
310status_t BufferQueueConsumer::attachBuffer(int* outSlot,
311 const sp<android::GraphicBuffer>& buffer) {
312 ATRACE_CALL();
313
314 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700315 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316 return BAD_VALUE;
317 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700318 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800319 return BAD_VALUE;
320 }
321
322 Mutex::Autolock lock(mCore->mMutex);
323
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700324 if (mCore->mSingleBufferMode) {
325 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer"
326 "mode");
327 return BAD_VALUE;
328 }
329
Dan Stoza0de7ea72015-04-23 13:20:51 -0700330 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800331 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800332 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700333 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800335 }
336 }
337
338 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700339 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800340 "(max %d)", numAcquiredBuffers,
341 mCore->mMaxAcquiredBufferCount);
342 return INVALID_OPERATION;
343 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700344
Dan Stoza812ed062015-06-02 15:45:22 -0700345 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
346 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
347 "[queue %u]", buffer->getGenerationNumber(),
348 mCore->mGenerationNumber);
349 return BAD_VALUE;
350 }
351
Dan Stoza0de7ea72015-04-23 13:20:51 -0700352 // Find a free slot to put the buffer into
353 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
354 if (!mCore->mFreeSlots.empty()) {
355 auto slot = mCore->mFreeSlots.begin();
356 found = *slot;
357 mCore->mFreeSlots.erase(slot);
358 } else if (!mCore->mFreeBuffers.empty()) {
359 found = mCore->mFreeBuffers.front();
360 mCore->mFreeBuffers.remove(found);
361 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800362 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700363 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800364 return NO_MEMORY;
365 }
366
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800367 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 *outSlot = found;
369 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700370 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800371
372 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700373 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800374 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800375 mSlots[*outSlot].mFence = Fence::NO_FENCE;
376 mSlots[*outSlot].mFrameNumber = 0;
377
Dan Stoza99b18b42014-03-28 15:34:33 -0700378 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
379 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
380 // Binder traffic by not un/flattening the GraphicBuffer. However, it
381 // requires that the consumer maintain a cached copy of the slot <--> buffer
382 // mappings, which is why the consumer doesn't need the valid pointer on
383 // acquire.
384 //
385 // The StreamSplitter is one of the primary users of the attach/detach
386 // logic, and while it is running, all buffers it acquires are immediately
387 // detached, and all buffers it eventually releases are ones that were
388 // attached (as opposed to having been obtained from acquireBuffer), so it
389 // doesn't make sense to maintain the slot/buffer mappings, which would
390 // become invalid for every buffer during detach/attach. By setting this to
391 // false, the valid GraphicBuffer pointer will always be sent with acquire
392 // for attached buffers.
393 mSlots[*outSlot].mAcquireCalled = false;
394
Pablo Ceballos9e314332016-01-12 13:49:19 -0800395 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700396
Dan Stoza9f3053d2014-03-06 15:14:33 -0800397 return NO_ERROR;
398}
399
Dan Stoza289ade12014-02-28 11:17:17 -0800400status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
401 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
402 EGLSyncKHR eglFence) {
403 ATRACE_CALL();
404 ATRACE_BUFFER_INDEX(slot);
405
Dan Stoza9f3053d2014-03-06 15:14:33 -0800406 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
407 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700408 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
409 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800410 return BAD_VALUE;
411 }
412
Dan Stozad1c10362014-03-28 15:19:08 -0700413 sp<IProducerListener> listener;
414 { // Autolock scope
415 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800416
Dan Stozad1c10362014-03-28 15:19:08 -0700417 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700418 // we can ignore this releaseBuffer for the old buffer.
419 // Ignore this for the shared buffer where the frame number can easily
420 // get out of sync due to the buffer being queued and acquired at the
421 // same time.
422 if (frameNumber != mSlots[slot].mFrameNumber &&
423 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700424 return STALE_BUFFER_SLOT;
425 }
Dan Stoza289ade12014-02-28 11:17:17 -0800426
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800427 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700428 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700429 "but its state was %s", slot,
430 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800431 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800432 }
Dan Stoza289ade12014-02-28 11:17:17 -0800433
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800434 mSlots[slot].mEglDisplay = eglDisplay;
435 mSlots[slot].mEglFence = eglFence;
436 mSlots[slot].mFence = releaseFence;
437 mSlots[slot].mBufferState.release();
438
439 // After leaving single buffer mode, the shared buffer will
440 // still be around. Mark it as no longer shared if this
441 // operation causes it to be free.
442 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
443 mSlots[slot].mBufferState.mShared = false;
444 }
445 // Don't put the shared buffer on the free list.
446 if (!mSlots[slot].mBufferState.isShared()) {
447 mCore->mActiveBuffers.erase(slot);
448 mCore->mFreeBuffers.push_back(slot);
449 }
450
451 listener = mCore->mConnectedProducerListener;
452 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
453
Dan Stozad1c10362014-03-28 15:19:08 -0700454 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800455 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700456 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800457
Dan Stozad1c10362014-03-28 15:19:08 -0700458 // Call back without lock held
459 if (listener != NULL) {
460 listener->onBufferReleased();
461 }
Dan Stoza289ade12014-02-28 11:17:17 -0800462
463 return NO_ERROR;
464}
465
466status_t BufferQueueConsumer::connect(
467 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
468 ATRACE_CALL();
469
470 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700471 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800472 return BAD_VALUE;
473 }
474
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700475 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800476 controlledByApp ? "true" : "false");
477
478 Mutex::Autolock lock(mCore->mMutex);
479
480 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700481 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800482 return NO_INIT;
483 }
484
485 mCore->mConsumerListener = consumerListener;
486 mCore->mConsumerControlledByApp = controlledByApp;
487
488 return NO_ERROR;
489}
490
491status_t BufferQueueConsumer::disconnect() {
492 ATRACE_CALL();
493
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700494 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800495
496 Mutex::Autolock lock(mCore->mMutex);
497
498 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700499 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800500 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800501 }
502
503 mCore->mIsAbandoned = true;
504 mCore->mConsumerListener = NULL;
505 mCore->mQueue.clear();
506 mCore->freeAllBuffersLocked();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800507 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800508 mCore->mDequeueCondition.broadcast();
509 return NO_ERROR;
510}
511
Dan Stozafebd4f42014-04-09 16:14:51 -0700512status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800513 ATRACE_CALL();
514
515 if (outSlotMask == NULL) {
516 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
517 return BAD_VALUE;
518 }
519
520 Mutex::Autolock lock(mCore->mMutex);
521
522 if (mCore->mIsAbandoned) {
523 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
524 return NO_INIT;
525 }
526
Dan Stozafebd4f42014-04-09 16:14:51 -0700527 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800528 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800529 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700530 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800531 }
532 }
533
534 // Remove from the mask queued buffers for which acquire has been called,
535 // since the consumer will not receive their buffer addresses and so must
536 // retain their cached information
537 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
538 while (current != mCore->mQueue.end()) {
539 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700540 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800541 }
542 ++current;
543 }
544
Dan Stozafebd4f42014-04-09 16:14:51 -0700545 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800546 *outSlotMask = mask;
547 return NO_ERROR;
548}
549
550status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
551 uint32_t height) {
552 ATRACE_CALL();
553
554 if (width == 0 || height == 0) {
555 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
556 "height=%u)", width, height);
557 return BAD_VALUE;
558 }
559
560 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
561
562 Mutex::Autolock lock(mCore->mMutex);
563 mCore->mDefaultWidth = width;
564 mCore->mDefaultHeight = height;
565 return NO_ERROR;
566}
567
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700568status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800569 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800570
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700571 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
572 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
573 return BAD_VALUE;
574 }
Dan Stoza289ade12014-02-28 11:17:17 -0800575
576 Mutex::Autolock lock(mCore->mMutex);
577
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700578 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
579 BQ_LOGE("setMaxBufferCount: producer is already connected");
Dan Stoza289ade12014-02-28 11:17:17 -0800580 return INVALID_OPERATION;
581 }
582
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700583 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
584 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
585 "mMaxAcquiredBufferCount (%d)", bufferCount,
586 mCore->mMaxAcquiredBufferCount);
587 return BAD_VALUE;
588 }
589
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800590 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
591 mCore->mDequeueBufferCannotBlock, bufferCount) -
592 mCore->getMaxBufferCountLocked();
593 if (!mCore->adjustAvailableSlotsLocked(delta)) {
594 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
595 "available slots. Delta = %d", delta);
596 return BAD_VALUE;
597 }
598
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700599 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800600 return NO_ERROR;
601}
602
603status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
604 int maxAcquiredBuffers) {
605 ATRACE_CALL();
606
607 if (maxAcquiredBuffers < 1 ||
608 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
609 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
610 maxAcquiredBuffers);
611 return BAD_VALUE;
612 }
613
614 Mutex::Autolock lock(mCore->mMutex);
615
616 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
617 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
618 return INVALID_OPERATION;
619 }
620
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700621 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700622 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
623 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700624 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would exceed "
625 "the maxBufferCount (%d) (maxDequeued %d async %d)",
626 maxAcquiredBuffers, mCore->mMaxBufferCount,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700627 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
628 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700629 return BAD_VALUE;
630 }
631
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800632 if (!mCore->adjustAvailableSlotsLocked(
633 maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount)) {
634 BQ_LOGE("setMaxAcquiredBufferCount: BufferQueue failed to adjust the "
635 "number of available slots. Delta = %d",
636 maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount);
637 return BAD_VALUE;
638 }
639
Dan Stoza289ade12014-02-28 11:17:17 -0800640 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
641 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800642 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800643 return NO_ERROR;
644}
645
646void BufferQueueConsumer::setConsumerName(const String8& name) {
647 ATRACE_CALL();
648 BQ_LOGV("setConsumerName: '%s'", name.string());
649 Mutex::Autolock lock(mCore->mMutex);
650 mCore->mConsumerName = name;
651 mConsumerName = name;
652}
653
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800654status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800655 ATRACE_CALL();
656 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
657 Mutex::Autolock lock(mCore->mMutex);
658 mCore->mDefaultBufferFormat = defaultFormat;
659 return NO_ERROR;
660}
661
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800662status_t BufferQueueConsumer::setDefaultBufferDataSpace(
663 android_dataspace defaultDataSpace) {
664 ATRACE_CALL();
665 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
666 Mutex::Autolock lock(mCore->mMutex);
667 mCore->mDefaultBufferDataSpace = defaultDataSpace;
668 return NO_ERROR;
669}
670
Dan Stoza289ade12014-02-28 11:17:17 -0800671status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
672 ATRACE_CALL();
673 BQ_LOGV("setConsumerUsageBits: %#x", usage);
674 Mutex::Autolock lock(mCore->mMutex);
675 mCore->mConsumerUsageBits = usage;
676 return NO_ERROR;
677}
678
679status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
680 ATRACE_CALL();
681 BQ_LOGV("setTransformHint: %#x", hint);
682 Mutex::Autolock lock(mCore->mMutex);
683 mCore->mTransformHint = hint;
684 return NO_ERROR;
685}
686
Jesse Hall399184a2014-03-03 15:42:54 -0800687sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
688 return mCore->mSidebandStream;
689}
690
Dan Stoza289ade12014-02-28 11:17:17 -0800691void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
692 mCore->dump(result, prefix);
693}
694
695} // namespace android