ConsumerBase.cpp revision 74d211ae26a0257c6075a823812e40b55aa1e653
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 Mutex::Autolock lock(mMutex); 81 82 // Verify that abandon() has been called before we get here. This should 83 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a 84 // derived class to override that method and not call 85 // ConsumerBase::onLastStrongRef(). 86 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the " 87 "consumer is not abandoned!", mName.string()); 88} 89 90void ConsumerBase::onLastStrongRef(const void* id) { 91 abandon(); 92} 93 94void ConsumerBase::freeBufferLocked(int slotIndex) { 95 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); 96 mSlots[slotIndex].mGraphicBuffer = 0; 97 mSlots[slotIndex].mFence = Fence::NO_FENCE; 98} 99 100// Used for refactoring, should not be in final interface 101sp<BufferQueue> ConsumerBase::getBufferQueue() const { 102 Mutex::Autolock lock(mMutex); 103 return mBufferQueue; 104} 105 106void ConsumerBase::onFrameAvailable() { 107 CB_LOGV("onFrameAvailable"); 108 109 sp<FrameAvailableListener> listener; 110 { // scope for the lock 111 Mutex::Autolock lock(mMutex); 112 listener = mFrameAvailableListener.promote(); 113 } 114 115 if (listener != NULL) { 116 CB_LOGV("actually calling onFrameAvailable"); 117 listener->onFrameAvailable(); 118 } 119} 120 121void ConsumerBase::onBuffersReleased() { 122 Mutex::Autolock lock(mMutex); 123 124 CB_LOGV("onBuffersReleased"); 125 126 if (mAbandoned) { 127 // Nothing to do if we're already abandoned. 128 return; 129 } 130 131 uint32_t mask = 0; 132 mBufferQueue->getReleasedBuffers(&mask); 133 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 134 if (mask & (1 << i)) { 135 freeBufferLocked(i); 136 } 137 } 138} 139 140void ConsumerBase::abandon() { 141 CB_LOGV("abandon"); 142 Mutex::Autolock lock(mMutex); 143 144 if (!mAbandoned) { 145 abandonLocked(); 146 mAbandoned = true; 147 } 148} 149 150void ConsumerBase::abandonLocked() { 151 CB_LOGV("abandonLocked"); 152 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 153 freeBufferLocked(i); 154 } 155 // disconnect from the BufferQueue 156 mBufferQueue->consumerDisconnect(); 157 mBufferQueue.clear(); 158} 159 160void ConsumerBase::setFrameAvailableListener( 161 const wp<FrameAvailableListener>& listener) { 162 CB_LOGV("setFrameAvailableListener"); 163 Mutex::Autolock lock(mMutex); 164 mFrameAvailableListener = listener; 165} 166 167void ConsumerBase::dump(String8& result) const { 168 dump(result, ""); 169} 170 171void ConsumerBase::dump(String8& result, const char* prefix) const { 172 Mutex::Autolock _l(mMutex); 173 dumpLocked(result, prefix); 174} 175 176void ConsumerBase::dumpLocked(String8& result, const char* prefix) const { 177 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned)); 178 179 if (!mAbandoned) { 180 mBufferQueue->dump(result, prefix); 181 } 182} 183 184status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) { 185 status_t err = mBufferQueue->acquireBuffer(item); 186 if (err != NO_ERROR) { 187 return err; 188 } 189 190 if (item->mGraphicBuffer != NULL) { 191 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; 192 } 193 194 mSlots[item->mBuf].mFence = item->mFence; 195 196 CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf); 197 198 return OK; 199} 200 201status_t ConsumerBase::addReleaseFence(int slot, const sp<Fence>& fence) { 202 Mutex::Autolock lock(mMutex); 203 return addReleaseFenceLocked(slot, fence); 204} 205 206status_t ConsumerBase::addReleaseFenceLocked(int slot, const sp<Fence>& fence) { 207 CB_LOGV("addReleaseFenceLocked: slot=%d", slot); 208 209 if (!mSlots[slot].mFence.get()) { 210 mSlots[slot].mFence = fence; 211 } else { 212 sp<Fence> mergedFence = Fence::merge( 213 String8::format("%.28s:%d", mName.string(), slot), 214 mSlots[slot].mFence, fence); 215 if (!mergedFence.get()) { 216 CB_LOGE("failed to merge release fences"); 217 // synchronization is broken, the best we can do is hope fences 218 // signal in order so the new fence will act like a union 219 mSlots[slot].mFence = fence; 220 return BAD_VALUE; 221 } 222 mSlots[slot].mFence = mergedFence; 223 } 224 225 return OK; 226} 227 228status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display, 229 EGLSyncKHR eglFence) { 230 CB_LOGV("releaseBufferLocked: slot=%d", slot); 231 status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence, 232 mSlots[slot].mFence); 233 if (err == BufferQueue::STALE_BUFFER_SLOT) { 234 freeBufferLocked(slot); 235 } 236 237 mSlots[slot].mFence = Fence::NO_FENCE; 238 239 return err; 240} 241 242} // namespace android 243