ConsumerBase.cpp revision ca08833d5ea99130797e10ad68a651b50e99da74
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ConsumerBase"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
26#include <hardware/hardware.h>
27
28#include <gui/IGraphicBufferAlloc.h>
29#include <gui/ISurfaceComposer.h>
30#include <gui/SurfaceComposerClient.h>
31#include <gui/ConsumerBase.h>
32
33#include <private/gui/ComposerService.h>
34
35#include <utils/Log.h>
36#include <utils/String8.h>
37#include <utils/Trace.h>
38
39// Macros for including the ConsumerBase name in log messages
40#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
41#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
42#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
43#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
45
46namespace android {
47
48// Get an ID that's unique within this process.
49static int32_t createProcessUniqueId() {
50    static volatile int32_t globalCounter = 0;
51    return android_atomic_inc(&globalCounter);
52}
53
54ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) :
55        mAbandoned(false),
56        mBufferQueue(bufferQueue) {
57    // Choose a name using the PID and a process-unique ID.
58    mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
59
60    // Note that we can't create an sp<...>(this) in a ctor that will not keep a
61    // reference once the ctor ends, as that would cause the refcount of 'this'
62    // dropping to 0 at the end of the ctor.  Since all we need is a wp<...>
63    // that's what we create.
64    wp<BufferQueue::ConsumerListener> listener;
65    sp<BufferQueue::ConsumerListener> proxy;
66    listener = static_cast<BufferQueue::ConsumerListener*>(this);
67    proxy = new BufferQueue::ProxyConsumerListener(listener);
68
69    status_t err = mBufferQueue->consumerConnect(proxy);
70    if (err != NO_ERROR) {
71        CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
72                strerror(-err), err);
73    } else {
74        mBufferQueue->setConsumerName(mName);
75    }
76}
77
78ConsumerBase::~ConsumerBase() {
79	CB_LOGV("~ConsumerBase");
80    abandon();
81}
82
83void ConsumerBase::freeBufferLocked(int slotIndex) {
84    CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
85    mSlots[slotIndex].mGraphicBuffer = 0;
86    mSlots[slotIndex].mFence = Fence::NO_FENCE;
87}
88
89// Used for refactoring, should not be in final interface
90sp<BufferQueue> ConsumerBase::getBufferQueue() const {
91    Mutex::Autolock lock(mMutex);
92    return mBufferQueue;
93}
94
95void ConsumerBase::onFrameAvailable() {
96    CB_LOGV("onFrameAvailable");
97
98    sp<FrameAvailableListener> listener;
99    { // scope for the lock
100        Mutex::Autolock lock(mMutex);
101        listener = mFrameAvailableListener.promote();
102    }
103
104    if (listener != NULL) {
105        CB_LOGV("actually calling onFrameAvailable");
106        listener->onFrameAvailable();
107    }
108}
109
110void ConsumerBase::onBuffersReleased() {
111    Mutex::Autolock lock(mMutex);
112
113    CB_LOGV("onBuffersReleased");
114
115    if (mAbandoned) {
116        // Nothing to do if we're already abandoned.
117        return;
118    }
119
120    uint32_t mask = 0;
121    mBufferQueue->getReleasedBuffers(&mask);
122    for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
123        if (mask & (1 << i)) {
124            freeBufferLocked(i);
125        }
126    }
127}
128
129void ConsumerBase::abandon() {
130    CB_LOGV("abandon");
131    Mutex::Autolock lock(mMutex);
132
133    if (!mAbandoned) {
134        abandonLocked();
135        mAbandoned = true;
136    }
137}
138
139void ConsumerBase::abandonLocked() {
140	CB_LOGV("abandonLocked");
141    for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
142        freeBufferLocked(i);
143    }
144    // disconnect from the BufferQueue
145    mBufferQueue->consumerDisconnect();
146    mBufferQueue.clear();
147}
148
149void ConsumerBase::setFrameAvailableListener(
150        const wp<FrameAvailableListener>& listener) {
151    CB_LOGV("setFrameAvailableListener");
152    Mutex::Autolock lock(mMutex);
153    mFrameAvailableListener = listener;
154}
155
156void ConsumerBase::dump(String8& result) const {
157    char buffer[1024];
158    dump(result, "", buffer, 1024);
159}
160
161void ConsumerBase::dump(String8& result, const char* prefix,
162        char* buffer, size_t size) const {
163    Mutex::Autolock _l(mMutex);
164    dumpLocked(result, prefix, buffer, size);
165}
166
167void ConsumerBase::dumpLocked(String8& result, const char* prefix,
168        char* buffer, size_t SIZE) const {
169    snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned));
170    result.append(buffer);
171
172    if (!mAbandoned) {
173        mBufferQueue->dump(result, prefix, buffer, SIZE);
174    }
175}
176
177status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) {
178    status_t err = mBufferQueue->acquireBuffer(item);
179    if (err != NO_ERROR) {
180        return err;
181    }
182
183    if (item->mGraphicBuffer != NULL) {
184        mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
185    }
186
187    mSlots[item->mBuf].mFence = item->mFence;
188
189    CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
190
191    return OK;
192}
193
194status_t ConsumerBase::addReleaseFence(int slot, const sp<Fence>& fence) {
195    Mutex::Autolock lock(mMutex);
196    return addReleaseFenceLocked(slot, fence);
197}
198
199status_t ConsumerBase::addReleaseFenceLocked(int slot, const sp<Fence>& fence) {
200    CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
201
202    if (!mSlots[slot].mFence.get()) {
203        mSlots[slot].mFence = fence;
204    } else {
205        sp<Fence> mergedFence = Fence::merge(
206                String8::format("%.28s:%d", mName.string(), slot),
207                mSlots[slot].mFence, fence);
208        if (!mergedFence.get()) {
209            CB_LOGE("failed to merge release fences");
210            // synchronization is broken, the best we can do is hope fences
211            // signal in order so the new fence will act like a union
212            mSlots[slot].mFence = fence;
213            return BAD_VALUE;
214        }
215        mSlots[slot].mFence = mergedFence;
216    }
217
218    return OK;
219}
220
221status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display,
222       EGLSyncKHR eglFence) {
223    CB_LOGV("releaseBufferLocked: slot=%d", slot);
224    status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence,
225            mSlots[slot].mFence);
226    if (err == BufferQueue::STALE_BUFFER_SLOT) {
227        freeBufferLocked(slot);
228    }
229
230    mSlots[slot].mFence = Fence::NO_FENCE;
231
232    return err;
233}
234
235} // namespace android
236