ConsumerBase.cpp revision febd4f4f462444bfcb3f0618d07ac77e3fc1f6ad
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<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) : 55 mAbandoned(false), 56 mConsumer(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<ConsumerListener> listener = static_cast<ConsumerListener*>(this); 65 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener); 66 67 status_t err = mConsumer->consumerConnect(proxy, controlledByApp); 68 if (err != NO_ERROR) { 69 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)", 70 strerror(-err), err); 71 } else { 72 mConsumer->setConsumerName(mName); 73 } 74} 75 76ConsumerBase::~ConsumerBase() { 77 CB_LOGV("~ConsumerBase"); 78 Mutex::Autolock lock(mMutex); 79 80 // Verify that abandon() has been called before we get here. This should 81 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a 82 // derived class to override that method and not call 83 // ConsumerBase::onLastStrongRef(). 84 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the " 85 "consumer is not abandoned!", mName.string()); 86} 87 88void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) { 89 abandon(); 90} 91 92void ConsumerBase::freeBufferLocked(int slotIndex) { 93 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); 94 mSlots[slotIndex].mGraphicBuffer = 0; 95 mSlots[slotIndex].mFence = Fence::NO_FENCE; 96 mSlots[slotIndex].mFrameNumber = 0; 97} 98 99void ConsumerBase::onFrameAvailable() { 100 CB_LOGV("onFrameAvailable"); 101 102 sp<FrameAvailableListener> listener; 103 { // scope for the lock 104 Mutex::Autolock lock(mMutex); 105 listener = mFrameAvailableListener.promote(); 106 } 107 108 if (listener != NULL) { 109 CB_LOGV("actually calling onFrameAvailable"); 110 listener->onFrameAvailable(); 111 } 112} 113 114void ConsumerBase::onBuffersReleased() { 115 Mutex::Autolock lock(mMutex); 116 117 CB_LOGV("onBuffersReleased"); 118 119 if (mAbandoned) { 120 // Nothing to do if we're already abandoned. 121 return; 122 } 123 124 uint64_t mask = 0; 125 mConsumer->getReleasedBuffers(&mask); 126 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 127 if (mask & (1ULL << i)) { 128 freeBufferLocked(i); 129 } 130 } 131} 132 133void ConsumerBase::onSidebandStreamChanged() { 134} 135 136void ConsumerBase::abandon() { 137 CB_LOGV("abandon"); 138 Mutex::Autolock lock(mMutex); 139 140 if (!mAbandoned) { 141 abandonLocked(); 142 mAbandoned = true; 143 } 144} 145 146void ConsumerBase::abandonLocked() { 147 CB_LOGV("abandonLocked"); 148 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 149 freeBufferLocked(i); 150 } 151 // disconnect from the BufferQueue 152 mConsumer->consumerDisconnect(); 153 mConsumer.clear(); 154} 155 156void ConsumerBase::setFrameAvailableListener( 157 const wp<FrameAvailableListener>& listener) { 158 CB_LOGV("setFrameAvailableListener"); 159 Mutex::Autolock lock(mMutex); 160 mFrameAvailableListener = listener; 161} 162 163void ConsumerBase::dump(String8& result) const { 164 dump(result, ""); 165} 166 167void ConsumerBase::dump(String8& result, const char* prefix) const { 168 Mutex::Autolock _l(mMutex); 169 dumpLocked(result, prefix); 170} 171 172void ConsumerBase::dumpLocked(String8& result, const char* prefix) const { 173 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned)); 174 175 if (!mAbandoned) { 176 mConsumer->dump(result, prefix); 177 } 178} 179 180status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item, 181 nsecs_t presentWhen) { 182 status_t err = mConsumer->acquireBuffer(item, presentWhen); 183 if (err != NO_ERROR) { 184 return err; 185 } 186 187 if (item->mGraphicBuffer != NULL) { 188 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; 189 } 190 191 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber; 192 mSlots[item->mBuf].mFence = item->mFence; 193 194 CB_LOGV("acquireBufferLocked: -> slot=%d/%llu", 195 item->mBuf, item->mFrameNumber); 196 197 return OK; 198} 199 200status_t ConsumerBase::addReleaseFence(int slot, 201 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { 202 Mutex::Autolock lock(mMutex); 203 return addReleaseFenceLocked(slot, graphicBuffer, fence); 204} 205 206status_t ConsumerBase::addReleaseFenceLocked(int slot, 207 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { 208 CB_LOGV("addReleaseFenceLocked: slot=%d", slot); 209 210 // If consumer no longer tracks this graphicBuffer, we can safely 211 // drop this fence, as it will never be received by the producer. 212 if (!stillTracking(slot, graphicBuffer)) { 213 return OK; 214 } 215 216 if (!mSlots[slot].mFence.get()) { 217 mSlots[slot].mFence = fence; 218 } else { 219 sp<Fence> mergedFence = Fence::merge( 220 String8::format("%.28s:%d", mName.string(), slot), 221 mSlots[slot].mFence, fence); 222 if (!mergedFence.get()) { 223 CB_LOGE("failed to merge release fences"); 224 // synchronization is broken, the best we can do is hope fences 225 // signal in order so the new fence will act like a union 226 mSlots[slot].mFence = fence; 227 return BAD_VALUE; 228 } 229 mSlots[slot].mFence = mergedFence; 230 } 231 232 return OK; 233} 234 235status_t ConsumerBase::releaseBufferLocked( 236 int slot, const sp<GraphicBuffer> graphicBuffer, 237 EGLDisplay display, EGLSyncKHR eglFence) { 238 // If consumer no longer tracks this graphicBuffer (we received a new 239 // buffer on the same slot), the buffer producer is definitely no longer 240 // tracking it. 241 if (!stillTracking(slot, graphicBuffer)) { 242 return OK; 243 } 244 245 CB_LOGV("releaseBufferLocked: slot=%d/%llu", 246 slot, mSlots[slot].mFrameNumber); 247 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber, 248 display, eglFence, mSlots[slot].mFence); 249 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) { 250 freeBufferLocked(slot); 251 } 252 253 mSlots[slot].mFence = Fence::NO_FENCE; 254 255 return err; 256} 257 258bool ConsumerBase::stillTracking(int slot, 259 const sp<GraphicBuffer> graphicBuffer) { 260 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) { 261 return false; 262 } 263 return (mSlots[slot].mGraphicBuffer != NULL && 264 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle); 265} 266 267} // namespace android 268