11a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis/*
21a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * Copyright (C) 2010 The Android Open Source Project
31a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis *
41a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * Licensed under the Apache License, Version 2.0 (the "License");
51a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * you may not use this file except in compliance with the License.
61a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * You may obtain a copy of the License at
71a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis *
81a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis *      http://www.apache.org/licenses/LICENSE-2.0
91a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis *
101a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * Unless required by applicable law or agreed to in writing, software
111a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * distributed under the License is distributed on an "AS IS" BASIS,
121a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * See the License for the specific language governing permissions and
141a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis * limitations under the License.
151a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis */
161a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
17911004506dcb6ee68efdfd6636e0ffc72e6972b8Mark Salyzyn#include <inttypes.h>
18911004506dcb6ee68efdfd6636e0ffc72e6972b8Mark Salyzyn
191a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define LOG_TAG "ConsumerBase"
201a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
211a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis//#define LOG_NDEBUG 0
221a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
231a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define EGL_EGLEXT_PROTOTYPES
241a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
251a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <EGL/egl.h>
261a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <EGL/eglext.h>
271a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
281a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <hardware/hardware.h>
291a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
301a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <gui/IGraphicBufferAlloc.h>
311a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <gui/ISurfaceComposer.h>
321a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <gui/SurfaceComposerClient.h>
331a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <gui/ConsumerBase.h>
341a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
351a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <private/gui/ComposerService.h>
361a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
371a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <utils/Log.h>
381a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <utils/String8.h>
391a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <utils/Trace.h>
401a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
411a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis// Macros for including the ConsumerBase name in log messages
421a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
431a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
441a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
451a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
461a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
471a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
481a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisnamespace android {
491a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
501a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis// Get an ID that's unique within this process.
511a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisstatic int32_t createProcessUniqueId() {
521a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    static volatile int32_t globalCounter = 0;
531a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    return android_atomic_inc(&globalCounter);
541a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
551a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
56db89edc94bd2a78226b407f9f7261e202e7fa325Mathias AgopianConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
579fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis        mAbandoned(false),
58db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian        mConsumer(bufferQueue) {
591a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    // Choose a name using the PID and a process-unique ID.
601a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
611a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
621a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    // Note that we can't create an sp<...>(this) in a ctor that will not keep a
631a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    // reference once the ctor ends, as that would cause the refcount of 'this'
641a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    // dropping to 0 at the end of the ctor.  Since all we need is a wp<...>
651a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    // that's what we create.
66a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
67a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
681a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
69db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
701a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (err != NO_ERROR) {
712adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden        CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
721a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis                strerror(-err), err);
731a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    } else {
74db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian        mConsumer->setConsumerName(mName);
751a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
761a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
771a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
781a4d883dcc1725892bfb5c28dec255a233186524Jamie GennisConsumerBase::~ConsumerBase() {
79ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    CB_LOGV("~ConsumerBase");
80ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    Mutex::Autolock lock(mMutex);
81ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis
82ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    // Verify that abandon() has been called before we get here.  This should
83ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
84ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    // derived class to override that method and not call
85ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    // ConsumerBase::onLastStrongRef().
86ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis    LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
87ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis        "consumer is not abandoned!", mName.string());
88ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis}
89ad669b04f4633957eea55b8ad2d8253adcefe39bJamie Gennis
907d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkinvoid ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
911a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    abandon();
921a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
931a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
941a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid ConsumerBase::freeBufferLocked(int slotIndex) {
951a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
961a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mSlots[slotIndex].mGraphicBuffer = 0;
971df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    mSlots[slotIndex].mFence = Fence::NO_FENCE;
98c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    mSlots[slotIndex].mFrameNumber = 0;
991a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1001a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1018dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stozavoid ConsumerBase::onFrameAvailable(const BufferItem& item) {
1021a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    CB_LOGV("onFrameAvailable");
1031a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1041a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    sp<FrameAvailableListener> listener;
1051a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    { // scope for the lock
1061a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        Mutex::Autolock lock(mMutex);
107a4a3149a36bc69a06e4824aeae909ab910661070Igor Murashkin        listener = mFrameAvailableListener.promote();
1081a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1091a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1101a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (listener != NULL) {
1111a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        CB_LOGV("actually calling onFrameAvailable");
1128dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza        listener->onFrameAvailable(item);
1131a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1141a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1151a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1161a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid ConsumerBase::onBuffersReleased() {
11772c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis    Mutex::Autolock lock(mMutex);
118b21a4e3b5f7f07ed160ca6e1809313e2a8e2a6a4Jamie Gennis
11972c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis    CB_LOGV("onBuffersReleased");
1201a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
12172c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis    if (mAbandoned) {
12272c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis        // Nothing to do if we're already abandoned.
12372c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis        return;
1241a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1251a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
126febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza    uint64_t mask = 0;
127db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer->getReleasedBuffers(&mask);
1281a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
129febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza        if (mask & (1ULL << i)) {
13072c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis            freeBufferLocked(i);
13172c3f7d88160b7c279f90f0efe3c1cb12cd140aeJamie Gennis        }
1321a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1331a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1341a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
135399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hallvoid ConsumerBase::onSidebandStreamChanged() {
136399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall}
137399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall
1381a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid ConsumerBase::abandon() {
1391a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    CB_LOGV("abandon");
1401a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    Mutex::Autolock lock(mMutex);
1411a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1421a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (!mAbandoned) {
1431a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        abandonLocked();
1441a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mAbandoned = true;
1451a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1461a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1471a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1481a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid ConsumerBase::abandonLocked() {
1491a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis	CB_LOGV("abandonLocked");
1501a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
1511a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        freeBufferLocked(i);
1521a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1531a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    // disconnect from the BufferQueue
154db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer->consumerDisconnect();
155db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer.clear();
1561a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1571a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1581a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid ConsumerBase::setFrameAvailableListener(
159a4a3149a36bc69a06e4824aeae909ab910661070Igor Murashkin        const wp<FrameAvailableListener>& listener) {
1601a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    CB_LOGV("setFrameAvailableListener");
1611a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    Mutex::Autolock lock(mMutex);
1621a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    mFrameAvailableListener = listener;
1631a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1641a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1651a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennisvoid ConsumerBase::dump(String8& result) const {
16674d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    dump(result, "");
1671a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1681a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
16974d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid ConsumerBase::dump(String8& result, const char* prefix) const {
1701a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    Mutex::Autolock _l(mMutex);
17174d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    dumpLocked(result, prefix);
1721a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1731a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
17474d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
17574d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
1761a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1771a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (!mAbandoned) {
178db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian        mConsumer->dump(result, prefix);
1791a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1801a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
1811a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1821585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFaddenstatus_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
1831585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        nsecs_t presentWhen) {
184db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    status_t err = mConsumer->acquireBuffer(item, presentWhen);
1851a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (err != NO_ERROR) {
1861a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        return err;
1871a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1881a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1891a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    if (item->mGraphicBuffer != NULL) {
1901a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
1911a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
1921a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
193c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
194b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis    mSlots[item->mBuf].mFence = item->mFence;
195b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis
196911004506dcb6ee68efdfd6636e0ffc72e6972b8Mark Salyzyn    CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
197c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            item->mBuf, item->mFrameNumber);
1981a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
1991a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    return OK;
2001a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
2011a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
202c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnarstatus_t ConsumerBase::addReleaseFence(int slot,
203c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
2049504eb915c9628e130f45019bdefda0168089886Jesse Hall    Mutex::Autolock lock(mMutex);
205c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    return addReleaseFenceLocked(slot, graphicBuffer, fence);
2069504eb915c9628e130f45019bdefda0168089886Jesse Hall}
2079504eb915c9628e130f45019bdefda0168089886Jesse Hall
208c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnarstatus_t ConsumerBase::addReleaseFenceLocked(int slot,
209c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
2109504eb915c9628e130f45019bdefda0168089886Jesse Hall    CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
211b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis
212c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // If consumer no longer tracks this graphicBuffer, we can safely
213c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // drop this fence, as it will never be received by the producer.
214c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    if (!stillTracking(slot, graphicBuffer)) {
215c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        return OK;
216c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    }
217c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar
218b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis    if (!mSlots[slot].mFence.get()) {
219b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis        mSlots[slot].mFence = fence;
220b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis    } else {
221b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis        sp<Fence> mergedFence = Fence::merge(
2227aff4a5de47bf32b0934f5744cd1df4ce666d2d2Jamie Gennis                String8::format("%.28s:%d", mName.string(), slot),
223b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis                mSlots[slot].mFence, fence);
224b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis        if (!mergedFence.get()) {
225b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis            CB_LOGE("failed to merge release fences");
226b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis            // synchronization is broken, the best we can do is hope fences
227b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis            // signal in order so the new fence will act like a union
228b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis            mSlots[slot].mFence = fence;
229b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis            return BAD_VALUE;
230b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis        }
231b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis        mSlots[slot].mFence = mergedFence;
232b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis    }
233b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis
234b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis    return OK;
235b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis}
236b27254154642575dfb4bbfa79fbedde7d7ee23ddJamie Gennis
237c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnarstatus_t ConsumerBase::releaseBufferLocked(
238c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        int slot, const sp<GraphicBuffer> graphicBuffer,
239c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        EGLDisplay display, EGLSyncKHR eglFence) {
240c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // If consumer no longer tracks this graphicBuffer (we received a new
241c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // buffer on the same slot), the buffer producer is definitely no longer
242c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // tracking it.
243c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    if (!stillTracking(slot, graphicBuffer)) {
244c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        return OK;
245c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    }
246c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar
247911004506dcb6ee68efdfd6636e0ffc72e6972b8Mark Salyzyn    CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
248c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            slot, mSlots[slot].mFrameNumber);
249db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
250c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            display, eglFence, mSlots[slot].mFence);
2517d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
2521a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis        freeBufferLocked(slot);
2531a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    }
2541a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
2551df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    mSlots[slot].mFence = Fence::NO_FENCE;
2561a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
2571a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis    return err;
2581a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis}
2591a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis
260c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnarbool ConsumerBase::stillTracking(int slot,
261c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        const sp<GraphicBuffer> graphicBuffer) {
262c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
263c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        return false;
264c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    }
265c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    return (mSlots[slot].mGraphicBuffer != NULL &&
266c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
267c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar}
268c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar
2692adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden} // namespace android
270