Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 20 | #pragma clang diagnostic ignored "-Wextra" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 21 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 22 | //#define LOG_NDEBUG 0 |
| 23 | #undef LOG_TAG |
| 24 | #define LOG_TAG "BufferStateLayer" |
| 25 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 26 | |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 27 | #include "BufferStateLayer.h" |
| 28 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 29 | #include <limits> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 30 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame^] | 31 | #include <FrameTimeline/FrameTimeline.h> |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 32 | #include <compositionengine/LayerFECompositionState.h> |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 33 | #include <gui/BufferQueue.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 34 | #include <private/gui/SyncFeatures.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 35 | #include <renderengine/Image.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 36 | |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 37 | #include "EffectLayer.h" |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 38 | #include "TimeStats/TimeStats.h" |
Valerie Hau | 0bc0915 | 2018-12-20 07:42:47 -0800 | [diff] [blame] | 39 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame^] | 42 | using PresentState = frametimeline::SurfaceFrame::PresentState; |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 43 | // clang-format off |
| 44 | const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{ |
| 45 | 1, 0, 0, 0, |
| 46 | 0, 1, 0, 0, |
| 47 | 0, 0, 1, 0, |
| 48 | 0, 0, 0, 1 |
| 49 | }; |
| 50 | // clang-format on |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 51 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 52 | BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) |
| 53 | : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) { |
Marissa Wall | 3ff826c | 2019-02-07 11:58:25 -0800 | [diff] [blame] | 54 | mCurrentState.dataspace = ui::Dataspace::V0_SRGB; |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 55 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 56 | |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 57 | BufferStateLayer::~BufferStateLayer() { |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 58 | // The original layer and the clone layer share the same texture and buffer. Therefore, only |
| 59 | // one of the layers, in this case the original layer, needs to handle the deletion. The |
| 60 | // original layer and the clone should be removed at the same time so there shouldn't be any |
| 61 | // issue with the clone layer trying to use the texture. |
| 62 | if (mBufferInfo.mBuffer != nullptr && !isClone()) { |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 63 | // Ensure that mBuffer is uncached from RenderEngine here, as |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 64 | // RenderEngine may have been using the buffer as an external texture |
| 65 | // after the client uncached the buffer. |
| 66 | auto& engine(mFlinger->getRenderEngine()); |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 67 | engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId()); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 71 | status_t BufferStateLayer::addReleaseFence(const sp<CallbackHandle>& ch, |
| 72 | const sp<Fence>& fence) { |
| 73 | if (ch == nullptr) { |
| 74 | return OK; |
| 75 | } |
| 76 | if (!ch->previousReleaseFence.get()) { |
| 77 | ch->previousReleaseFence = fence; |
| 78 | return OK; |
| 79 | } |
| 80 | |
| 81 | // Below logic is lifted from ConsumerBase.cpp: |
| 82 | // Check status of fences first because merging is expensive. |
| 83 | // Merging an invalid fence with any other fence results in an |
| 84 | // invalid fence. |
| 85 | auto currentStatus = ch->previousReleaseFence->getStatus(); |
| 86 | if (currentStatus == Fence::Status::Invalid) { |
| 87 | ALOGE("Existing fence has invalid state, layer: %s", mName.c_str()); |
| 88 | return BAD_VALUE; |
| 89 | } |
| 90 | |
| 91 | auto incomingStatus = fence->getStatus(); |
| 92 | if (incomingStatus == Fence::Status::Invalid) { |
| 93 | ALOGE("New fence has invalid state, layer: %s", mName.c_str()); |
| 94 | ch->previousReleaseFence = fence; |
| 95 | return BAD_VALUE; |
| 96 | } |
| 97 | |
| 98 | // If both fences are signaled or both are unsignaled, we need to merge |
| 99 | // them to get an accurate timestamp. |
| 100 | if (currentStatus == incomingStatus) { |
| 101 | char fenceName[32] = {}; |
| 102 | snprintf(fenceName, 32, "%.28s", mName.c_str()); |
| 103 | sp<Fence> mergedFence = Fence::merge( |
| 104 | fenceName, ch->previousReleaseFence, fence); |
| 105 | if (!mergedFence.get()) { |
| 106 | ALOGE("failed to merge release fences, layer: %s", mName.c_str()); |
| 107 | // synchronization is broken, the best we can do is hope fences |
| 108 | // signal in order so the new fence will act like a union |
| 109 | ch->previousReleaseFence = fence; |
| 110 | return BAD_VALUE; |
| 111 | } |
| 112 | ch->previousReleaseFence = mergedFence; |
| 113 | } else if (incomingStatus == Fence::Status::Unsignaled) { |
| 114 | // If one fence has signaled and the other hasn't, the unsignaled |
| 115 | // fence will approximately correspond with the correct timestamp. |
| 116 | // There's a small race if both fences signal at about the same time |
| 117 | // and their statuses are retrieved with unfortunate timing. However, |
| 118 | // by this point, they will have both signaled and only the timestamp |
| 119 | // will be slightly off; any dependencies after this point will |
| 120 | // already have been met. |
| 121 | ch->previousReleaseFence = fence; |
| 122 | } |
| 123 | // else if (currentStatus == Fence::Status::Unsignaled) is a no-op. |
| 124 | |
| 125 | return OK; |
| 126 | } |
| 127 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 128 | // ----------------------------------------------------------------------- |
| 129 | // Interface implementation for Layer |
| 130 | // ----------------------------------------------------------------------- |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 131 | void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 132 | if (!releaseFence->isValid()) { |
| 133 | return; |
| 134 | } |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 135 | // The previous release fence notifies the client that SurfaceFlinger is done with the previous |
| 136 | // buffer that was presented on this layer. The first transaction that came in this frame that |
| 137 | // replaced the previous buffer on this layer needs this release fence, because the fence will |
| 138 | // let the client know when that previous buffer is removed from the screen. |
| 139 | // |
| 140 | // Every other transaction on this layer does not need a release fence because no other |
| 141 | // Transactions that were set on this layer this frame are going to have their preceeding buffer |
| 142 | // removed from the display this frame. |
| 143 | // |
| 144 | // For example, if we have 3 transactions this frame. The first transaction doesn't contain a |
| 145 | // buffer so it doesn't need a previous release fence because the layer still needs the previous |
| 146 | // buffer. The second transaction contains a buffer so it needs a previous release fence because |
| 147 | // the previous buffer will be released this frame. The third transaction also contains a |
| 148 | // buffer. It replaces the buffer in the second transaction. The buffer in the second |
| 149 | // transaction will now no longer be presented so it is released immediately and the third |
| 150 | // transaction doesn't need a previous release fence. |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 151 | sp<CallbackHandle> ch; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 152 | for (auto& handle : mDrawingState.callbackHandles) { |
| 153 | if (handle->releasePreviousBuffer) { |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 154 | ch = handle; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 155 | break; |
| 156 | } |
| 157 | } |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 158 | auto status = addReleaseFence(ch, releaseFence); |
| 159 | if (status != OK) { |
| 160 | ALOGE("Failed to add release fence for layer %s", getName().c_str()); |
| 161 | } |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 162 | |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 163 | mPreviousReleaseFence = releaseFence; |
| 164 | |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 165 | // Prevent tracing the same release multiple times. |
| 166 | if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) { |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 167 | mPreviousReleasedFrameNumber = mPreviousFrameNumber; |
| 168 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 171 | void BufferStateLayer::onSurfaceFrameCreated( |
| 172 | const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) { |
| 173 | mPendingJankClassifications.emplace_back(surfaceFrame); |
| 174 | } |
| 175 | |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 176 | void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) { |
Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 177 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 178 | handle->transformHint = mTransformHint; |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 179 | handle->dequeueReadyTime = dequeueReadyTime; |
Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 182 | std::vector<JankData> jankData; |
| 183 | jankData.reserve(mPendingJankClassifications.size()); |
| 184 | while (!mPendingJankClassifications.empty() |
| 185 | && mPendingJankClassifications.front()->getJankType()) { |
| 186 | std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame = |
| 187 | mPendingJankClassifications.front(); |
| 188 | mPendingJankClassifications.pop_front(); |
| 189 | jankData.emplace_back( |
| 190 | JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value())); |
| 191 | } |
| 192 | |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 193 | mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles( |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 194 | mDrawingState.callbackHandles, jankData); |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 195 | |
| 196 | mDrawingState.callbackHandles = {}; |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 197 | |
| 198 | const sp<Fence>& releaseFence(mPreviousReleaseFence); |
| 199 | std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence); |
| 200 | { |
| 201 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 202 | if (mPreviousFrameNumber != 0) { |
| 203 | mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime, |
| 204 | std::move(releaseFenceTime)); |
| 205 | } |
| 206 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 209 | void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence, |
| 210 | const CompositorTiming& compositorTiming) { |
| 211 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 212 | handle->gpuCompositionDoneFence = glDoneFence; |
| 213 | handle->compositorTiming = compositorTiming; |
| 214 | } |
| 215 | } |
| 216 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 217 | bool BufferStateLayer::willPresentCurrentTransaction() const { |
| 218 | // Returns true if the most recent Transaction applied to CurrentState will be presented. |
Robert Carr | 321e83c | 2019-08-19 15:49:30 -0700 | [diff] [blame] | 219 | return (getSidebandStreamChanged() || getAutoRefresh() || |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 220 | (mCurrentState.modified && |
chaviw | 8ba8b07 | 2021-01-25 14:55:46 -0800 | [diff] [blame] | 221 | (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Valerie Hau | 3282b3c | 2020-02-03 15:37:27 -0800 | [diff] [blame] | 224 | /* TODO: vhau uncomment once deferred transaction migration complete in |
| 225 | * WindowManager |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 226 | void BufferStateLayer::pushPendingState() { |
| 227 | if (!mCurrentState.modified) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 228 | return; |
| 229 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 230 | mPendingStates.push_back(mCurrentState); |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 231 | ATRACE_INT(mTransactionName.c_str(), mPendingStates.size()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 232 | } |
Valerie Hau | 3282b3c | 2020-02-03 15:37:27 -0800 | [diff] [blame] | 233 | */ |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 234 | |
| 235 | bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) { |
Valerie Hau | 3282b3c | 2020-02-03 15:37:27 -0800 | [diff] [blame] | 236 | mCurrentStateModified = mCurrentState.modified; |
| 237 | bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit); |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 238 | if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) { |
Ady Abraham | 7f8a1e6 | 2020-09-28 16:09:35 -0700 | [diff] [blame] | 239 | // Update the acquire fence time if we have a buffer |
| 240 | mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime); |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 241 | } |
Valerie Hau | 3282b3c | 2020-02-03 15:37:27 -0800 | [diff] [blame] | 242 | mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 243 | mCurrentState.modified = false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 244 | return stateUpdateAvailable; |
| 245 | } |
| 246 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 247 | // Crop that applies to the window |
| 248 | Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const { |
| 249 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool BufferStateLayer::setTransform(uint32_t transform) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 253 | if (mCurrentState.transform == transform) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 254 | mCurrentState.transform = transform; |
| 255 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 256 | setTransactionFlags(eTransactionNeeded); |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 261 | if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false; |
| 262 | mCurrentState.sequence++; |
| 263 | mCurrentState.transformToDisplayInverse = transformToDisplayInverse; |
| 264 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 265 | setTransactionFlags(eTransactionNeeded); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | bool BufferStateLayer::setCrop(const Rect& crop) { |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 270 | Rect c = crop; |
| 271 | if (c.left < 0) { |
| 272 | c.left = 0; |
| 273 | } |
| 274 | if (c.top < 0) { |
| 275 | c.top = 0; |
| 276 | } |
| 277 | // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below |
| 278 | // treats all invalid rectangles the same. |
| 279 | if (!c.isValid()) { |
| 280 | c.makeInvalid(); |
| 281 | } |
| 282 | |
| 283 | if (mCurrentState.crop == c) return false; |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 284 | mCurrentState.crop = c; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 285 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 286 | setTransactionFlags(eTransactionNeeded); |
| 287 | return true; |
| 288 | } |
| 289 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 290 | bool BufferStateLayer::setFrame(const Rect& frame) { |
| 291 | int x = frame.left; |
| 292 | int y = frame.top; |
| 293 | int w = frame.getWidth(); |
| 294 | int h = frame.getHeight(); |
| 295 | |
Marissa Wall | 0f3242d | 2018-12-20 15:10:22 -0800 | [diff] [blame] | 296 | if (x < 0) { |
| 297 | x = 0; |
| 298 | w = frame.right; |
| 299 | } |
| 300 | |
| 301 | if (y < 0) { |
| 302 | y = 0; |
| 303 | h = frame.bottom; |
| 304 | } |
| 305 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 306 | if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y && |
| 307 | mCurrentState.active.w == w && mCurrentState.active.h == h) { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
| 311 | if (!frame.isValid()) { |
| 312 | x = y = w = h = 0; |
| 313 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 314 | mCurrentState.active.transform.set(x, y); |
| 315 | mCurrentState.active.w = w; |
| 316 | mCurrentState.active.h = h; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 317 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 318 | mCurrentState.sequence++; |
| 319 | mCurrentState.modified = true; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 320 | setTransactionFlags(eTransactionNeeded); |
| 321 | return true; |
| 322 | } |
| 323 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 324 | bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime, |
| 325 | nsecs_t desiredPresentTime) { |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 326 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 327 | mAcquireTimeline.updateSignalTimes(); |
| 328 | std::shared_ptr<FenceTime> acquireFenceTime = |
| 329 | std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE)); |
| 330 | NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime, |
| 331 | acquireFenceTime}; |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 332 | mFrameEventHistory.setProducerWantsEvents(); |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 333 | mFrameEventHistory.addQueue(newTimestamps); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence, |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 338 | nsecs_t postTime, nsecs_t desiredPresentTime, bool isAutoTimestamp, |
Vishnu Nair | adf632b | 2021-01-07 14:05:08 -0800 | [diff] [blame] | 339 | const client_cache_t& clientCacheId, uint64_t frameNumber, |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame^] | 340 | std::optional<nsecs_t> /* dequeueTime */, |
| 341 | const FrameTimelineInfo& info) { |
Robert Carr | 0c1966e | 2020-10-19 12:12:08 -0700 | [diff] [blame] | 342 | ATRACE_CALL(); |
| 343 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 344 | if (mCurrentState.buffer) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 345 | mReleasePreviousBuffer = true; |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 346 | if (mCurrentState.buffer != mDrawingState.buffer) { |
| 347 | // If mCurrentState has a buffer, and we are about to update again |
| 348 | // before swapping to drawing state, then the first buffer will be |
| 349 | // dropped and we should decrement the pending buffer count. |
| 350 | decrementPendingBufferCount(); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame^] | 351 | if (mCurrentState.bufferSurfaceFrameTX != nullptr) { |
| 352 | addSurfaceFrameDroppedForBuffer(mCurrentState.bufferSurfaceFrameTX); |
| 353 | mCurrentState.bufferSurfaceFrameTX.reset(); |
| 354 | } |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 355 | } |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Vishnu Nair | 6b7c5c9 | 2020-09-29 17:27:05 -0700 | [diff] [blame] | 358 | mCurrentState.frameNumber = frameNumber; |
Valerie Hau | 2f54d64 | 2020-01-22 09:37:03 -0800 | [diff] [blame] | 359 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 360 | mCurrentState.buffer = buffer; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 361 | mCurrentState.clientCacheId = clientCacheId; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 362 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 363 | setTransactionFlags(eTransactionNeeded); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 364 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 365 | const int32_t layerId = getSequence(); |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 366 | mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(), |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 367 | mOwnerUid, postTime); |
chaviw | fa67b55 | 2019-08-12 16:51:55 -0700 | [diff] [blame] | 368 | mCurrentState.desiredPresentTime = desiredPresentTime; |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 369 | mCurrentState.isAutoTimestamp = isAutoTimestamp; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 370 | |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 371 | mFlinger->mScheduler->recordLayerHistory(this, isAutoTimestamp ? 0 : desiredPresentTime, |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 372 | LayerHistory::LayerUpdateType::Buffer); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 373 | |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 374 | addFrameEvent(acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame^] | 375 | |
| 376 | if (info.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 377 | setFrameTimelineVsyncForBufferTransaction(info, postTime); |
| 378 | } |
| 379 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
| 383 | bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 384 | // The acquire fences of BufferStateLayers have already signaled before they are set |
| 385 | mCallbackHandleAcquireTime = fence->getSignalTime(); |
| 386 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 387 | mCurrentState.acquireFence = fence; |
| 388 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 389 | setTransactionFlags(eTransactionNeeded); |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 394 | if (mCurrentState.dataspace == dataspace) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 395 | mCurrentState.dataspace = dataspace; |
| 396 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 397 | setTransactionFlags(eTransactionNeeded); |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 402 | if (mCurrentState.hdrMetadata == hdrMetadata) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 403 | mCurrentState.hdrMetadata = hdrMetadata; |
| 404 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 405 | setTransactionFlags(eTransactionNeeded); |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 410 | mCurrentState.surfaceDamageRegion = surfaceDamage; |
| 411 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 412 | setTransactionFlags(eTransactionNeeded); |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | bool BufferStateLayer::setApi(int32_t api) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 417 | if (mCurrentState.api == api) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 418 | mCurrentState.api = api; |
| 419 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 420 | setTransactionFlags(eTransactionNeeded); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 425 | if (mCurrentState.sidebandStream == sidebandStream) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 426 | mCurrentState.sidebandStream = sidebandStream; |
| 427 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 428 | setTransactionFlags(eTransactionNeeded); |
| 429 | |
| 430 | if (!mSidebandStreamChanged.exchange(true)) { |
| 431 | // mSidebandStreamChanged was false |
| 432 | mFlinger->signalLayerUpdate(); |
| 433 | } |
| 434 | return true; |
| 435 | } |
| 436 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 437 | bool BufferStateLayer::setTransactionCompletedListeners( |
| 438 | const std::vector<sp<CallbackHandle>>& handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 439 | // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 440 | if (handles.empty()) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 441 | mReleasePreviousBuffer = false; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
| 445 | const bool willPresent = willPresentCurrentTransaction(); |
| 446 | |
| 447 | for (const auto& handle : handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 448 | // If this transaction set a buffer on this layer, release its previous buffer |
| 449 | handle->releasePreviousBuffer = mReleasePreviousBuffer; |
| 450 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 451 | // If this layer will be presented in this frame |
| 452 | if (willPresent) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 453 | // If this transaction set an acquire fence on this layer, set its acquire time |
| 454 | handle->acquireTime = mCallbackHandleAcquireTime; |
| 455 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 456 | // Notify the transaction completed thread that there is a pending latched callback |
| 457 | // handle |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 458 | mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 459 | |
| 460 | // Store so latched time and release fence can be set |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 461 | mCurrentState.callbackHandles.push_back(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 462 | |
| 463 | } else { // If this layer will NOT need to be relatched and presented this frame |
| 464 | // Notify the transaction completed thread this handle is done |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 465 | mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 469 | mReleasePreviousBuffer = false; |
| 470 | mCallbackHandleAcquireTime = -1; |
| 471 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 472 | return willPresent; |
| 473 | } |
| 474 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 475 | bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 476 | mCurrentState.transparentRegionHint = transparent; |
| 477 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 478 | setTransactionFlags(eTransactionNeeded); |
| 479 | return true; |
| 480 | } |
| 481 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 482 | Rect BufferStateLayer::getBufferSize(const State& s) const { |
| 483 | // for buffer state layers we use the display frame size as the buffer size. |
| 484 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 485 | return Rect(getActiveWidth(s), getActiveHeight(s)); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 486 | } |
| 487 | |
chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 488 | if (mBufferInfo.mBuffer == nullptr) { |
| 489 | return Rect::INVALID_RECT; |
| 490 | } |
| 491 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 492 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 493 | const auto& p = mDrawingParent.promote(); |
| 494 | if (p != nullptr) { |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 495 | Rect parentBounds = Rect(p->getBounds(Region())); |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 496 | if (!parentBounds.isEmpty()) { |
| 497 | return parentBounds; |
| 498 | } |
| 499 | } |
| 500 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 501 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 502 | } |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 503 | |
| 504 | FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const { |
| 505 | const State& s(getDrawingState()); |
| 506 | // for buffer state layers we use the display frame size as the buffer size. |
| 507 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 508 | return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s)); |
| 509 | } |
| 510 | |
| 511 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 512 | return parentBounds; |
| 513 | } |
| 514 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 515 | // ----------------------------------------------------------------------- |
| 516 | |
| 517 | // ----------------------------------------------------------------------- |
| 518 | // Interface implementation for BufferLayer |
| 519 | // ----------------------------------------------------------------------- |
| 520 | bool BufferStateLayer::fenceHasSignaled() const { |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 521 | const bool fenceSignaled = |
| 522 | getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled; |
| 523 | if (!fenceSignaled) { |
| 524 | mFlinger->mTimeStats->incrementLatchSkipped(getSequence(), |
| 525 | TimeStats::LatchSkipReason::LateAcquire); |
| 526 | } |
| 527 | |
| 528 | return fenceSignaled; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 531 | bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const { |
Ady Abraham | cd1580c | 2019-04-29 15:40:03 -0700 | [diff] [blame] | 532 | if (!hasFrameUpdate() || isRemovedFromCurrentState()) { |
| 533 | return true; |
| 534 | } |
| 535 | |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 536 | return mCurrentState.isAutoTimestamp || mCurrentState.desiredPresentTime <= expectedPresentTime; |
Ady Abraham | cd1580c | 2019-04-29 15:40:03 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 539 | bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) { |
| 540 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 541 | handle->refreshStartTime = refreshStartTime; |
| 542 | } |
| 543 | return BufferLayer::onPreComposition(refreshStartTime); |
| 544 | } |
| 545 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 546 | uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const { |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 547 | return mDrawingState.frameNumber; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Robert Carr | fe1209c | 2020-02-11 12:25:35 -0800 | [diff] [blame] | 550 | /** |
| 551 | * This is the frameNumber used for deferred transaction signalling. We need to use this because |
| 552 | * of cases where we defer a transaction for a surface to itself. In the BLAST world this |
| 553 | * may not make a huge amount of sense (Why not just merge the Buffer transaction with the |
| 554 | * deferred transaction?) but this is an important legacy use case, for example moving |
| 555 | * a window at the same time it draws makes use of this kind of technique. So anyway |
| 556 | * imagine we have something like this: |
| 557 | * |
| 558 | * Transaction { // containing |
| 559 | * Buffer -> frameNumber = 2 |
| 560 | * DeferTransactionUntil -> frameNumber = 2 |
| 561 | * Random other stuff |
| 562 | * } |
| 563 | * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber). |
| 564 | * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we |
| 565 | * haven't swapped mCurrentState to mDrawingState yet we will think the sync point |
| 566 | * is not ready. So we will return false from applyPendingState and not swap |
| 567 | * current state to drawing state. But because we don't swap current state |
| 568 | * to drawing state the number will never update and we will be stuck. This way |
| 569 | * we can see we need to return the frame number for the buffer we are about |
| 570 | * to apply. |
| 571 | */ |
| 572 | uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const { |
| 573 | return mCurrentState.frameNumber; |
| 574 | } |
| 575 | |
Vishnu Nair | cf26a0a | 2020-11-13 12:56:20 -0800 | [diff] [blame] | 576 | void BufferStateLayer::setAutoRefresh(bool autoRefresh) { |
| 577 | if (!mAutoRefresh.exchange(autoRefresh)) { |
| 578 | mFlinger->signalLayerUpdate(); |
| 579 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 582 | bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 583 | if (mSidebandStreamChanged.exchange(false)) { |
| 584 | const State& s(getDrawingState()); |
| 585 | // mSidebandStreamChanged was true |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 586 | mSidebandStream = s.sidebandStream; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 587 | editCompositionState()->sidebandStream = mSidebandStream; |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 588 | if (mSidebandStream != nullptr) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 589 | setTransactionFlags(eTransactionNeeded); |
| 590 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 591 | } |
| 592 | recomputeVisibleRegions = true; |
| 593 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 594 | return true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 595 | } |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 596 | return false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 599 | bool BufferStateLayer::hasFrameUpdate() const { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 600 | const State& c(getCurrentState()); |
| 601 | return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Ady Abraham | 63a3e59 | 2021-01-06 10:47:15 -0800 | [diff] [blame] | 604 | std::optional<nsecs_t> BufferStateLayer::nextPredictedPresentTime() const { |
Ady Abraham | ce4adf1 | 2020-12-15 18:45:12 -0800 | [diff] [blame] | 605 | if (!getDrawingState().isAutoTimestamp || !mSurfaceFrame) { |
Ady Abraham | 63a3e59 | 2021-01-06 10:47:15 -0800 | [diff] [blame] | 606 | return std::nullopt; |
Ady Abraham | ce4adf1 | 2020-12-15 18:45:12 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | return mSurfaceFrame->getPredictions().presentTime; |
| 610 | } |
| 611 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 612 | status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime, |
| 613 | nsecs_t /*expectedPresentTime*/) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 614 | const State& s(getDrawingState()); |
| 615 | |
| 616 | if (!s.buffer) { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 617 | if (s.bgColorLayer) { |
| 618 | for (auto& handle : mDrawingState.callbackHandles) { |
| 619 | handle->latchTime = latchTime; |
| 620 | } |
| 621 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 622 | return NO_ERROR; |
| 623 | } |
| 624 | |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 625 | for (auto& handle : mDrawingState.callbackHandles) { |
| 626 | handle->latchTime = latchTime; |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 627 | handle->frameNumber = mDrawingState.frameNumber; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 628 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 629 | |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 630 | const int32_t layerId = getSequence(); |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 631 | mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber, |
chaviw | 95631e3 | 2020-06-09 13:43:32 -0700 | [diff] [blame] | 632 | std::make_shared<FenceTime>(mDrawingState.acquireFence)); |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 633 | mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 634 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame^] | 635 | auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX; |
| 636 | if (bufferSurfaceFrame != nullptr && |
| 637 | bufferSurfaceFrame->getPresentState() != PresentState::Presented) { |
| 638 | // Update only if the bufferSurfaceFrame wasn't already presented. A Presented |
| 639 | // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we |
| 640 | // are processing the next state. |
| 641 | addSurfaceFramePresentedForBuffer(bufferSurfaceFrame, |
| 642 | mDrawingState.acquireFence->getSignalTime(), latchTime); |
| 643 | bufferSurfaceFrame.reset(); |
| 644 | } |
| 645 | |
Marissa Wall | 16c112d | 2019-03-20 13:21:13 -0700 | [diff] [blame] | 646 | mCurrentStateModified = false; |
| 647 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 648 | return NO_ERROR; |
| 649 | } |
| 650 | |
| 651 | status_t BufferStateLayer::updateActiveBuffer() { |
| 652 | const State& s(getDrawingState()); |
| 653 | |
| 654 | if (s.buffer == nullptr) { |
| 655 | return BAD_VALUE; |
| 656 | } |
chaviw | df3c5e8 | 2021-01-07 13:00:37 -0800 | [diff] [blame] | 657 | |
| 658 | if (s.buffer != mBufferInfo.mBuffer) { |
| 659 | decrementPendingBufferCount(); |
| 660 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 661 | |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 662 | mPreviousBufferId = getCurrentBufferId(); |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 663 | mBufferInfo.mBuffer = s.buffer; |
| 664 | mBufferInfo.mFence = s.acquireFence; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 665 | |
| 666 | return NO_ERROR; |
| 667 | } |
| 668 | |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 669 | status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 670 | // TODO(marissaw): support frame history events |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 671 | mPreviousFrameNumber = mCurrentFrameNumber; |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 672 | mCurrentFrameNumber = mDrawingState.frameNumber; |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 673 | { |
| 674 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 675 | mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime); |
| 676 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 677 | return NO_ERROR; |
| 678 | } |
| 679 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 680 | void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) { |
| 681 | std::lock_guard lock(mMutex); |
| 682 | if (!clientCacheId.isValid()) { |
| 683 | ALOGE("invalid process, failed to erase buffer"); |
| 684 | return; |
| 685 | } |
| 686 | eraseBufferLocked(clientCacheId); |
| 687 | } |
| 688 | |
| 689 | uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) { |
| 690 | std::lock_guard<std::mutex> lock(mMutex); |
| 691 | auto itr = mCachedBuffers.find(clientCacheId); |
| 692 | if (itr == mCachedBuffers.end()) { |
| 693 | return addCachedBuffer(clientCacheId); |
| 694 | } |
| 695 | auto& [hwcCacheSlot, counter] = itr->second; |
| 696 | counter = mCounter++; |
| 697 | return hwcCacheSlot; |
| 698 | } |
| 699 | |
| 700 | uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId) |
| 701 | REQUIRES(mMutex) { |
| 702 | if (!clientCacheId.isValid()) { |
| 703 | ALOGE("invalid process, returning invalid slot"); |
| 704 | return BufferQueue::INVALID_BUFFER_SLOT; |
| 705 | } |
| 706 | |
| 707 | ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this)); |
| 708 | |
| 709 | uint32_t hwcCacheSlot = getFreeHwcCacheSlot(); |
| 710 | mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++}; |
| 711 | return hwcCacheSlot; |
| 712 | } |
| 713 | |
| 714 | uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) { |
| 715 | if (mFreeHwcCacheSlots.empty()) { |
| 716 | evictLeastRecentlyUsed(); |
| 717 | } |
| 718 | |
| 719 | uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top(); |
| 720 | mFreeHwcCacheSlots.pop(); |
| 721 | return hwcCacheSlot; |
| 722 | } |
| 723 | |
| 724 | void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) { |
| 725 | uint64_t minCounter = UINT_MAX; |
| 726 | client_cache_t minClientCacheId = {}; |
| 727 | for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) { |
| 728 | const auto& [hwcCacheSlot, counter] = slotCounter; |
| 729 | if (counter < minCounter) { |
| 730 | minCounter = counter; |
| 731 | minClientCacheId = clientCacheId; |
| 732 | } |
| 733 | } |
| 734 | eraseBufferLocked(minClientCacheId); |
| 735 | |
| 736 | ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this); |
| 737 | } |
| 738 | |
| 739 | void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId) |
| 740 | REQUIRES(mMutex) { |
| 741 | auto itr = mCachedBuffers.find(clientCacheId); |
| 742 | if (itr == mCachedBuffers.end()) { |
| 743 | return; |
| 744 | } |
| 745 | auto& [hwcCacheSlot, counter] = itr->second; |
| 746 | |
| 747 | // TODO send to hwc cache and resources |
| 748 | |
| 749 | mFreeHwcCacheSlots.push(hwcCacheSlot); |
| 750 | mCachedBuffers.erase(clientCacheId); |
| 751 | } |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 752 | |
| 753 | void BufferStateLayer::gatherBufferInfo() { |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 754 | BufferLayer::gatherBufferInfo(); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 755 | |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 756 | const State& s(getDrawingState()); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 757 | mBufferInfo.mDesiredPresentTime = s.desiredPresentTime; |
| 758 | mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence); |
| 759 | mBufferInfo.mFence = s.acquireFence; |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 760 | mBufferInfo.mTransform = s.transform; |
| 761 | mBufferInfo.mDataspace = translateDataspace(s.dataspace); |
| 762 | mBufferInfo.mCrop = computeCrop(s); |
| 763 | mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 764 | mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion; |
| 765 | mBufferInfo.mHdrMetadata = s.hdrMetadata; |
| 766 | mBufferInfo.mApi = s.api; |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 767 | mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse; |
chaviw | f83ce18 | 2019-09-12 14:43:08 -0700 | [diff] [blame] | 768 | mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Robert Carr | 916b036 | 2020-10-06 13:53:03 -0700 | [diff] [blame] | 771 | uint32_t BufferStateLayer::getEffectiveScalingMode() const { |
| 772 | return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 773 | } |
| 774 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 775 | Rect BufferStateLayer::computeCrop(const State& s) { |
| 776 | if (s.crop.isEmpty() && s.buffer) { |
| 777 | return s.buffer->getBounds(); |
| 778 | } else if (s.buffer) { |
| 779 | Rect crop = s.crop; |
| 780 | crop.left = std::max(crop.left, 0); |
| 781 | crop.top = std::max(crop.top, 0); |
| 782 | uint32_t bufferWidth = s.buffer->getWidth(); |
| 783 | uint32_t bufferHeight = s.buffer->getHeight(); |
| 784 | if (bufferHeight <= std::numeric_limits<int32_t>::max() && |
| 785 | bufferWidth <= std::numeric_limits<int32_t>::max()) { |
| 786 | crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth)); |
| 787 | crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight)); |
| 788 | } |
| 789 | if (!crop.isValid()) { |
| 790 | // Crop rect is out of bounds, return whole buffer |
| 791 | return s.buffer->getBounds(); |
| 792 | } |
| 793 | return crop; |
| 794 | } |
| 795 | return s.crop; |
| 796 | } |
| 797 | |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 798 | sp<Layer> BufferStateLayer::createClone() { |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 799 | LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata()); |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 800 | args.textureName = mTextureName; |
Lloyd Pique | 1c3a5eb | 2019-10-03 13:07:08 -0700 | [diff] [blame] | 801 | sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args); |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 802 | layer->mHwcSlotGenerator = mHwcSlotGenerator; |
| 803 | layer->setInitialValuesForClone(this); |
| 804 | return layer; |
| 805 | } |
Valerie Hau | 92bf548 | 2020-02-10 09:49:08 -0800 | [diff] [blame] | 806 | |
| 807 | Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const { |
| 808 | const auto& p = mDrawingParent.promote(); |
| 809 | if (p != nullptr) { |
| 810 | RoundedCornerState parentState = p->getRoundedCornerState(); |
| 811 | if (parentState.radius > 0) { |
| 812 | ui::Transform t = getActiveTransform(getDrawingState()); |
| 813 | t = t.inverse(); |
| 814 | parentState.cropRect = t.transform(parentState.cropRect); |
| 815 | // The rounded corners shader only accepts 1 corner radius for performance reasons, |
| 816 | // but a transform matrix can define horizontal and vertical scales. |
| 817 | // Let's take the average between both of them and pass into the shader, practically we |
| 818 | // never do this type of transformation on windows anyway. |
| 819 | parentState.radius *= (t[0][0] + t[1][1]) / 2.0f; |
| 820 | return parentState; |
| 821 | } |
| 822 | } |
| 823 | const float radius = getDrawingState().cornerRadius; |
| 824 | const State& s(getDrawingState()); |
| 825 | if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX)) |
| 826 | return RoundedCornerState(); |
| 827 | return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()), |
| 828 | static_cast<float>(s.active.transform.ty()), |
| 829 | static_cast<float>(s.active.transform.tx() + s.active.w), |
| 830 | static_cast<float>(s.active.transform.ty() + s.active.h)), |
| 831 | radius); |
| 832 | } |
Vishnu Nair | e7f79c5 | 2020-10-29 14:45:03 -0700 | [diff] [blame] | 833 | |
| 834 | bool BufferStateLayer::bufferNeedsFiltering() const { |
| 835 | const State& s(getDrawingState()); |
| 836 | if (!s.buffer) { |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | uint32_t bufferWidth = s.buffer->width; |
| 841 | uint32_t bufferHeight = s.buffer->height; |
| 842 | |
| 843 | // Undo any transformations on the buffer and return the result. |
| 844 | if (s.transform & ui::Transform::ROT_90) { |
| 845 | std::swap(bufferWidth, bufferHeight); |
| 846 | } |
| 847 | |
| 848 | if (s.transformToDisplayInverse) { |
| 849 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
| 850 | if (invTransform & ui::Transform::ROT_90) { |
| 851 | std::swap(bufferWidth, bufferHeight); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | const Rect layerSize{getBounds()}; |
| 856 | return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight; |
| 857 | } |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 858 | |
| 859 | void BufferStateLayer::incrementPendingBufferCount() { |
| 860 | mPendingBufferTransactions++; |
| 861 | tracePendingBufferCount(); |
| 862 | } |
| 863 | |
| 864 | void BufferStateLayer::decrementPendingBufferCount() { |
| 865 | mPendingBufferTransactions--; |
| 866 | tracePendingBufferCount(); |
| 867 | } |
| 868 | |
| 869 | void BufferStateLayer::tracePendingBufferCount() { |
| 870 | ATRACE_INT(mBlastTransactionName.c_str(), mPendingBufferTransactions); |
| 871 | } |
| 872 | |
| 873 | uint32_t BufferStateLayer::doTransaction(uint32_t flags) { |
| 874 | if (mDrawingState.buffer != nullptr && mDrawingState.buffer != mBufferInfo.mBuffer) { |
| 875 | // If we are about to update mDrawingState.buffer but it has not yet latched |
| 876 | // then we will drop a buffer and should decrement the pending buffer count. |
| 877 | // This logic may not work perfectly in the face of a BufferStateLayer being the |
| 878 | // deferred side of a deferred transaction, but we don't expect this use case. |
| 879 | decrementPendingBufferCount(); |
| 880 | } |
| 881 | return Layer::doTransaction(flags); |
| 882 | } |
| 883 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 884 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 885 | |
| 886 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 887 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |