BufferQueue.cpp revision d142f4b787abae7a5c392ded0dd2741574a0bce2
1/*
2 * Copyright (C) 2012 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
17#define LOG_TAG "BufferQueue"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26
27#include <gui/BufferQueue.h>
28#include <gui/IConsumerListener.h>
29#include <gui/ISurfaceComposer.h>
30#include <private/gui/ComposerService.h>
31
32#include <utils/Log.h>
33#include <utils/Trace.h>
34
35// Macros for including the BufferQueue name in log messages
36#define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
37#define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
38#define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
39#define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
40#define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
41
42#define ATRACE_BUFFER_INDEX(index)                                            \
43    if (ATRACE_ENABLED()) {                                                   \
44        char ___traceBuf[1024];                                               \
45        snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(),         \
46                (index));                                                     \
47        android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf);           \
48    }
49
50namespace android {
51
52// Get an ID that's unique within this process.
53static int32_t createProcessUniqueId() {
54    static volatile int32_t globalCounter = 0;
55    return android_atomic_inc(&globalCounter);
56}
57
58static const char* scalingModeName(int scalingMode) {
59    switch (scalingMode) {
60        case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
61        case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
62        case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
63        default: return "Unknown";
64    }
65}
66
67BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) :
68    mDefaultWidth(1),
69    mDefaultHeight(1),
70    mMaxAcquiredBufferCount(1),
71    mDefaultMaxBufferCount(2),
72    mOverrideMaxBufferCount(0),
73    mConsumerControlledByApp(false),
74    mDequeueBufferCannotBlock(false),
75    mUseAsyncBuffer(true),
76    mConnectedApi(NO_CONNECTED_API),
77    mAbandoned(false),
78    mFrameCounter(0),
79    mBufferHasBeenQueued(false),
80    mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
81    mConsumerUsageBits(0),
82    mTransformHint(0)
83{
84    // Choose a name using the PID and a process-unique ID.
85    mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
86
87    ST_LOGV("BufferQueue");
88    if (allocator == NULL) {
89        sp<ISurfaceComposer> composer(ComposerService::getComposerService());
90        mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
91        if (mGraphicBufferAlloc == 0) {
92            ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
93        }
94    } else {
95        mGraphicBufferAlloc = allocator;
96    }
97}
98
99BufferQueue::~BufferQueue() {
100    ST_LOGV("~BufferQueue");
101}
102
103status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
104    const int minBufferCount = mUseAsyncBuffer ? 2 : 1;
105    if (count < minBufferCount || count > NUM_BUFFER_SLOTS)
106        return BAD_VALUE;
107
108    mDefaultMaxBufferCount = count;
109    mDequeueCondition.broadcast();
110
111    return NO_ERROR;
112}
113
114void BufferQueue::setConsumerName(const String8& name) {
115    Mutex::Autolock lock(mMutex);
116    mConsumerName = name;
117}
118
119status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
120    Mutex::Autolock lock(mMutex);
121    mDefaultBufferFormat = defaultFormat;
122    return NO_ERROR;
123}
124
125status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
126    Mutex::Autolock lock(mMutex);
127    mConsumerUsageBits = usage;
128    return NO_ERROR;
129}
130
131status_t BufferQueue::setTransformHint(uint32_t hint) {
132    ST_LOGV("setTransformHint: %02x", hint);
133    Mutex::Autolock lock(mMutex);
134    mTransformHint = hint;
135    return NO_ERROR;
136}
137
138status_t BufferQueue::setBufferCount(int bufferCount) {
139    ST_LOGV("setBufferCount: count=%d", bufferCount);
140
141    sp<IConsumerListener> listener;
142    {
143        Mutex::Autolock lock(mMutex);
144
145        if (mAbandoned) {
146            ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
147            return NO_INIT;
148        }
149        if (bufferCount > NUM_BUFFER_SLOTS) {
150            ST_LOGE("setBufferCount: bufferCount too large (max %d)",
151                    NUM_BUFFER_SLOTS);
152            return BAD_VALUE;
153        }
154
155        // Error out if the user has dequeued buffers
156        for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) {
157            if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
158                ST_LOGE("setBufferCount: client owns some buffers");
159                return -EINVAL;
160            }
161        }
162
163        if (bufferCount == 0) {
164            mOverrideMaxBufferCount = 0;
165            mDequeueCondition.broadcast();
166            return NO_ERROR;
167        }
168
169        // fine to assume async to false before we're setting the buffer count
170        const int minBufferSlots = getMinMaxBufferCountLocked(false);
171        if (bufferCount < minBufferSlots) {
172            ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
173                    "minimum (%d)", bufferCount, minBufferSlots);
174            return BAD_VALUE;
175        }
176
177        // here we're guaranteed that the client doesn't have dequeued buffers
178        // and will release all of its buffer references.  We don't clear the
179        // queue, however, so currently queued buffers still get displayed.
180        freeAllBuffersLocked();
181        mOverrideMaxBufferCount = bufferCount;
182        mDequeueCondition.broadcast();
183        listener = mConsumerListener;
184    } // scope for lock
185
186    if (listener != NULL) {
187        listener->onBuffersReleased();
188    }
189
190    return NO_ERROR;
191}
192
193int BufferQueue::query(int what, int* outValue)
194{
195    ATRACE_CALL();
196    Mutex::Autolock lock(mMutex);
197
198    if (mAbandoned) {
199        ST_LOGE("query: BufferQueue has been abandoned!");
200        return NO_INIT;
201    }
202
203    int value;
204    switch (what) {
205    case NATIVE_WINDOW_WIDTH:
206        value = mDefaultWidth;
207        break;
208    case NATIVE_WINDOW_HEIGHT:
209        value = mDefaultHeight;
210        break;
211    case NATIVE_WINDOW_FORMAT:
212        value = mDefaultBufferFormat;
213        break;
214    case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
215        value = getMinUndequeuedBufferCount(false);
216        break;
217    case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
218        value = (mQueue.size() >= 2);
219        break;
220    case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
221        value = mConsumerUsageBits;
222        break;
223    default:
224        return BAD_VALUE;
225    }
226    outValue[0] = value;
227    return NO_ERROR;
228}
229
230status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
231    ATRACE_CALL();
232    ST_LOGV("requestBuffer: slot=%d", slot);
233    Mutex::Autolock lock(mMutex);
234    if (mAbandoned) {
235        ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
236        return NO_INIT;
237    }
238    if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
239        ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
240                NUM_BUFFER_SLOTS, slot);
241        return BAD_VALUE;
242    } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
243        ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
244                slot, mSlots[slot].mBufferState);
245        return BAD_VALUE;
246    }
247    mSlots[slot].mRequestBufferCalled = true;
248    *buf = mSlots[slot].mGraphicBuffer;
249    return NO_ERROR;
250}
251
252status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
253        uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
254    ATRACE_CALL();
255    ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
256
257    if ((w && !h) || (!w && h)) {
258        ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
259        return BAD_VALUE;
260    }
261
262    status_t returnFlags(OK);
263    EGLDisplay dpy = EGL_NO_DISPLAY;
264    EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
265
266    { // Scope for the lock
267        Mutex::Autolock lock(mMutex);
268
269        if (format == 0) {
270            format = mDefaultBufferFormat;
271        }
272        // turn on usage bits the consumer requested
273        usage |= mConsumerUsageBits;
274
275        int found = -1;
276        bool tryAgain = true;
277        while (tryAgain) {
278            if (mAbandoned) {
279                ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
280                return NO_INIT;
281            }
282
283            const int maxBufferCount = getMaxBufferCountLocked(async);
284            if (async && mOverrideMaxBufferCount) {
285                // FIXME: some drivers are manually setting the buffer-count (which they
286                // shouldn't), so we do this extra test here to handle that case.
287                // This is TEMPORARY, until we get this fixed.
288                if (mOverrideMaxBufferCount < maxBufferCount) {
289                    ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
290                    return BAD_VALUE;
291                }
292            }
293
294            // Free up any buffers that are in slots beyond the max buffer
295            // count.
296            for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
297                assert(mSlots[i].mBufferState == BufferSlot::FREE);
298                if (mSlots[i].mGraphicBuffer != NULL) {
299                    freeBufferLocked(i);
300                    returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
301                }
302            }
303
304            // look for a free buffer to give to the client
305            found = INVALID_BUFFER_SLOT;
306            int dequeuedCount = 0;
307            int acquiredCount = 0;
308            for (int i = 0; i < maxBufferCount; i++) {
309                const int state = mSlots[i].mBufferState;
310                switch (state) {
311                    case BufferSlot::DEQUEUED:
312                        dequeuedCount++;
313                        break;
314                    case BufferSlot::ACQUIRED:
315                        acquiredCount++;
316                        break;
317                    case BufferSlot::FREE:
318                        /* We return the oldest of the free buffers to avoid
319                         * stalling the producer if possible.  This is because
320                         * the consumer may still have pending reads of the
321                         * buffers in flight.
322                         */
323                        if ((found < 0) ||
324                                mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
325                            found = i;
326                        }
327                        break;
328                }
329            }
330
331            // clients are not allowed to dequeue more than one buffer
332            // if they didn't set a buffer count.
333            if (!mOverrideMaxBufferCount && dequeuedCount) {
334                ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
335                        "setting the buffer count");
336                return -EINVAL;
337            }
338
339            // See whether a buffer has been queued since the last
340            // setBufferCount so we know whether to perform the min undequeued
341            // buffers check below.
342            if (mBufferHasBeenQueued) {
343                // make sure the client is not trying to dequeue more buffers
344                // than allowed.
345                const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
346                const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
347                if (newUndequeuedCount < minUndequeuedCount) {
348                    ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
349                            "exceeded (dequeued=%d undequeudCount=%d)",
350                            minUndequeuedCount, dequeuedCount,
351                            newUndequeuedCount);
352                    return -EBUSY;
353                }
354            }
355
356            // If no buffer is found, wait for a buffer to be released or for
357            // the max buffer count to change.
358            tryAgain = found == INVALID_BUFFER_SLOT;
359            if (tryAgain) {
360                // return an error if we're in "cannot block" mode (producer and consumer
361                // are controlled by the application) -- however, the consumer is allowed
362                // to acquire briefly an extra buffer (which could cause us to have to wait here)
363                // and that's okay because we know the wait will be brief (it happens
364                // if we dequeue a buffer while the consumer has acquired one but not released
365                // the old one yet -- for e.g.: see GLConsumer::updateTexImage()).
366                if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) {
367                    ST_LOGE("dequeueBuffer: would block! returning an error instead.");
368                    return WOULD_BLOCK;
369                }
370                mDequeueCondition.wait(mMutex);
371            }
372        }
373
374
375        if (found == INVALID_BUFFER_SLOT) {
376            // This should not happen.
377            ST_LOGE("dequeueBuffer: no available buffer slots");
378            return -EBUSY;
379        }
380
381        const int buf = found;
382        *outBuf = found;
383
384        ATRACE_BUFFER_INDEX(buf);
385
386        const bool useDefaultSize = !w && !h;
387        if (useDefaultSize) {
388            // use the default size
389            w = mDefaultWidth;
390            h = mDefaultHeight;
391        }
392
393        mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
394
395        const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
396        if ((buffer == NULL) ||
397            (uint32_t(buffer->width)  != w) ||
398            (uint32_t(buffer->height) != h) ||
399            (uint32_t(buffer->format) != format) ||
400            ((uint32_t(buffer->usage) & usage) != usage))
401        {
402            mSlots[buf].mAcquireCalled = false;
403            mSlots[buf].mGraphicBuffer = NULL;
404            mSlots[buf].mRequestBufferCalled = false;
405            mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
406            mSlots[buf].mFence = Fence::NO_FENCE;
407            mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
408
409            returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
410        }
411
412
413        if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) {
414            ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. "
415                    "buf=%d, w=%d, h=%d, format=%d",
416                    buf, buffer->width, buffer->height, buffer->format);
417        }
418
419        dpy = mSlots[buf].mEglDisplay;
420        eglFence = mSlots[buf].mEglFence;
421        *outFence = mSlots[buf].mFence;
422        mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
423        mSlots[buf].mFence = Fence::NO_FENCE;
424    }  // end lock scope
425
426    if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
427        status_t error;
428        sp<GraphicBuffer> graphicBuffer(
429                mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error));
430        if (graphicBuffer == 0) {
431            ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
432            return error;
433        }
434
435        { // Scope for the lock
436            Mutex::Autolock lock(mMutex);
437
438            if (mAbandoned) {
439                ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
440                return NO_INIT;
441            }
442
443            mSlots[*outBuf].mFrameNumber = ~0;
444            mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
445        }
446    }
447
448    if (eglFence != EGL_NO_SYNC_KHR) {
449        EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
450        // If something goes wrong, log the error, but return the buffer without
451        // synchronizing access to it.  It's too late at this point to abort the
452        // dequeue operation.
453        if (result == EGL_FALSE) {
454            ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
455        } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
456            ST_LOGE("dequeueBuffer: timeout waiting for fence");
457        }
458        eglDestroySyncKHR(dpy, eglFence);
459    }
460
461    ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
462            mSlots[*outBuf].mFrameNumber,
463            mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
464
465    return returnFlags;
466}
467
468status_t BufferQueue::queueBuffer(int buf,
469        const QueueBufferInput& input, QueueBufferOutput* output) {
470    ATRACE_CALL();
471    ATRACE_BUFFER_INDEX(buf);
472
473    Rect crop;
474    uint32_t transform;
475    int scalingMode;
476    int64_t timestamp;
477    bool isAutoTimestamp;
478    bool async;
479    sp<Fence> fence;
480
481    input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode, &transform,
482            &async, &fence);
483
484    if (fence == NULL) {
485        ST_LOGE("queueBuffer: fence is NULL");
486        return BAD_VALUE;
487    }
488
489    switch (scalingMode) {
490        case NATIVE_WINDOW_SCALING_MODE_FREEZE:
491        case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
492        case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
493        case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
494            break;
495        default:
496            ST_LOGE("unknown scaling mode: %d", scalingMode);
497            return -EINVAL;
498    }
499
500    sp<IConsumerListener> listener;
501
502    { // scope for the lock
503        Mutex::Autolock lock(mMutex);
504
505        if (mAbandoned) {
506            ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
507            return NO_INIT;
508        }
509
510        const int maxBufferCount = getMaxBufferCountLocked(async);
511        if (async && mOverrideMaxBufferCount) {
512            // FIXME: some drivers are manually setting the buffer-count (which they
513            // shouldn't), so we do this extra test here to handle that case.
514            // This is TEMPORARY, until we get this fixed.
515            if (mOverrideMaxBufferCount < maxBufferCount) {
516                ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
517                return BAD_VALUE;
518            }
519        }
520        if (buf < 0 || buf >= maxBufferCount) {
521            ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
522                    maxBufferCount, buf);
523            return -EINVAL;
524        } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
525            ST_LOGE("queueBuffer: slot %d is not owned by the client "
526                    "(state=%d)", buf, mSlots[buf].mBufferState);
527            return -EINVAL;
528        } else if (!mSlots[buf].mRequestBufferCalled) {
529            ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
530                    "buffer", buf);
531            return -EINVAL;
532        }
533
534        ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
535                "tr=%#x scale=%s",
536                buf, mFrameCounter + 1, timestamp,
537                crop.left, crop.top, crop.right, crop.bottom,
538                transform, scalingModeName(scalingMode));
539
540        const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
541        Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
542        Rect croppedCrop;
543        crop.intersect(bufferRect, &croppedCrop);
544        if (croppedCrop != crop) {
545            ST_LOGE("queueBuffer: crop rect is not contained within the "
546                    "buffer in slot %d", buf);
547            return -EINVAL;
548        }
549
550        mSlots[buf].mFence = fence;
551        mSlots[buf].mBufferState = BufferSlot::QUEUED;
552        mFrameCounter++;
553        mSlots[buf].mFrameNumber = mFrameCounter;
554
555        BufferItem item;
556        item.mAcquireCalled = mSlots[buf].mAcquireCalled;
557        item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
558        item.mCrop = crop;
559        item.mTransform = transform;
560        item.mScalingMode = scalingMode;
561        item.mTimestamp = timestamp;
562        item.mIsAutoTimestamp = isAutoTimestamp;
563        item.mFrameNumber = mFrameCounter;
564        item.mBuf = buf;
565        item.mFence = fence;
566        item.mIsDroppable = mDequeueBufferCannotBlock || async;
567
568        if (mQueue.empty()) {
569            // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
570            // simply queue this buffer.
571            mQueue.push_back(item);
572            listener = mConsumerListener;
573        } else {
574            // when the queue is not empty, we need to look at the front buffer
575            // state and see if we need to replace it.
576            Fifo::iterator front(mQueue.begin());
577            if (front->mIsDroppable) {
578                // buffer slot currently queued is marked free if still tracked
579                if (stillTracking(front)) {
580                    mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
581                    // reset the frame number of the freed buffer so that it is the first in
582                    // line to be dequeued again.
583                    mSlots[front->mBuf].mFrameNumber = 0;
584                }
585                // and we record the new buffer in the queued list
586                *front = item;
587            } else {
588                mQueue.push_back(item);
589                listener = mConsumerListener;
590            }
591        }
592
593        mBufferHasBeenQueued = true;
594        mDequeueCondition.broadcast();
595
596        output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
597                mQueue.size());
598
599        ATRACE_INT(mConsumerName.string(), mQueue.size());
600    } // scope for the lock
601
602    // call back without lock held
603    if (listener != 0) {
604        listener->onFrameAvailable();
605    }
606    return NO_ERROR;
607}
608
609void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
610    ATRACE_CALL();
611    ST_LOGV("cancelBuffer: slot=%d", buf);
612    Mutex::Autolock lock(mMutex);
613
614    if (mAbandoned) {
615        ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
616        return;
617    }
618
619    if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
620        ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
621                NUM_BUFFER_SLOTS, buf);
622        return;
623    } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
624        ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
625                buf, mSlots[buf].mBufferState);
626        return;
627    } else if (fence == NULL) {
628        ST_LOGE("cancelBuffer: fence is NULL");
629        return;
630    }
631    mSlots[buf].mBufferState = BufferSlot::FREE;
632    mSlots[buf].mFrameNumber = 0;
633    mSlots[buf].mFence = fence;
634    mDequeueCondition.broadcast();
635}
636
637status_t BufferQueue::connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
638    ATRACE_CALL();
639    ST_LOGV("connect: api=%d producerControlledByApp=%s", api,
640            producerControlledByApp ? "true" : "false");
641    Mutex::Autolock lock(mMutex);
642
643    if (mAbandoned) {
644        ST_LOGE("connect: BufferQueue has been abandoned!");
645        return NO_INIT;
646    }
647
648    if (mConsumerListener == NULL) {
649        ST_LOGE("connect: BufferQueue has no consumer!");
650        return NO_INIT;
651    }
652
653    int err = NO_ERROR;
654    switch (api) {
655        case NATIVE_WINDOW_API_EGL:
656        case NATIVE_WINDOW_API_CPU:
657        case NATIVE_WINDOW_API_MEDIA:
658        case NATIVE_WINDOW_API_CAMERA:
659            if (mConnectedApi != NO_CONNECTED_API) {
660                ST_LOGE("connect: already connected (cur=%d, req=%d)",
661                        mConnectedApi, api);
662                err = -EINVAL;
663            } else {
664                mConnectedApi = api;
665                output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
666                        mQueue.size());
667            }
668            break;
669        default:
670            err = -EINVAL;
671            break;
672    }
673
674    mBufferHasBeenQueued = false;
675    mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
676
677    return err;
678}
679
680status_t BufferQueue::disconnect(int api) {
681    ATRACE_CALL();
682    ST_LOGV("disconnect: api=%d", api);
683
684    int err = NO_ERROR;
685    sp<IConsumerListener> listener;
686
687    { // Scope for the lock
688        Mutex::Autolock lock(mMutex);
689
690        if (mAbandoned) {
691            // it is not really an error to disconnect after the surface
692            // has been abandoned, it should just be a no-op.
693            return NO_ERROR;
694        }
695
696        switch (api) {
697            case NATIVE_WINDOW_API_EGL:
698            case NATIVE_WINDOW_API_CPU:
699            case NATIVE_WINDOW_API_MEDIA:
700            case NATIVE_WINDOW_API_CAMERA:
701                if (mConnectedApi == api) {
702                    freeAllBuffersLocked();
703                    mConnectedApi = NO_CONNECTED_API;
704                    mDequeueCondition.broadcast();
705                    listener = mConsumerListener;
706                } else {
707                    ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
708                            mConnectedApi, api);
709                    err = -EINVAL;
710                }
711                break;
712            default:
713                ST_LOGE("disconnect: unknown API %d", api);
714                err = -EINVAL;
715                break;
716        }
717    }
718
719    if (listener != NULL) {
720        listener->onBuffersReleased();
721    }
722
723    return err;
724}
725
726void BufferQueue::dump(String8& result, const char* prefix) const {
727    Mutex::Autolock _l(mMutex);
728
729    String8 fifo;
730    int fifoSize = 0;
731    Fifo::const_iterator i(mQueue.begin());
732    while (i != mQueue.end()) {
733        fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
734                "xform=0x%02x, time=%#llx, scale=%s\n",
735                i->mBuf, i->mGraphicBuffer.get(),
736                i->mCrop.left, i->mCrop.top, i->mCrop.right,
737                i->mCrop.bottom, i->mTransform, i->mTimestamp,
738                scalingModeName(i->mScalingMode)
739                );
740        i++;
741        fifoSize++;
742    }
743
744
745    result.appendFormat(
746            "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
747            "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
748            prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
749            mDefaultHeight, mDefaultBufferFormat, mTransformHint,
750            fifoSize, fifo.string());
751
752    struct {
753        const char * operator()(int state) const {
754            switch (state) {
755                case BufferSlot::DEQUEUED: return "DEQUEUED";
756                case BufferSlot::QUEUED: return "QUEUED";
757                case BufferSlot::FREE: return "FREE";
758                case BufferSlot::ACQUIRED: return "ACQUIRED";
759                default: return "Unknown";
760            }
761        }
762    } stateName;
763
764    // just trim the free buffers to not spam the dump
765    int maxBufferCount = 0;
766    for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
767        const BufferSlot& slot(mSlots[i]);
768        if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
769            maxBufferCount = i+1;
770            break;
771        }
772    }
773
774    for (int i=0 ; i<maxBufferCount ; i++) {
775        const BufferSlot& slot(mSlots[i]);
776        const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
777        result.appendFormat(
778            "%s%s[%02d:%p] state=%-8s",
779                prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
780                stateName(slot.mBufferState)
781        );
782
783        if (buf != NULL) {
784            result.appendFormat(
785                    ", %p [%4ux%4u:%4u,%3X]",
786                    buf->handle, buf->width, buf->height, buf->stride,
787                    buf->format);
788        }
789        result.append("\n");
790    }
791}
792
793void BufferQueue::freeBufferLocked(int slot) {
794    ST_LOGV("freeBufferLocked: slot=%d", slot);
795    mSlots[slot].mGraphicBuffer = 0;
796    if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
797        mSlots[slot].mNeedsCleanupOnRelease = true;
798    }
799    mSlots[slot].mBufferState = BufferSlot::FREE;
800    mSlots[slot].mFrameNumber = 0;
801    mSlots[slot].mAcquireCalled = false;
802
803    // destroy fence as BufferQueue now takes ownership
804    if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
805        eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
806        mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
807    }
808    mSlots[slot].mFence = Fence::NO_FENCE;
809}
810
811void BufferQueue::freeAllBuffersLocked() {
812    mBufferHasBeenQueued = false;
813    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
814        freeBufferLocked(i);
815    }
816}
817
818status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) {
819    ATRACE_CALL();
820    Mutex::Autolock _l(mMutex);
821
822    // Check that the consumer doesn't currently have the maximum number of
823    // buffers acquired.  We allow the max buffer count to be exceeded by one
824    // buffer, so that the consumer can successfully set up the newly acquired
825    // buffer before releasing the old one.
826    int numAcquiredBuffers = 0;
827    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
828        if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
829            numAcquiredBuffers++;
830        }
831    }
832    if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
833        ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
834                numAcquiredBuffers, mMaxAcquiredBufferCount);
835        return INVALID_OPERATION;
836    }
837
838    // check if queue is empty
839    // In asynchronous mode the list is guaranteed to be one buffer
840    // deep, while in synchronous mode we use the oldest buffer.
841    if (mQueue.empty()) {
842        return NO_BUFFER_AVAILABLE;
843    }
844
845    Fifo::iterator front(mQueue.begin());
846
847    // If expectedPresent is specified, we may not want to return a buffer yet.
848    // If it's specified and there's more than one buffer queued, we may
849    // want to drop a buffer.
850    if (expectedPresent != 0) {
851        const int MAX_REASONABLE_NSEC = 1000000000ULL;  // 1 second
852
853        // The "expectedPresent" argument indicates when the buffer is expected
854        // to be presented on-screen.  If the buffer's desired-present time
855        // is earlier (less) than expectedPresent, meaning it'll be displayed
856        // on time or possibly late if we show it ASAP, we acquire and return
857        // it.  If we don't want to display it until after the expectedPresent
858        // time, we return PRESENT_LATER without acquiring it.
859        //
860        // To be safe, we don't defer acquisition if expectedPresent is
861        // more than one second in the future beyond the desired present time
862        // (i.e. we'd be holding the buffer for a long time).
863        //
864        // NOTE: code assumes monotonic time values from the system clock are
865        // positive.
866
867        // Start by checking to see if we can drop frames.  We skip this check
868        // if the timestamps are being auto-generated by Surface -- if the
869        // app isn't generating timestamps explicitly, they probably don't
870        // want frames to be discarded based on them.
871        while (mQueue.size() > 1 && !mQueue[0].mIsAutoTimestamp) {
872            // If entry[1] is timely, drop entry[0] (and repeat).  We apply
873            // an additional criteria here: we only drop the earlier buffer if
874            // our desiredPresent falls within +/- 1 second of the expected
875            // present.  Otherwise, bogus desiredPresent times (e.g. 0 or
876            // a small relative timestamp), which normally mean "ignore the
877            // timestamp and acquire immediately", would cause us to drop
878            // frames.
879            //
880            // We may want to add an additional criteria: don't drop the
881            // earlier buffer if entry[1]'s fence hasn't signaled yet.
882            //
883            // (Vector front is [0], back is [size()-1])
884            const BufferItem& bi(mQueue[1]);
885            nsecs_t desiredPresent = bi.mTimestamp;
886            if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
887                    desiredPresent > expectedPresent) {
888                // This buffer is set to display in the near future, or
889                // desiredPresent is garbage.  Either way we don't want to
890                // drop the previous buffer just to get this on screen sooner.
891                ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld",
892                        desiredPresent, expectedPresent, desiredPresent - expectedPresent,
893                        systemTime(CLOCK_MONOTONIC));
894                break;
895            }
896            ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d",
897                    desiredPresent, expectedPresent, mQueue.size());
898            if (stillTracking(front)) {
899                // front buffer is still in mSlots, so mark the slot as free
900                mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
901            }
902            mQueue.erase(front);
903            front = mQueue.begin();
904        }
905
906        // See if the front buffer is due.
907        nsecs_t desiredPresent = front->mTimestamp;
908        if (desiredPresent > expectedPresent &&
909                desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
910            ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld",
911                    desiredPresent, expectedPresent, desiredPresent - expectedPresent,
912                    systemTime(CLOCK_MONOTONIC));
913            return PRESENT_LATER;
914        }
915
916        ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld",
917                desiredPresent, expectedPresent, desiredPresent - expectedPresent,
918                systemTime(CLOCK_MONOTONIC));
919    }
920
921    int buf = front->mBuf;
922    *buffer = *front;
923    ATRACE_BUFFER_INDEX(buf);
924
925    ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
926            front->mBuf, front->mFrameNumber,
927            front->mGraphicBuffer->handle);
928    // if front buffer still being tracked update slot state
929    if (stillTracking(front)) {
930        mSlots[buf].mAcquireCalled = true;
931        mSlots[buf].mNeedsCleanupOnRelease = false;
932        mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
933        mSlots[buf].mFence = Fence::NO_FENCE;
934    }
935
936    // If the buffer has previously been acquired by the consumer, set
937    // mGraphicBuffer to NULL to avoid unnecessarily remapping this
938    // buffer on the consumer side.
939    if (buffer->mAcquireCalled) {
940        buffer->mGraphicBuffer = NULL;
941    }
942
943    mQueue.erase(front);
944    mDequeueCondition.broadcast();
945
946    ATRACE_INT(mConsumerName.string(), mQueue.size());
947
948    return NO_ERROR;
949}
950
951status_t BufferQueue::releaseBuffer(
952        int buf, uint64_t frameNumber, EGLDisplay display,
953        EGLSyncKHR eglFence, const sp<Fence>& fence) {
954    ATRACE_CALL();
955    ATRACE_BUFFER_INDEX(buf);
956
957    if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
958        return BAD_VALUE;
959    }
960
961    Mutex::Autolock _l(mMutex);
962
963    // If the frame number has changed because buffer has been reallocated,
964    // we can ignore this releaseBuffer for the old buffer.
965    if (frameNumber != mSlots[buf].mFrameNumber) {
966        return STALE_BUFFER_SLOT;
967    }
968
969
970    // Internal state consistency checks:
971    // Make sure this buffers hasn't been queued while we were owning it (acquired)
972    Fifo::iterator front(mQueue.begin());
973    Fifo::const_iterator const end(mQueue.end());
974    while (front != end) {
975        if (front->mBuf == buf) {
976            LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
977                    "acquired", mConsumerName.string(), frameNumber, buf);
978            break; // never reached
979        }
980        front++;
981    }
982
983    // The buffer can now only be released if its in the acquired state
984    if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
985        mSlots[buf].mEglDisplay = display;
986        mSlots[buf].mEglFence = eglFence;
987        mSlots[buf].mFence = fence;
988        mSlots[buf].mBufferState = BufferSlot::FREE;
989    } else if (mSlots[buf].mNeedsCleanupOnRelease) {
990        ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
991        mSlots[buf].mNeedsCleanupOnRelease = false;
992        return STALE_BUFFER_SLOT;
993    } else {
994        ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
995        return -EINVAL;
996    }
997
998    mDequeueCondition.broadcast();
999    return NO_ERROR;
1000}
1001
1002status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener,
1003        bool controlledByApp) {
1004    ST_LOGV("consumerConnect controlledByApp=%s",
1005            controlledByApp ? "true" : "false");
1006    Mutex::Autolock lock(mMutex);
1007
1008    if (mAbandoned) {
1009        ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
1010        return NO_INIT;
1011    }
1012    if (consumerListener == NULL) {
1013        ST_LOGE("consumerConnect: consumerListener may not be NULL");
1014        return BAD_VALUE;
1015    }
1016
1017    mConsumerListener = consumerListener;
1018    mConsumerControlledByApp = controlledByApp;
1019
1020    return NO_ERROR;
1021}
1022
1023status_t BufferQueue::consumerDisconnect() {
1024    ST_LOGV("consumerDisconnect");
1025    Mutex::Autolock lock(mMutex);
1026
1027    if (mConsumerListener == NULL) {
1028        ST_LOGE("consumerDisconnect: No consumer is connected!");
1029        return -EINVAL;
1030    }
1031
1032    mAbandoned = true;
1033    mConsumerListener = NULL;
1034    mQueue.clear();
1035    freeAllBuffersLocked();
1036    mDequeueCondition.broadcast();
1037    return NO_ERROR;
1038}
1039
1040status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
1041    ST_LOGV("getReleasedBuffers");
1042    Mutex::Autolock lock(mMutex);
1043
1044    if (mAbandoned) {
1045        ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
1046        return NO_INIT;
1047    }
1048
1049    uint32_t mask = 0;
1050    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1051        if (!mSlots[i].mAcquireCalled) {
1052            mask |= 1 << i;
1053        }
1054    }
1055
1056    // Remove buffers in flight (on the queue) from the mask where acquire has
1057    // been called, as the consumer will not receive the buffer address, so
1058    // it should not free these slots.
1059    Fifo::iterator front(mQueue.begin());
1060    while (front != mQueue.end()) {
1061        if (front->mAcquireCalled)
1062            mask &= ~(1 << front->mBuf);
1063        front++;
1064    }
1065
1066    *slotMask = mask;
1067
1068    ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1069    return NO_ERROR;
1070}
1071
1072status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
1073    ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1074    if (!w || !h) {
1075        ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1076                w, h);
1077        return BAD_VALUE;
1078    }
1079
1080    Mutex::Autolock lock(mMutex);
1081    mDefaultWidth = w;
1082    mDefaultHeight = h;
1083    return NO_ERROR;
1084}
1085
1086status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
1087    ATRACE_CALL();
1088    Mutex::Autolock lock(mMutex);
1089    return setDefaultMaxBufferCountLocked(bufferCount);
1090}
1091
1092status_t BufferQueue::disableAsyncBuffer() {
1093    ATRACE_CALL();
1094    Mutex::Autolock lock(mMutex);
1095    if (mConsumerListener != NULL) {
1096        ST_LOGE("disableAsyncBuffer: consumer already connected!");
1097        return INVALID_OPERATION;
1098    }
1099    mUseAsyncBuffer = false;
1100    return NO_ERROR;
1101}
1102
1103status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1104    ATRACE_CALL();
1105    Mutex::Autolock lock(mMutex);
1106    if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1107        ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1108                maxAcquiredBuffers);
1109        return BAD_VALUE;
1110    }
1111    if (mConnectedApi != NO_CONNECTED_API) {
1112        return INVALID_OPERATION;
1113    }
1114    mMaxAcquiredBufferCount = maxAcquiredBuffers;
1115    return NO_ERROR;
1116}
1117
1118int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
1119    // if dequeueBuffer is allowed to error out, we don't have to
1120    // add an extra buffer.
1121    if (!mUseAsyncBuffer)
1122        return mMaxAcquiredBufferCount;
1123
1124    // we're in async mode, or we want to prevent the app to
1125    // deadlock itself, we throw-in an extra buffer to guarantee it.
1126    if (mDequeueBufferCannotBlock || async)
1127        return mMaxAcquiredBufferCount+1;
1128
1129    return mMaxAcquiredBufferCount;
1130}
1131
1132int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
1133    return getMinUndequeuedBufferCount(async) + 1;
1134}
1135
1136int BufferQueue::getMaxBufferCountLocked(bool async) const {
1137    int minMaxBufferCount = getMinMaxBufferCountLocked(async);
1138
1139    int maxBufferCount = mDefaultMaxBufferCount;
1140    if (maxBufferCount < minMaxBufferCount) {
1141        maxBufferCount = minMaxBufferCount;
1142    }
1143    if (mOverrideMaxBufferCount != 0) {
1144        assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1145        maxBufferCount = mOverrideMaxBufferCount;
1146    }
1147
1148    // Any buffers that are dequeued by the producer or sitting in the queue
1149    // waiting to be consumed need to have their slots preserved.  Such
1150    // buffers will temporarily keep the max buffer count up until the slots
1151    // no longer need to be preserved.
1152    for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1153        BufferSlot::BufferState state = mSlots[i].mBufferState;
1154        if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1155            maxBufferCount = i + 1;
1156        }
1157    }
1158
1159    return maxBufferCount;
1160}
1161
1162bool BufferQueue::stillTracking(const BufferItem *item) const {
1163    const BufferSlot &slot = mSlots[item->mBuf];
1164
1165    ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1166            "slot: { slot=%d/%llu, buffer=%p }",
1167            item->mBuf, item->mFrameNumber,
1168            (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1169            item->mBuf, slot.mFrameNumber,
1170            (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1171
1172    // Compare item with its original buffer slot.  We can check the slot
1173    // as the buffer would not be moved to a different slot by the producer.
1174    return (slot.mGraphicBuffer != NULL &&
1175            item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1176}
1177
1178BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1179        const wp<ConsumerListener>& consumerListener):
1180        mConsumerListener(consumerListener) {}
1181
1182BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1183
1184void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1185    sp<ConsumerListener> listener(mConsumerListener.promote());
1186    if (listener != NULL) {
1187        listener->onFrameAvailable();
1188    }
1189}
1190
1191void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1192    sp<ConsumerListener> listener(mConsumerListener.promote());
1193    if (listener != NULL) {
1194        listener->onBuffersReleased();
1195    }
1196}
1197
1198}; // namespace android
1199