blob: 5ea4a10772bd35bbfea163ade95b511f53723a5d [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();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700121 mCore->mMaxDequeuedBufferCount = bufferCount - minBufferSlots + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800122 mCore->mOverrideMaxBufferCount = bufferCount;
123 mCore->mDequeueCondition.broadcast();
124 listener = mCore->mConsumerListener;
125 } // Autolock scope
126
127 // Call back without lock held
128 if (listener != NULL) {
129 listener->onBuffersReleased();
130 }
131
132 return NO_ERROR;
133}
134
Pablo Ceballosfa455352015-08-12 17:47:47 -0700135status_t BufferQueueProducer::setMaxDequeuedBufferCount(
136 int maxDequeuedBuffers) {
137 ATRACE_CALL();
138 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
139 maxDequeuedBuffers);
140
141 sp<IConsumerListener> listener;
142 { // Autolock scope
143 Mutex::Autolock lock(mCore->mMutex);
144 mCore->waitWhileAllocatingLocked();
145
146 if (mCore->mIsAbandoned) {
147 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
148 "abandoned");
149 return NO_INIT;
150 }
151
152 // There must be no dequeued buffers when changing the buffer count.
153 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
154 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
155 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
156 return BAD_VALUE;
157 }
158 }
159
160 int bufferCount = mCore->getMinUndequeuedBufferCountLocked(
161 mCore->mAsyncMode);
162 bufferCount += maxDequeuedBuffers;
163
164 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
165 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
166 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
167 return BAD_VALUE;
168 }
169
170 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(
171 mCore->mAsyncMode);
172 if (bufferCount < minBufferSlots) {
173 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
174 "less than minimum %d", bufferCount, minBufferSlots);
175 return BAD_VALUE;
176 }
177
178 // Here we are guaranteed that the producer doesn't have any dequeued
179 // buffers and will release all of its buffer references. We don't
180 // clear the queue, however, so that currently queued buffers still
181 // get displayed.
182 mCore->freeAllBuffersLocked();
183 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
184 mCore->mOverrideMaxBufferCount = bufferCount;
185 mCore->mDequeueCondition.broadcast();
186 listener = mCore->mConsumerListener;
187 } // Autolock scope
188
189 // Call back without lock held
190 if (listener != NULL) {
191 listener->onBuffersReleased();
192 }
193
194 return NO_ERROR;
195}
196
197status_t BufferQueueProducer::setAsyncMode(bool async) {
198 ATRACE_CALL();
199 BQ_LOGV("setAsyncMode: async = %d", async);
200
201 sp<IConsumerListener> listener;
202 { // Autolock scope
203 Mutex::Autolock lock(mCore->mMutex);
204 mCore->waitWhileAllocatingLocked();
205
206 if (mCore->mIsAbandoned) {
207 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
208 return NO_INIT;
209 }
210
211 // There must be no dequeued buffers when changing the async mode.
212 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
213 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
214 BQ_LOGE("setAsyncMode: buffer owned by producer");
215 return BAD_VALUE;
216 }
217 }
218
219 mCore->mAsyncMode = async;
220 mCore->mDequeueCondition.broadcast();
221 listener = mCore->mConsumerListener;
222 } // Autolock scope
223
224 // Call back without lock held
225 if (listener != NULL) {
226 listener->onBuffersReleased();
227 }
228 return NO_ERROR;
229}
230
Dan Stoza9f3053d2014-03-06 15:14:33 -0800231status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
232 bool async, int* found, status_t* returnFlags) const {
233 bool tryAgain = true;
234 while (tryAgain) {
235 if (mCore->mIsAbandoned) {
236 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
237 return NO_INIT;
238 }
239
240 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
241 if (async && mCore->mOverrideMaxBufferCount) {
242 // FIXME: Some drivers are manually setting the buffer count
243 // (which they shouldn't), so we do this extra test here to
244 // handle that case. This is TEMPORARY until we get this fixed.
245 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
246 BQ_LOGE("%s: async mode is invalid with buffer count override",
247 caller);
248 return BAD_VALUE;
249 }
250 }
251
252 // Free up any buffers that are in slots beyond the max buffer count
253 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
254 assert(mSlots[s].mBufferState == BufferSlot::FREE);
255 if (mSlots[s].mGraphicBuffer != NULL) {
256 mCore->freeBufferLocked(s);
257 *returnFlags |= RELEASE_ALL_BUFFERS;
258 }
259 }
260
Dan Stoza9f3053d2014-03-06 15:14:33 -0800261 int dequeuedCount = 0;
262 int acquiredCount = 0;
263 for (int s = 0; s < maxBufferCount; ++s) {
264 switch (mSlots[s].mBufferState) {
265 case BufferSlot::DEQUEUED:
266 ++dequeuedCount;
267 break;
268 case BufferSlot::ACQUIRED:
269 ++acquiredCount;
270 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800271 default:
272 break;
273 }
274 }
275
276 // Producers are not allowed to dequeue more than one buffer if they
277 // did not set a buffer count
278 if (!mCore->mOverrideMaxBufferCount && dequeuedCount) {
279 BQ_LOGE("%s: can't dequeue multiple buffers without setting the "
280 "buffer count", caller);
281 return INVALID_OPERATION;
282 }
283
284 // See whether a buffer has been queued since the last
285 // setBufferCount so we know whether to perform the min undequeued
286 // buffers check below
287 if (mCore->mBufferHasBeenQueued) {
288 // Make sure the producer is not trying to dequeue more buffers
289 // than allowed
290 const int newUndequeuedCount =
291 maxBufferCount - (dequeuedCount + 1);
292 const int minUndequeuedCount =
293 mCore->getMinUndequeuedBufferCountLocked(async);
294 if (newUndequeuedCount < minUndequeuedCount) {
295 BQ_LOGE("%s: min undequeued buffer count (%d) exceeded "
296 "(dequeued=%d undequeued=%d)",
297 caller, minUndequeuedCount,
298 dequeuedCount, newUndequeuedCount);
299 return INVALID_OPERATION;
300 }
301 }
302
Dan Stoza0de7ea72015-04-23 13:20:51 -0700303 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
304
Dan Stozaae3c3682014-04-18 15:43:35 -0700305 // If we disconnect and reconnect quickly, we can be in a state where
306 // our slots are empty but we have many buffers in the queue. This can
307 // cause us to run out of memory if we outrun the consumer. Wait here if
308 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700309 bool tooManyBuffers = mCore->mQueue.size()
310 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700311 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700312 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700313 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700314 } else {
315 if (!mCore->mFreeBuffers.empty()) {
316 auto slot = mCore->mFreeBuffers.begin();
317 *found = *slot;
318 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700319 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700320 auto slot = mCore->mFreeSlots.begin();
321 // Only return free slots up to the max buffer count
322 if (*slot < maxBufferCount) {
323 *found = *slot;
324 mCore->mFreeSlots.erase(slot);
325 }
326 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700327 }
328
329 // If no buffer is found, or if the queue has too many buffers
330 // outstanding, wait for a buffer to be acquired or released, or for the
331 // max buffer count to change.
332 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
333 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 if (tryAgain) {
335 // Return an error if we're in non-blocking mode (producer and
336 // consumer are controlled by the application).
337 // However, the consumer is allowed to briefly acquire an extra
338 // buffer (which could cause us to have to wait here), which is
339 // okay, since it is only used to implement an atomic acquire +
340 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700341 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800342 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
343 return WOULD_BLOCK;
344 }
345 mCore->mDequeueCondition.wait(mCore->mMutex);
346 }
347 } // while (tryAgain)
348
349 return NO_ERROR;
350}
351
Dan Stoza289ade12014-02-28 11:17:17 -0800352status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
353 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800354 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800355 ATRACE_CALL();
356 { // Autolock scope
357 Mutex::Autolock lock(mCore->mMutex);
358 mConsumerName = mCore->mConsumerName;
359 } // Autolock scope
360
361 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
362 async ? "true" : "false", width, height, format, usage);
363
364 if ((width && !height) || (!width && height)) {
365 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
366 return BAD_VALUE;
367 }
368
369 status_t returnFlags = NO_ERROR;
370 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
371 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800373
374 { // Autolock scope
375 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700376 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800377
378 if (format == 0) {
379 format = mCore->mDefaultBufferFormat;
380 }
381
382 // Enable the usage bits the consumer requested
383 usage |= mCore->mConsumerUsageBits;
384
Dan Stoza9de72932015-04-16 17:28:43 -0700385 const bool useDefaultSize = !width && !height;
386 if (useDefaultSize) {
387 width = mCore->mDefaultWidth;
388 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800389 }
Dan Stoza289ade12014-02-28 11:17:17 -0800390
Dan Stoza9de72932015-04-16 17:28:43 -0700391 int found = BufferItem::INVALID_BUFFER_SLOT;
392 while (found == BufferItem::INVALID_BUFFER_SLOT) {
393 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
394 &found, &returnFlags);
395 if (status != NO_ERROR) {
396 return status;
397 }
398
399 // This should not happen
400 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
401 BQ_LOGE("dequeueBuffer: no available buffer slots");
402 return -EBUSY;
403 }
404
405 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
406
407 // If we are not allowed to allocate new buffers,
408 // waitForFreeSlotThenRelock must have returned a slot containing a
409 // buffer. If this buffer would require reallocation to meet the
410 // requested attributes, we free it and attempt to get another one.
411 if (!mCore->mAllowAllocation) {
412 if (buffer->needsReallocation(width, height, format, usage)) {
413 mCore->freeBufferLocked(found);
414 found = BufferItem::INVALID_BUFFER_SLOT;
415 continue;
416 }
417 }
Dan Stoza289ade12014-02-28 11:17:17 -0800418 }
419
420 *outSlot = found;
421 ATRACE_BUFFER_INDEX(found);
422
Dan Stoza9f3053d2014-03-06 15:14:33 -0800423 attachedByConsumer = mSlots[found].mAttachedByConsumer;
424
Dan Stoza289ade12014-02-28 11:17:17 -0800425 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
426
427 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
428 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700429 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800430 {
431 mSlots[found].mAcquireCalled = false;
432 mSlots[found].mGraphicBuffer = NULL;
433 mSlots[found].mRequestBufferCalled = false;
434 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
435 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
436 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800437 mCore->mBufferAge = 0;
Dan Stoza289ade12014-02-28 11:17:17 -0800438
439 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800440 } else {
441 // We add 1 because that will be the frame number when this buffer
442 // is queued
443 mCore->mBufferAge =
444 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800445 }
446
Dan Stoza800b41a2015-04-28 14:20:04 -0700447 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
448 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800449
Dan Stoza289ade12014-02-28 11:17:17 -0800450 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
451 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
452 "slot=%d w=%d h=%d format=%u",
453 found, buffer->width, buffer->height, buffer->format);
454 }
455
456 eglDisplay = mSlots[found].mEglDisplay;
457 eglFence = mSlots[found].mEglFence;
458 *outFence = mSlots[found].mFence;
459 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
460 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700461
462 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800463 } // Autolock scope
464
465 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
466 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700467 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800468 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800469 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800470 if (graphicBuffer == NULL) {
471 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
472 return error;
473 }
474
475 { // Autolock scope
476 Mutex::Autolock lock(mCore->mMutex);
477
478 if (mCore->mIsAbandoned) {
479 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
480 return NO_INIT;
481 }
482
Dan Stoza812ed062015-06-02 15:45:22 -0700483 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
Dan Stoza289ade12014-02-28 11:17:17 -0800484 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
485 } // Autolock scope
486 }
487
Dan Stoza9f3053d2014-03-06 15:14:33 -0800488 if (attachedByConsumer) {
489 returnFlags |= BUFFER_NEEDS_REALLOCATION;
490 }
491
Dan Stoza289ade12014-02-28 11:17:17 -0800492 if (eglFence != EGL_NO_SYNC_KHR) {
493 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
494 1000000000);
495 // If something goes wrong, log the error, but return the buffer without
496 // synchronizing access to it. It's too late at this point to abort the
497 // dequeue operation.
498 if (result == EGL_FALSE) {
499 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
500 eglGetError());
501 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
502 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
503 }
504 eglDestroySyncKHR(eglDisplay, eglFence);
505 }
506
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700507 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
508 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800509 mSlots[*outSlot].mFrameNumber,
510 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
511
512 return returnFlags;
513}
514
Dan Stoza9f3053d2014-03-06 15:14:33 -0800515status_t BufferQueueProducer::detachBuffer(int slot) {
516 ATRACE_CALL();
517 ATRACE_BUFFER_INDEX(slot);
518 BQ_LOGV("detachBuffer(P): slot %d", slot);
519 Mutex::Autolock lock(mCore->mMutex);
520
521 if (mCore->mIsAbandoned) {
522 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
523 return NO_INIT;
524 }
525
526 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
527 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
528 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
529 return BAD_VALUE;
530 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
531 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
532 "(state = %d)", slot, mSlots[slot].mBufferState);
533 return BAD_VALUE;
534 } else if (!mSlots[slot].mRequestBufferCalled) {
535 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
536 slot);
537 return BAD_VALUE;
538 }
539
540 mCore->freeBufferLocked(slot);
541 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700542 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800543
544 return NO_ERROR;
545}
546
Dan Stozad9822a32014-03-28 15:25:31 -0700547status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
548 sp<Fence>* outFence) {
549 ATRACE_CALL();
550
551 if (outBuffer == NULL) {
552 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
553 return BAD_VALUE;
554 } else if (outFence == NULL) {
555 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
556 return BAD_VALUE;
557 }
558
559 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700560 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700561
562 if (mCore->mIsAbandoned) {
563 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
564 return NO_INIT;
565 }
566
Dan Stoza0de7ea72015-04-23 13:20:51 -0700567 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000568 return NO_MEMORY;
569 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700570
Dan Stoza0de7ea72015-04-23 13:20:51 -0700571 int found = mCore->mFreeBuffers.front();
572 mCore->mFreeBuffers.remove(found);
573
Dan Stozad9822a32014-03-28 15:25:31 -0700574 BQ_LOGV("detachNextBuffer detached slot %d", found);
575
576 *outBuffer = mSlots[found].mGraphicBuffer;
577 *outFence = mSlots[found].mFence;
578 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700579 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700580
581 return NO_ERROR;
582}
583
Dan Stoza9f3053d2014-03-06 15:14:33 -0800584status_t BufferQueueProducer::attachBuffer(int* outSlot,
585 const sp<android::GraphicBuffer>& buffer) {
586 ATRACE_CALL();
587
588 if (outSlot == NULL) {
589 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
590 return BAD_VALUE;
591 } else if (buffer == NULL) {
592 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
593 return BAD_VALUE;
594 }
595
596 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700597 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800598
Dan Stoza812ed062015-06-02 15:45:22 -0700599 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
600 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
601 "[queue %u]", buffer->getGenerationNumber(),
602 mCore->mGenerationNumber);
603 return BAD_VALUE;
604 }
605
Dan Stoza9f3053d2014-03-06 15:14:33 -0800606 status_t returnFlags = NO_ERROR;
607 int found;
608 // TODO: Should we provide an async flag to attachBuffer? It seems
609 // unlikely that buffers which we are attaching to a BufferQueue will
610 // be asynchronous (droppable), but it may not be impossible.
611 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
612 &found, &returnFlags);
613 if (status != NO_ERROR) {
614 return status;
615 }
616
617 // This should not happen
618 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
619 BQ_LOGE("attachBuffer(P): no available buffer slots");
620 return -EBUSY;
621 }
622
623 *outSlot = found;
624 ATRACE_BUFFER_INDEX(*outSlot);
625 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
626 *outSlot, returnFlags);
627
628 mSlots[*outSlot].mGraphicBuffer = buffer;
629 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
630 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
631 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700632 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800633
Dan Stoza0de7ea72015-04-23 13:20:51 -0700634 mCore->validateConsistencyLocked();
635
Dan Stoza9f3053d2014-03-06 15:14:33 -0800636 return returnFlags;
637}
638
Dan Stoza289ade12014-02-28 11:17:17 -0800639status_t BufferQueueProducer::queueBuffer(int slot,
640 const QueueBufferInput &input, QueueBufferOutput *output) {
641 ATRACE_CALL();
642 ATRACE_BUFFER_INDEX(slot);
643
644 int64_t timestamp;
645 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800646 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700647 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800648 int scalingMode;
649 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700650 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800651 bool async;
652 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800653 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
654 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700655 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800656
657 if (fence == NULL) {
658 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800659 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800660 }
661
662 switch (scalingMode) {
663 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
664 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
665 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
666 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
667 break;
668 default:
669 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800670 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800671 }
672
Dan Stoza8dc55392014-11-04 11:37:46 -0800673 sp<IConsumerListener> frameAvailableListener;
674 sp<IConsumerListener> frameReplacedListener;
675 int callbackTicket = 0;
676 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800677 { // Autolock scope
678 Mutex::Autolock lock(mCore->mMutex);
679
680 if (mCore->mIsAbandoned) {
681 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
682 return NO_INIT;
683 }
684
685 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
686 if (async && mCore->mOverrideMaxBufferCount) {
687 // FIXME: Some drivers are manually setting the buffer count
688 // (which they shouldn't), so we do this extra test here to
689 // handle that case. This is TEMPORARY until we get this fixed.
690 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
691 BQ_LOGE("queueBuffer: async mode is invalid with "
692 "buffer count override");
693 return BAD_VALUE;
694 }
695 }
696
697 if (slot < 0 || slot >= maxBufferCount) {
698 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
699 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800700 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800701 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
702 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
703 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800704 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800705 } else if (!mSlots[slot].mRequestBufferCalled) {
706 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
707 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800708 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800709 }
710
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800711 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700712 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800713 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800714 crop.left, crop.top, crop.right, crop.bottom, transform,
715 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800716
717 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
718 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700719 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800720 crop.intersect(bufferRect, &croppedRect);
721 if (croppedRect != crop) {
722 BQ_LOGE("queueBuffer: crop rect is not contained within the "
723 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800724 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800725 }
726
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800727 // Override UNKNOWN dataspace with consumer default
728 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
729 dataSpace = mCore->mDefaultBufferDataSpace;
730 }
731
Dan Stoza289ade12014-02-28 11:17:17 -0800732 mSlots[slot].mFence = fence;
733 mSlots[slot].mBufferState = BufferSlot::QUEUED;
734 ++mCore->mFrameCounter;
735 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
736
Dan Stoza289ade12014-02-28 11:17:17 -0800737 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
738 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
739 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800740 item.mTransform = transform &
741 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800742 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800743 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
744 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800745 item.mTimestamp = timestamp;
746 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800747 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800748 item.mFrameNumber = mCore->mFrameCounter;
749 item.mSlot = slot;
750 item.mFence = fence;
751 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700752 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800753
Ruben Brunk1681d952014-06-27 15:51:55 -0700754 mStickyTransform = stickyTransform;
755
Dan Stoza289ade12014-02-28 11:17:17 -0800756 if (mCore->mQueue.empty()) {
757 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
758 // and simply queue this buffer
759 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800760 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800761 } else {
762 // When the queue is not empty, we need to look at the front buffer
763 // state to see if we need to replace it
764 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
765 if (front->mIsDroppable) {
766 // If the front queued buffer is still being tracked, we first
767 // mark it as freed
768 if (mCore->stillTracking(front)) {
769 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700770 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800771 }
772 // Overwrite the droppable buffer with the incoming one
773 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800774 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800775 } else {
776 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800777 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800778 }
779 }
780
781 mCore->mBufferHasBeenQueued = true;
782 mCore->mDequeueCondition.broadcast();
783
784 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800785 mCore->mTransformHint,
786 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800787
788 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800789
790 // Take a ticket for the callback functions
791 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700792
793 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800794 } // Autolock scope
795
Eric Penner99a0afb2014-09-30 11:28:30 -0700796 // Wait without lock held
797 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
798 // Waiting here allows for two full buffers to be queued but not a
799 // third. In the event that frames take varying time, this makes a
800 // small trade-off in favor of latency rather than throughput.
801 mLastQueueBufferFence->waitForever("Throttling EGL Production");
802 mLastQueueBufferFence = fence;
803 }
804
Dan Stoza8dc55392014-11-04 11:37:46 -0800805 // Don't send the GraphicBuffer through the callback, and don't send
806 // the slot number, since the consumer shouldn't need it
807 item.mGraphicBuffer.clear();
808 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
809
810 // Call back without the main BufferQueue lock held, but with the callback
811 // lock held so we can ensure that callbacks occur in order
812 {
813 Mutex::Autolock lock(mCallbackMutex);
814 while (callbackTicket != mCurrentCallbackTicket) {
815 mCallbackCondition.wait(mCallbackMutex);
816 }
817
818 if (frameAvailableListener != NULL) {
819 frameAvailableListener->onFrameAvailable(item);
820 } else if (frameReplacedListener != NULL) {
821 frameReplacedListener->onFrameReplaced(item);
822 }
823
824 ++mCurrentCallbackTicket;
825 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800826 }
827
828 return NO_ERROR;
829}
830
831void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
832 ATRACE_CALL();
833 BQ_LOGV("cancelBuffer: slot %d", slot);
834 Mutex::Autolock lock(mCore->mMutex);
835
836 if (mCore->mIsAbandoned) {
837 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
838 return;
839 }
840
Dan Stoza3e96f192014-03-03 10:16:19 -0800841 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800842 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800843 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800844 return;
845 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
846 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
847 "(state = %d)", slot, mSlots[slot].mBufferState);
848 return;
849 } else if (fence == NULL) {
850 BQ_LOGE("cancelBuffer: fence is NULL");
851 return;
852 }
853
Dan Stoza0de7ea72015-04-23 13:20:51 -0700854 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800855 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800856 mSlots[slot].mFence = fence;
857 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700858 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800859}
860
861int BufferQueueProducer::query(int what, int *outValue) {
862 ATRACE_CALL();
863 Mutex::Autolock lock(mCore->mMutex);
864
865 if (outValue == NULL) {
866 BQ_LOGE("query: outValue was NULL");
867 return BAD_VALUE;
868 }
869
870 if (mCore->mIsAbandoned) {
871 BQ_LOGE("query: BufferQueue has been abandoned");
872 return NO_INIT;
873 }
874
875 int value;
876 switch (what) {
877 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800878 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800879 break;
880 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800881 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800882 break;
883 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800884 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800885 break;
886 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
887 value = mCore->getMinUndequeuedBufferCountLocked(false);
888 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700889 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800890 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700891 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800892 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
893 value = (mCore->mQueue.size() > 1);
894 break;
895 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800896 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800897 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800898 case NATIVE_WINDOW_DEFAULT_DATASPACE:
899 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
900 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800901 case NATIVE_WINDOW_BUFFER_AGE:
902 if (mCore->mBufferAge > INT32_MAX) {
903 value = 0;
904 } else {
905 value = static_cast<int32_t>(mCore->mBufferAge);
906 }
907 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800908 default:
909 return BAD_VALUE;
910 }
911
912 BQ_LOGV("query: %d? %d", what, value);
913 *outValue = value;
914 return NO_ERROR;
915}
916
Dan Stozaf0eaf252014-03-21 13:05:51 -0700917status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800918 int api, bool producerControlledByApp, QueueBufferOutput *output) {
919 ATRACE_CALL();
920 Mutex::Autolock lock(mCore->mMutex);
921 mConsumerName = mCore->mConsumerName;
922 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
923 producerControlledByApp ? "true" : "false");
924
Dan Stozaae3c3682014-04-18 15:43:35 -0700925 if (mCore->mIsAbandoned) {
926 BQ_LOGE("connect(P): BufferQueue has been abandoned");
927 return NO_INIT;
928 }
Dan Stoza289ade12014-02-28 11:17:17 -0800929
Dan Stozaae3c3682014-04-18 15:43:35 -0700930 if (mCore->mConsumerListener == NULL) {
931 BQ_LOGE("connect(P): BufferQueue has no consumer");
932 return NO_INIT;
933 }
Dan Stoza289ade12014-02-28 11:17:17 -0800934
Dan Stozaae3c3682014-04-18 15:43:35 -0700935 if (output == NULL) {
936 BQ_LOGE("connect(P): output was NULL");
937 return BAD_VALUE;
938 }
Dan Stoza289ade12014-02-28 11:17:17 -0800939
Dan Stozaae3c3682014-04-18 15:43:35 -0700940 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
941 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
942 mCore->mConnectedApi, api);
943 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800944 }
945
946 int status = NO_ERROR;
947 switch (api) {
948 case NATIVE_WINDOW_API_EGL:
949 case NATIVE_WINDOW_API_CPU:
950 case NATIVE_WINDOW_API_MEDIA:
951 case NATIVE_WINDOW_API_CAMERA:
952 mCore->mConnectedApi = api;
953 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800954 mCore->mTransformHint,
955 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800956
957 // Set up a death notification so that we can disconnect
958 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700959 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800960 IInterface::asBinder(listener)->remoteBinder() != NULL) {
961 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800962 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700963 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800964 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
965 strerror(-status), status);
966 }
967 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700968 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800969 break;
970 default:
971 BQ_LOGE("connect(P): unknown API %d", api);
972 status = BAD_VALUE;
973 break;
974 }
975
976 mCore->mBufferHasBeenQueued = false;
977 mCore->mDequeueBufferCannotBlock =
978 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -0700979 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800980
981 return status;
982}
983
984status_t BufferQueueProducer::disconnect(int api) {
985 ATRACE_CALL();
986 BQ_LOGV("disconnect(P): api %d", api);
987
988 int status = NO_ERROR;
989 sp<IConsumerListener> listener;
990 { // Autolock scope
991 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700992 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800993
994 if (mCore->mIsAbandoned) {
995 // It's not really an error to disconnect after the surface has
996 // been abandoned; it should just be a no-op.
997 return NO_ERROR;
998 }
999
1000 switch (api) {
1001 case NATIVE_WINDOW_API_EGL:
1002 case NATIVE_WINDOW_API_CPU:
1003 case NATIVE_WINDOW_API_MEDIA:
1004 case NATIVE_WINDOW_API_CAMERA:
1005 if (mCore->mConnectedApi == api) {
1006 mCore->freeAllBuffersLocked();
1007
1008 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001009 if (mCore->mConnectedProducerListener != NULL) {
1010 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001011 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001012 // This can fail if we're here because of the death
1013 // notification, but we just ignore it
1014 token->unlinkToDeath(
1015 static_cast<IBinder::DeathRecipient*>(this));
1016 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001017 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001018 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001019 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001020 mCore->mDequeueCondition.broadcast();
1021 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001022 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1023 BQ_LOGE("disconnect(P): still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001024 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001025 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001026 }
1027 break;
1028 default:
1029 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001030 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001031 break;
1032 }
1033 } // Autolock scope
1034
1035 // Call back without lock held
1036 if (listener != NULL) {
1037 listener->onBuffersReleased();
1038 }
1039
1040 return status;
1041}
1042
Jesse Hall399184a2014-03-03 15:42:54 -08001043status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001044 sp<IConsumerListener> listener;
1045 { // Autolock scope
1046 Mutex::Autolock _l(mCore->mMutex);
1047 mCore->mSidebandStream = stream;
1048 listener = mCore->mConsumerListener;
1049 } // Autolock scope
1050
1051 if (listener != NULL) {
1052 listener->onSidebandStreamChanged();
1053 }
Jesse Hall399184a2014-03-03 15:42:54 -08001054 return NO_ERROR;
1055}
1056
Dan Stoza29a3e902014-06-20 13:13:57 -07001057void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001058 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001059 ATRACE_CALL();
1060 while (true) {
1061 Vector<int> freeSlots;
1062 size_t newBufferCount = 0;
1063 uint32_t allocWidth = 0;
1064 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001065 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001066 uint32_t allocUsage = 0;
1067 { // Autolock scope
1068 Mutex::Autolock lock(mCore->mMutex);
1069 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001070
Dan Stoza9de72932015-04-16 17:28:43 -07001071 if (!mCore->mAllowAllocation) {
1072 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1073 "BufferQueue");
1074 return;
1075 }
1076
Antoine Labour78014f32014-07-15 21:17:03 -07001077 int currentBufferCount = 0;
1078 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
1079 if (mSlots[slot].mGraphicBuffer != NULL) {
1080 ++currentBufferCount;
1081 } else {
1082 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1083 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1084 slot);
1085 continue;
1086 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001087
Antoine Labour11f14872014-07-25 18:14:42 -07001088 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001089 }
1090 }
1091
1092 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
1093 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1094 currentBufferCount, maxBufferCount);
1095 if (maxBufferCount <= currentBufferCount)
1096 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001097 newBufferCount =
1098 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001099 if (freeSlots.size() < newBufferCount) {
1100 BQ_LOGE("allocateBuffers: ran out of free slots");
1101 return;
1102 }
1103 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1104 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1105 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1106 allocUsage = usage | mCore->mConsumerUsageBits;
1107
1108 mCore->mIsAllocating = true;
1109 } // Autolock scope
1110
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001111 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001112 for (size_t i = 0; i < newBufferCount; ++i) {
1113 status_t result = NO_ERROR;
1114 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1115 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1116 if (result != NO_ERROR) {
1117 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1118 " %u, usage %u)", width, height, format, usage);
1119 Mutex::Autolock lock(mCore->mMutex);
1120 mCore->mIsAllocating = false;
1121 mCore->mIsAllocatingCondition.broadcast();
1122 return;
1123 }
1124 buffers.push_back(graphicBuffer);
1125 }
1126
1127 { // Autolock scope
1128 Mutex::Autolock lock(mCore->mMutex);
1129 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1130 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001131 PixelFormat checkFormat = format != 0 ?
1132 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001133 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1134 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1135 checkFormat != allocFormat || checkUsage != allocUsage) {
1136 // Something changed while we released the lock. Retry.
1137 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1138 mCore->mIsAllocating = false;
1139 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001140 continue;
1141 }
1142
Antoine Labour78014f32014-07-15 21:17:03 -07001143 for (size_t i = 0; i < newBufferCount; ++i) {
1144 int slot = freeSlots[i];
1145 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1146 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1147 // allocated.
1148 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1149 "Dropping allocated buffer.", slot);
1150 continue;
1151 }
1152 mCore->freeBufferLocked(slot); // Clean up the slot first
1153 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001154 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001155
1156 // freeBufferLocked puts this slot on the free slots list. Since
1157 // we then attached a buffer, move the slot to free buffer list.
1158 mCore->mFreeSlots.erase(slot);
1159 mCore->mFreeBuffers.push_front(slot);
1160
Antoine Labour78014f32014-07-15 21:17:03 -07001161 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1162 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001163
Antoine Labour78014f32014-07-15 21:17:03 -07001164 mCore->mIsAllocating = false;
1165 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001166 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001167 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001168 }
1169}
1170
Dan Stoza9de72932015-04-16 17:28:43 -07001171status_t BufferQueueProducer::allowAllocation(bool allow) {
1172 ATRACE_CALL();
1173 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1174
1175 Mutex::Autolock lock(mCore->mMutex);
1176 mCore->mAllowAllocation = allow;
1177 return NO_ERROR;
1178}
1179
Dan Stoza812ed062015-06-02 15:45:22 -07001180status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1181 ATRACE_CALL();
1182 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1183
1184 Mutex::Autolock lock(mCore->mMutex);
1185 mCore->mGenerationNumber = generationNumber;
1186 return NO_ERROR;
1187}
1188
Dan Stozac6f30bd2015-06-08 09:32:50 -07001189String8 BufferQueueProducer::getConsumerName() const {
1190 ATRACE_CALL();
1191 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1192 return mConsumerName;
1193}
1194
Dan Stoza289ade12014-02-28 11:17:17 -08001195void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1196 // If we're here, it means that a producer we were connected to died.
1197 // We're guaranteed that we are still connected to it because we remove
1198 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1199 // without synchronization here.
1200 int api = mCore->mConnectedApi;
1201 disconnect(api);
1202}
1203
1204} // namespace android