blob: 21f1a4a53fbe7ee5afceefcc35508966ec5f371c [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
181 const Rect roundedFrameInDisplay =
182 getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000183 info.frameLeft = roundedFrameInDisplay.left;
184 info.frameTop = roundedFrameInDisplay.top;
185 info.frameRight = roundedFrameInDisplay.right;
186 info.frameBottom = roundedFrameInDisplay.bottom;
187
188 ui::Transform inputToLayer;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000189 inputToLayer.set(inputBounds.left, inputBounds.top);
190 const ui::Transform layerToScreen = getInputTransform(snapshot);
191 const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000192
193 // InputDispatcher expects a display-to-input transform.
194 info.transform = inputToDisplay.inverse();
195
196 // The touchable region is specified in the input coordinate space. Change it to display space.
197 info.touchableRegion =
198 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
199}
200
201void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
202 if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
203 return;
204 }
205
206 // Check if we need to drop input unconditionally
207 const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
208 if (dropInputMode == gui::DropInputMode::ALL) {
209 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
210 ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
211 return;
212 }
213
214 // Check if we need to check if the window is obscured by parent
215 if (dropInputMode != gui::DropInputMode::OBSCURED) {
216 return;
217 }
218
219 // Check if the parent has set an alpha on the layer
220 if (parentSnapshot.color.a != 1.0_hf) {
221 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
222 ALOGV("Dropping input for %s as requested by policy because alpha=%f",
223 snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
224 }
225
226 // Check if the parent has cropped the buffer
227 Rect bufferSize = snapshot.croppedBufferSize;
228 if (!bufferSize.isValid()) {
229 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
230 return;
231 }
232
233 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
234 // To check if the layer has been cropped, we take the buffer bounds, apply the local
235 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
236 // match then the layer has not been cropped by its parents.
237 Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
238 bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
239
240 if (croppedByParent) {
241 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
242 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
243 snapshot.name.c_str());
244 } else {
245 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
246 // input if the window is obscured. This check should be done in surfaceflinger but the
247 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
248 // drop input events if the window is obscured.
249 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
250 }
251}
252
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000253auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
254 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
255 if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
256 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
257 : Hwc2::IComposerClient::BlendMode::COVERAGE;
258 }
259 return blendMode;
260}
261
Vishnu Nair80a5a702023-02-11 01:21:51 +0000262void updateVisibility(LayerSnapshot& snapshot, bool visible) {
263 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000264
265 // TODO(b/238781169) we are ignoring this compat for now, since we will have
266 // to remove any optimization based on visibility.
267
268 // For compatibility reasons we let layers which can receive input
269 // receive input before they have actually submitted a buffer. Because
270 // of this we use canReceiveInput instead of isVisible to check the
271 // policy-visibility, ignoring the buffer state. However for layers with
272 // hasInputInfo()==false we can use the real visibility state.
273 // We are just using these layers for occlusion detection in
274 // InputDispatcher, and obviously if they aren't visible they can't occlude
275 // anything.
Vishnu Nair80a5a702023-02-11 01:21:51 +0000276 const bool visibleForInput =
Vishnu Nair40d02282023-02-28 21:11:40 +0000277 snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000278 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naira02943f2023-06-03 13:44:46 -0700279 LLOGV(snapshot.sequence, "updating visibility %s %s", visible ? "true" : "false",
280 snapshot.getDebugString().c_str());
Vishnu Naircfb2d252023-01-19 04:44:02 +0000281}
282
283bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
284 if (requested.potentialCursor) {
285 return false;
286 }
287
288 if (snapshot.inputInfo.token != nullptr) {
289 return true;
290 }
291
292 if (snapshot.hasBufferOrSidebandStream()) {
293 return true;
294 }
295
296 return requested.windowInfoHandle &&
297 requested.windowInfoHandle->getInfo()->inputConfig.test(
298 gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
299}
300
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000301void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested,
302 const LayerSnapshotBuilder::Args& args) {
303 snapshot.metadata.clear();
304 for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) {
305 auto compatIter = args.genericLayerMetadataKeyMap.find(key);
306 if (compatIter == std::end(args.genericLayerMetadataKeyMap)) {
307 continue;
308 }
309 const uint32_t id = compatIter->second;
310 auto it = requested.metadata.mMap.find(id);
311 if (it == std::end(requested.metadata.mMap)) {
312 continue;
313 }
314
315 snapshot.metadata.emplace(key,
316 compositionengine::GenericLayerMetadataEntry{mandatory,
317 it->second});
318 }
319}
320
Vishnu Naircfb2d252023-01-19 04:44:02 +0000321void clearChanges(LayerSnapshot& snapshot) {
322 snapshot.changes.clear();
Vishnu Naira02943f2023-06-03 13:44:46 -0700323 snapshot.clientChanges = 0;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000324 snapshot.contentDirty = false;
325 snapshot.hasReadyFrame = false;
326 snapshot.sidebandStreamHasFrame = false;
327 snapshot.surfaceDamage.clear();
328}
329
Vishnu Naira02943f2023-06-03 13:44:46 -0700330// TODO (b/259407931): Remove.
331uint32_t getPrimaryDisplayRotationFlags(
332 const ui::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
333 for (auto& [_, display] : displays) {
334 if (display.isPrimary) {
335 return display.rotationFlags;
336 }
337 }
338 return 0;
339}
340
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000341} // namespace
342
343LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
344 LayerSnapshot snapshot;
Vishnu Nair92990e22023-02-24 20:01:05 +0000345 snapshot.path = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000346 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
Vishnu Naira02943f2023-06-03 13:44:46 -0700347 snapshot.clientChanges = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000348 snapshot.isHiddenByPolicyFromParent = false;
349 snapshot.isHiddenByPolicyFromRelativeParent = false;
350 snapshot.parentTransform.reset();
351 snapshot.geomLayerTransform.reset();
352 snapshot.geomInverseLayerTransform.reset();
353 snapshot.geomLayerBounds = getMaxDisplayBounds({});
354 snapshot.roundedCorner = RoundedCornerState();
355 snapshot.stretchEffect = {};
356 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
357 snapshot.outputFilter.toInternalDisplay = false;
358 snapshot.isSecure = false;
359 snapshot.color.a = 1.0_hf;
360 snapshot.colorTransformIsIdentity = true;
361 snapshot.shadowRadius = 0.f;
362 snapshot.layerMetadata.mMap.clear();
363 snapshot.relativeLayerMetadata.mMap.clear();
364 snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
365 snapshot.dropInputMode = gui::DropInputMode::NONE;
366 snapshot.isTrustedOverlay = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000367 snapshot.gameMode = gui::GameMode::Unsupported;
368 snapshot.frameRate = {};
369 snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000370 return snapshot;
371}
372
373LayerSnapshotBuilder::LayerSnapshotBuilder() : mRootSnapshot(getRootSnapshot()) {}
374
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");
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000425 if (args.parentCrop) {
426 mRootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000427 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000428 mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
429 }
430 if (args.displayChanges) {
431 mRootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
432 RequestedLayerState::Changes::Geometry;
433 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000434 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
435 mRootSnapshot.changes |=
436 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
Vishnu Naira02943f2023-06-03 13:44:46 -0700437 mRootSnapshot.clientChanges |= layer_state_t::eReparent;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000438 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700439
440 for (auto& snapshot : mSnapshots) {
441 if (snapshot->reachablilty == LayerSnapshot::Reachablilty::Reachable) {
442 snapshot->reachablilty = LayerSnapshot::Reachablilty::Unreachable;
443 }
444 }
445
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000446 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000447 if (args.root.getLayer()) {
448 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
449 // multiple children.
450 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
451 LayerHierarchy::Variant::Attached);
Vishnu Naird1f74982023-06-15 20:16:51 -0700452 updateSnapshotsInHierarchy(args, args.root, root, mRootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000453 } else {
454 for (auto& [childHierarchy, variant] : args.root.mChildren) {
455 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
456 childHierarchy->getLayer()->id,
457 variant);
Vishnu Naird1f74982023-06-15 20:16:51 -0700458 updateSnapshotsInHierarchy(args, *childHierarchy, root, mRootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000459 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000460 }
461
Vishnu Nair29354ec2023-03-28 18:51:28 -0700462 // Update touchable region crops outside the main update pass. This is because a layer could be
463 // cropped by any other layer and it requires both snapshots to be updated.
464 updateTouchableRegionCrop(args);
465
Vishnu Nairfccd6362023-02-24 23:39:53 +0000466 const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000467 clearChanges(mRootSnapshot);
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 Nair92990e22023-02-24 20:01:05 +0000583 if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) {
584 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
585 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700586 mPathToSnapshot[path] = snapshot;
587
588 mIdToSnapshots.emplace(path.id, snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000589 return snapshot;
590}
591
Vishnu Nairfccd6362023-02-24 23:39:53 +0000592bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000593 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000594 !args.layerLifecycleManager.getGlobalChanges().any(
595 RequestedLayerState::Changes::Hierarchy |
596 RequestedLayerState::Changes::Visibility)) {
597 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
598 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000599 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000600 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000601 mResortSnapshots = false;
602
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000603 size_t globalZ = 0;
604 args.root.traverseInZOrder(
605 [this, &globalZ](const LayerHierarchy&,
606 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
607 LayerSnapshot* snapshot = getSnapshot(traversalPath);
608 if (!snapshot) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700609 return true;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000610 }
611
Vishnu Naircfb2d252023-01-19 04:44:02 +0000612 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000613 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000614 size_t oldZ = snapshot->globalZ;
615 size_t newZ = globalZ++;
616 snapshot->globalZ = newZ;
617 if (oldZ == newZ) {
618 return true;
619 }
620 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000621 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
622 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000623 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
624 mSnapshots.begin() + static_cast<ssize_t>(newZ));
625 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000626 return true;
627 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000628 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000629 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000630 while (globalZ < mSnapshots.size()) {
631 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000632 /* mark unreachable snapshots as explicitly invisible */
633 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Naira02943f2023-06-03 13:44:46 -0700634 if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
Vishnu Nairfccd6362023-02-24 23:39:53 +0000635 hasUnreachableSnapshots = true;
636 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000637 globalZ++;
638 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000639 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000640}
641
642void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
643 const LayerSnapshot& parentSnapshot,
644 bool parentIsRelative, const Args& args) {
645 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000646 snapshot.isHiddenByPolicyFromRelativeParent =
647 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000648 if (args.includeMetadata) {
649 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
650 }
651 } else {
652 snapshot.isHiddenByPolicyFromRelativeParent =
653 parentSnapshot.isHiddenByPolicyFromRelativeParent;
654 if (args.includeMetadata) {
655 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
656 }
657 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700658 if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
659 snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
660 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000661}
662
Vishnu Nair42b918e2023-07-18 20:05:29 +0000663void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
664 const LayerSnapshot& childSnapshot,
665 const Args& args) {
666 if (args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000667 !args.layerLifecycleManager.getGlobalChanges().any(
668 RequestedLayerState::Changes::Hierarchy) &&
669 !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) &&
670 !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000671 return;
672 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000673
Vishnu Nair42b918e2023-07-18 20:05:29 +0000674 using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility;
675 if (snapshot.frameRate.rate.isValid() ||
676 snapshot.frameRate.type == FrameRateCompatibility::NoVote) {
677 // we already have a valid framerate.
678 return;
679 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000680
Vishnu Nair42b918e2023-07-18 20:05:29 +0000681 // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes
682 // for the same reason we are allowing touch boost for those layers. See
683 // RefreshRateSelector::rankFrameRates for details.
684 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.rate.isValid() &&
685 childSnapshot.frameRate.type == FrameRateCompatibility::Default;
686 const auto layerVotedWithNoVote =
687 childSnapshot.frameRate.type == FrameRateCompatibility::NoVote;
688 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.rate.isValid() &&
689 childSnapshot.frameRate.type == FrameRateCompatibility::Exact;
690
691 bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
692 layerVotedWithExactCompatibility;
693
694 // If we don't have a valid frame rate, but the children do, we set this
695 // layer as NoVote to allow the children to control the refresh rate
696 if (childHasValidFrameRate) {
697 snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
698 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000699 }
700}
701
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000702void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
703 snapshot.isHiddenByPolicyFromRelativeParent = false;
704 snapshot.relativeLayerMetadata.mMap.clear();
705}
706
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000707void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
708 const RequestedLayerState& requested,
709 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000710 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000711 // Always update flags and visibility
712 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
713 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
714 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000715 RequestedLayerState::Changes::AffectsChildren |
Vishnu Naira02943f2023-06-03 13:44:46 -0700716 RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode);
717 snapshot.changes |= parentChanges;
718 if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry;
719 snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable;
720 snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000721 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000722 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
723 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair80a5a702023-02-11 01:21:51 +0000724
Vishnu Nair92990e22023-02-24 20:01:05 +0000725 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700726 snapshot.clientChanges & layer_state_t::eReparent ||
Vishnu Nair92990e22023-02-24 20:01:05 +0000727 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
728 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000729
Vishnu Naira02943f2023-06-03 13:44:46 -0700730 if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) {
731 // If root layer, use the layer stack otherwise get the parent's layer stack.
732 snapshot.outputFilter.layerStack =
733 parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
734 ? requested.layerStack
735 : parentSnapshot.outputFilter.layerStack;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000736 }
737
Vishnu Nair92990e22023-02-24 20:01:05 +0000738 if (snapshot.isHiddenByPolicyFromParent &&
739 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000740 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700741 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Naircfb2d252023-01-19 04:44:02 +0000742 RequestedLayerState::Changes::Input)) {
743 updateInput(snapshot, requested, parentSnapshot, path, args);
744 }
745 return;
746 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000747
Vishnu Naira02943f2023-06-03 13:44:46 -0700748 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) {
749 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
750 // marked as skip capture
751 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
752 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
753 }
754
755 if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000756 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
757 snapshot.alpha = snapshot.color.a;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700758 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Naira02943f2023-06-03 13:44:46 -0700759 }
Vishnu Nair29354ec2023-03-28 18:51:28 -0700760
Vishnu Naira02943f2023-06-03 13:44:46 -0700761 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000762 snapshot.isSecure =
763 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000764 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
765 (requested.flags & layer_state_t::eLayerSkipScreenshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700766 }
767
768 if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) {
769 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
770 }
771
772 if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000773 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
774 ? requested.stretchEffect
775 : parentSnapshot.stretchEffect;
Vishnu Naira02943f2023-06-03 13:44:46 -0700776 }
777
778 if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000779 if (!parentSnapshot.colorTransformIsIdentity) {
780 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
781 snapshot.colorTransformIsIdentity = false;
782 } else {
783 snapshot.colorTransform = requested.colorTransform;
784 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
785 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700786 }
787
788 if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::GameMode)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000789 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
790 ? requested.gameMode
791 : parentSnapshot.gameMode;
Vishnu Naira02943f2023-06-03 13:44:46 -0700792 updateMetadata(snapshot, requested, args);
793 if (args.includeMetadata) {
794 snapshot.layerMetadata = parentSnapshot.layerMetadata;
795 snapshot.layerMetadata.merge(requested.metadata);
796 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000797 }
798
Vishnu Naira02943f2023-06-03 13:44:46 -0700799 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged ||
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700800 args.displayChanges) {
801 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
802 ? requested.fixedTransformHint
803 : parentSnapshot.fixedTransformHint;
804
805 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
806 snapshot.transformHint = snapshot.fixedTransformHint;
807 } else {
808 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
809 snapshot.transformHint = display.has_value()
810 ? std::make_optional<>(display->get().transformHint)
811 : std::nullopt;
812 }
813 }
814
Vishnu Nair42b918e2023-07-18 20:05:29 +0000815 if (forceUpdate ||
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000816 args.layerLifecycleManager.getGlobalChanges().any(
817 RequestedLayerState::Changes::Hierarchy) ||
Vishnu Nair42b918e2023-07-18 20:05:29 +0000818 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
819 RequestedLayerState::Changes::Hierarchy)) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000820 snapshot.frameRate = (requested.requestedFrameRate.rate.isValid() ||
821 (requested.requestedFrameRate.type ==
822 scheduler::LayerInfo::FrameRateCompatibility::NoVote))
823 ? requested.requestedFrameRate
824 : parentSnapshot.frameRate;
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000825 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000826 }
827
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000828 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
829 snapshot.frameRateSelectionPriority =
830 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
831 ? parentSnapshot.frameRateSelectionPriority
832 : requested.frameRateSelectionPriority;
833 }
834
Vishnu Naira02943f2023-06-03 13:44:46 -0700835 if (forceUpdate ||
836 snapshot.clientChanges &
837 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
838 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000839 snapshot.backgroundBlurRadius = args.supportsBlur
840 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
841 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000842 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000843 for (auto& region : snapshot.blurRegions) {
844 region.alpha = region.alpha * snapshot.color.a;
845 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000846 }
847
Vishnu Naira02943f2023-06-03 13:44:46 -0700848 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
849 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700850 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700851 }
852
853 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
Vishnu Nair0808ae62023-08-07 21:42:42 -0700854 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
855 RequestedLayerState::Changes::BufferUsageFlags)) {
856 updateRoundedCorner(snapshot, requested, parentSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000857 }
858
Vishnu Naira02943f2023-06-03 13:44:46 -0700859 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
860 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
861 updateShadows(snapshot, requested, args.globalShadowSettings);
862 }
863
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000864 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700865 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000866 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000867 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000868 }
869
870 // computed snapshot properties
Alec Mouri994761f2023-08-04 21:50:55 +0000871 snapshot.forceClientComposition =
872 snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000873 snapshot.contentOpaque = snapshot.isContentOpaque();
874 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000875 snapshot.color.a == 1.f;
876 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000877 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000878 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
879 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
880 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
881 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
882 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000883}
884
885void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
886 const RequestedLayerState& requested,
Vishnu Nair0808ae62023-08-07 21:42:42 -0700887 const LayerSnapshot& parentSnapshot,
888 const Args& args) {
889 if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
890 snapshot.roundedCorner = RoundedCornerState();
891 return;
892 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000893 snapshot.roundedCorner = RoundedCornerState();
894 RoundedCornerState parentRoundedCorner;
895 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
896 parentRoundedCorner = parentSnapshot.roundedCorner;
897 ui::Transform t = snapshot.localTransform.inverse();
898 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
899 parentRoundedCorner.radius.x *= t.getScaleX();
900 parentRoundedCorner.radius.y *= t.getScaleY();
901 }
902
903 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
904 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
905 RoundedCornerState layerSettings(layerCropRect, radius);
906 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
907 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
908 if (layerSettingsValid && parentRoundedCornerValid) {
909 // If the parent and the layer have rounded corner settings, use the parent settings if
910 // the parent crop is entirely inside the layer crop. This has limitations and cause
911 // rendering artifacts. See b/200300845 for correct fix.
912 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
913 parentRoundedCorner.cropRect.top > layerCropRect.top &&
914 parentRoundedCorner.cropRect.right < layerCropRect.right &&
915 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
916 snapshot.roundedCorner = parentRoundedCorner;
917 } else {
918 snapshot.roundedCorner = layerSettings;
919 }
920 } else if (layerSettingsValid) {
921 snapshot.roundedCorner = layerSettings;
922 } else if (parentRoundedCornerValid) {
923 snapshot.roundedCorner = parentRoundedCorner;
924 }
925}
926
927void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
928 const RequestedLayerState& requested,
929 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700930 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000931 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000932 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000933 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
934 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000935 auto& t = snapshot.geomLayerTransform;
936 auto& requestedT = requested.requestedTransform;
937 std::string transformDebug =
938 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
939 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
940 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
941 std::string bufferDebug;
942 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700943 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000944 auto& destFrame = requested.destinationFrame;
945 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
946 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
947 requested.externalTexture->getWidth(),
948 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700949 primaryDisplayRotationFlags, destFrame.left,
950 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000951 unRotBuffer.getHeight(), unRotBuffer.getWidth());
952 }
953 ALOGW("Resetting transform for %s because it is invalid.%s%s",
954 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000955 snapshot.geomLayerTransform.reset();
956 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000957 if (transformWasInvalid != snapshot.invalidTransform) {
958 // If transform is invalid, the layer will be hidden.
959 mResortSnapshots = true;
960 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000961 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
962
963 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
964 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
965 snapshot.geomLayerBounds =
966 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
967 if (!requested.crop.isEmpty()) {
968 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
969 }
970 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
971 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000972 const Rect geomLayerBoundsWithoutTransparentRegion =
973 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
974 requested.transparentRegion);
975 snapshot.transformedBoundsWithoutTransparentRegion =
976 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000977 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
978
979 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000980 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000981 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000982 if (requested.potentialCursor) {
983 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
984 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000985}
986
Vishnu Naira02943f2023-06-03 13:44:46 -0700987void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000988 const renderengine::ShadowSettings& globalShadowSettings) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000989 if (snapshot.shadowRadius > 0.f) {
990 snapshot.shadowSettings = globalShadowSettings;
991
992 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
993 // it if transparent regions are present. This may not be necessary since shadows are
994 // typically cast by layers without transparent regions.
995 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
996
997 // If the casting layer is translucent, we need to fill in the shadow underneath the
998 // layer. Otherwise the generated shadow will only be shown around the casting layer.
999 snapshot.shadowSettings.casterIsTranslucent =
1000 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1001 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1002 snapshot.shadowSettings.spotColor *= snapshot.alpha;
1003 }
1004}
1005
1006void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1007 const RequestedLayerState& requested,
1008 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +00001009 const LayerHierarchy::TraversalPath& path,
1010 const Args& args) {
1011 if (requested.windowInfoHandle) {
1012 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1013 } else {
1014 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001015 // b/271132344 revisit this and see if we can always use the layers uid/pid
1016 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001017 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001018 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001019 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001020 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001021
Vishnu Nair93b8b792023-02-27 19:40:24 +00001022 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001023 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001024 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001025 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001026 return;
1027 }
1028
Vishnu Naircfb2d252023-01-19 04:44:02 +00001029 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1030 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1031 args.displays.get(snapshot.outputFilter.layerStack);
1032 bool noValidDisplay = !displayInfoOpt.has_value();
1033 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1034
1035 if (!requested.windowInfoHandle) {
1036 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1037 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001038 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1039
1040 if (noValidDisplay) {
1041 // Do not let the window receive touches if it is not associated with a valid display
1042 // transform. We still allow the window to receive keys and prevent ANRs.
1043 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1044 }
1045
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001046 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair40d02282023-02-28 21:11:40 +00001047 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1048 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1049 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001050 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1051 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1052 snapshot.dropInputMode = gui::DropInputMode::ALL;
1053 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1054 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1055 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1056 } else {
1057 snapshot.dropInputMode = gui::DropInputMode::NONE;
1058 }
1059
1060 handleDropInputMode(snapshot, parentSnapshot);
1061
1062 // If the window will be blacked out on a display because the display does not have the secure
1063 // flag and the layer has the secure flag set, then drop input.
1064 if (!displayInfo.isSecure && snapshot.isSecure) {
1065 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1066 }
1067
Vishnu Naira02943f2023-06-03 13:44:46 -07001068 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001069 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001070 }
1071 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1072 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001073 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001074 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001075 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1076 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001077 }
1078
1079 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1080 // if it was set by WM for a known system overlay
1081 if (snapshot.isTrustedOverlay) {
1082 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1083 }
1084
1085 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1086 // touches from going outside the cloned area.
1087 if (path.isClone()) {
1088 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001089 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1090 // WM or the client.
1091 snapshot.inputInfo.inputConfig.clear(gui::WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001092 }
1093}
1094
1095std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1096 return mSnapshots;
1097}
1098
Vishnu Naircfb2d252023-01-19 04:44:02 +00001099void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1100 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1101 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1102 if (!snapshot.isVisible) continue;
1103 visitor(snapshot);
1104 }
1105}
1106
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001107// Visit each visible snapshot in z-order
1108void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1109 const LayerHierarchy& root) const {
1110 root.traverseInZOrder(
1111 [this, visitor](const LayerHierarchy&,
1112 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1113 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1114 if (snapshot && snapshot->isVisible) {
1115 visitor(*snapshot);
1116 }
1117 return true;
1118 });
1119}
1120
Vishnu Naircfb2d252023-01-19 04:44:02 +00001121void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1122 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1123 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1124 if (!snapshot->isVisible) continue;
1125 visitor(snapshot);
1126 }
1127}
1128
1129void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1130 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1131 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1132 if (!snapshot.hasInputInfo()) continue;
1133 visitor(snapshot);
1134 }
1135}
1136
Vishnu Nair29354ec2023-03-28 18:51:28 -07001137void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1138 if (mNeedsTouchableRegionCrop.empty()) {
1139 return;
1140 }
1141
1142 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1143 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1144 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1145 RequestedLayerState::Changes::Input;
1146
1147 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001148 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001149 return;
1150 }
1151
1152 for (auto& path : mNeedsTouchableRegionCrop) {
1153 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1154 if (!snapshot) {
1155 continue;
1156 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001157 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1158 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001159 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1160 args.displays.get(snapshot->outputFilter.layerStack);
1161 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1162 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1163
1164 bool needsUpdate =
1165 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1166 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1167 needsUpdate =
1168 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1169 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1170 needsUpdate = needsUpdate ||
1171 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1172
1173 if (!needsUpdate) {
1174 continue;
1175 }
1176
1177 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1178 Rect inputBoundsInDisplaySpace;
1179 if (!cropLayerSnapshot) {
1180 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1181 inputBoundsInDisplaySpace =
1182 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1183 } else {
1184 FloatRect inputBounds =
1185 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1186 inputBoundsInDisplaySpace =
1187 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1188 displayInfo.transform);
1189 }
1190 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1191 } else if (cropLayerSnapshot) {
1192 FloatRect inputBounds =
1193 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1194 Rect inputBoundsInDisplaySpace =
1195 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1196 displayInfo.transform);
1197 snapshot->inputInfo.touchableRegion = snapshot->inputInfo.touchableRegion.intersect(
1198 displayInfo.transform.transform(inputBoundsInDisplaySpace));
1199 }
1200
1201 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1202 // touches from going outside the cloned area.
1203 if (clonedRootSnapshot) {
1204 const Rect rect =
1205 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1206 snapshot->inputInfo.touchableRegion =
1207 snapshot->inputInfo.touchableRegion.intersect(rect);
1208 }
1209 }
1210}
1211
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001212} // namespace android::surfaceflinger::frontend