Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2010 The Android Open Source Project |
michael@0 | 3 | * Copyright (C) 2013 Mozilla Foundation |
michael@0 | 4 | * |
michael@0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 6 | * you may not use this file except in compliance with the License. |
michael@0 | 7 | * You may obtain a copy of the License at |
michael@0 | 8 | * |
michael@0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 10 | * |
michael@0 | 11 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 14 | * See the License for the specific language governing permissions and |
michael@0 | 15 | * limitations under the License. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #define LOG_TAG "GonkConsumerBase" |
michael@0 | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
michael@0 | 20 | //#define LOG_NDEBUG 0 |
michael@0 | 21 | |
michael@0 | 22 | #define EGL_EGLEXT_PROTOTYPES |
michael@0 | 23 | |
michael@0 | 24 | #include <hardware/hardware.h> |
michael@0 | 25 | |
michael@0 | 26 | #include <gui/IGraphicBufferAlloc.h> |
michael@0 | 27 | #include <utils/Log.h> |
michael@0 | 28 | #include <utils/String8.h> |
michael@0 | 29 | |
michael@0 | 30 | #include "GonkConsumerBaseJB.h" |
michael@0 | 31 | |
michael@0 | 32 | // Macros for including the GonkConsumerBase name in log messages |
michael@0 | 33 | #define CB_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) |
michael@0 | 34 | #define CB_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
michael@0 | 35 | #define CB_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) |
michael@0 | 36 | #define CB_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) |
michael@0 | 37 | #define CB_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) |
michael@0 | 38 | |
michael@0 | 39 | namespace android { |
michael@0 | 40 | |
michael@0 | 41 | // Get an ID that's unique within this process. |
michael@0 | 42 | static int32_t createProcessUniqueId() { |
michael@0 | 43 | static volatile int32_t globalCounter = 0; |
michael@0 | 44 | return android_atomic_inc(&globalCounter); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | GonkConsumerBase::GonkConsumerBase(const sp<GonkBufferQueue>& bufferQueue) : |
michael@0 | 48 | mAbandoned(false), |
michael@0 | 49 | mBufferQueue(bufferQueue) { |
michael@0 | 50 | // Choose a name using the PID and a process-unique ID. |
michael@0 | 51 | mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); |
michael@0 | 52 | |
michael@0 | 53 | // Note that we can't create an sp<...>(this) in a ctor that will not keep a |
michael@0 | 54 | // reference once the ctor ends, as that would cause the refcount of 'this' |
michael@0 | 55 | // dropping to 0 at the end of the ctor. Since all we need is a wp<...> |
michael@0 | 56 | // that's what we create. |
michael@0 | 57 | wp<GonkBufferQueue::ConsumerListener> listener; |
michael@0 | 58 | sp<GonkBufferQueue::ConsumerListener> proxy; |
michael@0 | 59 | listener = static_cast<GonkBufferQueue::ConsumerListener*>(this); |
michael@0 | 60 | proxy = new GonkBufferQueue::ProxyConsumerListener(listener); |
michael@0 | 61 | |
michael@0 | 62 | status_t err = mBufferQueue->consumerConnect(proxy); |
michael@0 | 63 | if (err != NO_ERROR) { |
michael@0 | 64 | CB_LOGE("GonkConsumerBase: error connecting to GonkBufferQueue: %s (%d)", |
michael@0 | 65 | strerror(-err), err); |
michael@0 | 66 | } else { |
michael@0 | 67 | mBufferQueue->setConsumerName(mName); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | GonkConsumerBase::~GonkConsumerBase() { |
michael@0 | 72 | CB_LOGV("~GonkConsumerBase"); |
michael@0 | 73 | Mutex::Autolock lock(mMutex); |
michael@0 | 74 | |
michael@0 | 75 | // Verify that abandon() has been called before we get here. This should |
michael@0 | 76 | // be done by GonkConsumerBase::onLastStrongRef(), but it's possible for a |
michael@0 | 77 | // derived class to override that method and not call |
michael@0 | 78 | // GonkConsumerBase::onLastStrongRef(). |
michael@0 | 79 | LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~GonkConsumerBase was called, but the " |
michael@0 | 80 | "consumer is not abandoned!", mName.string()); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void GonkConsumerBase::onLastStrongRef(const void* id) { |
michael@0 | 84 | abandon(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | void GonkConsumerBase::freeBufferLocked(int slotIndex) { |
michael@0 | 88 | CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
michael@0 | 89 | mSlots[slotIndex].mGraphicBuffer = 0; |
michael@0 | 90 | mSlots[slotIndex].mFence = Fence::NO_FENCE; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // Used for refactoring, should not be in final interface |
michael@0 | 94 | sp<GonkBufferQueue> GonkConsumerBase::getBufferQueue() const { |
michael@0 | 95 | Mutex::Autolock lock(mMutex); |
michael@0 | 96 | return mBufferQueue; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | void GonkConsumerBase::onFrameAvailable() { |
michael@0 | 100 | CB_LOGV("onFrameAvailable"); |
michael@0 | 101 | |
michael@0 | 102 | sp<FrameAvailableListener> listener; |
michael@0 | 103 | { // scope for the lock |
michael@0 | 104 | Mutex::Autolock lock(mMutex); |
michael@0 | 105 | #if ANDROID_VERSION == 17 |
michael@0 | 106 | listener = mFrameAvailableListener; |
michael@0 | 107 | #else |
michael@0 | 108 | listener = mFrameAvailableListener.promote(); |
michael@0 | 109 | #endif |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | if (listener != NULL) { |
michael@0 | 113 | CB_LOGV("actually calling onFrameAvailable"); |
michael@0 | 114 | listener->onFrameAvailable(); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | void GonkConsumerBase::onBuffersReleased() { |
michael@0 | 119 | Mutex::Autolock lock(mMutex); |
michael@0 | 120 | |
michael@0 | 121 | CB_LOGV("onBuffersReleased"); |
michael@0 | 122 | |
michael@0 | 123 | if (mAbandoned) { |
michael@0 | 124 | // Nothing to do if we're already abandoned. |
michael@0 | 125 | return; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | uint32_t mask = 0; |
michael@0 | 129 | mBufferQueue->getReleasedBuffers(&mask); |
michael@0 | 130 | for (int i = 0; i < GonkBufferQueue::NUM_BUFFER_SLOTS; i++) { |
michael@0 | 131 | if (mask & (1 << i)) { |
michael@0 | 132 | freeBufferLocked(i); |
michael@0 | 133 | } |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | void GonkConsumerBase::abandon() { |
michael@0 | 138 | CB_LOGV("abandon"); |
michael@0 | 139 | Mutex::Autolock lock(mMutex); |
michael@0 | 140 | |
michael@0 | 141 | if (!mAbandoned) { |
michael@0 | 142 | abandonLocked(); |
michael@0 | 143 | mAbandoned = true; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void GonkConsumerBase::abandonLocked() { |
michael@0 | 148 | CB_LOGV("abandonLocked"); |
michael@0 | 149 | for (int i =0; i < GonkBufferQueue::NUM_BUFFER_SLOTS; i++) { |
michael@0 | 150 | freeBufferLocked(i); |
michael@0 | 151 | } |
michael@0 | 152 | // disconnect from the GonkBufferQueue |
michael@0 | 153 | mBufferQueue->consumerDisconnect(); |
michael@0 | 154 | mBufferQueue.clear(); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | void GonkConsumerBase::setFrameAvailableListener( |
michael@0 | 158 | #if ANDROID_VERSION == 17 |
michael@0 | 159 | const sp<FrameAvailableListener>& listener) { |
michael@0 | 160 | #else |
michael@0 | 161 | const wp<FrameAvailableListener>& listener) { |
michael@0 | 162 | #endif |
michael@0 | 163 | CB_LOGV("setFrameAvailableListener"); |
michael@0 | 164 | Mutex::Autolock lock(mMutex); |
michael@0 | 165 | mFrameAvailableListener = listener; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | void GonkConsumerBase::dump(String8& result) const { |
michael@0 | 169 | char buffer[1024]; |
michael@0 | 170 | dump(result, "", buffer, 1024); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | void GonkConsumerBase::dump(String8& result, const char* prefix, |
michael@0 | 174 | char* buffer, size_t size) const { |
michael@0 | 175 | Mutex::Autolock _l(mMutex); |
michael@0 | 176 | dumpLocked(result, prefix, buffer, size); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | void GonkConsumerBase::dumpLocked(String8& result, const char* prefix, |
michael@0 | 180 | char* buffer, size_t SIZE) const { |
michael@0 | 181 | snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned)); |
michael@0 | 182 | result.append(buffer); |
michael@0 | 183 | |
michael@0 | 184 | if (!mAbandoned) { |
michael@0 | 185 | mBufferQueue->dump(result, prefix, buffer, SIZE); |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | status_t GonkConsumerBase::acquireBufferLocked(GonkBufferQueue::BufferItem *item) { |
michael@0 | 190 | status_t err = mBufferQueue->acquireBuffer(item); |
michael@0 | 191 | if (err != NO_ERROR) { |
michael@0 | 192 | return err; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if (item->mGraphicBuffer != NULL) { |
michael@0 | 196 | mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | mSlots[item->mBuf].mFence = item->mFence; |
michael@0 | 200 | |
michael@0 | 201 | CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf); |
michael@0 | 202 | |
michael@0 | 203 | return OK; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | status_t GonkConsumerBase::addReleaseFence(int slot, const sp<Fence>& fence) { |
michael@0 | 207 | Mutex::Autolock lock(mMutex); |
michael@0 | 208 | return addReleaseFenceLocked(slot, fence); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | status_t GonkConsumerBase::addReleaseFenceLocked(int slot, const sp<Fence>& fence) { |
michael@0 | 212 | CB_LOGV("addReleaseFenceLocked: slot=%d", slot); |
michael@0 | 213 | |
michael@0 | 214 | if (!mSlots[slot].mFence.get()) { |
michael@0 | 215 | mSlots[slot].mFence = fence; |
michael@0 | 216 | } else { |
michael@0 | 217 | sp<Fence> mergedFence = Fence::merge( |
michael@0 | 218 | String8::format("%.28s:%d", mName.string(), slot), |
michael@0 | 219 | mSlots[slot].mFence, fence); |
michael@0 | 220 | if (!mergedFence.get()) { |
michael@0 | 221 | CB_LOGE("failed to merge release fences"); |
michael@0 | 222 | // synchronization is broken, the best we can do is hope fences |
michael@0 | 223 | // signal in order so the new fence will act like a union |
michael@0 | 224 | mSlots[slot].mFence = fence; |
michael@0 | 225 | return BAD_VALUE; |
michael@0 | 226 | } |
michael@0 | 227 | mSlots[slot].mFence = mergedFence; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | return OK; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | status_t GonkConsumerBase::releaseBufferLocked(int slot) { |
michael@0 | 234 | CB_LOGV("releaseBufferLocked: slot=%d", slot); |
michael@0 | 235 | status_t err = mBufferQueue->releaseBuffer(slot, mSlots[slot].mFence); |
michael@0 | 236 | if (err == GonkBufferQueue::STALE_BUFFER_SLOT) { |
michael@0 | 237 | freeBufferLocked(slot); |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | mSlots[slot].mFence = Fence::NO_FENCE; |
michael@0 | 241 | |
michael@0 | 242 | return err; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | } // namespace android |