blob: 6f9f21f2966690cff5deb7a9fb9ad020b7a9e1eb [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;
52 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
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());
136 if (mCore->stillTracking(front)) {
137 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700138 mSlots[front->mSlot].mBufferState.freeQueued();
139
140 // After leaving single buffer mode, the shared buffer will
141 // still be around. Mark it as no longer shared if this
142 // operation causes it to be free.
143 if (!mCore->mSingleBufferMode &&
144 mSlots[front->mSlot].mBufferState.isFree()) {
145 mSlots[front->mSlot].mBufferState.mShared = false;
146 }
147 // Don't put the shared buffer on the free list.
148 if (!mSlots[front->mSlot].mBufferState.isShared()) {
149 mCore->mFreeBuffers.push_back(front->mSlot);
150 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700151 listener = mCore->mConnectedProducerListener;
152 ++numDroppedBuffers;
153 }
154 mCore->mQueue.erase(front);
155 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700156 }
157
Lajos Molnar5f920c12015-07-13 16:04:24 -0700158 // See if the front buffer is ready to be acquired
159 nsecs_t desiredPresent = front->mTimestamp;
160 bool bufferIsDue = desiredPresent <= expectedPresent ||
161 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
162 bool consumerIsReady = maxFrameNumber > 0 ?
163 front->mFrameNumber <= maxFrameNumber : true;
164 if (!bufferIsDue || !consumerIsReady) {
165 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
166 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
167 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700168 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800169 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700170 systemTime(CLOCK_MONOTONIC),
171 front->mFrameNumber, maxFrameNumber);
172 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800173 }
174
Lajos Molnar5f920c12015-07-13 16:04:24 -0700175 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
176 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800177 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700178 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800179 }
180
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700181 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
182
183 if (sharedBufferAvailable && mCore->mQueue.empty()) {
184 // make sure the buffer has finished allocating before acquiring it
185 mCore->waitWhileAllocatingLocked();
186
187 slot = mCore->mSingleBufferSlot;
188
189 // Recreate the BufferItem for the shared buffer from the data that
190 // was cached when it was last queued.
191 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
192 outBuffer->mFence = Fence::NO_FENCE;
193 outBuffer->mCrop = mCore->mSingleBufferCache.crop;
194 outBuffer->mTransform = mCore->mSingleBufferCache.transform &
195 ~static_cast<uint32_t>(
196 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
197 outBuffer->mScalingMode = mCore->mSingleBufferCache.scalingMode;
198 outBuffer->mDataSpace = mCore->mSingleBufferCache.dataspace;
199 outBuffer->mFrameNumber = mCore->mFrameCounter;
200 outBuffer->mSlot = slot;
201 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
202 outBuffer->mTransformToDisplayInverse =
203 (mCore->mSingleBufferCache.transform &
204 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
205 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700206 outBuffer->mSingleBufferMode = true;
207 outBuffer->mQueuedBuffer = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700208 } else {
209 slot = front->mSlot;
210 *outBuffer = *front;
211 }
212
Pablo Ceballos06312182015-10-07 16:32:12 -0700213 outBuffer->mSingleBufferMode = mCore->mSingleBufferMode;
214
Lajos Molnar5f920c12015-07-13 16:04:24 -0700215 ATRACE_BUFFER_INDEX(slot);
216
217 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700218 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Lajos Molnar5f920c12015-07-13 16:04:24 -0700219 // If the front buffer is still being tracked, update its slot state
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700220 if (mCore->stillTracking(outBuffer)) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700221 mSlots[slot].mAcquireCalled = true;
222 mSlots[slot].mNeedsCleanupOnRelease = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700223 // Don't decrease the queue count if the BufferItem wasn't
224 // previously in the queue. This happens in single buffer mode when
225 // the queue is empty and the BufferItem is created above.
226 if (mCore->mQueue.empty()) {
227 mSlots[slot].mBufferState.acquireNotInQueue();
228 } else {
229 mSlots[slot].mBufferState.acquire();
230 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700231 mSlots[slot].mFence = Fence::NO_FENCE;
232 }
233
234 // If the buffer has previously been acquired by the consumer, set
235 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
236 // on the consumer side
237 if (outBuffer->mAcquireCalled) {
238 outBuffer->mGraphicBuffer = NULL;
239 }
240
241 mCore->mQueue.erase(front);
242
243 // We might have freed a slot while dropping old buffers, or the producer
244 // may be blocked waiting for the number of buffers in the queue to
245 // decrease.
246 mCore->mDequeueCondition.broadcast();
247
248 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
249
250 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800251 }
252
Lajos Molnar5f920c12015-07-13 16:04:24 -0700253 if (listener != NULL) {
254 for (int i = 0; i < numDroppedBuffers; ++i) {
255 listener->onBufferReleased();
256 }
Dan Stoza289ade12014-02-28 11:17:17 -0800257 }
258
Dan Stoza289ade12014-02-28 11:17:17 -0800259 return NO_ERROR;
260}
261
Dan Stoza9f3053d2014-03-06 15:14:33 -0800262status_t BufferQueueConsumer::detachBuffer(int slot) {
263 ATRACE_CALL();
264 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700265 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800266 Mutex::Autolock lock(mCore->mMutex);
267
268 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700269 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800270 return NO_INIT;
271 }
272
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700273 if (mCore->mSingleBufferMode) {
274 BQ_LOGE("detachBuffer: detachBuffer not allowed in single buffer"
275 "mode");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800276 return BAD_VALUE;
277 }
278
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700279 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
280 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
281 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
282 return BAD_VALUE;
283 } else if (!mSlots[slot].mBufferState.isAcquired()) {
284 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
285 "(state = %s)", slot, mSlots[slot].mBufferState.string());
286 return BAD_VALUE;
287 }
288
289 mSlots[slot].mBufferState.detachConsumer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800290 mCore->freeBufferLocked(slot);
291 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700292 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293
294 return NO_ERROR;
295}
296
297status_t BufferQueueConsumer::attachBuffer(int* outSlot,
298 const sp<android::GraphicBuffer>& buffer) {
299 ATRACE_CALL();
300
301 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700302 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800303 return BAD_VALUE;
304 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700305 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800306 return BAD_VALUE;
307 }
308
309 Mutex::Autolock lock(mCore->mMutex);
310
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700311 if (mCore->mSingleBufferMode) {
312 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer"
313 "mode");
314 return BAD_VALUE;
315 }
316
Dan Stoza0de7ea72015-04-23 13:20:51 -0700317 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318 int numAcquiredBuffers = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800319 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700320 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800321 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322 }
323 }
324
325 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700326 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800327 "(max %d)", numAcquiredBuffers,
328 mCore->mMaxAcquiredBufferCount);
329 return INVALID_OPERATION;
330 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700331
Dan Stoza812ed062015-06-02 15:45:22 -0700332 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
333 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
334 "[queue %u]", buffer->getGenerationNumber(),
335 mCore->mGenerationNumber);
336 return BAD_VALUE;
337 }
338
Dan Stoza0de7ea72015-04-23 13:20:51 -0700339 // Find a free slot to put the buffer into
340 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
341 if (!mCore->mFreeSlots.empty()) {
342 auto slot = mCore->mFreeSlots.begin();
343 found = *slot;
344 mCore->mFreeSlots.erase(slot);
345 } else if (!mCore->mFreeBuffers.empty()) {
346 found = mCore->mFreeBuffers.front();
347 mCore->mFreeBuffers.remove(found);
348 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800349 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700350 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800351 return NO_MEMORY;
352 }
353
354 *outSlot = found;
355 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700356 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800357
358 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700359 mSlots[*outSlot].mBufferState.attachConsumer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800360 mSlots[*outSlot].mAttachedByConsumer = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800361 mSlots[*outSlot].mNeedsCleanupOnRelease = false;
362 mSlots[*outSlot].mFence = Fence::NO_FENCE;
363 mSlots[*outSlot].mFrameNumber = 0;
364
Dan Stoza99b18b42014-03-28 15:34:33 -0700365 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
366 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
367 // Binder traffic by not un/flattening the GraphicBuffer. However, it
368 // requires that the consumer maintain a cached copy of the slot <--> buffer
369 // mappings, which is why the consumer doesn't need the valid pointer on
370 // acquire.
371 //
372 // The StreamSplitter is one of the primary users of the attach/detach
373 // logic, and while it is running, all buffers it acquires are immediately
374 // detached, and all buffers it eventually releases are ones that were
375 // attached (as opposed to having been obtained from acquireBuffer), so it
376 // doesn't make sense to maintain the slot/buffer mappings, which would
377 // become invalid for every buffer during detach/attach. By setting this to
378 // false, the valid GraphicBuffer pointer will always be sent with acquire
379 // for attached buffers.
380 mSlots[*outSlot].mAcquireCalled = false;
381
Dan Stoza0de7ea72015-04-23 13:20:51 -0700382 mCore->validateConsistencyLocked();
383
Dan Stoza9f3053d2014-03-06 15:14:33 -0800384 return NO_ERROR;
385}
386
Dan Stoza289ade12014-02-28 11:17:17 -0800387status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
388 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
389 EGLSyncKHR eglFence) {
390 ATRACE_CALL();
391 ATRACE_BUFFER_INDEX(slot);
392
Dan Stoza9f3053d2014-03-06 15:14:33 -0800393 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
394 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700395 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
396 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800397 return BAD_VALUE;
398 }
399
Dan Stozad1c10362014-03-28 15:19:08 -0700400 sp<IProducerListener> listener;
401 { // Autolock scope
402 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800403
Dan Stozad1c10362014-03-28 15:19:08 -0700404 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700405 // we can ignore this releaseBuffer for the old buffer.
406 // Ignore this for the shared buffer where the frame number can easily
407 // get out of sync due to the buffer being queued and acquired at the
408 // same time.
409 if (frameNumber != mSlots[slot].mFrameNumber &&
410 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700411 return STALE_BUFFER_SLOT;
412 }
Dan Stoza289ade12014-02-28 11:17:17 -0800413
Dan Stozad1c10362014-03-28 15:19:08 -0700414
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700415 if (mSlots[slot].mBufferState.isAcquired()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700416 mSlots[slot].mEglDisplay = eglDisplay;
417 mSlots[slot].mEglFence = eglFence;
418 mSlots[slot].mFence = releaseFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700419 mSlots[slot].mBufferState.release();
420
421 // After leaving single buffer mode, the shared buffer will
422 // still be around. Mark it as no longer shared if this
423 // operation causes it to be free.
424 if (!mCore->mSingleBufferMode &&
425 mSlots[slot].mBufferState.isFree()) {
426 mSlots[slot].mBufferState.mShared = false;
427 }
428 // Don't put the shared buffer on the free list.
429 if (!mSlots[slot].mBufferState.isShared()) {
430 mCore->mFreeBuffers.push_back(slot);
431 }
432
Dan Stozad1c10362014-03-28 15:19:08 -0700433 listener = mCore->mConnectedProducerListener;
434 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
435 } else if (mSlots[slot].mNeedsCleanupOnRelease) {
436 BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700437 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stozad1c10362014-03-28 15:19:08 -0700438 mSlots[slot].mNeedsCleanupOnRelease = false;
439 return STALE_BUFFER_SLOT;
440 } else {
Dan Stoza52937cd2015-05-01 16:42:55 -0700441 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700442 "but its state was %s", slot,
443 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800444 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800445 }
Dan Stoza289ade12014-02-28 11:17:17 -0800446
Dan Stozad1c10362014-03-28 15:19:08 -0700447 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700448 mCore->validateConsistencyLocked();
Dan Stozad1c10362014-03-28 15:19:08 -0700449 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800450
Dan Stozad1c10362014-03-28 15:19:08 -0700451 // Call back without lock held
452 if (listener != NULL) {
453 listener->onBufferReleased();
454 }
Dan Stoza289ade12014-02-28 11:17:17 -0800455
456 return NO_ERROR;
457}
458
459status_t BufferQueueConsumer::connect(
460 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
461 ATRACE_CALL();
462
463 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700464 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800465 return BAD_VALUE;
466 }
467
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700468 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800469 controlledByApp ? "true" : "false");
470
471 Mutex::Autolock lock(mCore->mMutex);
472
473 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700474 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800475 return NO_INIT;
476 }
477
478 mCore->mConsumerListener = consumerListener;
479 mCore->mConsumerControlledByApp = controlledByApp;
480
481 return NO_ERROR;
482}
483
484status_t BufferQueueConsumer::disconnect() {
485 ATRACE_CALL();
486
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700487 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800488
489 Mutex::Autolock lock(mCore->mMutex);
490
491 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700492 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800493 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800494 }
495
496 mCore->mIsAbandoned = true;
497 mCore->mConsumerListener = NULL;
498 mCore->mQueue.clear();
499 mCore->freeAllBuffersLocked();
500 mCore->mDequeueCondition.broadcast();
501 return NO_ERROR;
502}
503
Dan Stozafebd4f42014-04-09 16:14:51 -0700504status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800505 ATRACE_CALL();
506
507 if (outSlotMask == NULL) {
508 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
509 return BAD_VALUE;
510 }
511
512 Mutex::Autolock lock(mCore->mMutex);
513
514 if (mCore->mIsAbandoned) {
515 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
516 return NO_INIT;
517 }
518
Dan Stozafebd4f42014-04-09 16:14:51 -0700519 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800520 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800521 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700522 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800523 }
524 }
525
526 // Remove from the mask queued buffers for which acquire has been called,
527 // since the consumer will not receive their buffer addresses and so must
528 // retain their cached information
529 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
530 while (current != mCore->mQueue.end()) {
531 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700532 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800533 }
534 ++current;
535 }
536
Dan Stozafebd4f42014-04-09 16:14:51 -0700537 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800538 *outSlotMask = mask;
539 return NO_ERROR;
540}
541
542status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
543 uint32_t height) {
544 ATRACE_CALL();
545
546 if (width == 0 || height == 0) {
547 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
548 "height=%u)", width, height);
549 return BAD_VALUE;
550 }
551
552 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
553
554 Mutex::Autolock lock(mCore->mMutex);
555 mCore->mDefaultWidth = width;
556 mCore->mDefaultHeight = height;
557 return NO_ERROR;
558}
559
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700560status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800561 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800562
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700563 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
564 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
565 return BAD_VALUE;
566 }
Dan Stoza289ade12014-02-28 11:17:17 -0800567
568 Mutex::Autolock lock(mCore->mMutex);
569
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700570 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
571 BQ_LOGE("setMaxBufferCount: producer is already connected");
Dan Stoza289ade12014-02-28 11:17:17 -0800572 return INVALID_OPERATION;
573 }
574
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700575 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
576 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
577 "mMaxAcquiredBufferCount (%d)", bufferCount,
578 mCore->mMaxAcquiredBufferCount);
579 return BAD_VALUE;
580 }
581
582 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800583 return NO_ERROR;
584}
585
586status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
587 int maxAcquiredBuffers) {
588 ATRACE_CALL();
589
590 if (maxAcquiredBuffers < 1 ||
591 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
592 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
593 maxAcquiredBuffers);
594 return BAD_VALUE;
595 }
596
597 Mutex::Autolock lock(mCore->mMutex);
598
599 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
600 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
601 return INVALID_OPERATION;
602 }
603
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700604 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700605 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
606 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700607 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would exceed "
608 "the maxBufferCount (%d) (maxDequeued %d async %d)",
609 maxAcquiredBuffers, mCore->mMaxBufferCount,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700610 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
611 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700612 return BAD_VALUE;
613 }
614
Dan Stoza289ade12014-02-28 11:17:17 -0800615 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
616 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
617 return NO_ERROR;
618}
619
620void BufferQueueConsumer::setConsumerName(const String8& name) {
621 ATRACE_CALL();
622 BQ_LOGV("setConsumerName: '%s'", name.string());
623 Mutex::Autolock lock(mCore->mMutex);
624 mCore->mConsumerName = name;
625 mConsumerName = name;
626}
627
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800628status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800629 ATRACE_CALL();
630 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
631 Mutex::Autolock lock(mCore->mMutex);
632 mCore->mDefaultBufferFormat = defaultFormat;
633 return NO_ERROR;
634}
635
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800636status_t BufferQueueConsumer::setDefaultBufferDataSpace(
637 android_dataspace defaultDataSpace) {
638 ATRACE_CALL();
639 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
640 Mutex::Autolock lock(mCore->mMutex);
641 mCore->mDefaultBufferDataSpace = defaultDataSpace;
642 return NO_ERROR;
643}
644
Dan Stoza289ade12014-02-28 11:17:17 -0800645status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
646 ATRACE_CALL();
647 BQ_LOGV("setConsumerUsageBits: %#x", usage);
648 Mutex::Autolock lock(mCore->mMutex);
649 mCore->mConsumerUsageBits = usage;
650 return NO_ERROR;
651}
652
653status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
654 ATRACE_CALL();
655 BQ_LOGV("setTransformHint: %#x", hint);
656 Mutex::Autolock lock(mCore->mMutex);
657 mCore->mTransformHint = hint;
658 return NO_ERROR;
659}
660
Jesse Hall399184a2014-03-03 15:42:54 -0800661sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
662 return mCore->mSidebandStream;
663}
664
Dan Stoza289ade12014-02-28 11:17:17 -0800665void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
666 mCore->dump(result, prefix);
667}
668
669} // namespace android