blob: ae796b14db0bc916dba21fc90bbf8bb31bc7bef2 [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();
41 Mutex::Autolock lock(mCore->mMutex);
42
43 // Check that the consumer doesn't currently have the maximum number of
44 // buffers acquired. We allow the max buffer count to be exceeded by one
45 // buffer so that the consumer can successfully set up the newly acquired
46 // buffer before releasing the old one.
47 int numAcquiredBuffers = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -080048 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -080049 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
50 ++numAcquiredBuffers;
51 }
52 }
53 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
54 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
55 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
56 return INVALID_OPERATION;
57 }
58
59 // Check if the queue is empty.
60 // In asynchronous mode the list is guaranteed to be one buffer deep,
61 // while in synchronous mode we use the oldest buffer.
62 if (mCore->mQueue.empty()) {
63 return NO_BUFFER_AVAILABLE;
64 }
65
66 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
67
68 // If expectedPresent is specified, we may not want to return a buffer yet.
69 // If it's specified and there's more than one buffer queued, we may want
70 // to drop a buffer.
71 if (expectedPresent != 0) {
72 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
73
74 // The 'expectedPresent' argument indicates when the buffer is expected
75 // to be presented on-screen. If the buffer's desired present time is
76 // earlier (less) than expectedPresent -- meaning it will be displayed
77 // on time or possibly late if we show it as soon as possible -- we
78 // acquire and return it. If we don't want to display it until after the
79 // expectedPresent time, we return PRESENT_LATER without acquiring it.
80 //
81 // To be safe, we don't defer acquisition if expectedPresent is more
82 // than one second in the future beyond the desired present time
83 // (i.e., we'd be holding the buffer for a long time).
84 //
85 // NOTE: Code assumes monotonic time values from the system clock
86 // are positive.
87
88 // Start by checking to see if we can drop frames. We skip this check if
89 // the timestamps are being auto-generated by Surface. If the app isn't
90 // generating timestamps explicitly, it probably doesn't want frames to
91 // be discarded based on them.
92 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
Dan Stozaa4650a52015-05-12 12:56:16 -070093 const BufferItem& bufferItem(mCore->mQueue[1]);
94
95 // If dropping entry[0] would leave us with a buffer that the
96 // consumer is not yet ready for, don't drop it.
97 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
Dan Stozaecc50402015-04-28 14:42:06 -070098 break;
99 }
100
Dan Stoza289ade12014-02-28 11:17:17 -0800101 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
102 // additional criterion here: we only drop the earlier buffer if our
103 // desiredPresent falls within +/- 1 second of the expected present.
104 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
105 // relative timestamp), which normally mean "ignore the timestamp
106 // and acquire immediately", would cause us to drop frames.
107 //
108 // We may want to add an additional criterion: don't drop the
109 // earlier buffer if entry[1]'s fence hasn't signaled yet.
Dan Stoza289ade12014-02-28 11:17:17 -0800110 nsecs_t desiredPresent = bufferItem.mTimestamp;
111 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
112 desiredPresent > expectedPresent) {
113 // This buffer is set to display in the near future, or
114 // desiredPresent is garbage. Either way we don't want to drop
115 // the previous buffer just to get this on the screen sooner.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700116 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
117 PRId64 " (%" PRId64 ") now=%" PRId64,
118 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800119 desiredPresent - expectedPresent,
120 systemTime(CLOCK_MONOTONIC));
121 break;
122 }
123
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700124 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
125 " size=%zu",
Dan Stoza289ade12014-02-28 11:17:17 -0800126 desiredPresent, expectedPresent, mCore->mQueue.size());
127 if (mCore->stillTracking(front)) {
128 // Front buffer is still in mSlots, so mark the slot as free
129 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700130 mCore->mFreeBuffers.push_back(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800131 }
132 mCore->mQueue.erase(front);
133 front = mCore->mQueue.begin();
134 }
135
Dan Stozaa4650a52015-05-12 12:56:16 -0700136 // See if the front buffer is ready to be acquired
Dan Stoza289ade12014-02-28 11:17:17 -0800137 nsecs_t desiredPresent = front->mTimestamp;
Dan Stozaa4650a52015-05-12 12:56:16 -0700138 bool bufferIsDue = desiredPresent <= expectedPresent ||
139 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
140 bool consumerIsReady = maxFrameNumber > 0 ?
141 front->mFrameNumber <= maxFrameNumber : true;
142 if (!bufferIsDue || !consumerIsReady) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700143 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
Dan Stozaa4650a52015-05-12 12:56:16 -0700144 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
145 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700146 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800147 desiredPresent - expectedPresent,
Dan Stozaa4650a52015-05-12 12:56:16 -0700148 systemTime(CLOCK_MONOTONIC),
149 front->mFrameNumber, maxFrameNumber);
Dan Stoza289ade12014-02-28 11:17:17 -0800150 return PRESENT_LATER;
151 }
152
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700153 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
154 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800155 desiredPresent - expectedPresent,
156 systemTime(CLOCK_MONOTONIC));
157 }
158
159 int slot = front->mSlot;
160 *outBuffer = *front;
161 ATRACE_BUFFER_INDEX(slot);
162
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700163 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Dan Stoza289ade12014-02-28 11:17:17 -0800164 slot, front->mFrameNumber, front->mGraphicBuffer->handle);
165 // If the front buffer is still being tracked, update its slot state
166 if (mCore->stillTracking(front)) {
167 mSlots[slot].mAcquireCalled = true;
168 mSlots[slot].mNeedsCleanupOnRelease = false;
169 mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
170 mSlots[slot].mFence = Fence::NO_FENCE;
171 }
172
173 // If the buffer has previously been acquired by the consumer, set
174 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
175 // on the consumer side
176 if (outBuffer->mAcquireCalled) {
177 outBuffer->mGraphicBuffer = NULL;
178 }
179
180 mCore->mQueue.erase(front);
Dan Stozaae3c3682014-04-18 15:43:35 -0700181
182 // We might have freed a slot while dropping old buffers, or the producer
183 // may be blocked waiting for the number of buffers in the queue to
184 // decrease.
Dan Stoza289ade12014-02-28 11:17:17 -0800185 mCore->mDequeueCondition.broadcast();
186
187 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
188
Dan Stoza0de7ea72015-04-23 13:20:51 -0700189 mCore->validateConsistencyLocked();
190
Dan Stoza289ade12014-02-28 11:17:17 -0800191 return NO_ERROR;
192}
193
Dan Stoza9f3053d2014-03-06 15:14:33 -0800194status_t BufferQueueConsumer::detachBuffer(int slot) {
195 ATRACE_CALL();
196 ATRACE_BUFFER_INDEX(slot);
197 BQ_LOGV("detachBuffer(C): slot %d", slot);
198 Mutex::Autolock lock(mCore->mMutex);
199
200 if (mCore->mIsAbandoned) {
201 BQ_LOGE("detachBuffer(C): BufferQueue has been abandoned");
202 return NO_INIT;
203 }
204
205 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
206 BQ_LOGE("detachBuffer(C): slot index %d out of range [0, %d)",
207 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
208 return BAD_VALUE;
209 } else if (mSlots[slot].mBufferState != BufferSlot::ACQUIRED) {
210 BQ_LOGE("detachBuffer(C): slot %d is not owned by the consumer "
211 "(state = %d)", slot, mSlots[slot].mBufferState);
212 return BAD_VALUE;
213 }
214
215 mCore->freeBufferLocked(slot);
216 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700217 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800218
219 return NO_ERROR;
220}
221
222status_t BufferQueueConsumer::attachBuffer(int* outSlot,
223 const sp<android::GraphicBuffer>& buffer) {
224 ATRACE_CALL();
225
226 if (outSlot == NULL) {
227 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
228 return BAD_VALUE;
229 } else if (buffer == NULL) {
230 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
231 return BAD_VALUE;
232 }
233
234 Mutex::Autolock lock(mCore->mMutex);
235
Dan Stoza0de7ea72015-04-23 13:20:51 -0700236 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800237 int numAcquiredBuffers = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800238 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
239 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
240 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800241 }
242 }
243
244 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
245 BQ_LOGE("attachBuffer(P): max acquired buffer count reached: %d "
246 "(max %d)", numAcquiredBuffers,
247 mCore->mMaxAcquiredBufferCount);
248 return INVALID_OPERATION;
249 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700250
Dan Stoza812ed062015-06-02 15:45:22 -0700251 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
252 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
253 "[queue %u]", buffer->getGenerationNumber(),
254 mCore->mGenerationNumber);
255 return BAD_VALUE;
256 }
257
Dan Stoza0de7ea72015-04-23 13:20:51 -0700258 // Find a free slot to put the buffer into
259 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
260 if (!mCore->mFreeSlots.empty()) {
261 auto slot = mCore->mFreeSlots.begin();
262 found = *slot;
263 mCore->mFreeSlots.erase(slot);
264 } else if (!mCore->mFreeBuffers.empty()) {
265 found = mCore->mFreeBuffers.front();
266 mCore->mFreeBuffers.remove(found);
267 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800268 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
269 BQ_LOGE("attachBuffer(P): could not find free buffer slot");
270 return NO_MEMORY;
271 }
272
273 *outSlot = found;
274 ATRACE_BUFFER_INDEX(*outSlot);
275 BQ_LOGV("attachBuffer(C): returning slot %d", *outSlot);
276
277 mSlots[*outSlot].mGraphicBuffer = buffer;
278 mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED;
279 mSlots[*outSlot].mAttachedByConsumer = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800280 mSlots[*outSlot].mNeedsCleanupOnRelease = false;
281 mSlots[*outSlot].mFence = Fence::NO_FENCE;
282 mSlots[*outSlot].mFrameNumber = 0;
283
Dan Stoza99b18b42014-03-28 15:34:33 -0700284 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
285 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
286 // Binder traffic by not un/flattening the GraphicBuffer. However, it
287 // requires that the consumer maintain a cached copy of the slot <--> buffer
288 // mappings, which is why the consumer doesn't need the valid pointer on
289 // acquire.
290 //
291 // The StreamSplitter is one of the primary users of the attach/detach
292 // logic, and while it is running, all buffers it acquires are immediately
293 // detached, and all buffers it eventually releases are ones that were
294 // attached (as opposed to having been obtained from acquireBuffer), so it
295 // doesn't make sense to maintain the slot/buffer mappings, which would
296 // become invalid for every buffer during detach/attach. By setting this to
297 // false, the valid GraphicBuffer pointer will always be sent with acquire
298 // for attached buffers.
299 mSlots[*outSlot].mAcquireCalled = false;
300
Dan Stoza0de7ea72015-04-23 13:20:51 -0700301 mCore->validateConsistencyLocked();
302
Dan Stoza9f3053d2014-03-06 15:14:33 -0800303 return NO_ERROR;
304}
305
Dan Stoza289ade12014-02-28 11:17:17 -0800306status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
307 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
308 EGLSyncKHR eglFence) {
309 ATRACE_CALL();
310 ATRACE_BUFFER_INDEX(slot);
311
Dan Stoza9f3053d2014-03-06 15:14:33 -0800312 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
313 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700314 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
315 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800316 return BAD_VALUE;
317 }
318
Dan Stozad1c10362014-03-28 15:19:08 -0700319 sp<IProducerListener> listener;
320 { // Autolock scope
321 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800322
Dan Stozad1c10362014-03-28 15:19:08 -0700323 // If the frame number has changed because the buffer has been reallocated,
324 // we can ignore this releaseBuffer for the old buffer
325 if (frameNumber != mSlots[slot].mFrameNumber) {
326 return STALE_BUFFER_SLOT;
327 }
Dan Stoza289ade12014-02-28 11:17:17 -0800328
Dan Stozad1c10362014-03-28 15:19:08 -0700329 // Make sure this buffer hasn't been queued while acquired by the consumer
330 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
331 while (current != mCore->mQueue.end()) {
332 if (current->mSlot == slot) {
333 BQ_LOGE("releaseBuffer: buffer slot %d pending release is "
334 "currently queued", slot);
335 return BAD_VALUE;
336 }
337 ++current;
338 }
339
340 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
341 mSlots[slot].mEglDisplay = eglDisplay;
342 mSlots[slot].mEglFence = eglFence;
343 mSlots[slot].mFence = releaseFence;
344 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700345 mCore->mFreeBuffers.push_back(slot);
Dan Stozad1c10362014-03-28 15:19:08 -0700346 listener = mCore->mConnectedProducerListener;
347 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
348 } else if (mSlots[slot].mNeedsCleanupOnRelease) {
349 BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d "
350 "(state = %d)", slot, mSlots[slot].mBufferState);
351 mSlots[slot].mNeedsCleanupOnRelease = false;
352 return STALE_BUFFER_SLOT;
353 } else {
Dan Stoza52937cd2015-05-01 16:42:55 -0700354 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Dan Stozad1c10362014-03-28 15:19:08 -0700355 "but its state was %d", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800356 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800357 }
Dan Stoza289ade12014-02-28 11:17:17 -0800358
Dan Stozad1c10362014-03-28 15:19:08 -0700359 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700360 mCore->validateConsistencyLocked();
Dan Stozad1c10362014-03-28 15:19:08 -0700361 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800362
Dan Stozad1c10362014-03-28 15:19:08 -0700363 // Call back without lock held
364 if (listener != NULL) {
365 listener->onBufferReleased();
366 }
Dan Stoza289ade12014-02-28 11:17:17 -0800367
368 return NO_ERROR;
369}
370
371status_t BufferQueueConsumer::connect(
372 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
373 ATRACE_CALL();
374
375 if (consumerListener == NULL) {
376 BQ_LOGE("connect(C): consumerListener may not be NULL");
377 return BAD_VALUE;
378 }
379
380 BQ_LOGV("connect(C): controlledByApp=%s",
381 controlledByApp ? "true" : "false");
382
383 Mutex::Autolock lock(mCore->mMutex);
384
385 if (mCore->mIsAbandoned) {
386 BQ_LOGE("connect(C): BufferQueue has been abandoned");
387 return NO_INIT;
388 }
389
390 mCore->mConsumerListener = consumerListener;
391 mCore->mConsumerControlledByApp = controlledByApp;
392
393 return NO_ERROR;
394}
395
396status_t BufferQueueConsumer::disconnect() {
397 ATRACE_CALL();
398
399 BQ_LOGV("disconnect(C)");
400
401 Mutex::Autolock lock(mCore->mMutex);
402
403 if (mCore->mConsumerListener == NULL) {
404 BQ_LOGE("disconnect(C): no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800405 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800406 }
407
408 mCore->mIsAbandoned = true;
409 mCore->mConsumerListener = NULL;
410 mCore->mQueue.clear();
411 mCore->freeAllBuffersLocked();
412 mCore->mDequeueCondition.broadcast();
413 return NO_ERROR;
414}
415
Dan Stozafebd4f42014-04-09 16:14:51 -0700416status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800417 ATRACE_CALL();
418
419 if (outSlotMask == NULL) {
420 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
421 return BAD_VALUE;
422 }
423
424 Mutex::Autolock lock(mCore->mMutex);
425
426 if (mCore->mIsAbandoned) {
427 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
428 return NO_INIT;
429 }
430
Dan Stozafebd4f42014-04-09 16:14:51 -0700431 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800432 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800433 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700434 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800435 }
436 }
437
438 // Remove from the mask queued buffers for which acquire has been called,
439 // since the consumer will not receive their buffer addresses and so must
440 // retain their cached information
441 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
442 while (current != mCore->mQueue.end()) {
443 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700444 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800445 }
446 ++current;
447 }
448
Dan Stozafebd4f42014-04-09 16:14:51 -0700449 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800450 *outSlotMask = mask;
451 return NO_ERROR;
452}
453
454status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
455 uint32_t height) {
456 ATRACE_CALL();
457
458 if (width == 0 || height == 0) {
459 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
460 "height=%u)", width, height);
461 return BAD_VALUE;
462 }
463
464 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
465
466 Mutex::Autolock lock(mCore->mMutex);
467 mCore->mDefaultWidth = width;
468 mCore->mDefaultHeight = height;
469 return NO_ERROR;
470}
471
472status_t BufferQueueConsumer::setDefaultMaxBufferCount(int bufferCount) {
473 ATRACE_CALL();
474 Mutex::Autolock lock(mCore->mMutex);
475 return mCore->setDefaultMaxBufferCountLocked(bufferCount);
476}
477
478status_t BufferQueueConsumer::disableAsyncBuffer() {
479 ATRACE_CALL();
480
481 Mutex::Autolock lock(mCore->mMutex);
482
483 if (mCore->mConsumerListener != NULL) {
484 BQ_LOGE("disableAsyncBuffer: consumer already connected");
485 return INVALID_OPERATION;
486 }
487
488 BQ_LOGV("disableAsyncBuffer");
489 mCore->mUseAsyncBuffer = false;
490 return NO_ERROR;
491}
492
493status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
494 int maxAcquiredBuffers) {
495 ATRACE_CALL();
496
497 if (maxAcquiredBuffers < 1 ||
498 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
499 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
500 maxAcquiredBuffers);
501 return BAD_VALUE;
502 }
503
504 Mutex::Autolock lock(mCore->mMutex);
505
506 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
507 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
508 return INVALID_OPERATION;
509 }
510
511 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
512 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
513 return NO_ERROR;
514}
515
516void BufferQueueConsumer::setConsumerName(const String8& name) {
517 ATRACE_CALL();
518 BQ_LOGV("setConsumerName: '%s'", name.string());
519 Mutex::Autolock lock(mCore->mMutex);
520 mCore->mConsumerName = name;
521 mConsumerName = name;
522}
523
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800524status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800525 ATRACE_CALL();
526 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
527 Mutex::Autolock lock(mCore->mMutex);
528 mCore->mDefaultBufferFormat = defaultFormat;
529 return NO_ERROR;
530}
531
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800532status_t BufferQueueConsumer::setDefaultBufferDataSpace(
533 android_dataspace defaultDataSpace) {
534 ATRACE_CALL();
535 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
536 Mutex::Autolock lock(mCore->mMutex);
537 mCore->mDefaultBufferDataSpace = defaultDataSpace;
538 return NO_ERROR;
539}
540
Dan Stoza289ade12014-02-28 11:17:17 -0800541status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
542 ATRACE_CALL();
543 BQ_LOGV("setConsumerUsageBits: %#x", usage);
544 Mutex::Autolock lock(mCore->mMutex);
545 mCore->mConsumerUsageBits = usage;
546 return NO_ERROR;
547}
548
549status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
550 ATRACE_CALL();
551 BQ_LOGV("setTransformHint: %#x", hint);
552 Mutex::Autolock lock(mCore->mMutex);
553 mCore->mTransformHint = hint;
554 return NO_ERROR;
555}
556
Jesse Hall399184a2014-03-03 15:42:54 -0800557sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
558 return mCore->mSidebandStream;
559}
560
Dan Stoza289ade12014-02-28 11:17:17 -0800561void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
562 mCore->dump(result, prefix);
563}
564
565} // namespace android