BufferQueue.cpp revision 1c8e95cf86f2182986385bc1ee85f13f425f3a3a
16b091c53000c843211c218ce40287a7edca9bc63Daniel Lam/*
26b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Copyright (C) 2012 The Android Open Source Project
36b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
46b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Licensed under the Apache License, Version 2.0 (the "License");
56b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * you may not use this file except in compliance with the License.
66b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * You may obtain a copy of the License at
76b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
86b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *      http://www.apache.org/licenses/LICENSE-2.0
96b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Unless required by applicable law or agreed to in writing, software
116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * distributed under the License is distributed on an "AS IS" BASIS,
126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * See the License for the specific language governing permissions and
146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * limitations under the License.
156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam */
166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define LOG_TAG "BufferQueue"
18eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam//#define LOG_NDEBUG 0
191c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define GL_GLEXT_PROTOTYPES
226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define EGL_EGLEXT_PROTOTYPES
236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <EGL/egl.h>
256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <EGL/eglext.h>
266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <gui/BufferQueue.h>
2890ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/ISurfaceComposer.h>
296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <private/gui/ComposerService.h>
306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/Log.h>
32eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#include <gui/SurfaceTexture.h>
331c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// This compile option causes SurfaceTexture to return the buffer that is currently
366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// attached to the GL texture from dequeueBuffer when no other buffers are
376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// available.  It requires the drivers (Gralloc, GL, OMX IL, and Camera) to do
386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// implicit cross-process synchronization to prevent the buffer from being
396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// written to before the buffer has (a) been detached from the GL texture and
406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// (b) all GL reads from the buffer have completed.
41eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
42eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam// During refactoring, do not support dequeuing the current buffer
43eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#undef ALLOW_DEQUEUE_CURRENT_BUFFER
44eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER    true
476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#else
496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER    false
506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#endif
516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// Macros for including the BufferQueue name in log messages
53eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
54eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
55eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
56eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
57eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam#define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
596b091c53000c843211c218ce40287a7edca9bc63Daniel Lamnamespace android {
606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// Get an ID that's unique within this process.
626b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatic int32_t createProcessUniqueId() {
636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    static volatile int32_t globalCounter = 0;
646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return android_atomic_inc(&globalCounter);
656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
676b091c53000c843211c218ce40287a7edca9bc63Daniel LamBufferQueue::BufferQueue( bool allowSynchronousMode ) :
686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDefaultWidth(1),
696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDefaultHeight(1),
706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mPixelFormat(PIXEL_FORMAT_RGBA_8888),
716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mClientBufferCount(0),
736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mNextTransform(0),
756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSynchronousMode(false),
776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mAllowSynchronousMode(allowSynchronousMode),
786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mConnectedApi(NO_CONNECTED_API),
796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mAbandoned(false),
80eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mFrameCounter(0),
81eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued(false)
826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam{
836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // Choose a name using the PID and a process-unique ID.
84eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("BufferQueue");
876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    sp<ISurfaceComposer> composer(ComposerService::getComposerService());
886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mNextCrop.makeInvalid();
906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
926b091c53000c843211c218ce40287a7edca9bc63Daniel LamBufferQueue::~BufferQueue() {
936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("~BufferQueue");
946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
966b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setBufferCountServerLocked(int bufferCount) {
976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount > NUM_BUFFER_SLOTS)
986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // special-case, nothing to do
1016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount == mBufferCount)
1026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return OK;
1036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mClientBufferCount &&
1056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bufferCount >= mBufferCount) {
1066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // easy, we just have more buffers
1076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mBufferCount = bufferCount;
1086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mServerBufferCount = bufferCount;
1096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mDequeueCondition.signal();
1106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } else {
1116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // we're here because we're either
1126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - reducing the number of available buffers
1136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - or there is a client-buffer-count in effect
1146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // less than 2 buffers is never allowed
1166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (bufferCount < 2)
1176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return BAD_VALUE;
1186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // when there is non client-buffer-count in effect, the client is not
1206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // allowed to dequeue more than one buffer at a time,
1216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // so the next time they dequeue a buffer, we know that they don't
1226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // own one. the actual resizing will happen during the next
1236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // dequeueBuffer.
1246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mServerBufferCount = bufferCount;
1266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
1286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
130eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lambool BufferQueue::isSynchronousMode() const {
131eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
132eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return mSynchronousMode;
133eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
134eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
135eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::setConsumerName(const String8& name) {
136eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
137eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mConsumerName = name;
138eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
139eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
140eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::setFrameAvailableListener(
141eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const sp<FrameAvailableListener>& listener) {
142eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    ST_LOGV("setFrameAvailableListener");
143eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
144eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mFrameAvailableListener = listener;
145eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
146eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
1476b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setBufferCount(int bufferCount) {
1486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setBufferCount: count=%d", bufferCount);
1496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
1506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
1526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
1536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
1546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount > NUM_BUFFER_SLOTS) {
1566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setBufferCount: bufferCount larger than slots available");
1576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
1586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // Error out if the user has dequeued buffers
1616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i=0 ; i<mBufferCount ; i++) {
1626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
1636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("setBufferCount: client owns some buffers");
1646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
1656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
1666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    const int minBufferSlots = mSynchronousMode ?
1696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
1706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount == 0) {
1716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mClientBufferCount = 0;
1726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bufferCount = (mServerBufferCount >= minBufferSlots) ?
1736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mServerBufferCount : minBufferSlots;
1746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return setBufferCountServerLocked(bufferCount);
1756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount < minBufferSlots) {
1786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
1796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                "minimum (%d)", bufferCount, minBufferSlots);
1806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
1816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // here we're guaranteed that the client doesn't have dequeued buffers
1846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // and will release all of its buffer references.
1856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    freeAllBuffersLocked();
1866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mBufferCount = bufferCount;
1876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mClientBufferCount = bufferCount;
188eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
1896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mQueue.clear();
1906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDequeueCondition.signal();
1916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
1926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
194b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lamint BufferQueue::query(int what, int* outValue)
195b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam{
1961c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
197b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    Mutex::Autolock lock(mMutex);
198b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
199b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    if (mAbandoned) {
200b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        ST_LOGE("query: SurfaceTexture has been abandoned!");
201b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        return NO_INIT;
202b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    }
203b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
204b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    int value;
205b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    switch (what) {
206b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_WIDTH:
207b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mDefaultWidth;
208b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
209b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_HEIGHT:
210b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mDefaultHeight;
211b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
212b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_FORMAT:
213b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mPixelFormat;
214b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
215b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
216b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mSynchronousMode ?
217b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam                (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
218b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
219b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    default:
220b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        return BAD_VALUE;
221b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    }
222b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    outValue[0] = value;
223b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    return NO_ERROR;
224b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam}
225b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
2266b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
2271c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
2286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("requestBuffer: slot=%d", slot);
2296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
2306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
2316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
2326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
2336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (slot < 0 || mBufferCount <= slot) {
2356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
2366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount, slot);
2376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
2386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[slot].mRequestBufferCalled = true;
2406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    *buf = mSlots[slot].mGraphicBuffer;
2416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return NO_ERROR;
2426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
2436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2446b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
2456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t format, uint32_t usage) {
2461c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
2476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
2486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if ((w && !h) || (!w && h)) {
2506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
2516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
2526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t returnFlags(OK);
2556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    EGLDisplay dpy = EGL_NO_DISPLAY;
2566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    EGLSyncKHR fence = EGL_NO_SYNC_KHR;
2576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    { // Scope for the lock
2596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Mutex::Autolock lock(mMutex);
2606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int found = -1;
2626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int foundSync = -1;
2636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int dequeuedCount = 0;
2646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bool tryAgain = true;
2656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        while (tryAgain) {
2666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mAbandoned) {
2676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
2686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return NO_INIT;
2696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
2706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // We need to wait for the FIFO to drain if the number of buffer
2726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // needs to change.
2736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
2746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // The condition "number of buffers needs to change" is true if
2756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // - the client doesn't care about how many buffers there are
2766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // - AND the actual number of buffer is different from what was
2776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   set in the last setBufferCountServer()
2786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //                         - OR -
2796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   setBufferCountServer() was set to a value incompatible with
2806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   the synchronization mode (for instance because the sync mode
2816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   changed since)
2826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
2836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // As long as this condition is true AND the FIFO is not empty, we
2846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // wait on mDequeueCondition.
2856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            const int minBufferCountNeeded = mSynchronousMode ?
2876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
2886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
2906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    ((mServerBufferCount != mBufferCount) ||
2916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            (mServerBufferCount < minBufferCountNeeded));
2926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
2946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // wait for the FIFO to drain
2956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.wait(mMutex);
2966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // NOTE: we continue here because we need to reevaluate our
2976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // whole state (eg: we could be abandoned or disconnected)
2986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                continue;
2996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (numberOfBuffersNeedsToChange) {
3026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // here we're guaranteed that mQueue is empty
3036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                freeAllBuffersLocked();
3046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount = mServerBufferCount;
3056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (mBufferCount < minBufferCountNeeded)
3066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mBufferCount = minBufferCountNeeded;
307eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                mBufferHasBeenQueued = false;
3086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
3096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // look for a free buffer to give to the client
3126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            found = INVALID_BUFFER_SLOT;
3136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            foundSync = INVALID_BUFFER_SLOT;
3146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            dequeuedCount = 0;
3156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            for (int i = 0; i < mBufferCount; i++) {
3166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                const int state = mSlots[i].mBufferState;
3176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (state == BufferSlot::DEQUEUED) {
3186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    dequeuedCount++;
3196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
321eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                // this logic used to be if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER)
322eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                // but dequeuing the current buffer is disabled.
323eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                if (false) {
324eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // This functionality has been temporarily removed so
325eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // BufferQueue and SurfaceTexture can be refactored into
326eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // separate objects
3276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                } else {
3286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    if (state == BufferSlot::FREE) {
3296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        /* We return the oldest of the free buffers to avoid
3306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * stalling the producer if possible.  This is because
3316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * the consumer may still have pending reads of the
3326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * buffers in flight.
3336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         */
3346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        bool isOlder = mSlots[i].mFrameNumber <
3356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                                mSlots[found].mFrameNumber;
3366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        if (found < 0 || isOlder) {
3376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            foundSync = i;
3386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            found = i;
3396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        }
3406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    }
3416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // clients are not allowed to dequeue more than one buffer
3456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // if they didn't set a buffer count.
3466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (!mClientBufferCount && dequeuedCount) {
3476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
3486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        "setting the buffer count");
3496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return -EINVAL;
3506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // See whether a buffer has been queued since the last
3536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // setBufferCount so we know whether to perform the
3546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // MIN_UNDEQUEUED_BUFFERS check below.
355eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            if (mBufferHasBeenQueued) {
3566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // make sure the client is not trying to dequeue more buffers
3576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // than allowed.
3586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                const int avail = mBufferCount - (dequeuedCount+1);
3596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
3606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
3616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            "(dequeued=%d)",
3626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
3636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            dequeuedCount);
3646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    return -EBUSY;
3656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // we're in synchronous mode and didn't find a buffer, we need to
3696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // wait for some buffers to be consumed
3706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
3716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (tryAgain) {
3726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.wait(mMutex);
3736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
3756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
3776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // foundSync guaranteed to be != INVALID_BUFFER_SLOT
3786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            found = foundSync;
3796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
3806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (found == INVALID_BUFFER_SLOT) {
3826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // This should not happen.
3836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("dequeueBuffer: no available buffer slots");
3846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EBUSY;
3856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
3866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const int buf = found;
3886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        *outBuf = found;
3896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const bool useDefaultSize = !w && !h;
3916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (useDefaultSize) {
3926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // use the default size
3936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            w = mDefaultWidth;
3946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            h = mDefaultHeight;
3956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
3966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const bool updateFormat = (format != 0);
3986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (!updateFormat) {
3996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // keep the current (or default) format
4006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            format = mPixelFormat;
4016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // buffer is now in DEQUEUED (but can also be current at the same time,
4046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // if we're in synchronous mode)
4056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
4066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
4086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if ((buffer == NULL) ||
4096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->width)  != w) ||
4106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->height) != h) ||
4116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->format) != format) ||
4126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ((uint32_t(buffer->usage) & usage) != usage))
4136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        {
4146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            usage |= GraphicBuffer::USAGE_HW_TEXTURE;
4156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            status_t error;
4166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            sp<GraphicBuffer> graphicBuffer(
4176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mGraphicBufferAlloc->createGraphicBuffer(
4186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            w, h, format, usage, &error));
4196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (graphicBuffer == 0) {
4206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
4216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        "failed");
4226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return error;
4236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (updateFormat) {
4256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mPixelFormat = format;
4266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
427eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
428eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mSlots[buf].mAcquireCalled = false;
4296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mGraphicBuffer = graphicBuffer;
4306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mRequestBufferCalled = false;
4316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mFence = EGL_NO_SYNC_KHR;
432eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
433eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
434eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
435eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
436eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
4376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
4386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        dpy = mSlots[buf].mEglDisplay;
4416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        fence = mSlots[buf].mFence;
4426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mFence = EGL_NO_SYNC_KHR;
443eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }  // end lock scope
4446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (fence != EGL_NO_SYNC_KHR) {
4466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
4476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // If something goes wrong, log the error, but return the buffer without
4486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // synchronizing access to it.  It's too late at this point to abort the
4496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // dequeue operation.
4506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (result == EGL_FALSE) {
4516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ALOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
4526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
4536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ALOGE("dequeueBuffer: timeout waiting for fence");
4546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        eglDestroySyncKHR(dpy, fence);
456eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
4576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
4586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf,
4606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
4616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return returnFlags;
4636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
4646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4656b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setSynchronousMode(bool enabled) {
4661c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
4676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setSynchronousMode: enabled=%d", enabled);
4686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
4696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
4716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
4726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
4736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
4746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t err = OK;
4766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mAllowSynchronousMode && enabled)
4776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return err;
4786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!enabled) {
4806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // going to asynchronous mode, drain the queue
4816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        err = drainQueueLocked();
4826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (err != NO_ERROR)
4836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return err;
4846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
4856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mSynchronousMode != enabled) {
4876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - if we're going to asynchronous mode, the queue is guaranteed to be
4886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // empty here
4896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - if the client set the number of buffers, we're guaranteed that
4906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // we have at least 3 (because we don't allow less)
4916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSynchronousMode = enabled;
4926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mDequeueCondition.signal();
4936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
4946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
4956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
4966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4976b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::queueBuffer(int buf, int64_t timestamp,
4986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
4991c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
5006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
5016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    sp<FrameAvailableListener> listener;
5036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    { // scope for the lock
5056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Mutex::Autolock lock(mMutex);
5066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mAbandoned) {
5076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
5086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
5096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (buf < 0 || buf >= mBufferCount) {
5116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
5126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mBufferCount, buf);
5136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
5156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot %d is not owned by the client "
5166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    "(state=%d)", buf, mSlots[buf].mBufferState);
5176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (!mSlots[buf].mRequestBufferCalled) {
5196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
5206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    "buffer", buf);
5216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode) {
5256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // In synchronous mode we queue all buffers in a FIFO.
5266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mQueue.push_back(buf);
5276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // Synchronous mode always signals that an additional frame should
5296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // be consumed.
5306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            listener = mFrameAvailableListener;
5316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else {
5326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // In asynchronous mode we only keep the most recent buffer.
5336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mQueue.empty()) {
5346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mQueue.push_back(buf);
5356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // Asynchronous mode only signals that a frame should be
5376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // consumed if no previous frame was pending. If a frame were
5386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // pending then the consumer would have already been notified.
5396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                listener = mFrameAvailableListener;
5406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
5416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                Fifo::iterator front(mQueue.begin());
5426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // buffer currently queued is freed
5436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mSlots[*front].mBufferState = BufferSlot::FREE;
5446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // and we record the new buffer index in the queued list
5456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *front = buf;
5466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
5476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mBufferState = BufferSlot::QUEUED;
5506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mCrop = mNextCrop;
5516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mTransform = mNextTransform;
5526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mScalingMode = mNextScalingMode;
5536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mTimestamp = timestamp;
5546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mFrameCounter++;
5556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mFrameNumber = mFrameCounter;
5566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
557eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mBufferHasBeenQueued = true;
5586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mDequeueCondition.signal();
5596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        *outWidth = mDefaultWidth;
5616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        *outHeight = mDefaultHeight;
5626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        *outTransform = 0;
5631c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
5641c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis        ATRACE_INT(mConsumerName.string(), mQueue.size());
5656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } // scope for the lock
5666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // call back without lock held
5686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (listener != 0) {
5696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        listener->onFrameAvailable();
5706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
5726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
5736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5746b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::cancelBuffer(int buf) {
5751c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
5766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("cancelBuffer: slot=%d", buf);
5776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
5786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
5806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
5816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
5826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (buf < 0 || buf >= mBufferCount) {
5856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
5866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount, buf);
5876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
5886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
5896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
5906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                buf, mSlots[buf].mBufferState);
5916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
5926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[buf].mBufferState = BufferSlot::FREE;
5946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[buf].mFrameNumber = 0;
5956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDequeueCondition.signal();
5966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
5976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5986b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setCrop(const Rect& crop) {
5991c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
6016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            crop.bottom);
6026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setCrop: BufferQueue has been abandoned!");
6066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
6076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mNextCrop = crop;
6096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
6106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6126b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setTransform(uint32_t transform) {
6131c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setTransform: xform=%#x", transform);
6156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setTransform: BufferQueue has been abandoned!");
6186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
6196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mNextTransform = transform;
6216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
6226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6246b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setScalingMode(int mode) {
6251c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setScalingMode: mode=%d", mode);
6276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    switch (mode) {
6296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_SCALING_MODE_FREEZE:
6306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
6316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
6326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        default:
6336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("unknown scaling mode: %d", mode);
6346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return BAD_VALUE;
6356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mNextScalingMode = mode;
6396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
6406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6426b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::connect(int api,
6436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
6441c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("connect: api=%d", api);
6466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("connect: BufferQueue has been abandoned!");
6506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
6516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int err = NO_ERROR;
6546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    switch (api) {
6556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_EGL:
6566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CPU:
6576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_MEDIA:
6586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CAMERA:
6596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mConnectedApi != NO_CONNECTED_API) {
6606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("connect: already connected (cur=%d, req=%d)",
6616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        mConnectedApi, api);
6626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                err = -EINVAL;
6636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
6646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mConnectedApi = api;
6656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *outWidth = mDefaultWidth;
6666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *outHeight = mDefaultHeight;
6676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *outTransform = 0;
6686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
6696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
6706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        default:
6716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            err = -EINVAL;
6726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
6736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
674eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
675eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
676eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
6776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
6786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6806b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::disconnect(int api) {
6811c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("disconnect: api=%d", api);
6836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // it is not really an error to disconnect after the surface
6876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // has been abandoned, it should just be a no-op.
6886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_ERROR;
6896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int err = NO_ERROR;
6926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    switch (api) {
6936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_EGL:
6946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CPU:
6956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_MEDIA:
6966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CAMERA:
6976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mConnectedApi == api) {
6986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                drainQueueAndFreeBuffersLocked();
6996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mConnectedApi = NO_CONNECTED_API;
7006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mNextCrop.makeInvalid();
7016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
7026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mNextTransform = 0;
7036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.signal();
7046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
7056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
7066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        mConnectedApi, api);
7076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                err = -EINVAL;
7086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
7096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
7106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        default:
7116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("disconnect: unknown API %d", api);
7126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            err = -EINVAL;
7136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
7146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
7156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
7166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
7176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
718eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::dump(String8& result) const
719eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
720eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    char buffer[1024];
721eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    BufferQueue::dump(result, "", buffer, 1024);
722eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
723eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
724eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::dump(String8& result, const char* prefix,
725eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        char* buffer, size_t SIZE) const
726eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
727eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
728eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    snprintf(buffer, SIZE,
729eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            "%snext   : {crop=[%d,%d,%d,%d], transform=0x%02x}\n"
730eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            ,prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
731eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mNextCrop.bottom, mNextTransform
732eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    );
733eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    result.append(buffer);
734eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
735eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    String8 fifo;
736eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    int fifoSize = 0;
737eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Fifo::const_iterator i(mQueue.begin());
738eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    while (i != mQueue.end()) {
739eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       snprintf(buffer, SIZE, "%02d ", *i++);
740eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       fifoSize++;
741eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       fifo.append(buffer);
742eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
743eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
744eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    snprintf(buffer, SIZE,
745eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            "%s-BufferQueue mBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
746eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            "mPixelFormat=%d, FIFO(%d)={%s}\n",
747eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
748eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mDefaultHeight, mPixelFormat, fifoSize, fifo.string());
749eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    result.append(buffer);
750eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
751eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
752eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    struct {
753eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const char * operator()(int state) const {
754eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            switch (state) {
755eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::DEQUEUED: return "DEQUEUED";
756eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::QUEUED: return "QUEUED";
757eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::FREE: return "FREE";
758eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::ACQUIRED: return "ACQUIRED";
759eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                default: return "Unknown";
760eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            }
761eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
762eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    } stateName;
763eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
764eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    for (int i=0 ; i<mBufferCount ; i++) {
765eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const BufferSlot& slot(mSlots[i]);
766eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        snprintf(buffer, SIZE,
767eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "%s%s[%02d] "
768eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "state=%-8s, crop=[%d,%d,%d,%d], "
769eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "transform=0x%02x, timestamp=%lld",
770eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i,
771eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                stateName(slot.mBufferState),
772eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
773eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
774eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        );
775eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        result.append(buffer);
776eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
777eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
778eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        if (buf != NULL) {
779eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            snprintf(buffer, SIZE,
780eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    ", %p [%4ux%4u:%4u,%3X]",
781eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    buf->handle, buf->width, buf->height, buf->stride,
782eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    buf->format);
783eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            result.append(buffer);
784eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
785eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        result.append("\n");
786eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
787eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
788eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
7896b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeBufferLocked(int i) {
7906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[i].mGraphicBuffer = 0;
7916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[i].mBufferState = BufferSlot::FREE;
7926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[i].mFrameNumber = 0;
793eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[i].mAcquireCalled = false;
794eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
795eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // destroy fence as BufferQueue now takes ownership
796eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (mSlots[i].mFence != EGL_NO_SYNC_KHR) {
797eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        eglDestroySyncKHR(mSlots[i].mEglDisplay, mSlots[i].mFence);
798eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[i].mFence = EGL_NO_SYNC_KHR;
7996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
8006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
8016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
8026b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeAllBuffersLocked() {
8036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ALOGW_IF(!mQueue.isEmpty(),
8046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            "freeAllBuffersLocked called but mQueue is not empty");
805eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mQueue.clear();
806eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
8076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
8086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        freeBufferLocked(i);
8096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
8106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
8116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
812eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::acquire(BufferItem *buffer) {
813eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
814eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // check if queue is empty
815eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // In asynchronous mode the list is guaranteed to be one buffer
816eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // deep, while in synchronous mode we use the oldest buffer.
817eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (!mQueue.empty()) {
818eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        Fifo::iterator front(mQueue.begin());
819eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        int buf = *front;
820eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
821eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        if (mSlots[buf].mAcquireCalled) {
822eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            buffer->mGraphicBuffer = NULL;
823eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
824eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        else {
825eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer;
826eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
827eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mCrop = mSlots[buf].mCrop;
828eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mTransform = mSlots[buf].mTransform;
829eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mScalingMode = mSlots[buf].mScalingMode;
830eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mFrameNumber = mSlots[buf].mFrameNumber;
831eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mBuf = buf;
832eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mAcquireCalled = true;
833eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
834eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
835eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mQueue.erase(front);
8361c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
8371c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis        ATRACE_INT(mConsumerName.string(), mQueue.size());
838eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
839eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    else {
840eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return -EINVAL; //should be a better return code
841eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
842eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
843eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
844eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
845eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
846eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::releaseBuffer(int buf, EGLDisplay display,
847eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        EGLSyncKHR fence) {
848eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
849eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
850eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (buf == INVALID_BUFFER_SLOT) {
851eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return -EINVAL;
852eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
853eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
854eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[buf].mEglDisplay = display;
855eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[buf].mFence = fence;
856eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
857eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // The current buffer becomes FREE if it was still in the queued
858eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // state. If it has already been given to the client
859eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // (synchronous mode), then it stays in DEQUEUED state.
860eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (mSlots[buf].mBufferState == BufferSlot::QUEUED
861eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            || mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
862eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mBufferState = BufferSlot::FREE;
863eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
864eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDequeueCondition.signal();
865eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
866eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
867eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
868eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
869eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::consumerDisconnect() {
870eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
871eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // Once the SurfaceTexture disconnects, the BufferQueue
872eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // is considered abandoned
873eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mAbandoned = true;
874eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    freeAllBuffersLocked();
875eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDequeueCondition.signal();
876eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
877eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
878eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
879eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
880eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
881eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
882eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (!w || !h) {
883eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
884eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                w, h);
885eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return BAD_VALUE;
886eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
887eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
888eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
889eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDefaultWidth = w;
890eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDefaultHeight = h;
891eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
892eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
893eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
894eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::setBufferCountServer(int bufferCount) {
8951c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
896eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
897eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return setBufferCountServerLocked(bufferCount);
898eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
899eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
9006b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeAllBuffersExceptHeadLocked() {
9016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ALOGW_IF(!mQueue.isEmpty(),
9026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
9036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int head = -1;
9046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mQueue.empty()) {
9056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Fifo::iterator front(mQueue.begin());
9066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        head = *front;
9076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
908eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
9096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
9106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (i != head) {
9116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeBufferLocked(i);
9126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
9146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
9156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
9166b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::drainQueueLocked() {
9176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    while (mSynchronousMode && !mQueue.isEmpty()) {
9186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mDequeueCondition.wait(mMutex);
9196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mAbandoned) {
9206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("drainQueueLocked: BufferQueue has been abandoned!");
9216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
9226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mConnectedApi == NO_CONNECTED_API) {
9246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("drainQueueLocked: BufferQueue is not connected!");
9256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
9266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
9286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return NO_ERROR;
9296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
9306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
9316b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::drainQueueAndFreeBuffersLocked() {
9326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t err = drainQueueLocked();
9336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (err == NO_ERROR) {
9346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode) {
9356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeAllBuffersLocked();
9366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else {
9376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeAllBuffersExceptHeadLocked();
9386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
9406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
9416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
9426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
9436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}; // namespace android
944