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