Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // #define LOG_NDEBUG 0 |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "LayerSnapshotBuilder" |
| 21 | |
| 22 | #include "LayerSnapshotBuilder.h" |
| 23 | #include <gui/TraceUtils.h> |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 24 | #include <ui/FloatRect.h> |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 25 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 26 | #include <numeric> |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 27 | #include <optional> |
| 28 | |
| 29 | #include <gui/TraceUtils.h> |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 30 | #include "DisplayHardware/HWC2.h" |
| 31 | #include "DisplayHardware/Hal.h" |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 32 | #include "LayerLog.h" |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 33 | #include "LayerSnapshotBuilder.h" |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 34 | #include "TimeStats/TimeStats.h" |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 35 | #include "ftl/small_map.h" |
| 36 | |
| 37 | namespace android::surfaceflinger::frontend { |
| 38 | |
| 39 | using namespace ftl::flag_operators; |
| 40 | |
| 41 | namespace { |
| 42 | FloatRect getMaxDisplayBounds( |
| 43 | const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) { |
| 44 | const ui::Size maxSize = [&displays] { |
| 45 | if (displays.empty()) return ui::Size{5000, 5000}; |
| 46 | |
| 47 | return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize, |
| 48 | [](ui::Size size, const auto& pair) -> ui::Size { |
| 49 | const auto& display = pair.second; |
| 50 | return {std::max(size.getWidth(), display.info.logicalWidth), |
| 51 | std::max(size.getHeight(), display.info.logicalHeight)}; |
| 52 | }); |
| 53 | }(); |
| 54 | |
| 55 | // Ignore display bounds for now since they will be computed later. Use a large Rect bound |
| 56 | // to ensure it's bigger than an actual display will be. |
| 57 | const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f; |
| 58 | const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f; |
| 59 | |
| 60 | return {-xMax, -yMax, xMax, yMax}; |
| 61 | } |
| 62 | |
| 63 | // Applies the given transform to the region, while protecting against overflows caused by any |
| 64 | // offsets. If applying the offset in the transform to any of the Rects in the region would result |
| 65 | // in an overflow, they are not added to the output Region. |
| 66 | Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r, |
| 67 | const std::string& debugWindowName) { |
| 68 | // Round the translation using the same rounding strategy used by ui::Transform. |
| 69 | const auto tx = static_cast<int32_t>(t.tx() + 0.5); |
| 70 | const auto ty = static_cast<int32_t>(t.ty() + 0.5); |
| 71 | |
| 72 | ui::Transform transformWithoutOffset = t; |
| 73 | transformWithoutOffset.set(0.f, 0.f); |
| 74 | |
| 75 | const Region transformed = transformWithoutOffset.transform(r); |
| 76 | |
| 77 | // Apply the translation to each of the Rects in the region while discarding any that overflow. |
| 78 | Region ret; |
| 79 | for (const auto& rect : transformed) { |
| 80 | Rect newRect; |
| 81 | if (__builtin_add_overflow(rect.left, tx, &newRect.left) || |
| 82 | __builtin_add_overflow(rect.top, ty, &newRect.top) || |
| 83 | __builtin_add_overflow(rect.right, tx, &newRect.right) || |
| 84 | __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) { |
| 85 | ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.", |
| 86 | debugWindowName.c_str()); |
| 87 | continue; |
| 88 | } |
| 89 | ret.orSelf(newRect); |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * We don't want to send the layer's transform to input, but rather the |
| 96 | * parent's transform. This is because Layer's transform is |
| 97 | * information about how the buffer is placed on screen. The parent's |
| 98 | * transform makes more sense to send since it's information about how the |
| 99 | * layer is placed on screen. This transform is used by input to determine |
| 100 | * how to go from screen space back to window space. |
| 101 | */ |
| 102 | ui::Transform getInputTransform(const LayerSnapshot& snapshot) { |
| 103 | if (!snapshot.hasBufferOrSidebandStream()) { |
| 104 | return snapshot.geomLayerTransform; |
| 105 | } |
| 106 | return snapshot.parentTransform; |
| 107 | } |
| 108 | |
| 109 | /** |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 110 | * Returns the bounds used to fill the input frame and the touchable region. |
| 111 | * |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 112 | * Similar to getInputTransform, we need to update the bounds to include the transform. |
| 113 | * This is because bounds don't include the buffer transform, where the input assumes |
| 114 | * that's already included. |
| 115 | */ |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 116 | std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) { |
| 117 | FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect(); |
| 118 | if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() && |
| 119 | snapshot.localTransform.getType() != ui::Transform::IDENTITY) { |
| 120 | inputBounds = snapshot.localTransform.transform(inputBounds); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 123 | bool inputBoundsValid = snapshot.croppedBufferSize.isValid(); |
| 124 | if (!inputBoundsValid) { |
| 125 | /** |
| 126 | * Input bounds are based on the layer crop or buffer size. But if we are using |
| 127 | * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then |
| 128 | * we can use the parent bounds as the input bounds if the layer does not have buffer |
| 129 | * or a crop. We want to unify this logic but because of compat reasons we cannot always |
| 130 | * use the parent bounds. A layer without a buffer can get input. So when a window is |
| 131 | * initially added, its touchable region can fill its parent layer bounds and that can |
| 132 | * have negative consequences. |
| 133 | */ |
| 134 | inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{}; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 135 | } |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 136 | |
| 137 | // Clamp surface inset to the input bounds. |
| 138 | const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset); |
| 139 | const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f); |
| 140 | const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f); |
| 141 | |
| 142 | // Apply the insets to the input bounds. |
| 143 | inputBounds.left += xSurfaceInset; |
| 144 | inputBounds.top += ySurfaceInset; |
| 145 | inputBounds.right -= xSurfaceInset; |
| 146 | inputBounds.bottom -= ySurfaceInset; |
| 147 | return {inputBounds, inputBoundsValid}; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 150 | Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds, |
| 151 | const ui::Transform& screenToDisplay) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 152 | // InputDispatcher works in the display device's coordinate space. Here, we calculate the |
| 153 | // frame and transform used for the layer, which determines the bounds and the coordinate space |
| 154 | // within which the layer will receive input. |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 155 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 156 | // Coordinate space definitions: |
| 157 | // - display: The display device's coordinate space. Correlates to pixels on the display. |
| 158 | // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space. |
| 159 | // - layer: The coordinate space of this layer. |
| 160 | // - input: The coordinate space in which this layer will receive input events. This could be |
| 161 | // different than layer space if a surfaceInset is used, which changes the origin |
| 162 | // of the input space. |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 163 | |
| 164 | // Crop the input bounds to ensure it is within the parent's bounds. |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 165 | const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 166 | |
| 167 | const ui::Transform layerToScreen = getInputTransform(snapshot); |
| 168 | const ui::Transform layerToDisplay = screenToDisplay * layerToScreen; |
| 169 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 170 | return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)}; |
| 171 | } |
| 172 | |
| 173 | void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay, |
| 174 | const LayerSnapshot& snapshot) { |
| 175 | auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false); |
| 176 | if (!inputBoundsValid) { |
| 177 | info.touchableRegion.clear(); |
| 178 | } |
| 179 | |
| 180 | const Rect roundedFrameInDisplay = |
| 181 | getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 182 | info.frameLeft = roundedFrameInDisplay.left; |
| 183 | info.frameTop = roundedFrameInDisplay.top; |
| 184 | info.frameRight = roundedFrameInDisplay.right; |
| 185 | info.frameBottom = roundedFrameInDisplay.bottom; |
| 186 | |
| 187 | ui::Transform inputToLayer; |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 188 | inputToLayer.set(inputBounds.left, inputBounds.top); |
| 189 | const ui::Transform layerToScreen = getInputTransform(snapshot); |
| 190 | const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 191 | |
| 192 | // InputDispatcher expects a display-to-input transform. |
| 193 | info.transform = inputToDisplay.inverse(); |
| 194 | |
| 195 | // The touchable region is specified in the input coordinate space. Change it to display space. |
| 196 | info.touchableRegion = |
| 197 | transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name); |
| 198 | } |
| 199 | |
| 200 | void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) { |
| 201 | if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | // Check if we need to drop input unconditionally |
| 206 | const gui::DropInputMode dropInputMode = snapshot.dropInputMode; |
| 207 | if (dropInputMode == gui::DropInputMode::ALL) { |
| 208 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 209 | ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str()); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | // Check if we need to check if the window is obscured by parent |
| 214 | if (dropInputMode != gui::DropInputMode::OBSCURED) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | // Check if the parent has set an alpha on the layer |
| 219 | if (parentSnapshot.color.a != 1.0_hf) { |
| 220 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 221 | ALOGV("Dropping input for %s as requested by policy because alpha=%f", |
| 222 | snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a)); |
| 223 | } |
| 224 | |
| 225 | // Check if the parent has cropped the buffer |
| 226 | Rect bufferSize = snapshot.croppedBufferSize; |
| 227 | if (!bufferSize.isValid()) { |
| 228 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | // Screenbounds are the layer bounds cropped by parents, transformed to screenspace. |
| 233 | // To check if the layer has been cropped, we take the buffer bounds, apply the local |
| 234 | // layer crop and apply the same set of transforms to move to screenspace. If the bounds |
| 235 | // match then the layer has not been cropped by its parents. |
| 236 | Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize)); |
| 237 | bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds}; |
| 238 | |
| 239 | if (croppedByParent) { |
| 240 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 241 | ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent", |
| 242 | snapshot.name.c_str()); |
| 243 | } else { |
| 244 | // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop |
| 245 | // input if the window is obscured. This check should be done in surfaceflinger but the |
| 246 | // logic currently resides in inputflinger. So pass the if_obscured check to input to only |
| 247 | // drop input events if the window is obscured. |
| 248 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED; |
| 249 | } |
| 250 | } |
| 251 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 252 | auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) { |
| 253 | auto blendMode = Hwc2::IComposerClient::BlendMode::NONE; |
| 254 | if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) { |
| 255 | blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED |
| 256 | : Hwc2::IComposerClient::BlendMode::COVERAGE; |
| 257 | } |
| 258 | return blendMode; |
| 259 | } |
| 260 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 261 | void updateSurfaceDamage(const RequestedLayerState& requested, bool hasReadyFrame, |
| 262 | bool forceFullDamage, Region& outSurfaceDamageRegion) { |
| 263 | if (!hasReadyFrame) { |
| 264 | outSurfaceDamageRegion.clear(); |
| 265 | return; |
| 266 | } |
| 267 | if (forceFullDamage) { |
| 268 | outSurfaceDamageRegion = Region::INVALID_REGION; |
| 269 | } else { |
| 270 | outSurfaceDamageRegion = requested.surfaceDamageRegion; |
| 271 | } |
| 272 | } |
| 273 | |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 274 | void updateVisibility(LayerSnapshot& snapshot, bool visible) { |
| 275 | snapshot.isVisible = visible; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 276 | |
| 277 | // TODO(b/238781169) we are ignoring this compat for now, since we will have |
| 278 | // to remove any optimization based on visibility. |
| 279 | |
| 280 | // For compatibility reasons we let layers which can receive input |
| 281 | // receive input before they have actually submitted a buffer. Because |
| 282 | // of this we use canReceiveInput instead of isVisible to check the |
| 283 | // policy-visibility, ignoring the buffer state. However for layers with |
| 284 | // hasInputInfo()==false we can use the real visibility state. |
| 285 | // We are just using these layers for occlusion detection in |
| 286 | // InputDispatcher, and obviously if they aren't visible they can't occlude |
| 287 | // anything. |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 288 | const bool visibleForInput = |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 289 | snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 290 | snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) { |
| 294 | if (requested.potentialCursor) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | if (snapshot.inputInfo.token != nullptr) { |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | if (snapshot.hasBufferOrSidebandStream()) { |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | return requested.windowInfoHandle && |
| 307 | requested.windowInfoHandle->getInfo()->inputConfig.test( |
| 308 | gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
| 309 | } |
| 310 | |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 311 | void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested, |
| 312 | const LayerSnapshotBuilder::Args& args) { |
| 313 | snapshot.metadata.clear(); |
| 314 | for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) { |
| 315 | auto compatIter = args.genericLayerMetadataKeyMap.find(key); |
| 316 | if (compatIter == std::end(args.genericLayerMetadataKeyMap)) { |
| 317 | continue; |
| 318 | } |
| 319 | const uint32_t id = compatIter->second; |
| 320 | auto it = requested.metadata.mMap.find(id); |
| 321 | if (it == std::end(requested.metadata.mMap)) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | snapshot.metadata.emplace(key, |
| 326 | compositionengine::GenericLayerMetadataEntry{mandatory, |
| 327 | it->second}); |
| 328 | } |
| 329 | } |
| 330 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 331 | void clearChanges(LayerSnapshot& snapshot) { |
| 332 | snapshot.changes.clear(); |
| 333 | snapshot.contentDirty = false; |
| 334 | snapshot.hasReadyFrame = false; |
| 335 | snapshot.sidebandStreamHasFrame = false; |
| 336 | snapshot.surfaceDamage.clear(); |
| 337 | } |
| 338 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 339 | } // namespace |
| 340 | |
| 341 | LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() { |
| 342 | LayerSnapshot snapshot; |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 343 | snapshot.path = LayerHierarchy::TraversalPath::ROOT; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 344 | snapshot.changes = ftl::Flags<RequestedLayerState::Changes>(); |
| 345 | snapshot.isHiddenByPolicyFromParent = false; |
| 346 | snapshot.isHiddenByPolicyFromRelativeParent = false; |
| 347 | snapshot.parentTransform.reset(); |
| 348 | snapshot.geomLayerTransform.reset(); |
| 349 | snapshot.geomInverseLayerTransform.reset(); |
| 350 | snapshot.geomLayerBounds = getMaxDisplayBounds({}); |
| 351 | snapshot.roundedCorner = RoundedCornerState(); |
| 352 | snapshot.stretchEffect = {}; |
| 353 | snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK; |
| 354 | snapshot.outputFilter.toInternalDisplay = false; |
| 355 | snapshot.isSecure = false; |
| 356 | snapshot.color.a = 1.0_hf; |
| 357 | snapshot.colorTransformIsIdentity = true; |
| 358 | snapshot.shadowRadius = 0.f; |
| 359 | snapshot.layerMetadata.mMap.clear(); |
| 360 | snapshot.relativeLayerMetadata.mMap.clear(); |
| 361 | snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED; |
| 362 | snapshot.dropInputMode = gui::DropInputMode::NONE; |
| 363 | snapshot.isTrustedOverlay = false; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 364 | snapshot.gameMode = gui::GameMode::Unsupported; |
| 365 | snapshot.frameRate = {}; |
| 366 | snapshot.fixedTransformHint = ui::Transform::ROT_INVALID; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 367 | return snapshot; |
| 368 | } |
| 369 | |
| 370 | LayerSnapshotBuilder::LayerSnapshotBuilder() : mRootSnapshot(getRootSnapshot()) {} |
| 371 | |
| 372 | LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 373 | args.forceUpdate = ForceUpdateFlags::ALL; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 374 | updateSnapshots(args); |
| 375 | } |
| 376 | |
| 377 | bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 378 | if (args.forceUpdate != ForceUpdateFlags::NONE || args.displayChanges) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 379 | // force update requested, or we have display changes, so skip the fast path |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
| 383 | if (args.layerLifecycleManager.getGlobalChanges().get() == 0) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 384 | return true; |
| 385 | } |
| 386 | |
| 387 | if (args.layerLifecycleManager.getGlobalChanges() != RequestedLayerState::Changes::Content) { |
| 388 | // We have changes that require us to walk the hierarchy and update child layers. |
| 389 | // No fast path for you. |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | // There are only content changes which do not require any child layer snapshots to be updated. |
| 394 | ALOGV("%s", __func__); |
| 395 | ATRACE_NAME("FastPath"); |
| 396 | |
| 397 | // Collect layers with changes |
| 398 | ftl::SmallMap<uint32_t, RequestedLayerState*, 10> layersWithChanges; |
| 399 | for (auto& layer : args.layerLifecycleManager.getLayers()) { |
| 400 | if (layer->changes.test(RequestedLayerState::Changes::Content)) { |
| 401 | layersWithChanges.emplace_or_replace(layer->id, layer.get()); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Walk through the snapshots, clearing previous change flags and updating the snapshots |
| 406 | // if needed. |
| 407 | for (auto& snapshot : mSnapshots) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 408 | auto it = layersWithChanges.find(snapshot->path.id); |
| 409 | if (it != layersWithChanges.end()) { |
| 410 | ALOGV("%s fast path snapshot changes = %s", __func__, |
| 411 | mRootSnapshot.changes.string().c_str()); |
| 412 | LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT; |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 413 | updateSnapshot(*snapshot, args, *it->second, mRootSnapshot, root); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | void LayerSnapshotBuilder::updateSnapshots(const Args& args) { |
| 420 | ATRACE_NAME("UpdateSnapshots"); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 421 | if (args.parentCrop) { |
| 422 | mRootSnapshot.geomLayerBounds = *args.parentCrop; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 423 | } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 424 | mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays); |
| 425 | } |
| 426 | if (args.displayChanges) { |
| 427 | mRootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren | |
| 428 | RequestedLayerState::Changes::Geometry; |
| 429 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 430 | if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) { |
| 431 | mRootSnapshot.changes |= |
| 432 | RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility; |
| 433 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 434 | LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 435 | if (args.root.getLayer()) { |
| 436 | // The hierarchy can have a root layer when used for screenshots otherwise, it will have |
| 437 | // multiple children. |
| 438 | LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id, |
| 439 | LayerHierarchy::Variant::Attached); |
| 440 | updateSnapshotsInHierarchy(args, args.root, root, mRootSnapshot); |
| 441 | } else { |
| 442 | for (auto& [childHierarchy, variant] : args.root.mChildren) { |
| 443 | LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, |
| 444 | childHierarchy->getLayer()->id, |
| 445 | variant); |
| 446 | updateSnapshotsInHierarchy(args, *childHierarchy, root, mRootSnapshot); |
| 447 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 450 | const bool hasUnreachableSnapshots = sortSnapshotsByZ(args); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 451 | clearChanges(mRootSnapshot); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 452 | |
| 453 | // Destroy unreachable snapshots |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 454 | if (!hasUnreachableSnapshots) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 458 | auto it = mSnapshots.begin(); |
| 459 | while (it < mSnapshots.end()) { |
| 460 | auto& traversalPath = it->get()->path; |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 461 | if (!it->get()->unreachable) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 462 | it++; |
| 463 | continue; |
| 464 | } |
| 465 | |
| 466 | mIdToSnapshot.erase(traversalPath); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 467 | mSnapshots.back()->globalZ = it->get()->globalZ; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 468 | std::iter_swap(it, mSnapshots.end() - 1); |
| 469 | mSnapshots.erase(mSnapshots.end() - 1); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void LayerSnapshotBuilder::update(const Args& args) { |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 474 | for (auto& snapshot : mSnapshots) { |
| 475 | clearChanges(*snapshot); |
| 476 | } |
| 477 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 478 | if (tryFastUpdate(args)) { |
| 479 | return; |
| 480 | } |
| 481 | updateSnapshots(args); |
| 482 | } |
| 483 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 484 | const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy( |
| 485 | const Args& args, const LayerHierarchy& hierarchy, |
| 486 | LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 487 | const RequestedLayerState* layer = hierarchy.getLayer(); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 488 | LayerSnapshot* snapshot = getSnapshot(traversalPath); |
| 489 | const bool newSnapshot = snapshot == nullptr; |
| 490 | if (newSnapshot) { |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 491 | snapshot = createSnapshot(traversalPath, *layer, parentSnapshot); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 492 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 493 | scheduler::LayerInfo::FrameRate oldFrameRate = snapshot->frameRate; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 494 | if (traversalPath.isRelative()) { |
| 495 | bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative; |
| 496 | updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args); |
| 497 | } else { |
| 498 | if (traversalPath.isAttached()) { |
| 499 | resetRelativeState(*snapshot); |
| 500 | } |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 501 | updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | for (auto& [childHierarchy, variant] : hierarchy.mChildren) { |
| 505 | LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath, |
| 506 | childHierarchy->getLayer()->id, |
| 507 | variant); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 508 | const LayerSnapshot& childSnapshot = |
| 509 | updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot); |
| 510 | updateChildState(*snapshot, childSnapshot, args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 511 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 512 | |
| 513 | if (oldFrameRate == snapshot->frameRate) { |
| 514 | snapshot->changes.clear(RequestedLayerState::Changes::FrameRate); |
| 515 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 516 | return *snapshot; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const { |
| 520 | if (layerId == UNASSIGNED_LAYER_ID) { |
| 521 | return nullptr; |
| 522 | } |
| 523 | LayerHierarchy::TraversalPath path{.id = layerId}; |
| 524 | return getSnapshot(path); |
| 525 | } |
| 526 | |
| 527 | LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const { |
| 528 | auto it = mIdToSnapshot.find(id); |
| 529 | return it == mIdToSnapshot.end() ? nullptr : it->second; |
| 530 | } |
| 531 | |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 532 | LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path, |
| 533 | const RequestedLayerState& layer, |
| 534 | const LayerSnapshot& parentSnapshot) { |
| 535 | mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path)); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 536 | LayerSnapshot* snapshot = mSnapshots.back().get(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 537 | snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1; |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 538 | if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) { |
| 539 | snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath; |
| 540 | } |
| 541 | mIdToSnapshot[path] = snapshot; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 542 | return snapshot; |
| 543 | } |
| 544 | |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 545 | bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 546 | if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE && |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 547 | !args.layerLifecycleManager.getGlobalChanges().any( |
| 548 | RequestedLayerState::Changes::Hierarchy | |
| 549 | RequestedLayerState::Changes::Visibility)) { |
| 550 | // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting |
| 551 | // the snapshots. |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 552 | return false; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 553 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 554 | mResortSnapshots = false; |
| 555 | |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 556 | for (auto& snapshot : mSnapshots) { |
| 557 | snapshot->unreachable = true; |
| 558 | } |
| 559 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 560 | size_t globalZ = 0; |
| 561 | args.root.traverseInZOrder( |
| 562 | [this, &globalZ](const LayerHierarchy&, |
| 563 | const LayerHierarchy::TraversalPath& traversalPath) -> bool { |
| 564 | LayerSnapshot* snapshot = getSnapshot(traversalPath); |
| 565 | if (!snapshot) { |
| 566 | return false; |
| 567 | } |
| 568 | |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 569 | snapshot->unreachable = false; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 570 | if (snapshot->getIsVisible() || snapshot->hasInputInfo()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 571 | updateVisibility(*snapshot, snapshot->getIsVisible()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 572 | size_t oldZ = snapshot->globalZ; |
| 573 | size_t newZ = globalZ++; |
| 574 | snapshot->globalZ = newZ; |
| 575 | if (oldZ == newZ) { |
| 576 | return true; |
| 577 | } |
| 578 | mSnapshots[newZ]->globalZ = oldZ; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 579 | LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ, |
| 580 | snapshot->getDebugString().c_str()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 581 | std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ), |
| 582 | mSnapshots.begin() + static_cast<ssize_t>(newZ)); |
| 583 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 584 | return true; |
| 585 | }); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 586 | mNumInterestingSnapshots = (int)globalZ; |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 587 | bool hasUnreachableSnapshots = false; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 588 | while (globalZ < mSnapshots.size()) { |
| 589 | mSnapshots[globalZ]->globalZ = globalZ; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 590 | /* mark unreachable snapshots as explicitly invisible */ |
| 591 | updateVisibility(*mSnapshots[globalZ], false); |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 592 | if (mSnapshots[globalZ]->unreachable) { |
| 593 | hasUnreachableSnapshots = true; |
| 594 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 595 | globalZ++; |
| 596 | } |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 597 | return hasUnreachableSnapshots; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot, |
| 601 | const LayerSnapshot& parentSnapshot, |
| 602 | bool parentIsRelative, const Args& args) { |
| 603 | if (parentIsRelative) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 604 | snapshot.isHiddenByPolicyFromRelativeParent = |
| 605 | parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 606 | if (args.includeMetadata) { |
| 607 | snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata; |
| 608 | } |
| 609 | } else { |
| 610 | snapshot.isHiddenByPolicyFromRelativeParent = |
| 611 | parentSnapshot.isHiddenByPolicyFromRelativeParent; |
| 612 | if (args.includeMetadata) { |
| 613 | snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata; |
| 614 | } |
| 615 | } |
| 616 | snapshot.isVisible = snapshot.getIsVisible(); |
| 617 | } |
| 618 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 619 | void LayerSnapshotBuilder::updateChildState(LayerSnapshot& snapshot, |
| 620 | const LayerSnapshot& childSnapshot, const Args& args) { |
| 621 | if (snapshot.childState.hasValidFrameRate) { |
| 622 | return; |
| 623 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 624 | if (args.forceUpdate == ForceUpdateFlags::ALL || |
| 625 | childSnapshot.changes.test(RequestedLayerState::Changes::FrameRate)) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 626 | // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes |
| 627 | // for the same reason we are allowing touch boost for those layers. See |
| 628 | // RefreshRateSelector::rankFrameRates for details. |
| 629 | using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility; |
| 630 | const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.rate.isValid() && |
| 631 | childSnapshot.frameRate.type == FrameRateCompatibility::Default; |
| 632 | const auto layerVotedWithNoVote = |
| 633 | childSnapshot.frameRate.type == FrameRateCompatibility::NoVote; |
| 634 | const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.rate.isValid() && |
| 635 | childSnapshot.frameRate.type == FrameRateCompatibility::Exact; |
| 636 | |
| 637 | snapshot.childState.hasValidFrameRate |= layerVotedWithDefaultCompatibility || |
| 638 | layerVotedWithNoVote || layerVotedWithExactCompatibility; |
| 639 | |
| 640 | // If we don't have a valid frame rate, but the children do, we set this |
| 641 | // layer as NoVote to allow the children to control the refresh rate |
| 642 | if (!snapshot.frameRate.rate.isValid() && |
| 643 | snapshot.frameRate.type != FrameRateCompatibility::NoVote && |
| 644 | snapshot.childState.hasValidFrameRate) { |
| 645 | snapshot.frameRate = |
| 646 | scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote); |
| 647 | snapshot.changes |= childSnapshot.changes & RequestedLayerState::Changes::FrameRate; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 652 | void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) { |
| 653 | snapshot.isHiddenByPolicyFromRelativeParent = false; |
| 654 | snapshot.relativeLayerMetadata.mMap.clear(); |
| 655 | } |
| 656 | |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 657 | uint32_t getPrimaryDisplayRotationFlags( |
| 658 | const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) { |
| 659 | for (auto& [_, display] : displays) { |
| 660 | if (display.isPrimary) { |
| 661 | return display.rotationFlags; |
| 662 | } |
| 663 | } |
| 664 | return 0; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args, |
| 668 | const RequestedLayerState& requested, |
| 669 | const LayerSnapshot& parentSnapshot, |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 670 | const LayerHierarchy::TraversalPath& path) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 671 | // Always update flags and visibility |
| 672 | ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes & |
| 673 | (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry | |
| 674 | RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 675 | RequestedLayerState::Changes::AffectsChildren | |
| 676 | RequestedLayerState::Changes::FrameRate); |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 677 | snapshot.changes |= parentChanges | requested.changes; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 678 | snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent || |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 679 | parentSnapshot.invalidTransform || requested.isHiddenByPolicy() || |
| 680 | (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 681 | snapshot.contentDirty = requested.what & layer_state_t::CONTENT_DIRTY; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 682 | // TODO(b/238781169) scope down the changes to only buffer updates. |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 683 | snapshot.hasReadyFrame = requested.hasReadyFrame(); |
| 684 | snapshot.sidebandStreamHasFrame = requested.hasSidebandStreamFrame(); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 685 | updateSurfaceDamage(requested, snapshot.hasReadyFrame, args.forceFullDamage, |
| 686 | snapshot.surfaceDamage); |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 687 | snapshot.outputFilter.layerStack = parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT |
| 688 | ? requested.layerStack |
| 689 | : parentSnapshot.outputFilter.layerStack; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 690 | |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 691 | uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays); |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 692 | const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL || |
| 693 | snapshot.changes.any(RequestedLayerState::Changes::Visibility | |
| 694 | RequestedLayerState::Changes::Created); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 695 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 696 | // always update the buffer regardless of visibility |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 697 | if (forceUpdate || requested.what & layer_state_t::BUFFER_CHANGES || args.displayChanges) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 698 | snapshot.acquireFence = |
| 699 | (requested.externalTexture && |
| 700 | requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged)) |
| 701 | ? requested.bufferData->acquireFence |
| 702 | : Fence::NO_FENCE; |
| 703 | snapshot.buffer = |
| 704 | requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr; |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 705 | snapshot.bufferSize = requested.getBufferSize(primaryDisplayRotationFlags); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 706 | snapshot.geomBufferSize = snapshot.bufferSize; |
| 707 | snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize); |
| 708 | snapshot.dataspace = requested.dataspace; |
| 709 | snapshot.externalTexture = requested.externalTexture; |
| 710 | snapshot.frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0; |
| 711 | snapshot.geomBufferTransform = requested.bufferTransform; |
| 712 | snapshot.geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse; |
| 713 | snapshot.geomContentCrop = requested.getBufferCrop(); |
| 714 | snapshot.geomUsesSourceCrop = snapshot.hasBufferOrSidebandStream(); |
| 715 | snapshot.hasProtectedContent = requested.externalTexture && |
| 716 | requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED; |
| 717 | snapshot.isHdrY410 = requested.dataspace == ui::Dataspace::BT2020_ITU_PQ && |
| 718 | requested.api == NATIVE_WINDOW_API_MEDIA && |
| 719 | requested.bufferData->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102; |
| 720 | snapshot.sidebandStream = requested.sidebandStream; |
| 721 | snapshot.transparentRegionHint = requested.transparentRegion; |
| 722 | snapshot.color.rgb = requested.getColor().rgb; |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 723 | snapshot.currentSdrHdrRatio = requested.currentSdrHdrRatio; |
| 724 | snapshot.desiredSdrHdrRatio = requested.desiredSdrHdrRatio; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 727 | if (snapshot.isHiddenByPolicyFromParent && |
| 728 | !snapshot.changes.test(RequestedLayerState::Changes::Created)) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 729 | if (forceUpdate || |
| 730 | snapshot.changes.any(RequestedLayerState::Changes::Hierarchy | |
| 731 | RequestedLayerState::Changes::Geometry | |
| 732 | RequestedLayerState::Changes::Input)) { |
| 733 | updateInput(snapshot, requested, parentSnapshot, path, args); |
| 734 | } |
| 735 | return; |
| 736 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 737 | |
| 738 | if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren)) { |
| 739 | // If root layer, use the layer stack otherwise get the parent's layer stack. |
| 740 | snapshot.color.a = parentSnapshot.color.a * requested.color.a; |
| 741 | snapshot.alpha = snapshot.color.a; |
| 742 | snapshot.isSecure = |
| 743 | parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure); |
| 744 | snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 745 | snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay || |
| 746 | (requested.flags & layer_state_t::eLayerSkipScreenshot); |
| 747 | snapshot.stretchEffect = (requested.stretchEffect.hasEffect()) |
| 748 | ? requested.stretchEffect |
| 749 | : parentSnapshot.stretchEffect; |
| 750 | if (!parentSnapshot.colorTransformIsIdentity) { |
| 751 | snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform; |
| 752 | snapshot.colorTransformIsIdentity = false; |
| 753 | } else { |
| 754 | snapshot.colorTransform = requested.colorTransform; |
| 755 | snapshot.colorTransformIsIdentity = !requested.hasColorTransform; |
| 756 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 757 | snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE) |
| 758 | ? requested.gameMode |
| 759 | : parentSnapshot.gameMode; |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 760 | // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers |
| 761 | // marked as skip capture |
| 762 | snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag || |
| 763 | (requested.layerStackToMirror != ui::INVALID_LAYER_STACK); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 766 | if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren) || |
| 767 | args.displayChanges) { |
| 768 | snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID |
| 769 | ? requested.fixedTransformHint |
| 770 | : parentSnapshot.fixedTransformHint; |
| 771 | |
| 772 | if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) { |
| 773 | snapshot.transformHint = snapshot.fixedTransformHint; |
| 774 | } else { |
| 775 | const auto display = args.displays.get(snapshot.outputFilter.layerStack); |
| 776 | snapshot.transformHint = display.has_value() |
| 777 | ? std::make_optional<>(display->get().transformHint) |
| 778 | : std::nullopt; |
| 779 | } |
| 780 | } |
| 781 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 782 | if (forceUpdate || |
| 783 | snapshot.changes.any(RequestedLayerState::Changes::FrameRate | |
| 784 | RequestedLayerState::Changes::Hierarchy)) { |
| 785 | snapshot.frameRate = (requested.requestedFrameRate.rate.isValid() || |
| 786 | (requested.requestedFrameRate.type == |
| 787 | scheduler::LayerInfo::FrameRateCompatibility::NoVote)) |
| 788 | ? requested.requestedFrameRate |
| 789 | : parentSnapshot.frameRate; |
| 790 | } |
| 791 | |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 792 | if (forceUpdate || requested.what & layer_state_t::eMetadataChanged) { |
| 793 | updateMetadata(snapshot, requested, args); |
| 794 | } |
| 795 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 796 | if (forceUpdate || requested.changes.get() != 0) { |
| 797 | snapshot.compositionType = requested.getCompositionType(); |
| 798 | snapshot.dimmingEnabled = requested.dimmingEnabled; |
| 799 | snapshot.layerOpaqueFlagSet = |
| 800 | (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque; |
Alec Mouri | f4af03e | 2023-02-11 00:25:24 +0000 | [diff] [blame] | 801 | snapshot.cachingHint = requested.cachingHint; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 802 | snapshot.frameRateSelectionPriority = requested.frameRateSelectionPriority; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 805 | if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Content)) { |
| 806 | snapshot.color.rgb = requested.getColor().rgb; |
| 807 | snapshot.isColorspaceAgnostic = requested.colorSpaceAgnostic; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 808 | snapshot.backgroundBlurRadius = args.supportsBlur |
| 809 | ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius) |
| 810 | : 0; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 811 | snapshot.blurRegions = requested.blurRegions; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 812 | for (auto& region : snapshot.blurRegions) { |
| 813 | region.alpha = region.alpha * snapshot.color.a; |
| 814 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 815 | snapshot.hdrMetadata = requested.hdrMetadata; |
| 816 | } |
| 817 | |
| 818 | if (forceUpdate || |
| 819 | snapshot.changes.any(RequestedLayerState::Changes::Hierarchy | |
| 820 | RequestedLayerState::Changes::Geometry)) { |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 821 | updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 822 | updateRoundedCorner(snapshot, requested, parentSnapshot); |
| 823 | } |
| 824 | |
| 825 | if (forceUpdate || |
| 826 | snapshot.changes.any(RequestedLayerState::Changes::Hierarchy | |
| 827 | RequestedLayerState::Changes::Geometry | |
| 828 | RequestedLayerState::Changes::Input)) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 829 | updateInput(snapshot, requested, parentSnapshot, path, args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | // computed snapshot properties |
| 833 | updateShadows(snapshot, requested, args.globalShadowSettings); |
| 834 | if (args.includeMetadata) { |
| 835 | snapshot.layerMetadata = parentSnapshot.layerMetadata; |
| 836 | snapshot.layerMetadata.merge(requested.metadata); |
| 837 | } |
| 838 | snapshot.forceClientComposition = snapshot.isHdrY410 || snapshot.shadowSettings.length > 0 || |
| 839 | requested.blurRegions.size() > 0 || snapshot.stretchEffect.hasEffect(); |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 840 | snapshot.contentOpaque = snapshot.isContentOpaque(); |
| 841 | snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() && |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 842 | snapshot.color.a == 1.f; |
| 843 | snapshot.blendMode = getBlendMode(snapshot, requested); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 844 | LLOGV(snapshot.sequence, |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 845 | "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s", |
| 846 | args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "", |
| 847 | snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(), |
| 848 | parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(), |
| 849 | std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot, |
| 853 | const RequestedLayerState& requested, |
| 854 | const LayerSnapshot& parentSnapshot) { |
| 855 | snapshot.roundedCorner = RoundedCornerState(); |
| 856 | RoundedCornerState parentRoundedCorner; |
| 857 | if (parentSnapshot.roundedCorner.hasRoundedCorners()) { |
| 858 | parentRoundedCorner = parentSnapshot.roundedCorner; |
| 859 | ui::Transform t = snapshot.localTransform.inverse(); |
| 860 | parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect); |
| 861 | parentRoundedCorner.radius.x *= t.getScaleX(); |
| 862 | parentRoundedCorner.radius.y *= t.getScaleY(); |
| 863 | } |
| 864 | |
| 865 | FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect(); |
| 866 | const vec2 radius(requested.cornerRadius, requested.cornerRadius); |
| 867 | RoundedCornerState layerSettings(layerCropRect, radius); |
| 868 | const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty(); |
| 869 | const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners(); |
| 870 | if (layerSettingsValid && parentRoundedCornerValid) { |
| 871 | // If the parent and the layer have rounded corner settings, use the parent settings if |
| 872 | // the parent crop is entirely inside the layer crop. This has limitations and cause |
| 873 | // rendering artifacts. See b/200300845 for correct fix. |
| 874 | if (parentRoundedCorner.cropRect.left > layerCropRect.left && |
| 875 | parentRoundedCorner.cropRect.top > layerCropRect.top && |
| 876 | parentRoundedCorner.cropRect.right < layerCropRect.right && |
| 877 | parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) { |
| 878 | snapshot.roundedCorner = parentRoundedCorner; |
| 879 | } else { |
| 880 | snapshot.roundedCorner = layerSettings; |
| 881 | } |
| 882 | } else if (layerSettingsValid) { |
| 883 | snapshot.roundedCorner = layerSettings; |
| 884 | } else if (parentRoundedCornerValid) { |
| 885 | snapshot.roundedCorner = parentRoundedCorner; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot, |
| 890 | const RequestedLayerState& requested, |
| 891 | const LayerSnapshot& parentSnapshot, |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 892 | uint32_t primaryDisplayRotationFlags) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 893 | snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize); |
| 894 | snapshot.geomCrop = requested.crop; |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 895 | snapshot.localTransform = requested.getTransform(primaryDisplayRotationFlags); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 896 | snapshot.localTransformInverse = snapshot.localTransform.inverse(); |
| 897 | snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 898 | const bool transformWasInvalid = snapshot.invalidTransform; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 899 | snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform); |
| 900 | if (snapshot.invalidTransform) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 901 | auto& t = snapshot.geomLayerTransform; |
| 902 | auto& requestedT = requested.requestedTransform; |
| 903 | std::string transformDebug = |
| 904 | base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}", |
| 905 | t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(), |
| 906 | requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy()); |
| 907 | std::string bufferDebug; |
| 908 | if (requested.externalTexture) { |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 909 | auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 910 | auto& destFrame = requested.destinationFrame; |
| 911 | bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d" |
| 912 | " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}", |
| 913 | requested.externalTexture->getWidth(), |
| 914 | requested.externalTexture->getHeight(), |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame^] | 915 | primaryDisplayRotationFlags, destFrame.left, |
| 916 | destFrame.top, destFrame.right, destFrame.bottom, |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 917 | unRotBuffer.getHeight(), unRotBuffer.getWidth()); |
| 918 | } |
| 919 | ALOGW("Resetting transform for %s because it is invalid.%s%s", |
| 920 | snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 921 | snapshot.geomLayerTransform.reset(); |
| 922 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 923 | if (transformWasInvalid != snapshot.invalidTransform) { |
| 924 | // If transform is invalid, the layer will be hidden. |
| 925 | mResortSnapshots = true; |
| 926 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 927 | snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse(); |
| 928 | |
| 929 | FloatRect parentBounds = parentSnapshot.geomLayerBounds; |
| 930 | parentBounds = snapshot.localTransform.inverse().transform(parentBounds); |
| 931 | snapshot.geomLayerBounds = |
| 932 | (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds; |
| 933 | if (!requested.crop.isEmpty()) { |
| 934 | snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect()); |
| 935 | } |
| 936 | snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds); |
| 937 | snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 938 | const Rect geomLayerBoundsWithoutTransparentRegion = |
| 939 | RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds), |
| 940 | requested.transparentRegion); |
| 941 | snapshot.transformedBoundsWithoutTransparentRegion = |
| 942 | snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 943 | snapshot.parentTransform = parentSnapshot.geomLayerTransform; |
| 944 | |
| 945 | // Subtract the transparent region and snap to the bounds |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 946 | const Rect bounds = |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 947 | RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 948 | if (requested.potentialCursor) { |
| 949 | snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds); |
| 950 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, |
| 954 | const RequestedLayerState& requested, |
| 955 | const renderengine::ShadowSettings& globalShadowSettings) { |
| 956 | snapshot.shadowRadius = requested.shadowRadius; |
| 957 | snapshot.shadowSettings.length = requested.shadowRadius; |
| 958 | if (snapshot.shadowRadius > 0.f) { |
| 959 | snapshot.shadowSettings = globalShadowSettings; |
| 960 | |
| 961 | // Note: this preserves existing behavior of shadowing the entire layer and not cropping |
| 962 | // it if transparent regions are present. This may not be necessary since shadows are |
| 963 | // typically cast by layers without transparent regions. |
| 964 | snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds; |
| 965 | |
| 966 | // If the casting layer is translucent, we need to fill in the shadow underneath the |
| 967 | // layer. Otherwise the generated shadow will only be shown around the casting layer. |
| 968 | snapshot.shadowSettings.casterIsTranslucent = |
| 969 | !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f); |
| 970 | snapshot.shadowSettings.ambientColor *= snapshot.alpha; |
| 971 | snapshot.shadowSettings.spotColor *= snapshot.alpha; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot, |
| 976 | const RequestedLayerState& requested, |
| 977 | const LayerSnapshot& parentSnapshot, |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 978 | const LayerHierarchy::TraversalPath& path, |
| 979 | const Args& args) { |
| 980 | if (requested.windowInfoHandle) { |
| 981 | snapshot.inputInfo = *requested.windowInfoHandle->getInfo(); |
| 982 | } else { |
| 983 | snapshot.inputInfo = {}; |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 984 | // b/271132344 revisit this and see if we can always use the layers uid/pid |
| 985 | snapshot.inputInfo.name = requested.name; |
| 986 | snapshot.inputInfo.ownerUid = static_cast<int32_t>(requested.ownerUid); |
| 987 | snapshot.inputInfo.ownerPid = requested.ownerPid; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 988 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 989 | |
Vishnu Nair | 93b8b79 | 2023-02-27 19:40:24 +0000 | [diff] [blame] | 990 | snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 991 | snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 992 | if (!needsInputInfo(snapshot, requested)) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 993 | return; |
| 994 | } |
| 995 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 996 | static frontend::DisplayInfo sDefaultInfo = {.isSecure = false}; |
| 997 | const std::optional<frontend::DisplayInfo> displayInfoOpt = |
| 998 | args.displays.get(snapshot.outputFilter.layerStack); |
| 999 | bool noValidDisplay = !displayInfoOpt.has_value(); |
| 1000 | auto displayInfo = displayInfoOpt.value_or(sDefaultInfo); |
| 1001 | |
| 1002 | if (!requested.windowInfoHandle) { |
| 1003 | snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL; |
| 1004 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1005 | fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot); |
| 1006 | |
| 1007 | if (noValidDisplay) { |
| 1008 | // Do not let the window receive touches if it is not associated with a valid display |
| 1009 | // transform. We still allow the window to receive keys and prevent ANRs. |
| 1010 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE; |
| 1011 | } |
| 1012 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1013 | snapshot.inputInfo.alpha = snapshot.color.a; |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 1014 | snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo() |
| 1015 | ? requested.windowInfoHandle->getInfo()->touchOcclusionMode |
| 1016 | : parentSnapshot.inputInfo.touchOcclusionMode; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1017 | if (requested.dropInputMode == gui::DropInputMode::ALL || |
| 1018 | parentSnapshot.dropInputMode == gui::DropInputMode::ALL) { |
| 1019 | snapshot.dropInputMode = gui::DropInputMode::ALL; |
| 1020 | } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED || |
| 1021 | parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) { |
| 1022 | snapshot.dropInputMode = gui::DropInputMode::OBSCURED; |
| 1023 | } else { |
| 1024 | snapshot.dropInputMode = gui::DropInputMode::NONE; |
| 1025 | } |
| 1026 | |
| 1027 | handleDropInputMode(snapshot, parentSnapshot); |
| 1028 | |
| 1029 | // If the window will be blacked out on a display because the display does not have the secure |
| 1030 | // flag and the layer has the secure flag set, then drop input. |
| 1031 | if (!displayInfo.isSecure && snapshot.isSecure) { |
| 1032 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 1033 | } |
| 1034 | |
| 1035 | auto cropLayerSnapshot = getSnapshot(requested.touchCropId); |
| 1036 | if (snapshot.inputInfo.replaceTouchableRegionWithCrop) { |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 1037 | Rect inputBoundsInDisplaySpace; |
| 1038 | if (!cropLayerSnapshot) { |
| 1039 | FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first; |
| 1040 | inputBoundsInDisplaySpace = |
| 1041 | getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform); |
| 1042 | } else { |
| 1043 | FloatRect inputBounds = |
| 1044 | getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first; |
| 1045 | inputBoundsInDisplaySpace = |
| 1046 | getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds, |
| 1047 | displayInfo.transform); |
| 1048 | } |
| 1049 | snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1050 | } else if (cropLayerSnapshot) { |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 1051 | FloatRect inputBounds = getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first; |
| 1052 | Rect inputBoundsInDisplaySpace = |
| 1053 | getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds, |
| 1054 | displayInfo.transform); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1055 | snapshot.inputInfo.touchableRegion = snapshot.inputInfo.touchableRegion.intersect( |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 1056 | displayInfo.transform.transform(inputBoundsInDisplaySpace)); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state |
| 1060 | // if it was set by WM for a known system overlay |
| 1061 | if (snapshot.isTrustedOverlay) { |
| 1062 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY; |
| 1063 | } |
| 1064 | |
| 1065 | // If the layer is a clone, we need to crop the input region to cloned root to prevent |
| 1066 | // touches from going outside the cloned area. |
| 1067 | if (path.isClone()) { |
| 1068 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE; |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 1069 | auto clonedRootSnapshot = getSnapshot(snapshot.mirrorRootPath); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1070 | if (clonedRootSnapshot) { |
| 1071 | const Rect rect = |
| 1072 | displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds}); |
| 1073 | snapshot.inputInfo.touchableRegion = snapshot.inputInfo.touchableRegion.intersect(rect); |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() { |
| 1079 | return mSnapshots; |
| 1080 | } |
| 1081 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1082 | void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const { |
| 1083 | for (int i = 0; i < mNumInterestingSnapshots; i++) { |
| 1084 | LayerSnapshot& snapshot = *mSnapshots[(size_t)i]; |
| 1085 | if (!snapshot.isVisible) continue; |
| 1086 | visitor(snapshot); |
| 1087 | } |
| 1088 | } |
| 1089 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 1090 | // Visit each visible snapshot in z-order |
| 1091 | void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor, |
| 1092 | const LayerHierarchy& root) const { |
| 1093 | root.traverseInZOrder( |
| 1094 | [this, visitor](const LayerHierarchy&, |
| 1095 | const LayerHierarchy::TraversalPath& traversalPath) -> bool { |
| 1096 | LayerSnapshot* snapshot = getSnapshot(traversalPath); |
| 1097 | if (snapshot && snapshot->isVisible) { |
| 1098 | visitor(*snapshot); |
| 1099 | } |
| 1100 | return true; |
| 1101 | }); |
| 1102 | } |
| 1103 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1104 | void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) { |
| 1105 | for (int i = 0; i < mNumInterestingSnapshots; i++) { |
| 1106 | std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i); |
| 1107 | if (!snapshot->isVisible) continue; |
| 1108 | visitor(snapshot); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const { |
| 1113 | for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) { |
| 1114 | LayerSnapshot& snapshot = *mSnapshots[(size_t)i]; |
| 1115 | if (!snapshot.hasInputInfo()) continue; |
| 1116 | visitor(snapshot); |
| 1117 | } |
| 1118 | } |
| 1119 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1120 | } // namespace android::surfaceflinger::frontend |