2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "BufferQueue"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
21 #define GL_GLEXT_PROTOTYPES
22 #define EGL_EGLEXT_PROTOTYPES
25 #include <EGL/eglext.h>
27 #include <gui/BufferQueue.h>
28 #include <gui/IConsumerListener.h>
29 #include <gui/ISurfaceComposer.h>
30 #include <private/gui/ComposerService.h>
32 #include <utils/Log.h>
33 #include <utils/Trace.h>
34 #include <utils/CallStack.h>
36 // Macros for including the BufferQueue name in log messages
37 #define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
38 #define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
39 #define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
40 #define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
41 #define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
43 #define ATRACE_BUFFER_INDEX(index) \
44 if (ATRACE_ENABLED()) { \
45 char ___traceBuf[1024]; \
46 snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), \
48 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
53 // Get an ID that's unique within this process.
54 static int32_t createProcessUniqueId() {
55 static volatile int32_t globalCounter = 0;
56 return android_atomic_inc(&globalCounter);
59 static const char* scalingModeName(int scalingMode) {
60 switch (scalingMode) {
61 case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
62 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
63 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
64 default: return "Unknown";
68 BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) :
71 mMaxAcquiredBufferCount(1),
72 mDefaultMaxBufferCount(2),
73 mOverrideMaxBufferCount(0),
74 mConsumerControlledByApp(false),
75 mDequeueBufferCannotBlock(false),
76 mUseAsyncBuffer(true),
77 mConnectedApi(NO_CONNECTED_API),
80 mBufferHasBeenQueued(false),
81 mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
82 mConsumerUsageBits(0),
85 // Choose a name using the PID and a process-unique ID.
86 mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
88 ST_LOGV("BufferQueue");
89 if (allocator == NULL) {
90 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
91 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
92 if (mGraphicBufferAlloc == 0) {
93 ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
96 mGraphicBufferAlloc = allocator;
100 BufferQueue::~BufferQueue() {
101 ST_LOGV("~BufferQueue");
104 status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
105 const int minBufferCount = mUseAsyncBuffer ? 2 : 1;
106 if (count < minBufferCount || count > NUM_BUFFER_SLOTS)
109 mDefaultMaxBufferCount = count;
110 mDequeueCondition.broadcast();
115 void BufferQueue::setConsumerName(const String8& name) {
116 Mutex::Autolock lock(mMutex);
117 mConsumerName = name;
120 status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
121 Mutex::Autolock lock(mMutex);
122 mDefaultBufferFormat = defaultFormat;
126 status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
127 Mutex::Autolock lock(mMutex);
128 mConsumerUsageBits = usage;
132 status_t BufferQueue::setTransformHint(uint32_t hint) {
133 ST_LOGV("setTransformHint: %02x", hint);
134 Mutex::Autolock lock(mMutex);
135 mTransformHint = hint;
139 status_t BufferQueue::setBufferCount(int bufferCount) {
140 ST_LOGV("setBufferCount: count=%d", bufferCount);
142 sp<IConsumerListener> listener;
144 Mutex::Autolock lock(mMutex);
147 ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
150 if (bufferCount > NUM_BUFFER_SLOTS) {
151 ST_LOGE("setBufferCount: bufferCount too large (max %d)",
156 // Error out if the user has dequeued buffers
157 for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) {
158 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
159 ST_LOGE("setBufferCount: client owns some buffers");
164 if (bufferCount == 0) {
165 mOverrideMaxBufferCount = 0;
166 mDequeueCondition.broadcast();
170 // fine to assume async to false before we're setting the buffer count
171 const int minBufferSlots = getMinMaxBufferCountLocked(false);
172 if (bufferCount < minBufferSlots) {
173 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
174 "minimum (%d)", bufferCount, minBufferSlots);
178 // here we're guaranteed that the client doesn't have dequeued buffers
179 // and will release all of its buffer references. We don't clear the
180 // queue, however, so currently queued buffers still get displayed.
181 freeAllBuffersLocked();
182 mOverrideMaxBufferCount = bufferCount;
183 mDequeueCondition.broadcast();
184 listener = mConsumerListener;
187 if (listener != NULL) {
188 listener->onBuffersReleased();
194 int BufferQueue::query(int what, int* outValue)
197 Mutex::Autolock lock(mMutex);
200 ST_LOGE("query: BufferQueue has been abandoned!");
206 case NATIVE_WINDOW_WIDTH:
207 value = mDefaultWidth;
209 case NATIVE_WINDOW_HEIGHT:
210 value = mDefaultHeight;
212 case NATIVE_WINDOW_FORMAT:
213 value = mDefaultBufferFormat;
215 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
216 value = getMinUndequeuedBufferCount(false);
218 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
219 value = (mQueue.size() >= 2);
221 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
222 value = mConsumerUsageBits;
231 status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
233 ST_LOGV("requestBuffer: slot=%d", slot);
234 Mutex::Autolock lock(mMutex);
236 ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
239 if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
240 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
241 NUM_BUFFER_SLOTS, slot);
243 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
244 ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
245 slot, mSlots[slot].mBufferState);
248 mSlots[slot].mRequestBufferCalled = true;
249 *buf = mSlots[slot].mGraphicBuffer;
253 status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
254 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
256 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
258 if ((w && !h) || (!w && h)) {
259 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
263 status_t returnFlags(OK);
264 EGLDisplay dpy = EGL_NO_DISPLAY;
265 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
267 { // Scope for the lock
268 Mutex::Autolock lock(mMutex);
271 format = mDefaultBufferFormat;
273 // turn on usage bits the consumer requested
274 usage |= mConsumerUsageBits;
277 bool tryAgain = true;
280 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
284 const int maxBufferCount = getMaxBufferCountLocked(async);
285 if (async && mOverrideMaxBufferCount) {
286 // FIXME: some drivers are manually setting the buffer-count (which they
287 // shouldn't), so we do this extra test here to handle that case.
288 // This is TEMPORARY, until we get this fixed.
289 if (mOverrideMaxBufferCount < maxBufferCount) {
290 ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
295 // Free up any buffers that are in slots beyond the max buffer
297 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
298 assert(mSlots[i].mBufferState == BufferSlot::FREE);
299 if (mSlots[i].mGraphicBuffer != NULL) {
301 returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
305 // look for a free buffer to give to the client
306 found = INVALID_BUFFER_SLOT;
307 int dequeuedCount = 0;
308 int acquiredCount = 0;
309 for (int i = 0; i < maxBufferCount; i++) {
310 const int state = mSlots[i].mBufferState;
312 case BufferSlot::DEQUEUED:
315 case BufferSlot::ACQUIRED:
318 case BufferSlot::FREE:
319 /* We return the oldest of the free buffers to avoid
320 * stalling the producer if possible. This is because
321 * the consumer may still have pending reads of the
325 mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
332 // clients are not allowed to dequeue more than one buffer
333 // if they didn't set a buffer count.
334 if (!mOverrideMaxBufferCount && dequeuedCount) {
335 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
336 "setting the buffer count");
340 // See whether a buffer has been queued since the last
341 // setBufferCount so we know whether to perform the min undequeued
342 // buffers check below.
343 if (mBufferHasBeenQueued) {
344 // make sure the client is not trying to dequeue more buffers
346 const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
347 const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
348 if (newUndequeuedCount < minUndequeuedCount) {
349 ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
350 "exceeded (dequeued=%d undequeudCount=%d)",
351 minUndequeuedCount, dequeuedCount,
357 // If no buffer is found, wait for a buffer to be released or for
358 // the max buffer count to change.
359 tryAgain = found == INVALID_BUFFER_SLOT;
361 // return an error if we're in "cannot block" mode (producer and consumer
362 // are controlled by the application) -- however, the consumer is allowed
363 // to acquire briefly an extra buffer (which could cause us to have to wait here)
364 // and that's okay because we know the wait will be brief (it happens
365 // if we dequeue a buffer while the consumer has acquired one but not released
366 // the old one yet -- for e.g.: see GLConsumer::updateTexImage()).
367 if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) {
368 ST_LOGE("dequeueBuffer: would block! returning an error instead.");
371 mDequeueCondition.wait(mMutex);
376 if (found == INVALID_BUFFER_SLOT) {
377 // This should not happen.
378 ST_LOGE("dequeueBuffer: no available buffer slots");
382 const int buf = found;
385 ATRACE_BUFFER_INDEX(buf);
387 const bool useDefaultSize = !w && !h;
388 if (useDefaultSize) {
389 // use the default size
394 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
396 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
397 if ((buffer == NULL) ||
398 (uint32_t(buffer->width) != w) ||
399 (uint32_t(buffer->height) != h) ||
400 (uint32_t(buffer->format) != format) ||
401 ((uint32_t(buffer->usage) & usage) != usage))
403 mSlots[buf].mAcquireCalled = false;
404 mSlots[buf].mGraphicBuffer = NULL;
405 mSlots[buf].mRequestBufferCalled = false;
406 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
407 mSlots[buf].mFence = Fence::NO_FENCE;
408 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
410 returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
414 if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) {
415 ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. "
416 "buf=%d, w=%d, h=%d, format=%d",
417 buf, buffer->width, buffer->height, buffer->format);
420 dpy = mSlots[buf].mEglDisplay;
421 eglFence = mSlots[buf].mEglFence;
422 *outFence = mSlots[buf].mFence;
423 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
424 mSlots[buf].mFence = Fence::NO_FENCE;
427 if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
429 sp<GraphicBuffer> graphicBuffer(
430 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error));
431 if (graphicBuffer == 0) {
432 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
436 { // Scope for the lock
437 Mutex::Autolock lock(mMutex);
440 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
444 mSlots[*outBuf].mFrameNumber = ~0;
445 mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
449 if (eglFence != EGL_NO_SYNC_KHR) {
450 EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
451 // If something goes wrong, log the error, but return the buffer without
452 // synchronizing access to it. It's too late at this point to abort the
453 // dequeue operation.
454 if (result == EGL_FALSE) {
455 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
456 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
457 ST_LOGE("dequeueBuffer: timeout waiting for fence");
459 eglDestroySyncKHR(dpy, eglFence);
462 ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
463 mSlots[*outBuf].mFrameNumber,
464 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
469 status_t BufferQueue::queueBuffer(int buf,
470 const QueueBufferInput& input, QueueBufferOutput* output) {
472 ATRACE_BUFFER_INDEX(buf);
481 input.deflate(×tamp, &crop, &scalingMode, &transform, &async, &fence);
484 ST_LOGE("queueBuffer: fence is NULL");
488 switch (scalingMode) {
489 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
490 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
491 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
492 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
495 ST_LOGE("unknown scaling mode: %d", scalingMode);
499 sp<IConsumerListener> listener;
501 { // scope for the lock
502 Mutex::Autolock lock(mMutex);
505 ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
509 const int maxBufferCount = getMaxBufferCountLocked(async);
510 if (async && mOverrideMaxBufferCount) {
511 // FIXME: some drivers are manually setting the buffer-count (which they
512 // shouldn't), so we do this extra test here to handle that case.
513 // This is TEMPORARY, until we get this fixed.
514 if (mOverrideMaxBufferCount < maxBufferCount) {
515 ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
519 if (buf < 0 || buf >= maxBufferCount) {
520 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
521 maxBufferCount, buf);
523 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
524 ST_LOGE("queueBuffer: slot %d is not owned by the client "
525 "(state=%d)", buf, mSlots[buf].mBufferState);
527 } else if (!mSlots[buf].mRequestBufferCalled) {
528 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
533 ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
535 buf, mFrameCounter + 1, timestamp,
536 crop.left, crop.top, crop.right, crop.bottom,
537 transform, scalingModeName(scalingMode));
539 const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
540 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
542 crop.intersect(bufferRect, &croppedCrop);
543 if (croppedCrop != crop) {
544 ST_LOGE("queueBuffer: crop rect is not contained within the "
545 "buffer in slot %d", buf);
549 mSlots[buf].mFence = fence;
550 mSlots[buf].mBufferState = BufferSlot::QUEUED;
552 mSlots[buf].mFrameNumber = mFrameCounter;
555 item.mAcquireCalled = mSlots[buf].mAcquireCalled;
556 item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
558 item.mTransform = transform;
559 item.mScalingMode = scalingMode;
560 item.mTimestamp = timestamp;
561 item.mFrameNumber = mFrameCounter;
564 item.mIsDroppable = mDequeueBufferCannotBlock || async;
566 if (mQueue.empty()) {
567 // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
568 // simply queue this buffer.
569 mQueue.push_back(item);
570 listener = mConsumerListener;
572 // when the queue is not empty, we need to look at the front buffer
573 // state and see if we need to replace it.
574 Fifo::iterator front(mQueue.begin());
575 if (front->mIsDroppable) {
576 // buffer slot currently queued is marked free if still tracked
577 if (stillTracking(front)) {
578 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
579 // reset the frame number of the freed buffer so that it is the first in
580 // line to be dequeued again.
581 mSlots[front->mBuf].mFrameNumber = 0;
583 // and we record the new buffer in the queued list
586 mQueue.push_back(item);
587 listener = mConsumerListener;
591 mBufferHasBeenQueued = true;
592 mDequeueCondition.broadcast();
594 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
597 ATRACE_INT(mConsumerName.string(), mQueue.size());
598 } // scope for the lock
600 // call back without lock held
602 listener->onFrameAvailable();
607 void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
609 ST_LOGV("cancelBuffer: slot=%d", buf);
610 Mutex::Autolock lock(mMutex);
613 ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
617 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
618 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
619 NUM_BUFFER_SLOTS, buf);
621 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
622 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
623 buf, mSlots[buf].mBufferState);
625 } else if (fence == NULL) {
626 ST_LOGE("cancelBuffer: fence is NULL");
629 mSlots[buf].mBufferState = BufferSlot::FREE;
630 mSlots[buf].mFrameNumber = 0;
631 mSlots[buf].mFence = fence;
632 mDequeueCondition.broadcast();
635 status_t BufferQueue::connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
637 ST_LOGV("connect: api=%d", api);
638 Mutex::Autolock lock(mMutex);
641 ST_LOGE("connect: BufferQueue has been abandoned!");
645 if (mConsumerListener == NULL) {
646 ST_LOGE("connect: BufferQueue has no consumer!");
652 case NATIVE_WINDOW_API_EGL:
653 case NATIVE_WINDOW_API_CPU:
654 case NATIVE_WINDOW_API_MEDIA:
655 case NATIVE_WINDOW_API_CAMERA:
656 if (mConnectedApi != NO_CONNECTED_API) {
657 ST_LOGE("connect: already connected (cur=%d, req=%d)",
662 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
671 mBufferHasBeenQueued = false;
672 mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
677 status_t BufferQueue::disconnect(int api) {
679 ST_LOGV("disconnect: api=%d", api);
682 sp<IConsumerListener> listener;
684 { // Scope for the lock
685 Mutex::Autolock lock(mMutex);
688 // it is not really an error to disconnect after the surface
689 // has been abandoned, it should just be a no-op.
694 case NATIVE_WINDOW_API_EGL:
695 case NATIVE_WINDOW_API_CPU:
696 case NATIVE_WINDOW_API_MEDIA:
697 case NATIVE_WINDOW_API_CAMERA:
698 if (mConnectedApi == api) {
699 freeAllBuffersLocked();
700 mConnectedApi = NO_CONNECTED_API;
701 mDequeueCondition.broadcast();
702 listener = mConsumerListener;
704 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
710 ST_LOGE("disconnect: unknown API %d", api);
716 if (listener != NULL) {
717 listener->onBuffersReleased();
723 void BufferQueue::dump(String8& result, const char* prefix) const {
724 Mutex::Autolock _l(mMutex);
728 Fifo::const_iterator i(mQueue.begin());
729 while (i != mQueue.end()) {
730 fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
731 "xform=0x%02x, time=%#llx, scale=%s\n",
732 i->mBuf, i->mGraphicBuffer.get(),
733 i->mCrop.left, i->mCrop.top, i->mCrop.right,
734 i->mCrop.bottom, i->mTransform, i->mTimestamp,
735 scalingModeName(i->mScalingMode)
743 "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
744 "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
745 prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
746 mDefaultHeight, mDefaultBufferFormat, mTransformHint,
747 fifoSize, fifo.string());
750 const char * operator()(int state) const {
752 case BufferSlot::DEQUEUED: return "DEQUEUED";
753 case BufferSlot::QUEUED: return "QUEUED";
754 case BufferSlot::FREE: return "FREE";
755 case BufferSlot::ACQUIRED: return "ACQUIRED";
756 default: return "Unknown";
761 // just trim the free buffers to not spam the dump
762 int maxBufferCount = 0;
763 for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
764 const BufferSlot& slot(mSlots[i]);
765 if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
766 maxBufferCount = i+1;
771 for (int i=0 ; i<maxBufferCount ; i++) {
772 const BufferSlot& slot(mSlots[i]);
773 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
775 "%s%s[%02d:%p] state=%-8s",
776 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
777 stateName(slot.mBufferState)
782 ", %p [%4ux%4u:%4u,%3X]",
783 buf->handle, buf->width, buf->height, buf->stride,
790 void BufferQueue::freeBufferLocked(int slot) {
791 ST_LOGV("freeBufferLocked: slot=%d", slot);
792 mSlots[slot].mGraphicBuffer = 0;
793 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
794 mSlots[slot].mNeedsCleanupOnRelease = true;
796 mSlots[slot].mBufferState = BufferSlot::FREE;
797 mSlots[slot].mFrameNumber = 0;
798 mSlots[slot].mAcquireCalled = false;
800 // destroy fence as BufferQueue now takes ownership
801 if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
802 eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
803 mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
805 mSlots[slot].mFence = Fence::NO_FENCE;
808 void BufferQueue::freeAllBuffersLocked() {
809 mBufferHasBeenQueued = false;
810 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
815 status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) {
817 Mutex::Autolock _l(mMutex);
819 // Check that the consumer doesn't currently have the maximum number of
820 // buffers acquired. We allow the max buffer count to be exceeded by one
821 // buffer, so that the consumer can successfully set up the newly acquired
822 // buffer before releasing the old one.
823 int numAcquiredBuffers = 0;
824 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
825 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
826 numAcquiredBuffers++;
829 if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
830 ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
831 numAcquiredBuffers, mMaxAcquiredBufferCount);
832 return INVALID_OPERATION;
835 // check if queue is empty
836 // In asynchronous mode the list is guaranteed to be one buffer
837 // deep, while in synchronous mode we use the oldest buffer.
838 if (mQueue.empty()) {
839 return NO_BUFFER_AVAILABLE;
842 Fifo::iterator front(mQueue.begin());
844 // If expectedPresent is specified, we may not want to return a buffer yet.
845 // If it's specified and there's more than one buffer queued, we may
846 // want to drop a buffer.
847 if (expectedPresent != 0) {
848 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
850 // The "expectedPresent" argument indicates when the buffer is expected
851 // to be presented on-screen. If the buffer's desired-present time
852 // is earlier (less) than expectedPresent, meaning it'll be displayed
853 // on time or possibly late if we show it ASAP, we acquire and return
854 // it. If we don't want to display it until after the expectedPresent
855 // time, we return PRESENT_LATER without acquiring it.
857 // To be safe, we don't defer acquisition if expectedPresent is
858 // more than one second in the future beyond the desired present time
859 // (i.e. we'd be holding the buffer for a long time).
861 // NOTE: code assumes monotonic time values from the system clock are
863 while (false && mQueue.size() > 1) {
864 // If entry[1] is timely, drop entry[0] (and repeat). We apply
865 // an additional criteria here: we only drop the earlier buffer if
866 // our desiredPresent falls within +/- 1 second of the expected
867 // present. Otherwise, bogus desiredPresent times (e.g. 0 or
868 // a small relative timestamp), which normally mean "ignore the
869 // timestamp and acquire immediately", would cause us to drop
872 // We may want to add an additional criteria: don't drop the
873 // earlier buffer if entry[1]'s fence hasn't signaled yet.
875 // (Vector front is [0], back is [size()-1])
876 const BufferItem& bi(mQueue[1]);
877 nsecs_t desiredPresent = bi.mTimestamp;
878 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
879 desiredPresent > expectedPresent) {
880 // This buffer is set to display in the near future, or
881 // desiredPresent is garbage. Either way we don't want to
882 // drop the previous buffer just to get this on screen sooner.
883 ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld",
884 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
885 systemTime(CLOCK_MONOTONIC));
888 ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d",
889 desiredPresent, expectedPresent, mQueue.size());
890 if (stillTracking(front)) {
891 // front buffer is still in mSlots, so mark the slot as free
892 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
895 front = mQueue.begin();
898 // See if the front buffer is due.
899 nsecs_t desiredPresent = front->mTimestamp;
900 if (desiredPresent > expectedPresent &&
901 desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
902 ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld",
903 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
904 systemTime(CLOCK_MONOTONIC));
905 return PRESENT_LATER;
908 ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld",
909 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
910 systemTime(CLOCK_MONOTONIC));
913 int buf = front->mBuf;
915 ATRACE_BUFFER_INDEX(buf);
917 ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
918 front->mBuf, front->mFrameNumber,
919 front->mGraphicBuffer->handle);
920 // if front buffer still being tracked update slot state
921 if (stillTracking(front)) {
922 mSlots[buf].mAcquireCalled = true;
923 mSlots[buf].mNeedsCleanupOnRelease = false;
924 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
925 mSlots[buf].mFence = Fence::NO_FENCE;
928 // If the buffer has previously been acquired by the consumer, set
929 // mGraphicBuffer to NULL to avoid unnecessarily remapping this
930 // buffer on the consumer side.
931 if (buffer->mAcquireCalled) {
932 buffer->mGraphicBuffer = NULL;
936 mDequeueCondition.broadcast();
938 ATRACE_INT(mConsumerName.string(), mQueue.size());
943 status_t BufferQueue::releaseBuffer(
944 int buf, uint64_t frameNumber, EGLDisplay display,
945 EGLSyncKHR eglFence, const sp<Fence>& fence) {
947 ATRACE_BUFFER_INDEX(buf);
949 if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
953 Mutex::Autolock _l(mMutex);
955 // If the frame number has changed because buffer has been reallocated,
956 // we can ignore this releaseBuffer for the old buffer.
957 if (frameNumber != mSlots[buf].mFrameNumber) {
958 return STALE_BUFFER_SLOT;
962 // Internal state consistency checks:
963 // Make sure this buffers hasn't been queued while we were owning it (acquired)
964 Fifo::iterator front(mQueue.begin());
965 Fifo::const_iterator const end(mQueue.end());
966 while (front != end) {
967 if (front->mBuf == buf) {
968 LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
969 "acquired", mConsumerName.string(), frameNumber, buf);
970 break; // never reached
975 // The buffer can now only be released if its in the acquired state
976 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
977 mSlots[buf].mEglDisplay = display;
978 mSlots[buf].mEglFence = eglFence;
979 mSlots[buf].mFence = fence;
980 mSlots[buf].mBufferState = BufferSlot::FREE;
981 } else if (mSlots[buf].mNeedsCleanupOnRelease) {
982 ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
983 mSlots[buf].mNeedsCleanupOnRelease = false;
984 return STALE_BUFFER_SLOT;
986 ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
990 mDequeueCondition.broadcast();
994 status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener,
995 bool controlledByApp) {
996 ST_LOGV("consumerConnect");
997 Mutex::Autolock lock(mMutex);
1000 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
1003 if (consumerListener == NULL) {
1004 ST_LOGE("consumerConnect: consumerListener may not be NULL");
1008 mConsumerListener = consumerListener;
1009 mConsumerControlledByApp = controlledByApp;
1014 status_t BufferQueue::consumerDisconnect() {
1015 ST_LOGV("consumerDisconnect");
1016 Mutex::Autolock lock(mMutex);
1018 if (mConsumerListener == NULL) {
1019 ST_LOGE("consumerDisconnect: No consumer is connected!");
1024 mConsumerListener = NULL;
1026 freeAllBuffersLocked();
1027 mDequeueCondition.broadcast();
1031 status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
1032 ST_LOGV("getReleasedBuffers");
1033 Mutex::Autolock lock(mMutex);
1036 ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
1041 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1042 if (!mSlots[i].mAcquireCalled) {
1047 // Remove buffers in flight (on the queue) from the mask where acquire has
1048 // been called, as the consumer will not receive the buffer address, so
1049 // it should not free these slots.
1050 Fifo::iterator front(mQueue.begin());
1051 while (front != mQueue.end()) {
1052 if (front->mAcquireCalled)
1053 mask &= ~(1 << front->mBuf);
1059 ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1063 status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
1064 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1066 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1071 Mutex::Autolock lock(mMutex);
1077 status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
1079 Mutex::Autolock lock(mMutex);
1080 return setDefaultMaxBufferCountLocked(bufferCount);
1083 status_t BufferQueue::disableAsyncBuffer() {
1085 Mutex::Autolock lock(mMutex);
1086 if (mConsumerListener != NULL) {
1087 ST_LOGE("disableAsyncBuffer: consumer already connected!");
1088 return INVALID_OPERATION;
1090 mUseAsyncBuffer = false;
1094 status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1096 Mutex::Autolock lock(mMutex);
1097 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1098 ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1099 maxAcquiredBuffers);
1102 if (mConnectedApi != NO_CONNECTED_API) {
1103 return INVALID_OPERATION;
1105 mMaxAcquiredBufferCount = maxAcquiredBuffers;
1109 int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
1110 // if dequeueBuffer is allowed to error out, we don't have to
1111 // add an extra buffer.
1112 if (!mUseAsyncBuffer)
1113 return mMaxAcquiredBufferCount;
1115 // we're in async mode, or we want to prevent the app to
1116 // deadlock itself, we throw-in an extra buffer to guarantee it.
1117 if (mDequeueBufferCannotBlock || async)
1118 return mMaxAcquiredBufferCount+1;
1120 return mMaxAcquiredBufferCount;
1123 int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
1124 return getMinUndequeuedBufferCount(async) + 1;
1127 int BufferQueue::getMaxBufferCountLocked(bool async) const {
1128 int minMaxBufferCount = getMinMaxBufferCountLocked(async);
1130 int maxBufferCount = mDefaultMaxBufferCount;
1131 if (maxBufferCount < minMaxBufferCount) {
1132 maxBufferCount = minMaxBufferCount;
1134 if (mOverrideMaxBufferCount != 0) {
1135 assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1136 maxBufferCount = mOverrideMaxBufferCount;
1139 // Any buffers that are dequeued by the producer or sitting in the queue
1140 // waiting to be consumed need to have their slots preserved. Such
1141 // buffers will temporarily keep the max buffer count up until the slots
1142 // no longer need to be preserved.
1143 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1144 BufferSlot::BufferState state = mSlots[i].mBufferState;
1145 if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1146 maxBufferCount = i + 1;
1150 return maxBufferCount;
1153 bool BufferQueue::stillTracking(const BufferItem *item) const {
1154 const BufferSlot &slot = mSlots[item->mBuf];
1156 ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1157 "slot: { slot=%d/%llu, buffer=%p }",
1158 item->mBuf, item->mFrameNumber,
1159 (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1160 item->mBuf, slot.mFrameNumber,
1161 (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1163 // Compare item with its original buffer slot. We can check the slot
1164 // as the buffer would not be moved to a different slot by the producer.
1165 return (slot.mGraphicBuffer != NULL &&
1166 item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1169 BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1170 const wp<ConsumerListener>& consumerListener):
1171 mConsumerListener(consumerListener) {}
1173 BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1175 void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1176 sp<ConsumerListener> listener(mConsumerListener.promote());
1177 if (listener != NULL) {
1178 listener->onFrameAvailable();
1182 void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1183 sp<ConsumerListener> listener(mConsumerListener.promote());
1184 if (listener != NULL) {
1185 listener->onBuffersReleased();
1189 }; // namespace android