ConsumerBase.cpp revision c5d7b7d323bba8772a9005f7d300ad983a04733a
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 mSlots[slotIndex].mFrameNumber = 0; 99} 100 101// Used for refactoring, should not be in final interface 102sp<BufferQueue> ConsumerBase::getBufferQueue() const { 103 Mutex::Autolock lock(mMutex); 104 return mBufferQueue; 105} 106 107void ConsumerBase::onFrameAvailable() { 108 CB_LOGV("onFrameAvailable"); 109 110 sp<FrameAvailableListener> listener; 111 { // scope for the lock 112 Mutex::Autolock lock(mMutex); 113 listener = mFrameAvailableListener.promote(); 114 } 115 116 if (listener != NULL) { 117 CB_LOGV("actually calling onFrameAvailable"); 118 listener->onFrameAvailable(); 119 } 120} 121 122void ConsumerBase::onBuffersReleased() { 123 Mutex::Autolock lock(mMutex); 124 125 CB_LOGV("onBuffersReleased"); 126 127 if (mAbandoned) { 128 // Nothing to do if we're already abandoned. 129 return; 130 } 131 132 uint32_t mask = 0; 133 mBufferQueue->getReleasedBuffers(&mask); 134 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 135 if (mask & (1 << i)) { 136 freeBufferLocked(i); 137 } 138 } 139} 140 141void ConsumerBase::abandon() { 142 CB_LOGV("abandon"); 143 Mutex::Autolock lock(mMutex); 144 145 if (!mAbandoned) { 146 abandonLocked(); 147 mAbandoned = true; 148 } 149} 150 151void ConsumerBase::abandonLocked() { 152 CB_LOGV("abandonLocked"); 153 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 154 freeBufferLocked(i); 155 } 156 // disconnect from the BufferQueue 157 mBufferQueue->consumerDisconnect(); 158 mBufferQueue.clear(); 159} 160 161void ConsumerBase::setFrameAvailableListener( 162 const wp<FrameAvailableListener>& listener) { 163 CB_LOGV("setFrameAvailableListener"); 164 Mutex::Autolock lock(mMutex); 165 mFrameAvailableListener = listener; 166} 167 168void ConsumerBase::dump(String8& result) const { 169 dump(result, ""); 170} 171 172void ConsumerBase::dump(String8& result, const char* prefix) const { 173 Mutex::Autolock _l(mMutex); 174 dumpLocked(result, prefix); 175} 176 177void ConsumerBase::dumpLocked(String8& result, const char* prefix) const { 178 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned)); 179 180 if (!mAbandoned) { 181 mBufferQueue->dump(result, prefix); 182 } 183} 184 185status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) { 186 status_t err = mBufferQueue->acquireBuffer(item); 187 if (err != NO_ERROR) { 188 return err; 189 } 190 191 if (item->mGraphicBuffer != NULL) { 192 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; 193 } 194 195 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber; 196 mSlots[item->mBuf].mFence = item->mFence; 197 198 CB_LOGV("acquireBufferLocked: -> slot=%d/%llu", 199 item->mBuf, item->mFrameNumber); 200 201 return OK; 202} 203 204status_t ConsumerBase::addReleaseFence(int slot, 205 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { 206 Mutex::Autolock lock(mMutex); 207 return addReleaseFenceLocked(slot, graphicBuffer, fence); 208} 209 210status_t ConsumerBase::addReleaseFenceLocked(int slot, 211 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { 212 CB_LOGV("addReleaseFenceLocked: slot=%d", slot); 213 214 // If consumer no longer tracks this graphicBuffer, we can safely 215 // drop this fence, as it will never be received by the producer. 216 if (!stillTracking(slot, graphicBuffer)) { 217 return OK; 218 } 219 220 if (!mSlots[slot].mFence.get()) { 221 mSlots[slot].mFence = fence; 222 } else { 223 sp<Fence> mergedFence = Fence::merge( 224 String8::format("%.28s:%d", mName.string(), slot), 225 mSlots[slot].mFence, fence); 226 if (!mergedFence.get()) { 227 CB_LOGE("failed to merge release fences"); 228 // synchronization is broken, the best we can do is hope fences 229 // signal in order so the new fence will act like a union 230 mSlots[slot].mFence = fence; 231 return BAD_VALUE; 232 } 233 mSlots[slot].mFence = mergedFence; 234 } 235 236 return OK; 237} 238 239status_t ConsumerBase::releaseBufferLocked( 240 int slot, const sp<GraphicBuffer> graphicBuffer, 241 EGLDisplay display, EGLSyncKHR eglFence) { 242 // If consumer no longer tracks this graphicBuffer (we received a new 243 // buffer on the same slot), the buffer producer is definitely no longer 244 // tracking it. 245 if (!stillTracking(slot, graphicBuffer)) { 246 return OK; 247 } 248 249 CB_LOGV("releaseBufferLocked: slot=%d/%llu", 250 slot, mSlots[slot].mFrameNumber); 251 status_t err = mBufferQueue->releaseBuffer(slot, mSlots[slot].mFrameNumber, 252 display, eglFence, mSlots[slot].mFence); 253 if (err == BufferQueue::STALE_BUFFER_SLOT) { 254 freeBufferLocked(slot); 255 } 256 257 mSlots[slot].mFence = Fence::NO_FENCE; 258 259 return err; 260} 261 262bool ConsumerBase::stillTracking(int slot, 263 const sp<GraphicBuffer> graphicBuffer) { 264 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) { 265 return false; 266 } 267 return (mSlots[slot].mGraphicBuffer != NULL && 268 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle); 269} 270 271} // namespace android 272