BufferQueue.cpp revision 1a4d883dcc1725892bfb5c28dec255a233186524
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)                                            \
60695e331f01b136155b1559b3c878b7c5bb631bc1Jamie Gennis    if (ATRACE_ENABLED()) {                                                   \
61695e331f01b136155b1559b3c878b7c5bb631bc1Jamie Gennis        char ___traceBuf[1024];                                               \
62695e331f01b136155b1559b3c878b7c5bb631bc1Jamie Gennis        snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(),         \
63695e331f01b136155b1559b3c878b7c5bb631bc1Jamie Gennis                (index));                                                     \
64695e331f01b136155b1559b3c878b7c5bb631bc1Jamie Gennis        android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf);           \
65695e331f01b136155b1559b3c878b7c5bb631bc1Jamie Gennis    }
66546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
676b091c53000c843211c218ce40287a7edca9bc63Daniel Lamnamespace android {
686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// Get an ID that's unique within this process.
706b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatic int32_t createProcessUniqueId() {
716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    static volatile int32_t globalCounter = 0;
726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return android_atomic_inc(&globalCounter);
736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
75cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennisstatic const char* scalingModeName(int scalingMode) {
76cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis    switch (scalingMode) {
77cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis        case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
78cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis        case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
79cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis        case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
80cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis        default: return "Unknown";
81cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis    }
82cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis}
83cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis
843e87601170141229d661df93e2f59e1ced73474bMathias AgopianBufferQueue::BufferQueue(bool allowSynchronousMode, int bufferCount,
853e87601170141229d661df93e2f59e1ced73474bMathias Agopian        const sp<IGraphicBufferAlloc>& allocator) :
866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDefaultWidth(1),
876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mDefaultHeight(1),
88abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mMinUndequeuedBuffers(bufferCount),
89abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mMinAsyncBufferSlots(bufferCount + 1),
90abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mMinSyncBufferSlots(bufferCount),
91abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mBufferCount(mMinAsyncBufferSlots),
926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mClientBufferCount(0),
93abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    mServerBufferCount(mMinAsyncBufferSlots),
946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSynchronousMode(false),
956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mAllowSynchronousMode(allowSynchronousMode),
966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mConnectedApi(NO_CONNECTED_API),
976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mAbandoned(false),
98eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mFrameCounter(0),
99b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mBufferHasBeenQueued(false),
1001a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
101b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mConsumerUsageBits(0),
102b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mTransformHint(0)
1036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam{
1046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // Choose a name using the PID and a process-unique ID.
105eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
1066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("BufferQueue");
1083e87601170141229d661df93e2f59e1ced73474bMathias Agopian    if (allocator == NULL) {
1093e87601170141229d661df93e2f59e1ced73474bMathias Agopian        sp<ISurfaceComposer> composer(ComposerService::getComposerService());
1103e87601170141229d661df93e2f59e1ced73474bMathias Agopian        mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
1113e87601170141229d661df93e2f59e1ced73474bMathias Agopian        if (mGraphicBufferAlloc == 0) {
1123e87601170141229d661df93e2f59e1ced73474bMathias Agopian            ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
1133e87601170141229d661df93e2f59e1ced73474bMathias Agopian        }
1143e87601170141229d661df93e2f59e1ced73474bMathias Agopian    } else {
1153e87601170141229d661df93e2f59e1ced73474bMathias Agopian        mGraphicBufferAlloc = allocator;
116abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam    }
1176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1196b091c53000c843211c218ce40287a7edca9bc63Daniel LamBufferQueue::~BufferQueue() {
1206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("~BufferQueue");
1216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1236b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setBufferCountServerLocked(int bufferCount) {
1246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount > NUM_BUFFER_SLOTS)
1256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
1266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1271a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mServerBufferCount = bufferCount;
1281a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (bufferCount == mBufferCount)
1306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return OK;
1316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mClientBufferCount &&
1336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bufferCount >= mBufferCount) {
1346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // easy, we just have more buffers
1356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mBufferCount = bufferCount;
13674ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
1376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } else {
1386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // we're here because we're either
1396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - reducing the number of available buffers
1406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - or there is a client-buffer-count in effect
1416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // less than 2 buffers is never allowed
1436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (bufferCount < 2)
1446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return BAD_VALUE;
1456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // when there is non client-buffer-count in effect, the client is not
1476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // allowed to dequeue more than one buffer at a time,
1486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // so the next time they dequeue a buffer, we know that they don't
1496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // own one. the actual resizing will happen during the next
1506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // dequeueBuffer.
1516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
152b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        mDequeueCondition.broadcast();
1536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
1556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
1566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
157eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lambool BufferQueue::isSynchronousMode() const {
158eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
159eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return mSynchronousMode;
160eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
161eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
162eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::setConsumerName(const String8& name) {
163eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
164eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mConsumerName = name;
165eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
166eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
167b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lamstatus_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
168b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    Mutex::Autolock lock(mMutex);
169b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mDefaultBufferFormat = defaultFormat;
170b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return OK;
171b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam}
172b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
173b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lamstatus_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
174b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    Mutex::Autolock lock(mMutex);
175b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mConsumerUsageBits = usage;
176b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return OK;
177b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam}
178b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
179b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lamstatus_t BufferQueue::setTransformHint(uint32_t hint) {
180b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    Mutex::Autolock lock(mMutex);
181b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mTransformHint = hint;
182b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return OK;
183b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam}
184b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
1856b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setBufferCount(int bufferCount) {
1866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setBufferCount: count=%d", bufferCount);
1876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1889abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    sp<ConsumerListener> listener;
1899abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    {
1909abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        Mutex::Autolock lock(mMutex);
1916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1929abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (mAbandoned) {
1939abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
1949abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return NO_INIT;
1959abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
1969abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (bufferCount > NUM_BUFFER_SLOTS) {
1979abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            ST_LOGE("setBufferCount: bufferCount larger than slots available");
1989abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return BAD_VALUE;
1996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
2006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2019abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // Error out if the user has dequeued buffers
2029abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        for (int i=0 ; i<mBufferCount ; i++) {
2039abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
2049abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                ST_LOGE("setBufferCount: client owns some buffers");
2059abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                return -EINVAL;
2069abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            }
2079abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
2086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2099abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        const int minBufferSlots = mSynchronousMode ?
210abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam            mMinSyncBufferSlots : mMinAsyncBufferSlots;
2119abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (bufferCount == 0) {
2129abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            mClientBufferCount = 0;
2139abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            bufferCount = (mServerBufferCount >= minBufferSlots) ?
2149abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                    mServerBufferCount : minBufferSlots;
2159abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return setBufferCountServerLocked(bufferCount);
2169abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
2179abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
2189abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        if (bufferCount < minBufferSlots) {
2199abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
2209abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam                    "minimum (%d)", bufferCount, minBufferSlots);
2219abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam            return BAD_VALUE;
2229abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        }
2239abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
2249abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // here we're guaranteed that the client doesn't have dequeued buffers
2259abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        // and will release all of its buffer references.
2269abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        freeAllBuffersLocked();
2279abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mBufferCount = bufferCount;
2289abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mClientBufferCount = bufferCount;
2299abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mBufferHasBeenQueued = false;
2309abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mQueue.clear();
2319abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mDequeueCondition.broadcast();
2329abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        listener = mConsumerListener;
2339abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    } // scope for lock
2349abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
2359abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    if (listener != NULL) {
2369abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        listener->onBuffersReleased();
2376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
2406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
2416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
242b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lamint BufferQueue::query(int what, int* outValue)
243b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam{
2441c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
245b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    Mutex::Autolock lock(mMutex);
246b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
247b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    if (mAbandoned) {
248b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        ST_LOGE("query: SurfaceTexture has been abandoned!");
249b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        return NO_INIT;
250b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    }
251b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
252b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    int value;
253b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    switch (what) {
254b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_WIDTH:
255b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mDefaultWidth;
256b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
257b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_HEIGHT:
258b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mDefaultHeight;
259b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
260b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_FORMAT:
2611a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        value = mDefaultBufferFormat;
262b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
263b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
264b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        value = mSynchronousMode ?
265abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                (mMinUndequeuedBuffers-1) : mMinUndequeuedBuffers;
266b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        break;
2672488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian    case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
2682488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian        value = (mQueue.size() >= 2);
2692488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian        break;
270b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    default:
271b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam        return BAD_VALUE;
272b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    }
273b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    outValue[0] = value;
274b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    return NO_ERROR;
275b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam}
276b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
2776b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
2781c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
2796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("requestBuffer: slot=%d", slot);
2806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
2816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
2826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
2836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
2846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (slot < 0 || mBufferCount <= slot) {
2866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
2876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount, slot);
2886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
2896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
2906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[slot].mRequestBufferCalled = true;
2916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    *buf = mSlots[slot].mGraphicBuffer;
2926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return NO_ERROR;
2936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
2946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
295f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hallstatus_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>& outFence,
296f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall        uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
2971c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
2986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
2996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if ((w && !h) || (!w && h)) {
3016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
3026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return BAD_VALUE;
3036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
3046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t returnFlags(OK);
3066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    EGLDisplay dpy = EGL_NO_DISPLAY;
307c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
3086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    { // Scope for the lock
3106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Mutex::Autolock lock(mMutex);
3116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
312b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        if (format == 0) {
313b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam            format = mDefaultBufferFormat;
314b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        }
315b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        // turn on usage bits the consumer requested
316b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam        usage |= mConsumerUsageBits;
317b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
3186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int found = -1;
3196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        int dequeuedCount = 0;
3206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bool tryAgain = true;
3216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        while (tryAgain) {
3226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mAbandoned) {
3236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
3246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return NO_INIT;
3256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // We need to wait for the FIFO to drain if the number of buffer
3286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // needs to change.
3296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
3306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // The condition "number of buffers needs to change" is true if
3316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // - the client doesn't care about how many buffers there are
3326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // - AND the actual number of buffer is different from what was
3336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   set in the last setBufferCountServer()
3346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //                         - OR -
3356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   setBufferCountServer() was set to a value incompatible with
3366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   the synchronization mode (for instance because the sync mode
3376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //   changed since)
3386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
3396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // As long as this condition is true AND the FIFO is not empty, we
3406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // wait on mDequeueCondition.
3416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            const int minBufferCountNeeded = mSynchronousMode ?
343abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                    mMinSyncBufferSlots : mMinAsyncBufferSlots;
3446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
3466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    ((mServerBufferCount != mBufferCount) ||
3476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            (mServerBufferCount < minBufferCountNeeded));
3486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
3506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // wait for the FIFO to drain
3516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.wait(mMutex);
3526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // NOTE: we continue here because we need to reevaluate our
3536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // whole state (eg: we could be abandoned or disconnected)
3546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                continue;
3556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (numberOfBuffersNeedsToChange) {
3586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // here we're guaranteed that mQueue is empty
3596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                freeAllBuffersLocked();
3606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount = mServerBufferCount;
3616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (mBufferCount < minBufferCountNeeded)
3626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mBufferCount = minBufferCountNeeded;
363eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                mBufferHasBeenQueued = false;
3646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
3656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // look for a free buffer to give to the client
3686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            found = INVALID_BUFFER_SLOT;
3696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            dequeuedCount = 0;
3706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            for (int i = 0; i < mBufferCount; i++) {
3716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                const int state = mSlots[i].mBufferState;
3726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                if (state == BufferSlot::DEQUEUED) {
3736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    dequeuedCount++;
3746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
376eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                // this logic used to be if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER)
377eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                // but dequeuing the current buffer is disabled.
378eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                if (false) {
379eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // This functionality has been temporarily removed so
380eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // BufferQueue and SurfaceTexture can be refactored into
381eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    // separate objects
3826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                } else {
3836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    if (state == BufferSlot::FREE) {
3846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        /* We return the oldest of the free buffers to avoid
3856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * stalling the producer if possible.  This is because
3866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * the consumer may still have pending reads of the
3876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         * buffers in flight.
3886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                         */
3896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        bool isOlder = mSlots[i].mFrameNumber <
3906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                                mSlots[found].mFrameNumber;
3916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        if (found < 0 || isOlder) {
3926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            found = i;
3936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        }
3946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    }
3956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
3966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
3976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // clients are not allowed to dequeue more than one buffer
3996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // if they didn't set a buffer count.
4006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (!mClientBufferCount && dequeuedCount) {
4016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
4026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        "setting the buffer count");
4036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return -EINVAL;
4046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // See whether a buffer has been queued since the last
4076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // setBufferCount so we know whether to perform the
408abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam            // mMinUndequeuedBuffers check below.
409eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            if (mBufferHasBeenQueued) {
4106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // make sure the client is not trying to dequeue more buffers
4116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // than allowed.
4126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                const int avail = mBufferCount - (dequeuedCount+1);
413abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                if (avail < (mMinUndequeuedBuffers-int(mSynchronousMode))) {
414abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                    ST_LOGE("dequeueBuffer: mMinUndequeuedBuffers=%d exceeded "
4156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            "(dequeued=%d)",
416abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam                            mMinUndequeuedBuffers-int(mSynchronousMode),
4176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            dequeuedCount);
4186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    return -EBUSY;
4196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                }
4206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
422c2c1f2f24cd70bdcf1958157bab38467bf0fdc71Daniel Lam            // if no buffer is found, wait for a buffer to be released
423c2c1f2f24cd70bdcf1958157bab38467bf0fdc71Daniel Lam            tryAgain = found == INVALID_BUFFER_SLOT;
4246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (tryAgain) {
4256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mDequeueCondition.wait(mMutex);
4266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
4276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (found == INVALID_BUFFER_SLOT) {
4316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // This should not happen.
4326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("dequeueBuffer: no available buffer slots");
4336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EBUSY;
4346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const int buf = found;
4376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        *outBuf = found;
4386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
439546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian        ATRACE_BUFFER_INDEX(buf);
440546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
4416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const bool useDefaultSize = !w && !h;
4426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (useDefaultSize) {
4436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // use the default size
4446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            w = mDefaultWidth;
4456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            h = mDefaultHeight;
4466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // buffer is now in DEQUEUED (but can also be current at the same time,
4496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // if we're in synchronous mode)
4506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
4516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
4536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if ((buffer == NULL) ||
4546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->width)  != w) ||
4556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->height) != h) ||
4566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            (uint32_t(buffer->format) != format) ||
4576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ((uint32_t(buffer->usage) & usage) != usage))
4586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        {
4596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            status_t error;
4606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            sp<GraphicBuffer> graphicBuffer(
4616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mGraphicBufferAlloc->createGraphicBuffer(
4626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                            w, h, format, usage, &error));
4636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (graphicBuffer == 0) {
4646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
4656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        "failed");
4666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                return error;
4676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
468eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
469eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mSlots[buf].mAcquireCalled = false;
4706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mGraphicBuffer = graphicBuffer;
4716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[buf].mRequestBufferCalled = false;
472c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
473c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            mSlots[buf].mFence.clear();
474eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
475eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
4766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
4776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
4786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        dpy = mSlots[buf].mEglDisplay;
480c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        eglFence = mSlots[buf].mEglFence;
481c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        outFence = mSlots[buf].mFence;
482c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
483c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        mSlots[buf].mFence.clear();
484eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }  // end lock scope
4856b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
486c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    if (eglFence != EGL_NO_SYNC_KHR) {
487c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
4886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // If something goes wrong, log the error, but return the buffer without
4896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // synchronizing access to it.  It's too late at this point to abort the
4906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // dequeue operation.
4916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (result == EGL_FALSE) {
492b7a6b96301c00c630610df4cb55a45d666200817Jamie Gennis            ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
4936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
494b7a6b96301c00c630610df4cb55a45d666200817Jamie Gennis            ST_LOGE("dequeueBuffer: timeout waiting for fence");
4956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
496c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        eglDestroySyncKHR(dpy, eglFence);
4976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
4986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf,
5006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
5016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return returnFlags;
5036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
5046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5056b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::setSynchronousMode(bool enabled) {
5061c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
5076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("setSynchronousMode: enabled=%d", enabled);
5086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
5096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
5116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
5126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
5136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t err = OK;
5166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mAllowSynchronousMode && enabled)
5176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return err;
5186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!enabled) {
5206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // going to asynchronous mode, drain the queue
5216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        err = drainQueueLocked();
5226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (err != NO_ERROR)
5236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return err;
5246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mSynchronousMode != enabled) {
5276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - if we're going to asynchronous mode, the queue is guaranteed to be
5286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // empty here
5296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // - if the client set the number of buffers, we're guaranteed that
5306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // we have at least 3 (because we don't allow less)
5316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSynchronousMode = enabled;
53274ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
5336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
5346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
5356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
5366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
537f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopianstatus_t BufferQueue::queueBuffer(int buf,
538f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian        const QueueBufferInput& input, QueueBufferOutput* output) {
5391c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
540546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_BUFFER_INDEX(buf);
541546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
542efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    Rect crop;
543efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    uint32_t transform;
544efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    int scalingMode;
545efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    int64_t timestamp;
546c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    sp<Fence> fence;
547efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis
548c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    input.deflate(&timestamp, &crop, &scalingMode, &transform, &fence);
549efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis
550cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis    ST_LOGV("queueBuffer: slot=%d time=%#llx crop=[%d,%d,%d,%d] tr=%#x "
551cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis            "scale=%s",
552cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis            buf, timestamp, crop.left, crop.top, crop.right, crop.bottom,
553cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis            transform, scalingModeName(scalingMode));
5546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
555fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<ConsumerListener> listener;
5566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    { // scope for the lock
5586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Mutex::Autolock lock(mMutex);
5596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mAbandoned) {
5606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
5616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
5626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (buf < 0 || buf >= mBufferCount) {
5646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
5656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    mBufferCount, buf);
5666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
5686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot %d is not owned by the client "
5696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    "(state=%d)", buf, mSlots[buf].mBufferState);
5706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else if (!mSlots[buf].mRequestBufferCalled) {
5726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
5736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                    "buffer", buf);
5746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return -EINVAL;
5756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
5766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
577d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis        const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
578d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis        Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
579d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis        Rect croppedCrop;
580d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis        crop.intersect(bufferRect, &croppedCrop);
581d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis        if (croppedCrop != crop) {
582d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis            ST_LOGE("queueBuffer: crop rect is not contained within the "
583d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis                    "buffer in slot %d", buf);
584d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis            return -EINVAL;
585d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis        }
586d72f233ffa125856a44976a50a66ceb669f49ab2Jamie Gennis
5876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode) {
5886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // In synchronous mode we queue all buffers in a FIFO.
5896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            mQueue.push_back(buf);
5906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // Synchronous mode always signals that an additional frame should
5926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // be consumed.
593fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            listener = mConsumerListener;
5946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else {
5956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // In asynchronous mode we only keep the most recent buffer.
5966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mQueue.empty()) {
5976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mQueue.push_back(buf);
5986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // Asynchronous mode only signals that a frame should be
6006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // consumed if no previous frame was pending. If a frame were
6016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // pending then the consumer would have already been notified.
602fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                listener = mConsumerListener;
6036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
6046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                Fifo::iterator front(mQueue.begin());
6056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // buffer currently queued is freed
6066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mSlots[*front].mBufferState = BufferSlot::FREE;
6076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                // and we record the new buffer index in the queued list
6086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                *front = buf;
6096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
6106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
6116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
612efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis        mSlots[buf].mTimestamp = timestamp;
613efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis        mSlots[buf].mCrop = crop;
614efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis        mSlots[buf].mTransform = transform;
615c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        mSlots[buf].mFence = fence;
616f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian
617851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        switch (scalingMode) {
618851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian            case NATIVE_WINDOW_SCALING_MODE_FREEZE:
619851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian            case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
620016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam            case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
621851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                break;
622851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian            default:
623851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                ST_LOGE("unknown scaling mode: %d (ignoring)", scalingMode);
624851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                scalingMode = mSlots[buf].mScalingMode;
625851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian                break;
626851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        }
627851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian
6286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mBufferState = BufferSlot::QUEUED;
629851ef8f1bfbb164d61b1528a529a464f0a60dbafMathias Agopian        mSlots[buf].mScalingMode = scalingMode;
6306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mFrameCounter++;
6316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mSlots[buf].mFrameNumber = mFrameCounter;
6326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
633eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mBufferHasBeenQueued = true;
63474ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
6356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6362488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian        output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
6372488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian                mQueue.size());
6381c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
6391c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis        ATRACE_INT(mConsumerName.string(), mQueue.size());
6406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } // scope for the lock
6416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // call back without lock held
6436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (listener != 0) {
6446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        listener->onFrameAvailable();
6456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return OK;
6476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6486b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
649c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hallvoid BufferQueue::cancelBuffer(int buf, sp<Fence> fence) {
6501c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("cancelBuffer: slot=%d", buf);
6526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6556b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
6566b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
6576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (buf < 0 || buf >= mBufferCount) {
6606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
6616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mBufferCount, buf);
6626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
6636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
6646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
6656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                buf, mSlots[buf].mBufferState);
6666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return;
6676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6686b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[buf].mBufferState = BufferSlot::FREE;
6696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mSlots[buf].mFrameNumber = 0;
670c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    mSlots[buf].mFence = fence;
67174ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke    mDequeueCondition.broadcast();
6726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
6736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
67424202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopianstatus_t BufferQueue::connect(int api, QueueBufferOutput* output) {
6751c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("connect: api=%d", api);
6776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Mutex::Autolock lock(mMutex);
6786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
6796b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (mAbandoned) {
6806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        ST_LOGE("connect: BufferQueue has been abandoned!");
6816b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        return NO_INIT;
6826b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
6836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
684fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mConsumerListener == NULL) {
685fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("connect: BufferQueue has no consumer!");
686fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return NO_INIT;
687fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
688fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
6896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int err = NO_ERROR;
6906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    switch (api) {
6916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_EGL:
6926b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CPU:
6936b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_MEDIA:
6946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        case NATIVE_WINDOW_API_CAMERA:
6956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            if (mConnectedApi != NO_CONNECTED_API) {
6966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                ST_LOGE("connect: already connected (cur=%d, req=%d)",
6976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                        mConnectedApi, api);
6986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                err = -EINVAL;
6996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            } else {
7006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                mConnectedApi = api;
7012488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian                output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
7022488b20aec097accb20a853d9876bb0a5dc04636Mathias Agopian                        mQueue.size());
7036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            }
7046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
7056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        default:
7066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            err = -EINVAL;
7076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            break;
7086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
709eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
710eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
711eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
7126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
7136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
7146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
7156b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::disconnect(int api) {
7161c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
7176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ST_LOGV("disconnect: api=%d", api);
7186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
7196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int err = NO_ERROR;
720fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<ConsumerListener> listener;
721fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
722fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    { // Scope for the lock
723fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        Mutex::Autolock lock(mMutex);
724fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
725fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        if (mAbandoned) {
726fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            // it is not really an error to disconnect after the surface
727fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            // has been abandoned, it should just be a no-op.
728fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            return NO_ERROR;
729fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        }
730fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
731fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        switch (api) {
732fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_EGL:
733fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_CPU:
734fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_MEDIA:
735fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            case NATIVE_WINDOW_API_CAMERA:
736fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                if (mConnectedApi == api) {
737fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    drainQueueAndFreeBuffersLocked();
738fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    mConnectedApi = NO_CONNECTED_API;
739fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    mDequeueCondition.broadcast();
740fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    listener = mConsumerListener;
741fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                } else {
742fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
743fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                            mConnectedApi, api);
744fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                    err = -EINVAL;
745fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                }
746fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                break;
747fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            default:
748fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                ST_LOGE("disconnect: unknown API %d", api);
7496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam                err = -EINVAL;
750fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis                break;
751fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        }
7526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
753fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
754fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (listener != NULL) {
755fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        listener->onBuffersReleased();
756fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
757fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
7586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
7596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
7606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
761eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::dump(String8& result) const
762eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
763eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    char buffer[1024];
764eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    BufferQueue::dump(result, "", buffer, 1024);
765eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
766eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
767eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamvoid BufferQueue::dump(String8& result, const char* prefix,
768eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        char* buffer, size_t SIZE) const
769eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
770eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
771eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
772eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    String8 fifo;
773eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    int fifoSize = 0;
774eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Fifo::const_iterator i(mQueue.begin());
775eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    while (i != mQueue.end()) {
776eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       snprintf(buffer, SIZE, "%02d ", *i++);
777eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       fifoSize++;
778eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam       fifo.append(buffer);
779eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
780eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
781eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    snprintf(buffer, SIZE,
782eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            "%s-BufferQueue mBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
7831a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis            "default-format=%d, FIFO(%d)={%s}\n",
784eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
7851a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis            mDefaultHeight, mDefaultBufferFormat, fifoSize, fifo.string());
786eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    result.append(buffer);
787eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
788eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
789eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    struct {
790eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const char * operator()(int state) const {
791eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            switch (state) {
792eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::DEQUEUED: return "DEQUEUED";
793eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::QUEUED: return "QUEUED";
794eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::FREE: return "FREE";
795eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                case BufferSlot::ACQUIRED: return "ACQUIRED";
796eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                default: return "Unknown";
797eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            }
798eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
799eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    } stateName;
800eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
801eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    for (int i=0 ; i<mBufferCount ; i++) {
802eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const BufferSlot& slot(mSlots[i]);
803eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        snprintf(buffer, SIZE,
804eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "%s%s[%02d] "
805eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                "state=%-8s, crop=[%d,%d,%d,%d], "
806cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis                "xform=0x%02x, time=%#llx, scale=%s",
807eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i,
808eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                stateName(slot.mBufferState),
809eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
810cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis                slot.mCrop.bottom, slot.mTransform, slot.mTimestamp,
811cd1806e210f2633423f0fb14d39fa00d03974223Jamie Gennis                scalingModeName(slot.mScalingMode)
812eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        );
813eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        result.append(buffer);
814eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
815eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
816eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        if (buf != NULL) {
817eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            snprintf(buffer, SIZE,
818eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    ", %p [%4ux%4u:%4u,%3X]",
819eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    buf->handle, buf->width, buf->height, buf->stride,
820eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                    buf->format);
821eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            result.append(buffer);
822eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
823eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        result.append("\n");
824eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
825eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
826eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
8271a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid BufferQueue::freeBufferLocked(int slot) {
8281a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    ST_LOGV("freeBufferLocked: slot=%d", slot);
8291a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mSlots[slot].mGraphicBuffer = 0;
8301a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
8311a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mSlots[slot].mNeedsCleanupOnRelease = true;
8329abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    }
8331a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mSlots[slot].mBufferState = BufferSlot::FREE;
8341a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mSlots[slot].mFrameNumber = 0;
8351a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mSlots[slot].mAcquireCalled = false;
836eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
837eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // destroy fence as BufferQueue now takes ownership
8381a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
8391a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
8401a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
8416b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
8421a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mSlots[slot].mFence.clear();
8436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
8446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
8456b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeAllBuffersLocked() {
8466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    ALOGW_IF(!mQueue.isEmpty(),
8476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            "freeAllBuffersLocked called but mQueue is not empty");
848eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mQueue.clear();
849eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
8506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
8516b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        freeBufferLocked(i);
8526b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
8536b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
8546b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
855fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisstatus_t BufferQueue::acquireBuffer(BufferItem *buffer) {
856546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_CALL();
857eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
858eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // check if queue is empty
859eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // In asynchronous mode the list is guaranteed to be one buffer
860eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // deep, while in synchronous mode we use the oldest buffer.
861eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (!mQueue.empty()) {
862eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        Fifo::iterator front(mQueue.begin());
863eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        int buf = *front;
864eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
865546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian        ATRACE_BUFFER_INDEX(buf);
866546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
867eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        if (mSlots[buf].mAcquireCalled) {
868eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            buffer->mGraphicBuffer = NULL;
869fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        } else {
870eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer;
871eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
872eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mCrop = mSlots[buf].mCrop;
873eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mTransform = mSlots[buf].mTransform;
874eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mScalingMode = mSlots[buf].mScalingMode;
875eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mFrameNumber = mSlots[buf].mFrameNumber;
8763fcee50ffaeb745819356e395408b4d7e3239c13Daniel Lam        buffer->mTimestamp = mSlots[buf].mTimestamp;
877eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        buffer->mBuf = buf;
878b42b1ac1587aebda5e2f334d95b620271fafba4eJesse Hall        buffer->mFence = mSlots[buf].mFence;
879eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
8801a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mSlots[buf].mAcquireCalled = true;
8811a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mSlots[buf].mNeedsCleanupOnRelease = false;
882eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
8831a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mSlots[buf].mFence.clear();
8841a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
885eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mQueue.erase(front);
88674ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke        mDequeueCondition.broadcast();
8871c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
8881c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis        ATRACE_INT(mConsumerName.string(), mQueue.size());
889fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    } else {
890fbcda930dd8b2823cfeb160fd0131f5897b7522fDaniel Lam        return NO_BUFFER_AVAILABLE;
891eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
892eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
893eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
894eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
895eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
896eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::releaseBuffer(int buf, EGLDisplay display,
897c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        EGLSyncKHR eglFence, const sp<Fence>& fence) {
898546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_CALL();
899546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian    ATRACE_BUFFER_INDEX(buf);
900546ed2d7d98ce4f1415647913a231a6b4fc6ca66Mathias Agopian
901eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock _l(mMutex);
902eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
903eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (buf == INVALID_BUFFER_SLOT) {
904eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return -EINVAL;
905eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
906eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
907eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[buf].mEglDisplay = display;
908c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    mSlots[buf].mEglFence = eglFence;
909eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mSlots[buf].mFence = fence;
910eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
9119abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    // The buffer can now only be released if its in the acquired state
9129abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
913eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        mSlots[buf].mBufferState = BufferSlot::FREE;
9149abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    } else if (mSlots[buf].mNeedsCleanupOnRelease) {
9159abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
9169abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        mSlots[buf].mNeedsCleanupOnRelease = false;
9179abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        return STALE_BUFFER_SLOT;
9189abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    } else {
9199abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
9209abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        return -EINVAL;
921eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
922eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
9239abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam    mDequeueCondition.broadcast();
924eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
925eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
926eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
927fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisstatus_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) {
928fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("consumerConnect");
929fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    Mutex::Autolock lock(mMutex);
930fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
931fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mAbandoned) {
932fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
933fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return NO_INIT;
934fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
935fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
936fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mConsumerListener = consumerListener;
937fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
938fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    return OK;
939fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
940fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
941eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::consumerDisconnect() {
942fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("consumerDisconnect");
943eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
944b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
945fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mConsumerListener == NULL) {
946fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("consumerDisconnect: No consumer is connected!");
947fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return -EINVAL;
948fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
949b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
950fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mAbandoned = true;
951fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mConsumerListener = NULL;
952b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mQueue.clear();
953eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    freeAllBuffersLocked();
95474ff8c23f44f1546d0c7004302f4c7ba726ff4c0Dave Burke    mDequeueCondition.broadcast();
955eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
956eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
957eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
958fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisstatus_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
959fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("getReleasedBuffers");
960fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    Mutex::Autolock lock(mMutex);
961fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
962fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (mAbandoned) {
963fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
964fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        return NO_INIT;
965fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
966fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
967fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    uint32_t mask = 0;
968fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
969fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        if (!mSlots[i].mAcquireCalled) {
970fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis            mask |= 1 << i;
971fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        }
972fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
973fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    *slotMask = mask;
974fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
975fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
976fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    return NO_ERROR;
977fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
978fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
979eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
980eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam{
981eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
982eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    if (!w || !h) {
983eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
984eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam                w, h);
985eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        return BAD_VALUE;
986eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    }
987eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
988eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
989eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDefaultWidth = w;
990eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mDefaultHeight = h;
991eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return OK;
992eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
993eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
994eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamstatus_t BufferQueue::setBufferCountServer(int bufferCount) {
9951c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
996eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    Mutex::Autolock lock(mMutex);
997eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    return setBufferCountServerLocked(bufferCount);
998eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam}
999eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
10006b091c53000c843211c218ce40287a7edca9bc63Daniel Lamvoid BufferQueue::freeAllBuffersExceptHeadLocked() {
10016b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int head = -1;
10026b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (!mQueue.empty()) {
10036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        Fifo::iterator front(mQueue.begin());
10046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        head = *front;
10056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
1006eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    mBufferHasBeenQueued = false;
10076b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
10086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (i != head) {
10096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeBufferLocked(i);
10106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
10116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
10126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
10136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
10146b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::drainQueueLocked() {
10156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    while (mSynchronousMode && !mQueue.isEmpty()) {
10166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        mDequeueCondition.wait(mMutex);
10176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mAbandoned) {
10186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("drainQueueLocked: BufferQueue has been abandoned!");
10196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
10206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
10216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mConnectedApi == NO_CONNECTED_API) {
10226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            ST_LOGE("drainQueueLocked: BufferQueue is not connected!");
10236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            return NO_INIT;
10246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
10256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
10266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return NO_ERROR;
10276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
10286b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
10296b091c53000c843211c218ce40287a7edca9bc63Daniel Lamstatus_t BufferQueue::drainQueueAndFreeBuffersLocked() {
10306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    status_t err = drainQueueLocked();
10316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    if (err == NO_ERROR) {
10326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        if (mSynchronousMode) {
10336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeAllBuffersLocked();
10346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        } else {
10356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            freeAllBuffersExceptHeadLocked();
10366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
10376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    }
10386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    return err;
10396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}
10406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
1041fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie GennisBufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1042fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        const wp<BufferQueue::ConsumerListener>& consumerListener):
1043fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        mConsumerListener(consumerListener) {}
1044fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1045fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie GennisBufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1046fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1047fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisvoid BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1048fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1049fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (listener != NULL) {
1050fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        listener->onFrameAvailable();
1051fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
1052fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
1053fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1054fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisvoid BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1055fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1056fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    if (listener != NULL) {
1057fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        listener->onBuffersReleased();
1058fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    }
1059fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis}
1060fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
10616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}; // namespace android
1062