Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 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 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 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 | |
| 46 | namespace android { |
| 47 | |
| 48 | // Get an ID that's unique within this process. |
| 49 | static int32_t createProcessUniqueId() { |
| 50 | static volatile int32_t globalCounter = 0; |
| 51 | return android_atomic_inc(&globalCounter); |
| 52 | } |
| 53 | |
| 54 | ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) : |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 55 | mAbandoned(false), |
| 56 | mBufferQueue(bufferQueue) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 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<BufferQueue::ConsumerListener> listener; |
| 65 | sp<BufferQueue::ConsumerListener> proxy; |
| 66 | listener = static_cast<BufferQueue::ConsumerListener*>(this); |
| 67 | proxy = new BufferQueue::ProxyConsumerListener(listener); |
| 68 | |
| 69 | status_t err = mBufferQueue->consumerConnect(proxy); |
| 70 | if (err != NO_ERROR) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 71 | CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)", |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 72 | strerror(-err), err); |
| 73 | } else { |
| 74 | mBufferQueue->setConsumerName(mName); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | ConsumerBase::~ConsumerBase() { |
Jamie Gennis | ad669b0 | 2013-04-05 16:41:27 -0700 | [diff] [blame] | 79 | CB_LOGV("~ConsumerBase"); |
| 80 | Mutex::Autolock lock(mMutex); |
| 81 | |
| 82 | // Verify that abandon() has been called before we get here. This should |
| 83 | // be done by ConsumerBase::onLastStrongRef(), but it's possible for a |
| 84 | // derived class to override that method and not call |
| 85 | // ConsumerBase::onLastStrongRef(). |
| 86 | LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the " |
| 87 | "consumer is not abandoned!", mName.string()); |
| 88 | } |
| 89 | |
| 90 | void ConsumerBase::onLastStrongRef(const void* id) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 91 | abandon(); |
| 92 | } |
| 93 | |
| 94 | void ConsumerBase::freeBufferLocked(int slotIndex) { |
| 95 | CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 96 | mSlots[slotIndex].mGraphicBuffer = 0; |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 97 | mSlots[slotIndex].mFence = Fence::NO_FENCE; |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 98 | mSlots[slotIndex].mFrameNumber = 0; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Used for refactoring, should not be in final interface |
| 102 | sp<BufferQueue> ConsumerBase::getBufferQueue() const { |
| 103 | Mutex::Autolock lock(mMutex); |
| 104 | return mBufferQueue; |
| 105 | } |
| 106 | |
| 107 | void ConsumerBase::onFrameAvailable() { |
| 108 | CB_LOGV("onFrameAvailable"); |
| 109 | |
| 110 | sp<FrameAvailableListener> listener; |
| 111 | { // scope for the lock |
| 112 | Mutex::Autolock lock(mMutex); |
Igor Murashkin | a4a3149 | 2012-10-29 13:36:11 -0700 | [diff] [blame] | 113 | listener = mFrameAvailableListener.promote(); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (listener != NULL) { |
| 117 | CB_LOGV("actually calling onFrameAvailable"); |
| 118 | listener->onFrameAvailable(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void ConsumerBase::onBuffersReleased() { |
Jamie Gennis | 72c3f7d | 2012-12-07 00:41:56 -0800 | [diff] [blame] | 123 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 124 | |
Jamie Gennis | 72c3f7d | 2012-12-07 00:41:56 -0800 | [diff] [blame] | 125 | CB_LOGV("onBuffersReleased"); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 126 | |
Jamie Gennis | 72c3f7d | 2012-12-07 00:41:56 -0800 | [diff] [blame] | 127 | if (mAbandoned) { |
| 128 | // Nothing to do if we're already abandoned. |
| 129 | return; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Jamie Gennis | 72c3f7d | 2012-12-07 00:41:56 -0800 | [diff] [blame] | 132 | uint32_t mask = 0; |
| 133 | mBufferQueue->getReleasedBuffers(&mask); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 134 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
Jamie Gennis | 72c3f7d | 2012-12-07 00:41:56 -0800 | [diff] [blame] | 135 | if (mask & (1 << i)) { |
| 136 | freeBufferLocked(i); |
| 137 | } |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | void ConsumerBase::abandon() { |
| 142 | CB_LOGV("abandon"); |
| 143 | Mutex::Autolock lock(mMutex); |
| 144 | |
| 145 | if (!mAbandoned) { |
| 146 | abandonLocked(); |
| 147 | mAbandoned = true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void ConsumerBase::abandonLocked() { |
| 152 | CB_LOGV("abandonLocked"); |
| 153 | for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 154 | freeBufferLocked(i); |
| 155 | } |
| 156 | // disconnect from the BufferQueue |
| 157 | mBufferQueue->consumerDisconnect(); |
| 158 | mBufferQueue.clear(); |
| 159 | } |
| 160 | |
| 161 | void ConsumerBase::setFrameAvailableListener( |
Igor Murashkin | a4a3149 | 2012-10-29 13:36:11 -0700 | [diff] [blame] | 162 | const wp<FrameAvailableListener>& listener) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 163 | CB_LOGV("setFrameAvailableListener"); |
| 164 | Mutex::Autolock lock(mMutex); |
| 165 | mFrameAvailableListener = listener; |
| 166 | } |
| 167 | |
| 168 | void ConsumerBase::dump(String8& result) const { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 169 | dump(result, ""); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 172 | void ConsumerBase::dump(String8& result, const char* prefix) const { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 173 | Mutex::Autolock _l(mMutex); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 174 | dumpLocked(result, prefix); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 177 | void ConsumerBase::dumpLocked(String8& result, const char* prefix) const { |
| 178 | result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned)); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 179 | |
| 180 | if (!mAbandoned) { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 181 | mBufferQueue->dump(result, prefix); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) { |
| 186 | status_t err = mBufferQueue->acquireBuffer(item); |
| 187 | if (err != NO_ERROR) { |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | if (item->mGraphicBuffer != NULL) { |
| 192 | mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer; |
| 193 | } |
| 194 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 195 | mSlots[item->mBuf].mFrameNumber = item->mFrameNumber; |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 196 | mSlots[item->mBuf].mFence = item->mFence; |
| 197 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 198 | CB_LOGV("acquireBufferLocked: -> slot=%d/%llu", |
| 199 | item->mBuf, item->mFrameNumber); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 200 | |
| 201 | return OK; |
| 202 | } |
| 203 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 204 | status_t ConsumerBase::addReleaseFence(int slot, |
| 205 | const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { |
Jesse Hall | 9504eb9 | 2012-10-05 14:34:21 -0700 | [diff] [blame] | 206 | Mutex::Autolock lock(mMutex); |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 207 | return addReleaseFenceLocked(slot, graphicBuffer, fence); |
Jesse Hall | 9504eb9 | 2012-10-05 14:34:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 210 | status_t ConsumerBase::addReleaseFenceLocked(int slot, |
| 211 | const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) { |
Jesse Hall | 9504eb9 | 2012-10-05 14:34:21 -0700 | [diff] [blame] | 212 | CB_LOGV("addReleaseFenceLocked: slot=%d", slot); |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 213 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 214 | // If consumer no longer tracks this graphicBuffer, we can safely |
| 215 | // drop this fence, as it will never be received by the producer. |
| 216 | if (!stillTracking(slot, graphicBuffer)) { |
| 217 | return OK; |
| 218 | } |
| 219 | |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 220 | if (!mSlots[slot].mFence.get()) { |
| 221 | mSlots[slot].mFence = fence; |
| 222 | } else { |
| 223 | sp<Fence> mergedFence = Fence::merge( |
Jamie Gennis | 7aff4a5 | 2012-09-24 12:25:15 -0700 | [diff] [blame] | 224 | String8::format("%.28s:%d", mName.string(), slot), |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 225 | mSlots[slot].mFence, fence); |
| 226 | if (!mergedFence.get()) { |
| 227 | CB_LOGE("failed to merge release fences"); |
| 228 | // synchronization is broken, the best we can do is hope fences |
| 229 | // signal in order so the new fence will act like a union |
| 230 | mSlots[slot].mFence = fence; |
| 231 | return BAD_VALUE; |
| 232 | } |
| 233 | mSlots[slot].mFence = mergedFence; |
| 234 | } |
| 235 | |
| 236 | return OK; |
| 237 | } |
| 238 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 239 | status_t ConsumerBase::releaseBufferLocked( |
| 240 | int slot, const sp<GraphicBuffer> graphicBuffer, |
| 241 | EGLDisplay display, EGLSyncKHR eglFence) { |
| 242 | // If consumer no longer tracks this graphicBuffer (we received a new |
| 243 | // buffer on the same slot), the buffer producer is definitely no longer |
| 244 | // tracking it. |
| 245 | if (!stillTracking(slot, graphicBuffer)) { |
| 246 | return OK; |
| 247 | } |
| 248 | |
| 249 | CB_LOGV("releaseBufferLocked: slot=%d/%llu", |
| 250 | slot, mSlots[slot].mFrameNumber); |
| 251 | status_t err = mBufferQueue->releaseBuffer(slot, mSlots[slot].mFrameNumber, |
| 252 | display, eglFence, mSlots[slot].mFence); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 253 | if (err == BufferQueue::STALE_BUFFER_SLOT) { |
| 254 | freeBufferLocked(slot); |
| 255 | } |
| 256 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 257 | mSlots[slot].mFence = Fence::NO_FENCE; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 258 | |
| 259 | return err; |
| 260 | } |
| 261 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 262 | bool ConsumerBase::stillTracking(int slot, |
| 263 | const sp<GraphicBuffer> graphicBuffer) { |
| 264 | if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) { |
| 265 | return false; |
| 266 | } |
| 267 | return (mSlots[slot].mGraphicBuffer != NULL && |
| 268 | mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle); |
| 269 | } |
| 270 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 271 | } // namespace android |