blob: 3a19d0b389e98890fa6652dd88d53cbdbfb78f4d [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) {
526 if (depth > 50) {
527 TransactionTraceWriter::getInstance().invoke("layer_builder_stack_overflow_",
528 /*overwrite=*/false);
529 LOG_ALWAYS_FATAL("Cycle detected in LayerSnapshotBuilder. See "
530 "builder_stack_overflow_transactions.winscope");
531 }
532
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000533 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000534 LayerSnapshot* snapshot = getSnapshot(traversalPath);
535 const bool newSnapshot = snapshot == nullptr;
Vishnu Naira02943f2023-06-03 13:44:46 -0700536 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000537 if (newSnapshot) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000538 snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700539 snapshot->merge(*layer, /*forceUpdate=*/true, /*displayChanges=*/true, args.forceFullDamage,
540 primaryDisplayRotationFlags);
541 snapshot->changes |= RequestedLayerState::Changes::Created;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000542 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000543 scheduler::LayerInfo::FrameRate oldFrameRate = snapshot->frameRate;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000544 if (traversalPath.isRelative()) {
545 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
546 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
547 } else {
548 if (traversalPath.isAttached()) {
549 resetRelativeState(*snapshot);
550 }
Vishnu Nair92990e22023-02-24 20:01:05 +0000551 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000552 }
553
554 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
555 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
556 childHierarchy->getLayer()->id,
557 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000558 const LayerSnapshot& childSnapshot =
Vishnu Naird1f74982023-06-15 20:16:51 -0700559 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot,
560 depth + 1);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000561 updateChildState(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000562 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000563
564 if (oldFrameRate == snapshot->frameRate) {
565 snapshot->changes.clear(RequestedLayerState::Changes::FrameRate);
566 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000567 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000568}
569
570LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
571 if (layerId == UNASSIGNED_LAYER_ID) {
572 return nullptr;
573 }
574 LayerHierarchy::TraversalPath path{.id = layerId};
575 return getSnapshot(path);
576}
577
578LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700579 auto it = mPathToSnapshot.find(id);
580 return it == mPathToSnapshot.end() ? nullptr : it->second;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000581}
582
Vishnu Nair92990e22023-02-24 20:01:05 +0000583LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
584 const RequestedLayerState& layer,
585 const LayerSnapshot& parentSnapshot) {
586 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000587 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000588 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
Vishnu Nair92990e22023-02-24 20:01:05 +0000589 if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) {
590 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
591 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700592 mPathToSnapshot[path] = snapshot;
593
594 mIdToSnapshots.emplace(path.id, snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000595 return snapshot;
596}
597
Vishnu Nairfccd6362023-02-24 23:39:53 +0000598bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000599 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000600 !args.layerLifecycleManager.getGlobalChanges().any(
601 RequestedLayerState::Changes::Hierarchy |
602 RequestedLayerState::Changes::Visibility)) {
603 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
604 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000605 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000606 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000607 mResortSnapshots = false;
608
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000609 size_t globalZ = 0;
610 args.root.traverseInZOrder(
611 [this, &globalZ](const LayerHierarchy&,
612 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
613 LayerSnapshot* snapshot = getSnapshot(traversalPath);
614 if (!snapshot) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700615 return true;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000616 }
617
Vishnu Naircfb2d252023-01-19 04:44:02 +0000618 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000619 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000620 size_t oldZ = snapshot->globalZ;
621 size_t newZ = globalZ++;
622 snapshot->globalZ = newZ;
623 if (oldZ == newZ) {
624 return true;
625 }
626 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000627 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
628 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000629 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
630 mSnapshots.begin() + static_cast<ssize_t>(newZ));
631 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000632 return true;
633 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000634 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000635 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000636 while (globalZ < mSnapshots.size()) {
637 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000638 /* mark unreachable snapshots as explicitly invisible */
639 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Naira02943f2023-06-03 13:44:46 -0700640 if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
Vishnu Nairfccd6362023-02-24 23:39:53 +0000641 hasUnreachableSnapshots = true;
642 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000643 globalZ++;
644 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000645 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000646}
647
648void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
649 const LayerSnapshot& parentSnapshot,
650 bool parentIsRelative, const Args& args) {
651 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000652 snapshot.isHiddenByPolicyFromRelativeParent =
653 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000654 if (args.includeMetadata) {
655 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
656 }
657 } else {
658 snapshot.isHiddenByPolicyFromRelativeParent =
659 parentSnapshot.isHiddenByPolicyFromRelativeParent;
660 if (args.includeMetadata) {
661 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
662 }
663 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700664 if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
665 snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
666 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000667}
668
Vishnu Naircfb2d252023-01-19 04:44:02 +0000669void LayerSnapshotBuilder::updateChildState(LayerSnapshot& snapshot,
670 const LayerSnapshot& childSnapshot, const Args& args) {
671 if (snapshot.childState.hasValidFrameRate) {
672 return;
673 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000674 if (args.forceUpdate == ForceUpdateFlags::ALL ||
675 childSnapshot.changes.test(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000676 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes
677 // for the same reason we are allowing touch boost for those layers. See
678 // RefreshRateSelector::rankFrameRates for details.
679 using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility;
680 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.rate.isValid() &&
681 childSnapshot.frameRate.type == FrameRateCompatibility::Default;
682 const auto layerVotedWithNoVote =
683 childSnapshot.frameRate.type == FrameRateCompatibility::NoVote;
684 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.rate.isValid() &&
685 childSnapshot.frameRate.type == FrameRateCompatibility::Exact;
686
687 snapshot.childState.hasValidFrameRate |= layerVotedWithDefaultCompatibility ||
688 layerVotedWithNoVote || layerVotedWithExactCompatibility;
689
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 (!snapshot.frameRate.rate.isValid() &&
693 snapshot.frameRate.type != FrameRateCompatibility::NoVote &&
694 snapshot.childState.hasValidFrameRate) {
695 snapshot.frameRate =
696 scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
697 snapshot.changes |= childSnapshot.changes & RequestedLayerState::Changes::FrameRate;
698 }
699 }
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 Naira02943f2023-06-03 13:44:46 -0700815 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000816 snapshot.frameRate = (requested.requestedFrameRate.rate.isValid() ||
817 (requested.requestedFrameRate.type ==
818 scheduler::LayerInfo::FrameRateCompatibility::NoVote))
819 ? requested.requestedFrameRate
820 : parentSnapshot.frameRate;
821 }
822
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000823 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
824 snapshot.frameRateSelectionPriority =
825 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
826 ? parentSnapshot.frameRateSelectionPriority
827 : requested.frameRateSelectionPriority;
828 }
829
Vishnu Naira02943f2023-06-03 13:44:46 -0700830 if (forceUpdate ||
831 snapshot.clientChanges &
832 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
833 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000834 snapshot.backgroundBlurRadius = args.supportsBlur
835 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
836 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000837 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000838 for (auto& region : snapshot.blurRegions) {
839 region.alpha = region.alpha * snapshot.color.a;
840 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000841 }
842
Vishnu Naira02943f2023-06-03 13:44:46 -0700843 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
844 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700845 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700846 }
847
848 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
849 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000850 updateRoundedCorner(snapshot, requested, parentSnapshot);
851 }
852
Vishnu Naira02943f2023-06-03 13:44:46 -0700853 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
854 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
855 updateShadows(snapshot, requested, args.globalShadowSettings);
856 }
857
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000858 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700859 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000860 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000861 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000862 }
863
864 // computed snapshot properties
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000865 snapshot.forceClientComposition = snapshot.isHdrY410 || snapshot.shadowSettings.length > 0 ||
866 requested.blurRegions.size() > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000867 snapshot.contentOpaque = snapshot.isContentOpaque();
868 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000869 snapshot.color.a == 1.f;
870 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000871 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000872 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
873 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
874 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
875 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
876 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000877}
878
879void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
880 const RequestedLayerState& requested,
881 const LayerSnapshot& parentSnapshot) {
882 snapshot.roundedCorner = RoundedCornerState();
883 RoundedCornerState parentRoundedCorner;
884 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
885 parentRoundedCorner = parentSnapshot.roundedCorner;
886 ui::Transform t = snapshot.localTransform.inverse();
887 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
888 parentRoundedCorner.radius.x *= t.getScaleX();
889 parentRoundedCorner.radius.y *= t.getScaleY();
890 }
891
892 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
893 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
894 RoundedCornerState layerSettings(layerCropRect, radius);
895 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
896 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
897 if (layerSettingsValid && parentRoundedCornerValid) {
898 // If the parent and the layer have rounded corner settings, use the parent settings if
899 // the parent crop is entirely inside the layer crop. This has limitations and cause
900 // rendering artifacts. See b/200300845 for correct fix.
901 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
902 parentRoundedCorner.cropRect.top > layerCropRect.top &&
903 parentRoundedCorner.cropRect.right < layerCropRect.right &&
904 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
905 snapshot.roundedCorner = parentRoundedCorner;
906 } else {
907 snapshot.roundedCorner = layerSettings;
908 }
909 } else if (layerSettingsValid) {
910 snapshot.roundedCorner = layerSettings;
911 } else if (parentRoundedCornerValid) {
912 snapshot.roundedCorner = parentRoundedCorner;
913 }
914}
915
916void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
917 const RequestedLayerState& requested,
918 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700919 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000920 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000921 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000922 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
923 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000924 auto& t = snapshot.geomLayerTransform;
925 auto& requestedT = requested.requestedTransform;
926 std::string transformDebug =
927 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
928 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
929 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
930 std::string bufferDebug;
931 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700932 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000933 auto& destFrame = requested.destinationFrame;
934 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
935 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
936 requested.externalTexture->getWidth(),
937 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700938 primaryDisplayRotationFlags, destFrame.left,
939 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000940 unRotBuffer.getHeight(), unRotBuffer.getWidth());
941 }
942 ALOGW("Resetting transform for %s because it is invalid.%s%s",
943 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000944 snapshot.geomLayerTransform.reset();
945 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000946 if (transformWasInvalid != snapshot.invalidTransform) {
947 // If transform is invalid, the layer will be hidden.
948 mResortSnapshots = true;
949 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000950 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
951
952 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
953 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
954 snapshot.geomLayerBounds =
955 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
956 if (!requested.crop.isEmpty()) {
957 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
958 }
959 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
960 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000961 const Rect geomLayerBoundsWithoutTransparentRegion =
962 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
963 requested.transparentRegion);
964 snapshot.transformedBoundsWithoutTransparentRegion =
965 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000966 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
967
968 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000969 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000970 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000971 if (requested.potentialCursor) {
972 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
973 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000974}
975
Vishnu Naira02943f2023-06-03 13:44:46 -0700976void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000977 const renderengine::ShadowSettings& globalShadowSettings) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000978 if (snapshot.shadowRadius > 0.f) {
979 snapshot.shadowSettings = globalShadowSettings;
980
981 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
982 // it if transparent regions are present. This may not be necessary since shadows are
983 // typically cast by layers without transparent regions.
984 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
985
986 // If the casting layer is translucent, we need to fill in the shadow underneath the
987 // layer. Otherwise the generated shadow will only be shown around the casting layer.
988 snapshot.shadowSettings.casterIsTranslucent =
989 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
990 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
991 snapshot.shadowSettings.spotColor *= snapshot.alpha;
992 }
993}
994
995void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
996 const RequestedLayerState& requested,
997 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000998 const LayerHierarchy::TraversalPath& path,
999 const Args& args) {
1000 if (requested.windowInfoHandle) {
1001 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1002 } else {
1003 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001004 // b/271132344 revisit this and see if we can always use the layers uid/pid
1005 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001006 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001007 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001008 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001009 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001010
Vishnu Nair93b8b792023-02-27 19:40:24 +00001011 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001012 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001013 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001014 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001015 return;
1016 }
1017
Vishnu Naircfb2d252023-01-19 04:44:02 +00001018 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1019 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1020 args.displays.get(snapshot.outputFilter.layerStack);
1021 bool noValidDisplay = !displayInfoOpt.has_value();
1022 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1023
1024 if (!requested.windowInfoHandle) {
1025 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1026 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001027 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1028
1029 if (noValidDisplay) {
1030 // Do not let the window receive touches if it is not associated with a valid display
1031 // transform. We still allow the window to receive keys and prevent ANRs.
1032 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1033 }
1034
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001035 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair40d02282023-02-28 21:11:40 +00001036 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1037 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1038 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001039 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1040 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1041 snapshot.dropInputMode = gui::DropInputMode::ALL;
1042 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1043 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1044 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1045 } else {
1046 snapshot.dropInputMode = gui::DropInputMode::NONE;
1047 }
1048
1049 handleDropInputMode(snapshot, parentSnapshot);
1050
1051 // If the window will be blacked out on a display because the display does not have the secure
1052 // flag and the layer has the secure flag set, then drop input.
1053 if (!displayInfo.isSecure && snapshot.isSecure) {
1054 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1055 }
1056
Vishnu Naira02943f2023-06-03 13:44:46 -07001057 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001058 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001059 }
1060 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1061 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001062 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001063 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001064 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1065 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001066 }
1067
1068 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1069 // if it was set by WM for a known system overlay
1070 if (snapshot.isTrustedOverlay) {
1071 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1072 }
1073
1074 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1075 // touches from going outside the cloned area.
1076 if (path.isClone()) {
1077 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001078 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1079 // WM or the client.
1080 snapshot.inputInfo.inputConfig.clear(gui::WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001081 }
1082}
1083
1084std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1085 return mSnapshots;
1086}
1087
Vishnu Naircfb2d252023-01-19 04:44:02 +00001088void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1089 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1090 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1091 if (!snapshot.isVisible) continue;
1092 visitor(snapshot);
1093 }
1094}
1095
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001096// Visit each visible snapshot in z-order
1097void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1098 const LayerHierarchy& root) const {
1099 root.traverseInZOrder(
1100 [this, visitor](const LayerHierarchy&,
1101 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1102 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1103 if (snapshot && snapshot->isVisible) {
1104 visitor(*snapshot);
1105 }
1106 return true;
1107 });
1108}
1109
Vishnu Naircfb2d252023-01-19 04:44:02 +00001110void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1111 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1112 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1113 if (!snapshot->isVisible) continue;
1114 visitor(snapshot);
1115 }
1116}
1117
1118void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1119 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1120 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1121 if (!snapshot.hasInputInfo()) continue;
1122 visitor(snapshot);
1123 }
1124}
1125
Vishnu Nair29354ec2023-03-28 18:51:28 -07001126void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1127 if (mNeedsTouchableRegionCrop.empty()) {
1128 return;
1129 }
1130
1131 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1132 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1133 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1134 RequestedLayerState::Changes::Input;
1135
1136 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001137 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001138 return;
1139 }
1140
1141 for (auto& path : mNeedsTouchableRegionCrop) {
1142 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1143 if (!snapshot) {
1144 continue;
1145 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001146 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1147 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001148 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1149 args.displays.get(snapshot->outputFilter.layerStack);
1150 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1151 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1152
1153 bool needsUpdate =
1154 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1155 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1156 needsUpdate =
1157 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1158 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1159 needsUpdate = needsUpdate ||
1160 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1161
1162 if (!needsUpdate) {
1163 continue;
1164 }
1165
1166 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1167 Rect inputBoundsInDisplaySpace;
1168 if (!cropLayerSnapshot) {
1169 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1170 inputBoundsInDisplaySpace =
1171 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1172 } else {
1173 FloatRect inputBounds =
1174 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1175 inputBoundsInDisplaySpace =
1176 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1177 displayInfo.transform);
1178 }
1179 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1180 } else if (cropLayerSnapshot) {
1181 FloatRect inputBounds =
1182 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1183 Rect inputBoundsInDisplaySpace =
1184 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1185 displayInfo.transform);
1186 snapshot->inputInfo.touchableRegion = snapshot->inputInfo.touchableRegion.intersect(
1187 displayInfo.transform.transform(inputBoundsInDisplaySpace));
1188 }
1189
1190 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1191 // touches from going outside the cloned area.
1192 if (clonedRootSnapshot) {
1193 const Rect rect =
1194 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1195 snapshot->inputInfo.touchableRegion =
1196 snapshot->inputInfo.touchableRegion.intersect(rect);
1197 }
1198 }
1199}
1200
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001201} // namespace android::surfaceflinger::frontend