blob: d182f6b7883447816dcfc1ce59f7d05ffec1b8bd [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
Dan Stoza289ade12014-02-28 11:17:17 -080023#include <gui/BufferItem.h>
24#include <gui/BufferQueueConsumer.h>
25#include <gui/BufferQueueCore.h>
26#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070027#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080028
29namespace android {
30
31BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
32 mCore(core),
33 mSlots(core->mSlots),
34 mConsumerName() {}
35
36BufferQueueConsumer::~BufferQueueConsumer() {}
37
38status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070039 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080040 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080041
Lajos Molnar5f920c12015-07-13 16:04:24 -070042 int numDroppedBuffers = 0;
43 sp<IProducerListener> listener;
44 {
45 Mutex::Autolock lock(mCore->mMutex);
46
47 // Check that the consumer doesn't currently have the maximum number of
48 // buffers acquired. We allow the max buffer count to be exceeded by one
49 // buffer so that the consumer can successfully set up the newly acquired
50 // buffer before releasing the old one.
51 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080052 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070053 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070054 ++numAcquiredBuffers;
55 }
Dan Stoza289ade12014-02-28 11:17:17 -080056 }
Lajos Molnar5f920c12015-07-13 16:04:24 -070057 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
58 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
59 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
60 return INVALID_OPERATION;
61 }
Dan Stoza289ade12014-02-28 11:17:17 -080062
Pablo Ceballosccdfd602015-10-07 15:05:45 -070063 bool sharedBufferAvailable = mCore->mSingleBufferMode &&
64 mCore->mSingleBufferSlot !=
65 BufferQueueCore::INVALID_BUFFER_SLOT;
66
Lajos Molnar5f920c12015-07-13 16:04:24 -070067 // In asynchronous mode the list is guaranteed to be one buffer deep,
68 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070069 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070070 return NO_BUFFER_AVAILABLE;
71 }
Dan Stoza289ade12014-02-28 11:17:17 -080072
Lajos Molnar5f920c12015-07-13 16:04:24 -070073 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080074
Lajos Molnar5f920c12015-07-13 16:04:24 -070075 // If expectedPresent is specified, we may not want to return a buffer yet.
76 // If it's specified and there's more than one buffer queued, we may want
77 // to drop a buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070078 // Skip this if we're in single buffer mode and the queue is empty,
79 // since in that case we'll just return the shared buffer.
80 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070081 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080082
Lajos Molnar5f920c12015-07-13 16:04:24 -070083 // The 'expectedPresent' argument indicates when the buffer is expected
84 // to be presented on-screen. If the buffer's desired present time is
85 // earlier (less) than expectedPresent -- meaning it will be displayed
86 // on time or possibly late if we show it as soon as possible -- we
87 // acquire and return it. If we don't want to display it until after the
88 // expectedPresent time, we return PRESENT_LATER without acquiring it.
89 //
90 // To be safe, we don't defer acquisition if expectedPresent is more
91 // than one second in the future beyond the desired present time
92 // (i.e., we'd be holding the buffer for a long time).
93 //
94 // NOTE: Code assumes monotonic time values from the system clock
95 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -080096
Lajos Molnar5f920c12015-07-13 16:04:24 -070097 // Start by checking to see if we can drop frames. We skip this check if
98 // the timestamps are being auto-generated by Surface. If the app isn't
99 // generating timestamps explicitly, it probably doesn't want frames to
100 // be discarded based on them.
101 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
102 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700103
Lajos Molnar5f920c12015-07-13 16:04:24 -0700104 // If dropping entry[0] would leave us with a buffer that the
105 // consumer is not yet ready for, don't drop it.
106 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
107 break;
108 }
109
110 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
111 // additional criterion here: we only drop the earlier buffer if our
112 // desiredPresent falls within +/- 1 second of the expected present.
113 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
114 // relative timestamp), which normally mean "ignore the timestamp
115 // and acquire immediately", would cause us to drop frames.
116 //
117 // We may want to add an additional criterion: don't drop the
118 // earlier buffer if entry[1]'s fence hasn't signaled yet.
119 nsecs_t desiredPresent = bufferItem.mTimestamp;
120 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
121 desiredPresent > expectedPresent) {
122 // This buffer is set to display in the near future, or
123 // desiredPresent is garbage. Either way we don't want to drop
124 // the previous buffer just to get this on the screen sooner.
125 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
126 PRId64 " (%" PRId64 ") now=%" PRId64,
127 desiredPresent, expectedPresent,
128 desiredPresent - expectedPresent,
129 systemTime(CLOCK_MONOTONIC));
130 break;
131 }
132
133 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
134 " size=%zu",
135 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800136
137 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700138 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700139 mSlots[front->mSlot].mBufferState.freeQueued();
140
141 // After leaving single buffer mode, the shared buffer will
142 // still be around. Mark it as no longer shared if this
143 // operation causes it to be free.
144 if (!mCore->mSingleBufferMode &&
145 mSlots[front->mSlot].mBufferState.isFree()) {
146 mSlots[front->mSlot].mBufferState.mShared = false;
147 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800148
149 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700150 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800151 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700152 mCore->mFreeBuffers.push_back(front->mSlot);
153 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800154
Lajos Molnar5f920c12015-07-13 16:04:24 -0700155 listener = mCore->mConnectedProducerListener;
156 ++numDroppedBuffers;
157 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800158
Lajos Molnar5f920c12015-07-13 16:04:24 -0700159 mCore->mQueue.erase(front);
160 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700161 }
162
Lajos Molnar5f920c12015-07-13 16:04:24 -0700163 // See if the front buffer is ready to be acquired
164 nsecs_t desiredPresent = front->mTimestamp;
165 bool bufferIsDue = desiredPresent <= expectedPresent ||
166 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
167 bool consumerIsReady = maxFrameNumber > 0 ?
168 front->mFrameNumber <= maxFrameNumber : true;
169 if (!bufferIsDue || !consumerIsReady) {
170 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
171 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
172 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700173 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800174 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700175 systemTime(CLOCK_MONOTONIC),
176 front->mFrameNumber, maxFrameNumber);
177 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800178 }
179
Lajos Molnar5f920c12015-07-13 16:04:24 -0700180 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
181 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800182 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700183 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800184 }
185
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700186 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
187
188 if (sharedBufferAvailable && mCore->mQueue.empty()) {
189 // make sure the buffer has finished allocating before acquiring it
190 mCore->waitWhileAllocatingLocked();
191
192 slot = mCore->mSingleBufferSlot;
193
194 // Recreate the BufferItem for the shared buffer from the data that
195 // was cached when it was last queued.
196 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
197 outBuffer->mFence = Fence::NO_FENCE;
198 outBuffer->mCrop = mCore->mSingleBufferCache.crop;
199 outBuffer->mTransform = mCore->mSingleBufferCache.transform &
200 ~static_cast<uint32_t>(
201 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
202 outBuffer->mScalingMode = mCore->mSingleBufferCache.scalingMode;
203 outBuffer->mDataSpace = mCore->mSingleBufferCache.dataspace;
204 outBuffer->mFrameNumber = mCore->mFrameCounter;
205 outBuffer->mSlot = slot;
206 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
207 outBuffer->mTransformToDisplayInverse =
208 (mCore->mSingleBufferCache.transform &
209 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
210 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700211 outBuffer->mSingleBufferMode = true;
212 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800213 outBuffer->mIsStale = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700214 } else {
215 slot = front->mSlot;
216 *outBuffer = *front;
217 }
218
Pablo Ceballos06312182015-10-07 16:32:12 -0700219 outBuffer->mSingleBufferMode = mCore->mSingleBufferMode;
220
Lajos Molnar5f920c12015-07-13 16:04:24 -0700221 ATRACE_BUFFER_INDEX(slot);
222
223 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700224 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800225
226 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700227 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700228 // Don't decrease the queue count if the BufferItem wasn't
229 // previously in the queue. This happens in single buffer mode when
230 // the queue is empty and the BufferItem is created above.
231 if (mCore->mQueue.empty()) {
232 mSlots[slot].mBufferState.acquireNotInQueue();
233 } else {
234 mSlots[slot].mBufferState.acquire();
235 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700236 mSlots[slot].mFence = Fence::NO_FENCE;
237 }
238
239 // If the buffer has previously been acquired by the consumer, set
240 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
241 // on the consumer side
242 if (outBuffer->mAcquireCalled) {
243 outBuffer->mGraphicBuffer = NULL;
244 }
245
246 mCore->mQueue.erase(front);
247
248 // We might have freed a slot while dropping old buffers, or the producer
249 // may be blocked waiting for the number of buffers in the queue to
250 // decrease.
251 mCore->mDequeueCondition.broadcast();
252
253 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
254
255 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800256 }
257
Lajos Molnar5f920c12015-07-13 16:04:24 -0700258 if (listener != NULL) {
259 for (int i = 0; i < numDroppedBuffers; ++i) {
260 listener->onBufferReleased();
261 }
Dan Stoza289ade12014-02-28 11:17:17 -0800262 }
263
Dan Stoza289ade12014-02-28 11:17:17 -0800264 return NO_ERROR;
265}
266
Dan Stoza9f3053d2014-03-06 15:14:33 -0800267status_t BufferQueueConsumer::detachBuffer(int slot) {
268 ATRACE_CALL();
269 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700270 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800271 Mutex::Autolock lock(mCore->mMutex);
272
273 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700274 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800275 return NO_INIT;
276 }
277
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800278 if (mCore->mSingleBufferMode || slot == mCore->mSingleBufferSlot) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700279 BQ_LOGE("detachBuffer: detachBuffer not allowed in single buffer"
280 "mode");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800281 return BAD_VALUE;
282 }
283
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700284 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
285 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
286 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
287 return BAD_VALUE;
288 } else if (!mSlots[slot].mBufferState.isAcquired()) {
289 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
290 "(state = %s)", slot, mSlots[slot].mBufferState.string());
291 return BAD_VALUE;
292 }
293
294 mSlots[slot].mBufferState.detachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800295 mCore->mActiveBuffers.erase(slot);
296 mCore->mFreeSlots.insert(slot);
297 mCore->clearBufferSlotLocked(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800298 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700299 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800300
301 return NO_ERROR;
302}
303
304status_t BufferQueueConsumer::attachBuffer(int* outSlot,
305 const sp<android::GraphicBuffer>& buffer) {
306 ATRACE_CALL();
307
308 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700309 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800310 return BAD_VALUE;
311 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700312 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800313 return BAD_VALUE;
314 }
315
316 Mutex::Autolock lock(mCore->mMutex);
317
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700318 if (mCore->mSingleBufferMode) {
319 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer"
320 "mode");
321 return BAD_VALUE;
322 }
323
Dan Stoza0de7ea72015-04-23 13:20:51 -0700324 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800326 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700327 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800328 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800329 }
330 }
331
332 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700333 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 "(max %d)", numAcquiredBuffers,
335 mCore->mMaxAcquiredBufferCount);
336 return INVALID_OPERATION;
337 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700338
Dan Stoza812ed062015-06-02 15:45:22 -0700339 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
340 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
341 "[queue %u]", buffer->getGenerationNumber(),
342 mCore->mGenerationNumber);
343 return BAD_VALUE;
344 }
345
Dan Stoza0de7ea72015-04-23 13:20:51 -0700346 // Find a free slot to put the buffer into
347 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
348 if (!mCore->mFreeSlots.empty()) {
349 auto slot = mCore->mFreeSlots.begin();
350 found = *slot;
351 mCore->mFreeSlots.erase(slot);
352 } else if (!mCore->mFreeBuffers.empty()) {
353 found = mCore->mFreeBuffers.front();
354 mCore->mFreeBuffers.remove(found);
355 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800356 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700357 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800358 return NO_MEMORY;
359 }
360
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800361 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800362 *outSlot = found;
363 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700364 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800365
366 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700367 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800368 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 mSlots[*outSlot].mFence = Fence::NO_FENCE;
370 mSlots[*outSlot].mFrameNumber = 0;
371
Dan Stoza99b18b42014-03-28 15:34:33 -0700372 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
373 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
374 // Binder traffic by not un/flattening the GraphicBuffer. However, it
375 // requires that the consumer maintain a cached copy of the slot <--> buffer
376 // mappings, which is why the consumer doesn't need the valid pointer on
377 // acquire.
378 //
379 // The StreamSplitter is one of the primary users of the attach/detach
380 // logic, and while it is running, all buffers it acquires are immediately
381 // detached, and all buffers it eventually releases are ones that were
382 // attached (as opposed to having been obtained from acquireBuffer), so it
383 // doesn't make sense to maintain the slot/buffer mappings, which would
384 // become invalid for every buffer during detach/attach. By setting this to
385 // false, the valid GraphicBuffer pointer will always be sent with acquire
386 // for attached buffers.
387 mSlots[*outSlot].mAcquireCalled = false;
388
Dan Stoza0de7ea72015-04-23 13:20:51 -0700389 mCore->validateConsistencyLocked();
390
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391 return NO_ERROR;
392}
393
Dan Stoza289ade12014-02-28 11:17:17 -0800394status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
395 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
396 EGLSyncKHR eglFence) {
397 ATRACE_CALL();
398 ATRACE_BUFFER_INDEX(slot);
399
Dan Stoza9f3053d2014-03-06 15:14:33 -0800400 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
401 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700402 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
403 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800404 return BAD_VALUE;
405 }
406
Dan Stozad1c10362014-03-28 15:19:08 -0700407 sp<IProducerListener> listener;
408 { // Autolock scope
409 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800410
Dan Stozad1c10362014-03-28 15:19:08 -0700411 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700412 // we can ignore this releaseBuffer for the old buffer.
413 // Ignore this for the shared buffer where the frame number can easily
414 // get out of sync due to the buffer being queued and acquired at the
415 // same time.
416 if (frameNumber != mSlots[slot].mFrameNumber &&
417 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700418 return STALE_BUFFER_SLOT;
419 }
Dan Stoza289ade12014-02-28 11:17:17 -0800420
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800421 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700422 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700423 "but its state was %s", slot,
424 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800425 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800426 }
Dan Stoza289ade12014-02-28 11:17:17 -0800427
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800428 mSlots[slot].mEglDisplay = eglDisplay;
429 mSlots[slot].mEglFence = eglFence;
430 mSlots[slot].mFence = releaseFence;
431 mSlots[slot].mBufferState.release();
432
433 // After leaving single buffer mode, the shared buffer will
434 // still be around. Mark it as no longer shared if this
435 // operation causes it to be free.
436 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
437 mSlots[slot].mBufferState.mShared = false;
438 }
439 // Don't put the shared buffer on the free list.
440 if (!mSlots[slot].mBufferState.isShared()) {
441 mCore->mActiveBuffers.erase(slot);
442 mCore->mFreeBuffers.push_back(slot);
443 }
444
445 listener = mCore->mConnectedProducerListener;
446 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
447
Dan Stozad1c10362014-03-28 15:19:08 -0700448 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700449 mCore->validateConsistencyLocked();
Dan Stozad1c10362014-03-28 15:19:08 -0700450 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800451
Dan Stozad1c10362014-03-28 15:19:08 -0700452 // Call back without lock held
453 if (listener != NULL) {
454 listener->onBufferReleased();
455 }
Dan Stoza289ade12014-02-28 11:17:17 -0800456
457 return NO_ERROR;
458}
459
460status_t BufferQueueConsumer::connect(
461 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
462 ATRACE_CALL();
463
464 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700465 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800466 return BAD_VALUE;
467 }
468
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700469 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800470 controlledByApp ? "true" : "false");
471
472 Mutex::Autolock lock(mCore->mMutex);
473
474 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700475 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800476 return NO_INIT;
477 }
478
479 mCore->mConsumerListener = consumerListener;
480 mCore->mConsumerControlledByApp = controlledByApp;
481
482 return NO_ERROR;
483}
484
485status_t BufferQueueConsumer::disconnect() {
486 ATRACE_CALL();
487
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700488 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800489
490 Mutex::Autolock lock(mCore->mMutex);
491
492 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700493 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800494 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800495 }
496
497 mCore->mIsAbandoned = true;
498 mCore->mConsumerListener = NULL;
499 mCore->mQueue.clear();
500 mCore->freeAllBuffersLocked();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800501 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800502 mCore->mDequeueCondition.broadcast();
503 return NO_ERROR;
504}
505
Dan Stozafebd4f42014-04-09 16:14:51 -0700506status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800507 ATRACE_CALL();
508
509 if (outSlotMask == NULL) {
510 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
511 return BAD_VALUE;
512 }
513
514 Mutex::Autolock lock(mCore->mMutex);
515
516 if (mCore->mIsAbandoned) {
517 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
518 return NO_INIT;
519 }
520
Dan Stozafebd4f42014-04-09 16:14:51 -0700521 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800522 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800523 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700524 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800525 }
526 }
527
528 // Remove from the mask queued buffers for which acquire has been called,
529 // since the consumer will not receive their buffer addresses and so must
530 // retain their cached information
531 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
532 while (current != mCore->mQueue.end()) {
533 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700534 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800535 }
536 ++current;
537 }
538
Dan Stozafebd4f42014-04-09 16:14:51 -0700539 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800540 *outSlotMask = mask;
541 return NO_ERROR;
542}
543
544status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
545 uint32_t height) {
546 ATRACE_CALL();
547
548 if (width == 0 || height == 0) {
549 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
550 "height=%u)", width, height);
551 return BAD_VALUE;
552 }
553
554 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
555
556 Mutex::Autolock lock(mCore->mMutex);
557 mCore->mDefaultWidth = width;
558 mCore->mDefaultHeight = height;
559 return NO_ERROR;
560}
561
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700562status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800563 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800564
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700565 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
566 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
567 return BAD_VALUE;
568 }
Dan Stoza289ade12014-02-28 11:17:17 -0800569
570 Mutex::Autolock lock(mCore->mMutex);
571
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700572 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
573 BQ_LOGE("setMaxBufferCount: producer is already connected");
Dan Stoza289ade12014-02-28 11:17:17 -0800574 return INVALID_OPERATION;
575 }
576
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700577 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
578 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
579 "mMaxAcquiredBufferCount (%d)", bufferCount,
580 mCore->mMaxAcquiredBufferCount);
581 return BAD_VALUE;
582 }
583
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800584 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
585 mCore->mDequeueBufferCannotBlock, bufferCount) -
586 mCore->getMaxBufferCountLocked();
587 if (!mCore->adjustAvailableSlotsLocked(delta)) {
588 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
589 "available slots. Delta = %d", delta);
590 return BAD_VALUE;
591 }
592
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700593 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800594 return NO_ERROR;
595}
596
597status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
598 int maxAcquiredBuffers) {
599 ATRACE_CALL();
600
601 if (maxAcquiredBuffers < 1 ||
602 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
603 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
604 maxAcquiredBuffers);
605 return BAD_VALUE;
606 }
607
608 Mutex::Autolock lock(mCore->mMutex);
609
610 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
611 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
612 return INVALID_OPERATION;
613 }
614
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700615 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700616 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
617 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700618 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would exceed "
619 "the maxBufferCount (%d) (maxDequeued %d async %d)",
620 maxAcquiredBuffers, mCore->mMaxBufferCount,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700621 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
622 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700623 return BAD_VALUE;
624 }
625
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800626 if (!mCore->adjustAvailableSlotsLocked(
627 maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount)) {
628 BQ_LOGE("setMaxAcquiredBufferCount: BufferQueue failed to adjust the "
629 "number of available slots. Delta = %d",
630 maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount);
631 return BAD_VALUE;
632 }
633
Dan Stoza289ade12014-02-28 11:17:17 -0800634 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
635 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800636 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800637 return NO_ERROR;
638}
639
640void BufferQueueConsumer::setConsumerName(const String8& name) {
641 ATRACE_CALL();
642 BQ_LOGV("setConsumerName: '%s'", name.string());
643 Mutex::Autolock lock(mCore->mMutex);
644 mCore->mConsumerName = name;
645 mConsumerName = name;
646}
647
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800648status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800649 ATRACE_CALL();
650 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
651 Mutex::Autolock lock(mCore->mMutex);
652 mCore->mDefaultBufferFormat = defaultFormat;
653 return NO_ERROR;
654}
655
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800656status_t BufferQueueConsumer::setDefaultBufferDataSpace(
657 android_dataspace defaultDataSpace) {
658 ATRACE_CALL();
659 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
660 Mutex::Autolock lock(mCore->mMutex);
661 mCore->mDefaultBufferDataSpace = defaultDataSpace;
662 return NO_ERROR;
663}
664
Dan Stoza289ade12014-02-28 11:17:17 -0800665status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
666 ATRACE_CALL();
667 BQ_LOGV("setConsumerUsageBits: %#x", usage);
668 Mutex::Autolock lock(mCore->mMutex);
669 mCore->mConsumerUsageBits = usage;
670 return NO_ERROR;
671}
672
673status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
674 ATRACE_CALL();
675 BQ_LOGV("setTransformHint: %#x", hint);
676 Mutex::Autolock lock(mCore->mMutex);
677 mCore->mTransformHint = hint;
678 return NO_ERROR;
679}
680
Jesse Hall399184a2014-03-03 15:42:54 -0800681sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
682 return mCore->mSidebandStream;
683}
684
Dan Stoza289ade12014-02-28 11:17:17 -0800685void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
686 mCore->dump(result, prefix);
687}
688
689} // namespace android