blob: b54641e98ffa997af6ea6375cff4c6547231a9e7 [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 "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Dan Stoza289ade12014-02-28 11:17:17 -080023#define EGL_EGLEXT_PROTOTYPES
24
25#include <gui/BufferItem.h>
26#include <gui/BufferQueueCore.h>
27#include <gui/BufferQueueProducer.h>
28#include <gui/IConsumerListener.h>
29#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070030#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080031
32#include <utils/Log.h>
33#include <utils/Trace.h>
34
35namespace android {
36
37BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
38 mCore(core),
39 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070040 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070041 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080042 mLastQueueBufferFence(Fence::NO_FENCE),
43 mCallbackMutex(),
44 mNextCallbackTicket(0),
45 mCurrentCallbackTicket(0),
46 mCallbackCondition() {}
Dan Stoza289ade12014-02-28 11:17:17 -080047
48BufferQueueProducer::~BufferQueueProducer() {}
49
50status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
51 ATRACE_CALL();
52 BQ_LOGV("requestBuffer: slot %d", slot);
53 Mutex::Autolock lock(mCore->mMutex);
54
55 if (mCore->mIsAbandoned) {
56 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
57 return NO_INIT;
58 }
59
Dan Stoza3e96f192014-03-03 10:16:19 -080060 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080061 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080062 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080063 return BAD_VALUE;
64 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
65 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
66 "(state = %d)", slot, mSlots[slot].mBufferState);
67 return BAD_VALUE;
68 }
69
70 mSlots[slot].mRequestBufferCalled = true;
71 *buf = mSlots[slot].mGraphicBuffer;
72 return NO_ERROR;
73}
74
75status_t BufferQueueProducer::setBufferCount(int bufferCount) {
76 ATRACE_CALL();
77 BQ_LOGV("setBufferCount: count = %d", bufferCount);
78
79 sp<IConsumerListener> listener;
80 { // Autolock scope
81 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -070082 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -080083
84 if (mCore->mIsAbandoned) {
85 BQ_LOGE("setBufferCount: BufferQueue has been abandoned");
86 return NO_INIT;
87 }
88
Dan Stoza3e96f192014-03-03 10:16:19 -080089 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080090 BQ_LOGE("setBufferCount: bufferCount %d too large (max %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080091 bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080092 return BAD_VALUE;
93 }
94
95 // There must be no dequeued buffers when changing the buffer count.
Dan Stoza3e96f192014-03-03 10:16:19 -080096 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -080097 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
98 BQ_LOGE("setBufferCount: buffer owned by producer");
Dan Stoza9f3053d2014-03-06 15:14:33 -080099 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800100 }
101 }
102
103 if (bufferCount == 0) {
104 mCore->mOverrideMaxBufferCount = 0;
105 mCore->mDequeueCondition.broadcast();
106 return NO_ERROR;
107 }
108
109 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(false);
110 if (bufferCount < minBufferSlots) {
111 BQ_LOGE("setBufferCount: requested buffer count %d is less than "
112 "minimum %d", bufferCount, minBufferSlots);
113 return BAD_VALUE;
114 }
115
116 // Here we are guaranteed that the producer doesn't have any dequeued
117 // buffers and will release all of its buffer references. We don't
118 // clear the queue, however, so that currently queued buffers still
119 // get displayed.
120 mCore->freeAllBuffersLocked();
121 mCore->mOverrideMaxBufferCount = bufferCount;
122 mCore->mDequeueCondition.broadcast();
123 listener = mCore->mConsumerListener;
124 } // Autolock scope
125
126 // Call back without lock held
127 if (listener != NULL) {
128 listener->onBuffersReleased();
129 }
130
131 return NO_ERROR;
132}
133
Dan Stoza9f3053d2014-03-06 15:14:33 -0800134status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
135 bool async, int* found, status_t* returnFlags) const {
136 bool tryAgain = true;
137 while (tryAgain) {
138 if (mCore->mIsAbandoned) {
139 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
140 return NO_INIT;
141 }
142
143 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
144 if (async && mCore->mOverrideMaxBufferCount) {
145 // FIXME: Some drivers are manually setting the buffer count
146 // (which they shouldn't), so we do this extra test here to
147 // handle that case. This is TEMPORARY until we get this fixed.
148 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
149 BQ_LOGE("%s: async mode is invalid with buffer count override",
150 caller);
151 return BAD_VALUE;
152 }
153 }
154
155 // Free up any buffers that are in slots beyond the max buffer count
156 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
157 assert(mSlots[s].mBufferState == BufferSlot::FREE);
158 if (mSlots[s].mGraphicBuffer != NULL) {
159 mCore->freeBufferLocked(s);
160 *returnFlags |= RELEASE_ALL_BUFFERS;
161 }
162 }
163
Dan Stoza9f3053d2014-03-06 15:14:33 -0800164 int dequeuedCount = 0;
165 int acquiredCount = 0;
166 for (int s = 0; s < maxBufferCount; ++s) {
167 switch (mSlots[s].mBufferState) {
168 case BufferSlot::DEQUEUED:
169 ++dequeuedCount;
170 break;
171 case BufferSlot::ACQUIRED:
172 ++acquiredCount;
173 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800174 default:
175 break;
176 }
177 }
178
179 // Producers are not allowed to dequeue more than one buffer if they
180 // did not set a buffer count
181 if (!mCore->mOverrideMaxBufferCount && dequeuedCount) {
182 BQ_LOGE("%s: can't dequeue multiple buffers without setting the "
183 "buffer count", caller);
184 return INVALID_OPERATION;
185 }
186
187 // See whether a buffer has been queued since the last
188 // setBufferCount so we know whether to perform the min undequeued
189 // buffers check below
190 if (mCore->mBufferHasBeenQueued) {
191 // Make sure the producer is not trying to dequeue more buffers
192 // than allowed
193 const int newUndequeuedCount =
194 maxBufferCount - (dequeuedCount + 1);
195 const int minUndequeuedCount =
196 mCore->getMinUndequeuedBufferCountLocked(async);
197 if (newUndequeuedCount < minUndequeuedCount) {
198 BQ_LOGE("%s: min undequeued buffer count (%d) exceeded "
199 "(dequeued=%d undequeued=%d)",
200 caller, minUndequeuedCount,
201 dequeuedCount, newUndequeuedCount);
202 return INVALID_OPERATION;
203 }
204 }
205
Dan Stoza8dddc992015-04-16 15:39:18 -0700206 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
207
Dan Stozaae3c3682014-04-18 15:43:35 -0700208 // If we disconnect and reconnect quickly, we can be in a state where
209 // our slots are empty but we have many buffers in the queue. This can
210 // cause us to run out of memory if we outrun the consumer. Wait here if
211 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700212 bool tooManyBuffers = mCore->mQueue.size()
213 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700214 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700215 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700216 mCore->mQueue.size());
Dan Stoza8dddc992015-04-16 15:39:18 -0700217 } else {
218 if (!mCore->mFreeBuffers.empty()) {
219 auto slot = mCore->mFreeBuffers.begin();
220 *found = *slot;
221 mCore->mFreeBuffers.erase(slot);
222 } else if (!mCore->mFreeSlots.empty()) {
223 auto slot = mCore->mFreeSlots.begin();
224 *found = *slot;
225 mCore->mFreeSlots.erase(slot);
226 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700227 }
228
229 // If no buffer is found, or if the queue has too many buffers
230 // outstanding, wait for a buffer to be acquired or released, or for the
231 // max buffer count to change.
232 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
233 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800234 if (tryAgain) {
235 // Return an error if we're in non-blocking mode (producer and
236 // consumer are controlled by the application).
237 // However, the consumer is allowed to briefly acquire an extra
238 // buffer (which could cause us to have to wait here), which is
239 // okay, since it is only used to implement an atomic acquire +
240 // release (e.g., in GLConsumer::updateTexImage())
241 if (mCore->mDequeueBufferCannotBlock &&
242 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
243 return WOULD_BLOCK;
244 }
245 mCore->mDequeueCondition.wait(mCore->mMutex);
246 }
247 } // while (tryAgain)
248
249 return NO_ERROR;
250}
251
Dan Stoza289ade12014-02-28 11:17:17 -0800252status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
253 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800254 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800255 ATRACE_CALL();
256 { // Autolock scope
257 Mutex::Autolock lock(mCore->mMutex);
258 mConsumerName = mCore->mConsumerName;
259 } // Autolock scope
260
261 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
262 async ? "true" : "false", width, height, format, usage);
263
264 if ((width && !height) || (!width && height)) {
265 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
266 return BAD_VALUE;
267 }
268
269 status_t returnFlags = NO_ERROR;
270 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
271 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800272 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800273
274 { // Autolock scope
275 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700276 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800277
278 if (format == 0) {
279 format = mCore->mDefaultBufferFormat;
280 }
281
282 // Enable the usage bits the consumer requested
283 usage |= mCore->mConsumerUsageBits;
284
Dan Stoza9f3053d2014-03-06 15:14:33 -0800285 int found;
286 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
287 &found, &returnFlags);
288 if (status != NO_ERROR) {
289 return status;
290 }
Dan Stoza289ade12014-02-28 11:17:17 -0800291
292 // This should not happen
293 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
294 BQ_LOGE("dequeueBuffer: no available buffer slots");
295 return -EBUSY;
296 }
297
298 *outSlot = found;
299 ATRACE_BUFFER_INDEX(found);
300
Dan Stoza9f3053d2014-03-06 15:14:33 -0800301 attachedByConsumer = mSlots[found].mAttachedByConsumer;
302
Dan Stoza289ade12014-02-28 11:17:17 -0800303 const bool useDefaultSize = !width && !height;
304 if (useDefaultSize) {
305 width = mCore->mDefaultWidth;
306 height = mCore->mDefaultHeight;
307 }
308
309 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
310
311 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
312 if ((buffer == NULL) ||
313 (static_cast<uint32_t>(buffer->width) != width) ||
314 (static_cast<uint32_t>(buffer->height) != height) ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800315 (buffer->format != format) ||
Dan Stoza289ade12014-02-28 11:17:17 -0800316 ((static_cast<uint32_t>(buffer->usage) & usage) != usage))
317 {
318 mSlots[found].mAcquireCalled = false;
319 mSlots[found].mGraphicBuffer = NULL;
320 mSlots[found].mRequestBufferCalled = false;
321 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
322 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
323 mSlots[found].mFence = Fence::NO_FENCE;
324
325 returnFlags |= BUFFER_NEEDS_REALLOCATION;
326 }
327
328 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
329 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
330 "slot=%d w=%d h=%d format=%u",
331 found, buffer->width, buffer->height, buffer->format);
332 }
333
334 eglDisplay = mSlots[found].mEglDisplay;
335 eglFence = mSlots[found].mEglFence;
336 *outFence = mSlots[found].mFence;
337 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
338 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza8dddc992015-04-16 15:39:18 -0700339
340 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800341 } // Autolock scope
342
343 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
344 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700345 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800346 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800347 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800348 if (graphicBuffer == NULL) {
349 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
350 return error;
351 }
352
353 { // Autolock scope
354 Mutex::Autolock lock(mCore->mMutex);
355
356 if (mCore->mIsAbandoned) {
357 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
358 return NO_INIT;
359 }
360
Dan Stoza289ade12014-02-28 11:17:17 -0800361 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
362 } // Autolock scope
363 }
364
Dan Stoza9f3053d2014-03-06 15:14:33 -0800365 if (attachedByConsumer) {
366 returnFlags |= BUFFER_NEEDS_REALLOCATION;
367 }
368
Dan Stoza289ade12014-02-28 11:17:17 -0800369 if (eglFence != EGL_NO_SYNC_KHR) {
370 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
371 1000000000);
372 // If something goes wrong, log the error, but return the buffer without
373 // synchronizing access to it. It's too late at this point to abort the
374 // dequeue operation.
375 if (result == EGL_FALSE) {
376 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
377 eglGetError());
378 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
379 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
380 }
381 eglDestroySyncKHR(eglDisplay, eglFence);
382 }
383
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700384 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
385 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800386 mSlots[*outSlot].mFrameNumber,
387 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
388
389 return returnFlags;
390}
391
Dan Stoza9f3053d2014-03-06 15:14:33 -0800392status_t BufferQueueProducer::detachBuffer(int slot) {
393 ATRACE_CALL();
394 ATRACE_BUFFER_INDEX(slot);
395 BQ_LOGV("detachBuffer(P): slot %d", slot);
396 Mutex::Autolock lock(mCore->mMutex);
397
398 if (mCore->mIsAbandoned) {
399 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
400 return NO_INIT;
401 }
402
403 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
404 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
405 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
406 return BAD_VALUE;
407 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
408 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
409 "(state = %d)", slot, mSlots[slot].mBufferState);
410 return BAD_VALUE;
411 } else if (!mSlots[slot].mRequestBufferCalled) {
412 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
413 slot);
414 return BAD_VALUE;
415 }
416
417 mCore->freeBufferLocked(slot);
418 mCore->mDequeueCondition.broadcast();
Dan Stoza8dddc992015-04-16 15:39:18 -0700419 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800420
421 return NO_ERROR;
422}
423
Dan Stozad9822a32014-03-28 15:25:31 -0700424status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
425 sp<Fence>* outFence) {
426 ATRACE_CALL();
427
428 if (outBuffer == NULL) {
429 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
430 return BAD_VALUE;
431 } else if (outFence == NULL) {
432 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
433 return BAD_VALUE;
434 }
435
436 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700437 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700438
439 if (mCore->mIsAbandoned) {
440 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
441 return NO_INIT;
442 }
443
Dan Stoza8dddc992015-04-16 15:39:18 -0700444 if (mCore->mFreeBuffers.empty()) {
Dan Stozad9822a32014-03-28 15:25:31 -0700445 return NO_MEMORY;
446 }
447
Dan Stoza8dddc992015-04-16 15:39:18 -0700448 int found = mCore->mFreeBuffers.front();
449 mCore->mFreeBuffers.remove(found);
450
Dan Stozad9822a32014-03-28 15:25:31 -0700451 BQ_LOGV("detachNextBuffer detached slot %d", found);
452
453 *outBuffer = mSlots[found].mGraphicBuffer;
454 *outFence = mSlots[found].mFence;
455 mCore->freeBufferLocked(found);
Dan Stoza8dddc992015-04-16 15:39:18 -0700456 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700457
458 return NO_ERROR;
459}
460
Dan Stoza9f3053d2014-03-06 15:14:33 -0800461status_t BufferQueueProducer::attachBuffer(int* outSlot,
462 const sp<android::GraphicBuffer>& buffer) {
463 ATRACE_CALL();
464
465 if (outSlot == NULL) {
466 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
467 return BAD_VALUE;
468 } else if (buffer == NULL) {
469 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
470 return BAD_VALUE;
471 }
472
473 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700474 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800475
476 status_t returnFlags = NO_ERROR;
477 int found;
478 // TODO: Should we provide an async flag to attachBuffer? It seems
479 // unlikely that buffers which we are attaching to a BufferQueue will
480 // be asynchronous (droppable), but it may not be impossible.
481 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
482 &found, &returnFlags);
483 if (status != NO_ERROR) {
484 return status;
485 }
486
487 // This should not happen
488 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
489 BQ_LOGE("attachBuffer(P): no available buffer slots");
490 return -EBUSY;
491 }
492
493 *outSlot = found;
494 ATRACE_BUFFER_INDEX(*outSlot);
495 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
496 *outSlot, returnFlags);
497
498 mSlots[*outSlot].mGraphicBuffer = buffer;
499 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
500 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
501 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700502 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800503
Dan Stoza8dddc992015-04-16 15:39:18 -0700504 mCore->validateConsistencyLocked();
505
Dan Stoza9f3053d2014-03-06 15:14:33 -0800506 return returnFlags;
507}
508
Dan Stoza289ade12014-02-28 11:17:17 -0800509status_t BufferQueueProducer::queueBuffer(int slot,
510 const QueueBufferInput &input, QueueBufferOutput *output) {
511 ATRACE_CALL();
512 ATRACE_BUFFER_INDEX(slot);
513
514 int64_t timestamp;
515 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800516 android_dataspace dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800517 Rect crop;
518 int scalingMode;
519 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700520 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800521 bool async;
522 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800523 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
524 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700525 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800526
527 if (fence == NULL) {
528 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800529 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800530 }
531
532 switch (scalingMode) {
533 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
534 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
535 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
536 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
537 break;
538 default:
539 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800540 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800541 }
542
Dan Stoza8dc55392014-11-04 11:37:46 -0800543 sp<IConsumerListener> frameAvailableListener;
544 sp<IConsumerListener> frameReplacedListener;
545 int callbackTicket = 0;
546 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800547 { // Autolock scope
548 Mutex::Autolock lock(mCore->mMutex);
549
550 if (mCore->mIsAbandoned) {
551 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
552 return NO_INIT;
553 }
554
555 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
556 if (async && mCore->mOverrideMaxBufferCount) {
557 // FIXME: Some drivers are manually setting the buffer count
558 // (which they shouldn't), so we do this extra test here to
559 // handle that case. This is TEMPORARY until we get this fixed.
560 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
561 BQ_LOGE("queueBuffer: async mode is invalid with "
562 "buffer count override");
563 return BAD_VALUE;
564 }
565 }
566
567 if (slot < 0 || slot >= maxBufferCount) {
568 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
569 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800570 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800571 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
572 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
573 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800574 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800575 } else if (!mSlots[slot].mRequestBufferCalled) {
576 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
577 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800578 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800579 }
580
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800581 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700582 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800583 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800584 crop.left, crop.top, crop.right, crop.bottom, transform,
585 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800586
587 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
588 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
589 Rect croppedRect;
590 crop.intersect(bufferRect, &croppedRect);
591 if (croppedRect != crop) {
592 BQ_LOGE("queueBuffer: crop rect is not contained within the "
593 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800594 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800595 }
596
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800597 // Override UNKNOWN dataspace with consumer default
598 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
599 dataSpace = mCore->mDefaultBufferDataSpace;
600 }
601
Dan Stoza289ade12014-02-28 11:17:17 -0800602 mSlots[slot].mFence = fence;
603 mSlots[slot].mBufferState = BufferSlot::QUEUED;
604 ++mCore->mFrameCounter;
605 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
606
Dan Stoza289ade12014-02-28 11:17:17 -0800607 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
608 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
609 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800610 item.mTransform = transform &
611 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800612 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800613 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
614 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800615 item.mTimestamp = timestamp;
616 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800617 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800618 item.mFrameNumber = mCore->mFrameCounter;
619 item.mSlot = slot;
620 item.mFence = fence;
621 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700622 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800623
Ruben Brunk1681d952014-06-27 15:51:55 -0700624 mStickyTransform = stickyTransform;
625
Dan Stoza289ade12014-02-28 11:17:17 -0800626 if (mCore->mQueue.empty()) {
627 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
628 // and simply queue this buffer
629 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800630 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800631 } else {
632 // When the queue is not empty, we need to look at the front buffer
633 // state to see if we need to replace it
634 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
635 if (front->mIsDroppable) {
636 // If the front queued buffer is still being tracked, we first
637 // mark it as freed
638 if (mCore->stillTracking(front)) {
639 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza8dddc992015-04-16 15:39:18 -0700640 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800641 }
642 // Overwrite the droppable buffer with the incoming one
643 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800644 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800645 } else {
646 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800647 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800648 }
649 }
650
651 mCore->mBufferHasBeenQueued = true;
652 mCore->mDequeueCondition.broadcast();
653
654 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800655 mCore->mTransformHint,
656 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800657
658 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800659
660 // Take a ticket for the callback functions
661 callbackTicket = mNextCallbackTicket++;
Dan Stoza8dddc992015-04-16 15:39:18 -0700662
663 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800664 } // Autolock scope
665
Eric Penner99a0afb2014-09-30 11:28:30 -0700666 // Wait without lock held
667 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
668 // Waiting here allows for two full buffers to be queued but not a
669 // third. In the event that frames take varying time, this makes a
670 // small trade-off in favor of latency rather than throughput.
671 mLastQueueBufferFence->waitForever("Throttling EGL Production");
672 mLastQueueBufferFence = fence;
673 }
674
Dan Stoza8dc55392014-11-04 11:37:46 -0800675 // Don't send the GraphicBuffer through the callback, and don't send
676 // the slot number, since the consumer shouldn't need it
677 item.mGraphicBuffer.clear();
678 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
679
680 // Call back without the main BufferQueue lock held, but with the callback
681 // lock held so we can ensure that callbacks occur in order
682 {
683 Mutex::Autolock lock(mCallbackMutex);
684 while (callbackTicket != mCurrentCallbackTicket) {
685 mCallbackCondition.wait(mCallbackMutex);
686 }
687
688 if (frameAvailableListener != NULL) {
689 frameAvailableListener->onFrameAvailable(item);
690 } else if (frameReplacedListener != NULL) {
691 frameReplacedListener->onFrameReplaced(item);
692 }
693
694 ++mCurrentCallbackTicket;
695 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800696 }
697
698 return NO_ERROR;
699}
700
701void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
702 ATRACE_CALL();
703 BQ_LOGV("cancelBuffer: slot %d", slot);
704 Mutex::Autolock lock(mCore->mMutex);
705
706 if (mCore->mIsAbandoned) {
707 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
708 return;
709 }
710
Dan Stoza3e96f192014-03-03 10:16:19 -0800711 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800712 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800713 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800714 return;
715 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
716 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
717 "(state = %d)", slot, mSlots[slot].mBufferState);
718 return;
719 } else if (fence == NULL) {
720 BQ_LOGE("cancelBuffer: fence is NULL");
721 return;
722 }
723
Dan Stoza8dddc992015-04-16 15:39:18 -0700724 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800725 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800726 mSlots[slot].mFence = fence;
727 mCore->mDequeueCondition.broadcast();
Dan Stoza8dddc992015-04-16 15:39:18 -0700728 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800729}
730
731int BufferQueueProducer::query(int what, int *outValue) {
732 ATRACE_CALL();
733 Mutex::Autolock lock(mCore->mMutex);
734
735 if (outValue == NULL) {
736 BQ_LOGE("query: outValue was NULL");
737 return BAD_VALUE;
738 }
739
740 if (mCore->mIsAbandoned) {
741 BQ_LOGE("query: BufferQueue has been abandoned");
742 return NO_INIT;
743 }
744
745 int value;
746 switch (what) {
747 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800748 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800749 break;
750 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800751 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800752 break;
753 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800754 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800755 break;
756 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
757 value = mCore->getMinUndequeuedBufferCountLocked(false);
758 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700759 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800760 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700761 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800762 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
763 value = (mCore->mQueue.size() > 1);
764 break;
765 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800766 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800767 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800768 case NATIVE_WINDOW_DEFAULT_DATASPACE:
769 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
770 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800771 default:
772 return BAD_VALUE;
773 }
774
775 BQ_LOGV("query: %d? %d", what, value);
776 *outValue = value;
777 return NO_ERROR;
778}
779
Dan Stozaf0eaf252014-03-21 13:05:51 -0700780status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800781 int api, bool producerControlledByApp, QueueBufferOutput *output) {
782 ATRACE_CALL();
783 Mutex::Autolock lock(mCore->mMutex);
784 mConsumerName = mCore->mConsumerName;
785 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
786 producerControlledByApp ? "true" : "false");
787
Dan Stozaae3c3682014-04-18 15:43:35 -0700788 if (mCore->mIsAbandoned) {
789 BQ_LOGE("connect(P): BufferQueue has been abandoned");
790 return NO_INIT;
791 }
Dan Stoza289ade12014-02-28 11:17:17 -0800792
Dan Stozaae3c3682014-04-18 15:43:35 -0700793 if (mCore->mConsumerListener == NULL) {
794 BQ_LOGE("connect(P): BufferQueue has no consumer");
795 return NO_INIT;
796 }
Dan Stoza289ade12014-02-28 11:17:17 -0800797
Dan Stozaae3c3682014-04-18 15:43:35 -0700798 if (output == NULL) {
799 BQ_LOGE("connect(P): output was NULL");
800 return BAD_VALUE;
801 }
Dan Stoza289ade12014-02-28 11:17:17 -0800802
Dan Stozaae3c3682014-04-18 15:43:35 -0700803 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
804 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
805 mCore->mConnectedApi, api);
806 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800807 }
808
809 int status = NO_ERROR;
810 switch (api) {
811 case NATIVE_WINDOW_API_EGL:
812 case NATIVE_WINDOW_API_CPU:
813 case NATIVE_WINDOW_API_MEDIA:
814 case NATIVE_WINDOW_API_CAMERA:
815 mCore->mConnectedApi = api;
816 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800817 mCore->mTransformHint,
818 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800819
820 // Set up a death notification so that we can disconnect
821 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700822 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800823 IInterface::asBinder(listener)->remoteBinder() != NULL) {
824 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800825 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700826 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800827 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
828 strerror(-status), status);
829 }
830 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700831 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800832 break;
833 default:
834 BQ_LOGE("connect(P): unknown API %d", api);
835 status = BAD_VALUE;
836 break;
837 }
838
839 mCore->mBufferHasBeenQueued = false;
840 mCore->mDequeueBufferCannotBlock =
841 mCore->mConsumerControlledByApp && producerControlledByApp;
842
843 return status;
844}
845
846status_t BufferQueueProducer::disconnect(int api) {
847 ATRACE_CALL();
848 BQ_LOGV("disconnect(P): api %d", api);
849
850 int status = NO_ERROR;
851 sp<IConsumerListener> listener;
852 { // Autolock scope
853 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700854 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800855
856 if (mCore->mIsAbandoned) {
857 // It's not really an error to disconnect after the surface has
858 // been abandoned; it should just be a no-op.
859 return NO_ERROR;
860 }
861
862 switch (api) {
863 case NATIVE_WINDOW_API_EGL:
864 case NATIVE_WINDOW_API_CPU:
865 case NATIVE_WINDOW_API_MEDIA:
866 case NATIVE_WINDOW_API_CAMERA:
867 if (mCore->mConnectedApi == api) {
868 mCore->freeAllBuffersLocked();
869
870 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700871 if (mCore->mConnectedProducerListener != NULL) {
872 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800873 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800874 // This can fail if we're here because of the death
875 // notification, but we just ignore it
876 token->unlinkToDeath(
877 static_cast<IBinder::DeathRecipient*>(this));
878 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700879 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800880 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800881 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800882 mCore->mDequeueCondition.broadcast();
883 listener = mCore->mConsumerListener;
Michael Lentine45e2fc22014-08-08 10:30:44 -0700884 } else {
885 BQ_LOGE("disconnect(P): connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800886 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800887 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800888 }
889 break;
890 default:
891 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800892 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800893 break;
894 }
895 } // Autolock scope
896
897 // Call back without lock held
898 if (listener != NULL) {
899 listener->onBuffersReleased();
900 }
901
902 return status;
903}
904
Jesse Hall399184a2014-03-03 15:42:54 -0800905status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900906 sp<IConsumerListener> listener;
907 { // Autolock scope
908 Mutex::Autolock _l(mCore->mMutex);
909 mCore->mSidebandStream = stream;
910 listener = mCore->mConsumerListener;
911 } // Autolock scope
912
913 if (listener != NULL) {
914 listener->onSidebandStreamChanged();
915 }
Jesse Hall399184a2014-03-03 15:42:54 -0800916 return NO_ERROR;
917}
918
Dan Stoza29a3e902014-06-20 13:13:57 -0700919void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800920 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -0700921 ATRACE_CALL();
922 while (true) {
923 Vector<int> freeSlots;
924 size_t newBufferCount = 0;
925 uint32_t allocWidth = 0;
926 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800927 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -0700928 uint32_t allocUsage = 0;
929 { // Autolock scope
930 Mutex::Autolock lock(mCore->mMutex);
931 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -0700932
Antoine Labour78014f32014-07-15 21:17:03 -0700933 int currentBufferCount = 0;
934 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
935 if (mSlots[slot].mGraphicBuffer != NULL) {
936 ++currentBufferCount;
937 } else {
938 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
939 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
940 slot);
941 continue;
942 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700943
Antoine Labour11f14872014-07-25 18:14:42 -0700944 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -0700945 }
946 }
947
948 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
949 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
950 currentBufferCount, maxBufferCount);
951 if (maxBufferCount <= currentBufferCount)
952 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800953 newBufferCount =
954 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -0700955 if (freeSlots.size() < newBufferCount) {
956 BQ_LOGE("allocateBuffers: ran out of free slots");
957 return;
958 }
959 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
960 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
961 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
962 allocUsage = usage | mCore->mConsumerUsageBits;
963
964 mCore->mIsAllocating = true;
965 } // Autolock scope
966
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800967 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -0700968 for (size_t i = 0; i < newBufferCount; ++i) {
969 status_t result = NO_ERROR;
970 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
971 allocWidth, allocHeight, allocFormat, allocUsage, &result));
972 if (result != NO_ERROR) {
973 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
974 " %u, usage %u)", width, height, format, usage);
975 Mutex::Autolock lock(mCore->mMutex);
976 mCore->mIsAllocating = false;
977 mCore->mIsAllocatingCondition.broadcast();
978 return;
979 }
980 buffers.push_back(graphicBuffer);
981 }
982
983 { // Autolock scope
984 Mutex::Autolock lock(mCore->mMutex);
985 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
986 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800987 PixelFormat checkFormat = format != 0 ?
988 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -0700989 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
990 if (checkWidth != allocWidth || checkHeight != allocHeight ||
991 checkFormat != allocFormat || checkUsage != allocUsage) {
992 // Something changed while we released the lock. Retry.
993 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
994 mCore->mIsAllocating = false;
995 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -0700996 continue;
997 }
998
Antoine Labour78014f32014-07-15 21:17:03 -0700999 for (size_t i = 0; i < newBufferCount; ++i) {
1000 int slot = freeSlots[i];
1001 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1002 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1003 // allocated.
1004 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1005 "Dropping allocated buffer.", slot);
1006 continue;
1007 }
1008 mCore->freeBufferLocked(slot); // Clean up the slot first
1009 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001010 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza8dddc992015-04-16 15:39:18 -07001011
1012 // freeBufferLocked puts this slot on the free slots list. Since
1013 // we then attached a buffer, move the slot to free buffer list.
1014 mCore->mFreeSlots.erase(slot);
1015 mCore->mFreeBuffers.push_front(slot);
1016
Antoine Labour78014f32014-07-15 21:17:03 -07001017 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1018 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001019
Antoine Labour78014f32014-07-15 21:17:03 -07001020 mCore->mIsAllocating = false;
1021 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza8dddc992015-04-16 15:39:18 -07001022 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001023 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001024 }
1025}
1026
Dan Stoza289ade12014-02-28 11:17:17 -08001027void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1028 // If we're here, it means that a producer we were connected to died.
1029 // We're guaranteed that we are still connected to it because we remove
1030 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1031 // without synchronization here.
1032 int api = mCore->mConnectedApi;
1033 disconnect(api);
1034}
1035
1036} // namespace android