blob: 6feebf72cb4c16cda953c070ea3742b970df33da [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(),
41 mStickyTransform(0) {}
Dan Stoza289ade12014-02-28 11:17:17 -080042
43BufferQueueProducer::~BufferQueueProducer() {}
44
45status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
46 ATRACE_CALL();
47 BQ_LOGV("requestBuffer: slot %d", slot);
48 Mutex::Autolock lock(mCore->mMutex);
49
50 if (mCore->mIsAbandoned) {
51 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
52 return NO_INIT;
53 }
54
Dan Stoza3e96f192014-03-03 10:16:19 -080055 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080056 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080057 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080058 return BAD_VALUE;
59 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
60 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
61 "(state = %d)", slot, mSlots[slot].mBufferState);
62 return BAD_VALUE;
63 }
64
65 mSlots[slot].mRequestBufferCalled = true;
66 *buf = mSlots[slot].mGraphicBuffer;
67 return NO_ERROR;
68}
69
70status_t BufferQueueProducer::setBufferCount(int bufferCount) {
71 ATRACE_CALL();
72 BQ_LOGV("setBufferCount: count = %d", bufferCount);
73
74 sp<IConsumerListener> listener;
75 { // Autolock scope
76 Mutex::Autolock lock(mCore->mMutex);
77
78 if (mCore->mIsAbandoned) {
79 BQ_LOGE("setBufferCount: BufferQueue has been abandoned");
80 return NO_INIT;
81 }
82
Dan Stoza3e96f192014-03-03 10:16:19 -080083 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080084 BQ_LOGE("setBufferCount: bufferCount %d too large (max %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080085 bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080086 return BAD_VALUE;
87 }
88
89 // There must be no dequeued buffers when changing the buffer count.
Dan Stoza3e96f192014-03-03 10:16:19 -080090 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -080091 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
92 BQ_LOGE("setBufferCount: buffer owned by producer");
Dan Stoza9f3053d2014-03-06 15:14:33 -080093 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -080094 }
95 }
96
97 if (bufferCount == 0) {
98 mCore->mOverrideMaxBufferCount = 0;
99 mCore->mDequeueCondition.broadcast();
100 return NO_ERROR;
101 }
102
103 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(false);
104 if (bufferCount < minBufferSlots) {
105 BQ_LOGE("setBufferCount: requested buffer count %d is less than "
106 "minimum %d", bufferCount, minBufferSlots);
107 return BAD_VALUE;
108 }
109
110 // Here we are guaranteed that the producer doesn't have any dequeued
111 // buffers and will release all of its buffer references. We don't
112 // clear the queue, however, so that currently queued buffers still
113 // get displayed.
114 mCore->freeAllBuffersLocked();
115 mCore->mOverrideMaxBufferCount = bufferCount;
116 mCore->mDequeueCondition.broadcast();
117 listener = mCore->mConsumerListener;
118 } // Autolock scope
119
120 // Call back without lock held
121 if (listener != NULL) {
122 listener->onBuffersReleased();
123 }
124
125 return NO_ERROR;
126}
127
Dan Stoza9f3053d2014-03-06 15:14:33 -0800128status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
129 bool async, int* found, status_t* returnFlags) const {
130 bool tryAgain = true;
131 while (tryAgain) {
132 if (mCore->mIsAbandoned) {
133 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
134 return NO_INIT;
135 }
136
137 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
138 if (async && mCore->mOverrideMaxBufferCount) {
139 // FIXME: Some drivers are manually setting the buffer count
140 // (which they shouldn't), so we do this extra test here to
141 // handle that case. This is TEMPORARY until we get this fixed.
142 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
143 BQ_LOGE("%s: async mode is invalid with buffer count override",
144 caller);
145 return BAD_VALUE;
146 }
147 }
148
149 // Free up any buffers that are in slots beyond the max buffer count
150 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
151 assert(mSlots[s].mBufferState == BufferSlot::FREE);
152 if (mSlots[s].mGraphicBuffer != NULL) {
153 mCore->freeBufferLocked(s);
154 *returnFlags |= RELEASE_ALL_BUFFERS;
155 }
156 }
157
158 // Look for a free buffer to give to the client
159 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
160 int dequeuedCount = 0;
161 int acquiredCount = 0;
162 for (int s = 0; s < maxBufferCount; ++s) {
163 switch (mSlots[s].mBufferState) {
164 case BufferSlot::DEQUEUED:
165 ++dequeuedCount;
166 break;
167 case BufferSlot::ACQUIRED:
168 ++acquiredCount;
169 break;
170 case BufferSlot::FREE:
171 // We return the oldest of the free buffers to avoid
172 // stalling the producer if possible, since the consumer
173 // may still have pending reads of in-flight buffers
174 if (*found == BufferQueueCore::INVALID_BUFFER_SLOT ||
175 mSlots[s].mFrameNumber < mSlots[*found].mFrameNumber) {
176 *found = s;
177 }
178 break;
179 default:
180 break;
181 }
182 }
183
184 // Producers are not allowed to dequeue more than one buffer if they
185 // did not set a buffer count
186 if (!mCore->mOverrideMaxBufferCount && dequeuedCount) {
187 BQ_LOGE("%s: can't dequeue multiple buffers without setting the "
188 "buffer count", caller);
189 return INVALID_OPERATION;
190 }
191
192 // See whether a buffer has been queued since the last
193 // setBufferCount so we know whether to perform the min undequeued
194 // buffers check below
195 if (mCore->mBufferHasBeenQueued) {
196 // Make sure the producer is not trying to dequeue more buffers
197 // than allowed
198 const int newUndequeuedCount =
199 maxBufferCount - (dequeuedCount + 1);
200 const int minUndequeuedCount =
201 mCore->getMinUndequeuedBufferCountLocked(async);
202 if (newUndequeuedCount < minUndequeuedCount) {
203 BQ_LOGE("%s: min undequeued buffer count (%d) exceeded "
204 "(dequeued=%d undequeued=%d)",
205 caller, minUndequeuedCount,
206 dequeuedCount, newUndequeuedCount);
207 return INVALID_OPERATION;
208 }
209 }
210
Dan Stozaae3c3682014-04-18 15:43:35 -0700211 // If we disconnect and reconnect quickly, we can be in a state where
212 // our slots are empty but we have many buffers in the queue. This can
213 // cause us to run out of memory if we outrun the consumer. Wait here if
214 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700215 bool tooManyBuffers = mCore->mQueue.size()
216 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700217 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700218 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700219 mCore->mQueue.size());
220 }
221
222 // If no buffer is found, or if the queue has too many buffers
223 // outstanding, wait for a buffer to be acquired or released, or for the
224 // max buffer count to change.
225 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
226 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800227 if (tryAgain) {
228 // Return an error if we're in non-blocking mode (producer and
229 // consumer are controlled by the application).
230 // However, the consumer is allowed to briefly acquire an extra
231 // buffer (which could cause us to have to wait here), which is
232 // okay, since it is only used to implement an atomic acquire +
233 // release (e.g., in GLConsumer::updateTexImage())
234 if (mCore->mDequeueBufferCannotBlock &&
235 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
236 return WOULD_BLOCK;
237 }
238 mCore->mDequeueCondition.wait(mCore->mMutex);
239 }
240 } // while (tryAgain)
241
242 return NO_ERROR;
243}
244
Dan Stoza289ade12014-02-28 11:17:17 -0800245status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
246 sp<android::Fence> *outFence, bool async,
247 uint32_t width, uint32_t height, uint32_t format, uint32_t usage) {
248 ATRACE_CALL();
249 { // Autolock scope
250 Mutex::Autolock lock(mCore->mMutex);
251 mConsumerName = mCore->mConsumerName;
252 } // Autolock scope
253
254 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
255 async ? "true" : "false", width, height, format, usage);
256
257 if ((width && !height) || (!width && height)) {
258 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
259 return BAD_VALUE;
260 }
261
262 status_t returnFlags = NO_ERROR;
263 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
264 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800265 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800266
267 { // Autolock scope
268 Mutex::Autolock lock(mCore->mMutex);
269
270 if (format == 0) {
271 format = mCore->mDefaultBufferFormat;
272 }
273
274 // Enable the usage bits the consumer requested
275 usage |= mCore->mConsumerUsageBits;
276
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 int found;
278 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
279 &found, &returnFlags);
280 if (status != NO_ERROR) {
281 return status;
282 }
Dan Stoza289ade12014-02-28 11:17:17 -0800283
284 // This should not happen
285 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
286 BQ_LOGE("dequeueBuffer: no available buffer slots");
287 return -EBUSY;
288 }
289
290 *outSlot = found;
291 ATRACE_BUFFER_INDEX(found);
292
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 attachedByConsumer = mSlots[found].mAttachedByConsumer;
294
Dan Stoza289ade12014-02-28 11:17:17 -0800295 const bool useDefaultSize = !width && !height;
296 if (useDefaultSize) {
297 width = mCore->mDefaultWidth;
298 height = mCore->mDefaultHeight;
299 }
300
301 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
302
303 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
304 if ((buffer == NULL) ||
305 (static_cast<uint32_t>(buffer->width) != width) ||
306 (static_cast<uint32_t>(buffer->height) != height) ||
307 (static_cast<uint32_t>(buffer->format) != format) ||
308 ((static_cast<uint32_t>(buffer->usage) & usage) != usage))
309 {
310 mSlots[found].mAcquireCalled = false;
311 mSlots[found].mGraphicBuffer = NULL;
312 mSlots[found].mRequestBufferCalled = false;
313 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
314 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
315 mSlots[found].mFence = Fence::NO_FENCE;
316
317 returnFlags |= BUFFER_NEEDS_REALLOCATION;
318 }
319
320 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
321 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
322 "slot=%d w=%d h=%d format=%u",
323 found, buffer->width, buffer->height, buffer->format);
324 }
325
326 eglDisplay = mSlots[found].mEglDisplay;
327 eglFence = mSlots[found].mEglFence;
328 *outFence = mSlots[found].mFence;
329 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
330 mSlots[found].mFence = Fence::NO_FENCE;
331 } // Autolock scope
332
333 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
334 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700335 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800336 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
337 width, height, format, usage, &error));
338 if (graphicBuffer == NULL) {
339 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
340 return error;
341 }
342
343 { // Autolock scope
344 Mutex::Autolock lock(mCore->mMutex);
345
346 if (mCore->mIsAbandoned) {
347 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
348 return NO_INIT;
349 }
350
Dan Stoza9f3053d2014-03-06 15:14:33 -0800351 mSlots[*outSlot].mFrameNumber = UINT32_MAX;
Dan Stoza289ade12014-02-28 11:17:17 -0800352 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
353 } // Autolock scope
354 }
355
Dan Stoza9f3053d2014-03-06 15:14:33 -0800356 if (attachedByConsumer) {
357 returnFlags |= BUFFER_NEEDS_REALLOCATION;
358 }
359
Dan Stoza289ade12014-02-28 11:17:17 -0800360 if (eglFence != EGL_NO_SYNC_KHR) {
361 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
362 1000000000);
363 // If something goes wrong, log the error, but return the buffer without
364 // synchronizing access to it. It's too late at this point to abort the
365 // dequeue operation.
366 if (result == EGL_FALSE) {
367 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
368 eglGetError());
369 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
370 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
371 }
372 eglDestroySyncKHR(eglDisplay, eglFence);
373 }
374
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700375 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
376 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800377 mSlots[*outSlot].mFrameNumber,
378 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
379
380 return returnFlags;
381}
382
Dan Stoza9f3053d2014-03-06 15:14:33 -0800383status_t BufferQueueProducer::detachBuffer(int slot) {
384 ATRACE_CALL();
385 ATRACE_BUFFER_INDEX(slot);
386 BQ_LOGV("detachBuffer(P): slot %d", slot);
387 Mutex::Autolock lock(mCore->mMutex);
388
389 if (mCore->mIsAbandoned) {
390 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
391 return NO_INIT;
392 }
393
394 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
395 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
396 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
397 return BAD_VALUE;
398 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
399 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
400 "(state = %d)", slot, mSlots[slot].mBufferState);
401 return BAD_VALUE;
402 } else if (!mSlots[slot].mRequestBufferCalled) {
403 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
404 slot);
405 return BAD_VALUE;
406 }
407
408 mCore->freeBufferLocked(slot);
409 mCore->mDequeueCondition.broadcast();
410
411 return NO_ERROR;
412}
413
Dan Stozad9822a32014-03-28 15:25:31 -0700414status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
415 sp<Fence>* outFence) {
416 ATRACE_CALL();
417
418 if (outBuffer == NULL) {
419 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
420 return BAD_VALUE;
421 } else if (outFence == NULL) {
422 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
423 return BAD_VALUE;
424 }
425
426 Mutex::Autolock lock(mCore->mMutex);
427
428 if (mCore->mIsAbandoned) {
429 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
430 return NO_INIT;
431 }
432
433 // Find the oldest valid slot
434 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
435 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
436 if (mSlots[s].mBufferState == BufferSlot::FREE &&
437 mSlots[s].mGraphicBuffer != NULL) {
438 if (found == BufferQueueCore::INVALID_BUFFER_SLOT ||
439 mSlots[s].mFrameNumber < mSlots[found].mFrameNumber) {
440 found = s;
441 }
442 }
443 }
444
445 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
446 return NO_MEMORY;
447 }
448
449 BQ_LOGV("detachNextBuffer detached slot %d", found);
450
451 *outBuffer = mSlots[found].mGraphicBuffer;
452 *outFence = mSlots[found].mFence;
453 mCore->freeBufferLocked(found);
454
455 return NO_ERROR;
456}
457
Dan Stoza9f3053d2014-03-06 15:14:33 -0800458status_t BufferQueueProducer::attachBuffer(int* outSlot,
459 const sp<android::GraphicBuffer>& buffer) {
460 ATRACE_CALL();
461
462 if (outSlot == NULL) {
463 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
464 return BAD_VALUE;
465 } else if (buffer == NULL) {
466 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
467 return BAD_VALUE;
468 }
469
470 Mutex::Autolock lock(mCore->mMutex);
471
472 status_t returnFlags = NO_ERROR;
473 int found;
474 // TODO: Should we provide an async flag to attachBuffer? It seems
475 // unlikely that buffers which we are attaching to a BufferQueue will
476 // be asynchronous (droppable), but it may not be impossible.
477 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
478 &found, &returnFlags);
479 if (status != NO_ERROR) {
480 return status;
481 }
482
483 // This should not happen
484 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
485 BQ_LOGE("attachBuffer(P): no available buffer slots");
486 return -EBUSY;
487 }
488
489 *outSlot = found;
490 ATRACE_BUFFER_INDEX(*outSlot);
491 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
492 *outSlot, returnFlags);
493
494 mSlots[*outSlot].mGraphicBuffer = buffer;
495 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
496 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
497 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700498 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800499
500 return returnFlags;
501}
502
Dan Stoza289ade12014-02-28 11:17:17 -0800503status_t BufferQueueProducer::queueBuffer(int slot,
504 const QueueBufferInput &input, QueueBufferOutput *output) {
505 ATRACE_CALL();
506 ATRACE_BUFFER_INDEX(slot);
507
508 int64_t timestamp;
509 bool isAutoTimestamp;
510 Rect crop;
511 int scalingMode;
512 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700513 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800514 bool async;
515 sp<Fence> fence;
516 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode, &transform,
Ruben Brunk1681d952014-06-27 15:51:55 -0700517 &async, &fence, &stickyTransform);
Dan Stoza289ade12014-02-28 11:17:17 -0800518
519 if (fence == NULL) {
520 BQ_LOGE("queueBuffer: fence is NULL");
521 return BAD_VALUE;
522 }
523
524 switch (scalingMode) {
525 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
526 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
527 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
528 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
529 break;
530 default:
531 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800532 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800533 }
534
535 sp<IConsumerListener> listener;
536 { // Autolock scope
537 Mutex::Autolock lock(mCore->mMutex);
538
539 if (mCore->mIsAbandoned) {
540 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
541 return NO_INIT;
542 }
543
544 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
545 if (async && mCore->mOverrideMaxBufferCount) {
546 // FIXME: Some drivers are manually setting the buffer count
547 // (which they shouldn't), so we do this extra test here to
548 // handle that case. This is TEMPORARY until we get this fixed.
549 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
550 BQ_LOGE("queueBuffer: async mode is invalid with "
551 "buffer count override");
552 return BAD_VALUE;
553 }
554 }
555
556 if (slot < 0 || slot >= maxBufferCount) {
557 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
558 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800559 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800560 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
561 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
562 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800563 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800564 } else if (!mSlots[slot].mRequestBufferCalled) {
565 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
566 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800567 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800568 }
569
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700570 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64
571 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800572 slot, mCore->mFrameCounter + 1, timestamp,
573 crop.left, crop.top, crop.right, crop.bottom,
574 transform, BufferItem::scalingModeName(scalingMode));
575
576 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
577 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
578 Rect croppedRect;
579 crop.intersect(bufferRect, &croppedRect);
580 if (croppedRect != crop) {
581 BQ_LOGE("queueBuffer: crop rect is not contained within the "
582 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800583 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800584 }
585
586 mSlots[slot].mFence = fence;
587 mSlots[slot].mBufferState = BufferSlot::QUEUED;
588 ++mCore->mFrameCounter;
589 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
590
591 BufferItem item;
592 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
593 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
594 item.mCrop = crop;
595 item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
596 item.mTransformToDisplayInverse =
597 bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
598 item.mScalingMode = scalingMode;
599 item.mTimestamp = timestamp;
600 item.mIsAutoTimestamp = isAutoTimestamp;
601 item.mFrameNumber = mCore->mFrameCounter;
602 item.mSlot = slot;
603 item.mFence = fence;
604 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
605
Ruben Brunk1681d952014-06-27 15:51:55 -0700606 mStickyTransform = stickyTransform;
607
Dan Stoza289ade12014-02-28 11:17:17 -0800608 if (mCore->mQueue.empty()) {
609 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
610 // and simply queue this buffer
611 mCore->mQueue.push_back(item);
612 listener = mCore->mConsumerListener;
613 } else {
614 // When the queue is not empty, we need to look at the front buffer
615 // state to see if we need to replace it
616 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
617 if (front->mIsDroppable) {
618 // If the front queued buffer is still being tracked, we first
619 // mark it as freed
620 if (mCore->stillTracking(front)) {
621 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
622 // Reset the frame number of the freed buffer so that it is
623 // the first in line to be dequeued again
624 mSlots[front->mSlot].mFrameNumber = 0;
625 }
626 // Overwrite the droppable buffer with the incoming one
627 *front = item;
628 } else {
629 mCore->mQueue.push_back(item);
630 listener = mCore->mConsumerListener;
631 }
632 }
633
634 mCore->mBufferHasBeenQueued = true;
635 mCore->mDequeueCondition.broadcast();
636
637 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
638 mCore->mTransformHint, mCore->mQueue.size());
639
640 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
641 } // Autolock scope
642
643 // Call back without lock held
644 if (listener != NULL) {
645 listener->onFrameAvailable();
646 }
647
648 return NO_ERROR;
649}
650
651void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
652 ATRACE_CALL();
653 BQ_LOGV("cancelBuffer: slot %d", slot);
654 Mutex::Autolock lock(mCore->mMutex);
655
656 if (mCore->mIsAbandoned) {
657 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
658 return;
659 }
660
Dan Stoza3e96f192014-03-03 10:16:19 -0800661 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800662 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800663 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800664 return;
665 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
666 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
667 "(state = %d)", slot, mSlots[slot].mBufferState);
668 return;
669 } else if (fence == NULL) {
670 BQ_LOGE("cancelBuffer: fence is NULL");
671 return;
672 }
673
674 mSlots[slot].mBufferState = BufferSlot::FREE;
675 mSlots[slot].mFrameNumber = 0;
676 mSlots[slot].mFence = fence;
677 mCore->mDequeueCondition.broadcast();
678}
679
680int BufferQueueProducer::query(int what, int *outValue) {
681 ATRACE_CALL();
682 Mutex::Autolock lock(mCore->mMutex);
683
684 if (outValue == NULL) {
685 BQ_LOGE("query: outValue was NULL");
686 return BAD_VALUE;
687 }
688
689 if (mCore->mIsAbandoned) {
690 BQ_LOGE("query: BufferQueue has been abandoned");
691 return NO_INIT;
692 }
693
694 int value;
695 switch (what) {
696 case NATIVE_WINDOW_WIDTH:
697 value = mCore->mDefaultWidth;
698 break;
699 case NATIVE_WINDOW_HEIGHT:
700 value = mCore->mDefaultHeight;
701 break;
702 case NATIVE_WINDOW_FORMAT:
703 value = mCore->mDefaultBufferFormat;
704 break;
705 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
706 value = mCore->getMinUndequeuedBufferCountLocked(false);
707 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700708 case NATIVE_WINDOW_STICKY_TRANSFORM:
709 value = static_cast<int>(mStickyTransform);
710 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800711 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
712 value = (mCore->mQueue.size() > 1);
713 break;
714 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
715 value = mCore->mConsumerUsageBits;
716 break;
717 default:
718 return BAD_VALUE;
719 }
720
721 BQ_LOGV("query: %d? %d", what, value);
722 *outValue = value;
723 return NO_ERROR;
724}
725
Dan Stozaf0eaf252014-03-21 13:05:51 -0700726status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800727 int api, bool producerControlledByApp, QueueBufferOutput *output) {
728 ATRACE_CALL();
729 Mutex::Autolock lock(mCore->mMutex);
730 mConsumerName = mCore->mConsumerName;
731 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
732 producerControlledByApp ? "true" : "false");
733
Dan Stozaae3c3682014-04-18 15:43:35 -0700734 if (mCore->mIsAbandoned) {
735 BQ_LOGE("connect(P): BufferQueue has been abandoned");
736 return NO_INIT;
737 }
Dan Stoza289ade12014-02-28 11:17:17 -0800738
Dan Stozaae3c3682014-04-18 15:43:35 -0700739 if (mCore->mConsumerListener == NULL) {
740 BQ_LOGE("connect(P): BufferQueue has no consumer");
741 return NO_INIT;
742 }
Dan Stoza289ade12014-02-28 11:17:17 -0800743
Dan Stozaae3c3682014-04-18 15:43:35 -0700744 if (output == NULL) {
745 BQ_LOGE("connect(P): output was NULL");
746 return BAD_VALUE;
747 }
Dan Stoza289ade12014-02-28 11:17:17 -0800748
Dan Stozaae3c3682014-04-18 15:43:35 -0700749 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
750 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
751 mCore->mConnectedApi, api);
752 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800753 }
754
755 int status = NO_ERROR;
756 switch (api) {
757 case NATIVE_WINDOW_API_EGL:
758 case NATIVE_WINDOW_API_CPU:
759 case NATIVE_WINDOW_API_MEDIA:
760 case NATIVE_WINDOW_API_CAMERA:
761 mCore->mConnectedApi = api;
762 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
763 mCore->mTransformHint, mCore->mQueue.size());
764
765 // Set up a death notification so that we can disconnect
766 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700767 if (listener != NULL &&
768 listener->asBinder()->remoteBinder() != NULL) {
769 status = listener->asBinder()->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800770 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700771 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800772 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
773 strerror(-status), status);
774 }
775 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700776 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800777 break;
778 default:
779 BQ_LOGE("connect(P): unknown API %d", api);
780 status = BAD_VALUE;
781 break;
782 }
783
784 mCore->mBufferHasBeenQueued = false;
785 mCore->mDequeueBufferCannotBlock =
786 mCore->mConsumerControlledByApp && producerControlledByApp;
787
788 return status;
789}
790
791status_t BufferQueueProducer::disconnect(int api) {
792 ATRACE_CALL();
793 BQ_LOGV("disconnect(P): api %d", api);
794
795 int status = NO_ERROR;
796 sp<IConsumerListener> listener;
797 { // Autolock scope
798 Mutex::Autolock lock(mCore->mMutex);
799
800 if (mCore->mIsAbandoned) {
801 // It's not really an error to disconnect after the surface has
802 // been abandoned; it should just be a no-op.
803 return NO_ERROR;
804 }
805
806 switch (api) {
807 case NATIVE_WINDOW_API_EGL:
808 case NATIVE_WINDOW_API_CPU:
809 case NATIVE_WINDOW_API_MEDIA:
810 case NATIVE_WINDOW_API_CAMERA:
811 if (mCore->mConnectedApi == api) {
812 mCore->freeAllBuffersLocked();
813
814 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700815 if (mCore->mConnectedProducerListener != NULL) {
816 sp<IBinder> token =
817 mCore->mConnectedProducerListener->asBinder();
Dan Stoza289ade12014-02-28 11:17:17 -0800818 // This can fail if we're here because of the death
819 // notification, but we just ignore it
820 token->unlinkToDeath(
821 static_cast<IBinder::DeathRecipient*>(this));
822 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700823 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800824 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800825 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800826 mCore->mDequeueCondition.broadcast();
827 listener = mCore->mConsumerListener;
828 } else {
829 BQ_LOGE("disconnect(P): connected to another API "
830 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800831 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800832 }
833 break;
834 default:
835 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800836 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800837 break;
838 }
839 } // Autolock scope
840
841 // Call back without lock held
842 if (listener != NULL) {
843 listener->onBuffersReleased();
844 }
845
846 return status;
847}
848
Jesse Hall399184a2014-03-03 15:42:54 -0800849status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900850 sp<IConsumerListener> listener;
851 { // Autolock scope
852 Mutex::Autolock _l(mCore->mMutex);
853 mCore->mSidebandStream = stream;
854 listener = mCore->mConsumerListener;
855 } // Autolock scope
856
857 if (listener != NULL) {
858 listener->onSidebandStreamChanged();
859 }
Jesse Hall399184a2014-03-03 15:42:54 -0800860 return NO_ERROR;
861}
862
Dan Stoza29a3e902014-06-20 13:13:57 -0700863void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
864 uint32_t height, uint32_t format, uint32_t usage) {
865 Vector<int> freeSlots;
866
867 Mutex::Autolock lock(mCore->mMutex);
868
869 int currentBufferCount = 0;
870 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
871 if (mSlots[slot].mGraphicBuffer != NULL) {
872 ++currentBufferCount;
873 } else {
874 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
875 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
876 slot);
877 continue;
878 }
879
880 freeSlots.push_front(slot);
881 }
882 }
883
884 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
885 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
886 currentBufferCount, maxBufferCount);
887 for (; currentBufferCount < maxBufferCount; ++currentBufferCount) {
888 if (freeSlots.empty()) {
889 BQ_LOGE("allocateBuffers: ran out of free slots");
890 return;
891 }
892
893 width = width > 0 ? width : mCore->mDefaultWidth;
894 height = height > 0 ? height : mCore->mDefaultHeight;
895 format = format != 0 ? format : mCore->mDefaultBufferFormat;
896 usage |= mCore->mConsumerUsageBits;
897
898 status_t result = NO_ERROR;
899 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
900 width, height, format, usage, &result));
901 if (result != NO_ERROR) {
902 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
903 " %u, usage %u)", width, height, format, usage);
904 return;
905 }
906
907 int slot = freeSlots[freeSlots.size() - 1];
908 mCore->freeBufferLocked(slot); // Clean up the slot first
909 mSlots[slot].mGraphicBuffer = graphicBuffer;
910 mSlots[slot].mFrameNumber = 0;
911 mSlots[slot].mFence = Fence::NO_FENCE;
912 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
913 freeSlots.pop();
914 }
915}
916
Dan Stoza289ade12014-02-28 11:17:17 -0800917void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
918 // If we're here, it means that a producer we were connected to died.
919 // We're guaranteed that we are still connected to it because we remove
920 // this callback upon disconnect. It's therefore safe to read mConnectedApi
921 // without synchronization here.
922 int api = mCore->mConnectedApi;
923 disconnect(api);
924}
925
926} // namespace android