blob: 2d424372f049e516d276208c14d3ab4edbe2c568 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001/*
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 Naira02943f2023-06-03 13:44:46 -070020#define LOG_TAG "SurfaceFlinger"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000021
Vishnu Nair8fc721b2022-12-22 20:06:32 +000022#include <numeric>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070023#include <optional>
24
Vishnu Nair9e0017e2024-05-22 19:02:44 +000025#include <common/FlagManager.h>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050026#include <ftl/small_map.h>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070027#include <gui/TraceUtils.h>
Vishnu Naira02943f2023-06-03 13:44:46 -070028#include <ui/DisplayMap.h>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050029#include <ui/FloatRect.h>
30
Vishnu Nair8fc721b2022-12-22 20:06:32 +000031#include "DisplayHardware/HWC2.h"
32#include "DisplayHardware/Hal.h"
Vishnu Nair3d8565a2023-06-30 07:23:24 +000033#include "Layer.h" // eFrameRateSelectionPriority constants
Vishnu Naircfb2d252023-01-19 04:44:02 +000034#include "LayerLog.h"
Vishnu Nairb76d99a2023-03-19 18:22:31 -070035#include "LayerSnapshotBuilder.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000036#include "TimeStats/TimeStats.h"
Vishnu Naird1f74982023-06-15 20:16:51 -070037#include "Tracing/TransactionTracing.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000038
39namespace android::surfaceflinger::frontend {
40
41using namespace ftl::flag_operators;
42
43namespace {
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050044
45FloatRect getMaxDisplayBounds(const DisplayInfos& displays) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000046 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.
68Region 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 */
104ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
105 if (!snapshot.hasBufferOrSidebandStream()) {
106 return snapshot.geomLayerTransform;
107 }
108 return snapshot.parentTransform;
109}
110
111/**
Vishnu Nairfed7c122023-03-18 01:54:43 +0000112 * Returns the bounds used to fill the input frame and the touchable region.
113 *
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000114 * 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 Nairfed7c122023-03-18 01:54:43 +0000118std::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 Nair8fc721b2022-12-22 20:06:32 +0000123 }
124
Vishnu Nairfed7c122023-03-18 01:54:43 +0000125 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 Nair8fc721b2022-12-22 20:06:32 +0000137 }
Vishnu Nairfed7c122023-03-18 01:54:43 +0000138
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 Nair8fc721b2022-12-22 20:06:32 +0000150}
151
Vishnu Nairfed7c122023-03-18 01:54:43 +0000152Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds,
153 const ui::Transform& screenToDisplay) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000154 // 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 Nairfed7c122023-03-18 01:54:43 +0000157
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000158 // 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 Nair8fc721b2022-12-22 20:06:32 +0000165
166 // Crop the input bounds to ensure it is within the parent's bounds.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000167 const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000168
169 const ui::Transform layerToScreen = getInputTransform(snapshot);
170 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
171
Vishnu Nairfed7c122023-03-18 01:54:43 +0000172 return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)};
173}
174
175void 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 Weingarten7f019192023-08-08 20:39:01 +0000182 info.frame = getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000183
184 ui::Transform inputToLayer;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000185 inputToLayer.set(inputBounds.left, inputBounds.top);
186 const ui::Transform layerToScreen = getInputTransform(snapshot);
187 const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000188
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
197void 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 Nair8fc721b2022-12-22 20:06:32 +0000249auto 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 Nair80a5a702023-02-11 01:21:51 +0000258void updateVisibility(LayerSnapshot& snapshot, bool visible) {
Vishnu Nairb4a6a772024-06-12 14:41:08 -0700259 if (snapshot.isVisible != visible) {
260 snapshot.changes |= RequestedLayerState::Changes::Visibility;
261 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000262 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000263
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 Nair80a5a702023-02-11 01:21:51 +0000275 const bool visibleForInput =
Vishnu Nair40d02282023-02-28 21:11:40 +0000276 snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000277 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naira02943f2023-06-03 13:44:46 -0700278 LLOGV(snapshot.sequence, "updating visibility %s %s", visible ? "true" : "false",
279 snapshot.getDebugString().c_str());
Vishnu Naircfb2d252023-01-19 04:44:02 +0000280}
281
282bool 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 Nairc765c6c2023-02-23 00:08:01 +0000300void 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
Vishnu Naircfb2d252023-01-19 04:44:02 +0000320void clearChanges(LayerSnapshot& snapshot) {
321 snapshot.changes.clear();
Vishnu Naira02943f2023-06-03 13:44:46 -0700322 snapshot.clientChanges = 0;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000323 snapshot.contentDirty = false;
324 snapshot.hasReadyFrame = false;
325 snapshot.sidebandStreamHasFrame = false;
326 snapshot.surfaceDamage.clear();
327}
328
Vishnu Naira02943f2023-06-03 13:44:46 -0700329// TODO (b/259407931): Remove.
330uint32_t getPrimaryDisplayRotationFlags(
331 const ui::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
332 for (auto& [_, display] : displays) {
333 if (display.isPrimary) {
334 return display.rotationFlags;
335 }
336 }
337 return 0;
338}
339
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000340} // namespace
341
342LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
343 LayerSnapshot snapshot;
Vishnu Nair92990e22023-02-24 20:01:05 +0000344 snapshot.path = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000345 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
Vishnu Naira02943f2023-06-03 13:44:46 -0700346 snapshot.clientChanges = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000347 snapshot.isHiddenByPolicyFromParent = false;
348 snapshot.isHiddenByPolicyFromRelativeParent = false;
349 snapshot.parentTransform.reset();
350 snapshot.geomLayerTransform.reset();
351 snapshot.geomInverseLayerTransform.reset();
352 snapshot.geomLayerBounds = getMaxDisplayBounds({});
353 snapshot.roundedCorner = RoundedCornerState();
354 snapshot.stretchEffect = {};
355 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
356 snapshot.outputFilter.toInternalDisplay = false;
357 snapshot.isSecure = false;
358 snapshot.color.a = 1.0_hf;
359 snapshot.colorTransformIsIdentity = true;
Vishnu Naird9e4f462023-10-06 04:05:45 +0000360 snapshot.shadowSettings.length = 0.f;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000361 snapshot.layerMetadata.mMap.clear();
362 snapshot.relativeLayerMetadata.mMap.clear();
363 snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
364 snapshot.dropInputMode = gui::DropInputMode::NONE;
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000365 snapshot.trustedOverlay = gui::TrustedOverlay::UNSET;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000366 snapshot.gameMode = gui::GameMode::Unsupported;
367 snapshot.frameRate = {};
368 snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
Vishnu Nair422b81c2024-05-16 05:44:28 +0000369 snapshot.ignoreLocalTransform = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000370 return snapshot;
371}
372
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000373LayerSnapshotBuilder::LayerSnapshotBuilder() {}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000374
375LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000376 args.forceUpdate = ForceUpdateFlags::ALL;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000377 updateSnapshots(args);
378}
379
380bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700381 const bool forceUpdate = args.forceUpdate != ForceUpdateFlags::NONE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000382
Vishnu Naira02943f2023-06-03 13:44:46 -0700383 if (args.layerLifecycleManager.getGlobalChanges().get() == 0 && !forceUpdate &&
384 !args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000385 return true;
386 }
387
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000388 // There are only content changes which do not require any child layer snapshots to be updated.
389 ALOGV("%s", __func__);
390 ATRACE_NAME("FastPath");
391
Vishnu Naira02943f2023-06-03 13:44:46 -0700392 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
393 if (forceUpdate || args.displayChanges) {
394 for (auto& snapshot : mSnapshots) {
395 const RequestedLayerState* requested =
396 args.layerLifecycleManager.getLayerFromId(snapshot->path.id);
397 if (!requested) continue;
398 snapshot->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
399 primaryDisplayRotationFlags);
400 }
401 return false;
402 }
403
404 // Walk through all the updated requested layer states and update the corresponding snapshots.
405 for (const RequestedLayerState* requested : args.layerLifecycleManager.getChangedLayers()) {
406 auto range = mIdToSnapshots.equal_range(requested->id);
407 for (auto it = range.first; it != range.second; it++) {
408 it->second->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
409 primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000410 }
411 }
412
Vishnu Naira02943f2023-06-03 13:44:46 -0700413 if ((args.layerLifecycleManager.getGlobalChanges().get() &
414 ~(RequestedLayerState::Changes::Content | RequestedLayerState::Changes::Buffer).get()) !=
415 0) {
416 // We have changes that require us to walk the hierarchy and update child layers.
417 // No fast path for you.
418 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000419 }
420 return true;
421}
422
423void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
424 ATRACE_NAME("UpdateSnapshots");
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000425 LayerSnapshot rootSnapshot = args.rootSnapshot;
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000426 if (args.parentCrop) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000427 rootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000428 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000429 rootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000430 }
431 if (args.displayChanges) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000432 rootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000433 RequestedLayerState::Changes::Geometry;
434 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000435 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000436 rootSnapshot.changes |=
Vishnu Naird47bcee2023-02-24 18:08:51 +0000437 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000438 rootSnapshot.clientChanges |= layer_state_t::eReparent;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000439 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700440
441 for (auto& snapshot : mSnapshots) {
442 if (snapshot->reachablilty == LayerSnapshot::Reachablilty::Reachable) {
443 snapshot->reachablilty = LayerSnapshot::Reachablilty::Unreachable;
444 }
445 }
446
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000447 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000448 if (args.root.getLayer()) {
449 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
450 // multiple children.
451 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
452 LayerHierarchy::Variant::Attached);
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000453 updateSnapshotsInHierarchy(args, args.root, root, rootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000454 } else {
455 for (auto& [childHierarchy, variant] : args.root.mChildren) {
456 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
457 childHierarchy->getLayer()->id,
458 variant);
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000459 updateSnapshotsInHierarchy(args, *childHierarchy, root, rootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000460 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000461 }
462
Vishnu Nair29354ec2023-03-28 18:51:28 -0700463 // Update touchable region crops outside the main update pass. This is because a layer could be
464 // cropped by any other layer and it requires both snapshots to be updated.
465 updateTouchableRegionCrop(args);
466
Vishnu Nairfccd6362023-02-24 23:39:53 +0000467 const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000468
Vishnu Nair29354ec2023-03-28 18:51:28 -0700469 // Destroy unreachable snapshots for clone layers. And destroy snapshots for non-clone
470 // layers if the layer have been destroyed.
471 // TODO(b/238781169) consider making clone layer ids stable as well
472 if (!hasUnreachableSnapshots && args.layerLifecycleManager.getDestroyedLayers().empty()) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000473 return;
474 }
475
Vishnu Nair29354ec2023-03-28 18:51:28 -0700476 std::unordered_set<uint32_t> destroyedLayerIds;
477 for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) {
478 destroyedLayerIds.insert(destroyedLayer->id);
479 }
480
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000481 auto it = mSnapshots.begin();
482 while (it < mSnapshots.end()) {
483 auto& traversalPath = it->get()->path;
Vishnu Naira02943f2023-06-03 13:44:46 -0700484 const bool unreachable =
485 it->get()->reachablilty == LayerSnapshot::Reachablilty::Unreachable;
486 const bool isClone = traversalPath.isClone();
487 const bool layerIsDestroyed =
488 destroyedLayerIds.find(traversalPath.id) != destroyedLayerIds.end();
489 const bool destroySnapshot = (unreachable && isClone) || layerIsDestroyed;
490
491 if (!destroySnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000492 it++;
493 continue;
494 }
495
Vishnu Naira02943f2023-06-03 13:44:46 -0700496 mPathToSnapshot.erase(traversalPath);
497
498 auto range = mIdToSnapshots.equal_range(traversalPath.id);
499 auto matchingSnapshot =
500 std::find_if(range.first, range.second, [&traversalPath](auto& snapshotWithId) {
501 return snapshotWithId.second->path == traversalPath;
502 });
503 mIdToSnapshots.erase(matchingSnapshot);
Vishnu Nair29354ec2023-03-28 18:51:28 -0700504 mNeedsTouchableRegionCrop.erase(traversalPath);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000505 mSnapshots.back()->globalZ = it->get()->globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000506 std::iter_swap(it, mSnapshots.end() - 1);
507 mSnapshots.erase(mSnapshots.end() - 1);
508 }
509}
510
511void LayerSnapshotBuilder::update(const Args& args) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000512 for (auto& snapshot : mSnapshots) {
513 clearChanges(*snapshot);
514 }
515
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000516 if (tryFastUpdate(args)) {
517 return;
518 }
519 updateSnapshots(args);
520}
521
Vishnu Naircfb2d252023-01-19 04:44:02 +0000522const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
523 const Args& args, const LayerHierarchy& hierarchy,
Vishnu Naird1f74982023-06-15 20:16:51 -0700524 LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot,
525 int depth) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700526 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
527 "Cycle detected in LayerSnapshotBuilder. See "
528 "builder_stack_overflow_transactions.winscope");
Vishnu Naird1f74982023-06-15 20:16:51 -0700529
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000530 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000531 LayerSnapshot* snapshot = getSnapshot(traversalPath);
532 const bool newSnapshot = snapshot == nullptr;
Vishnu Naira02943f2023-06-03 13:44:46 -0700533 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000534 if (newSnapshot) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000535 snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700536 snapshot->merge(*layer, /*forceUpdate=*/true, /*displayChanges=*/true, args.forceFullDamage,
537 primaryDisplayRotationFlags);
538 snapshot->changes |= RequestedLayerState::Changes::Created;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000539 }
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000540
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000541 if (traversalPath.isRelative()) {
542 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
543 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
544 } else {
545 if (traversalPath.isAttached()) {
546 resetRelativeState(*snapshot);
547 }
Vishnu Nair92990e22023-02-24 20:01:05 +0000548 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000549 }
550
551 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
552 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
553 childHierarchy->getLayer()->id,
554 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000555 const LayerSnapshot& childSnapshot =
Vishnu Naird1f74982023-06-15 20:16:51 -0700556 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot,
557 depth + 1);
Vishnu Nair42b918e2023-07-18 20:05:29 +0000558 updateFrameRateFromChildSnapshot(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000559 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000560
Vishnu Naircfb2d252023-01-19 04:44:02 +0000561 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000562}
563
564LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
565 if (layerId == UNASSIGNED_LAYER_ID) {
566 return nullptr;
567 }
568 LayerHierarchy::TraversalPath path{.id = layerId};
569 return getSnapshot(path);
570}
571
572LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700573 auto it = mPathToSnapshot.find(id);
574 return it == mPathToSnapshot.end() ? nullptr : it->second;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000575}
576
Vishnu Nair92990e22023-02-24 20:01:05 +0000577LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
578 const RequestedLayerState& layer,
579 const LayerSnapshot& parentSnapshot) {
580 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000581 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000582 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
Vishnu Nair491827d2024-04-29 23:43:26 +0000583 if (path.isClone() && !LayerHierarchy::isMirror(path.variant)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000584 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
585 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000586 snapshot->ignoreLocalTransform =
587 path.isClone() && path.variant == LayerHierarchy::Variant::Detached_Mirror;
Vishnu Naira02943f2023-06-03 13:44:46 -0700588 mPathToSnapshot[path] = snapshot;
589
590 mIdToSnapshots.emplace(path.id, snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000591 return snapshot;
592}
593
Vishnu Nairfccd6362023-02-24 23:39:53 +0000594bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000595 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000596 !args.layerLifecycleManager.getGlobalChanges().any(
Chavi Weingarten92c7d8c2024-01-19 23:25:45 +0000597 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility |
598 RequestedLayerState::Changes::Input)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000599 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
600 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000601 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000602 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000603 mResortSnapshots = false;
604
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000605 size_t globalZ = 0;
606 args.root.traverseInZOrder(
607 [this, &globalZ](const LayerHierarchy&,
608 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
609 LayerSnapshot* snapshot = getSnapshot(traversalPath);
610 if (!snapshot) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700611 return true;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000612 }
613
Vishnu Naircfb2d252023-01-19 04:44:02 +0000614 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000615 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000616 size_t oldZ = snapshot->globalZ;
617 size_t newZ = globalZ++;
618 snapshot->globalZ = newZ;
619 if (oldZ == newZ) {
620 return true;
621 }
622 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000623 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
624 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000625 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
626 mSnapshots.begin() + static_cast<ssize_t>(newZ));
627 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000628 return true;
629 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000630 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000631 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000632 while (globalZ < mSnapshots.size()) {
633 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000634 /* mark unreachable snapshots as explicitly invisible */
635 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Naira02943f2023-06-03 13:44:46 -0700636 if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
Vishnu Nairfccd6362023-02-24 23:39:53 +0000637 hasUnreachableSnapshots = true;
638 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000639 globalZ++;
640 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000641 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000642}
643
644void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
645 const LayerSnapshot& parentSnapshot,
646 bool parentIsRelative, const Args& args) {
647 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000648 snapshot.isHiddenByPolicyFromRelativeParent =
649 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000650 if (args.includeMetadata) {
651 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
652 }
653 } else {
654 snapshot.isHiddenByPolicyFromRelativeParent =
655 parentSnapshot.isHiddenByPolicyFromRelativeParent;
656 if (args.includeMetadata) {
657 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
658 }
659 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700660 if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
661 snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
662 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000663}
664
Vishnu Nair42b918e2023-07-18 20:05:29 +0000665void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
666 const LayerSnapshot& childSnapshot,
667 const Args& args) {
668 if (args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000669 !args.layerLifecycleManager.getGlobalChanges().any(
670 RequestedLayerState::Changes::Hierarchy) &&
671 !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) &&
672 !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000673 return;
674 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000675
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700676 using FrameRateCompatibility = scheduler::FrameRateCompatibility;
Rachel Leece6e0042023-06-27 11:22:54 -0700677 if (snapshot.frameRate.isValid()) {
Vishnu Nair42b918e2023-07-18 20:05:29 +0000678 // we already have a valid framerate.
679 return;
680 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000681
Vishnu Nair42b918e2023-07-18 20:05:29 +0000682 // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes
683 // for the same reason we are allowing touch boost for those layers. See
684 // RefreshRateSelector::rankFrameRates for details.
Rachel Leece6e0042023-06-27 11:22:54 -0700685 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
686 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Default;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000687 const auto layerVotedWithNoVote =
Rachel Leece6e0042023-06-27 11:22:54 -0700688 childSnapshot.frameRate.vote.type == FrameRateCompatibility::NoVote;
689 const auto layerVotedWithCategory =
690 childSnapshot.frameRate.category != FrameRateCategory::Default;
691 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
692 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Exact;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000693
694 bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
Rachel Leece6e0042023-06-27 11:22:54 -0700695 layerVotedWithCategory || layerVotedWithExactCompatibility;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000696
697 // If we don't have a valid frame rate, but the children do, we set this
698 // layer as NoVote to allow the children to control the refresh rate
699 if (childHasValidFrameRate) {
700 snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
701 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000702 }
703}
704
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000705void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
706 snapshot.isHiddenByPolicyFromRelativeParent = false;
707 snapshot.relativeLayerMetadata.mMap.clear();
708}
709
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000710void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
711 const RequestedLayerState& requested,
712 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000713 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000714 // Always update flags and visibility
715 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
716 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
717 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Nairf13c8982023-12-02 11:26:09 -0800718 RequestedLayerState::Changes::AffectsChildren | RequestedLayerState::Changes::Input |
Vishnu Naira02943f2023-06-03 13:44:46 -0700719 RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode);
720 snapshot.changes |= parentChanges;
721 if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry;
722 snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable;
723 snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000724 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000725 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
726 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair80a5a702023-02-11 01:21:51 +0000727
Vishnu Nair92990e22023-02-24 20:01:05 +0000728 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700729 snapshot.clientChanges & layer_state_t::eReparent ||
Vishnu Nair92990e22023-02-24 20:01:05 +0000730 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
731 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000732
Vishnu Naira02943f2023-06-03 13:44:46 -0700733 if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) {
734 // If root layer, use the layer stack otherwise get the parent's layer stack.
735 snapshot.outputFilter.layerStack =
736 parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
737 ? requested.layerStack
738 : parentSnapshot.outputFilter.layerStack;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000739 }
740
Chavi Weingartenb74093a2023-10-11 20:29:59 +0000741 if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) {
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000742 switch (requested.trustedOverlay) {
743 case gui::TrustedOverlay::UNSET:
744 snapshot.trustedOverlay = parentSnapshot.trustedOverlay;
745 break;
746 case gui::TrustedOverlay::DISABLED:
747 snapshot.trustedOverlay = FlagManager::getInstance().override_trusted_overlay()
748 ? requested.trustedOverlay
749 : parentSnapshot.trustedOverlay;
750 break;
751 case gui::TrustedOverlay::ENABLED:
752 snapshot.trustedOverlay = requested.trustedOverlay;
753 break;
754 }
Chavi Weingartenb74093a2023-10-11 20:29:59 +0000755 }
756
Vishnu Nair92990e22023-02-24 20:01:05 +0000757 if (snapshot.isHiddenByPolicyFromParent &&
758 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000759 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700760 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair494a2e42023-11-10 17:21:19 -0800761 RequestedLayerState::Changes::BufferSize |
Vishnu Naircfb2d252023-01-19 04:44:02 +0000762 RequestedLayerState::Changes::Input)) {
763 updateInput(snapshot, requested, parentSnapshot, path, args);
764 }
765 return;
766 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000767
Vishnu Naira02943f2023-06-03 13:44:46 -0700768 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) {
769 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
770 // marked as skip capture
771 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
772 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
773 }
774
775 if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000776 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
777 snapshot.alpha = snapshot.color.a;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700778 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Naira02943f2023-06-03 13:44:46 -0700779 }
Vishnu Nair29354ec2023-03-28 18:51:28 -0700780
Vishnu Naira02943f2023-06-03 13:44:46 -0700781 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000782 snapshot.isSecure =
783 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000784 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
785 (requested.flags & layer_state_t::eLayerSkipScreenshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700786 }
787
Vishnu Naira02943f2023-06-03 13:44:46 -0700788 if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000789 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
790 ? requested.stretchEffect
791 : parentSnapshot.stretchEffect;
Vishnu Naira02943f2023-06-03 13:44:46 -0700792 }
793
794 if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000795 if (!parentSnapshot.colorTransformIsIdentity) {
796 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
797 snapshot.colorTransformIsIdentity = false;
798 } else {
799 snapshot.colorTransform = requested.colorTransform;
800 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
801 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700802 }
803
Nergi Rahardi27613c32024-05-23 06:57:02 +0000804 if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::Metadata)) {
805 if (snapshot.changes.test(RequestedLayerState::Changes::GameMode)) {
806 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
807 ? requested.gameMode
808 : parentSnapshot.gameMode;
809 }
Garfield Tanad34a682024-05-21 15:25:35 +0000810 updateMetadata(snapshot, requested, args);
811 if (args.includeMetadata) {
812 snapshot.layerMetadata = parentSnapshot.layerMetadata;
813 snapshot.layerMetadata.merge(requested.metadata);
814 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000815 }
816
Vishnu Naira02943f2023-06-03 13:44:46 -0700817 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged ||
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700818 args.displayChanges) {
819 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
820 ? requested.fixedTransformHint
821 : parentSnapshot.fixedTransformHint;
822
823 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
824 snapshot.transformHint = snapshot.fixedTransformHint;
825 } else {
826 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
827 snapshot.transformHint = display.has_value()
828 ? std::make_optional<>(display->get().transformHint)
829 : std::nullopt;
830 }
831 }
832
Vishnu Nair42b918e2023-07-18 20:05:29 +0000833 if (forceUpdate ||
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000834 args.layerLifecycleManager.getGlobalChanges().any(
835 RequestedLayerState::Changes::Hierarchy) ||
Vishnu Nair42b918e2023-07-18 20:05:29 +0000836 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
837 RequestedLayerState::Changes::Hierarchy)) {
Rachel Leea021bb02023-11-20 21:51:09 -0800838 const bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy ==
Rachel Lee58cc90d2023-09-05 18:50:20 -0700839 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
Rachel Leea021bb02023-11-20 21:51:09 -0800840 const bool propagationAllowed = parentSnapshot.frameRateSelectionStrategy !=
Rachel Lee70f7b692023-11-22 11:24:02 -0800841 scheduler::LayerInfo::FrameRateSelectionStrategy::Self;
Rachel Leea021bb02023-11-20 21:51:09 -0800842 if ((!requested.requestedFrameRate.isValid() && propagationAllowed) ||
843 shouldOverrideChildren) {
Vishnu Nair30515cb2023-10-19 21:54:08 -0700844 snapshot.inheritedFrameRate = parentSnapshot.inheritedFrameRate;
845 } else {
846 snapshot.inheritedFrameRate = requested.requestedFrameRate;
847 }
848 // Set the framerate as the inherited frame rate and allow children to override it if
849 // needed.
850 snapshot.frameRate = snapshot.inheritedFrameRate;
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000851 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000852 }
853
Rachel Lee58cc90d2023-09-05 18:50:20 -0700854 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) {
Rachel Leea021bb02023-11-20 21:51:09 -0800855 if (parentSnapshot.frameRateSelectionStrategy ==
856 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren) {
857 snapshot.frameRateSelectionStrategy =
858 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
859 } else {
860 const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy(
861 requested.frameRateSelectionStrategy);
862 snapshot.frameRateSelectionStrategy = strategy;
863 }
Rachel Lee58cc90d2023-09-05 18:50:20 -0700864 }
865
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000866 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
867 snapshot.frameRateSelectionPriority =
868 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
869 ? parentSnapshot.frameRateSelectionPriority
870 : requested.frameRateSelectionPriority;
871 }
872
Vishnu Naira02943f2023-06-03 13:44:46 -0700873 if (forceUpdate ||
874 snapshot.clientChanges &
875 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
876 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000877 snapshot.backgroundBlurRadius = args.supportsBlur
878 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
879 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000880 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000881 for (auto& region : snapshot.blurRegions) {
882 region.alpha = region.alpha * snapshot.color.a;
883 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000884 }
885
Vishnu Naira02943f2023-06-03 13:44:46 -0700886 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
887 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700888 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700889 }
890
891 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
Vishnu Nair0808ae62023-08-07 21:42:42 -0700892 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
893 RequestedLayerState::Changes::BufferUsageFlags)) {
894 updateRoundedCorner(snapshot, requested, parentSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000895 }
896
Vishnu Naira02943f2023-06-03 13:44:46 -0700897 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
898 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
899 updateShadows(snapshot, requested, args.globalShadowSettings);
900 }
901
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000902 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700903 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000904 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000905 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000906 }
907
908 // computed snapshot properties
Alec Mouri994761f2023-08-04 21:50:55 +0000909 snapshot.forceClientComposition =
910 snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000911 snapshot.contentOpaque = snapshot.isContentOpaque();
912 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000913 snapshot.color.a == 1.f;
914 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000915 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000916 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
917 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
918 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
919 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
920 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000921}
922
923void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
924 const RequestedLayerState& requested,
Vishnu Nair0808ae62023-08-07 21:42:42 -0700925 const LayerSnapshot& parentSnapshot,
926 const Args& args) {
927 if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
928 snapshot.roundedCorner = RoundedCornerState();
929 return;
930 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000931 snapshot.roundedCorner = RoundedCornerState();
932 RoundedCornerState parentRoundedCorner;
933 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
934 parentRoundedCorner = parentSnapshot.roundedCorner;
935 ui::Transform t = snapshot.localTransform.inverse();
936 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
937 parentRoundedCorner.radius.x *= t.getScaleX();
938 parentRoundedCorner.radius.y *= t.getScaleY();
939 }
940
941 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
942 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
943 RoundedCornerState layerSettings(layerCropRect, radius);
944 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
945 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
946 if (layerSettingsValid && parentRoundedCornerValid) {
947 // If the parent and the layer have rounded corner settings, use the parent settings if
948 // the parent crop is entirely inside the layer crop. This has limitations and cause
949 // rendering artifacts. See b/200300845 for correct fix.
950 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
951 parentRoundedCorner.cropRect.top > layerCropRect.top &&
952 parentRoundedCorner.cropRect.right < layerCropRect.right &&
953 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
954 snapshot.roundedCorner = parentRoundedCorner;
955 } else {
956 snapshot.roundedCorner = layerSettings;
957 }
958 } else if (layerSettingsValid) {
959 snapshot.roundedCorner = layerSettings;
960 } else if (parentRoundedCornerValid) {
961 snapshot.roundedCorner = parentRoundedCorner;
962 }
963}
964
965void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
966 const RequestedLayerState& requested,
967 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700968 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000969 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000970 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000971 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
972 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000973 auto& t = snapshot.geomLayerTransform;
974 auto& requestedT = requested.requestedTransform;
975 std::string transformDebug =
976 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
977 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
978 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
979 std::string bufferDebug;
980 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700981 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000982 auto& destFrame = requested.destinationFrame;
983 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
984 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
985 requested.externalTexture->getWidth(),
986 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700987 primaryDisplayRotationFlags, destFrame.left,
988 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000989 unRotBuffer.getHeight(), unRotBuffer.getWidth());
990 }
991 ALOGW("Resetting transform for %s because it is invalid.%s%s",
992 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000993 snapshot.geomLayerTransform.reset();
994 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000995 if (transformWasInvalid != snapshot.invalidTransform) {
996 // If transform is invalid, the layer will be hidden.
997 mResortSnapshots = true;
998 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000999 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
1000
1001 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
1002 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
1003 snapshot.geomLayerBounds =
1004 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
1005 if (!requested.crop.isEmpty()) {
1006 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
1007 }
1008 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
1009 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001010 const Rect geomLayerBoundsWithoutTransparentRegion =
1011 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
1012 requested.transparentRegion);
1013 snapshot.transformedBoundsWithoutTransparentRegion =
1014 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001015 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
1016
1017 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +00001018 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001019 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001020 if (requested.potentialCursor) {
1021 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
1022 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001023}
1024
Vishnu Naira02943f2023-06-03 13:44:46 -07001025void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Naird9e4f462023-10-06 04:05:45 +00001026 const ShadowSettings& globalShadowSettings) {
1027 if (snapshot.shadowSettings.length > 0.f) {
1028 snapshot.shadowSettings.ambientColor = globalShadowSettings.ambientColor;
1029 snapshot.shadowSettings.spotColor = globalShadowSettings.spotColor;
1030 snapshot.shadowSettings.lightPos = globalShadowSettings.lightPos;
1031 snapshot.shadowSettings.lightRadius = globalShadowSettings.lightRadius;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001032
1033 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
1034 // it if transparent regions are present. This may not be necessary since shadows are
1035 // typically cast by layers without transparent regions.
1036 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
1037
1038 // If the casting layer is translucent, we need to fill in the shadow underneath the
1039 // layer. Otherwise the generated shadow will only be shown around the casting layer.
1040 snapshot.shadowSettings.casterIsTranslucent =
1041 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1042 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1043 snapshot.shadowSettings.spotColor *= snapshot.alpha;
1044 }
1045}
1046
1047void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1048 const RequestedLayerState& requested,
1049 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +00001050 const LayerHierarchy::TraversalPath& path,
1051 const Args& args) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001052 using InputConfig = gui::WindowInfo::InputConfig;
1053
Vishnu Naircfb2d252023-01-19 04:44:02 +00001054 if (requested.windowInfoHandle) {
1055 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1056 } else {
1057 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001058 // b/271132344 revisit this and see if we can always use the layers uid/pid
1059 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001060 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001061 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001062 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001063 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001064
Vishnu Nair93b8b792023-02-27 19:40:24 +00001065 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Linnan Li13bf76a2024-05-05 19:18:02 +08001066 snapshot.inputInfo.displayId =
1067 ui::LogicalDisplayId{static_cast<int32_t>(snapshot.outputFilter.layerStack.id)};
Vishnu Nairf13c8982023-12-02 11:26:09 -08001068 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1069 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1070 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair59a6be32024-01-29 10:26:21 -08001071 snapshot.inputInfo.canOccludePresentation = parentSnapshot.inputInfo.canOccludePresentation ||
1072 (requested.flags & layer_state_t::eCanOccludePresentation);
Vishnu Nairf13c8982023-12-02 11:26:09 -08001073 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1074 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1075 snapshot.dropInputMode = gui::DropInputMode::ALL;
1076 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1077 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1078 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1079 } else {
1080 snapshot.dropInputMode = gui::DropInputMode::NONE;
1081 }
1082
Prabir Pradhancf359192024-03-20 00:42:57 +00001083 if (snapshot.isSecure ||
Arpit Singh490ccc92024-04-30 14:26:21 +00001084 parentSnapshot.inputInfo.inputConfig.test(InputConfig::SENSITIVE_FOR_PRIVACY)) {
1085 snapshot.inputInfo.inputConfig |= InputConfig::SENSITIVE_FOR_PRIVACY;
Prabir Pradhancf359192024-03-20 00:42:57 +00001086 }
1087
Vishnu Nair29354ec2023-03-28 18:51:28 -07001088 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001089 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001090 return;
1091 }
1092
Vishnu Naircfb2d252023-01-19 04:44:02 +00001093 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1094 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1095 args.displays.get(snapshot.outputFilter.layerStack);
1096 bool noValidDisplay = !displayInfoOpt.has_value();
1097 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1098
1099 if (!requested.windowInfoHandle) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001100 snapshot.inputInfo.inputConfig = InputConfig::NO_INPUT_CHANNEL;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001101 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001102 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1103
1104 if (noValidDisplay) {
1105 // Do not let the window receive touches if it is not associated with a valid display
1106 // transform. We still allow the window to receive keys and prevent ANRs.
Prabir Pradhancf359192024-03-20 00:42:57 +00001107 snapshot.inputInfo.inputConfig |= InputConfig::NOT_TOUCHABLE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001108 }
1109
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001110 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001111
1112 handleDropInputMode(snapshot, parentSnapshot);
1113
1114 // If the window will be blacked out on a display because the display does not have the secure
1115 // flag and the layer has the secure flag set, then drop input.
1116 if (!displayInfo.isSecure && snapshot.isSecure) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001117 snapshot.inputInfo.inputConfig |= InputConfig::DROP_INPUT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001118 }
1119
Vishnu Naira02943f2023-06-03 13:44:46 -07001120 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001121 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001122 }
1123 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1124 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001125 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001126 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001127 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1128 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001129 }
1130
1131 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1132 // if it was set by WM for a known system overlay
Vishnu Nair9e0017e2024-05-22 19:02:44 +00001133 if (snapshot.trustedOverlay == gui::TrustedOverlay::ENABLED) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001134 snapshot.inputInfo.inputConfig |= InputConfig::TRUSTED_OVERLAY;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001135 }
1136
Vishnu Nair494a2e42023-11-10 17:21:19 -08001137 snapshot.inputInfo.contentSize = snapshot.croppedBufferSize.getSize();
1138
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001139 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1140 // touches from going outside the cloned area.
1141 if (path.isClone()) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001142 snapshot.inputInfo.inputConfig |= InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001143 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1144 // WM or the client.
Prabir Pradhancf359192024-03-20 00:42:57 +00001145 snapshot.inputInfo.inputConfig.clear(InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001146 }
1147}
1148
1149std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1150 return mSnapshots;
1151}
1152
Vishnu Naircfb2d252023-01-19 04:44:02 +00001153void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1154 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1155 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1156 if (!snapshot.isVisible) continue;
1157 visitor(snapshot);
1158 }
1159}
1160
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001161// Visit each visible snapshot in z-order
1162void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1163 const LayerHierarchy& root) const {
1164 root.traverseInZOrder(
1165 [this, visitor](const LayerHierarchy&,
1166 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1167 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1168 if (snapshot && snapshot->isVisible) {
1169 visitor(*snapshot);
1170 }
1171 return true;
1172 });
1173}
1174
Vishnu Naircfb2d252023-01-19 04:44:02 +00001175void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1176 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1177 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1178 if (!snapshot->isVisible) continue;
1179 visitor(snapshot);
1180 }
1181}
1182
1183void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1184 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1185 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1186 if (!snapshot.hasInputInfo()) continue;
1187 visitor(snapshot);
1188 }
1189}
1190
Vishnu Nair29354ec2023-03-28 18:51:28 -07001191void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1192 if (mNeedsTouchableRegionCrop.empty()) {
1193 return;
1194 }
1195
1196 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1197 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1198 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1199 RequestedLayerState::Changes::Input;
1200
1201 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001202 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001203 return;
1204 }
1205
1206 for (auto& path : mNeedsTouchableRegionCrop) {
1207 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1208 if (!snapshot) {
1209 continue;
1210 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001211 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1212 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001213 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1214 args.displays.get(snapshot->outputFilter.layerStack);
1215 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1216 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1217
1218 bool needsUpdate =
1219 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1220 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1221 needsUpdate =
1222 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1223 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1224 needsUpdate = needsUpdate ||
1225 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1226
1227 if (!needsUpdate) {
1228 continue;
1229 }
1230
1231 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1232 Rect inputBoundsInDisplaySpace;
1233 if (!cropLayerSnapshot) {
1234 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1235 inputBoundsInDisplaySpace =
1236 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1237 } else {
1238 FloatRect inputBounds =
1239 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1240 inputBoundsInDisplaySpace =
1241 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1242 displayInfo.transform);
1243 }
1244 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1245 } else if (cropLayerSnapshot) {
1246 FloatRect inputBounds =
1247 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1248 Rect inputBoundsInDisplaySpace =
1249 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1250 displayInfo.transform);
Chavi Weingarten1ba381e2024-01-09 21:54:11 +00001251 snapshot->inputInfo.touchableRegion =
1252 snapshot->inputInfo.touchableRegion.intersect(inputBoundsInDisplaySpace);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001253 }
1254
1255 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1256 // touches from going outside the cloned area.
1257 if (clonedRootSnapshot) {
1258 const Rect rect =
1259 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1260 snapshot->inputInfo.touchableRegion =
1261 snapshot->inputInfo.touchableRegion.intersect(rect);
1262 }
1263 }
1264}
1265
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001266} // namespace android::surfaceflinger::frontend