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/ISurfaceComposer.h>
29 #include <private/gui/ComposerService.h>
31 #include <utils/Log.h>
32 #include <utils/Trace.h>
34 // Macros for including the BufferQueue name in log messages
35 #define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
36 #define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
37 #define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
38 #define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
39 #define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
41 #define ATRACE_BUFFER_INDEX(index) \
42 if (ATRACE_ENABLED()) { \
43 char ___traceBuf[1024]; \
44 snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), \
46 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
51 // Get an ID that's unique within this process.
52 static int32_t createProcessUniqueId() {
53 static volatile int32_t globalCounter = 0;
54 return android_atomic_inc(&globalCounter);
57 static const char* scalingModeName(int scalingMode) {
58 switch (scalingMode) {
59 case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
60 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
61 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
62 default: return "Unknown";
66 BufferQueue::BufferQueue(bool allowSynchronousMode,
67 const sp<IGraphicBufferAlloc>& allocator) :
70 mMaxAcquiredBufferCount(1),
71 mDefaultMaxBufferCount(2),
72 mOverrideMaxBufferCount(0),
73 mSynchronousMode(false),
74 mAllowSynchronousMode(allowSynchronousMode),
75 mConnectedApi(NO_CONNECTED_API),
78 mBufferHasBeenQueued(false),
79 mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
80 mConsumerUsageBits(0),
83 // Choose a name using the PID and a process-unique ID.
84 mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
86 ST_LOGV("BufferQueue");
87 if (allocator == NULL) {
88 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
89 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
90 if (mGraphicBufferAlloc == 0) {
91 ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
94 mGraphicBufferAlloc = allocator;
98 BufferQueue::~BufferQueue() {
99 ST_LOGV("~BufferQueue");
102 status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
103 if (count < 2 || count > NUM_BUFFER_SLOTS)
106 mDefaultMaxBufferCount = count;
107 mDequeueCondition.broadcast();
112 bool BufferQueue::isSynchronousMode() const {
113 Mutex::Autolock lock(mMutex);
114 return mSynchronousMode;
117 void BufferQueue::setConsumerName(const String8& name) {
118 Mutex::Autolock lock(mMutex);
119 mConsumerName = name;
122 status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
123 Mutex::Autolock lock(mMutex);
124 mDefaultBufferFormat = defaultFormat;
128 status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
129 Mutex::Autolock lock(mMutex);
130 mConsumerUsageBits = usage;
134 status_t BufferQueue::setTransformHint(uint32_t hint) {
135 ST_LOGV("setTransformHint: %02x", hint);
136 Mutex::Autolock lock(mMutex);
137 mTransformHint = hint;
141 status_t BufferQueue::setBufferCount(int bufferCount) {
142 ST_LOGV("setBufferCount: count=%d", bufferCount);
144 sp<ConsumerListener> listener;
146 Mutex::Autolock lock(mMutex);
149 ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
152 if (bufferCount > NUM_BUFFER_SLOTS) {
153 ST_LOGE("setBufferCount: bufferCount too large (max %d)",
158 // Error out if the user has dequeued buffers
159 int maxBufferCount = getMaxBufferCountLocked();
160 for (int i=0 ; i<maxBufferCount; i++) {
161 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
162 ST_LOGE("setBufferCount: client owns some buffers");
167 const int minBufferSlots = getMinMaxBufferCountLocked();
168 if (bufferCount == 0) {
169 mOverrideMaxBufferCount = 0;
170 mDequeueCondition.broadcast();
174 if (bufferCount < minBufferSlots) {
175 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
176 "minimum (%d)", bufferCount, minBufferSlots);
180 // here we're guaranteed that the client doesn't have dequeued buffers
181 // and will release all of its buffer references. We don't clear the
182 // queue, however, so currently queued buffers still get displayed.
183 freeAllBuffersLocked();
184 mOverrideMaxBufferCount = bufferCount;
185 mDequeueCondition.broadcast();
186 listener = mConsumerListener;
189 if (listener != NULL) {
190 listener->onBuffersReleased();
196 int BufferQueue::query(int what, int* outValue)
199 Mutex::Autolock lock(mMutex);
202 ST_LOGE("query: BufferQueue has been abandoned!");
208 case NATIVE_WINDOW_WIDTH:
209 value = mDefaultWidth;
211 case NATIVE_WINDOW_HEIGHT:
212 value = mDefaultHeight;
214 case NATIVE_WINDOW_FORMAT:
215 value = mDefaultBufferFormat;
217 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
218 value = getMinUndequeuedBufferCountLocked();
220 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
221 value = (mQueue.size() >= 2);
230 status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
232 ST_LOGV("requestBuffer: slot=%d", slot);
233 Mutex::Autolock lock(mMutex);
235 ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
238 int maxBufferCount = getMaxBufferCountLocked();
239 if (slot < 0 || maxBufferCount <= slot) {
240 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
241 maxBufferCount, slot);
243 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
244 // XXX: I vaguely recall there was some reason this can be valid, but
245 // for the life of me I can't recall under what circumstances that's
247 ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
248 slot, mSlots[slot].mBufferState);
251 mSlots[slot].mRequestBufferCalled = true;
252 *buf = mSlots[slot].mGraphicBuffer;
256 status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence,
257 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
259 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
261 if ((w && !h) || (!w && h)) {
262 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
266 status_t returnFlags(OK);
267 EGLDisplay dpy = EGL_NO_DISPLAY;
268 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
270 { // Scope for the lock
271 Mutex::Autolock lock(mMutex);
274 format = mDefaultBufferFormat;
276 // turn on usage bits the consumer requested
277 usage |= mConsumerUsageBits;
280 int dequeuedCount = 0;
281 bool tryAgain = true;
284 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
288 const int maxBufferCount = getMaxBufferCountLocked();
290 // Free up any buffers that are in slots beyond the max buffer
292 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
293 assert(mSlots[i].mBufferState == BufferSlot::FREE);
294 if (mSlots[i].mGraphicBuffer != NULL) {
296 returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
300 // look for a free buffer to give to the client
301 found = INVALID_BUFFER_SLOT;
303 for (int i = 0; i < maxBufferCount; i++) {
304 const int state = mSlots[i].mBufferState;
305 if (state == BufferSlot::DEQUEUED) {
309 if (state == BufferSlot::FREE) {
310 /* We return the oldest of the free buffers to avoid
311 * stalling the producer if possible. This is because
312 * the consumer may still have pending reads of the
316 mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
322 // clients are not allowed to dequeue more than one buffer
323 // if they didn't set a buffer count.
324 if (!mOverrideMaxBufferCount && dequeuedCount) {
325 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
326 "setting the buffer count");
330 // See whether a buffer has been queued since the last
331 // setBufferCount so we know whether to perform the min undequeued
332 // buffers check below.
333 if (mBufferHasBeenQueued) {
334 // make sure the client is not trying to dequeue more buffers
336 const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
337 const int minUndequeuedCount = getMinUndequeuedBufferCountLocked();
338 if (newUndequeuedCount < minUndequeuedCount) {
339 ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
340 "exceeded (dequeued=%d undequeudCount=%d)",
341 minUndequeuedCount, dequeuedCount,
347 // If no buffer is found, wait for a buffer to be released or for
348 // the max buffer count to change.
349 tryAgain = found == INVALID_BUFFER_SLOT;
351 mDequeueCondition.wait(mMutex);
356 if (found == INVALID_BUFFER_SLOT) {
357 // This should not happen.
358 ST_LOGE("dequeueBuffer: no available buffer slots");
362 const int buf = found;
365 ATRACE_BUFFER_INDEX(buf);
367 const bool useDefaultSize = !w && !h;
368 if (useDefaultSize) {
369 // use the default size
374 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
376 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
377 if ((buffer == NULL) ||
378 (uint32_t(buffer->width) != w) ||
379 (uint32_t(buffer->height) != h) ||
380 (uint32_t(buffer->format) != format) ||
381 ((uint32_t(buffer->usage) & usage) != usage))
383 mSlots[buf].mAcquireCalled = false;
384 mSlots[buf].mGraphicBuffer = NULL;
385 mSlots[buf].mRequestBufferCalled = false;
386 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
387 mSlots[buf].mFence = Fence::NO_FENCE;
388 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
390 returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
393 dpy = mSlots[buf].mEglDisplay;
394 eglFence = mSlots[buf].mEglFence;
395 *outFence = mSlots[buf].mFence;
396 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
397 mSlots[buf].mFence = Fence::NO_FENCE;
400 if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
402 sp<GraphicBuffer> graphicBuffer(
403 mGraphicBufferAlloc->createGraphicBuffer(
404 w, h, format, usage, &error));
405 if (graphicBuffer == 0) {
406 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
411 { // Scope for the lock
412 Mutex::Autolock lock(mMutex);
415 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
419 mSlots[*outBuf].mFrameNumber = ~0;
420 mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
424 if (eglFence != EGL_NO_SYNC_KHR) {
425 EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
426 // If something goes wrong, log the error, but return the buffer without
427 // synchronizing access to it. It's too late at this point to abort the
428 // dequeue operation.
429 if (result == EGL_FALSE) {
430 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
431 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
432 ST_LOGE("dequeueBuffer: timeout waiting for fence");
434 eglDestroySyncKHR(dpy, eglFence);
437 ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
438 mSlots[*outBuf].mFrameNumber,
439 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
444 status_t BufferQueue::setSynchronousMode(bool enabled) {
446 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
447 Mutex::Autolock lock(mMutex);
450 ST_LOGE("setSynchronousMode: BufferQueue has been abandoned!");
455 if (!mAllowSynchronousMode && enabled)
459 // going to asynchronous mode, drain the queue
460 err = drainQueueLocked();
465 if (mSynchronousMode != enabled) {
466 // - if we're going to asynchronous mode, the queue is guaranteed to be
468 // - if the client set the number of buffers, we're guaranteed that
469 // we have at least 3 (because we don't allow less)
470 mSynchronousMode = enabled;
471 mDequeueCondition.broadcast();
476 status_t BufferQueue::queueBuffer(int buf,
477 const QueueBufferInput& input, QueueBufferOutput* output) {
479 ATRACE_BUFFER_INDEX(buf);
487 input.deflate(×tamp, &crop, &scalingMode, &transform, &fence);
490 ST_LOGE("queueBuffer: fence is NULL");
494 switch (scalingMode) {
495 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
496 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
497 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
498 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
501 ST_LOGE("unknown scaling mode: %d", scalingMode);
505 sp<ConsumerListener> listener;
507 { // scope for the lock
508 Mutex::Autolock lock(mMutex);
511 ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
514 int maxBufferCount = getMaxBufferCountLocked();
515 if (buf < 0 || buf >= maxBufferCount) {
516 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
517 maxBufferCount, buf);
519 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
520 ST_LOGE("queueBuffer: slot %d is not owned by the client "
521 "(state=%d)", buf, mSlots[buf].mBufferState);
523 } else if (!mSlots[buf].mRequestBufferCalled) {
524 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
529 ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
531 buf, mFrameCounter + 1, timestamp,
532 crop.left, crop.top, crop.right, crop.bottom,
533 transform, scalingModeName(scalingMode));
535 const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
536 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
538 crop.intersect(bufferRect, &croppedCrop);
539 if (croppedCrop != crop) {
540 ST_LOGE("queueBuffer: crop rect is not contained within the "
541 "buffer in slot %d", buf);
545 mSlots[buf].mFence = fence;
546 mSlots[buf].mBufferState = BufferSlot::QUEUED;
548 mSlots[buf].mFrameNumber = mFrameCounter;
551 item.mAcquireCalled = mSlots[buf].mAcquireCalled;
552 item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
554 item.mTransform = transform;
555 item.mScalingMode = scalingMode;
556 item.mTimestamp = timestamp;
557 item.mFrameNumber = mFrameCounter;
561 if (mSynchronousMode) {
562 // In synchronous mode we queue all buffers in a FIFO.
563 mQueue.push_back(item);
565 // Synchronous mode always signals that an additional frame should
567 listener = mConsumerListener;
569 // In asynchronous mode we only keep the most recent buffer.
570 if (mQueue.empty()) {
571 mQueue.push_back(item);
573 // Asynchronous mode only signals that a frame should be
574 // consumed if no previous frame was pending. If a frame were
575 // pending then the consumer would have already been notified.
576 listener = mConsumerListener;
578 Fifo::iterator front(mQueue.begin());
579 // buffer slot currently queued is marked free if still tracked
580 if (stillTracking(front)) {
581 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
583 // and we record the new buffer index in the queued list
588 mBufferHasBeenQueued = true;
589 mDequeueCondition.broadcast();
591 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
594 ATRACE_INT(mConsumerName.string(), mQueue.size());
595 } // scope for the lock
597 // call back without lock held
599 listener->onFrameAvailable();
604 void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
606 ST_LOGV("cancelBuffer: slot=%d", buf);
607 Mutex::Autolock lock(mMutex);
610 ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
614 int maxBufferCount = getMaxBufferCountLocked();
615 if (buf < 0 || buf >= maxBufferCount) {
616 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
617 maxBufferCount, buf);
619 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
620 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
621 buf, mSlots[buf].mBufferState);
623 } else if (fence == NULL) {
624 ST_LOGE("cancelBuffer: fence is NULL");
627 mSlots[buf].mBufferState = BufferSlot::FREE;
628 mSlots[buf].mFrameNumber = 0;
629 mSlots[buf].mFence = fence;
630 mDequeueCondition.broadcast();
633 status_t BufferQueue::connect(int api, QueueBufferOutput* output) {
635 ST_LOGV("connect: api=%d", api);
636 Mutex::Autolock lock(mMutex);
639 ST_LOGE("connect: BufferQueue has been abandoned!");
643 if (mConsumerListener == NULL) {
644 ST_LOGE("connect: BufferQueue has no consumer!");
650 case NATIVE_WINDOW_API_EGL:
651 case NATIVE_WINDOW_API_CPU:
652 case NATIVE_WINDOW_API_MEDIA:
653 case NATIVE_WINDOW_API_CAMERA:
654 if (mConnectedApi != NO_CONNECTED_API) {
655 ST_LOGE("connect: already connected (cur=%d, req=%d)",
660 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
669 mBufferHasBeenQueued = false;
674 status_t BufferQueue::disconnect(int api) {
676 ST_LOGV("disconnect: api=%d", api);
679 sp<ConsumerListener> listener;
681 { // Scope for the lock
682 Mutex::Autolock lock(mMutex);
685 // it is not really an error to disconnect after the surface
686 // has been abandoned, it should just be a no-op.
691 case NATIVE_WINDOW_API_EGL:
692 case NATIVE_WINDOW_API_CPU:
693 case NATIVE_WINDOW_API_MEDIA:
694 case NATIVE_WINDOW_API_CAMERA:
695 if (mConnectedApi == api) {
696 drainQueueAndFreeBuffersLocked();
697 mConnectedApi = NO_CONNECTED_API;
698 mDequeueCondition.broadcast();
699 listener = mConsumerListener;
701 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
707 ST_LOGE("disconnect: unknown API %d", api);
713 if (listener != NULL) {
714 listener->onBuffersReleased();
720 void BufferQueue::dump(String8& result) const {
721 BufferQueue::dump(result, "");
724 void BufferQueue::dump(String8& result, const char* prefix) const {
725 Mutex::Autolock _l(mMutex);
729 Fifo::const_iterator i(mQueue.begin());
730 while (i != mQueue.end()) {
731 fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
732 "xform=0x%02x, time=%#llx, scale=%s\n",
733 i->mBuf, i->mGraphicBuffer.get(),
734 i->mCrop.left, i->mCrop.top, i->mCrop.right,
735 i->mCrop.bottom, i->mTransform, i->mTimestamp,
736 scalingModeName(i->mScalingMode)
742 int maxBufferCount = getMaxBufferCountLocked();
745 "%s-BufferQueue maxBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
746 "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
747 prefix, maxBufferCount, mSynchronousMode, mDefaultWidth,
748 mDefaultHeight, mDefaultBufferFormat, mTransformHint,
749 fifoSize, fifo.string());
752 const char * operator()(int state) const {
754 case BufferSlot::DEQUEUED: return "DEQUEUED";
755 case BufferSlot::QUEUED: return "QUEUED";
756 case BufferSlot::FREE: return "FREE";
757 case BufferSlot::ACQUIRED: return "ACQUIRED";
758 default: return "Unknown";
763 for (int i=0 ; i<maxBufferCount ; i++) {
764 const BufferSlot& slot(mSlots[i]);
766 "%s%s[%02d:%p] state=%-8s",
767 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i,
768 slot.mGraphicBuffer.get(),
769 stateName(slot.mBufferState)
772 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
775 ", %p [%4ux%4u:%4u,%3X]",
776 buf->handle, buf->width, buf->height, buf->stride,
783 void BufferQueue::freeBufferLocked(int slot) {
784 ST_LOGV("freeBufferLocked: slot=%d", slot);
785 mSlots[slot].mGraphicBuffer = 0;
786 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
787 mSlots[slot].mNeedsCleanupOnRelease = true;
789 mSlots[slot].mBufferState = BufferSlot::FREE;
790 mSlots[slot].mFrameNumber = 0;
791 mSlots[slot].mAcquireCalled = false;
793 // destroy fence as BufferQueue now takes ownership
794 if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
795 eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
796 mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
798 mSlots[slot].mFence = Fence::NO_FENCE;
801 void BufferQueue::freeAllBuffersLocked() {
802 ALOGD_IF(!mQueue.isEmpty(),
803 "freeAllBuffersLocked called with non-empty mQueue");
804 mBufferHasBeenQueued = false;
805 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
810 status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t presentWhen) {
812 Mutex::Autolock _l(mMutex);
814 // Check that the consumer doesn't currently have the maximum number of
815 // buffers acquired. We allow the max buffer count to be exceeded by one
816 // buffer, so that the consumer can successfully set up the newly acquired
817 // buffer before releasing the old one.
818 int numAcquiredBuffers = 0;
819 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
820 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
821 numAcquiredBuffers++;
824 if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
825 ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
826 numAcquiredBuffers, mMaxAcquiredBufferCount);
827 return INVALID_OPERATION;
830 // check if queue is empty
831 // In asynchronous mode the list is guaranteed to be one buffer
832 // deep, while in synchronous mode we use the oldest buffer.
833 if (mQueue.empty()) {
834 return NO_BUFFER_AVAILABLE;
837 Fifo::iterator front(mQueue.begin());
838 int buf = front->mBuf;
840 // Compare the buffer's desired presentation time to the predicted
841 // actual display time.
843 // The "presentWhen" argument indicates when the buffer is expected
844 // to be presented on-screen. If the buffer's desired-present time
845 // is earlier (less) than presentWhen, meaning it'll be displayed
846 // on time or possibly late, we acquire and return it. If we don't want
847 // to display it until after the presentWhen time, we return PRESENT_LATER
848 // without acquiring it.
850 // To be safe, we don't refuse to acquire the buffer if presentWhen is
851 // more than one second in the future beyond the desired present time
852 // (i.e. we'd be holding the buffer for a really long time).
853 const int MAX_FUTURE_NSEC = 1000000000ULL;
854 nsecs_t desiredPresent = front->mTimestamp;
855 if (presentWhen != 0 && desiredPresent > presentWhen &&
856 desiredPresent - presentWhen < MAX_FUTURE_NSEC)
858 ALOGV("pts defer: des=%lld when=%lld (%lld) now=%lld",
859 desiredPresent, presentWhen, desiredPresent - presentWhen,
860 systemTime(CLOCK_MONOTONIC));
861 return PRESENT_LATER;
863 if (presentWhen != 0) {
864 ALOGV("pts accept: %p[%d] sig=%lld des=%lld when=%lld (%lld)",
865 mSlots, buf, mSlots[buf].mFence->getSignalTime(),
866 desiredPresent, presentWhen, desiredPresent - presentWhen);
870 ATRACE_BUFFER_INDEX(buf);
872 ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
873 front->mBuf, front->mFrameNumber,
874 front->mGraphicBuffer->handle);
875 // if front buffer still being tracked update slot state
876 if (stillTracking(front)) {
877 mSlots[buf].mAcquireCalled = true;
878 mSlots[buf].mNeedsCleanupOnRelease = false;
879 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
880 mSlots[buf].mFence = Fence::NO_FENCE;
883 // If the buffer has previously been acquired by the consumer, set
884 // mGraphicBuffer to NULL to avoid unnecessarily remapping this
885 // buffer on the consumer side.
886 if (buffer->mAcquireCalled) {
887 buffer->mGraphicBuffer = NULL;
891 mDequeueCondition.broadcast();
893 ATRACE_INT(mConsumerName.string(), mQueue.size());
898 status_t BufferQueue::releaseBuffer(
899 int buf, uint64_t frameNumber, EGLDisplay display,
900 EGLSyncKHR eglFence, const sp<Fence>& fence) {
902 ATRACE_BUFFER_INDEX(buf);
904 Mutex::Autolock _l(mMutex);
906 if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
910 // Check if this buffer slot is on the queue
911 bool slotQueued = false;
912 Fifo::iterator front(mQueue.begin());
913 while (front != mQueue.end() && !slotQueued) {
914 if (front->mBuf == buf)
919 // If the frame number has changed because buffer has been reallocated,
920 // we can ignore this releaseBuffer for the old buffer.
921 if (frameNumber != mSlots[buf].mFrameNumber) {
922 // This should only occur if new buffer is still in the queue
923 ALOGE_IF(!slotQueued,
924 "received old buffer(#%lld) after new buffer(#%lld) on same "
925 "slot #%d already acquired", frameNumber,
926 mSlots[buf].mFrameNumber, buf);
927 return STALE_BUFFER_SLOT;
929 // this should never happen
931 "received new buffer(#%lld) on slot #%d that has not yet been "
932 "acquired", frameNumber, buf);
934 // The buffer can now only be released if its in the acquired state
935 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
936 mSlots[buf].mEglDisplay = display;
937 mSlots[buf].mEglFence = eglFence;
938 mSlots[buf].mFence = fence;
939 mSlots[buf].mBufferState = BufferSlot::FREE;
940 } else if (mSlots[buf].mNeedsCleanupOnRelease) {
941 ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
942 mSlots[buf].mNeedsCleanupOnRelease = false;
943 return STALE_BUFFER_SLOT;
945 ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
949 mDequeueCondition.broadcast();
953 status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) {
954 ST_LOGV("consumerConnect");
955 Mutex::Autolock lock(mMutex);
958 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
961 if (consumerListener == NULL) {
962 ST_LOGE("consumerConnect: consumerListener may not be NULL");
966 mConsumerListener = consumerListener;
971 status_t BufferQueue::consumerDisconnect() {
972 ST_LOGV("consumerDisconnect");
973 Mutex::Autolock lock(mMutex);
975 if (mConsumerListener == NULL) {
976 ST_LOGE("consumerDisconnect: No consumer is connected!");
981 mConsumerListener = NULL;
983 freeAllBuffersLocked();
984 mDequeueCondition.broadcast();
988 status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
989 ST_LOGV("getReleasedBuffers");
990 Mutex::Autolock lock(mMutex);
993 ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
998 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
999 if (!mSlots[i].mAcquireCalled) {
1004 // Remove buffers in flight (on the queue) from the mask where acquire has
1005 // been called, as the consumer will not receive the buffer address, so
1006 // it should not free these slots.
1007 Fifo::iterator front(mQueue.begin());
1008 while (front != mQueue.end()) {
1009 if (front->mAcquireCalled)
1010 mask &= ~(1 << front->mBuf);
1016 ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1020 status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
1022 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1024 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1029 Mutex::Autolock lock(mMutex);
1035 status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
1037 Mutex::Autolock lock(mMutex);
1038 return setDefaultMaxBufferCountLocked(bufferCount);
1041 status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1043 Mutex::Autolock lock(mMutex);
1044 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1045 ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1046 maxAcquiredBuffers);
1049 if (mConnectedApi != NO_CONNECTED_API) {
1050 return INVALID_OPERATION;
1052 mMaxAcquiredBufferCount = maxAcquiredBuffers;
1056 status_t BufferQueue::drainQueueLocked() {
1057 while (mSynchronousMode && mQueue.size() > 1) {
1058 mDequeueCondition.wait(mMutex);
1060 ST_LOGE("drainQueueLocked: BufferQueue has been abandoned!");
1063 if (mConnectedApi == NO_CONNECTED_API) {
1064 ST_LOGE("drainQueueLocked: BufferQueue is not connected!");
1071 status_t BufferQueue::drainQueueAndFreeBuffersLocked() {
1072 status_t err = drainQueueLocked();
1073 if (err == NO_ERROR) {
1074 freeAllBuffersLocked();
1079 int BufferQueue::getMinMaxBufferCountLocked() const {
1080 return getMinUndequeuedBufferCountLocked() + 1;
1083 int BufferQueue::getMinUndequeuedBufferCountLocked() const {
1084 return mSynchronousMode ? mMaxAcquiredBufferCount :
1085 mMaxAcquiredBufferCount + 1;
1088 int BufferQueue::getMaxBufferCountLocked() const {
1089 int minMaxBufferCount = getMinMaxBufferCountLocked();
1091 int maxBufferCount = mDefaultMaxBufferCount;
1092 if (maxBufferCount < minMaxBufferCount) {
1093 maxBufferCount = minMaxBufferCount;
1095 if (mOverrideMaxBufferCount != 0) {
1096 assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1097 maxBufferCount = mOverrideMaxBufferCount;
1100 // Any buffers that are dequeued by the producer or sitting in the queue
1101 // waiting to be consumed need to have their slots preserved. Such
1102 // buffers will temporarily keep the max buffer count up until the slots
1103 // no longer need to be preserved.
1104 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1105 BufferSlot::BufferState state = mSlots[i].mBufferState;
1106 if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1107 maxBufferCount = i + 1;
1111 return maxBufferCount;
1114 bool BufferQueue::stillTracking(const BufferItem *item) const {
1115 const BufferSlot &slot = mSlots[item->mBuf];
1117 ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1118 "slot: { slot=%d/%llu, buffer=%p }",
1119 item->mBuf, item->mFrameNumber,
1120 (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1121 item->mBuf, slot.mFrameNumber,
1122 (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1124 // Compare item with its original buffer slot. We can check the slot
1125 // as the buffer would not be moved to a different slot by the producer.
1126 return (slot.mGraphicBuffer != NULL &&
1127 item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1130 BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1131 const wp<BufferQueue::ConsumerListener>& consumerListener):
1132 mConsumerListener(consumerListener) {}
1134 BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1136 void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1137 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1138 if (listener != NULL) {
1139 listener->onFrameAvailable();
1143 void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1144 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1145 if (listener != NULL) {
1146 listener->onBuffersReleased();
1150 }; // namespace android