Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "BLASTBufferQueue" |
| 19 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Valerie Hau | 65b8e87 | 2020-02-13 09:45:14 -0800 | [diff] [blame] | 22 | #include <cutils/properties.h> |
| 23 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 24 | #include <gui/BLASTBufferQueue.h> |
| 25 | #include <gui/BufferItemConsumer.h> |
Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 26 | #include <gui/GLConsumer.h> |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 27 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 28 | #include <utils/Trace.h> |
| 29 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 30 | #include <chrono> |
| 31 | |
| 32 | using namespace std::chrono_literals; |
| 33 | |
| 34 | namespace android { |
| 35 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 36 | void BLASTBufferItemConsumer::onDisconnect() { |
| 37 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 38 | mPreviouslyConnected = mCurrentlyConnected; |
| 39 | mCurrentlyConnected = false; |
| 40 | if (mPreviouslyConnected) { |
| 41 | mDisconnectEvents.push(mCurrentFrameNumber); |
| 42 | } |
| 43 | mFrameEventHistory.onDisconnect(); |
| 44 | } |
| 45 | |
| 46 | void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 47 | FrameEventHistoryDelta* outDelta) { |
| 48 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 49 | if (newTimestamps) { |
| 50 | // BufferQueueProducer only adds a new timestamp on |
| 51 | // queueBuffer |
| 52 | mCurrentFrameNumber = newTimestamps->frameNumber; |
| 53 | mFrameEventHistory.addQueue(*newTimestamps); |
| 54 | } |
| 55 | if (outDelta) { |
| 56 | // frame event histories will be processed |
| 57 | // only after the producer connects and requests |
| 58 | // deltas for the first time. Forward this intent |
| 59 | // to SF-side to turn event processing back on |
| 60 | mPreviouslyConnected = mCurrentlyConnected; |
| 61 | mCurrentlyConnected = true; |
| 62 | mFrameEventHistory.getAndResetDelta(outDelta); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime, |
| 67 | const sp<Fence>& glDoneFence, |
| 68 | const sp<Fence>& presentFence, |
| 69 | const sp<Fence>& prevReleaseFence, |
| 70 | CompositorTiming compositorTiming, |
| 71 | nsecs_t latchTime, nsecs_t dequeueReadyTime) { |
| 72 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 73 | |
| 74 | // if the producer is not connected, don't bother updating, |
| 75 | // the next producer that connects won't access this frame event |
| 76 | if (!mCurrentlyConnected) return; |
| 77 | std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence); |
| 78 | std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence); |
| 79 | std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence); |
| 80 | |
| 81 | mFrameEventHistory.addLatch(frameNumber, latchTime); |
| 82 | mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime)); |
| 83 | mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime); |
| 84 | mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime, |
| 85 | compositorTiming); |
| 86 | } |
| 87 | |
| 88 | void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) { |
| 89 | bool disconnect = false; |
| 90 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 91 | while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) { |
| 92 | disconnect = true; |
| 93 | mDisconnectEvents.pop(); |
| 94 | } |
| 95 | if (needsDisconnect != nullptr) *needsDisconnect = disconnect; |
| 96 | } |
| 97 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 98 | BLASTBufferQueue::BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height) |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 99 | : mSurfaceControl(surface), |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 100 | mWidth(width), |
| 101 | mHeight(height), |
| 102 | mNextTransaction(nullptr) { |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 103 | BufferQueue::createBufferQueue(&mProducer, &mConsumer); |
Valerie Hau | 0889c62 | 2020-02-19 15:04:47 -0800 | [diff] [blame^] | 104 | // since the adapter is in the client process, set dequeue timeout |
| 105 | // explicitly so that dequeueBuffer will block |
| 106 | mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max()); |
Valerie Hau | 65b8e87 | 2020-02-13 09:45:14 -0800 | [diff] [blame] | 107 | |
| 108 | int8_t disableTripleBuffer = property_get_bool("ro.sf.disable_triple_buffer", 0); |
| 109 | if (!disableTripleBuffer) { |
| 110 | mProducer->setMaxDequeuedBufferCount(2); |
| 111 | } |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 112 | mBufferItemConsumer = |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 113 | new BLASTBufferItemConsumer(mConsumer, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, 1, true); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 114 | static int32_t id = 0; |
| 115 | auto name = std::string("BLAST Consumer") + std::to_string(id); |
| 116 | id++; |
| 117 | mBufferItemConsumer->setName(String8(name.c_str())); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 118 | mBufferItemConsumer->setFrameAvailableListener(this); |
| 119 | mBufferItemConsumer->setBufferFreedListener(this); |
| 120 | mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight); |
| 121 | mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888); |
Valerie Hau | 2882e98 | 2020-01-23 13:33:10 -0800 | [diff] [blame] | 122 | mTransformHint = mSurfaceControl->getTransformHint(); |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 123 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 124 | mNumAcquired = 0; |
| 125 | mNumFrameAvailable = 0; |
| 126 | mPendingReleaseItem.item = BufferItem(); |
| 127 | mPendingReleaseItem.releaseFence = nullptr; |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, int width, int height) { |
| 131 | std::unique_lock _lock{mMutex}; |
| 132 | mSurfaceControl = surface; |
| 133 | mWidth = width; |
| 134 | mHeight = height; |
| 135 | mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight); |
| 136 | } |
| 137 | |
| 138 | static void transactionCallbackThunk(void* context, nsecs_t latchTime, |
| 139 | const sp<Fence>& presentFence, |
| 140 | const std::vector<SurfaceControlStats>& stats) { |
| 141 | if (context == nullptr) { |
| 142 | return; |
| 143 | } |
| 144 | BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context); |
| 145 | bq->transactionCallback(latchTime, presentFence, stats); |
| 146 | } |
| 147 | |
| 148 | void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/, |
| 149 | const std::vector<SurfaceControlStats>& stats) { |
| 150 | std::unique_lock _lock{mMutex}; |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 151 | ATRACE_CALL(); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 152 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 153 | if (!stats.empty()) { |
| 154 | mTransformHint = stats[0].transformHint; |
| 155 | mBufferItemConsumer->setTransformHint(mTransformHint); |
| 156 | mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber, |
| 157 | stats[0].frameEventStats.refreshStartTime, |
| 158 | stats[0].frameEventStats.gpuCompositionDoneFence, |
| 159 | stats[0].presentFence, |
| 160 | stats[0].previousReleaseFence, |
| 161 | stats[0].frameEventStats.compositorTiming, |
| 162 | stats[0].latchTime, |
| 163 | stats[0].frameEventStats.dequeueReadyTime); |
| 164 | } |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 165 | if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) { |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 166 | if (!stats.empty()) { |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 167 | mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence; |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 168 | } else { |
| 169 | ALOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback"); |
| 170 | mPendingReleaseItem.releaseFence = nullptr; |
| 171 | } |
| 172 | mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item, |
| 173 | mPendingReleaseItem.releaseFence |
| 174 | ? mPendingReleaseItem.releaseFence |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 175 | : Fence::NO_FENCE); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 176 | mNumAcquired--; |
| 177 | mPendingReleaseItem.item = BufferItem(); |
| 178 | mPendingReleaseItem.releaseFence = nullptr; |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 179 | } |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 180 | |
| 181 | if (mSubmitted.empty()) { |
| 182 | ALOGE("ERROR: callback with no corresponding submitted buffer item"); |
| 183 | } |
| 184 | mPendingReleaseItem.item = std::move(mSubmitted.front()); |
| 185 | mSubmitted.pop(); |
Robert Carr | e4943eb | 2020-02-15 18:34:59 -0800 | [diff] [blame] | 186 | |
| 187 | processNextBufferLocked(); |
| 188 | |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 189 | mCallbackCV.notify_all(); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 190 | decStrong((void*)transactionCallbackThunk); |
| 191 | } |
| 192 | |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 193 | void BLASTBufferQueue::processNextBufferLocked() { |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 194 | ATRACE_CALL(); |
Valerie Hau | 65b8e87 | 2020-02-13 09:45:14 -0800 | [diff] [blame] | 195 | if (mNumFrameAvailable == 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) { |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 199 | if (mSurfaceControl == nullptr) { |
| 200 | ALOGE("ERROR : surface control is null"); |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 204 | SurfaceComposerClient::Transaction localTransaction; |
| 205 | bool applyTransaction = true; |
| 206 | SurfaceComposerClient::Transaction* t = &localTransaction; |
Robert Carr | e4943eb | 2020-02-15 18:34:59 -0800 | [diff] [blame] | 207 | if (mNextTransaction != nullptr && mUseNextTransaction) { |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 208 | t = mNextTransaction; |
| 209 | mNextTransaction = nullptr; |
| 210 | applyTransaction = false; |
| 211 | } |
| 212 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 213 | BufferItem bufferItem; |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 214 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 215 | status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 216 | if (status != OK) { |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 219 | auto buffer = bufferItem.mGraphicBuffer; |
| 220 | mNumFrameAvailable--; |
| 221 | |
| 222 | if (buffer == nullptr) { |
| 223 | mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | mNumAcquired++; |
| 228 | mSubmitted.push(bufferItem); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 229 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 230 | bool needsDisconnect = false; |
| 231 | mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect); |
| 232 | |
| 233 | // if producer disconnected before, notify SurfaceFlinger |
| 234 | if (needsDisconnect) { |
| 235 | t->notifyProducerDisconnect(mSurfaceControl); |
| 236 | } |
| 237 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 238 | // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback. |
| 239 | incStrong((void*)transactionCallbackThunk); |
| 240 | |
| 241 | t->setBuffer(mSurfaceControl, buffer); |
| 242 | t->setAcquireFence(mSurfaceControl, |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 243 | bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 244 | t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this)); |
| 245 | |
Valerie Hau | 81ec607 | 2020-01-30 09:45:24 -0800 | [diff] [blame] | 246 | t->setFrame(mSurfaceControl, {0, 0, mWidth, mHeight}); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 247 | t->setCrop(mSurfaceControl, computeCrop(bufferItem)); |
| 248 | t->setTransform(mSurfaceControl, bufferItem.mTransform); |
Valerie Hau | 2882e98 | 2020-01-23 13:33:10 -0800 | [diff] [blame] | 249 | t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse); |
Valerie Hau | 181abd3 | 2020-01-27 14:18:28 -0800 | [diff] [blame] | 250 | t->setDesiredPresentTime(bufferItem.mTimestamp); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 251 | |
| 252 | if (applyTransaction) { |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 253 | t->apply(); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 257 | Rect BLASTBufferQueue::computeCrop(const BufferItem& item) { |
| 258 | if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { |
| 259 | return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight); |
| 260 | } |
| 261 | return item.mCrop; |
| 262 | } |
| 263 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 264 | void BLASTBufferQueue::onFrameAvailable(const BufferItem& /*item*/) { |
| 265 | ATRACE_CALL(); |
Valerie Hau | 0188adf | 2020-02-13 08:29:20 -0800 | [diff] [blame] | 266 | std::unique_lock _lock{mMutex}; |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 267 | |
Valerie Hau | 0188adf | 2020-02-13 08:29:20 -0800 | [diff] [blame] | 268 | if (mNextTransaction != nullptr) { |
Robert Carr | e4943eb | 2020-02-15 18:34:59 -0800 | [diff] [blame] | 269 | while (mNumFrameAvailable > 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) { |
Valerie Hau | 0188adf | 2020-02-13 08:29:20 -0800 | [diff] [blame] | 270 | mCallbackCV.wait(_lock); |
| 271 | } |
Robert Carr | e4943eb | 2020-02-15 18:34:59 -0800 | [diff] [blame] | 272 | mUseNextTransaction = true; |
Valerie Hau | 0188adf | 2020-02-13 08:29:20 -0800 | [diff] [blame] | 273 | } |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 274 | // add to shadow queue |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 275 | mNumFrameAvailable++; |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 276 | processNextBufferLocked(); |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 279 | void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) { |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 280 | std::lock_guard _lock{mMutex}; |
Robert Carr | e4943eb | 2020-02-15 18:34:59 -0800 | [diff] [blame] | 281 | mUseNextTransaction = false; |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 282 | mNextTransaction = t; |
| 283 | } |
| 284 | |
| 285 | } // namespace android |