BufferQueue.cpp revision f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246
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"
181c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis//#define LOG_NDEBUG 0
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
59546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian#define ATRACE_BUFFER_INDEX(index)                                            \
60546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    char ___traceBuf[1024];                                                   \
61546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), (index));   \
62546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf);
63546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
646b091c53000c843211c218ce40287a7edca9bc63Daniel Lamnamespace android {
656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// Get an ID that's unique within this process.
676b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatic int32_t createProcessUniqueId() {
686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    static volatile int32_t globalCounter = 0;
696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return android_atomic_inc(&globalCounter);
706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
72abe61bfda4938abd932465e27c29ba9e41aea606Daniel LamBufferQueue::BufferQueue(  bool allowSynchronousMode, int bufferCount ) :
736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDefaultWidth(1),
746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDefaultHeight(1),
756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mPixelFormat(PIXEL_FORMAT_RGBA_8888),
76abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mMinUndequeuedBuffers(bufferCount),
77abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mMinAsyncBufferSlots(bufferCount + 1),
78abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mMinSyncBufferSlots(bufferCount),
79abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mBufferCount(mMinAsyncBufferSlots),
806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mClientBufferCount(0),
81abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mServerBufferCount(mMinAsyncBufferSlots),
826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSynchronousMode(false),
836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mAllowSynchronousMode(allowSynchronousMode),
846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mConnectedApi(NO_CONNECTED_API),
856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mAbandoned(false),
86eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mFrameCounter(0),
87b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mBufferHasBeenQueued(false),
88b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mDefaultBufferFormat(0),
89b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mConsumerUsageBits(0),
90b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mTransformHint(0)
916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam{
926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // Choose a name using the PID and a process-unique ID.
93eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("BufferQueue");
966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    sp<ISurfaceComposer> composer(ComposerService::getComposerService());
976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
98abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    if (mGraphicBufferAlloc == 0) {
99abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam        ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
100abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    }
1016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1036b091c53000c843211c218ce40287a7edca9bc63Daniel LamBufferQueue::~BufferQueue() {
1046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("~BufferQueue");
1056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1076b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setBufferCountServerLocked(int bufferCount) {
1086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount > NUM_BUFFER_SLOTS)
1096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
1106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // special-case, nothing to do
1126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount == mBufferCount)
1136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return OK;
1146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mClientBufferCount &&
1166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bufferCount >= mBufferCount) {
1176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // easy, we just have more buffers
1186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mBufferCount = bufferCount;
1196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mServerBufferCount = bufferCount;
12074ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
1216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } else {
1226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // we're here because we're either
1236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - reducing the number of available buffers
1246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - or there is a client-buffer-count in effect
1256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // less than 2 buffers is never allowed
1276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (bufferCount < 2)
1286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return BAD_VALUE;
1296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // when there is non client-buffer-count in effect, the client is not
1316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // allowed to dequeue more than one buffer at a time,
1326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // so the next time they dequeue a buffer, we know that they don't
1336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // own one. the actual resizing will happen during the next
1346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // dequeueBuffer.
1356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mServerBufferCount = bufferCount;
137b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        mDequeueCondition.broadcast();
1386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
1406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
142eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lambool BufferQueue::isSynchronousMode() const {
143eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
144eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return mSynchronousMode;
145eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
146eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
147eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::setConsumerName(const String8& name) {
148eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
149eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mConsumerName = name;
150eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
151eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
152b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lamstatus_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
153b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    Mutex::Autolock lock(mMutex);
154b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mDefaultBufferFormat = defaultFormat;
155b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return OK;
156b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam}
157b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
158b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lamstatus_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
159b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    Mutex::Autolock lock(mMutex);
160b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mConsumerUsageBits = usage;
161b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return OK;
162b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam}
163b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
164b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lamstatus_t BufferQueue::setTransformHint(uint32_t hint) {
165b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    Mutex::Autolock lock(mMutex);
166b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mTransformHint = hint;
167b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return OK;
168b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam}
169b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
1706b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setBufferCount(int bufferCount) {
1716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setBufferCount: count=%d", bufferCount);
1726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1739abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    sp<ConsumerListener> listener;
1749abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    {
1759abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        Mutex::Autolock lock(mMutex);
1766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1779abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (mAbandoned) {
1789abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
1799abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return NO_INIT;
1809abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
1819abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (bufferCount > NUM_BUFFER_SLOTS) {
1829abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            ST_LOGE("setBufferCount: bufferCount larger than slots available");
1839abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return BAD_VALUE;
1846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
1856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1869abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // Error out if the user has dequeued buffers
1879abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        for (int i=0 ; i<mBufferCount ; i++) {
1889abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
1899abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                ST_LOGE("setBufferCount: client owns some buffers");
1909abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                return -EINVAL;
1919abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            }
1929abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
1936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1949abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        const int minBufferSlots = mSynchronousMode ?
195abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam            mMinSyncBufferSlots : mMinAsyncBufferSlots;
1969abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (bufferCount == 0) {
1979abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            mClientBufferCount = 0;
1989abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            bufferCount = (mServerBufferCount >= minBufferSlots) ?
1999abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                    mServerBufferCount : minBufferSlots;
2009abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return setBufferCountServerLocked(bufferCount);
2019abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
2029abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
2039abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (bufferCount < minBufferSlots) {
2049abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
2059abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                    "minimum (%d)", bufferCount, minBufferSlots);
2069abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return BAD_VALUE;
2079abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
2089abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
2099abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // here we're guaranteed that the client doesn't have dequeued buffers
2109abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // and will release all of its buffer references.
2119abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        freeAllBuffersLocked();
2129abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mBufferCount = bufferCount;
2139abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mClientBufferCount = bufferCount;
2149abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mBufferHasBeenQueued = false;
2159abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mQueue.clear();
2169abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mDequeueCondition.broadcast();
2179abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        listener = mConsumerListener;
2189abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    } // scope for lock
2199abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
2209abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    if (listener != NULL) {
2219abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        listener->onBuffersReleased();
2226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
2256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
2266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
227b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lamint BufferQueue::query(int what, int* outValue)
228b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam{
2291c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
230b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    Mutex::Autolock lock(mMutex);
231b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
232b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    if (mAbandoned) {
233b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        ST_LOGE("query: SurfaceTexture has been abandoned!");
234b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        return NO_INIT;
235b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    }
236b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
237b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    int value;
238b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    switch (what) {
239b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_WIDTH:
240b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mDefaultWidth;
241b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
242b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_HEIGHT:
243b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mDefaultHeight;
244b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
245b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_FORMAT:
246b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mPixelFormat;
247b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
248b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
249b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mSynchronousMode ?
250abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                (mMinUndequeuedBuffers-1) : mMinUndequeuedBuffers;
251b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
252b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    default:
253b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        return BAD_VALUE;
254b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    }
255b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    outValue[0] = value;
256b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    return NO_ERROR;
257b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam}
258b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
2596b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
2601c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
2616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("requestBuffer: slot=%d", slot);
2626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
2636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
2646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
2656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
2666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (slot < 0 || mBufferCount <= slot) {
2686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
2696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount, slot);
2706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
2716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[slot].mRequestBufferCalled = true;
2736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    *buf = mSlots[slot].mGraphicBuffer;
2746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return NO_ERROR;
2756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
2766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2776b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
2786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t format, uint32_t usage) {
2791c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
2806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
2816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if ((w && !h) || (!w && h)) {
2836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
2846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
2856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t returnFlags(OK);
2886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    EGLDisplay dpy = EGL_NO_DISPLAY;
2896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    EGLSyncKHR fence = EGL_NO_SYNC_KHR;
2906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    { // Scope for the lock
2926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Mutex::Autolock lock(mMutex);
2936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
294b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        if (format == 0) {
295b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam            format = mDefaultBufferFormat;
296b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        }
297b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        // turn on usage bits the consumer requested
298b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        usage |= mConsumerUsageBits;
299b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
3006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int found = -1;
3016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int foundSync = -1;
3026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int dequeuedCount = 0;
3036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bool tryAgain = true;
3046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        while (tryAgain) {
3056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mAbandoned) {
3066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
3076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return NO_INIT;
3086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // We need to wait for the FIFO to drain if the number of buffer
3116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // needs to change.
3126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
3136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // The condition "number of buffers needs to change" is true if
3146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // - the client doesn't care about how many buffers there are
3156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // - AND the actual number of buffer is different from what was
3166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   set in the last setBufferCountServer()
3176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //                         - OR -
3186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   setBufferCountServer() was set to a value incompatible with
3196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   the synchronization mode (for instance because the sync mode
3206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   changed since)
3216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
3226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // As long as this condition is true AND the FIFO is not empty, we
3236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // wait on mDequeueCondition.
3246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            const int minBufferCountNeeded = mSynchronousMode ?
326abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                    mMinSyncBufferSlots : mMinAsyncBufferSlots;
3276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
3296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    ((mServerBufferCount != mBufferCount) ||
3306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            (mServerBufferCount < minBufferCountNeeded));
3316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
3336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // wait for the FIFO to drain
3346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.wait(mMutex);
3356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // NOTE: we continue here because we need to reevaluate our
3366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // whole state (eg: we could be abandoned or disconnected)
3376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                continue;
3386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (numberOfBuffersNeedsToChange) {
3416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // here we're guaranteed that mQueue is empty
3426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                freeAllBuffersLocked();
3436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount = mServerBufferCount;
3446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (mBufferCount < minBufferCountNeeded)
3456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mBufferCount = minBufferCountNeeded;
346eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                mBufferHasBeenQueued = false;
3476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
3486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // look for a free buffer to give to the client
3516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            found = INVALID_BUFFER_SLOT;
3526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            foundSync = INVALID_BUFFER_SLOT;
3536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            dequeuedCount = 0;
3546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            for (int i = 0; i < mBufferCount; i++) {
3556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                const int state = mSlots[i].mBufferState;
3566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (state == BufferSlot::DEQUEUED) {
3576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    dequeuedCount++;
3586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
360eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                // this logic used to be if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER)
361eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                // but dequeuing the current buffer is disabled.
362eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                if (false) {
363eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // This functionality has been temporarily removed so
364eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // BufferQueue and SurfaceTexture can be refactored into
365eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // separate objects
3666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                } else {
3676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    if (state == BufferSlot::FREE) {
3686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        /* We return the oldest of the free buffers to avoid
3696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * stalling the producer if possible.  This is because
3706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * the consumer may still have pending reads of the
3716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * buffers in flight.
3726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         */
3736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        bool isOlder = mSlots[i].mFrameNumber <
3746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                                mSlots[found].mFrameNumber;
3756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        if (found < 0 || isOlder) {
3766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            foundSync = i;
3776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            found = i;
3786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        }
3796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    }
3806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // clients are not allowed to dequeue more than one buffer
3846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // if they didn't set a buffer count.
3856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (!mClientBufferCount && dequeuedCount) {
3866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
3876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        "setting the buffer count");
3886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return -EINVAL;
3896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // See whether a buffer has been queued since the last
3926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // setBufferCount so we know whether to perform the
393abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam            // mMinUndequeuedBuffers check below.
394eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            if (mBufferHasBeenQueued) {
3956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // make sure the client is not trying to dequeue more buffers
3966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // than allowed.
3976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                const int avail = mBufferCount - (dequeuedCount+1);
398abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                if (avail < (mMinUndequeuedBuffers-int(mSynchronousMode))) {
399abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                    ST_LOGE("dequeueBuffer: mMinUndequeuedBuffers=%d exceeded "
4006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            "(dequeued=%d)",
401abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                            mMinUndequeuedBuffers-int(mSynchronousMode),
4026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            dequeuedCount);
4036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    return -EBUSY;
4046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
4056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
407c2c1f2f24cd70bdcf1958157bab38467bf0fdc71Daniel Lam            // if no buffer is found, wait for a buffer to be released
408c2c1f2f24cd70bdcf1958157bab38467bf0fdc71Daniel Lam            tryAgain = found == INVALID_BUFFER_SLOT;
4096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (tryAgain) {
4106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.wait(mMutex);
4116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (found == INVALID_BUFFER_SLOT) {
4166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // This should not happen.
4176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("dequeueBuffer: no available buffer slots");
4186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EBUSY;
4196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const int buf = found;
4226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        *outBuf = found;
4236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
424546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian        ATRACE_BUFFER_INDEX(buf);
425546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
4266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const bool useDefaultSize = !w && !h;
4276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (useDefaultSize) {
4286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // use the default size
4296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            w = mDefaultWidth;
4306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            h = mDefaultHeight;
4316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const bool updateFormat = (format != 0);
4346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (!updateFormat) {
4356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // keep the current (or default) format
4366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            format = mPixelFormat;
4376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // buffer is now in DEQUEUED (but can also be current at the same time,
4406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // if we're in synchronous mode)
4416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
4426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
4446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if ((buffer == NULL) ||
4456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->width)  != w) ||
4466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->height) != h) ||
4476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->format) != format) ||
4486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ((uint32_t(buffer->usage) & usage) != usage))
4496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        {
4506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            usage |= GraphicBuffer::USAGE_HW_TEXTURE;
4516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            status_t error;
4526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            sp<GraphicBuffer> graphicBuffer(
4536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mGraphicBufferAlloc->createGraphicBuffer(
4546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            w, h, format, usage, &error));
4556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (graphicBuffer == 0) {
4566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
4576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        "failed");
4586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return error;
4596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (updateFormat) {
4616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mPixelFormat = format;
4626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
463eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
464eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mSlots[buf].mAcquireCalled = false;
4656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mGraphicBuffer = graphicBuffer;
4666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mRequestBufferCalled = false;
4676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mFence = EGL_NO_SYNC_KHR;
468eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
469eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
4706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
4716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        dpy = mSlots[buf].mEglDisplay;
4746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        fence = mSlots[buf].mFence;
4756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mFence = EGL_NO_SYNC_KHR;
476eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }  // end lock scope
4776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (fence != EGL_NO_SYNC_KHR) {
4796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
4806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // If something goes wrong, log the error, but return the buffer without
4816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // synchronizing access to it.  It's too late at this point to abort the
4826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // dequeue operation.
4836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (result == EGL_FALSE) {
4846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ALOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
4856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
4866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ALOGE("dequeueBuffer: timeout waiting for fence");
4876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        eglDestroySyncKHR(dpy, fence);
4896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
4906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf,
4926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
4936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return returnFlags;
4956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
4966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4976b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setSynchronousMode(bool enabled) {
4981c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
4996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setSynchronousMode: enabled=%d", enabled);
5006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
5016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
5036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
5046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
5056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t err = OK;
5086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mAllowSynchronousMode && enabled)
5096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return err;
5106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!enabled) {
5126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // going to asynchronous mode, drain the queue
5136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        err = drainQueueLocked();
5146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (err != NO_ERROR)
5156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return err;
5166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mSynchronousMode != enabled) {
5196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - if we're going to asynchronous mode, the queue is guaranteed to be
5206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // empty here
5216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - if the client set the number of buffers, we're guaranteed that
5226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // we have at least 3 (because we don't allow less)
5236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSynchronousMode = enabled;
52474ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
5256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
5276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
5286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
529f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopianstatus_t BufferQueue::queueBuffer(int buf,
530f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian        const QueueBufferInput& input, QueueBufferOutput* output) {
5311c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
532546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_BUFFER_INDEX(buf);
533546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
5346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
5356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
536fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<ConsumerListener> listener;
5376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    { // scope for the lock
5396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Mutex::Autolock lock(mMutex);
5406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mAbandoned) {
5416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
5426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
5436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (buf < 0 || buf >= mBufferCount) {
5456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
5466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mBufferCount, buf);
5476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
5496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot %d is not owned by the client "
5506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    "(state=%d)", buf, mSlots[buf].mBufferState);
5516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (!mSlots[buf].mRequestBufferCalled) {
5536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
5546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    "buffer", buf);
5556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode) {
5596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // In synchronous mode we queue all buffers in a FIFO.
5606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mQueue.push_back(buf);
5616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // Synchronous mode always signals that an additional frame should
5636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // be consumed.
564fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            listener = mConsumerListener;
5656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else {
5666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // In asynchronous mode we only keep the most recent buffer.
5676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mQueue.empty()) {
5686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mQueue.push_back(buf);
5696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // Asynchronous mode only signals that a frame should be
5716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // consumed if no previous frame was pending. If a frame were
5726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // pending then the consumer would have already been notified.
573fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                listener = mConsumerListener;
5746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
5756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                Fifo::iterator front(mQueue.begin());
5766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // buffer currently queued is freed
5776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mSlots[*front].mBufferState = BufferSlot::FREE;
5786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // and we record the new buffer index in the queued list
5796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *front = buf;
5806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
5816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
583f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian        int scalingMode;
584f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian
585f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian        input.deflate(
586f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian                &mSlots[buf].mTimestamp,
587f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian                &mSlots[buf].mCrop,
588f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian                &scalingMode,
589f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian                &mSlots[buf].mTransform);
590f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian
591f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian
592851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        switch (scalingMode) {
593851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian            case NATIVE_WINDOW_SCALING_MODE_FREEZE:
594851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian            case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
595851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                break;
596851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian            default:
597851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                ST_LOGE("unknown scaling mode: %d (ignoring)", scalingMode);
598851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                scalingMode = mSlots[buf].mScalingMode;
599851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                break;
600851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        }
601851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian
6026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mBufferState = BufferSlot::QUEUED;
603851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        mSlots[buf].mScalingMode = scalingMode;
6046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mFrameCounter++;
6056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mFrameNumber = mFrameCounter;
6066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
607eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mBufferHasBeenQueued = true;
60874ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
6096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
610f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian        output->inflate(mDefaultWidth, mDefaultHeight, mDefaultHeight);
6111c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
6121c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis        ATRACE_INT(mConsumerName.string(), mQueue.size());
6136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } // scope for the lock
6146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // call back without lock held
6166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (listener != 0) {
6176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        listener->onFrameAvailable();
6186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
6206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6226b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::cancelBuffer(int buf) {
6231c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("cancelBuffer: slot=%d", buf);
6256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
6296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
6306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (buf < 0 || buf >= mBufferCount) {
6336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
6346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount, buf);
6356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
6366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
6376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
6386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                buf, mSlots[buf].mBufferState);
6396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
6406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[buf].mBufferState = BufferSlot::FREE;
6426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[buf].mFrameNumber = 0;
64374ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke    mDequeueCondition.broadcast();
6446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6466b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::connect(int api,
6476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
6481c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("connect: api=%d", api);
6506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("connect: BufferQueue has been abandoned!");
6546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
6556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
657fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mConsumerListener == NULL) {
658fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("connect: BufferQueue has no consumer!");
659fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return NO_INIT;
660fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
661fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
6626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int err = NO_ERROR;
6636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    switch (api) {
6646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_EGL:
6656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CPU:
6666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_MEDIA:
6676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CAMERA:
6686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mConnectedApi != NO_CONNECTED_API) {
6696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("connect: already connected (cur=%d, req=%d)",
6706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        mConnectedApi, api);
6716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                err = -EINVAL;
6726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
6736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mConnectedApi = api;
6746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *outWidth = mDefaultWidth;
6756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *outHeight = mDefaultHeight;
6766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *outTransform = 0;
6776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
6786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
6796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        default:
6806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            err = -EINVAL;
6816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
6826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
683eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
684eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
685eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
6866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
6876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6896b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::disconnect(int api) {
6901c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("disconnect: api=%d", api);
6926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int err = NO_ERROR;
694fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<ConsumerListener> listener;
695fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
696fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    { // Scope for the lock
697fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        Mutex::Autolock lock(mMutex);
698fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
699fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        if (mAbandoned) {
700fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            // it is not really an error to disconnect after the surface
701fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            // has been abandoned, it should just be a no-op.
702fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            return NO_ERROR;
703fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        }
704fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
705fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        switch (api) {
706fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_EGL:
707fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_CPU:
708fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_MEDIA:
709fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_CAMERA:
710fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                if (mConnectedApi == api) {
711fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    drainQueueAndFreeBuffersLocked();
712fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    mConnectedApi = NO_CONNECTED_API;
713fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    mDequeueCondition.broadcast();
714fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    listener = mConsumerListener;
715fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                } else {
716fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
717fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                            mConnectedApi, api);
718fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    err = -EINVAL;
719fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                }
720fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                break;
721fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            default:
722fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                ST_LOGE("disconnect: unknown API %d", api);
7236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                err = -EINVAL;
724fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                break;
725fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        }
7266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
727fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
728fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (listener != NULL) {
729fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        listener->onBuffersReleased();
730fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
731fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
7326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
7336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
7346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
735eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::dump(String8& result) const
736eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
737eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    char buffer[1024];
738eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    BufferQueue::dump(result, "", buffer, 1024);
739eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
740eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
741eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::dump(String8& result, const char* prefix,
742eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        char* buffer, size_t SIZE) const
743eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
744eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
745eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
746eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    String8 fifo;
747eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    int fifoSize = 0;
748eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Fifo::const_iterator i(mQueue.begin());
749eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    while (i != mQueue.end()) {
750eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       snprintf(buffer, SIZE, "%02d ", *i++);
751eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       fifoSize++;
752eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       fifo.append(buffer);
753eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
754eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
755eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    snprintf(buffer, SIZE,
756eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            "%s-BufferQueue mBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
757eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            "mPixelFormat=%d, FIFO(%d)={%s}\n",
758eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
759eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mDefaultHeight, mPixelFormat, fifoSize, fifo.string());
760eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    result.append(buffer);
761eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
762eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
763eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    struct {
764eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const char * operator()(int state) const {
765eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            switch (state) {
766eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::DEQUEUED: return "DEQUEUED";
767eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::QUEUED: return "QUEUED";
768eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::FREE: return "FREE";
769eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::ACQUIRED: return "ACQUIRED";
770eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                default: return "Unknown";
771eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            }
772eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
773eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    } stateName;
774eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
775eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    for (int i=0 ; i<mBufferCount ; i++) {
776eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const BufferSlot& slot(mSlots[i]);
777eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        snprintf(buffer, SIZE,
778eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "%s%s[%02d] "
779eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "state=%-8s, crop=[%d,%d,%d,%d], "
780eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "transform=0x%02x, timestamp=%lld",
781eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i,
782eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                stateName(slot.mBufferState),
783eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
784eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
785eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        );
786eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        result.append(buffer);
787eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
788eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
789eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        if (buf != NULL) {
790eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            snprintf(buffer, SIZE,
791eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    ", %p [%4ux%4u:%4u,%3X]",
792eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    buf->handle, buf->width, buf->height, buf->stride,
793eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    buf->format);
794eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            result.append(buffer);
795eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
796eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        result.append("\n");
797eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
798eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
799eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
8006b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeBufferLocked(int i) {
8016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[i].mGraphicBuffer = 0;
8029abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
8039abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mSlots[i].mNeedsCleanupOnRelease = true;
8049abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    }
8056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[i].mBufferState = BufferSlot::FREE;
8066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[i].mFrameNumber = 0;
807eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[i].mAcquireCalled = false;
808eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
809eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // destroy fence as BufferQueue now takes ownership
810eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (mSlots[i].mFence != EGL_NO_SYNC_KHR) {
811eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        eglDestroySyncKHR(mSlots[i].mEglDisplay, mSlots[i].mFence);
812eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[i].mFence = EGL_NO_SYNC_KHR;
8136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
8146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
8156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
8166b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeAllBuffersLocked() {
8176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ALOGW_IF(!mQueue.isEmpty(),
8186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            "freeAllBuffersLocked called but mQueue is not empty");
819eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mQueue.clear();
820eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
8216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
8226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        freeBufferLocked(i);
8236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
8246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
8256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
826fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisstatus_t BufferQueue::acquireBuffer(BufferItem *buffer) {
827546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_CALL();
828eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
829eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // check if queue is empty
830eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // In asynchronous mode the list is guaranteed to be one buffer
831eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // deep, while in synchronous mode we use the oldest buffer.
832eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (!mQueue.empty()) {
833eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        Fifo::iterator front(mQueue.begin());
834eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        int buf = *front;
835eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
836546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian        ATRACE_BUFFER_INDEX(buf);
837546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
838eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        if (mSlots[buf].mAcquireCalled) {
839eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            buffer->mGraphicBuffer = NULL;
840fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        } else {
841eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer;
842eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
843eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mCrop = mSlots[buf].mCrop;
844eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mTransform = mSlots[buf].mTransform;
845eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mScalingMode = mSlots[buf].mScalingMode;
846eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mFrameNumber = mSlots[buf].mFrameNumber;
8473fcee50ffaeb745819356e395408b4d7e3239c13Daniel Lam        buffer->mTimestamp = mSlots[buf].mTimestamp;
848eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mBuf = buf;
849eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mAcquireCalled = true;
850eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
851eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
852eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mQueue.erase(front);
85374ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
8541c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
8551c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis        ATRACE_INT(mConsumerName.string(), mQueue.size());
856fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    } else {
857fbcda930dd8b2823cfeb160fd0131f5897b7522fDaniel Lam        return NO_BUFFER_AVAILABLE;
858eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
859eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
860eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
861eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
862eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
863eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::releaseBuffer(int buf, EGLDisplay display,
864eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        EGLSyncKHR fence) {
865546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_CALL();
866546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_BUFFER_INDEX(buf);
867546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
868eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
869eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
870eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (buf == INVALID_BUFFER_SLOT) {
871eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return -EINVAL;
872eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
873eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
874eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[buf].mEglDisplay = display;
875eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[buf].mFence = fence;
876eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
8779abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    // The buffer can now only be released if its in the acquired state
8789abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
879eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mBufferState = BufferSlot::FREE;
8809abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    } else if (mSlots[buf].mNeedsCleanupOnRelease) {
8819abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
8829abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mSlots[buf].mNeedsCleanupOnRelease = false;
8839abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        return STALE_BUFFER_SLOT;
8849abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    } else {
8859abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
8869abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        return -EINVAL;
887eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
888eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
8899abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    mDequeueCondition.broadcast();
890eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
891eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
892eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
893fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisstatus_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) {
894fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("consumerConnect");
895fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    Mutex::Autolock lock(mMutex);
896fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
897fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mAbandoned) {
898fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
899fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return NO_INIT;
900fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
901fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
902fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mConsumerListener = consumerListener;
903fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
904fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    return OK;
905fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
906fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
907eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::consumerDisconnect() {
908fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("consumerDisconnect");
909eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
910b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
911fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mConsumerListener == NULL) {
912fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("consumerDisconnect: No consumer is connected!");
913fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return -EINVAL;
914fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
915b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
916fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mAbandoned = true;
917fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mConsumerListener = NULL;
918b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mQueue.clear();
919eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    freeAllBuffersLocked();
92074ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke    mDequeueCondition.broadcast();
921eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
922eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
923eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
924fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisstatus_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
925fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("getReleasedBuffers");
926fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    Mutex::Autolock lock(mMutex);
927fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
928fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mAbandoned) {
929fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
930fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return NO_INIT;
931fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
932fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
933fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    uint32_t mask = 0;
934fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
935fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        if (!mSlots[i].mAcquireCalled) {
936fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            mask |= 1 << i;
937fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        }
938fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
939fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    *slotMask = mask;
940fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
941fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
942fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    return NO_ERROR;
943fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
944fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
945eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
946eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
947eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
948eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (!w || !h) {
949eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
950eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                w, h);
951eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return BAD_VALUE;
952eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
953eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
954eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
955eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDefaultWidth = w;
956eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDefaultHeight = h;
957eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
958eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
959eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
960eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::setBufferCountServer(int bufferCount) {
9611c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
962eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
963eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return setBufferCountServerLocked(bufferCount);
964eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
965eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
9666b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeAllBuffersExceptHeadLocked() {
9676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ALOGW_IF(!mQueue.isEmpty(),
9686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
9696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int head = -1;
9706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mQueue.empty()) {
9716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Fifo::iterator front(mQueue.begin());
9726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        head = *front;
9736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
974eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
9756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
9766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (i != head) {
9776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeBufferLocked(i);
9786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
9806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
9816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
9826b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::drainQueueLocked() {
9836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    while (mSynchronousMode && !mQueue.isEmpty()) {
9846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mDequeueCondition.wait(mMutex);
9856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mAbandoned) {
9866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("drainQueueLocked: BufferQueue has been abandoned!");
9876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
9886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mConnectedApi == NO_CONNECTED_API) {
9906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("drainQueueLocked: BufferQueue is not connected!");
9916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
9926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
9936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
9946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return NO_ERROR;
9956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
9966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
9976b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::drainQueueAndFreeBuffersLocked() {
9986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t err = drainQueueLocked();
9996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (err == NO_ERROR) {
10006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode) {
10016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeAllBuffersLocked();
10026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else {
10036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeAllBuffersExceptHeadLocked();
10046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
10056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
10066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
10076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
10086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1009fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie GennisBufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1010fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        const wp<BufferQueue::ConsumerListener>& consumerListener):
1011fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        mConsumerListener(consumerListener) {}
1012fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1013fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie GennisBufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1014fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1015fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisvoid BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1016fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1017fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (listener != NULL) {
1018fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        listener->onFrameAvailable();
1019fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
1020fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
1021fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1022fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisvoid BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1023fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1024fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (listener != NULL) {
1025fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        listener->onBuffersReleased();
1026fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
1027fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
1028fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
10296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}; // namespace android
1030