blob: d52b47f3b06063938187b80531e7c6612221fde5 [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) {
53 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
54 ++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
Lajos Molnar5f920c12015-07-13 16:04:24 -070063 // Check if the queue is empty.
64 // In asynchronous mode the list is guaranteed to be one buffer deep,
65 // while in synchronous mode we use the oldest buffer.
66 if (mCore->mQueue.empty()) {
67 return NO_BUFFER_AVAILABLE;
68 }
Dan Stoza289ade12014-02-28 11:17:17 -080069
Lajos Molnar5f920c12015-07-13 16:04:24 -070070 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080071
Lajos Molnar5f920c12015-07-13 16:04:24 -070072 // If expectedPresent is specified, we may not want to return a buffer yet.
73 // If it's specified and there's more than one buffer queued, we may want
74 // to drop a buffer.
75 if (expectedPresent != 0) {
76 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080077
Lajos Molnar5f920c12015-07-13 16:04:24 -070078 // The 'expectedPresent' argument indicates when the buffer is expected
79 // to be presented on-screen. If the buffer's desired present time is
80 // earlier (less) than expectedPresent -- meaning it will be displayed
81 // on time or possibly late if we show it as soon as possible -- we
82 // acquire and return it. If we don't want to display it until after the
83 // expectedPresent time, we return PRESENT_LATER without acquiring it.
84 //
85 // To be safe, we don't defer acquisition if expectedPresent is more
86 // than one second in the future beyond the desired present time
87 // (i.e., we'd be holding the buffer for a long time).
88 //
89 // NOTE: Code assumes monotonic time values from the system clock
90 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -080091
Lajos Molnar5f920c12015-07-13 16:04:24 -070092 // Start by checking to see if we can drop frames. We skip this check if
93 // the timestamps are being auto-generated by Surface. If the app isn't
94 // generating timestamps explicitly, it probably doesn't want frames to
95 // be discarded based on them.
96 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
97 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -070098
Lajos Molnar5f920c12015-07-13 16:04:24 -070099 // If dropping entry[0] would leave us with a buffer that the
100 // consumer is not yet ready for, don't drop it.
101 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
102 break;
103 }
104
105 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
106 // additional criterion here: we only drop the earlier buffer if our
107 // desiredPresent falls within +/- 1 second of the expected present.
108 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
109 // relative timestamp), which normally mean "ignore the timestamp
110 // and acquire immediately", would cause us to drop frames.
111 //
112 // We may want to add an additional criterion: don't drop the
113 // earlier buffer if entry[1]'s fence hasn't signaled yet.
114 nsecs_t desiredPresent = bufferItem.mTimestamp;
115 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
116 desiredPresent > expectedPresent) {
117 // This buffer is set to display in the near future, or
118 // desiredPresent is garbage. Either way we don't want to drop
119 // the previous buffer just to get this on the screen sooner.
120 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
121 PRId64 " (%" PRId64 ") now=%" PRId64,
122 desiredPresent, expectedPresent,
123 desiredPresent - expectedPresent,
124 systemTime(CLOCK_MONOTONIC));
125 break;
126 }
127
128 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
129 " size=%zu",
130 desiredPresent, expectedPresent, mCore->mQueue.size());
131 if (mCore->stillTracking(front)) {
132 // Front buffer is still in mSlots, so mark the slot as free
133 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
134 mCore->mFreeBuffers.push_back(front->mSlot);
135 listener = mCore->mConnectedProducerListener;
136 ++numDroppedBuffers;
137 }
138 mCore->mQueue.erase(front);
139 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700140 }
141
Lajos Molnar5f920c12015-07-13 16:04:24 -0700142 // See if the front buffer is ready to be acquired
143 nsecs_t desiredPresent = front->mTimestamp;
144 bool bufferIsDue = desiredPresent <= expectedPresent ||
145 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
146 bool consumerIsReady = maxFrameNumber > 0 ?
147 front->mFrameNumber <= maxFrameNumber : true;
148 if (!bufferIsDue || !consumerIsReady) {
149 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
150 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
151 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700152 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800153 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700154 systemTime(CLOCK_MONOTONIC),
155 front->mFrameNumber, maxFrameNumber);
156 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800157 }
158
Lajos Molnar5f920c12015-07-13 16:04:24 -0700159 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
160 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800161 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700162 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800163 }
164
Lajos Molnar5f920c12015-07-13 16:04:24 -0700165 int slot = front->mSlot;
166 *outBuffer = *front;
167 ATRACE_BUFFER_INDEX(slot);
168
169 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
170 slot, front->mFrameNumber, front->mGraphicBuffer->handle);
171 // If the front buffer is still being tracked, update its slot state
172 if (mCore->stillTracking(front)) {
173 mSlots[slot].mAcquireCalled = true;
174 mSlots[slot].mNeedsCleanupOnRelease = false;
175 mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
176 mSlots[slot].mFence = Fence::NO_FENCE;
177 }
178
179 // If the buffer has previously been acquired by the consumer, set
180 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
181 // on the consumer side
182 if (outBuffer->mAcquireCalled) {
183 outBuffer->mGraphicBuffer = NULL;
184 }
185
186 mCore->mQueue.erase(front);
187
188 // We might have freed a slot while dropping old buffers, or the producer
189 // may be blocked waiting for the number of buffers in the queue to
190 // decrease.
191 mCore->mDequeueCondition.broadcast();
192
193 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
194
195 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800196 }
197
Lajos Molnar5f920c12015-07-13 16:04:24 -0700198 if (listener != NULL) {
199 for (int i = 0; i < numDroppedBuffers; ++i) {
200 listener->onBufferReleased();
201 }
Dan Stoza289ade12014-02-28 11:17:17 -0800202 }
203
Dan Stoza289ade12014-02-28 11:17:17 -0800204 return NO_ERROR;
205}
206
Dan Stoza9f3053d2014-03-06 15:14:33 -0800207status_t BufferQueueConsumer::detachBuffer(int slot) {
208 ATRACE_CALL();
209 ATRACE_BUFFER_INDEX(slot);
210 BQ_LOGV("detachBuffer(C): slot %d", slot);
211 Mutex::Autolock lock(mCore->mMutex);
212
213 if (mCore->mIsAbandoned) {
214 BQ_LOGE("detachBuffer(C): BufferQueue has been abandoned");
215 return NO_INIT;
216 }
217
218 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
219 BQ_LOGE("detachBuffer(C): slot index %d out of range [0, %d)",
220 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
221 return BAD_VALUE;
222 } else if (mSlots[slot].mBufferState != BufferSlot::ACQUIRED) {
223 BQ_LOGE("detachBuffer(C): slot %d is not owned by the consumer "
224 "(state = %d)", slot, mSlots[slot].mBufferState);
225 return BAD_VALUE;
226 }
227
228 mCore->freeBufferLocked(slot);
229 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700230 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800231
232 return NO_ERROR;
233}
234
235status_t BufferQueueConsumer::attachBuffer(int* outSlot,
236 const sp<android::GraphicBuffer>& buffer) {
237 ATRACE_CALL();
238
239 if (outSlot == NULL) {
240 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
241 return BAD_VALUE;
242 } else if (buffer == NULL) {
243 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
244 return BAD_VALUE;
245 }
246
247 Mutex::Autolock lock(mCore->mMutex);
248
Dan Stoza0de7ea72015-04-23 13:20:51 -0700249 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800250 int numAcquiredBuffers = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800251 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
252 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
253 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800254 }
255 }
256
257 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
258 BQ_LOGE("attachBuffer(P): max acquired buffer count reached: %d "
259 "(max %d)", numAcquiredBuffers,
260 mCore->mMaxAcquiredBufferCount);
261 return INVALID_OPERATION;
262 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700263
Dan Stoza812ed062015-06-02 15:45:22 -0700264 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
265 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
266 "[queue %u]", buffer->getGenerationNumber(),
267 mCore->mGenerationNumber);
268 return BAD_VALUE;
269 }
270
Dan Stoza0de7ea72015-04-23 13:20:51 -0700271 // Find a free slot to put the buffer into
272 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
273 if (!mCore->mFreeSlots.empty()) {
274 auto slot = mCore->mFreeSlots.begin();
275 found = *slot;
276 mCore->mFreeSlots.erase(slot);
277 } else if (!mCore->mFreeBuffers.empty()) {
278 found = mCore->mFreeBuffers.front();
279 mCore->mFreeBuffers.remove(found);
280 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800281 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
282 BQ_LOGE("attachBuffer(P): could not find free buffer slot");
283 return NO_MEMORY;
284 }
285
286 *outSlot = found;
287 ATRACE_BUFFER_INDEX(*outSlot);
288 BQ_LOGV("attachBuffer(C): returning slot %d", *outSlot);
289
290 mSlots[*outSlot].mGraphicBuffer = buffer;
291 mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED;
292 mSlots[*outSlot].mAttachedByConsumer = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 mSlots[*outSlot].mNeedsCleanupOnRelease = false;
294 mSlots[*outSlot].mFence = Fence::NO_FENCE;
295 mSlots[*outSlot].mFrameNumber = 0;
296
Dan Stoza99b18b42014-03-28 15:34:33 -0700297 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
298 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
299 // Binder traffic by not un/flattening the GraphicBuffer. However, it
300 // requires that the consumer maintain a cached copy of the slot <--> buffer
301 // mappings, which is why the consumer doesn't need the valid pointer on
302 // acquire.
303 //
304 // The StreamSplitter is one of the primary users of the attach/detach
305 // logic, and while it is running, all buffers it acquires are immediately
306 // detached, and all buffers it eventually releases are ones that were
307 // attached (as opposed to having been obtained from acquireBuffer), so it
308 // doesn't make sense to maintain the slot/buffer mappings, which would
309 // become invalid for every buffer during detach/attach. By setting this to
310 // false, the valid GraphicBuffer pointer will always be sent with acquire
311 // for attached buffers.
312 mSlots[*outSlot].mAcquireCalled = false;
313
Dan Stoza0de7ea72015-04-23 13:20:51 -0700314 mCore->validateConsistencyLocked();
315
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316 return NO_ERROR;
317}
318
Dan Stoza289ade12014-02-28 11:17:17 -0800319status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
320 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
321 EGLSyncKHR eglFence) {
322 ATRACE_CALL();
323 ATRACE_BUFFER_INDEX(slot);
324
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
326 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700327 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
328 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800329 return BAD_VALUE;
330 }
331
Dan Stozad1c10362014-03-28 15:19:08 -0700332 sp<IProducerListener> listener;
333 { // Autolock scope
334 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800335
Dan Stozad1c10362014-03-28 15:19:08 -0700336 // If the frame number has changed because the buffer has been reallocated,
337 // we can ignore this releaseBuffer for the old buffer
338 if (frameNumber != mSlots[slot].mFrameNumber) {
339 return STALE_BUFFER_SLOT;
340 }
Dan Stoza289ade12014-02-28 11:17:17 -0800341
Dan Stozad1c10362014-03-28 15:19:08 -0700342 // Make sure this buffer hasn't been queued while acquired by the consumer
343 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
344 while (current != mCore->mQueue.end()) {
345 if (current->mSlot == slot) {
346 BQ_LOGE("releaseBuffer: buffer slot %d pending release is "
347 "currently queued", slot);
348 return BAD_VALUE;
349 }
350 ++current;
351 }
352
353 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
354 mSlots[slot].mEglDisplay = eglDisplay;
355 mSlots[slot].mEglFence = eglFence;
356 mSlots[slot].mFence = releaseFence;
357 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700358 mCore->mFreeBuffers.push_back(slot);
Dan Stozad1c10362014-03-28 15:19:08 -0700359 listener = mCore->mConnectedProducerListener;
360 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
361 } else if (mSlots[slot].mNeedsCleanupOnRelease) {
362 BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d "
363 "(state = %d)", slot, mSlots[slot].mBufferState);
364 mSlots[slot].mNeedsCleanupOnRelease = false;
365 return STALE_BUFFER_SLOT;
366 } else {
Dan Stoza52937cd2015-05-01 16:42:55 -0700367 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Dan Stozad1c10362014-03-28 15:19:08 -0700368 "but its state was %d", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800370 }
Dan Stoza289ade12014-02-28 11:17:17 -0800371
Dan Stozad1c10362014-03-28 15:19:08 -0700372 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700373 mCore->validateConsistencyLocked();
Dan Stozad1c10362014-03-28 15:19:08 -0700374 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800375
Dan Stozad1c10362014-03-28 15:19:08 -0700376 // Call back without lock held
377 if (listener != NULL) {
378 listener->onBufferReleased();
379 }
Dan Stoza289ade12014-02-28 11:17:17 -0800380
381 return NO_ERROR;
382}
383
384status_t BufferQueueConsumer::connect(
385 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
386 ATRACE_CALL();
387
388 if (consumerListener == NULL) {
389 BQ_LOGE("connect(C): consumerListener may not be NULL");
390 return BAD_VALUE;
391 }
392
393 BQ_LOGV("connect(C): controlledByApp=%s",
394 controlledByApp ? "true" : "false");
395
396 Mutex::Autolock lock(mCore->mMutex);
397
398 if (mCore->mIsAbandoned) {
399 BQ_LOGE("connect(C): BufferQueue has been abandoned");
400 return NO_INIT;
401 }
402
403 mCore->mConsumerListener = consumerListener;
404 mCore->mConsumerControlledByApp = controlledByApp;
405
406 return NO_ERROR;
407}
408
409status_t BufferQueueConsumer::disconnect() {
410 ATRACE_CALL();
411
412 BQ_LOGV("disconnect(C)");
413
414 Mutex::Autolock lock(mCore->mMutex);
415
416 if (mCore->mConsumerListener == NULL) {
417 BQ_LOGE("disconnect(C): no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800418 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800419 }
420
421 mCore->mIsAbandoned = true;
422 mCore->mConsumerListener = NULL;
423 mCore->mQueue.clear();
424 mCore->freeAllBuffersLocked();
425 mCore->mDequeueCondition.broadcast();
426 return NO_ERROR;
427}
428
Dan Stozafebd4f42014-04-09 16:14:51 -0700429status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800430 ATRACE_CALL();
431
432 if (outSlotMask == NULL) {
433 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
434 return BAD_VALUE;
435 }
436
437 Mutex::Autolock lock(mCore->mMutex);
438
439 if (mCore->mIsAbandoned) {
440 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
441 return NO_INIT;
442 }
443
Dan Stozafebd4f42014-04-09 16:14:51 -0700444 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800445 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800446 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700447 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800448 }
449 }
450
451 // Remove from the mask queued buffers for which acquire has been called,
452 // since the consumer will not receive their buffer addresses and so must
453 // retain their cached information
454 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
455 while (current != mCore->mQueue.end()) {
456 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700457 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800458 }
459 ++current;
460 }
461
Dan Stozafebd4f42014-04-09 16:14:51 -0700462 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800463 *outSlotMask = mask;
464 return NO_ERROR;
465}
466
467status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
468 uint32_t height) {
469 ATRACE_CALL();
470
471 if (width == 0 || height == 0) {
472 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
473 "height=%u)", width, height);
474 return BAD_VALUE;
475 }
476
477 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
478
479 Mutex::Autolock lock(mCore->mMutex);
480 mCore->mDefaultWidth = width;
481 mCore->mDefaultHeight = height;
482 return NO_ERROR;
483}
484
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700485status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800486 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800487
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700488 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
489 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
490 return BAD_VALUE;
491 }
Dan Stoza289ade12014-02-28 11:17:17 -0800492
493 Mutex::Autolock lock(mCore->mMutex);
494
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700495 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
496 BQ_LOGE("setMaxBufferCount: producer is already connected");
Dan Stoza289ade12014-02-28 11:17:17 -0800497 return INVALID_OPERATION;
498 }
499
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700500 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
501 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
502 "mMaxAcquiredBufferCount (%d)", bufferCount,
503 mCore->mMaxAcquiredBufferCount);
504 return BAD_VALUE;
505 }
506
507 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800508 return NO_ERROR;
509}
510
511status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
512 int maxAcquiredBuffers) {
513 ATRACE_CALL();
514
515 if (maxAcquiredBuffers < 1 ||
516 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
517 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
518 maxAcquiredBuffers);
519 return BAD_VALUE;
520 }
521
522 Mutex::Autolock lock(mCore->mMutex);
523
524 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
525 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
526 return INVALID_OPERATION;
527 }
528
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700529 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700530 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
531 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700532 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would exceed "
533 "the maxBufferCount (%d) (maxDequeued %d async %d)",
534 maxAcquiredBuffers, mCore->mMaxBufferCount,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700535 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
536 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700537 return BAD_VALUE;
538 }
539
Dan Stoza289ade12014-02-28 11:17:17 -0800540 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
541 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
542 return NO_ERROR;
543}
544
545void BufferQueueConsumer::setConsumerName(const String8& name) {
546 ATRACE_CALL();
547 BQ_LOGV("setConsumerName: '%s'", name.string());
548 Mutex::Autolock lock(mCore->mMutex);
549 mCore->mConsumerName = name;
550 mConsumerName = name;
551}
552
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800553status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800554 ATRACE_CALL();
555 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
556 Mutex::Autolock lock(mCore->mMutex);
557 mCore->mDefaultBufferFormat = defaultFormat;
558 return NO_ERROR;
559}
560
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800561status_t BufferQueueConsumer::setDefaultBufferDataSpace(
562 android_dataspace defaultDataSpace) {
563 ATRACE_CALL();
564 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
565 Mutex::Autolock lock(mCore->mMutex);
566 mCore->mDefaultBufferDataSpace = defaultDataSpace;
567 return NO_ERROR;
568}
569
Dan Stoza289ade12014-02-28 11:17:17 -0800570status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
571 ATRACE_CALL();
572 BQ_LOGV("setConsumerUsageBits: %#x", usage);
573 Mutex::Autolock lock(mCore->mMutex);
574 mCore->mConsumerUsageBits = usage;
575 return NO_ERROR;
576}
577
578status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
579 ATRACE_CALL();
580 BQ_LOGV("setTransformHint: %#x", hint);
581 Mutex::Autolock lock(mCore->mMutex);
582 mCore->mTransformHint = hint;
583 return NO_ERROR;
584}
585
Jesse Hall399184a2014-03-03 15:42:54 -0800586sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
587 return mCore->mSidebandStream;
588}
589
Dan Stoza289ade12014-02-28 11:17:17 -0800590void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
591 mCore->dump(result, prefix);
592}
593
594} // namespace android