2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "ConsumerBase"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
21 #define GL_GLEXT_PROTOTYPES
22 #define EGL_EGLEXT_PROTOTYPES
25 #include <EGL/eglext.h>
27 #include <hardware/hardware.h>
29 #include <gui/IGraphicBufferAlloc.h>
30 #include <gui/ISurfaceComposer.h>
31 #include <gui/SurfaceComposerClient.h>
32 #include <gui/ConsumerBase.h>
34 #include <private/gui/ComposerService.h>
36 #include <utils/Log.h>
37 #include <utils/String8.h>
38 #include <utils/Trace.h>
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__)
49 // Get an ID that's unique within this process.
50 static int32_t createProcessUniqueId() {
51 static volatile int32_t globalCounter = 0;
52 return android_atomic_inc(&globalCounter);
55 ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) :
57 mBufferQueue(bufferQueue) {
58 // Choose a name using the PID and a process-unique ID.
59 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
61 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
62 // reference once the ctor ends, as that would cause the refcount of 'this'
63 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
64 // that's what we create.
65 wp<BufferQueue::ConsumerListener> listener;
66 sp<BufferQueue::ConsumerListener> proxy;
67 listener = static_cast<BufferQueue::ConsumerListener*>(this);
68 proxy = new BufferQueue::ProxyConsumerListener(listener);
70 status_t err = mBufferQueue->consumerConnect(proxy);
71 if (err != NO_ERROR) {
72 CB_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)",
75 mBufferQueue->setConsumerName(mName);
79 ConsumerBase::~ConsumerBase() {
80 CB_LOGV("~ConsumerBase");
84 void ConsumerBase::freeBufferLocked(int slotIndex) {
85 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
86 mSlots[slotIndex].mGraphicBuffer = 0;
87 mSlots[slotIndex].mFence = 0;
90 // Used for refactoring, should not be in final interface
91 sp<BufferQueue> ConsumerBase::getBufferQueue() const {
92 Mutex::Autolock lock(mMutex);
96 void ConsumerBase::onFrameAvailable() {
97 CB_LOGV("onFrameAvailable");
99 sp<FrameAvailableListener> listener;
100 { // scope for the lock
101 Mutex::Autolock lock(mMutex);
102 listener = mFrameAvailableListener;
105 if (listener != NULL) {
106 CB_LOGV("actually calling onFrameAvailable");
107 listener->onFrameAvailable();
111 void ConsumerBase::onBuffersReleased() {
112 Mutex::Autolock lock(mMutex);
114 CB_LOGV("onBuffersReleased");
117 // Nothing to do if we're already abandoned.
122 mBufferQueue->getReleasedBuffers(&mask);
123 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
124 if (mask & (1 << i)) {
130 void ConsumerBase::abandon() {
132 Mutex::Autolock lock(mMutex);
140 void ConsumerBase::abandonLocked() {
141 CB_LOGV("abandonLocked");
142 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
145 // disconnect from the BufferQueue
146 mBufferQueue->consumerDisconnect();
147 mBufferQueue.clear();
150 void ConsumerBase::setFrameAvailableListener(
151 const sp<FrameAvailableListener>& listener) {
152 CB_LOGV("setFrameAvailableListener");
153 Mutex::Autolock lock(mMutex);
154 mFrameAvailableListener = listener;
157 void ConsumerBase::dump(String8& result) const {
159 dump(result, "", buffer, 1024);
162 void ConsumerBase::dump(String8& result, const char* prefix,
163 char* buffer, size_t size) const {
164 Mutex::Autolock _l(mMutex);
165 dumpLocked(result, prefix, buffer, size);
168 void ConsumerBase::dumpLocked(String8& result, const char* prefix,
169 char* buffer, size_t SIZE) const {
170 snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned));
171 result.append(buffer);
174 mBufferQueue->dump(result, prefix, buffer, SIZE);
178 status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) {
179 status_t err = mBufferQueue->acquireBuffer(item);
180 if (err != NO_ERROR) {
184 if (item->mGraphicBuffer != NULL) {
185 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
188 CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
193 status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display,
194 EGLSyncKHR eglFence, const sp<Fence>& fence) {
195 CB_LOGV("releaseBufferLocked: slot=%d", slot);
196 status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence, fence);
197 if (err == BufferQueue::STALE_BUFFER_SLOT) {
198 freeBufferLocked(slot);
201 mSlots[slot].mFence.clear();
206 } // namespace android