ConsumerBase.cpp revision dc13c5b85b099050c73297a19f1ef89308f7620b
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#include <inttypes.h> 18 19#define LOG_TAG "ConsumerBase" 20#define ATRACE_TAG ATRACE_TAG_GRAPHICS 21//#define LOG_NDEBUG 0 22 23#define EGL_EGLEXT_PROTOTYPES 24 25#include <EGL/egl.h> 26#include <EGL/eglext.h> 27 28#include <hardware/hardware.h> 29 30#include <gui/BufferItem.h> 31#include <gui/IGraphicBufferAlloc.h> 32#include <gui/ISurfaceComposer.h> 33#include <gui/SurfaceComposerClient.h> 34#include <gui/ConsumerBase.h> 35 36#include <private/gui/ComposerService.h> 37 38#include <utils/Log.h> 39#include <utils/String8.h> 40#include <utils/Trace.h> 41 42// Macros for including the ConsumerBase name in log messages 43#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) 44//#define CB_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) 45//#define CB_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) 46//#define CB_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) 47#define CB_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) 48 49namespace android { 50 51// Get an ID that's unique within this process. 52static int32_t createProcessUniqueId() { 53 static volatile int32_t globalCounter = 0; 54 return android_atomic_inc(&globalCounter); 55} 56 57ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) : 58 mAbandoned(false), 59 mConsumer(bufferQueue) { 60 // Choose a name using the PID and a process-unique ID. 61 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); 62 63 // Note that we can't create an sp<...>(this) in a ctor that will not keep a 64 // reference once the ctor ends, as that would cause the refcount of 'this' 65 // dropping to 0 at the end of the ctor. Since all we need is a wp<...> 66 // that's what we create. 67 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this); 68 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener); 69 70 status_t err = mConsumer->consumerConnect(proxy, controlledByApp); 71 if (err != NO_ERROR) { 72 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)", 73 strerror(-err), err); 74 } else { 75 mConsumer->setConsumerName(mName); 76 } 77} 78 79ConsumerBase::~ConsumerBase() { 80 CB_LOGV("~ConsumerBase"); 81 Mutex::Autolock lock(mMutex); 82 83 // Verify that abandon() has been called before we get here. This should 84 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a 85 // derived class to override that method and not call 86 // ConsumerBase::onLastStrongRef(). 87 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the " 88 "consumer is not abandoned!", mName.string()); 89} 90 91void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) { 92 abandon(); 93} 94 95void ConsumerBase::freeBufferLocked(int slotIndex) { 96 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); 97 mSlots[slotIndex].mGraphicBuffer = 0; 98 mSlots[slotIndex].mFence = Fence::NO_FENCE; 99 mSlots[slotIndex].mFrameNumber = 0; 100} 101 102void ConsumerBase::onFrameAvailable(const BufferItem& item) { 103 CB_LOGV("onFrameAvailable"); 104 105 sp<FrameAvailableListener> listener; 106 { // scope for the lock 107 Mutex::Autolock lock(mMutex); 108 listener = mFrameAvailableListener.promote(); 109 } 110 111 if (listener != NULL) { 112 CB_LOGV("actually calling onFrameAvailable"); 113 listener->onFrameAvailable(item); 114 } 115} 116 117void ConsumerBase::onFrameReplaced(const BufferItem &item) { 118 CB_LOGV("onFrameReplaced"); 119 120 sp<FrameAvailableListener> listener; 121 { 122 Mutex::Autolock lock(mMutex); 123 listener = mFrameAvailableListener.promote(); 124 } 125 126 if (listener != NULL) { 127 CB_LOGV("actually calling onFrameReplaced"); 128 listener->onFrameReplaced(item); 129 } 130} 131 132void ConsumerBase::onBuffersReleased() { 133 Mutex::Autolock lock(mMutex); 134 135 CB_LOGV("onBuffersReleased"); 136 137 if (mAbandoned) { 138 // Nothing to do if we're already abandoned. 139 return; 140 } 141 142 uint64_t mask = 0; 143 mConsumer->getReleasedBuffers(&mask); 144 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 145 if (mask & (1ULL << i)) { 146 freeBufferLocked(i); 147 } 148 } 149} 150 151void ConsumerBase::onSidebandStreamChanged() { 152} 153 154void ConsumerBase::abandon() { 155 CB_LOGV("abandon"); 156 Mutex::Autolock lock(mMutex); 157 158 if (!mAbandoned) { 159 abandonLocked(); 160 mAbandoned = true; 161 } 162} 163 164void ConsumerBase::abandonLocked() { 165 CB_LOGV("abandonLocked"); 166 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 167 freeBufferLocked(i); 168 } 169 // disconnect from the BufferQueue 170 mConsumer->consumerDisconnect(); 171 mConsumer.clear(); 172} 173 174void ConsumerBase::setFrameAvailableListener( 175 const wp<FrameAvailableListener>& listener) { 176 CB_LOGV("setFrameAvailableListener"); 177 Mutex::Autolock lock(mMutex); 178 mFrameAvailableListener = listener; 179} 180 181status_t ConsumerBase::detachBuffer(int slot) { 182 CB_LOGV("detachBuffer"); 183 Mutex::Autolock lock(mMutex); 184 185 status_t result = mConsumer->detachBuffer(slot); 186 if (result != NO_ERROR) { 187 CB_LOGE("Failed to detach buffer: %d", result); 188 return result; 189 } 190 191 freeBufferLocked(slot); 192 193 return result; 194} 195 196void ConsumerBase::dump(String8& result) const { 197 dump(result, ""); 198} 199 200void ConsumerBase::dump(String8& result, const char* prefix) const { 201 Mutex::Autolock _l(mMutex); 202 dumpLocked(result, prefix); 203} 204 205void ConsumerBase::dumpLocked(String8& result, const char* prefix) const { 206 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned)); 207 208 if (!mAbandoned) { 209 mConsumer->dump(result, prefix); 210 } 211} 212 213status_t ConsumerBase::acquireBufferLocked(BufferItem *item, 214 nsecs_t presentWhen) { 215 status_t err = mConsumer->acquireBuffer(item, presentWhen); 216 if (err != NO_ERROR) { 217 return err; 218 } 219 220 if (item->mGraphicBuffer != NULL) { 221 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; 222 } 223 224 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber; 225 mSlots[item->mBuf].mFence = item->mFence; 226 227 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64, 228 item->mBuf, item->mFrameNumber); 229 230 return OK; 231} 232 233status_t ConsumerBase::addReleaseFence(int slot, 234 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { 235 Mutex::Autolock lock(mMutex); 236 return addReleaseFenceLocked(slot, graphicBuffer, fence); 237} 238 239status_t ConsumerBase::addReleaseFenceLocked(int slot, 240 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { 241 CB_LOGV("addReleaseFenceLocked: slot=%d", slot); 242 243 // If consumer no longer tracks this graphicBuffer, we can safely 244 // drop this fence, as it will never be received by the producer. 245 if (!stillTracking(slot, graphicBuffer)) { 246 return OK; 247 } 248 249 if (!mSlots[slot].mFence.get()) { 250 mSlots[slot].mFence = fence; 251 } else { 252 sp<Fence> mergedFence = Fence::merge( 253 String8::format("%.28s:%d", mName.string(), slot), 254 mSlots[slot].mFence, fence); 255 if (!mergedFence.get()) { 256 CB_LOGE("failed to merge release fences"); 257 // synchronization is broken, the best we can do is hope fences 258 // signal in order so the new fence will act like a union 259 mSlots[slot].mFence = fence; 260 return BAD_VALUE; 261 } 262 mSlots[slot].mFence = mergedFence; 263 } 264 265 return OK; 266} 267 268status_t ConsumerBase::releaseBufferLocked( 269 int slot, const sp<GraphicBuffer> graphicBuffer, 270 EGLDisplay display, EGLSyncKHR eglFence) { 271 // If consumer no longer tracks this graphicBuffer (we received a new 272 // buffer on the same slot), the buffer producer is definitely no longer 273 // tracking it. 274 if (!stillTracking(slot, graphicBuffer)) { 275 return OK; 276 } 277 278 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64, 279 slot, mSlots[slot].mFrameNumber); 280 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber, 281 display, eglFence, mSlots[slot].mFence); 282 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) { 283 freeBufferLocked(slot); 284 } 285 286 mSlots[slot].mFence = Fence::NO_FENCE; 287 288 return err; 289} 290 291bool ConsumerBase::stillTracking(int slot, 292 const sp<GraphicBuffer> graphicBuffer) { 293 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) { 294 return false; 295 } 296 return (mSlots[slot].mGraphicBuffer != NULL && 297 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle); 298} 299 300} // namespace android 301