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 |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 20 | #define LOG_TAG "SurfaceFlinger" |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 21 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 22 | #include <numeric> |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 23 | #include <optional> |
| 24 | |
Vishnu Nair | 9e0017e | 2024-05-22 19:02:44 +0000 | [diff] [blame] | 25 | #include <common/FlagManager.h> |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 26 | #include <ftl/small_map.h> |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 27 | #include <gui/TraceUtils.h> |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 28 | #include <ui/DisplayMap.h> |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 29 | #include <ui/FloatRect.h> |
| 30 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 31 | #include "DisplayHardware/HWC2.h" |
| 32 | #include "DisplayHardware/Hal.h" |
Vishnu Nair | 3d8565a | 2023-06-30 07:23:24 +0000 | [diff] [blame] | 33 | #include "Layer.h" // eFrameRateSelectionPriority constants |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 34 | #include "LayerLog.h" |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 35 | #include "LayerSnapshotBuilder.h" |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 36 | #include "TimeStats/TimeStats.h" |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 37 | #include "Tracing/TransactionTracing.h" |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 38 | |
| 39 | namespace android::surfaceflinger::frontend { |
| 40 | |
| 41 | using namespace ftl::flag_operators; |
| 42 | |
| 43 | namespace { |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 44 | |
| 45 | FloatRect getMaxDisplayBounds(const DisplayInfos& displays) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 46 | const ui::Size maxSize = [&displays] { |
| 47 | if (displays.empty()) return ui::Size{5000, 5000}; |
| 48 | |
| 49 | return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize, |
| 50 | [](ui::Size size, const auto& pair) -> ui::Size { |
| 51 | const auto& display = pair.second; |
| 52 | return {std::max(size.getWidth(), display.info.logicalWidth), |
| 53 | std::max(size.getHeight(), display.info.logicalHeight)}; |
| 54 | }); |
| 55 | }(); |
| 56 | |
| 57 | // Ignore display bounds for now since they will be computed later. Use a large Rect bound |
| 58 | // to ensure it's bigger than an actual display will be. |
| 59 | const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f; |
| 60 | const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f; |
| 61 | |
| 62 | return {-xMax, -yMax, xMax, yMax}; |
| 63 | } |
| 64 | |
| 65 | // Applies the given transform to the region, while protecting against overflows caused by any |
| 66 | // offsets. If applying the offset in the transform to any of the Rects in the region would result |
| 67 | // in an overflow, they are not added to the output Region. |
| 68 | Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r, |
| 69 | const std::string& debugWindowName) { |
| 70 | // Round the translation using the same rounding strategy used by ui::Transform. |
| 71 | const auto tx = static_cast<int32_t>(t.tx() + 0.5); |
| 72 | const auto ty = static_cast<int32_t>(t.ty() + 0.5); |
| 73 | |
| 74 | ui::Transform transformWithoutOffset = t; |
| 75 | transformWithoutOffset.set(0.f, 0.f); |
| 76 | |
| 77 | const Region transformed = transformWithoutOffset.transform(r); |
| 78 | |
| 79 | // Apply the translation to each of the Rects in the region while discarding any that overflow. |
| 80 | Region ret; |
| 81 | for (const auto& rect : transformed) { |
| 82 | Rect newRect; |
| 83 | if (__builtin_add_overflow(rect.left, tx, &newRect.left) || |
| 84 | __builtin_add_overflow(rect.top, ty, &newRect.top) || |
| 85 | __builtin_add_overflow(rect.right, tx, &newRect.right) || |
| 86 | __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) { |
| 87 | ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.", |
| 88 | debugWindowName.c_str()); |
| 89 | continue; |
| 90 | } |
| 91 | ret.orSelf(newRect); |
| 92 | } |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * We don't want to send the layer's transform to input, but rather the |
| 98 | * parent's transform. This is because Layer's transform is |
| 99 | * information about how the buffer is placed on screen. The parent's |
| 100 | * transform makes more sense to send since it's information about how the |
| 101 | * layer is placed on screen. This transform is used by input to determine |
| 102 | * how to go from screen space back to window space. |
| 103 | */ |
| 104 | ui::Transform getInputTransform(const LayerSnapshot& snapshot) { |
| 105 | if (!snapshot.hasBufferOrSidebandStream()) { |
| 106 | return snapshot.geomLayerTransform; |
| 107 | } |
| 108 | return snapshot.parentTransform; |
| 109 | } |
| 110 | |
| 111 | /** |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 112 | * Returns the bounds used to fill the input frame and the touchable region. |
| 113 | * |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 114 | * Similar to getInputTransform, we need to update the bounds to include the transform. |
| 115 | * This is because bounds don't include the buffer transform, where the input assumes |
| 116 | * that's already included. |
| 117 | */ |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 118 | std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) { |
| 119 | FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect(); |
| 120 | if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() && |
| 121 | snapshot.localTransform.getType() != ui::Transform::IDENTITY) { |
| 122 | inputBounds = snapshot.localTransform.transform(inputBounds); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 125 | bool inputBoundsValid = snapshot.croppedBufferSize.isValid(); |
| 126 | if (!inputBoundsValid) { |
| 127 | /** |
| 128 | * Input bounds are based on the layer crop or buffer size. But if we are using |
| 129 | * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then |
| 130 | * we can use the parent bounds as the input bounds if the layer does not have buffer |
| 131 | * or a crop. We want to unify this logic but because of compat reasons we cannot always |
| 132 | * use the parent bounds. A layer without a buffer can get input. So when a window is |
| 133 | * initially added, its touchable region can fill its parent layer bounds and that can |
| 134 | * have negative consequences. |
| 135 | */ |
| 136 | inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{}; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 137 | } |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 138 | |
| 139 | // Clamp surface inset to the input bounds. |
| 140 | const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset); |
| 141 | const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f); |
| 142 | const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f); |
| 143 | |
| 144 | // Apply the insets to the input bounds. |
| 145 | inputBounds.left += xSurfaceInset; |
| 146 | inputBounds.top += ySurfaceInset; |
| 147 | inputBounds.right -= xSurfaceInset; |
| 148 | inputBounds.bottom -= ySurfaceInset; |
| 149 | return {inputBounds, inputBoundsValid}; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 152 | Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds, |
| 153 | const ui::Transform& screenToDisplay) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 154 | // InputDispatcher works in the display device's coordinate space. Here, we calculate the |
| 155 | // frame and transform used for the layer, which determines the bounds and the coordinate space |
| 156 | // within which the layer will receive input. |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 157 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 158 | // Coordinate space definitions: |
| 159 | // - display: The display device's coordinate space. Correlates to pixels on the display. |
| 160 | // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space. |
| 161 | // - layer: The coordinate space of this layer. |
| 162 | // - input: The coordinate space in which this layer will receive input events. This could be |
| 163 | // different than layer space if a surfaceInset is used, which changes the origin |
| 164 | // of the input space. |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 165 | |
| 166 | // 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] | 167 | const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 168 | |
| 169 | const ui::Transform layerToScreen = getInputTransform(snapshot); |
| 170 | const ui::Transform layerToDisplay = screenToDisplay * layerToScreen; |
| 171 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 172 | return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)}; |
| 173 | } |
| 174 | |
| 175 | void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay, |
| 176 | const LayerSnapshot& snapshot) { |
| 177 | auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false); |
| 178 | if (!inputBoundsValid) { |
| 179 | info.touchableRegion.clear(); |
| 180 | } |
| 181 | |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 182 | info.frame = getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 183 | |
| 184 | ui::Transform inputToLayer; |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 185 | inputToLayer.set(inputBounds.left, inputBounds.top); |
| 186 | const ui::Transform layerToScreen = getInputTransform(snapshot); |
| 187 | const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 188 | |
| 189 | // InputDispatcher expects a display-to-input transform. |
| 190 | info.transform = inputToDisplay.inverse(); |
| 191 | |
| 192 | // The touchable region is specified in the input coordinate space. Change it to display space. |
| 193 | info.touchableRegion = |
| 194 | transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name); |
| 195 | } |
| 196 | |
| 197 | void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) { |
| 198 | if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Check if we need to drop input unconditionally |
| 203 | const gui::DropInputMode dropInputMode = snapshot.dropInputMode; |
| 204 | if (dropInputMode == gui::DropInputMode::ALL) { |
| 205 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 206 | ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str()); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | // Check if we need to check if the window is obscured by parent |
| 211 | if (dropInputMode != gui::DropInputMode::OBSCURED) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // Check if the parent has set an alpha on the layer |
| 216 | if (parentSnapshot.color.a != 1.0_hf) { |
| 217 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 218 | ALOGV("Dropping input for %s as requested by policy because alpha=%f", |
| 219 | snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a)); |
| 220 | } |
| 221 | |
| 222 | // Check if the parent has cropped the buffer |
| 223 | Rect bufferSize = snapshot.croppedBufferSize; |
| 224 | if (!bufferSize.isValid()) { |
| 225 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED; |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | // Screenbounds are the layer bounds cropped by parents, transformed to screenspace. |
| 230 | // To check if the layer has been cropped, we take the buffer bounds, apply the local |
| 231 | // layer crop and apply the same set of transforms to move to screenspace. If the bounds |
| 232 | // match then the layer has not been cropped by its parents. |
| 233 | Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize)); |
| 234 | bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds}; |
| 235 | |
| 236 | if (croppedByParent) { |
| 237 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT; |
| 238 | ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent", |
| 239 | snapshot.name.c_str()); |
| 240 | } else { |
| 241 | // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop |
| 242 | // input if the window is obscured. This check should be done in surfaceflinger but the |
| 243 | // logic currently resides in inputflinger. So pass the if_obscured check to input to only |
| 244 | // drop input events if the window is obscured. |
| 245 | snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED; |
| 246 | } |
| 247 | } |
| 248 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 249 | auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) { |
| 250 | auto blendMode = Hwc2::IComposerClient::BlendMode::NONE; |
| 251 | if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) { |
| 252 | blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED |
| 253 | : Hwc2::IComposerClient::BlendMode::COVERAGE; |
| 254 | } |
| 255 | return blendMode; |
| 256 | } |
| 257 | |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 258 | void updateVisibility(LayerSnapshot& snapshot, bool visible) { |
Vishnu Nair | b4a6a77 | 2024-06-12 14:41:08 -0700 | [diff] [blame] | 259 | if (snapshot.isVisible != visible) { |
| 260 | snapshot.changes |= RequestedLayerState::Changes::Visibility; |
| 261 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 262 | snapshot.isVisible = visible; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 263 | |
| 264 | // TODO(b/238781169) we are ignoring this compat for now, since we will have |
| 265 | // to remove any optimization based on visibility. |
| 266 | |
| 267 | // For compatibility reasons we let layers which can receive input |
| 268 | // receive input before they have actually submitted a buffer. Because |
| 269 | // of this we use canReceiveInput instead of isVisible to check the |
| 270 | // policy-visibility, ignoring the buffer state. However for layers with |
| 271 | // hasInputInfo()==false we can use the real visibility state. |
| 272 | // We are just using these layers for occlusion detection in |
| 273 | // InputDispatcher, and obviously if they aren't visible they can't occlude |
| 274 | // anything. |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 275 | const bool visibleForInput = |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 276 | snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 277 | snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 278 | LLOGV(snapshot.sequence, "updating visibility %s %s", visible ? "true" : "false", |
| 279 | snapshot.getDebugString().c_str()); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) { |
| 283 | if (requested.potentialCursor) { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | if (snapshot.inputInfo.token != nullptr) { |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | if (snapshot.hasBufferOrSidebandStream()) { |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | return requested.windowInfoHandle && |
| 296 | requested.windowInfoHandle->getInfo()->inputConfig.test( |
| 297 | gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
| 298 | } |
| 299 | |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 300 | void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested, |
| 301 | const LayerSnapshotBuilder::Args& args) { |
| 302 | snapshot.metadata.clear(); |
| 303 | for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) { |
| 304 | auto compatIter = args.genericLayerMetadataKeyMap.find(key); |
| 305 | if (compatIter == std::end(args.genericLayerMetadataKeyMap)) { |
| 306 | continue; |
| 307 | } |
| 308 | const uint32_t id = compatIter->second; |
| 309 | auto it = requested.metadata.mMap.find(id); |
| 310 | if (it == std::end(requested.metadata.mMap)) { |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | snapshot.metadata.emplace(key, |
| 315 | compositionengine::GenericLayerMetadataEntry{mandatory, |
| 316 | it->second}); |
| 317 | } |
| 318 | } |
| 319 | |
Nergi Rahardi | 0dfc096 | 2024-05-23 06:57:36 +0000 | [diff] [blame^] | 320 | void updateMetadataAndGameMode(LayerSnapshot& snapshot, const RequestedLayerState& requested, |
| 321 | const LayerSnapshotBuilder::Args& args, |
| 322 | const LayerSnapshot& parentSnapshot) { |
| 323 | if (snapshot.changes.test(RequestedLayerState::Changes::GameMode)) { |
| 324 | snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE) |
| 325 | ? requested.gameMode |
| 326 | : parentSnapshot.gameMode; |
| 327 | } |
| 328 | updateMetadata(snapshot, requested, args); |
| 329 | if (args.includeMetadata) { |
| 330 | snapshot.layerMetadata = parentSnapshot.layerMetadata; |
| 331 | snapshot.layerMetadata.merge(requested.metadata); |
| 332 | } |
| 333 | } |
| 334 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 335 | void clearChanges(LayerSnapshot& snapshot) { |
| 336 | snapshot.changes.clear(); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 337 | snapshot.clientChanges = 0; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 338 | snapshot.contentDirty = false; |
| 339 | snapshot.hasReadyFrame = false; |
| 340 | snapshot.sidebandStreamHasFrame = false; |
| 341 | snapshot.surfaceDamage.clear(); |
| 342 | } |
| 343 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 344 | // TODO (b/259407931): Remove. |
| 345 | uint32_t getPrimaryDisplayRotationFlags( |
| 346 | const ui::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) { |
| 347 | for (auto& [_, display] : displays) { |
| 348 | if (display.isPrimary) { |
| 349 | return display.rotationFlags; |
| 350 | } |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 355 | } // namespace |
| 356 | |
| 357 | LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() { |
| 358 | LayerSnapshot snapshot; |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 359 | snapshot.path = LayerHierarchy::TraversalPath::ROOT; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 360 | snapshot.changes = ftl::Flags<RequestedLayerState::Changes>(); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 361 | snapshot.clientChanges = 0; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 362 | snapshot.isHiddenByPolicyFromParent = false; |
| 363 | snapshot.isHiddenByPolicyFromRelativeParent = false; |
| 364 | snapshot.parentTransform.reset(); |
| 365 | snapshot.geomLayerTransform.reset(); |
| 366 | snapshot.geomInverseLayerTransform.reset(); |
| 367 | snapshot.geomLayerBounds = getMaxDisplayBounds({}); |
| 368 | snapshot.roundedCorner = RoundedCornerState(); |
| 369 | snapshot.stretchEffect = {}; |
| 370 | snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK; |
| 371 | snapshot.outputFilter.toInternalDisplay = false; |
| 372 | snapshot.isSecure = false; |
| 373 | snapshot.color.a = 1.0_hf; |
| 374 | snapshot.colorTransformIsIdentity = true; |
Vishnu Nair | d9e4f46 | 2023-10-06 04:05:45 +0000 | [diff] [blame] | 375 | snapshot.shadowSettings.length = 0.f; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 376 | snapshot.layerMetadata.mMap.clear(); |
| 377 | snapshot.relativeLayerMetadata.mMap.clear(); |
| 378 | snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED; |
| 379 | snapshot.dropInputMode = gui::DropInputMode::NONE; |
Vishnu Nair | 9e0017e | 2024-05-22 19:02:44 +0000 | [diff] [blame] | 380 | snapshot.trustedOverlay = gui::TrustedOverlay::UNSET; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 381 | snapshot.gameMode = gui::GameMode::Unsupported; |
| 382 | snapshot.frameRate = {}; |
| 383 | snapshot.fixedTransformHint = ui::Transform::ROT_INVALID; |
Vishnu Nair | 422b81c | 2024-05-16 05:44:28 +0000 | [diff] [blame] | 384 | snapshot.ignoreLocalTransform = false; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 385 | return snapshot; |
| 386 | } |
| 387 | |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 388 | LayerSnapshotBuilder::LayerSnapshotBuilder() {} |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 389 | |
| 390 | LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 391 | args.forceUpdate = ForceUpdateFlags::ALL; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 392 | updateSnapshots(args); |
| 393 | } |
| 394 | |
| 395 | bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) { |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 396 | const bool forceUpdate = args.forceUpdate != ForceUpdateFlags::NONE; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 397 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 398 | if (args.layerLifecycleManager.getGlobalChanges().get() == 0 && !forceUpdate && |
| 399 | !args.displayChanges) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 403 | // There are only content changes which do not require any child layer snapshots to be updated. |
| 404 | ALOGV("%s", __func__); |
| 405 | ATRACE_NAME("FastPath"); |
| 406 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 407 | uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays); |
| 408 | if (forceUpdate || args.displayChanges) { |
| 409 | for (auto& snapshot : mSnapshots) { |
| 410 | const RequestedLayerState* requested = |
| 411 | args.layerLifecycleManager.getLayerFromId(snapshot->path.id); |
| 412 | if (!requested) continue; |
| 413 | snapshot->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage, |
| 414 | primaryDisplayRotationFlags); |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | // Walk through all the updated requested layer states and update the corresponding snapshots. |
| 420 | for (const RequestedLayerState* requested : args.layerLifecycleManager.getChangedLayers()) { |
| 421 | auto range = mIdToSnapshots.equal_range(requested->id); |
| 422 | for (auto it = range.first; it != range.second; it++) { |
| 423 | it->second->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage, |
| 424 | primaryDisplayRotationFlags); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 428 | if ((args.layerLifecycleManager.getGlobalChanges().get() & |
| 429 | ~(RequestedLayerState::Changes::Content | RequestedLayerState::Changes::Buffer).get()) != |
| 430 | 0) { |
| 431 | // We have changes that require us to walk the hierarchy and update child layers. |
| 432 | // No fast path for you. |
| 433 | return false; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 434 | } |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | void LayerSnapshotBuilder::updateSnapshots(const Args& args) { |
| 439 | ATRACE_NAME("UpdateSnapshots"); |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 440 | LayerSnapshot rootSnapshot = args.rootSnapshot; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 441 | if (args.parentCrop) { |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 442 | rootSnapshot.geomLayerBounds = *args.parentCrop; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 443 | } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) { |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 444 | rootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 445 | } |
| 446 | if (args.displayChanges) { |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 447 | rootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 448 | RequestedLayerState::Changes::Geometry; |
| 449 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 450 | if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) { |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 451 | rootSnapshot.changes |= |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 452 | RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility; |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 453 | rootSnapshot.clientChanges |= layer_state_t::eReparent; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 454 | } |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 455 | |
| 456 | for (auto& snapshot : mSnapshots) { |
| 457 | if (snapshot->reachablilty == LayerSnapshot::Reachablilty::Reachable) { |
| 458 | snapshot->reachablilty = LayerSnapshot::Reachablilty::Unreachable; |
| 459 | } |
| 460 | } |
| 461 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 462 | LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 463 | if (args.root.getLayer()) { |
| 464 | // The hierarchy can have a root layer when used for screenshots otherwise, it will have |
| 465 | // multiple children. |
| 466 | LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id, |
| 467 | LayerHierarchy::Variant::Attached); |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 468 | updateSnapshotsInHierarchy(args, args.root, root, rootSnapshot, /*depth=*/0); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 469 | } else { |
| 470 | for (auto& [childHierarchy, variant] : args.root.mChildren) { |
| 471 | LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, |
| 472 | childHierarchy->getLayer()->id, |
| 473 | variant); |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 474 | updateSnapshotsInHierarchy(args, *childHierarchy, root, rootSnapshot, /*depth=*/0); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 475 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 478 | // Update touchable region crops outside the main update pass. This is because a layer could be |
| 479 | // cropped by any other layer and it requires both snapshots to be updated. |
| 480 | updateTouchableRegionCrop(args); |
| 481 | |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 482 | const bool hasUnreachableSnapshots = sortSnapshotsByZ(args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 483 | |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 484 | // Destroy unreachable snapshots for clone layers. And destroy snapshots for non-clone |
| 485 | // layers if the layer have been destroyed. |
| 486 | // TODO(b/238781169) consider making clone layer ids stable as well |
| 487 | if (!hasUnreachableSnapshots && args.layerLifecycleManager.getDestroyedLayers().empty()) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 491 | std::unordered_set<uint32_t> destroyedLayerIds; |
| 492 | for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) { |
| 493 | destroyedLayerIds.insert(destroyedLayer->id); |
| 494 | } |
| 495 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 496 | auto it = mSnapshots.begin(); |
| 497 | while (it < mSnapshots.end()) { |
| 498 | auto& traversalPath = it->get()->path; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 499 | const bool unreachable = |
| 500 | it->get()->reachablilty == LayerSnapshot::Reachablilty::Unreachable; |
| 501 | const bool isClone = traversalPath.isClone(); |
| 502 | const bool layerIsDestroyed = |
| 503 | destroyedLayerIds.find(traversalPath.id) != destroyedLayerIds.end(); |
| 504 | const bool destroySnapshot = (unreachable && isClone) || layerIsDestroyed; |
| 505 | |
| 506 | if (!destroySnapshot) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 507 | it++; |
| 508 | continue; |
| 509 | } |
| 510 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 511 | mPathToSnapshot.erase(traversalPath); |
| 512 | |
| 513 | auto range = mIdToSnapshots.equal_range(traversalPath.id); |
| 514 | auto matchingSnapshot = |
| 515 | std::find_if(range.first, range.second, [&traversalPath](auto& snapshotWithId) { |
| 516 | return snapshotWithId.second->path == traversalPath; |
| 517 | }); |
| 518 | mIdToSnapshots.erase(matchingSnapshot); |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 519 | mNeedsTouchableRegionCrop.erase(traversalPath); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 520 | mSnapshots.back()->globalZ = it->get()->globalZ; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 521 | std::iter_swap(it, mSnapshots.end() - 1); |
| 522 | mSnapshots.erase(mSnapshots.end() - 1); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void LayerSnapshotBuilder::update(const Args& args) { |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 527 | for (auto& snapshot : mSnapshots) { |
| 528 | clearChanges(*snapshot); |
| 529 | } |
| 530 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 531 | if (tryFastUpdate(args)) { |
| 532 | return; |
| 533 | } |
| 534 | updateSnapshots(args); |
| 535 | } |
| 536 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 537 | const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy( |
| 538 | const Args& args, const LayerHierarchy& hierarchy, |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 539 | LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot, |
| 540 | int depth) { |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 541 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50, |
| 542 | "Cycle detected in LayerSnapshotBuilder. See " |
| 543 | "builder_stack_overflow_transactions.winscope"); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 544 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 545 | const RequestedLayerState* layer = hierarchy.getLayer(); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 546 | LayerSnapshot* snapshot = getSnapshot(traversalPath); |
| 547 | const bool newSnapshot = snapshot == nullptr; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 548 | uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 549 | if (newSnapshot) { |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 550 | snapshot = createSnapshot(traversalPath, *layer, parentSnapshot); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 551 | snapshot->merge(*layer, /*forceUpdate=*/true, /*displayChanges=*/true, args.forceFullDamage, |
| 552 | primaryDisplayRotationFlags); |
| 553 | snapshot->changes |= RequestedLayerState::Changes::Created; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 554 | } |
Vishnu Nair | 52d56fd | 2023-07-20 17:02:43 +0000 | [diff] [blame] | 555 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 556 | if (traversalPath.isRelative()) { |
| 557 | bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative; |
| 558 | updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args); |
| 559 | } else { |
| 560 | if (traversalPath.isAttached()) { |
| 561 | resetRelativeState(*snapshot); |
| 562 | } |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 563 | updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | for (auto& [childHierarchy, variant] : hierarchy.mChildren) { |
| 567 | LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath, |
| 568 | childHierarchy->getLayer()->id, |
| 569 | variant); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 570 | const LayerSnapshot& childSnapshot = |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 571 | updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot, |
| 572 | depth + 1); |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 573 | updateFrameRateFromChildSnapshot(*snapshot, childSnapshot, args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 574 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 575 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 576 | return *snapshot; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const { |
| 580 | if (layerId == UNASSIGNED_LAYER_ID) { |
| 581 | return nullptr; |
| 582 | } |
| 583 | LayerHierarchy::TraversalPath path{.id = layerId}; |
| 584 | return getSnapshot(path); |
| 585 | } |
| 586 | |
| 587 | LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const { |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 588 | auto it = mPathToSnapshot.find(id); |
| 589 | return it == mPathToSnapshot.end() ? nullptr : it->second; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 592 | LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path, |
| 593 | const RequestedLayerState& layer, |
| 594 | const LayerSnapshot& parentSnapshot) { |
| 595 | mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path)); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 596 | LayerSnapshot* snapshot = mSnapshots.back().get(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 597 | snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1; |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 598 | if (path.isClone() && !LayerHierarchy::isMirror(path.variant)) { |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 599 | snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath; |
| 600 | } |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 601 | snapshot->ignoreLocalTransform = |
| 602 | path.isClone() && path.variant == LayerHierarchy::Variant::Detached_Mirror; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 603 | mPathToSnapshot[path] = snapshot; |
| 604 | |
| 605 | mIdToSnapshots.emplace(path.id, snapshot); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 606 | return snapshot; |
| 607 | } |
| 608 | |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 609 | bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 610 | if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE && |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 611 | !args.layerLifecycleManager.getGlobalChanges().any( |
Chavi Weingarten | 92c7d8c | 2024-01-19 23:25:45 +0000 | [diff] [blame] | 612 | RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility | |
| 613 | RequestedLayerState::Changes::Input)) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 614 | // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting |
| 615 | // the snapshots. |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 616 | return false; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 617 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 618 | mResortSnapshots = false; |
| 619 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 620 | size_t globalZ = 0; |
| 621 | args.root.traverseInZOrder( |
| 622 | [this, &globalZ](const LayerHierarchy&, |
| 623 | const LayerHierarchy::TraversalPath& traversalPath) -> bool { |
| 624 | LayerSnapshot* snapshot = getSnapshot(traversalPath); |
| 625 | if (!snapshot) { |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 626 | return true; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 629 | if (snapshot->getIsVisible() || snapshot->hasInputInfo()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 630 | updateVisibility(*snapshot, snapshot->getIsVisible()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 631 | size_t oldZ = snapshot->globalZ; |
| 632 | size_t newZ = globalZ++; |
| 633 | snapshot->globalZ = newZ; |
| 634 | if (oldZ == newZ) { |
| 635 | return true; |
| 636 | } |
| 637 | mSnapshots[newZ]->globalZ = oldZ; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 638 | LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ, |
| 639 | snapshot->getDebugString().c_str()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 640 | std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ), |
| 641 | mSnapshots.begin() + static_cast<ssize_t>(newZ)); |
| 642 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 643 | return true; |
| 644 | }); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 645 | mNumInterestingSnapshots = (int)globalZ; |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 646 | bool hasUnreachableSnapshots = false; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 647 | while (globalZ < mSnapshots.size()) { |
| 648 | mSnapshots[globalZ]->globalZ = globalZ; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 649 | /* mark unreachable snapshots as explicitly invisible */ |
| 650 | updateVisibility(*mSnapshots[globalZ], false); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 651 | if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) { |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 652 | hasUnreachableSnapshots = true; |
| 653 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 654 | globalZ++; |
| 655 | } |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 656 | return hasUnreachableSnapshots; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot, |
| 660 | const LayerSnapshot& parentSnapshot, |
| 661 | bool parentIsRelative, const Args& args) { |
| 662 | if (parentIsRelative) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 663 | snapshot.isHiddenByPolicyFromRelativeParent = |
| 664 | parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 665 | if (args.includeMetadata) { |
| 666 | snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata; |
| 667 | } |
| 668 | } else { |
| 669 | snapshot.isHiddenByPolicyFromRelativeParent = |
| 670 | parentSnapshot.isHiddenByPolicyFromRelativeParent; |
| 671 | if (args.includeMetadata) { |
| 672 | snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata; |
| 673 | } |
| 674 | } |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 675 | if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) { |
| 676 | snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent; |
| 677 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 680 | void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot, |
| 681 | const LayerSnapshot& childSnapshot, |
| 682 | const Args& args) { |
| 683 | if (args.forceUpdate == ForceUpdateFlags::NONE && |
Vishnu Nair | 52d56fd | 2023-07-20 17:02:43 +0000 | [diff] [blame] | 684 | !args.layerLifecycleManager.getGlobalChanges().any( |
| 685 | RequestedLayerState::Changes::Hierarchy) && |
| 686 | !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) && |
| 687 | !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 688 | return; |
| 689 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 690 | |
Vishnu Nair | 3fbe326 | 2023-09-29 17:07:00 -0700 | [diff] [blame] | 691 | using FrameRateCompatibility = scheduler::FrameRateCompatibility; |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 692 | if (snapshot.frameRate.isValid()) { |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 693 | // we already have a valid framerate. |
| 694 | return; |
| 695 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 696 | |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 697 | // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes |
| 698 | // for the same reason we are allowing touch boost for those layers. See |
| 699 | // RefreshRateSelector::rankFrameRates for details. |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 700 | const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.vote.rate.isValid() && |
| 701 | childSnapshot.frameRate.vote.type == FrameRateCompatibility::Default; |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 702 | const auto layerVotedWithNoVote = |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 703 | childSnapshot.frameRate.vote.type == FrameRateCompatibility::NoVote; |
| 704 | const auto layerVotedWithCategory = |
| 705 | childSnapshot.frameRate.category != FrameRateCategory::Default; |
| 706 | const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.vote.rate.isValid() && |
| 707 | childSnapshot.frameRate.vote.type == FrameRateCompatibility::Exact; |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 708 | |
| 709 | bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote || |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 710 | layerVotedWithCategory || layerVotedWithExactCompatibility; |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 711 | |
| 712 | // If we don't have a valid frame rate, but the children do, we set this |
| 713 | // layer as NoVote to allow the children to control the refresh rate |
| 714 | if (childHasValidFrameRate) { |
| 715 | snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote); |
| 716 | snapshot.changes |= RequestedLayerState::Changes::FrameRate; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 720 | void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) { |
| 721 | snapshot.isHiddenByPolicyFromRelativeParent = false; |
| 722 | snapshot.relativeLayerMetadata.mMap.clear(); |
| 723 | } |
| 724 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 725 | void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args, |
| 726 | const RequestedLayerState& requested, |
| 727 | const LayerSnapshot& parentSnapshot, |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 728 | const LayerHierarchy::TraversalPath& path) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 729 | // Always update flags and visibility |
| 730 | ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes & |
| 731 | (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry | |
| 732 | RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata | |
Vishnu Nair | f13c898 | 2023-12-02 11:26:09 -0800 | [diff] [blame] | 733 | RequestedLayerState::Changes::AffectsChildren | RequestedLayerState::Changes::Input | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 734 | RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode); |
| 735 | snapshot.changes |= parentChanges; |
| 736 | if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry; |
| 737 | snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable; |
| 738 | snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 739 | snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent || |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 740 | parentSnapshot.invalidTransform || requested.isHiddenByPolicy() || |
| 741 | (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end()); |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 742 | |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 743 | const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL || |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 744 | snapshot.clientChanges & layer_state_t::eReparent || |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 745 | snapshot.changes.any(RequestedLayerState::Changes::Visibility | |
| 746 | RequestedLayerState::Changes::Created); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 747 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 748 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) { |
| 749 | // If root layer, use the layer stack otherwise get the parent's layer stack. |
| 750 | snapshot.outputFilter.layerStack = |
| 751 | parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT |
| 752 | ? requested.layerStack |
| 753 | : parentSnapshot.outputFilter.layerStack; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Chavi Weingarten | b74093a | 2023-10-11 20:29:59 +0000 | [diff] [blame] | 756 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) { |
Vishnu Nair | 9e0017e | 2024-05-22 19:02:44 +0000 | [diff] [blame] | 757 | switch (requested.trustedOverlay) { |
| 758 | case gui::TrustedOverlay::UNSET: |
| 759 | snapshot.trustedOverlay = parentSnapshot.trustedOverlay; |
| 760 | break; |
| 761 | case gui::TrustedOverlay::DISABLED: |
| 762 | snapshot.trustedOverlay = FlagManager::getInstance().override_trusted_overlay() |
| 763 | ? requested.trustedOverlay |
| 764 | : parentSnapshot.trustedOverlay; |
| 765 | break; |
| 766 | case gui::TrustedOverlay::ENABLED: |
| 767 | snapshot.trustedOverlay = requested.trustedOverlay; |
| 768 | break; |
| 769 | } |
Chavi Weingarten | b74093a | 2023-10-11 20:29:59 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 772 | if (snapshot.isHiddenByPolicyFromParent && |
| 773 | !snapshot.changes.test(RequestedLayerState::Changes::Created)) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 774 | if (forceUpdate || |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 775 | snapshot.changes.any(RequestedLayerState::Changes::Geometry | |
Vishnu Nair | 494a2e4 | 2023-11-10 17:21:19 -0800 | [diff] [blame] | 776 | RequestedLayerState::Changes::BufferSize | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 777 | RequestedLayerState::Changes::Input)) { |
| 778 | updateInput(snapshot, requested, parentSnapshot, path, args); |
| 779 | } |
Nergi Rahardi | 0dfc096 | 2024-05-23 06:57:36 +0000 | [diff] [blame^] | 780 | if (forceUpdate || |
| 781 | (args.includeMetadata && |
| 782 | snapshot.changes.test(RequestedLayerState::Changes::Metadata))) { |
| 783 | updateMetadataAndGameMode(snapshot, requested, args, parentSnapshot); |
| 784 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 785 | return; |
| 786 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 787 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 788 | if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) { |
| 789 | // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers |
| 790 | // marked as skip capture |
| 791 | snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag || |
| 792 | (requested.layerStackToMirror != ui::INVALID_LAYER_STACK); |
| 793 | } |
| 794 | |
| 795 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 796 | snapshot.color.a = parentSnapshot.color.a * requested.color.a; |
| 797 | snapshot.alpha = snapshot.color.a; |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 798 | snapshot.inputInfo.alpha = snapshot.color.a; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 799 | } |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 800 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 801 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 802 | snapshot.isSecure = |
| 803 | parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 804 | snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay || |
| 805 | (requested.flags & layer_state_t::eLayerSkipScreenshot); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 808 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 809 | snapshot.stretchEffect = (requested.stretchEffect.hasEffect()) |
| 810 | ? requested.stretchEffect |
| 811 | : parentSnapshot.stretchEffect; |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 815 | if (!parentSnapshot.colorTransformIsIdentity) { |
| 816 | snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform; |
| 817 | snapshot.colorTransformIsIdentity = false; |
| 818 | } else { |
| 819 | snapshot.colorTransform = requested.colorTransform; |
| 820 | snapshot.colorTransformIsIdentity = !requested.hasColorTransform; |
| 821 | } |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Nergi Rahardi | 27613c3 | 2024-05-23 06:57:02 +0000 | [diff] [blame] | 824 | if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::Metadata)) { |
Nergi Rahardi | 0dfc096 | 2024-05-23 06:57:36 +0000 | [diff] [blame^] | 825 | updateMetadataAndGameMode(snapshot, requested, args, parentSnapshot); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 828 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged || |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 829 | args.displayChanges) { |
| 830 | snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID |
| 831 | ? requested.fixedTransformHint |
| 832 | : parentSnapshot.fixedTransformHint; |
| 833 | |
| 834 | if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) { |
| 835 | snapshot.transformHint = snapshot.fixedTransformHint; |
| 836 | } else { |
| 837 | const auto display = args.displays.get(snapshot.outputFilter.layerStack); |
| 838 | snapshot.transformHint = display.has_value() |
| 839 | ? std::make_optional<>(display->get().transformHint) |
| 840 | : std::nullopt; |
| 841 | } |
| 842 | } |
| 843 | |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 844 | if (forceUpdate || |
Vishnu Nair | 52d56fd | 2023-07-20 17:02:43 +0000 | [diff] [blame] | 845 | args.layerLifecycleManager.getGlobalChanges().any( |
| 846 | RequestedLayerState::Changes::Hierarchy) || |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 847 | snapshot.changes.any(RequestedLayerState::Changes::FrameRate | |
| 848 | RequestedLayerState::Changes::Hierarchy)) { |
Rachel Lee | a021bb0 | 2023-11-20 21:51:09 -0800 | [diff] [blame] | 849 | const bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy == |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 850 | scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren; |
Rachel Lee | a021bb0 | 2023-11-20 21:51:09 -0800 | [diff] [blame] | 851 | const bool propagationAllowed = parentSnapshot.frameRateSelectionStrategy != |
Rachel Lee | 70f7b69 | 2023-11-22 11:24:02 -0800 | [diff] [blame] | 852 | scheduler::LayerInfo::FrameRateSelectionStrategy::Self; |
Rachel Lee | a021bb0 | 2023-11-20 21:51:09 -0800 | [diff] [blame] | 853 | if ((!requested.requestedFrameRate.isValid() && propagationAllowed) || |
| 854 | shouldOverrideChildren) { |
Vishnu Nair | 30515cb | 2023-10-19 21:54:08 -0700 | [diff] [blame] | 855 | snapshot.inheritedFrameRate = parentSnapshot.inheritedFrameRate; |
| 856 | } else { |
| 857 | snapshot.inheritedFrameRate = requested.requestedFrameRate; |
| 858 | } |
| 859 | // Set the framerate as the inherited frame rate and allow children to override it if |
| 860 | // needed. |
| 861 | snapshot.frameRate = snapshot.inheritedFrameRate; |
Vishnu Nair | 52d56fd | 2023-07-20 17:02:43 +0000 | [diff] [blame] | 862 | snapshot.changes |= RequestedLayerState::Changes::FrameRate; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 865 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) { |
Rachel Lee | a021bb0 | 2023-11-20 21:51:09 -0800 | [diff] [blame] | 866 | if (parentSnapshot.frameRateSelectionStrategy == |
| 867 | scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren) { |
| 868 | snapshot.frameRateSelectionStrategy = |
| 869 | scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren; |
| 870 | } else { |
| 871 | const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy( |
| 872 | requested.frameRateSelectionStrategy); |
| 873 | snapshot.frameRateSelectionStrategy = strategy; |
| 874 | } |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Vishnu Nair | 3d8565a | 2023-06-30 07:23:24 +0000 | [diff] [blame] | 877 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) { |
| 878 | snapshot.frameRateSelectionPriority = |
| 879 | (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET) |
| 880 | ? parentSnapshot.frameRateSelectionPriority |
| 881 | : requested.frameRateSelectionPriority; |
| 882 | } |
| 883 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 884 | if (forceUpdate || |
| 885 | snapshot.clientChanges & |
| 886 | (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged | |
| 887 | layer_state_t::eAlphaChanged)) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 888 | snapshot.backgroundBlurRadius = args.supportsBlur |
| 889 | ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius) |
| 890 | : 0; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 891 | snapshot.blurRegions = requested.blurRegions; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 892 | for (auto& region : snapshot.blurRegions) { |
| 893 | region.alpha = region.alpha * snapshot.color.a; |
| 894 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 897 | if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) { |
| 898 | uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays); |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 899 | updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged || |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 903 | snapshot.changes.any(RequestedLayerState::Changes::Geometry | |
| 904 | RequestedLayerState::Changes::BufferUsageFlags)) { |
| 905 | updateRoundedCorner(snapshot, requested, parentSnapshot, args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 908 | if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged || |
| 909 | snapshot.changes.any(RequestedLayerState::Changes::Geometry)) { |
| 910 | updateShadows(snapshot, requested, args.globalShadowSettings); |
| 911 | } |
| 912 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 913 | if (forceUpdate || |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 914 | snapshot.changes.any(RequestedLayerState::Changes::Geometry | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 915 | RequestedLayerState::Changes::Input)) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 916 | updateInput(snapshot, requested, parentSnapshot, path, args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | // computed snapshot properties |
Alec Mouri | 994761f | 2023-08-04 21:50:55 +0000 | [diff] [blame] | 920 | snapshot.forceClientComposition = |
| 921 | snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect(); |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 922 | snapshot.contentOpaque = snapshot.isContentOpaque(); |
| 923 | snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() && |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 924 | snapshot.color.a == 1.f; |
| 925 | snapshot.blendMode = getBlendMode(snapshot, requested); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 926 | LLOGV(snapshot.sequence, |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 927 | "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s", |
| 928 | args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "", |
| 929 | snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(), |
| 930 | parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(), |
| 931 | std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot, |
| 935 | const RequestedLayerState& requested, |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 936 | const LayerSnapshot& parentSnapshot, |
| 937 | const Args& args) { |
| 938 | if (args.skipRoundCornersWhenProtected && requested.isProtected()) { |
| 939 | snapshot.roundedCorner = RoundedCornerState(); |
| 940 | return; |
| 941 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 942 | snapshot.roundedCorner = RoundedCornerState(); |
| 943 | RoundedCornerState parentRoundedCorner; |
| 944 | if (parentSnapshot.roundedCorner.hasRoundedCorners()) { |
| 945 | parentRoundedCorner = parentSnapshot.roundedCorner; |
| 946 | ui::Transform t = snapshot.localTransform.inverse(); |
| 947 | parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect); |
| 948 | parentRoundedCorner.radius.x *= t.getScaleX(); |
| 949 | parentRoundedCorner.radius.y *= t.getScaleY(); |
| 950 | } |
| 951 | |
| 952 | FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect(); |
| 953 | const vec2 radius(requested.cornerRadius, requested.cornerRadius); |
| 954 | RoundedCornerState layerSettings(layerCropRect, radius); |
| 955 | const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty(); |
| 956 | const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners(); |
| 957 | if (layerSettingsValid && parentRoundedCornerValid) { |
| 958 | // If the parent and the layer have rounded corner settings, use the parent settings if |
| 959 | // the parent crop is entirely inside the layer crop. This has limitations and cause |
| 960 | // rendering artifacts. See b/200300845 for correct fix. |
| 961 | if (parentRoundedCorner.cropRect.left > layerCropRect.left && |
| 962 | parentRoundedCorner.cropRect.top > layerCropRect.top && |
| 963 | parentRoundedCorner.cropRect.right < layerCropRect.right && |
| 964 | parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) { |
| 965 | snapshot.roundedCorner = parentRoundedCorner; |
| 966 | } else { |
| 967 | snapshot.roundedCorner = layerSettings; |
| 968 | } |
| 969 | } else if (layerSettingsValid) { |
| 970 | snapshot.roundedCorner = layerSettings; |
| 971 | } else if (parentRoundedCornerValid) { |
| 972 | snapshot.roundedCorner = parentRoundedCorner; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot, |
| 977 | const RequestedLayerState& requested, |
| 978 | const LayerSnapshot& parentSnapshot, |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 979 | uint32_t primaryDisplayRotationFlags) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 980 | snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 981 | const bool transformWasInvalid = snapshot.invalidTransform; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 982 | snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform); |
| 983 | if (snapshot.invalidTransform) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 984 | auto& t = snapshot.geomLayerTransform; |
| 985 | auto& requestedT = requested.requestedTransform; |
| 986 | std::string transformDebug = |
| 987 | base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}", |
| 988 | t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(), |
| 989 | requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy()); |
| 990 | std::string bufferDebug; |
| 991 | if (requested.externalTexture) { |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 992 | auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 993 | auto& destFrame = requested.destinationFrame; |
| 994 | bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d" |
| 995 | " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}", |
| 996 | requested.externalTexture->getWidth(), |
| 997 | requested.externalTexture->getHeight(), |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 998 | primaryDisplayRotationFlags, destFrame.left, |
| 999 | destFrame.top, destFrame.right, destFrame.bottom, |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1000 | unRotBuffer.getHeight(), unRotBuffer.getWidth()); |
| 1001 | } |
| 1002 | ALOGW("Resetting transform for %s because it is invalid.%s%s", |
| 1003 | snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str()); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1004 | snapshot.geomLayerTransform.reset(); |
| 1005 | } |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1006 | if (transformWasInvalid != snapshot.invalidTransform) { |
| 1007 | // If transform is invalid, the layer will be hidden. |
| 1008 | mResortSnapshots = true; |
| 1009 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1010 | snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse(); |
| 1011 | |
| 1012 | FloatRect parentBounds = parentSnapshot.geomLayerBounds; |
| 1013 | parentBounds = snapshot.localTransform.inverse().transform(parentBounds); |
| 1014 | snapshot.geomLayerBounds = |
| 1015 | (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds; |
| 1016 | if (!requested.crop.isEmpty()) { |
| 1017 | snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect()); |
| 1018 | } |
| 1019 | snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds); |
| 1020 | snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1021 | const Rect geomLayerBoundsWithoutTransparentRegion = |
| 1022 | RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds), |
| 1023 | requested.transparentRegion); |
| 1024 | snapshot.transformedBoundsWithoutTransparentRegion = |
| 1025 | snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1026 | snapshot.parentTransform = parentSnapshot.geomLayerTransform; |
| 1027 | |
| 1028 | // Subtract the transparent region and snap to the bounds |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1029 | const Rect bounds = |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1030 | RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1031 | if (requested.potentialCursor) { |
| 1032 | snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds); |
| 1033 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 1036 | void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&, |
Vishnu Nair | d9e4f46 | 2023-10-06 04:05:45 +0000 | [diff] [blame] | 1037 | const ShadowSettings& globalShadowSettings) { |
| 1038 | if (snapshot.shadowSettings.length > 0.f) { |
| 1039 | snapshot.shadowSettings.ambientColor = globalShadowSettings.ambientColor; |
| 1040 | snapshot.shadowSettings.spotColor = globalShadowSettings.spotColor; |
| 1041 | snapshot.shadowSettings.lightPos = globalShadowSettings.lightPos; |
| 1042 | snapshot.shadowSettings.lightRadius = globalShadowSettings.lightRadius; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1043 | |
| 1044 | // Note: this preserves existing behavior of shadowing the entire layer and not cropping |
| 1045 | // it if transparent regions are present. This may not be necessary since shadows are |
| 1046 | // typically cast by layers without transparent regions. |
| 1047 | snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds; |
| 1048 | |
| 1049 | // If the casting layer is translucent, we need to fill in the shadow underneath the |
| 1050 | // layer. Otherwise the generated shadow will only be shown around the casting layer. |
| 1051 | snapshot.shadowSettings.casterIsTranslucent = |
| 1052 | !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f); |
| 1053 | snapshot.shadowSettings.ambientColor *= snapshot.alpha; |
| 1054 | snapshot.shadowSettings.spotColor *= snapshot.alpha; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot, |
| 1059 | const RequestedLayerState& requested, |
| 1060 | const LayerSnapshot& parentSnapshot, |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1061 | const LayerHierarchy::TraversalPath& path, |
| 1062 | const Args& args) { |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1063 | using InputConfig = gui::WindowInfo::InputConfig; |
| 1064 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1065 | if (requested.windowInfoHandle) { |
| 1066 | snapshot.inputInfo = *requested.windowInfoHandle->getInfo(); |
| 1067 | } else { |
| 1068 | snapshot.inputInfo = {}; |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 1069 | // b/271132344 revisit this and see if we can always use the layers uid/pid |
| 1070 | snapshot.inputInfo.name = requested.name; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1071 | snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid}; |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1072 | snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid}; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1073 | } |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1074 | snapshot.touchCropId = requested.touchCropId; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1075 | |
Vishnu Nair | 93b8b79 | 2023-02-27 19:40:24 +0000 | [diff] [blame] | 1076 | snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 1077 | snapshot.inputInfo.displayId = |
| 1078 | ui::LogicalDisplayId{static_cast<int32_t>(snapshot.outputFilter.layerStack.id)}; |
Vishnu Nair | f13c898 | 2023-12-02 11:26:09 -0800 | [diff] [blame] | 1079 | snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo() |
| 1080 | ? requested.windowInfoHandle->getInfo()->touchOcclusionMode |
| 1081 | : parentSnapshot.inputInfo.touchOcclusionMode; |
Vishnu Nair | 59a6be3 | 2024-01-29 10:26:21 -0800 | [diff] [blame] | 1082 | snapshot.inputInfo.canOccludePresentation = parentSnapshot.inputInfo.canOccludePresentation || |
| 1083 | (requested.flags & layer_state_t::eCanOccludePresentation); |
Vishnu Nair | f13c898 | 2023-12-02 11:26:09 -0800 | [diff] [blame] | 1084 | if (requested.dropInputMode == gui::DropInputMode::ALL || |
| 1085 | parentSnapshot.dropInputMode == gui::DropInputMode::ALL) { |
| 1086 | snapshot.dropInputMode = gui::DropInputMode::ALL; |
| 1087 | } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED || |
| 1088 | parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) { |
| 1089 | snapshot.dropInputMode = gui::DropInputMode::OBSCURED; |
| 1090 | } else { |
| 1091 | snapshot.dropInputMode = gui::DropInputMode::NONE; |
| 1092 | } |
| 1093 | |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1094 | if (snapshot.isSecure || |
Arpit Singh | 490ccc9 | 2024-04-30 14:26:21 +0000 | [diff] [blame] | 1095 | parentSnapshot.inputInfo.inputConfig.test(InputConfig::SENSITIVE_FOR_PRIVACY)) { |
| 1096 | snapshot.inputInfo.inputConfig |= InputConfig::SENSITIVE_FOR_PRIVACY; |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1099 | updateVisibility(snapshot, snapshot.isVisible); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1100 | if (!needsInputInfo(snapshot, requested)) { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1101 | return; |
| 1102 | } |
| 1103 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1104 | static frontend::DisplayInfo sDefaultInfo = {.isSecure = false}; |
| 1105 | const std::optional<frontend::DisplayInfo> displayInfoOpt = |
| 1106 | args.displays.get(snapshot.outputFilter.layerStack); |
| 1107 | bool noValidDisplay = !displayInfoOpt.has_value(); |
| 1108 | auto displayInfo = displayInfoOpt.value_or(sDefaultInfo); |
| 1109 | |
| 1110 | if (!requested.windowInfoHandle) { |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1111 | snapshot.inputInfo.inputConfig = InputConfig::NO_INPUT_CHANNEL; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1112 | } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1113 | fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot); |
| 1114 | |
| 1115 | if (noValidDisplay) { |
| 1116 | // Do not let the window receive touches if it is not associated with a valid display |
| 1117 | // transform. We still allow the window to receive keys and prevent ANRs. |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1118 | snapshot.inputInfo.inputConfig |= InputConfig::NOT_TOUCHABLE; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1121 | snapshot.inputInfo.alpha = snapshot.color.a; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1122 | |
| 1123 | handleDropInputMode(snapshot, parentSnapshot); |
| 1124 | |
| 1125 | // If the window will be blacked out on a display because the display does not have the secure |
| 1126 | // flag and the layer has the secure flag set, then drop input. |
| 1127 | if (!displayInfo.isSecure && snapshot.isSecure) { |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1128 | snapshot.inputInfo.inputConfig |= InputConfig::DROP_INPUT; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 1131 | if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) { |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1132 | mNeedsTouchableRegionCrop.insert(path); |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 1133 | } |
| 1134 | auto cropLayerSnapshot = getSnapshot(requested.touchCropId); |
| 1135 | if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) { |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1136 | FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first; |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 1137 | Rect inputBoundsInDisplaySpace = |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1138 | getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform); |
| 1139 | snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state |
| 1143 | // if it was set by WM for a known system overlay |
Vishnu Nair | 9e0017e | 2024-05-22 19:02:44 +0000 | [diff] [blame] | 1144 | if (snapshot.trustedOverlay == gui::TrustedOverlay::ENABLED) { |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1145 | snapshot.inputInfo.inputConfig |= InputConfig::TRUSTED_OVERLAY; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Vishnu Nair | 494a2e4 | 2023-11-10 17:21:19 -0800 | [diff] [blame] | 1148 | snapshot.inputInfo.contentSize = snapshot.croppedBufferSize.getSize(); |
| 1149 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1150 | // If the layer is a clone, we need to crop the input region to cloned root to prevent |
| 1151 | // touches from going outside the cloned area. |
| 1152 | if (path.isClone()) { |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1153 | snapshot.inputInfo.inputConfig |= InputConfig::CLONE; |
Vishnu Nair | 444f395 | 2023-04-11 13:01:02 -0700 | [diff] [blame] | 1154 | // Cloned layers shouldn't handle watch outside since their z order is not determined by |
| 1155 | // WM or the client. |
Prabir Pradhan | cf35919 | 2024-03-20 00:42:57 +0000 | [diff] [blame] | 1156 | snapshot.inputInfo.inputConfig.clear(InputConfig::WATCH_OUTSIDE_TOUCH); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() { |
| 1161 | return mSnapshots; |
| 1162 | } |
| 1163 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1164 | void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const { |
| 1165 | for (int i = 0; i < mNumInterestingSnapshots; i++) { |
| 1166 | LayerSnapshot& snapshot = *mSnapshots[(size_t)i]; |
| 1167 | if (!snapshot.isVisible) continue; |
| 1168 | visitor(snapshot); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 1172 | // Visit each visible snapshot in z-order |
| 1173 | void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor, |
| 1174 | const LayerHierarchy& root) const { |
| 1175 | root.traverseInZOrder( |
| 1176 | [this, visitor](const LayerHierarchy&, |
| 1177 | const LayerHierarchy::TraversalPath& traversalPath) -> bool { |
| 1178 | LayerSnapshot* snapshot = getSnapshot(traversalPath); |
| 1179 | if (snapshot && snapshot->isVisible) { |
| 1180 | visitor(*snapshot); |
| 1181 | } |
| 1182 | return true; |
| 1183 | }); |
| 1184 | } |
| 1185 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1186 | void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) { |
| 1187 | for (int i = 0; i < mNumInterestingSnapshots; i++) { |
| 1188 | std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i); |
| 1189 | if (!snapshot->isVisible) continue; |
| 1190 | visitor(snapshot); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Nergi Rahardi | 0dfc096 | 2024-05-23 06:57:36 +0000 | [diff] [blame^] | 1194 | void LayerSnapshotBuilder::forEachSnapshot(const Visitor& visitor, |
| 1195 | const ConstPredicate& predicate) { |
| 1196 | for (int i = 0; i < mNumInterestingSnapshots; i++) { |
| 1197 | std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i); |
| 1198 | if (!predicate(*snapshot)) continue; |
| 1199 | visitor(snapshot); |
| 1200 | } |
| 1201 | } |
| 1202 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 1203 | void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const { |
| 1204 | for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) { |
| 1205 | LayerSnapshot& snapshot = *mSnapshots[(size_t)i]; |
| 1206 | if (!snapshot.hasInputInfo()) continue; |
| 1207 | visitor(snapshot); |
| 1208 | } |
| 1209 | } |
| 1210 | |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1211 | void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) { |
| 1212 | if (mNeedsTouchableRegionCrop.empty()) { |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT = |
| 1217 | RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created | |
| 1218 | RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry | |
| 1219 | RequestedLayerState::Changes::Input; |
| 1220 | |
| 1221 | if (args.forceUpdate != ForceUpdateFlags::ALL && |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 1222 | !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) { |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | for (auto& path : mNeedsTouchableRegionCrop) { |
| 1227 | frontend::LayerSnapshot* snapshot = getSnapshot(path); |
| 1228 | if (!snapshot) { |
| 1229 | continue; |
| 1230 | } |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 1231 | LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s", |
| 1232 | snapshot->getDebugString().c_str()); |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1233 | const std::optional<frontend::DisplayInfo> displayInfoOpt = |
| 1234 | args.displays.get(snapshot->outputFilter.layerStack); |
| 1235 | static frontend::DisplayInfo sDefaultInfo = {.isSecure = false}; |
| 1236 | auto displayInfo = displayInfoOpt.value_or(sDefaultInfo); |
| 1237 | |
| 1238 | bool needsUpdate = |
| 1239 | args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT); |
| 1240 | auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId); |
| 1241 | needsUpdate = |
| 1242 | needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT)); |
| 1243 | auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr; |
| 1244 | needsUpdate = needsUpdate || |
| 1245 | (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT)); |
| 1246 | |
| 1247 | if (!needsUpdate) { |
| 1248 | continue; |
| 1249 | } |
| 1250 | |
| 1251 | if (snapshot->inputInfo.replaceTouchableRegionWithCrop) { |
| 1252 | Rect inputBoundsInDisplaySpace; |
| 1253 | if (!cropLayerSnapshot) { |
| 1254 | FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first; |
| 1255 | inputBoundsInDisplaySpace = |
| 1256 | getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform); |
| 1257 | } else { |
| 1258 | FloatRect inputBounds = |
| 1259 | getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first; |
| 1260 | inputBoundsInDisplaySpace = |
| 1261 | getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds, |
| 1262 | displayInfo.transform); |
| 1263 | } |
| 1264 | snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace); |
| 1265 | } else if (cropLayerSnapshot) { |
| 1266 | FloatRect inputBounds = |
| 1267 | getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first; |
| 1268 | Rect inputBoundsInDisplaySpace = |
| 1269 | getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds, |
| 1270 | displayInfo.transform); |
Chavi Weingarten | 1ba381e | 2024-01-09 21:54:11 +0000 | [diff] [blame] | 1271 | snapshot->inputInfo.touchableRegion = |
| 1272 | snapshot->inputInfo.touchableRegion.intersect(inputBoundsInDisplaySpace); |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | // If the layer is a clone, we need to crop the input region to cloned root to prevent |
| 1276 | // touches from going outside the cloned area. |
| 1277 | if (clonedRootSnapshot) { |
| 1278 | const Rect rect = |
| 1279 | displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds}); |
| 1280 | snapshot->inputInfo.touchableRegion = |
| 1281 | snapshot->inputInfo.touchableRegion.intersect(rect); |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 1286 | } // namespace android::surfaceflinger::frontend |