blob: 86e45c8b006d1895432bf54da07a21a286b694b4 [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);
Dan Stoza9de72932015-04-16 17:28:43 -0700222 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700223 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 Stoza9de72932015-04-16 17:28:43 -0700288 const bool useDefaultSize = !width && !height;
289 if (useDefaultSize) {
290 width = mCore->mDefaultWidth;
291 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800292 }
Dan Stoza289ade12014-02-28 11:17:17 -0800293
Dan Stoza9de72932015-04-16 17:28:43 -0700294 int found = BufferItem::INVALID_BUFFER_SLOT;
295 while (found == BufferItem::INVALID_BUFFER_SLOT) {
296 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
297 &found, &returnFlags);
298 if (status != NO_ERROR) {
299 return status;
300 }
301
302 // This should not happen
303 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
304 BQ_LOGE("dequeueBuffer: no available buffer slots");
305 return -EBUSY;
306 }
307
308 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
309
310 // If we are not allowed to allocate new buffers,
311 // waitForFreeSlotThenRelock must have returned a slot containing a
312 // buffer. If this buffer would require reallocation to meet the
313 // requested attributes, we free it and attempt to get another one.
314 if (!mCore->mAllowAllocation) {
315 if (buffer->needsReallocation(width, height, format, usage)) {
316 mCore->freeBufferLocked(found);
317 found = BufferItem::INVALID_BUFFER_SLOT;
318 continue;
319 }
320 }
Dan Stoza289ade12014-02-28 11:17:17 -0800321 }
322
323 *outSlot = found;
324 ATRACE_BUFFER_INDEX(found);
325
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 attachedByConsumer = mSlots[found].mAttachedByConsumer;
327
Dan Stoza289ade12014-02-28 11:17:17 -0800328 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
329
330 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
331 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700332 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800333 {
334 mSlots[found].mAcquireCalled = false;
335 mSlots[found].mGraphicBuffer = NULL;
336 mSlots[found].mRequestBufferCalled = false;
337 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
338 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
339 mSlots[found].mFence = Fence::NO_FENCE;
340
341 returnFlags |= BUFFER_NEEDS_REALLOCATION;
342 }
343
344 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
345 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
346 "slot=%d w=%d h=%d format=%u",
347 found, buffer->width, buffer->height, buffer->format);
348 }
349
350 eglDisplay = mSlots[found].mEglDisplay;
351 eglFence = mSlots[found].mEglFence;
352 *outFence = mSlots[found].mFence;
353 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
354 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700355
356 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800357 } // Autolock scope
358
359 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
360 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700361 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800362 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800363 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800364 if (graphicBuffer == NULL) {
365 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
366 return error;
367 }
368
369 { // Autolock scope
370 Mutex::Autolock lock(mCore->mMutex);
371
372 if (mCore->mIsAbandoned) {
373 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
374 return NO_INIT;
375 }
376
Dan Stoza289ade12014-02-28 11:17:17 -0800377 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
378 } // Autolock scope
379 }
380
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 if (attachedByConsumer) {
382 returnFlags |= BUFFER_NEEDS_REALLOCATION;
383 }
384
Dan Stoza289ade12014-02-28 11:17:17 -0800385 if (eglFence != EGL_NO_SYNC_KHR) {
386 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
387 1000000000);
388 // If something goes wrong, log the error, but return the buffer without
389 // synchronizing access to it. It's too late at this point to abort the
390 // dequeue operation.
391 if (result == EGL_FALSE) {
392 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
393 eglGetError());
394 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
395 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
396 }
397 eglDestroySyncKHR(eglDisplay, eglFence);
398 }
399
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700400 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
401 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800402 mSlots[*outSlot].mFrameNumber,
403 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
404
405 return returnFlags;
406}
407
Dan Stoza9f3053d2014-03-06 15:14:33 -0800408status_t BufferQueueProducer::detachBuffer(int slot) {
409 ATRACE_CALL();
410 ATRACE_BUFFER_INDEX(slot);
411 BQ_LOGV("detachBuffer(P): slot %d", slot);
412 Mutex::Autolock lock(mCore->mMutex);
413
414 if (mCore->mIsAbandoned) {
415 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
416 return NO_INIT;
417 }
418
419 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
420 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
421 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
422 return BAD_VALUE;
423 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
424 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
425 "(state = %d)", slot, mSlots[slot].mBufferState);
426 return BAD_VALUE;
427 } else if (!mSlots[slot].mRequestBufferCalled) {
428 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
429 slot);
430 return BAD_VALUE;
431 }
432
433 mCore->freeBufferLocked(slot);
434 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700435 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800436
437 return NO_ERROR;
438}
439
Dan Stozad9822a32014-03-28 15:25:31 -0700440status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
441 sp<Fence>* outFence) {
442 ATRACE_CALL();
443
444 if (outBuffer == NULL) {
445 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
446 return BAD_VALUE;
447 } else if (outFence == NULL) {
448 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
449 return BAD_VALUE;
450 }
451
452 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700453 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700454
455 if (mCore->mIsAbandoned) {
456 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
457 return NO_INIT;
458 }
459
Dan Stoza0de7ea72015-04-23 13:20:51 -0700460 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000461 return NO_MEMORY;
462 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700463
Dan Stoza0de7ea72015-04-23 13:20:51 -0700464 int found = mCore->mFreeBuffers.front();
465 mCore->mFreeBuffers.remove(found);
466
Dan Stozad9822a32014-03-28 15:25:31 -0700467 BQ_LOGV("detachNextBuffer detached slot %d", found);
468
469 *outBuffer = mSlots[found].mGraphicBuffer;
470 *outFence = mSlots[found].mFence;
471 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700472 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700473
474 return NO_ERROR;
475}
476
Dan Stoza9f3053d2014-03-06 15:14:33 -0800477status_t BufferQueueProducer::attachBuffer(int* outSlot,
478 const sp<android::GraphicBuffer>& buffer) {
479 ATRACE_CALL();
480
481 if (outSlot == NULL) {
482 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
483 return BAD_VALUE;
484 } else if (buffer == NULL) {
485 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
486 return BAD_VALUE;
487 }
488
489 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700490 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800491
492 status_t returnFlags = NO_ERROR;
493 int found;
494 // TODO: Should we provide an async flag to attachBuffer? It seems
495 // unlikely that buffers which we are attaching to a BufferQueue will
496 // be asynchronous (droppable), but it may not be impossible.
497 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
498 &found, &returnFlags);
499 if (status != NO_ERROR) {
500 return status;
501 }
502
503 // This should not happen
504 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
505 BQ_LOGE("attachBuffer(P): no available buffer slots");
506 return -EBUSY;
507 }
508
509 *outSlot = found;
510 ATRACE_BUFFER_INDEX(*outSlot);
511 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
512 *outSlot, returnFlags);
513
514 mSlots[*outSlot].mGraphicBuffer = buffer;
515 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
516 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
517 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700518 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800519
Dan Stoza0de7ea72015-04-23 13:20:51 -0700520 mCore->validateConsistencyLocked();
521
Dan Stoza9f3053d2014-03-06 15:14:33 -0800522 return returnFlags;
523}
524
Dan Stoza289ade12014-02-28 11:17:17 -0800525status_t BufferQueueProducer::queueBuffer(int slot,
526 const QueueBufferInput &input, QueueBufferOutput *output) {
527 ATRACE_CALL();
528 ATRACE_BUFFER_INDEX(slot);
529
530 int64_t timestamp;
531 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800532 android_dataspace dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800533 Rect crop;
534 int scalingMode;
535 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700536 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800537 bool async;
538 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800539 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
540 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700541 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800542
543 if (fence == NULL) {
544 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800545 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800546 }
547
548 switch (scalingMode) {
549 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
550 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
551 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
552 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
553 break;
554 default:
555 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800556 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800557 }
558
Dan Stoza8dc55392014-11-04 11:37:46 -0800559 sp<IConsumerListener> frameAvailableListener;
560 sp<IConsumerListener> frameReplacedListener;
561 int callbackTicket = 0;
562 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800563 { // Autolock scope
564 Mutex::Autolock lock(mCore->mMutex);
565
566 if (mCore->mIsAbandoned) {
567 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
568 return NO_INIT;
569 }
570
571 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
572 if (async && mCore->mOverrideMaxBufferCount) {
573 // FIXME: Some drivers are manually setting the buffer count
574 // (which they shouldn't), so we do this extra test here to
575 // handle that case. This is TEMPORARY until we get this fixed.
576 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
577 BQ_LOGE("queueBuffer: async mode is invalid with "
578 "buffer count override");
579 return BAD_VALUE;
580 }
581 }
582
583 if (slot < 0 || slot >= maxBufferCount) {
584 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
585 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800586 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800587 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
588 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
589 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800590 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800591 } else if (!mSlots[slot].mRequestBufferCalled) {
592 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
593 "a buffer", 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 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700598 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800599 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800600 crop.left, crop.top, crop.right, crop.bottom, transform,
601 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800602
603 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
604 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
605 Rect croppedRect;
606 crop.intersect(bufferRect, &croppedRect);
607 if (croppedRect != crop) {
608 BQ_LOGE("queueBuffer: crop rect is not contained within the "
609 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800610 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800611 }
612
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800613 // Override UNKNOWN dataspace with consumer default
614 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
615 dataSpace = mCore->mDefaultBufferDataSpace;
616 }
617
Dan Stoza289ade12014-02-28 11:17:17 -0800618 mSlots[slot].mFence = fence;
619 mSlots[slot].mBufferState = BufferSlot::QUEUED;
620 ++mCore->mFrameCounter;
621 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
622
Dan Stoza289ade12014-02-28 11:17:17 -0800623 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
624 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
625 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800626 item.mTransform = transform &
627 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800628 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800629 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
630 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800631 item.mTimestamp = timestamp;
632 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800633 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800634 item.mFrameNumber = mCore->mFrameCounter;
635 item.mSlot = slot;
636 item.mFence = fence;
637 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700638 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800639
Ruben Brunk1681d952014-06-27 15:51:55 -0700640 mStickyTransform = stickyTransform;
641
Dan Stoza289ade12014-02-28 11:17:17 -0800642 if (mCore->mQueue.empty()) {
643 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
644 // and simply queue this buffer
645 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800646 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800647 } else {
648 // When the queue is not empty, we need to look at the front buffer
649 // state to see if we need to replace it
650 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
651 if (front->mIsDroppable) {
652 // If the front queued buffer is still being tracked, we first
653 // mark it as freed
654 if (mCore->stillTracking(front)) {
655 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700656 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800657 }
658 // Overwrite the droppable buffer with the incoming one
659 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800660 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800661 } else {
662 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800663 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800664 }
665 }
666
667 mCore->mBufferHasBeenQueued = true;
668 mCore->mDequeueCondition.broadcast();
669
670 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800671 mCore->mTransformHint,
672 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800673
674 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800675
676 // Take a ticket for the callback functions
677 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700678
679 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800680 } // Autolock scope
681
Eric Penner99a0afb2014-09-30 11:28:30 -0700682 // Wait without lock held
683 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
684 // Waiting here allows for two full buffers to be queued but not a
685 // third. In the event that frames take varying time, this makes a
686 // small trade-off in favor of latency rather than throughput.
687 mLastQueueBufferFence->waitForever("Throttling EGL Production");
688 mLastQueueBufferFence = fence;
689 }
690
Dan Stoza8dc55392014-11-04 11:37:46 -0800691 // Don't send the GraphicBuffer through the callback, and don't send
692 // the slot number, since the consumer shouldn't need it
693 item.mGraphicBuffer.clear();
694 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
695
696 // Call back without the main BufferQueue lock held, but with the callback
697 // lock held so we can ensure that callbacks occur in order
698 {
699 Mutex::Autolock lock(mCallbackMutex);
700 while (callbackTicket != mCurrentCallbackTicket) {
701 mCallbackCondition.wait(mCallbackMutex);
702 }
703
704 if (frameAvailableListener != NULL) {
705 frameAvailableListener->onFrameAvailable(item);
706 } else if (frameReplacedListener != NULL) {
707 frameReplacedListener->onFrameReplaced(item);
708 }
709
710 ++mCurrentCallbackTicket;
711 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800712 }
713
714 return NO_ERROR;
715}
716
717void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
718 ATRACE_CALL();
719 BQ_LOGV("cancelBuffer: slot %d", slot);
720 Mutex::Autolock lock(mCore->mMutex);
721
722 if (mCore->mIsAbandoned) {
723 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
724 return;
725 }
726
Dan Stoza3e96f192014-03-03 10:16:19 -0800727 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800728 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800729 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800730 return;
731 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
732 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
733 "(state = %d)", slot, mSlots[slot].mBufferState);
734 return;
735 } else if (fence == NULL) {
736 BQ_LOGE("cancelBuffer: fence is NULL");
737 return;
738 }
739
Dan Stoza0de7ea72015-04-23 13:20:51 -0700740 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800741 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800742 mSlots[slot].mFence = fence;
743 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700744 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800745}
746
747int BufferQueueProducer::query(int what, int *outValue) {
748 ATRACE_CALL();
749 Mutex::Autolock lock(mCore->mMutex);
750
751 if (outValue == NULL) {
752 BQ_LOGE("query: outValue was NULL");
753 return BAD_VALUE;
754 }
755
756 if (mCore->mIsAbandoned) {
757 BQ_LOGE("query: BufferQueue has been abandoned");
758 return NO_INIT;
759 }
760
761 int value;
762 switch (what) {
763 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800764 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800765 break;
766 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800767 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800768 break;
769 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800770 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800771 break;
772 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
773 value = mCore->getMinUndequeuedBufferCountLocked(false);
774 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700775 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800776 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700777 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800778 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
779 value = (mCore->mQueue.size() > 1);
780 break;
781 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800782 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800783 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800784 case NATIVE_WINDOW_DEFAULT_DATASPACE:
785 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
786 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800787 default:
788 return BAD_VALUE;
789 }
790
791 BQ_LOGV("query: %d? %d", what, value);
792 *outValue = value;
793 return NO_ERROR;
794}
795
Dan Stozaf0eaf252014-03-21 13:05:51 -0700796status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800797 int api, bool producerControlledByApp, QueueBufferOutput *output) {
798 ATRACE_CALL();
799 Mutex::Autolock lock(mCore->mMutex);
800 mConsumerName = mCore->mConsumerName;
801 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
802 producerControlledByApp ? "true" : "false");
803
Dan Stozaae3c3682014-04-18 15:43:35 -0700804 if (mCore->mIsAbandoned) {
805 BQ_LOGE("connect(P): BufferQueue has been abandoned");
806 return NO_INIT;
807 }
Dan Stoza289ade12014-02-28 11:17:17 -0800808
Dan Stozaae3c3682014-04-18 15:43:35 -0700809 if (mCore->mConsumerListener == NULL) {
810 BQ_LOGE("connect(P): BufferQueue has no consumer");
811 return NO_INIT;
812 }
Dan Stoza289ade12014-02-28 11:17:17 -0800813
Dan Stozaae3c3682014-04-18 15:43:35 -0700814 if (output == NULL) {
815 BQ_LOGE("connect(P): output was NULL");
816 return BAD_VALUE;
817 }
Dan Stoza289ade12014-02-28 11:17:17 -0800818
Dan Stozaae3c3682014-04-18 15:43:35 -0700819 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
820 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
821 mCore->mConnectedApi, api);
822 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800823 }
824
825 int status = NO_ERROR;
826 switch (api) {
827 case NATIVE_WINDOW_API_EGL:
828 case NATIVE_WINDOW_API_CPU:
829 case NATIVE_WINDOW_API_MEDIA:
830 case NATIVE_WINDOW_API_CAMERA:
831 mCore->mConnectedApi = api;
832 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 mCore->mTransformHint,
834 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800835
836 // Set up a death notification so that we can disconnect
837 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700838 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800839 IInterface::asBinder(listener)->remoteBinder() != NULL) {
840 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800841 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700842 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800843 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
844 strerror(-status), status);
845 }
846 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700847 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800848 break;
849 default:
850 BQ_LOGE("connect(P): unknown API %d", api);
851 status = BAD_VALUE;
852 break;
853 }
854
855 mCore->mBufferHasBeenQueued = false;
856 mCore->mDequeueBufferCannotBlock =
857 mCore->mConsumerControlledByApp && producerControlledByApp;
858
859 return status;
860}
861
862status_t BufferQueueProducer::disconnect(int api) {
863 ATRACE_CALL();
864 BQ_LOGV("disconnect(P): api %d", api);
865
866 int status = NO_ERROR;
867 sp<IConsumerListener> listener;
868 { // Autolock scope
869 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700870 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800871
872 if (mCore->mIsAbandoned) {
873 // It's not really an error to disconnect after the surface has
874 // been abandoned; it should just be a no-op.
875 return NO_ERROR;
876 }
877
878 switch (api) {
879 case NATIVE_WINDOW_API_EGL:
880 case NATIVE_WINDOW_API_CPU:
881 case NATIVE_WINDOW_API_MEDIA:
882 case NATIVE_WINDOW_API_CAMERA:
883 if (mCore->mConnectedApi == api) {
884 mCore->freeAllBuffersLocked();
885
886 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700887 if (mCore->mConnectedProducerListener != NULL) {
888 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800889 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800890 // This can fail if we're here because of the death
891 // notification, but we just ignore it
892 token->unlinkToDeath(
893 static_cast<IBinder::DeathRecipient*>(this));
894 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700895 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800896 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800897 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800898 mCore->mDequeueCondition.broadcast();
899 listener = mCore->mConsumerListener;
Michael Lentine45e2fc22014-08-08 10:30:44 -0700900 } else {
901 BQ_LOGE("disconnect(P): connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800902 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800903 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800904 }
905 break;
906 default:
907 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800908 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800909 break;
910 }
911 } // Autolock scope
912
913 // Call back without lock held
914 if (listener != NULL) {
915 listener->onBuffersReleased();
916 }
917
918 return status;
919}
920
Jesse Hall399184a2014-03-03 15:42:54 -0800921status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900922 sp<IConsumerListener> listener;
923 { // Autolock scope
924 Mutex::Autolock _l(mCore->mMutex);
925 mCore->mSidebandStream = stream;
926 listener = mCore->mConsumerListener;
927 } // Autolock scope
928
929 if (listener != NULL) {
930 listener->onSidebandStreamChanged();
931 }
Jesse Hall399184a2014-03-03 15:42:54 -0800932 return NO_ERROR;
933}
934
Dan Stoza29a3e902014-06-20 13:13:57 -0700935void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800936 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -0700937 ATRACE_CALL();
938 while (true) {
939 Vector<int> freeSlots;
940 size_t newBufferCount = 0;
941 uint32_t allocWidth = 0;
942 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800943 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -0700944 uint32_t allocUsage = 0;
945 { // Autolock scope
946 Mutex::Autolock lock(mCore->mMutex);
947 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -0700948
Dan Stoza9de72932015-04-16 17:28:43 -0700949 if (!mCore->mAllowAllocation) {
950 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
951 "BufferQueue");
952 return;
953 }
954
Antoine Labour78014f32014-07-15 21:17:03 -0700955 int currentBufferCount = 0;
956 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
957 if (mSlots[slot].mGraphicBuffer != NULL) {
958 ++currentBufferCount;
959 } else {
960 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
961 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
962 slot);
963 continue;
964 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700965
Antoine Labour11f14872014-07-25 18:14:42 -0700966 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -0700967 }
968 }
969
970 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
971 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
972 currentBufferCount, maxBufferCount);
973 if (maxBufferCount <= currentBufferCount)
974 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800975 newBufferCount =
976 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -0700977 if (freeSlots.size() < newBufferCount) {
978 BQ_LOGE("allocateBuffers: ran out of free slots");
979 return;
980 }
981 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
982 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
983 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
984 allocUsage = usage | mCore->mConsumerUsageBits;
985
986 mCore->mIsAllocating = true;
987 } // Autolock scope
988
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800989 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -0700990 for (size_t i = 0; i < newBufferCount; ++i) {
991 status_t result = NO_ERROR;
992 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
993 allocWidth, allocHeight, allocFormat, allocUsage, &result));
994 if (result != NO_ERROR) {
995 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
996 " %u, usage %u)", width, height, format, usage);
997 Mutex::Autolock lock(mCore->mMutex);
998 mCore->mIsAllocating = false;
999 mCore->mIsAllocatingCondition.broadcast();
1000 return;
1001 }
1002 buffers.push_back(graphicBuffer);
1003 }
1004
1005 { // Autolock scope
1006 Mutex::Autolock lock(mCore->mMutex);
1007 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1008 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001009 PixelFormat checkFormat = format != 0 ?
1010 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001011 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1012 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1013 checkFormat != allocFormat || checkUsage != allocUsage) {
1014 // Something changed while we released the lock. Retry.
1015 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1016 mCore->mIsAllocating = false;
1017 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001018 continue;
1019 }
1020
Antoine Labour78014f32014-07-15 21:17:03 -07001021 for (size_t i = 0; i < newBufferCount; ++i) {
1022 int slot = freeSlots[i];
1023 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1024 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1025 // allocated.
1026 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1027 "Dropping allocated buffer.", slot);
1028 continue;
1029 }
1030 mCore->freeBufferLocked(slot); // Clean up the slot first
1031 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001032 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001033
1034 // freeBufferLocked puts this slot on the free slots list. Since
1035 // we then attached a buffer, move the slot to free buffer list.
1036 mCore->mFreeSlots.erase(slot);
1037 mCore->mFreeBuffers.push_front(slot);
1038
Antoine Labour78014f32014-07-15 21:17:03 -07001039 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1040 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001041
Antoine Labour78014f32014-07-15 21:17:03 -07001042 mCore->mIsAllocating = false;
1043 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001044 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001045 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001046 }
1047}
1048
Dan Stoza9de72932015-04-16 17:28:43 -07001049status_t BufferQueueProducer::allowAllocation(bool allow) {
1050 ATRACE_CALL();
1051 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1052
1053 Mutex::Autolock lock(mCore->mMutex);
1054 mCore->mAllowAllocation = allow;
1055 return NO_ERROR;
1056}
1057
Dan Stoza289ade12014-02-28 11:17:17 -08001058void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1059 // If we're here, it means that a producer we were connected to died.
1060 // We're guaranteed that we are still connected to it because we remove
1061 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1062 // without synchronization here.
1063 int api = mCore->mConnectedApi;
1064 disconnect(api);
1065}
1066
1067} // namespace android