blob: ea37309fdb0d7eb1bcfd196f044173acd3e905a9 [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
Dan Stoza3e96f192014-03-03 10:16:19 -080017#define LOG_TAG "BufferQueueProducer"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
Dan Stoza289ade12014-02-28 11:17:17 -080021#define EGL_EGLEXT_PROTOTYPES
22
23#include <gui/BufferItem.h>
24#include <gui/BufferQueueCore.h>
25#include <gui/BufferQueueProducer.h>
26#include <gui/IConsumerListener.h>
27#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070028#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080029
30#include <utils/Log.h>
31#include <utils/Trace.h>
32
33namespace android {
34
35BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
36 mCore(core),
37 mSlots(core->mSlots),
38 mConsumerName() {}
39
40BufferQueueProducer::~BufferQueueProducer() {}
41
42status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
43 ATRACE_CALL();
44 BQ_LOGV("requestBuffer: slot %d", slot);
45 Mutex::Autolock lock(mCore->mMutex);
46
47 if (mCore->mIsAbandoned) {
48 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
49 return NO_INIT;
50 }
51
Dan Stoza3e96f192014-03-03 10:16:19 -080052 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080053 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080054 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080055 return BAD_VALUE;
56 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
57 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
58 "(state = %d)", slot, mSlots[slot].mBufferState);
59 return BAD_VALUE;
60 }
61
62 mSlots[slot].mRequestBufferCalled = true;
63 *buf = mSlots[slot].mGraphicBuffer;
64 return NO_ERROR;
65}
66
67status_t BufferQueueProducer::setBufferCount(int bufferCount) {
68 ATRACE_CALL();
69 BQ_LOGV("setBufferCount: count = %d", bufferCount);
70
71 sp<IConsumerListener> listener;
72 { // Autolock scope
73 Mutex::Autolock lock(mCore->mMutex);
74
75 if (mCore->mIsAbandoned) {
76 BQ_LOGE("setBufferCount: BufferQueue has been abandoned");
77 return NO_INIT;
78 }
79
Dan Stoza3e96f192014-03-03 10:16:19 -080080 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080081 BQ_LOGE("setBufferCount: bufferCount %d too large (max %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080082 bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080083 return BAD_VALUE;
84 }
85
86 // There must be no dequeued buffers when changing the buffer count.
Dan Stoza3e96f192014-03-03 10:16:19 -080087 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -080088 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
89 BQ_LOGE("setBufferCount: buffer owned by producer");
Dan Stoza9f3053d2014-03-06 15:14:33 -080090 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -080091 }
92 }
93
94 if (bufferCount == 0) {
95 mCore->mOverrideMaxBufferCount = 0;
96 mCore->mDequeueCondition.broadcast();
97 return NO_ERROR;
98 }
99
100 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(false);
101 if (bufferCount < minBufferSlots) {
102 BQ_LOGE("setBufferCount: requested buffer count %d is less than "
103 "minimum %d", bufferCount, minBufferSlots);
104 return BAD_VALUE;
105 }
106
107 // Here we are guaranteed that the producer doesn't have any dequeued
108 // buffers and will release all of its buffer references. We don't
109 // clear the queue, however, so that currently queued buffers still
110 // get displayed.
111 mCore->freeAllBuffersLocked();
112 mCore->mOverrideMaxBufferCount = bufferCount;
113 mCore->mDequeueCondition.broadcast();
114 listener = mCore->mConsumerListener;
115 } // Autolock scope
116
117 // Call back without lock held
118 if (listener != NULL) {
119 listener->onBuffersReleased();
120 }
121
122 return NO_ERROR;
123}
124
Dan Stoza9f3053d2014-03-06 15:14:33 -0800125status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
126 bool async, int* found, status_t* returnFlags) const {
127 bool tryAgain = true;
128 while (tryAgain) {
129 if (mCore->mIsAbandoned) {
130 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
131 return NO_INIT;
132 }
133
134 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
135 if (async && mCore->mOverrideMaxBufferCount) {
136 // FIXME: Some drivers are manually setting the buffer count
137 // (which they shouldn't), so we do this extra test here to
138 // handle that case. This is TEMPORARY until we get this fixed.
139 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
140 BQ_LOGE("%s: async mode is invalid with buffer count override",
141 caller);
142 return BAD_VALUE;
143 }
144 }
145
146 // Free up any buffers that are in slots beyond the max buffer count
147 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
148 assert(mSlots[s].mBufferState == BufferSlot::FREE);
149 if (mSlots[s].mGraphicBuffer != NULL) {
150 mCore->freeBufferLocked(s);
151 *returnFlags |= RELEASE_ALL_BUFFERS;
152 }
153 }
154
155 // Look for a free buffer to give to the client
156 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
157 int dequeuedCount = 0;
158 int acquiredCount = 0;
159 for (int s = 0; s < maxBufferCount; ++s) {
160 switch (mSlots[s].mBufferState) {
161 case BufferSlot::DEQUEUED:
162 ++dequeuedCount;
163 break;
164 case BufferSlot::ACQUIRED:
165 ++acquiredCount;
166 break;
167 case BufferSlot::FREE:
168 // We return the oldest of the free buffers to avoid
169 // stalling the producer if possible, since the consumer
170 // may still have pending reads of in-flight buffers
171 if (*found == BufferQueueCore::INVALID_BUFFER_SLOT ||
172 mSlots[s].mFrameNumber < mSlots[*found].mFrameNumber) {
173 *found = s;
174 }
175 break;
176 default:
177 break;
178 }
179 }
180
181 // Producers are not allowed to dequeue more than one buffer if they
182 // did not set a buffer count
183 if (!mCore->mOverrideMaxBufferCount && dequeuedCount) {
184 BQ_LOGE("%s: can't dequeue multiple buffers without setting the "
185 "buffer count", caller);
186 return INVALID_OPERATION;
187 }
188
189 // See whether a buffer has been queued since the last
190 // setBufferCount so we know whether to perform the min undequeued
191 // buffers check below
192 if (mCore->mBufferHasBeenQueued) {
193 // Make sure the producer is not trying to dequeue more buffers
194 // than allowed
195 const int newUndequeuedCount =
196 maxBufferCount - (dequeuedCount + 1);
197 const int minUndequeuedCount =
198 mCore->getMinUndequeuedBufferCountLocked(async);
199 if (newUndequeuedCount < minUndequeuedCount) {
200 BQ_LOGE("%s: min undequeued buffer count (%d) exceeded "
201 "(dequeued=%d undequeued=%d)",
202 caller, minUndequeuedCount,
203 dequeuedCount, newUndequeuedCount);
204 return INVALID_OPERATION;
205 }
206 }
207
208 // If no buffer is found, wait for a buffer to be released or for
209 // the max buffer count to change
210 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT);
211 if (tryAgain) {
212 // Return an error if we're in non-blocking mode (producer and
213 // consumer are controlled by the application).
214 // However, the consumer is allowed to briefly acquire an extra
215 // buffer (which could cause us to have to wait here), which is
216 // okay, since it is only used to implement an atomic acquire +
217 // release (e.g., in GLConsumer::updateTexImage())
218 if (mCore->mDequeueBufferCannotBlock &&
219 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
220 return WOULD_BLOCK;
221 }
222 mCore->mDequeueCondition.wait(mCore->mMutex);
223 }
224 } // while (tryAgain)
225
226 return NO_ERROR;
227}
228
Dan Stoza289ade12014-02-28 11:17:17 -0800229status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
230 sp<android::Fence> *outFence, bool async,
231 uint32_t width, uint32_t height, uint32_t format, uint32_t usage) {
232 ATRACE_CALL();
233 { // Autolock scope
234 Mutex::Autolock lock(mCore->mMutex);
235 mConsumerName = mCore->mConsumerName;
236 } // Autolock scope
237
238 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
239 async ? "true" : "false", width, height, format, usage);
240
241 if ((width && !height) || (!width && height)) {
242 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
243 return BAD_VALUE;
244 }
245
246 status_t returnFlags = NO_ERROR;
247 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
248 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800249 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800250
251 { // Autolock scope
252 Mutex::Autolock lock(mCore->mMutex);
253
254 if (format == 0) {
255 format = mCore->mDefaultBufferFormat;
256 }
257
258 // Enable the usage bits the consumer requested
259 usage |= mCore->mConsumerUsageBits;
260
Dan Stoza9f3053d2014-03-06 15:14:33 -0800261 int found;
262 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
263 &found, &returnFlags);
264 if (status != NO_ERROR) {
265 return status;
266 }
Dan Stoza289ade12014-02-28 11:17:17 -0800267
268 // This should not happen
269 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
270 BQ_LOGE("dequeueBuffer: no available buffer slots");
271 return -EBUSY;
272 }
273
274 *outSlot = found;
275 ATRACE_BUFFER_INDEX(found);
276
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 attachedByConsumer = mSlots[found].mAttachedByConsumer;
278
Dan Stoza289ade12014-02-28 11:17:17 -0800279 const bool useDefaultSize = !width && !height;
280 if (useDefaultSize) {
281 width = mCore->mDefaultWidth;
282 height = mCore->mDefaultHeight;
283 }
284
285 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
286
287 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
288 if ((buffer == NULL) ||
289 (static_cast<uint32_t>(buffer->width) != width) ||
290 (static_cast<uint32_t>(buffer->height) != height) ||
291 (static_cast<uint32_t>(buffer->format) != format) ||
292 ((static_cast<uint32_t>(buffer->usage) & usage) != usage))
293 {
294 mSlots[found].mAcquireCalled = false;
295 mSlots[found].mGraphicBuffer = NULL;
296 mSlots[found].mRequestBufferCalled = false;
297 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
298 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
299 mSlots[found].mFence = Fence::NO_FENCE;
300
301 returnFlags |= BUFFER_NEEDS_REALLOCATION;
302 }
303
304 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
305 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
306 "slot=%d w=%d h=%d format=%u",
307 found, buffer->width, buffer->height, buffer->format);
308 }
309
310 eglDisplay = mSlots[found].mEglDisplay;
311 eglFence = mSlots[found].mEglFence;
312 *outFence = mSlots[found].mFence;
313 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
314 mSlots[found].mFence = Fence::NO_FENCE;
315 } // Autolock scope
316
317 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
318 status_t error;
319 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
320 width, height, format, usage, &error));
321 if (graphicBuffer == NULL) {
322 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
323 return error;
324 }
325
326 { // Autolock scope
327 Mutex::Autolock lock(mCore->mMutex);
328
329 if (mCore->mIsAbandoned) {
330 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
331 return NO_INIT;
332 }
333
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 mSlots[*outSlot].mFrameNumber = UINT32_MAX;
Dan Stoza289ade12014-02-28 11:17:17 -0800335 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
336 } // Autolock scope
337 }
338
Dan Stoza9f3053d2014-03-06 15:14:33 -0800339 if (attachedByConsumer) {
340 returnFlags |= BUFFER_NEEDS_REALLOCATION;
341 }
342
Dan Stoza289ade12014-02-28 11:17:17 -0800343 if (eglFence != EGL_NO_SYNC_KHR) {
344 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
345 1000000000);
346 // If something goes wrong, log the error, but return the buffer without
347 // synchronizing access to it. It's too late at this point to abort the
348 // dequeue operation.
349 if (result == EGL_FALSE) {
350 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
351 eglGetError());
352 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
353 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
354 }
355 eglDestroySyncKHR(eglDisplay, eglFence);
356 }
357
358 BQ_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outSlot,
359 mSlots[*outSlot].mFrameNumber,
360 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
361
362 return returnFlags;
363}
364
Dan Stoza9f3053d2014-03-06 15:14:33 -0800365status_t BufferQueueProducer::detachBuffer(int slot) {
366 ATRACE_CALL();
367 ATRACE_BUFFER_INDEX(slot);
368 BQ_LOGV("detachBuffer(P): slot %d", slot);
369 Mutex::Autolock lock(mCore->mMutex);
370
371 if (mCore->mIsAbandoned) {
372 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
373 return NO_INIT;
374 }
375
376 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
377 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
378 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
379 return BAD_VALUE;
380 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
381 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
382 "(state = %d)", slot, mSlots[slot].mBufferState);
383 return BAD_VALUE;
384 } else if (!mSlots[slot].mRequestBufferCalled) {
385 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
386 slot);
387 return BAD_VALUE;
388 }
389
390 mCore->freeBufferLocked(slot);
391 mCore->mDequeueCondition.broadcast();
392
393 return NO_ERROR;
394}
395
396status_t BufferQueueProducer::attachBuffer(int* outSlot,
397 const sp<android::GraphicBuffer>& buffer) {
398 ATRACE_CALL();
399
400 if (outSlot == NULL) {
401 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
402 return BAD_VALUE;
403 } else if (buffer == NULL) {
404 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
405 return BAD_VALUE;
406 }
407
408 Mutex::Autolock lock(mCore->mMutex);
409
410 status_t returnFlags = NO_ERROR;
411 int found;
412 // TODO: Should we provide an async flag to attachBuffer? It seems
413 // unlikely that buffers which we are attaching to a BufferQueue will
414 // be asynchronous (droppable), but it may not be impossible.
415 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
416 &found, &returnFlags);
417 if (status != NO_ERROR) {
418 return status;
419 }
420
421 // This should not happen
422 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
423 BQ_LOGE("attachBuffer(P): no available buffer slots");
424 return -EBUSY;
425 }
426
427 *outSlot = found;
428 ATRACE_BUFFER_INDEX(*outSlot);
429 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
430 *outSlot, returnFlags);
431
432 mSlots[*outSlot].mGraphicBuffer = buffer;
433 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
434 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
435 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700436 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800437
438 return returnFlags;
439}
440
Dan Stoza289ade12014-02-28 11:17:17 -0800441status_t BufferQueueProducer::queueBuffer(int slot,
442 const QueueBufferInput &input, QueueBufferOutput *output) {
443 ATRACE_CALL();
444 ATRACE_BUFFER_INDEX(slot);
445
446 int64_t timestamp;
447 bool isAutoTimestamp;
448 Rect crop;
449 int scalingMode;
450 uint32_t transform;
451 bool async;
452 sp<Fence> fence;
453 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode, &transform,
454 &async, &fence);
455
456 if (fence == NULL) {
457 BQ_LOGE("queueBuffer: fence is NULL");
458 return BAD_VALUE;
459 }
460
461 switch (scalingMode) {
462 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
463 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
464 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
465 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
466 break;
467 default:
468 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800469 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800470 }
471
472 sp<IConsumerListener> listener;
473 { // Autolock scope
474 Mutex::Autolock lock(mCore->mMutex);
475
476 if (mCore->mIsAbandoned) {
477 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
478 return NO_INIT;
479 }
480
481 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
482 if (async && mCore->mOverrideMaxBufferCount) {
483 // FIXME: Some drivers are manually setting the buffer count
484 // (which they shouldn't), so we do this extra test here to
485 // handle that case. This is TEMPORARY until we get this fixed.
486 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
487 BQ_LOGE("queueBuffer: async mode is invalid with "
488 "buffer count override");
489 return BAD_VALUE;
490 }
491 }
492
493 if (slot < 0 || slot >= maxBufferCount) {
494 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
495 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800496 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800497 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
498 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
499 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800500 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800501 } else if (!mSlots[slot].mRequestBufferCalled) {
502 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
503 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800504 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800505 }
506
507 BQ_LOGV("queueBuffer: slot=%d/%llu time=%llu crop=[%d,%d,%d,%d] "
508 "transform=%#x scale=%s",
509 slot, mCore->mFrameCounter + 1, timestamp,
510 crop.left, crop.top, crop.right, crop.bottom,
511 transform, BufferItem::scalingModeName(scalingMode));
512
513 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
514 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
515 Rect croppedRect;
516 crop.intersect(bufferRect, &croppedRect);
517 if (croppedRect != crop) {
518 BQ_LOGE("queueBuffer: crop rect is not contained within the "
519 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800520 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800521 }
522
523 mSlots[slot].mFence = fence;
524 mSlots[slot].mBufferState = BufferSlot::QUEUED;
525 ++mCore->mFrameCounter;
526 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
527
528 BufferItem item;
529 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
530 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
531 item.mCrop = crop;
532 item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
533 item.mTransformToDisplayInverse =
534 bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
535 item.mScalingMode = scalingMode;
536 item.mTimestamp = timestamp;
537 item.mIsAutoTimestamp = isAutoTimestamp;
538 item.mFrameNumber = mCore->mFrameCounter;
539 item.mSlot = slot;
540 item.mFence = fence;
541 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
542
543 if (mCore->mQueue.empty()) {
544 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
545 // and simply queue this buffer
546 mCore->mQueue.push_back(item);
547 listener = mCore->mConsumerListener;
548 } else {
549 // When the queue is not empty, we need to look at the front buffer
550 // state to see if we need to replace it
551 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
552 if (front->mIsDroppable) {
553 // If the front queued buffer is still being tracked, we first
554 // mark it as freed
555 if (mCore->stillTracking(front)) {
556 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
557 // Reset the frame number of the freed buffer so that it is
558 // the first in line to be dequeued again
559 mSlots[front->mSlot].mFrameNumber = 0;
560 }
561 // Overwrite the droppable buffer with the incoming one
562 *front = item;
563 } else {
564 mCore->mQueue.push_back(item);
565 listener = mCore->mConsumerListener;
566 }
567 }
568
569 mCore->mBufferHasBeenQueued = true;
570 mCore->mDequeueCondition.broadcast();
571
572 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
573 mCore->mTransformHint, mCore->mQueue.size());
574
575 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
576 } // Autolock scope
577
578 // Call back without lock held
579 if (listener != NULL) {
580 listener->onFrameAvailable();
581 }
582
583 return NO_ERROR;
584}
585
586void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
587 ATRACE_CALL();
588 BQ_LOGV("cancelBuffer: slot %d", slot);
589 Mutex::Autolock lock(mCore->mMutex);
590
591 if (mCore->mIsAbandoned) {
592 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
593 return;
594 }
595
Dan Stoza3e96f192014-03-03 10:16:19 -0800596 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800597 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800598 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800599 return;
600 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
601 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
602 "(state = %d)", slot, mSlots[slot].mBufferState);
603 return;
604 } else if (fence == NULL) {
605 BQ_LOGE("cancelBuffer: fence is NULL");
606 return;
607 }
608
609 mSlots[slot].mBufferState = BufferSlot::FREE;
610 mSlots[slot].mFrameNumber = 0;
611 mSlots[slot].mFence = fence;
612 mCore->mDequeueCondition.broadcast();
613}
614
615int BufferQueueProducer::query(int what, int *outValue) {
616 ATRACE_CALL();
617 Mutex::Autolock lock(mCore->mMutex);
618
619 if (outValue == NULL) {
620 BQ_LOGE("query: outValue was NULL");
621 return BAD_VALUE;
622 }
623
624 if (mCore->mIsAbandoned) {
625 BQ_LOGE("query: BufferQueue has been abandoned");
626 return NO_INIT;
627 }
628
629 int value;
630 switch (what) {
631 case NATIVE_WINDOW_WIDTH:
632 value = mCore->mDefaultWidth;
633 break;
634 case NATIVE_WINDOW_HEIGHT:
635 value = mCore->mDefaultHeight;
636 break;
637 case NATIVE_WINDOW_FORMAT:
638 value = mCore->mDefaultBufferFormat;
639 break;
640 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
641 value = mCore->getMinUndequeuedBufferCountLocked(false);
642 break;
643 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
644 value = (mCore->mQueue.size() > 1);
645 break;
646 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
647 value = mCore->mConsumerUsageBits;
648 break;
649 default:
650 return BAD_VALUE;
651 }
652
653 BQ_LOGV("query: %d? %d", what, value);
654 *outValue = value;
655 return NO_ERROR;
656}
657
Dan Stozaf0eaf252014-03-21 13:05:51 -0700658status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800659 int api, bool producerControlledByApp, QueueBufferOutput *output) {
660 ATRACE_CALL();
661 Mutex::Autolock lock(mCore->mMutex);
662 mConsumerName = mCore->mConsumerName;
663 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
664 producerControlledByApp ? "true" : "false");
665
666 // If we disconnect and reconnect quickly, we can be in a state where our
667 // slots are empty but we have many buffers in the queue. This can cause us
668 // to run out of memory if we outrun the consumer. Wait here if it looks
669 // like we have too many buffers queued up.
670 while (true) {
671 if (mCore->mIsAbandoned) {
672 BQ_LOGE("connect(P): BufferQueue has been abandoned");
673 return NO_INIT;
674 }
675
676 if (mCore->mConsumerListener == NULL) {
677 BQ_LOGE("connect(P): BufferQueue has no consumer");
678 return NO_INIT;
679 }
680
681 if (output == NULL) {
682 BQ_LOGE("connect(P): output was NULL");
683 return BAD_VALUE;
684 }
685
686 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
687 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
688 mCore->mConnectedApi, api);
689 return BAD_VALUE;
690 }
691
692 size_t maxBufferCount = mCore->getMaxBufferCountLocked(false);
693 if (mCore->mQueue.size() <= maxBufferCount) {
694 // The queue size seems small enough to proceed
695 // TODO: Make this bound tighter?
696 break;
697 }
698
699 BQ_LOGV("connect(P): queue size is %d, waiting", mCore->mQueue.size());
700 mCore->mDequeueCondition.wait(mCore->mMutex);
701 }
702
703 int status = NO_ERROR;
704 switch (api) {
705 case NATIVE_WINDOW_API_EGL:
706 case NATIVE_WINDOW_API_CPU:
707 case NATIVE_WINDOW_API_MEDIA:
708 case NATIVE_WINDOW_API_CAMERA:
709 mCore->mConnectedApi = api;
710 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
711 mCore->mTransformHint, mCore->mQueue.size());
712
713 // Set up a death notification so that we can disconnect
714 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700715 if (listener != NULL &&
716 listener->asBinder()->remoteBinder() != NULL) {
717 status = listener->asBinder()->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800718 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700719 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800720 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
721 strerror(-status), status);
722 }
723 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700724 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800725 break;
726 default:
727 BQ_LOGE("connect(P): unknown API %d", api);
728 status = BAD_VALUE;
729 break;
730 }
731
732 mCore->mBufferHasBeenQueued = false;
733 mCore->mDequeueBufferCannotBlock =
734 mCore->mConsumerControlledByApp && producerControlledByApp;
735
736 return status;
737}
738
739status_t BufferQueueProducer::disconnect(int api) {
740 ATRACE_CALL();
741 BQ_LOGV("disconnect(P): api %d", api);
742
743 int status = NO_ERROR;
744 sp<IConsumerListener> listener;
745 { // Autolock scope
746 Mutex::Autolock lock(mCore->mMutex);
747
748 if (mCore->mIsAbandoned) {
749 // It's not really an error to disconnect after the surface has
750 // been abandoned; it should just be a no-op.
751 return NO_ERROR;
752 }
753
754 switch (api) {
755 case NATIVE_WINDOW_API_EGL:
756 case NATIVE_WINDOW_API_CPU:
757 case NATIVE_WINDOW_API_MEDIA:
758 case NATIVE_WINDOW_API_CAMERA:
759 if (mCore->mConnectedApi == api) {
760 mCore->freeAllBuffersLocked();
761
762 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700763 if (mCore->mConnectedProducerListener != NULL) {
764 sp<IBinder> token =
765 mCore->mConnectedProducerListener->asBinder();
Dan Stoza289ade12014-02-28 11:17:17 -0800766 // This can fail if we're here because of the death
767 // notification, but we just ignore it
768 token->unlinkToDeath(
769 static_cast<IBinder::DeathRecipient*>(this));
770 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700771 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800772 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800773 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800774 mCore->mDequeueCondition.broadcast();
775 listener = mCore->mConsumerListener;
776 } else {
777 BQ_LOGE("disconnect(P): connected to another API "
778 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800779 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800780 }
781 break;
782 default:
783 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800784 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800785 break;
786 }
787 } // Autolock scope
788
789 // Call back without lock held
790 if (listener != NULL) {
791 listener->onBuffersReleased();
792 }
793
794 return status;
795}
796
Jesse Hall399184a2014-03-03 15:42:54 -0800797status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900798 sp<IConsumerListener> listener;
799 { // Autolock scope
800 Mutex::Autolock _l(mCore->mMutex);
801 mCore->mSidebandStream = stream;
802 listener = mCore->mConsumerListener;
803 } // Autolock scope
804
805 if (listener != NULL) {
806 listener->onSidebandStreamChanged();
807 }
Jesse Hall399184a2014-03-03 15:42:54 -0800808 return NO_ERROR;
809}
810
Dan Stoza289ade12014-02-28 11:17:17 -0800811void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
812 // If we're here, it means that a producer we were connected to died.
813 // We're guaranteed that we are still connected to it because we remove
814 // this callback upon disconnect. It's therefore safe to read mConnectedApi
815 // without synchronization here.
816 int api = mCore->mConnectedApi;
817 disconnect(api);
818}
819
820} // namespace android