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