blob: a27d5f033ec8cf74302bd11e48ff8111ed595ca6 [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 Stoza0de7ea72015-04-23 13:20:51 -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 Stoza0de7ea72015-04-23 13:20:51 -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 // Only return free slots up to the max buffer count
225 if (*slot < maxBufferCount) {
226 *found = *slot;
227 mCore->mFreeSlots.erase(slot);
228 }
229 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700230 }
231
232 // If no buffer is found, or if the queue has too many buffers
233 // outstanding, wait for a buffer to be acquired or released, or for the
234 // max buffer count to change.
235 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
236 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800237 if (tryAgain) {
238 // Return an error if we're in non-blocking mode (producer and
239 // consumer are controlled by the application).
240 // However, the consumer is allowed to briefly acquire an extra
241 // buffer (which could cause us to have to wait here), which is
242 // okay, since it is only used to implement an atomic acquire +
243 // release (e.g., in GLConsumer::updateTexImage())
244 if (mCore->mDequeueBufferCannotBlock &&
245 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
246 return WOULD_BLOCK;
247 }
248 mCore->mDequeueCondition.wait(mCore->mMutex);
249 }
250 } // while (tryAgain)
251
252 return NO_ERROR;
253}
254
Dan Stoza289ade12014-02-28 11:17:17 -0800255status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
256 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800257 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800258 ATRACE_CALL();
259 { // Autolock scope
260 Mutex::Autolock lock(mCore->mMutex);
261 mConsumerName = mCore->mConsumerName;
262 } // Autolock scope
263
264 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
265 async ? "true" : "false", width, height, format, usage);
266
267 if ((width && !height) || (!width && height)) {
268 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
269 return BAD_VALUE;
270 }
271
272 status_t returnFlags = NO_ERROR;
273 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
274 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800275 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800276
277 { // Autolock scope
278 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700279 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800280
281 if (format == 0) {
282 format = mCore->mDefaultBufferFormat;
283 }
284
285 // Enable the usage bits the consumer requested
286 usage |= mCore->mConsumerUsageBits;
287
Dan Stoza9f3053d2014-03-06 15:14:33 -0800288 int found;
289 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
290 &found, &returnFlags);
291 if (status != NO_ERROR) {
292 return status;
293 }
Dan Stoza289ade12014-02-28 11:17:17 -0800294
295 // This should not happen
296 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
297 BQ_LOGE("dequeueBuffer: no available buffer slots");
298 return -EBUSY;
299 }
300
301 *outSlot = found;
302 ATRACE_BUFFER_INDEX(found);
303
Dan Stoza9f3053d2014-03-06 15:14:33 -0800304 attachedByConsumer = mSlots[found].mAttachedByConsumer;
305
Dan Stoza289ade12014-02-28 11:17:17 -0800306 const bool useDefaultSize = !width && !height;
307 if (useDefaultSize) {
308 width = mCore->mDefaultWidth;
309 height = mCore->mDefaultHeight;
310 }
311
312 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
313
314 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
315 if ((buffer == NULL) ||
316 (static_cast<uint32_t>(buffer->width) != width) ||
317 (static_cast<uint32_t>(buffer->height) != height) ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800318 (buffer->format != format) ||
Dan Stoza289ade12014-02-28 11:17:17 -0800319 ((static_cast<uint32_t>(buffer->usage) & usage) != usage))
320 {
321 mSlots[found].mAcquireCalled = false;
322 mSlots[found].mGraphicBuffer = NULL;
323 mSlots[found].mRequestBufferCalled = false;
324 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
325 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
326 mSlots[found].mFence = Fence::NO_FENCE;
327
328 returnFlags |= BUFFER_NEEDS_REALLOCATION;
329 }
330
331 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
332 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
333 "slot=%d w=%d h=%d format=%u",
334 found, buffer->width, buffer->height, buffer->format);
335 }
336
337 eglDisplay = mSlots[found].mEglDisplay;
338 eglFence = mSlots[found].mEglFence;
339 *outFence = mSlots[found].mFence;
340 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
341 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700342
343 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800344 } // Autolock scope
345
346 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
347 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700348 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800349 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800350 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800351 if (graphicBuffer == NULL) {
352 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
353 return error;
354 }
355
356 { // Autolock scope
357 Mutex::Autolock lock(mCore->mMutex);
358
359 if (mCore->mIsAbandoned) {
360 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
361 return NO_INIT;
362 }
363
Dan Stoza289ade12014-02-28 11:17:17 -0800364 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
365 } // Autolock scope
366 }
367
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 if (attachedByConsumer) {
369 returnFlags |= BUFFER_NEEDS_REALLOCATION;
370 }
371
Dan Stoza289ade12014-02-28 11:17:17 -0800372 if (eglFence != EGL_NO_SYNC_KHR) {
373 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
374 1000000000);
375 // If something goes wrong, log the error, but return the buffer without
376 // synchronizing access to it. It's too late at this point to abort the
377 // dequeue operation.
378 if (result == EGL_FALSE) {
379 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
380 eglGetError());
381 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
382 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
383 }
384 eglDestroySyncKHR(eglDisplay, eglFence);
385 }
386
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700387 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
388 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800389 mSlots[*outSlot].mFrameNumber,
390 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
391
392 return returnFlags;
393}
394
Dan Stoza9f3053d2014-03-06 15:14:33 -0800395status_t BufferQueueProducer::detachBuffer(int slot) {
396 ATRACE_CALL();
397 ATRACE_BUFFER_INDEX(slot);
398 BQ_LOGV("detachBuffer(P): slot %d", slot);
399 Mutex::Autolock lock(mCore->mMutex);
400
401 if (mCore->mIsAbandoned) {
402 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
403 return NO_INIT;
404 }
405
406 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
407 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
408 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
409 return BAD_VALUE;
410 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
411 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
412 "(state = %d)", slot, mSlots[slot].mBufferState);
413 return BAD_VALUE;
414 } else if (!mSlots[slot].mRequestBufferCalled) {
415 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
416 slot);
417 return BAD_VALUE;
418 }
419
420 mCore->freeBufferLocked(slot);
421 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700422 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800423
424 return NO_ERROR;
425}
426
Dan Stozad9822a32014-03-28 15:25:31 -0700427status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
428 sp<Fence>* outFence) {
429 ATRACE_CALL();
430
431 if (outBuffer == NULL) {
432 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
433 return BAD_VALUE;
434 } else if (outFence == NULL) {
435 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
436 return BAD_VALUE;
437 }
438
439 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700440 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700441
442 if (mCore->mIsAbandoned) {
443 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
444 return NO_INIT;
445 }
446
Dan Stoza0de7ea72015-04-23 13:20:51 -0700447 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000448 return NO_MEMORY;
449 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700450
Dan Stoza0de7ea72015-04-23 13:20:51 -0700451 int found = mCore->mFreeBuffers.front();
452 mCore->mFreeBuffers.remove(found);
453
Dan Stozad9822a32014-03-28 15:25:31 -0700454 BQ_LOGV("detachNextBuffer detached slot %d", found);
455
456 *outBuffer = mSlots[found].mGraphicBuffer;
457 *outFence = mSlots[found].mFence;
458 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700459 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700460
461 return NO_ERROR;
462}
463
Dan Stoza9f3053d2014-03-06 15:14:33 -0800464status_t BufferQueueProducer::attachBuffer(int* outSlot,
465 const sp<android::GraphicBuffer>& buffer) {
466 ATRACE_CALL();
467
468 if (outSlot == NULL) {
469 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
470 return BAD_VALUE;
471 } else if (buffer == NULL) {
472 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
473 return BAD_VALUE;
474 }
475
476 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700477 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800478
479 status_t returnFlags = NO_ERROR;
480 int found;
481 // TODO: Should we provide an async flag to attachBuffer? It seems
482 // unlikely that buffers which we are attaching to a BufferQueue will
483 // be asynchronous (droppable), but it may not be impossible.
484 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
485 &found, &returnFlags);
486 if (status != NO_ERROR) {
487 return status;
488 }
489
490 // This should not happen
491 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
492 BQ_LOGE("attachBuffer(P): no available buffer slots");
493 return -EBUSY;
494 }
495
496 *outSlot = found;
497 ATRACE_BUFFER_INDEX(*outSlot);
498 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
499 *outSlot, returnFlags);
500
501 mSlots[*outSlot].mGraphicBuffer = buffer;
502 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
503 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
504 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700505 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800506
Dan Stoza0de7ea72015-04-23 13:20:51 -0700507 mCore->validateConsistencyLocked();
508
Dan Stoza9f3053d2014-03-06 15:14:33 -0800509 return returnFlags;
510}
511
Dan Stoza289ade12014-02-28 11:17:17 -0800512status_t BufferQueueProducer::queueBuffer(int slot,
513 const QueueBufferInput &input, QueueBufferOutput *output) {
514 ATRACE_CALL();
515 ATRACE_BUFFER_INDEX(slot);
516
517 int64_t timestamp;
518 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800519 android_dataspace dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800520 Rect crop;
521 int scalingMode;
522 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700523 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800524 bool async;
525 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800526 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
527 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700528 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800529
530 if (fence == NULL) {
531 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800532 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800533 }
534
535 switch (scalingMode) {
536 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
537 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
538 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
539 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
540 break;
541 default:
542 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800543 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800544 }
545
Dan Stoza8dc55392014-11-04 11:37:46 -0800546 sp<IConsumerListener> frameAvailableListener;
547 sp<IConsumerListener> frameReplacedListener;
548 int callbackTicket = 0;
549 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800550 { // Autolock scope
551 Mutex::Autolock lock(mCore->mMutex);
552
553 if (mCore->mIsAbandoned) {
554 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
555 return NO_INIT;
556 }
557
558 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
559 if (async && mCore->mOverrideMaxBufferCount) {
560 // FIXME: Some drivers are manually setting the buffer count
561 // (which they shouldn't), so we do this extra test here to
562 // handle that case. This is TEMPORARY until we get this fixed.
563 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
564 BQ_LOGE("queueBuffer: async mode is invalid with "
565 "buffer count override");
566 return BAD_VALUE;
567 }
568 }
569
570 if (slot < 0 || slot >= maxBufferCount) {
571 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
572 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800573 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800574 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
575 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
576 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800577 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800578 } else if (!mSlots[slot].mRequestBufferCalled) {
579 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
580 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800581 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800582 }
583
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800584 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700585 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800586 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800587 crop.left, crop.top, crop.right, crop.bottom, transform,
588 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800589
590 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
591 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
592 Rect croppedRect;
593 crop.intersect(bufferRect, &croppedRect);
594 if (croppedRect != crop) {
595 BQ_LOGE("queueBuffer: crop rect is not contained within the "
596 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800597 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800598 }
599
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800600 // Override UNKNOWN dataspace with consumer default
601 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
602 dataSpace = mCore->mDefaultBufferDataSpace;
603 }
604
Dan Stoza289ade12014-02-28 11:17:17 -0800605 mSlots[slot].mFence = fence;
606 mSlots[slot].mBufferState = BufferSlot::QUEUED;
607 ++mCore->mFrameCounter;
608 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
609
Dan Stoza289ade12014-02-28 11:17:17 -0800610 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
611 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
612 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800613 item.mTransform = transform &
614 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800615 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800616 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
617 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800618 item.mTimestamp = timestamp;
619 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800620 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800621 item.mFrameNumber = mCore->mFrameCounter;
622 item.mSlot = slot;
623 item.mFence = fence;
624 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700625 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800626
Ruben Brunk1681d952014-06-27 15:51:55 -0700627 mStickyTransform = stickyTransform;
628
Dan Stoza289ade12014-02-28 11:17:17 -0800629 if (mCore->mQueue.empty()) {
630 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
631 // and simply queue this buffer
632 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800633 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800634 } else {
635 // When the queue is not empty, we need to look at the front buffer
636 // state to see if we need to replace it
637 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
638 if (front->mIsDroppable) {
639 // If the front queued buffer is still being tracked, we first
640 // mark it as freed
641 if (mCore->stillTracking(front)) {
642 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700643 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800644 }
645 // Overwrite the droppable buffer with the incoming one
646 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800647 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800648 } else {
649 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800650 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800651 }
652 }
653
654 mCore->mBufferHasBeenQueued = true;
655 mCore->mDequeueCondition.broadcast();
656
657 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800658 mCore->mTransformHint,
659 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800660
661 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800662
663 // Take a ticket for the callback functions
664 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700665
666 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800667 } // Autolock scope
668
Eric Penner99a0afb2014-09-30 11:28:30 -0700669 // Wait without lock held
670 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
671 // Waiting here allows for two full buffers to be queued but not a
672 // third. In the event that frames take varying time, this makes a
673 // small trade-off in favor of latency rather than throughput.
674 mLastQueueBufferFence->waitForever("Throttling EGL Production");
675 mLastQueueBufferFence = fence;
676 }
677
Dan Stoza8dc55392014-11-04 11:37:46 -0800678 // Don't send the GraphicBuffer through the callback, and don't send
679 // the slot number, since the consumer shouldn't need it
680 item.mGraphicBuffer.clear();
681 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
682
683 // Call back without the main BufferQueue lock held, but with the callback
684 // lock held so we can ensure that callbacks occur in order
685 {
686 Mutex::Autolock lock(mCallbackMutex);
687 while (callbackTicket != mCurrentCallbackTicket) {
688 mCallbackCondition.wait(mCallbackMutex);
689 }
690
691 if (frameAvailableListener != NULL) {
692 frameAvailableListener->onFrameAvailable(item);
693 } else if (frameReplacedListener != NULL) {
694 frameReplacedListener->onFrameReplaced(item);
695 }
696
697 ++mCurrentCallbackTicket;
698 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800699 }
700
701 return NO_ERROR;
702}
703
704void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
705 ATRACE_CALL();
706 BQ_LOGV("cancelBuffer: slot %d", slot);
707 Mutex::Autolock lock(mCore->mMutex);
708
709 if (mCore->mIsAbandoned) {
710 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
711 return;
712 }
713
Dan Stoza3e96f192014-03-03 10:16:19 -0800714 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800715 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800716 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800717 return;
718 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
719 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
720 "(state = %d)", slot, mSlots[slot].mBufferState);
721 return;
722 } else if (fence == NULL) {
723 BQ_LOGE("cancelBuffer: fence is NULL");
724 return;
725 }
726
Dan Stoza0de7ea72015-04-23 13:20:51 -0700727 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800728 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800729 mSlots[slot].mFence = fence;
730 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700731 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800732}
733
734int BufferQueueProducer::query(int what, int *outValue) {
735 ATRACE_CALL();
736 Mutex::Autolock lock(mCore->mMutex);
737
738 if (outValue == NULL) {
739 BQ_LOGE("query: outValue was NULL");
740 return BAD_VALUE;
741 }
742
743 if (mCore->mIsAbandoned) {
744 BQ_LOGE("query: BufferQueue has been abandoned");
745 return NO_INIT;
746 }
747
748 int value;
749 switch (what) {
750 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800751 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800752 break;
753 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800754 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800755 break;
756 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800757 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800758 break;
759 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
760 value = mCore->getMinUndequeuedBufferCountLocked(false);
761 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700762 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800763 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700764 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800765 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
766 value = (mCore->mQueue.size() > 1);
767 break;
768 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800769 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800770 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800771 case NATIVE_WINDOW_DEFAULT_DATASPACE:
772 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
773 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800774 default:
775 return BAD_VALUE;
776 }
777
778 BQ_LOGV("query: %d? %d", what, value);
779 *outValue = value;
780 return NO_ERROR;
781}
782
Dan Stozaf0eaf252014-03-21 13:05:51 -0700783status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800784 int api, bool producerControlledByApp, QueueBufferOutput *output) {
785 ATRACE_CALL();
786 Mutex::Autolock lock(mCore->mMutex);
787 mConsumerName = mCore->mConsumerName;
788 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
789 producerControlledByApp ? "true" : "false");
790
Dan Stozaae3c3682014-04-18 15:43:35 -0700791 if (mCore->mIsAbandoned) {
792 BQ_LOGE("connect(P): BufferQueue has been abandoned");
793 return NO_INIT;
794 }
Dan Stoza289ade12014-02-28 11:17:17 -0800795
Dan Stozaae3c3682014-04-18 15:43:35 -0700796 if (mCore->mConsumerListener == NULL) {
797 BQ_LOGE("connect(P): BufferQueue has no consumer");
798 return NO_INIT;
799 }
Dan Stoza289ade12014-02-28 11:17:17 -0800800
Dan Stozaae3c3682014-04-18 15:43:35 -0700801 if (output == NULL) {
802 BQ_LOGE("connect(P): output was NULL");
803 return BAD_VALUE;
804 }
Dan Stoza289ade12014-02-28 11:17:17 -0800805
Dan Stozaae3c3682014-04-18 15:43:35 -0700806 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
807 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
808 mCore->mConnectedApi, api);
809 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800810 }
811
812 int status = NO_ERROR;
813 switch (api) {
814 case NATIVE_WINDOW_API_EGL:
815 case NATIVE_WINDOW_API_CPU:
816 case NATIVE_WINDOW_API_MEDIA:
817 case NATIVE_WINDOW_API_CAMERA:
818 mCore->mConnectedApi = api;
819 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800820 mCore->mTransformHint,
821 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800822
823 // Set up a death notification so that we can disconnect
824 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700825 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800826 IInterface::asBinder(listener)->remoteBinder() != NULL) {
827 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800828 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700829 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800830 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
831 strerror(-status), status);
832 }
833 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700834 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800835 break;
836 default:
837 BQ_LOGE("connect(P): unknown API %d", api);
838 status = BAD_VALUE;
839 break;
840 }
841
842 mCore->mBufferHasBeenQueued = false;
843 mCore->mDequeueBufferCannotBlock =
844 mCore->mConsumerControlledByApp && producerControlledByApp;
845
846 return status;
847}
848
849status_t BufferQueueProducer::disconnect(int api) {
850 ATRACE_CALL();
851 BQ_LOGV("disconnect(P): api %d", api);
852
853 int status = NO_ERROR;
854 sp<IConsumerListener> listener;
855 { // Autolock scope
856 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700857 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800858
859 if (mCore->mIsAbandoned) {
860 // It's not really an error to disconnect after the surface has
861 // been abandoned; it should just be a no-op.
862 return NO_ERROR;
863 }
864
865 switch (api) {
866 case NATIVE_WINDOW_API_EGL:
867 case NATIVE_WINDOW_API_CPU:
868 case NATIVE_WINDOW_API_MEDIA:
869 case NATIVE_WINDOW_API_CAMERA:
870 if (mCore->mConnectedApi == api) {
871 mCore->freeAllBuffersLocked();
872
873 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700874 if (mCore->mConnectedProducerListener != NULL) {
875 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800876 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800877 // This can fail if we're here because of the death
878 // notification, but we just ignore it
879 token->unlinkToDeath(
880 static_cast<IBinder::DeathRecipient*>(this));
881 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700882 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800883 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800884 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800885 mCore->mDequeueCondition.broadcast();
886 listener = mCore->mConsumerListener;
Michael Lentine45e2fc22014-08-08 10:30:44 -0700887 } else {
888 BQ_LOGE("disconnect(P): connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800889 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800890 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800891 }
892 break;
893 default:
894 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800895 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800896 break;
897 }
898 } // Autolock scope
899
900 // Call back without lock held
901 if (listener != NULL) {
902 listener->onBuffersReleased();
903 }
904
905 return status;
906}
907
Jesse Hall399184a2014-03-03 15:42:54 -0800908status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900909 sp<IConsumerListener> listener;
910 { // Autolock scope
911 Mutex::Autolock _l(mCore->mMutex);
912 mCore->mSidebandStream = stream;
913 listener = mCore->mConsumerListener;
914 } // Autolock scope
915
916 if (listener != NULL) {
917 listener->onSidebandStreamChanged();
918 }
Jesse Hall399184a2014-03-03 15:42:54 -0800919 return NO_ERROR;
920}
921
Dan Stoza29a3e902014-06-20 13:13:57 -0700922void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800923 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -0700924 ATRACE_CALL();
925 while (true) {
926 Vector<int> freeSlots;
927 size_t newBufferCount = 0;
928 uint32_t allocWidth = 0;
929 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800930 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -0700931 uint32_t allocUsage = 0;
932 { // Autolock scope
933 Mutex::Autolock lock(mCore->mMutex);
934 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -0700935
Antoine Labour78014f32014-07-15 21:17:03 -0700936 int currentBufferCount = 0;
937 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
938 if (mSlots[slot].mGraphicBuffer != NULL) {
939 ++currentBufferCount;
940 } else {
941 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
942 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
943 slot);
944 continue;
945 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700946
Antoine Labour11f14872014-07-25 18:14:42 -0700947 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -0700948 }
949 }
950
951 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
952 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
953 currentBufferCount, maxBufferCount);
954 if (maxBufferCount <= currentBufferCount)
955 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800956 newBufferCount =
957 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -0700958 if (freeSlots.size() < newBufferCount) {
959 BQ_LOGE("allocateBuffers: ran out of free slots");
960 return;
961 }
962 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
963 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
964 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
965 allocUsage = usage | mCore->mConsumerUsageBits;
966
967 mCore->mIsAllocating = true;
968 } // Autolock scope
969
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800970 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -0700971 for (size_t i = 0; i < newBufferCount; ++i) {
972 status_t result = NO_ERROR;
973 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
974 allocWidth, allocHeight, allocFormat, allocUsage, &result));
975 if (result != NO_ERROR) {
976 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
977 " %u, usage %u)", width, height, format, usage);
978 Mutex::Autolock lock(mCore->mMutex);
979 mCore->mIsAllocating = false;
980 mCore->mIsAllocatingCondition.broadcast();
981 return;
982 }
983 buffers.push_back(graphicBuffer);
984 }
985
986 { // Autolock scope
987 Mutex::Autolock lock(mCore->mMutex);
988 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
989 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800990 PixelFormat checkFormat = format != 0 ?
991 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -0700992 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
993 if (checkWidth != allocWidth || checkHeight != allocHeight ||
994 checkFormat != allocFormat || checkUsage != allocUsage) {
995 // Something changed while we released the lock. Retry.
996 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
997 mCore->mIsAllocating = false;
998 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -0700999 continue;
1000 }
1001
Antoine Labour78014f32014-07-15 21:17:03 -07001002 for (size_t i = 0; i < newBufferCount; ++i) {
1003 int slot = freeSlots[i];
1004 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1005 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1006 // allocated.
1007 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1008 "Dropping allocated buffer.", slot);
1009 continue;
1010 }
1011 mCore->freeBufferLocked(slot); // Clean up the slot first
1012 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001013 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001014
1015 // freeBufferLocked puts this slot on the free slots list. Since
1016 // we then attached a buffer, move the slot to free buffer list.
1017 mCore->mFreeSlots.erase(slot);
1018 mCore->mFreeBuffers.push_front(slot);
1019
Antoine Labour78014f32014-07-15 21:17:03 -07001020 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1021 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001022
Antoine Labour78014f32014-07-15 21:17:03 -07001023 mCore->mIsAllocating = false;
1024 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001025 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001026 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001027 }
1028}
1029
Dan Stoza289ade12014-02-28 11:17:17 -08001030void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1031 // If we're here, it means that a producer we were connected to died.
1032 // We're guaranteed that we are still connected to it because we remove
1033 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1034 // without synchronization here.
1035 int api = mCore->mConnectedApi;
1036 disconnect(api);
1037}
1038
1039} // namespace android