blob: 4c9fb0655b1276a7f7b56aa11b4d6805a1244f29 [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
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050025#include <ftl/small_map.h>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070026#include <gui/TraceUtils.h>
Vishnu Naira02943f2023-06-03 13:44:46 -070027#include <ui/DisplayMap.h>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050028#include <ui/FloatRect.h>
29
Vishnu Nair8fc721b2022-12-22 20:06:32 +000030#include "DisplayHardware/HWC2.h"
31#include "DisplayHardware/Hal.h"
Vishnu Nair3d8565a2023-06-30 07:23:24 +000032#include "Layer.h" // eFrameRateSelectionPriority constants
Vishnu Naircfb2d252023-01-19 04:44:02 +000033#include "LayerLog.h"
Vishnu Nairb76d99a2023-03-19 18:22:31 -070034#include "LayerSnapshotBuilder.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000035#include "TimeStats/TimeStats.h"
Vishnu Naird1f74982023-06-15 20:16:51 -070036#include "Tracing/TransactionTracing.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000037
38namespace android::surfaceflinger::frontend {
39
40using namespace ftl::flag_operators;
41
42namespace {
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050043
44FloatRect getMaxDisplayBounds(const DisplayInfos& displays) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000045 const ui::Size maxSize = [&displays] {
46 if (displays.empty()) return ui::Size{5000, 5000};
47
48 return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize,
49 [](ui::Size size, const auto& pair) -> ui::Size {
50 const auto& display = pair.second;
51 return {std::max(size.getWidth(), display.info.logicalWidth),
52 std::max(size.getHeight(), display.info.logicalHeight)};
53 });
54 }();
55
56 // Ignore display bounds for now since they will be computed later. Use a large Rect bound
57 // to ensure it's bigger than an actual display will be.
58 const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f;
59 const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f;
60
61 return {-xMax, -yMax, xMax, yMax};
62}
63
64// Applies the given transform to the region, while protecting against overflows caused by any
65// offsets. If applying the offset in the transform to any of the Rects in the region would result
66// in an overflow, they are not added to the output Region.
67Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
68 const std::string& debugWindowName) {
69 // Round the translation using the same rounding strategy used by ui::Transform.
70 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
71 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
72
73 ui::Transform transformWithoutOffset = t;
74 transformWithoutOffset.set(0.f, 0.f);
75
76 const Region transformed = transformWithoutOffset.transform(r);
77
78 // Apply the translation to each of the Rects in the region while discarding any that overflow.
79 Region ret;
80 for (const auto& rect : transformed) {
81 Rect newRect;
82 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
83 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
84 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
85 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
86 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
87 debugWindowName.c_str());
88 continue;
89 }
90 ret.orSelf(newRect);
91 }
92 return ret;
93}
94
95/*
96 * We don't want to send the layer's transform to input, but rather the
97 * parent's transform. This is because Layer's transform is
98 * information about how the buffer is placed on screen. The parent's
99 * transform makes more sense to send since it's information about how the
100 * layer is placed on screen. This transform is used by input to determine
101 * how to go from screen space back to window space.
102 */
103ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
104 if (!snapshot.hasBufferOrSidebandStream()) {
105 return snapshot.geomLayerTransform;
106 }
107 return snapshot.parentTransform;
108}
109
110/**
Vishnu Nairfed7c122023-03-18 01:54:43 +0000111 * Returns the bounds used to fill the input frame and the touchable region.
112 *
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000113 * Similar to getInputTransform, we need to update the bounds to include the transform.
114 * This is because bounds don't include the buffer transform, where the input assumes
115 * that's already included.
116 */
Vishnu Nairfed7c122023-03-18 01:54:43 +0000117std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) {
118 FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect();
119 if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() &&
120 snapshot.localTransform.getType() != ui::Transform::IDENTITY) {
121 inputBounds = snapshot.localTransform.transform(inputBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000122 }
123
Vishnu Nairfed7c122023-03-18 01:54:43 +0000124 bool inputBoundsValid = snapshot.croppedBufferSize.isValid();
125 if (!inputBoundsValid) {
126 /**
127 * Input bounds are based on the layer crop or buffer size. But if we are using
128 * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then
129 * we can use the parent bounds as the input bounds if the layer does not have buffer
130 * or a crop. We want to unify this logic but because of compat reasons we cannot always
131 * use the parent bounds. A layer without a buffer can get input. So when a window is
132 * initially added, its touchable region can fill its parent layer bounds and that can
133 * have negative consequences.
134 */
135 inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000136 }
Vishnu Nairfed7c122023-03-18 01:54:43 +0000137
138 // Clamp surface inset to the input bounds.
139 const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset);
140 const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f);
141 const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f);
142
143 // Apply the insets to the input bounds.
144 inputBounds.left += xSurfaceInset;
145 inputBounds.top += ySurfaceInset;
146 inputBounds.right -= xSurfaceInset;
147 inputBounds.bottom -= ySurfaceInset;
148 return {inputBounds, inputBoundsValid};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000149}
150
Vishnu Nairfed7c122023-03-18 01:54:43 +0000151Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds,
152 const ui::Transform& screenToDisplay) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000153 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
154 // frame and transform used for the layer, which determines the bounds and the coordinate space
155 // within which the layer will receive input.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000156
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000157 // Coordinate space definitions:
158 // - display: The display device's coordinate space. Correlates to pixels on the display.
159 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
160 // - layer: The coordinate space of this layer.
161 // - input: The coordinate space in which this layer will receive input events. This could be
162 // different than layer space if a surfaceInset is used, which changes the origin
163 // of the input space.
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000164
165 // Crop the input bounds to ensure it is within the parent's bounds.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000166 const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000167
168 const ui::Transform layerToScreen = getInputTransform(snapshot);
169 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
170
Vishnu Nairfed7c122023-03-18 01:54:43 +0000171 return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)};
172}
173
174void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay,
175 const LayerSnapshot& snapshot) {
176 auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false);
177 if (!inputBoundsValid) {
178 info.touchableRegion.clear();
179 }
180
Chavi Weingarten7f019192023-08-08 20:39:01 +0000181 info.frame = getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000182
183 ui::Transform inputToLayer;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000184 inputToLayer.set(inputBounds.left, inputBounds.top);
185 const ui::Transform layerToScreen = getInputTransform(snapshot);
186 const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000187
188 // InputDispatcher expects a display-to-input transform.
189 info.transform = inputToDisplay.inverse();
190
191 // The touchable region is specified in the input coordinate space. Change it to display space.
192 info.touchableRegion =
193 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
194}
195
196void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
197 if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
198 return;
199 }
200
201 // Check if we need to drop input unconditionally
202 const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
203 if (dropInputMode == gui::DropInputMode::ALL) {
204 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
205 ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
206 return;
207 }
208
209 // Check if we need to check if the window is obscured by parent
210 if (dropInputMode != gui::DropInputMode::OBSCURED) {
211 return;
212 }
213
214 // Check if the parent has set an alpha on the layer
215 if (parentSnapshot.color.a != 1.0_hf) {
216 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
217 ALOGV("Dropping input for %s as requested by policy because alpha=%f",
218 snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
219 }
220
221 // Check if the parent has cropped the buffer
222 Rect bufferSize = snapshot.croppedBufferSize;
223 if (!bufferSize.isValid()) {
224 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
225 return;
226 }
227
228 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
229 // To check if the layer has been cropped, we take the buffer bounds, apply the local
230 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
231 // match then the layer has not been cropped by its parents.
232 Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
233 bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
234
235 if (croppedByParent) {
236 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
237 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
238 snapshot.name.c_str());
239 } else {
240 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
241 // input if the window is obscured. This check should be done in surfaceflinger but the
242 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
243 // drop input events if the window is obscured.
244 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
245 }
246}
247
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000248auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
249 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
250 if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
251 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
252 : Hwc2::IComposerClient::BlendMode::COVERAGE;
253 }
254 return blendMode;
255}
256
Vishnu Nair80a5a702023-02-11 01:21:51 +0000257void updateVisibility(LayerSnapshot& snapshot, bool visible) {
258 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000259
260 // TODO(b/238781169) we are ignoring this compat for now, since we will have
261 // to remove any optimization based on visibility.
262
263 // For compatibility reasons we let layers which can receive input
264 // receive input before they have actually submitted a buffer. Because
265 // of this we use canReceiveInput instead of isVisible to check the
266 // policy-visibility, ignoring the buffer state. However for layers with
267 // hasInputInfo()==false we can use the real visibility state.
268 // We are just using these layers for occlusion detection in
269 // InputDispatcher, and obviously if they aren't visible they can't occlude
270 // anything.
Vishnu Nair80a5a702023-02-11 01:21:51 +0000271 const bool visibleForInput =
Vishnu Nair40d02282023-02-28 21:11:40 +0000272 snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000273 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naira02943f2023-06-03 13:44:46 -0700274 LLOGV(snapshot.sequence, "updating visibility %s %s", visible ? "true" : "false",
275 snapshot.getDebugString().c_str());
Vishnu Naircfb2d252023-01-19 04:44:02 +0000276}
277
278bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
279 if (requested.potentialCursor) {
280 return false;
281 }
282
283 if (snapshot.inputInfo.token != nullptr) {
284 return true;
285 }
286
287 if (snapshot.hasBufferOrSidebandStream()) {
288 return true;
289 }
290
291 return requested.windowInfoHandle &&
292 requested.windowInfoHandle->getInfo()->inputConfig.test(
293 gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
294}
295
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000296void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested,
297 const LayerSnapshotBuilder::Args& args) {
298 snapshot.metadata.clear();
299 for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) {
300 auto compatIter = args.genericLayerMetadataKeyMap.find(key);
301 if (compatIter == std::end(args.genericLayerMetadataKeyMap)) {
302 continue;
303 }
304 const uint32_t id = compatIter->second;
305 auto it = requested.metadata.mMap.find(id);
306 if (it == std::end(requested.metadata.mMap)) {
307 continue;
308 }
309
310 snapshot.metadata.emplace(key,
311 compositionengine::GenericLayerMetadataEntry{mandatory,
312 it->second});
313 }
314}
315
Vishnu Naircfb2d252023-01-19 04:44:02 +0000316void clearChanges(LayerSnapshot& snapshot) {
317 snapshot.changes.clear();
Vishnu Naira02943f2023-06-03 13:44:46 -0700318 snapshot.clientChanges = 0;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000319 snapshot.contentDirty = false;
320 snapshot.hasReadyFrame = false;
321 snapshot.sidebandStreamHasFrame = false;
322 snapshot.surfaceDamage.clear();
323}
324
Vishnu Naira02943f2023-06-03 13:44:46 -0700325// TODO (b/259407931): Remove.
326uint32_t getPrimaryDisplayRotationFlags(
327 const ui::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
328 for (auto& [_, display] : displays) {
329 if (display.isPrimary) {
330 return display.rotationFlags;
331 }
332 }
333 return 0;
334}
335
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000336} // namespace
337
338LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
339 LayerSnapshot snapshot;
Vishnu Nair92990e22023-02-24 20:01:05 +0000340 snapshot.path = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000341 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
Vishnu Naira02943f2023-06-03 13:44:46 -0700342 snapshot.clientChanges = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000343 snapshot.isHiddenByPolicyFromParent = false;
344 snapshot.isHiddenByPolicyFromRelativeParent = false;
345 snapshot.parentTransform.reset();
346 snapshot.geomLayerTransform.reset();
347 snapshot.geomInverseLayerTransform.reset();
348 snapshot.geomLayerBounds = getMaxDisplayBounds({});
349 snapshot.roundedCorner = RoundedCornerState();
350 snapshot.stretchEffect = {};
351 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
352 snapshot.outputFilter.toInternalDisplay = false;
353 snapshot.isSecure = false;
354 snapshot.color.a = 1.0_hf;
355 snapshot.colorTransformIsIdentity = true;
356 snapshot.shadowRadius = 0.f;
357 snapshot.layerMetadata.mMap.clear();
358 snapshot.relativeLayerMetadata.mMap.clear();
359 snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
360 snapshot.dropInputMode = gui::DropInputMode::NONE;
361 snapshot.isTrustedOverlay = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000362 snapshot.gameMode = gui::GameMode::Unsupported;
363 snapshot.frameRate = {};
364 snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000365 return snapshot;
366}
367
368LayerSnapshotBuilder::LayerSnapshotBuilder() : mRootSnapshot(getRootSnapshot()) {}
369
370LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000371 args.forceUpdate = ForceUpdateFlags::ALL;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000372 updateSnapshots(args);
373}
374
375bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700376 const bool forceUpdate = args.forceUpdate != ForceUpdateFlags::NONE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000377
Vishnu Naira02943f2023-06-03 13:44:46 -0700378 if (args.layerLifecycleManager.getGlobalChanges().get() == 0 && !forceUpdate &&
379 !args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000380 return true;
381 }
382
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000383 // There are only content changes which do not require any child layer snapshots to be updated.
384 ALOGV("%s", __func__);
385 ATRACE_NAME("FastPath");
386
Vishnu Naira02943f2023-06-03 13:44:46 -0700387 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
388 if (forceUpdate || args.displayChanges) {
389 for (auto& snapshot : mSnapshots) {
390 const RequestedLayerState* requested =
391 args.layerLifecycleManager.getLayerFromId(snapshot->path.id);
392 if (!requested) continue;
393 snapshot->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
394 primaryDisplayRotationFlags);
395 }
396 return false;
397 }
398
399 // Walk through all the updated requested layer states and update the corresponding snapshots.
400 for (const RequestedLayerState* requested : args.layerLifecycleManager.getChangedLayers()) {
401 auto range = mIdToSnapshots.equal_range(requested->id);
402 for (auto it = range.first; it != range.second; it++) {
403 it->second->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
404 primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000405 }
406 }
407
Vishnu Naira02943f2023-06-03 13:44:46 -0700408 if ((args.layerLifecycleManager.getGlobalChanges().get() &
409 ~(RequestedLayerState::Changes::Content | RequestedLayerState::Changes::Buffer).get()) !=
410 0) {
411 // We have changes that require us to walk the hierarchy and update child layers.
412 // No fast path for you.
413 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000414 }
415 return true;
416}
417
418void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
419 ATRACE_NAME("UpdateSnapshots");
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000420 if (args.parentCrop) {
421 mRootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000422 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000423 mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
424 }
425 if (args.displayChanges) {
426 mRootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
427 RequestedLayerState::Changes::Geometry;
428 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000429 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
430 mRootSnapshot.changes |=
431 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
Vishnu Naira02943f2023-06-03 13:44:46 -0700432 mRootSnapshot.clientChanges |= layer_state_t::eReparent;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000433 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700434
435 for (auto& snapshot : mSnapshots) {
436 if (snapshot->reachablilty == LayerSnapshot::Reachablilty::Reachable) {
437 snapshot->reachablilty = LayerSnapshot::Reachablilty::Unreachable;
438 }
439 }
440
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000441 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000442 if (args.root.getLayer()) {
443 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
444 // multiple children.
445 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
446 LayerHierarchy::Variant::Attached);
Vishnu Naird1f74982023-06-15 20:16:51 -0700447 updateSnapshotsInHierarchy(args, args.root, root, mRootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000448 } else {
449 for (auto& [childHierarchy, variant] : args.root.mChildren) {
450 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
451 childHierarchy->getLayer()->id,
452 variant);
Vishnu Naird1f74982023-06-15 20:16:51 -0700453 updateSnapshotsInHierarchy(args, *childHierarchy, root, mRootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000454 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000455 }
456
Vishnu Nair29354ec2023-03-28 18:51:28 -0700457 // Update touchable region crops outside the main update pass. This is because a layer could be
458 // cropped by any other layer and it requires both snapshots to be updated.
459 updateTouchableRegionCrop(args);
460
Vishnu Nairfccd6362023-02-24 23:39:53 +0000461 const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000462 clearChanges(mRootSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000463
Vishnu Nair29354ec2023-03-28 18:51:28 -0700464 // Destroy unreachable snapshots for clone layers. And destroy snapshots for non-clone
465 // layers if the layer have been destroyed.
466 // TODO(b/238781169) consider making clone layer ids stable as well
467 if (!hasUnreachableSnapshots && args.layerLifecycleManager.getDestroyedLayers().empty()) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000468 return;
469 }
470
Vishnu Nair29354ec2023-03-28 18:51:28 -0700471 std::unordered_set<uint32_t> destroyedLayerIds;
472 for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) {
473 destroyedLayerIds.insert(destroyedLayer->id);
474 }
475
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000476 auto it = mSnapshots.begin();
477 while (it < mSnapshots.end()) {
478 auto& traversalPath = it->get()->path;
Vishnu Naira02943f2023-06-03 13:44:46 -0700479 const bool unreachable =
480 it->get()->reachablilty == LayerSnapshot::Reachablilty::Unreachable;
481 const bool isClone = traversalPath.isClone();
482 const bool layerIsDestroyed =
483 destroyedLayerIds.find(traversalPath.id) != destroyedLayerIds.end();
484 const bool destroySnapshot = (unreachable && isClone) || layerIsDestroyed;
485
486 if (!destroySnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000487 it++;
488 continue;
489 }
490
Vishnu Naira02943f2023-06-03 13:44:46 -0700491 mPathToSnapshot.erase(traversalPath);
492
493 auto range = mIdToSnapshots.equal_range(traversalPath.id);
494 auto matchingSnapshot =
495 std::find_if(range.first, range.second, [&traversalPath](auto& snapshotWithId) {
496 return snapshotWithId.second->path == traversalPath;
497 });
498 mIdToSnapshots.erase(matchingSnapshot);
Vishnu Nair29354ec2023-03-28 18:51:28 -0700499 mNeedsTouchableRegionCrop.erase(traversalPath);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000500 mSnapshots.back()->globalZ = it->get()->globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000501 std::iter_swap(it, mSnapshots.end() - 1);
502 mSnapshots.erase(mSnapshots.end() - 1);
503 }
504}
505
506void LayerSnapshotBuilder::update(const Args& args) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000507 for (auto& snapshot : mSnapshots) {
508 clearChanges(*snapshot);
509 }
510
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000511 if (tryFastUpdate(args)) {
512 return;
513 }
514 updateSnapshots(args);
515}
516
Vishnu Naircfb2d252023-01-19 04:44:02 +0000517const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
518 const Args& args, const LayerHierarchy& hierarchy,
Vishnu Naird1f74982023-06-15 20:16:51 -0700519 LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot,
520 int depth) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700521 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
522 "Cycle detected in LayerSnapshotBuilder. See "
523 "builder_stack_overflow_transactions.winscope");
Vishnu Naird1f74982023-06-15 20:16:51 -0700524
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000525 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000526 LayerSnapshot* snapshot = getSnapshot(traversalPath);
527 const bool newSnapshot = snapshot == nullptr;
Vishnu Naira02943f2023-06-03 13:44:46 -0700528 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000529 if (newSnapshot) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000530 snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700531 snapshot->merge(*layer, /*forceUpdate=*/true, /*displayChanges=*/true, args.forceFullDamage,
532 primaryDisplayRotationFlags);
533 snapshot->changes |= RequestedLayerState::Changes::Created;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000534 }
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000535
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000536 if (traversalPath.isRelative()) {
537 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
538 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
539 } else {
540 if (traversalPath.isAttached()) {
541 resetRelativeState(*snapshot);
542 }
Vishnu Nair92990e22023-02-24 20:01:05 +0000543 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000544 }
545
546 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
547 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
548 childHierarchy->getLayer()->id,
549 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000550 const LayerSnapshot& childSnapshot =
Vishnu Naird1f74982023-06-15 20:16:51 -0700551 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot,
552 depth + 1);
Vishnu Nair42b918e2023-07-18 20:05:29 +0000553 updateFrameRateFromChildSnapshot(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000554 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000555
Vishnu Naircfb2d252023-01-19 04:44:02 +0000556 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000557}
558
559LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
560 if (layerId == UNASSIGNED_LAYER_ID) {
561 return nullptr;
562 }
563 LayerHierarchy::TraversalPath path{.id = layerId};
564 return getSnapshot(path);
565}
566
567LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700568 auto it = mPathToSnapshot.find(id);
569 return it == mPathToSnapshot.end() ? nullptr : it->second;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000570}
571
Vishnu Nair92990e22023-02-24 20:01:05 +0000572LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
573 const RequestedLayerState& layer,
574 const LayerSnapshot& parentSnapshot) {
575 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000576 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000577 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
Vishnu Nair92990e22023-02-24 20:01:05 +0000578 if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) {
579 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
580 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700581 mPathToSnapshot[path] = snapshot;
582
583 mIdToSnapshots.emplace(path.id, snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000584 return snapshot;
585}
586
Vishnu Nairfccd6362023-02-24 23:39:53 +0000587bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000588 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000589 !args.layerLifecycleManager.getGlobalChanges().any(
590 RequestedLayerState::Changes::Hierarchy |
591 RequestedLayerState::Changes::Visibility)) {
592 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
593 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000594 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000595 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000596 mResortSnapshots = false;
597
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000598 size_t globalZ = 0;
599 args.root.traverseInZOrder(
600 [this, &globalZ](const LayerHierarchy&,
601 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
602 LayerSnapshot* snapshot = getSnapshot(traversalPath);
603 if (!snapshot) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700604 return true;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000605 }
606
Vishnu Naircfb2d252023-01-19 04:44:02 +0000607 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000608 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000609 size_t oldZ = snapshot->globalZ;
610 size_t newZ = globalZ++;
611 snapshot->globalZ = newZ;
612 if (oldZ == newZ) {
613 return true;
614 }
615 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000616 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
617 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000618 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
619 mSnapshots.begin() + static_cast<ssize_t>(newZ));
620 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000621 return true;
622 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000623 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000624 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000625 while (globalZ < mSnapshots.size()) {
626 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000627 /* mark unreachable snapshots as explicitly invisible */
628 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Naira02943f2023-06-03 13:44:46 -0700629 if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
Vishnu Nairfccd6362023-02-24 23:39:53 +0000630 hasUnreachableSnapshots = true;
631 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000632 globalZ++;
633 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000634 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000635}
636
637void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
638 const LayerSnapshot& parentSnapshot,
639 bool parentIsRelative, const Args& args) {
640 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000641 snapshot.isHiddenByPolicyFromRelativeParent =
642 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000643 if (args.includeMetadata) {
644 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
645 }
646 } else {
647 snapshot.isHiddenByPolicyFromRelativeParent =
648 parentSnapshot.isHiddenByPolicyFromRelativeParent;
649 if (args.includeMetadata) {
650 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
651 }
652 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700653 if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
654 snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
655 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000656}
657
Vishnu Nair42b918e2023-07-18 20:05:29 +0000658void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
659 const LayerSnapshot& childSnapshot,
660 const Args& args) {
661 if (args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000662 !args.layerLifecycleManager.getGlobalChanges().any(
663 RequestedLayerState::Changes::Hierarchy) &&
664 !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) &&
665 !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000666 return;
667 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000668
Vishnu Nair42b918e2023-07-18 20:05:29 +0000669 using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility;
Rachel Leece6e0042023-06-27 11:22:54 -0700670 if (snapshot.frameRate.isValid()) {
Vishnu Nair42b918e2023-07-18 20:05:29 +0000671 // we already have a valid framerate.
672 return;
673 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000674
Vishnu Nair42b918e2023-07-18 20:05:29 +0000675 // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes
676 // for the same reason we are allowing touch boost for those layers. See
677 // RefreshRateSelector::rankFrameRates for details.
Rachel Leece6e0042023-06-27 11:22:54 -0700678 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
679 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Default;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000680 const auto layerVotedWithNoVote =
Rachel Leece6e0042023-06-27 11:22:54 -0700681 childSnapshot.frameRate.vote.type == FrameRateCompatibility::NoVote;
682 const auto layerVotedWithCategory =
683 childSnapshot.frameRate.category != FrameRateCategory::Default;
684 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
685 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Exact;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000686
687 bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
Rachel Leece6e0042023-06-27 11:22:54 -0700688 layerVotedWithCategory || layerVotedWithExactCompatibility;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000689
690 // If we don't have a valid frame rate, but the children do, we set this
691 // layer as NoVote to allow the children to control the refresh rate
692 if (childHasValidFrameRate) {
693 snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
694 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000695 }
696}
697
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000698void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
699 snapshot.isHiddenByPolicyFromRelativeParent = false;
700 snapshot.relativeLayerMetadata.mMap.clear();
701}
702
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000703void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
704 const RequestedLayerState& requested,
705 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000706 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000707 // Always update flags and visibility
708 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
709 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
710 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000711 RequestedLayerState::Changes::AffectsChildren |
Vishnu Naira02943f2023-06-03 13:44:46 -0700712 RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode);
713 snapshot.changes |= parentChanges;
714 if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry;
715 snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable;
716 snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000717 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000718 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
719 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair80a5a702023-02-11 01:21:51 +0000720
Vishnu Nair92990e22023-02-24 20:01:05 +0000721 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700722 snapshot.clientChanges & layer_state_t::eReparent ||
Vishnu Nair92990e22023-02-24 20:01:05 +0000723 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
724 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000725
Vishnu Naira02943f2023-06-03 13:44:46 -0700726 if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) {
727 // If root layer, use the layer stack otherwise get the parent's layer stack.
728 snapshot.outputFilter.layerStack =
729 parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
730 ? requested.layerStack
731 : parentSnapshot.outputFilter.layerStack;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000732 }
733
Vishnu Nair92990e22023-02-24 20:01:05 +0000734 if (snapshot.isHiddenByPolicyFromParent &&
735 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000736 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700737 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Naircfb2d252023-01-19 04:44:02 +0000738 RequestedLayerState::Changes::Input)) {
739 updateInput(snapshot, requested, parentSnapshot, path, args);
740 }
741 return;
742 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000743
Vishnu Naira02943f2023-06-03 13:44:46 -0700744 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) {
745 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
746 // marked as skip capture
747 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
748 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
749 }
750
751 if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000752 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
753 snapshot.alpha = snapshot.color.a;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700754 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Naira02943f2023-06-03 13:44:46 -0700755 }
Vishnu Nair29354ec2023-03-28 18:51:28 -0700756
Vishnu Naira02943f2023-06-03 13:44:46 -0700757 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000758 snapshot.isSecure =
759 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000760 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
761 (requested.flags & layer_state_t::eLayerSkipScreenshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700762 }
763
764 if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) {
765 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
766 }
767
768 if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000769 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
770 ? requested.stretchEffect
771 : parentSnapshot.stretchEffect;
Vishnu Naira02943f2023-06-03 13:44:46 -0700772 }
773
774 if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000775 if (!parentSnapshot.colorTransformIsIdentity) {
776 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
777 snapshot.colorTransformIsIdentity = false;
778 } else {
779 snapshot.colorTransform = requested.colorTransform;
780 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
781 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700782 }
783
784 if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::GameMode)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000785 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
786 ? requested.gameMode
787 : parentSnapshot.gameMode;
Vishnu Naira02943f2023-06-03 13:44:46 -0700788 updateMetadata(snapshot, requested, args);
789 if (args.includeMetadata) {
790 snapshot.layerMetadata = parentSnapshot.layerMetadata;
791 snapshot.layerMetadata.merge(requested.metadata);
792 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000793 }
794
Vishnu Naira02943f2023-06-03 13:44:46 -0700795 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged ||
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700796 args.displayChanges) {
797 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
798 ? requested.fixedTransformHint
799 : parentSnapshot.fixedTransformHint;
800
801 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
802 snapshot.transformHint = snapshot.fixedTransformHint;
803 } else {
804 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
805 snapshot.transformHint = display.has_value()
806 ? std::make_optional<>(display->get().transformHint)
807 : std::nullopt;
808 }
809 }
810
Vishnu Nair42b918e2023-07-18 20:05:29 +0000811 if (forceUpdate ||
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000812 args.layerLifecycleManager.getGlobalChanges().any(
813 RequestedLayerState::Changes::Hierarchy) ||
Vishnu Nair42b918e2023-07-18 20:05:29 +0000814 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
815 RequestedLayerState::Changes::Hierarchy)) {
Rachel Lee58cc90d2023-09-05 18:50:20 -0700816 bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy ==
817 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
818 snapshot.frameRate = !requested.requestedFrameRate.isValid() || shouldOverrideChildren
819 ? parentSnapshot.frameRate
820 : requested.requestedFrameRate;
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000821 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000822 }
823
Rachel Lee58cc90d2023-09-05 18:50:20 -0700824 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) {
825 const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy(
826 requested.frameRateSelectionStrategy);
827 snapshot.frameRateSelectionStrategy =
828 strategy == scheduler::LayerInfo::FrameRateSelectionStrategy::Self
829 ? parentSnapshot.frameRateSelectionStrategy
830 : strategy;
831 }
832
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000833 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
834 snapshot.frameRateSelectionPriority =
835 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
836 ? parentSnapshot.frameRateSelectionPriority
837 : requested.frameRateSelectionPriority;
838 }
839
Vishnu Naira02943f2023-06-03 13:44:46 -0700840 if (forceUpdate ||
841 snapshot.clientChanges &
842 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
843 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000844 snapshot.backgroundBlurRadius = args.supportsBlur
845 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
846 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000847 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000848 for (auto& region : snapshot.blurRegions) {
849 region.alpha = region.alpha * snapshot.color.a;
850 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000851 }
852
Vishnu Naira02943f2023-06-03 13:44:46 -0700853 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
854 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700855 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700856 }
857
858 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
Vishnu Nair0808ae62023-08-07 21:42:42 -0700859 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
860 RequestedLayerState::Changes::BufferUsageFlags)) {
861 updateRoundedCorner(snapshot, requested, parentSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000862 }
863
Vishnu Naira02943f2023-06-03 13:44:46 -0700864 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
865 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
866 updateShadows(snapshot, requested, args.globalShadowSettings);
867 }
868
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000869 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700870 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000871 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000872 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000873 }
874
875 // computed snapshot properties
Alec Mouri994761f2023-08-04 21:50:55 +0000876 snapshot.forceClientComposition =
877 snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000878 snapshot.contentOpaque = snapshot.isContentOpaque();
879 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000880 snapshot.color.a == 1.f;
881 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000882 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000883 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
884 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
885 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
886 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
887 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000888}
889
890void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
891 const RequestedLayerState& requested,
Vishnu Nair0808ae62023-08-07 21:42:42 -0700892 const LayerSnapshot& parentSnapshot,
893 const Args& args) {
894 if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
895 snapshot.roundedCorner = RoundedCornerState();
896 return;
897 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000898 snapshot.roundedCorner = RoundedCornerState();
899 RoundedCornerState parentRoundedCorner;
900 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
901 parentRoundedCorner = parentSnapshot.roundedCorner;
902 ui::Transform t = snapshot.localTransform.inverse();
903 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
904 parentRoundedCorner.radius.x *= t.getScaleX();
905 parentRoundedCorner.radius.y *= t.getScaleY();
906 }
907
908 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
909 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
910 RoundedCornerState layerSettings(layerCropRect, radius);
911 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
912 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
913 if (layerSettingsValid && parentRoundedCornerValid) {
914 // If the parent and the layer have rounded corner settings, use the parent settings if
915 // the parent crop is entirely inside the layer crop. This has limitations and cause
916 // rendering artifacts. See b/200300845 for correct fix.
917 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
918 parentRoundedCorner.cropRect.top > layerCropRect.top &&
919 parentRoundedCorner.cropRect.right < layerCropRect.right &&
920 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
921 snapshot.roundedCorner = parentRoundedCorner;
922 } else {
923 snapshot.roundedCorner = layerSettings;
924 }
925 } else if (layerSettingsValid) {
926 snapshot.roundedCorner = layerSettings;
927 } else if (parentRoundedCornerValid) {
928 snapshot.roundedCorner = parentRoundedCorner;
929 }
930}
931
932void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
933 const RequestedLayerState& requested,
934 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700935 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000936 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000937 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000938 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
939 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000940 auto& t = snapshot.geomLayerTransform;
941 auto& requestedT = requested.requestedTransform;
942 std::string transformDebug =
943 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
944 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
945 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
946 std::string bufferDebug;
947 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700948 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000949 auto& destFrame = requested.destinationFrame;
950 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
951 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
952 requested.externalTexture->getWidth(),
953 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700954 primaryDisplayRotationFlags, destFrame.left,
955 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000956 unRotBuffer.getHeight(), unRotBuffer.getWidth());
957 }
958 ALOGW("Resetting transform for %s because it is invalid.%s%s",
959 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000960 snapshot.geomLayerTransform.reset();
961 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000962 if (transformWasInvalid != snapshot.invalidTransform) {
963 // If transform is invalid, the layer will be hidden.
964 mResortSnapshots = true;
965 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000966 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
967
968 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
969 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
970 snapshot.geomLayerBounds =
971 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
972 if (!requested.crop.isEmpty()) {
973 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
974 }
975 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
976 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000977 const Rect geomLayerBoundsWithoutTransparentRegion =
978 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
979 requested.transparentRegion);
980 snapshot.transformedBoundsWithoutTransparentRegion =
981 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000982 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
983
984 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000985 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000986 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000987 if (requested.potentialCursor) {
988 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
989 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000990}
991
Vishnu Naira02943f2023-06-03 13:44:46 -0700992void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000993 const renderengine::ShadowSettings& globalShadowSettings) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000994 if (snapshot.shadowRadius > 0.f) {
995 snapshot.shadowSettings = globalShadowSettings;
996
997 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
998 // it if transparent regions are present. This may not be necessary since shadows are
999 // typically cast by layers without transparent regions.
1000 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
1001
1002 // If the casting layer is translucent, we need to fill in the shadow underneath the
1003 // layer. Otherwise the generated shadow will only be shown around the casting layer.
1004 snapshot.shadowSettings.casterIsTranslucent =
1005 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1006 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1007 snapshot.shadowSettings.spotColor *= snapshot.alpha;
1008 }
1009}
1010
1011void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1012 const RequestedLayerState& requested,
1013 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +00001014 const LayerHierarchy::TraversalPath& path,
1015 const Args& args) {
1016 if (requested.windowInfoHandle) {
1017 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1018 } else {
1019 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001020 // b/271132344 revisit this and see if we can always use the layers uid/pid
1021 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001022 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001023 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001024 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001025 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001026
Vishnu Nair93b8b792023-02-27 19:40:24 +00001027 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001028 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001029 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001030 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001031 return;
1032 }
1033
Vishnu Naircfb2d252023-01-19 04:44:02 +00001034 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1035 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1036 args.displays.get(snapshot.outputFilter.layerStack);
1037 bool noValidDisplay = !displayInfoOpt.has_value();
1038 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1039
1040 if (!requested.windowInfoHandle) {
1041 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1042 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001043 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1044
1045 if (noValidDisplay) {
1046 // Do not let the window receive touches if it is not associated with a valid display
1047 // transform. We still allow the window to receive keys and prevent ANRs.
1048 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1049 }
1050
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001051 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair40d02282023-02-28 21:11:40 +00001052 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1053 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1054 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001055 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1056 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1057 snapshot.dropInputMode = gui::DropInputMode::ALL;
1058 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1059 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1060 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1061 } else {
1062 snapshot.dropInputMode = gui::DropInputMode::NONE;
1063 }
1064
1065 handleDropInputMode(snapshot, parentSnapshot);
1066
1067 // If the window will be blacked out on a display because the display does not have the secure
1068 // flag and the layer has the secure flag set, then drop input.
1069 if (!displayInfo.isSecure && snapshot.isSecure) {
1070 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1071 }
1072
Vishnu Naira02943f2023-06-03 13:44:46 -07001073 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001074 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001075 }
1076 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1077 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001078 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001079 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001080 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1081 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001082 }
1083
1084 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1085 // if it was set by WM for a known system overlay
1086 if (snapshot.isTrustedOverlay) {
1087 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1088 }
1089
1090 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1091 // touches from going outside the cloned area.
1092 if (path.isClone()) {
1093 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001094 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1095 // WM or the client.
1096 snapshot.inputInfo.inputConfig.clear(gui::WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001097 }
1098}
1099
1100std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1101 return mSnapshots;
1102}
1103
Vishnu Naircfb2d252023-01-19 04:44:02 +00001104void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1105 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1106 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1107 if (!snapshot.isVisible) continue;
1108 visitor(snapshot);
1109 }
1110}
1111
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001112// Visit each visible snapshot in z-order
1113void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1114 const LayerHierarchy& root) const {
1115 root.traverseInZOrder(
1116 [this, visitor](const LayerHierarchy&,
1117 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1118 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1119 if (snapshot && snapshot->isVisible) {
1120 visitor(*snapshot);
1121 }
1122 return true;
1123 });
1124}
1125
Vishnu Naircfb2d252023-01-19 04:44:02 +00001126void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1127 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1128 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1129 if (!snapshot->isVisible) continue;
1130 visitor(snapshot);
1131 }
1132}
1133
1134void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1135 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1136 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1137 if (!snapshot.hasInputInfo()) continue;
1138 visitor(snapshot);
1139 }
1140}
1141
Vishnu Nair29354ec2023-03-28 18:51:28 -07001142void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1143 if (mNeedsTouchableRegionCrop.empty()) {
1144 return;
1145 }
1146
1147 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1148 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1149 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1150 RequestedLayerState::Changes::Input;
1151
1152 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001153 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001154 return;
1155 }
1156
1157 for (auto& path : mNeedsTouchableRegionCrop) {
1158 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1159 if (!snapshot) {
1160 continue;
1161 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001162 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1163 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001164 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1165 args.displays.get(snapshot->outputFilter.layerStack);
1166 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1167 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1168
1169 bool needsUpdate =
1170 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1171 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1172 needsUpdate =
1173 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1174 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1175 needsUpdate = needsUpdate ||
1176 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1177
1178 if (!needsUpdate) {
1179 continue;
1180 }
1181
1182 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1183 Rect inputBoundsInDisplaySpace;
1184 if (!cropLayerSnapshot) {
1185 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1186 inputBoundsInDisplaySpace =
1187 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1188 } else {
1189 FloatRect inputBounds =
1190 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1191 inputBoundsInDisplaySpace =
1192 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1193 displayInfo.transform);
1194 }
1195 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1196 } else if (cropLayerSnapshot) {
1197 FloatRect inputBounds =
1198 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1199 Rect inputBoundsInDisplaySpace =
1200 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1201 displayInfo.transform);
1202 snapshot->inputInfo.touchableRegion = snapshot->inputInfo.touchableRegion.intersect(
1203 displayInfo.transform.transform(inputBoundsInDisplaySpace));
1204 }
1205
1206 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1207 // touches from going outside the cloned area.
1208 if (clonedRootSnapshot) {
1209 const Rect rect =
1210 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1211 snapshot->inputInfo.touchableRegion =
1212 snapshot->inputInfo.touchableRegion.intersect(rect);
1213 }
1214 }
1215}
1216
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001217} // namespace android::surfaceflinger::frontend