ConsumerBase.cpp revision b5fe96b8130b5bacbc1762ba7f819a9f9b72bf59
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 GL_GLEXT_PROTOTYPES 22#define EGL_EGLEXT_PROTOTYPES 23 24#include <EGL/egl.h> 25#include <EGL/eglext.h> 26 27#include <hardware/hardware.h> 28 29#include <gui/IGraphicBufferAlloc.h> 30#include <gui/ISurfaceComposer.h> 31#include <gui/SurfaceComposerClient.h> 32#include <gui/ConsumerBase.h> 33 34#include <private/gui/ComposerService.h> 35 36#include <utils/Log.h> 37#include <utils/String8.h> 38#include <utils/Trace.h> 39 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__) 46 47namespace android { 48 49// Get an ID that's unique within this process. 50static int32_t createProcessUniqueId() { 51 static volatile int32_t globalCounter = 0; 52 return android_atomic_inc(&globalCounter); 53} 54 55ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) : 56 mAbandoned(false), 57 mBufferQueue(bufferQueue) { 58 // Choose a name using the PID and a process-unique ID. 59 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); 60 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); 69 70 status_t err = mBufferQueue->consumerConnect(proxy); 71 if (err != NO_ERROR) { 72 CB_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)", 73 strerror(-err), err); 74 } else { 75 mBufferQueue->setConsumerName(mName); 76 } 77} 78 79ConsumerBase::~ConsumerBase() { 80 CB_LOGV("~ConsumerBase"); 81 abandon(); 82} 83 84void ConsumerBase::freeBufferLocked(int slotIndex) { 85 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); 86 mSlots[slotIndex].mGraphicBuffer = 0; 87 mSlots[slotIndex].mFence = 0; 88} 89 90// Used for refactoring, should not be in final interface 91sp<BufferQueue> ConsumerBase::getBufferQueue() const { 92 Mutex::Autolock lock(mMutex); 93 return mBufferQueue; 94} 95 96void ConsumerBase::onFrameAvailable() { 97 CB_LOGV("onFrameAvailable"); 98 99 sp<FrameAvailableListener> listener; 100 { // scope for the lock 101 Mutex::Autolock lock(mMutex); 102 listener = mFrameAvailableListener.promote(); 103 } 104 105 if (listener != NULL) { 106 CB_LOGV("actually calling onFrameAvailable"); 107 listener->onFrameAvailable(); 108 } 109} 110 111void ConsumerBase::onBuffersReleased() { 112 sp<GraphicBuffer> bufRefs[BufferQueue::NUM_BUFFER_SLOTS]; 113 114 { // Scope for the lock 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 uint32_t mask = 0; 125 mBufferQueue->getReleasedBuffers(&mask); 126 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 127 if (mask & (1 << i)) { 128 // Grab a local reference to the buffers so that they don't 129 // get freed while the lock is held. 130 bufRefs[i] = mSlots[i].mGraphicBuffer; 131 132 freeBufferLocked(i); 133 } 134 } 135 } 136 137 // Clear the local buffer references. This would happen automatically 138 // when the array gets dtor'd, but I'm doing it explicitly for clarity. 139 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 140 bufRefs[i].clear(); 141 } 142} 143 144void ConsumerBase::abandon() { 145 CB_LOGV("abandon"); 146 Mutex::Autolock lock(mMutex); 147 148 if (!mAbandoned) { 149 abandonLocked(); 150 mAbandoned = true; 151 } 152} 153 154void ConsumerBase::abandonLocked() { 155 CB_LOGV("abandonLocked"); 156 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 157 freeBufferLocked(i); 158 } 159 // disconnect from the BufferQueue 160 mBufferQueue->consumerDisconnect(); 161 mBufferQueue.clear(); 162} 163 164void ConsumerBase::setFrameAvailableListener( 165 const wp<FrameAvailableListener>& listener) { 166 CB_LOGV("setFrameAvailableListener"); 167 Mutex::Autolock lock(mMutex); 168 mFrameAvailableListener = listener; 169} 170 171void ConsumerBase::dump(String8& result) const { 172 char buffer[1024]; 173 dump(result, "", buffer, 1024); 174} 175 176void ConsumerBase::dump(String8& result, const char* prefix, 177 char* buffer, size_t size) const { 178 Mutex::Autolock _l(mMutex); 179 dumpLocked(result, prefix, buffer, size); 180} 181 182void ConsumerBase::dumpLocked(String8& result, const char* prefix, 183 char* buffer, size_t SIZE) const { 184 snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned)); 185 result.append(buffer); 186 187 if (!mAbandoned) { 188 mBufferQueue->dump(result, prefix, buffer, SIZE); 189 } 190} 191 192status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) { 193 status_t err = mBufferQueue->acquireBuffer(item); 194 if (err != NO_ERROR) { 195 return err; 196 } 197 198 if (item->mGraphicBuffer != NULL) { 199 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; 200 } 201 202 mSlots[item->mBuf].mFence = item->mFence; 203 204 CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf); 205 206 return OK; 207} 208 209status_t ConsumerBase::addReleaseFence(int slot, const sp<Fence>& fence) { 210 Mutex::Autolock lock(mMutex); 211 return addReleaseFenceLocked(slot, fence); 212} 213 214status_t ConsumerBase::addReleaseFenceLocked(int slot, const sp<Fence>& fence) { 215 CB_LOGV("addReleaseFenceLocked: slot=%d", slot); 216 217 if (!mSlots[slot].mFence.get()) { 218 mSlots[slot].mFence = fence; 219 } else { 220 sp<Fence> mergedFence = Fence::merge( 221 String8::format("%.28s:%d", mName.string(), slot), 222 mSlots[slot].mFence, fence); 223 if (!mergedFence.get()) { 224 CB_LOGE("failed to merge release fences"); 225 // synchronization is broken, the best we can do is hope fences 226 // signal in order so the new fence will act like a union 227 mSlots[slot].mFence = fence; 228 return BAD_VALUE; 229 } 230 mSlots[slot].mFence = mergedFence; 231 } 232 233 return OK; 234} 235 236status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display, 237 EGLSyncKHR eglFence) { 238 CB_LOGV("releaseBufferLocked: slot=%d", slot); 239 status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence, 240 mSlots[slot].mFence); 241 if (err == BufferQueue::STALE_BUFFER_SLOT) { 242 freeBufferLocked(slot); 243 } 244 245 mSlots[slot].mFence.clear(); 246 247 return err; 248} 249 250} // namespace android