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