ConsumerBase.cpp revision 1a4d883dcc1725892bfb5c28dec255a233186524
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 GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26
27#include <hardware/hardware.h>
28
29#include <gui/IGraphicBufferAlloc.h>
30#include <gui/ISurfaceComposer.h>
31#include <gui/SurfaceComposerClient.h>
32#include <gui/ConsumerBase.h>
33
34#include <private/gui/ComposerService.h>
35
36#include <utils/Log.h>
37#include <utils/String8.h>
38#include <utils/Trace.h>
39
40// Macros for including the ConsumerBase name in log messages
41#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
42#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
43#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
45#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
46
47namespace android {
48
49// Get an ID that's unique within this process.
50static int32_t createProcessUniqueId() {
51    static volatile int32_t globalCounter = 0;
52    return android_atomic_inc(&globalCounter);
53}
54
55ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) :
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("SurfaceTexture: 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 = 0;
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;
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 sp<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    CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
188
189    return OK;
190}
191
192status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display,
193       EGLSyncKHR eglFence, const sp<Fence>& fence) {
194    CB_LOGV("releaseBufferLocked: slot=%d", slot);
195    status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence, fence);
196    if (err == BufferQueue::STALE_BUFFER_SLOT) {
197        freeBufferLocked(slot);
198    }
199
200    mSlots[slot].mFence.clear();
201
202    return err;
203}
204
205} // namespace android