blob: 7df36457dfd14404d04121e1ffe5bd27ce1c3e33 [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
20#define LOG_TAG "LayerSnapshotBuilder"
21
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>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050027#include <ui/FloatRect.h>
28
Vishnu Nair8fc721b2022-12-22 20:06:32 +000029#include "DisplayHardware/HWC2.h"
30#include "DisplayHardware/Hal.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000031#include "LayerLog.h"
Vishnu Nairb76d99a2023-03-19 18:22:31 -070032#include "LayerSnapshotBuilder.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000033#include "TimeStats/TimeStats.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000034
35namespace android::surfaceflinger::frontend {
36
37using namespace ftl::flag_operators;
38
39namespace {
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050040
41FloatRect getMaxDisplayBounds(const DisplayInfos& displays) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000042 const ui::Size maxSize = [&displays] {
43 if (displays.empty()) return ui::Size{5000, 5000};
44
45 return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize,
46 [](ui::Size size, const auto& pair) -> ui::Size {
47 const auto& display = pair.second;
48 return {std::max(size.getWidth(), display.info.logicalWidth),
49 std::max(size.getHeight(), display.info.logicalHeight)};
50 });
51 }();
52
53 // Ignore display bounds for now since they will be computed later. Use a large Rect bound
54 // to ensure it's bigger than an actual display will be.
55 const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f;
56 const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f;
57
58 return {-xMax, -yMax, xMax, yMax};
59}
60
61// Applies the given transform to the region, while protecting against overflows caused by any
62// offsets. If applying the offset in the transform to any of the Rects in the region would result
63// in an overflow, they are not added to the output Region.
64Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
65 const std::string& debugWindowName) {
66 // Round the translation using the same rounding strategy used by ui::Transform.
67 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
68 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
69
70 ui::Transform transformWithoutOffset = t;
71 transformWithoutOffset.set(0.f, 0.f);
72
73 const Region transformed = transformWithoutOffset.transform(r);
74
75 // Apply the translation to each of the Rects in the region while discarding any that overflow.
76 Region ret;
77 for (const auto& rect : transformed) {
78 Rect newRect;
79 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
80 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
81 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
82 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
83 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
84 debugWindowName.c_str());
85 continue;
86 }
87 ret.orSelf(newRect);
88 }
89 return ret;
90}
91
92/*
93 * We don't want to send the layer's transform to input, but rather the
94 * parent's transform. This is because Layer's transform is
95 * information about how the buffer is placed on screen. The parent's
96 * transform makes more sense to send since it's information about how the
97 * layer is placed on screen. This transform is used by input to determine
98 * how to go from screen space back to window space.
99 */
100ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
101 if (!snapshot.hasBufferOrSidebandStream()) {
102 return snapshot.geomLayerTransform;
103 }
104 return snapshot.parentTransform;
105}
106
107/**
Vishnu Nairfed7c122023-03-18 01:54:43 +0000108 * Returns the bounds used to fill the input frame and the touchable region.
109 *
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000110 * Similar to getInputTransform, we need to update the bounds to include the transform.
111 * This is because bounds don't include the buffer transform, where the input assumes
112 * that's already included.
113 */
Vishnu Nairfed7c122023-03-18 01:54:43 +0000114std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) {
115 FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect();
116 if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() &&
117 snapshot.localTransform.getType() != ui::Transform::IDENTITY) {
118 inputBounds = snapshot.localTransform.transform(inputBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000119 }
120
Vishnu Nairfed7c122023-03-18 01:54:43 +0000121 bool inputBoundsValid = snapshot.croppedBufferSize.isValid();
122 if (!inputBoundsValid) {
123 /**
124 * Input bounds are based on the layer crop or buffer size. But if we are using
125 * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then
126 * we can use the parent bounds as the input bounds if the layer does not have buffer
127 * or a crop. We want to unify this logic but because of compat reasons we cannot always
128 * use the parent bounds. A layer without a buffer can get input. So when a window is
129 * initially added, its touchable region can fill its parent layer bounds and that can
130 * have negative consequences.
131 */
132 inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000133 }
Vishnu Nairfed7c122023-03-18 01:54:43 +0000134
135 // Clamp surface inset to the input bounds.
136 const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset);
137 const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f);
138 const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f);
139
140 // Apply the insets to the input bounds.
141 inputBounds.left += xSurfaceInset;
142 inputBounds.top += ySurfaceInset;
143 inputBounds.right -= xSurfaceInset;
144 inputBounds.bottom -= ySurfaceInset;
145 return {inputBounds, inputBoundsValid};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000146}
147
Vishnu Nairfed7c122023-03-18 01:54:43 +0000148Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds,
149 const ui::Transform& screenToDisplay) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000150 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
151 // frame and transform used for the layer, which determines the bounds and the coordinate space
152 // within which the layer will receive input.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000153
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000154 // Coordinate space definitions:
155 // - display: The display device's coordinate space. Correlates to pixels on the display.
156 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
157 // - layer: The coordinate space of this layer.
158 // - input: The coordinate space in which this layer will receive input events. This could be
159 // different than layer space if a surfaceInset is used, which changes the origin
160 // of the input space.
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000161
162 // Crop the input bounds to ensure it is within the parent's bounds.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000163 const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000164
165 const ui::Transform layerToScreen = getInputTransform(snapshot);
166 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
167
Vishnu Nairfed7c122023-03-18 01:54:43 +0000168 return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)};
169}
170
171void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay,
172 const LayerSnapshot& snapshot) {
173 auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false);
174 if (!inputBoundsValid) {
175 info.touchableRegion.clear();
176 }
177
178 const Rect roundedFrameInDisplay =
179 getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000180 info.frameLeft = roundedFrameInDisplay.left;
181 info.frameTop = roundedFrameInDisplay.top;
182 info.frameRight = roundedFrameInDisplay.right;
183 info.frameBottom = roundedFrameInDisplay.bottom;
184
185 ui::Transform inputToLayer;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000186 inputToLayer.set(inputBounds.left, inputBounds.top);
187 const ui::Transform layerToScreen = getInputTransform(snapshot);
188 const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000189
190 // InputDispatcher expects a display-to-input transform.
191 info.transform = inputToDisplay.inverse();
192
193 // The touchable region is specified in the input coordinate space. Change it to display space.
194 info.touchableRegion =
195 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
196}
197
198void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
199 if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
200 return;
201 }
202
203 // Check if we need to drop input unconditionally
204 const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
205 if (dropInputMode == gui::DropInputMode::ALL) {
206 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
207 ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
208 return;
209 }
210
211 // Check if we need to check if the window is obscured by parent
212 if (dropInputMode != gui::DropInputMode::OBSCURED) {
213 return;
214 }
215
216 // Check if the parent has set an alpha on the layer
217 if (parentSnapshot.color.a != 1.0_hf) {
218 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
219 ALOGV("Dropping input for %s as requested by policy because alpha=%f",
220 snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
221 }
222
223 // Check if the parent has cropped the buffer
224 Rect bufferSize = snapshot.croppedBufferSize;
225 if (!bufferSize.isValid()) {
226 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
227 return;
228 }
229
230 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
231 // To check if the layer has been cropped, we take the buffer bounds, apply the local
232 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
233 // match then the layer has not been cropped by its parents.
234 Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
235 bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
236
237 if (croppedByParent) {
238 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
239 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
240 snapshot.name.c_str());
241 } else {
242 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
243 // input if the window is obscured. This check should be done in surfaceflinger but the
244 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
245 // drop input events if the window is obscured.
246 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
247 }
248}
249
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000250auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
251 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
252 if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
253 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
254 : Hwc2::IComposerClient::BlendMode::COVERAGE;
255 }
256 return blendMode;
257}
258
Vishnu Naircfb2d252023-01-19 04:44:02 +0000259void updateSurfaceDamage(const RequestedLayerState& requested, bool hasReadyFrame,
260 bool forceFullDamage, Region& outSurfaceDamageRegion) {
261 if (!hasReadyFrame) {
262 outSurfaceDamageRegion.clear();
263 return;
264 }
265 if (forceFullDamage) {
266 outSurfaceDamageRegion = Region::INVALID_REGION;
267 } else {
268 outSurfaceDamageRegion = requested.surfaceDamageRegion;
269 }
270}
271
Vishnu Nair80a5a702023-02-11 01:21:51 +0000272void updateVisibility(LayerSnapshot& snapshot, bool visible) {
273 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000274
275 // TODO(b/238781169) we are ignoring this compat for now, since we will have
276 // to remove any optimization based on visibility.
277
278 // For compatibility reasons we let layers which can receive input
279 // receive input before they have actually submitted a buffer. Because
280 // of this we use canReceiveInput instead of isVisible to check the
281 // policy-visibility, ignoring the buffer state. However for layers with
282 // hasInputInfo()==false we can use the real visibility state.
283 // We are just using these layers for occlusion detection in
284 // InputDispatcher, and obviously if they aren't visible they can't occlude
285 // anything.
Vishnu Nair80a5a702023-02-11 01:21:51 +0000286 const bool visibleForInput =
Vishnu Nair40d02282023-02-28 21:11:40 +0000287 snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000288 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000289}
290
291bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
292 if (requested.potentialCursor) {
293 return false;
294 }
295
296 if (snapshot.inputInfo.token != nullptr) {
297 return true;
298 }
299
300 if (snapshot.hasBufferOrSidebandStream()) {
301 return true;
302 }
303
304 return requested.windowInfoHandle &&
305 requested.windowInfoHandle->getInfo()->inputConfig.test(
306 gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
307}
308
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000309void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested,
310 const LayerSnapshotBuilder::Args& args) {
311 snapshot.metadata.clear();
312 for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) {
313 auto compatIter = args.genericLayerMetadataKeyMap.find(key);
314 if (compatIter == std::end(args.genericLayerMetadataKeyMap)) {
315 continue;
316 }
317 const uint32_t id = compatIter->second;
318 auto it = requested.metadata.mMap.find(id);
319 if (it == std::end(requested.metadata.mMap)) {
320 continue;
321 }
322
323 snapshot.metadata.emplace(key,
324 compositionengine::GenericLayerMetadataEntry{mandatory,
325 it->second});
326 }
327}
328
Vishnu Naircfb2d252023-01-19 04:44:02 +0000329void clearChanges(LayerSnapshot& snapshot) {
330 snapshot.changes.clear();
331 snapshot.contentDirty = false;
332 snapshot.hasReadyFrame = false;
333 snapshot.sidebandStreamHasFrame = false;
334 snapshot.surfaceDamage.clear();
335}
336
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000337} // namespace
338
339LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
340 LayerSnapshot snapshot;
Vishnu Nair92990e22023-02-24 20:01:05 +0000341 snapshot.path = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000342 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
343 snapshot.isHiddenByPolicyFromParent = false;
344 snapshot.isHiddenByPolicyFromRelativeParent = false;
345 snapshot.parentTransform.reset();
346 snapshot.geomLayerTransform.reset();
347 snapshot.geomInverseLayerTransform.reset();
348 snapshot.geomLayerBounds = getMaxDisplayBounds({});
349 snapshot.roundedCorner = RoundedCornerState();
350 snapshot.stretchEffect = {};
351 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
352 snapshot.outputFilter.toInternalDisplay = false;
353 snapshot.isSecure = false;
354 snapshot.color.a = 1.0_hf;
355 snapshot.colorTransformIsIdentity = true;
356 snapshot.shadowRadius = 0.f;
357 snapshot.layerMetadata.mMap.clear();
358 snapshot.relativeLayerMetadata.mMap.clear();
359 snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
360 snapshot.dropInputMode = gui::DropInputMode::NONE;
361 snapshot.isTrustedOverlay = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000362 snapshot.gameMode = gui::GameMode::Unsupported;
363 snapshot.frameRate = {};
364 snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000365 return snapshot;
366}
367
368LayerSnapshotBuilder::LayerSnapshotBuilder() : mRootSnapshot(getRootSnapshot()) {}
369
370LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000371 args.forceUpdate = ForceUpdateFlags::ALL;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000372 updateSnapshots(args);
373}
374
375bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000376 if (args.forceUpdate != ForceUpdateFlags::NONE || args.displayChanges) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000377 // force update requested, or we have display changes, so skip the fast path
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000378 return false;
379 }
380
381 if (args.layerLifecycleManager.getGlobalChanges().get() == 0) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000382 return true;
383 }
384
385 if (args.layerLifecycleManager.getGlobalChanges() != RequestedLayerState::Changes::Content) {
386 // We have changes that require us to walk the hierarchy and update child layers.
387 // No fast path for you.
388 return false;
389 }
390
391 // There are only content changes which do not require any child layer snapshots to be updated.
392 ALOGV("%s", __func__);
393 ATRACE_NAME("FastPath");
394
395 // Collect layers with changes
396 ftl::SmallMap<uint32_t, RequestedLayerState*, 10> layersWithChanges;
397 for (auto& layer : args.layerLifecycleManager.getLayers()) {
398 if (layer->changes.test(RequestedLayerState::Changes::Content)) {
399 layersWithChanges.emplace_or_replace(layer->id, layer.get());
400 }
401 }
402
403 // Walk through the snapshots, clearing previous change flags and updating the snapshots
404 // if needed.
405 for (auto& snapshot : mSnapshots) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000406 auto it = layersWithChanges.find(snapshot->path.id);
407 if (it != layersWithChanges.end()) {
408 ALOGV("%s fast path snapshot changes = %s", __func__,
409 mRootSnapshot.changes.string().c_str());
410 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair92990e22023-02-24 20:01:05 +0000411 updateSnapshot(*snapshot, args, *it->second, mRootSnapshot, root);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000412 }
413 }
414 return true;
415}
416
417void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
418 ATRACE_NAME("UpdateSnapshots");
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000419 if (args.parentCrop) {
420 mRootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000421 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000422 mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
423 }
424 if (args.displayChanges) {
425 mRootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
426 RequestedLayerState::Changes::Geometry;
427 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000428 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
429 mRootSnapshot.changes |=
430 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
431 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000432 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000433 if (args.root.getLayer()) {
434 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
435 // multiple children.
436 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
437 LayerHierarchy::Variant::Attached);
438 updateSnapshotsInHierarchy(args, args.root, root, mRootSnapshot);
439 } else {
440 for (auto& [childHierarchy, variant] : args.root.mChildren) {
441 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
442 childHierarchy->getLayer()->id,
443 variant);
444 updateSnapshotsInHierarchy(args, *childHierarchy, root, mRootSnapshot);
445 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000446 }
447
Vishnu Nair29354ec2023-03-28 18:51:28 -0700448 // Update touchable region crops outside the main update pass. This is because a layer could be
449 // cropped by any other layer and it requires both snapshots to be updated.
450 updateTouchableRegionCrop(args);
451
Vishnu Nairfccd6362023-02-24 23:39:53 +0000452 const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000453 clearChanges(mRootSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000454
Vishnu Nair29354ec2023-03-28 18:51:28 -0700455 // Destroy unreachable snapshots for clone layers. And destroy snapshots for non-clone
456 // layers if the layer have been destroyed.
457 // TODO(b/238781169) consider making clone layer ids stable as well
458 if (!hasUnreachableSnapshots && args.layerLifecycleManager.getDestroyedLayers().empty()) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000459 return;
460 }
461
Vishnu Nair29354ec2023-03-28 18:51:28 -0700462 std::unordered_set<uint32_t> destroyedLayerIds;
463 for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) {
464 destroyedLayerIds.insert(destroyedLayer->id);
465 }
466
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000467 auto it = mSnapshots.begin();
468 while (it < mSnapshots.end()) {
469 auto& traversalPath = it->get()->path;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700470 if (!it->get()->unreachable &&
471 destroyedLayerIds.find(traversalPath.id) == destroyedLayerIds.end()) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000472 it++;
473 continue;
474 }
475
476 mIdToSnapshot.erase(traversalPath);
Vishnu Nair29354ec2023-03-28 18:51:28 -0700477 mNeedsTouchableRegionCrop.erase(traversalPath);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000478 mSnapshots.back()->globalZ = it->get()->globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000479 std::iter_swap(it, mSnapshots.end() - 1);
480 mSnapshots.erase(mSnapshots.end() - 1);
481 }
482}
483
484void LayerSnapshotBuilder::update(const Args& args) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000485 for (auto& snapshot : mSnapshots) {
486 clearChanges(*snapshot);
487 }
488
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000489 if (tryFastUpdate(args)) {
490 return;
491 }
492 updateSnapshots(args);
493}
494
Vishnu Naircfb2d252023-01-19 04:44:02 +0000495const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
496 const Args& args, const LayerHierarchy& hierarchy,
497 LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000498 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000499 LayerSnapshot* snapshot = getSnapshot(traversalPath);
500 const bool newSnapshot = snapshot == nullptr;
501 if (newSnapshot) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000502 snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000503 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000504 scheduler::LayerInfo::FrameRate oldFrameRate = snapshot->frameRate;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000505 if (traversalPath.isRelative()) {
506 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
507 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
508 } else {
509 if (traversalPath.isAttached()) {
510 resetRelativeState(*snapshot);
511 }
Vishnu Nair92990e22023-02-24 20:01:05 +0000512 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000513 }
514
515 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
516 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
517 childHierarchy->getLayer()->id,
518 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000519 const LayerSnapshot& childSnapshot =
520 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot);
521 updateChildState(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000522 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000523
524 if (oldFrameRate == snapshot->frameRate) {
525 snapshot->changes.clear(RequestedLayerState::Changes::FrameRate);
526 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000527 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000528}
529
530LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
531 if (layerId == UNASSIGNED_LAYER_ID) {
532 return nullptr;
533 }
534 LayerHierarchy::TraversalPath path{.id = layerId};
535 return getSnapshot(path);
536}
537
538LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
539 auto it = mIdToSnapshot.find(id);
540 return it == mIdToSnapshot.end() ? nullptr : it->second;
541}
542
Vishnu Nair92990e22023-02-24 20:01:05 +0000543LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
544 const RequestedLayerState& layer,
545 const LayerSnapshot& parentSnapshot) {
546 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000547 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000548 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
Vishnu Nair92990e22023-02-24 20:01:05 +0000549 if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) {
550 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
551 }
552 mIdToSnapshot[path] = snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000553 return snapshot;
554}
555
Vishnu Nairfccd6362023-02-24 23:39:53 +0000556bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000557 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000558 !args.layerLifecycleManager.getGlobalChanges().any(
559 RequestedLayerState::Changes::Hierarchy |
560 RequestedLayerState::Changes::Visibility)) {
561 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
562 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000563 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000564 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000565 mResortSnapshots = false;
566
Vishnu Nairfccd6362023-02-24 23:39:53 +0000567 for (auto& snapshot : mSnapshots) {
Vishnu Nair29354ec2023-03-28 18:51:28 -0700568 snapshot->unreachable = snapshot->path.isClone();
Vishnu Nairfccd6362023-02-24 23:39:53 +0000569 }
570
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000571 size_t globalZ = 0;
572 args.root.traverseInZOrder(
573 [this, &globalZ](const LayerHierarchy&,
574 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
575 LayerSnapshot* snapshot = getSnapshot(traversalPath);
576 if (!snapshot) {
577 return false;
578 }
579
Vishnu Nairfccd6362023-02-24 23:39:53 +0000580 snapshot->unreachable = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000581 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000582 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000583 size_t oldZ = snapshot->globalZ;
584 size_t newZ = globalZ++;
585 snapshot->globalZ = newZ;
586 if (oldZ == newZ) {
587 return true;
588 }
589 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000590 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
591 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000592 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
593 mSnapshots.begin() + static_cast<ssize_t>(newZ));
594 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000595 return true;
596 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000597 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000598 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000599 while (globalZ < mSnapshots.size()) {
600 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000601 /* mark unreachable snapshots as explicitly invisible */
602 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Nairfccd6362023-02-24 23:39:53 +0000603 if (mSnapshots[globalZ]->unreachable) {
604 hasUnreachableSnapshots = true;
605 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000606 globalZ++;
607 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000608 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000609}
610
611void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
612 const LayerSnapshot& parentSnapshot,
613 bool parentIsRelative, const Args& args) {
614 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000615 snapshot.isHiddenByPolicyFromRelativeParent =
616 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000617 if (args.includeMetadata) {
618 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
619 }
620 } else {
621 snapshot.isHiddenByPolicyFromRelativeParent =
622 parentSnapshot.isHiddenByPolicyFromRelativeParent;
623 if (args.includeMetadata) {
624 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
625 }
626 }
627 snapshot.isVisible = snapshot.getIsVisible();
628}
629
Vishnu Naircfb2d252023-01-19 04:44:02 +0000630void LayerSnapshotBuilder::updateChildState(LayerSnapshot& snapshot,
631 const LayerSnapshot& childSnapshot, const Args& args) {
632 if (snapshot.childState.hasValidFrameRate) {
633 return;
634 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000635 if (args.forceUpdate == ForceUpdateFlags::ALL ||
636 childSnapshot.changes.test(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000637 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes
638 // for the same reason we are allowing touch boost for those layers. See
639 // RefreshRateSelector::rankFrameRates for details.
640 using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility;
641 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.rate.isValid() &&
642 childSnapshot.frameRate.type == FrameRateCompatibility::Default;
643 const auto layerVotedWithNoVote =
644 childSnapshot.frameRate.type == FrameRateCompatibility::NoVote;
645 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.rate.isValid() &&
646 childSnapshot.frameRate.type == FrameRateCompatibility::Exact;
647
648 snapshot.childState.hasValidFrameRate |= layerVotedWithDefaultCompatibility ||
649 layerVotedWithNoVote || layerVotedWithExactCompatibility;
650
651 // If we don't have a valid frame rate, but the children do, we set this
652 // layer as NoVote to allow the children to control the refresh rate
653 if (!snapshot.frameRate.rate.isValid() &&
654 snapshot.frameRate.type != FrameRateCompatibility::NoVote &&
655 snapshot.childState.hasValidFrameRate) {
656 snapshot.frameRate =
657 scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
658 snapshot.changes |= childSnapshot.changes & RequestedLayerState::Changes::FrameRate;
659 }
660 }
661}
662
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000663void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
664 snapshot.isHiddenByPolicyFromRelativeParent = false;
665 snapshot.relativeLayerMetadata.mMap.clear();
666}
667
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500668uint32_t getPrimaryDisplayRotationFlags(const DisplayInfos& displays) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700669 for (auto& [_, display] : displays) {
670 if (display.isPrimary) {
671 return display.rotationFlags;
672 }
673 }
674 return 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000675}
676
677void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
678 const RequestedLayerState& requested,
679 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000680 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000681 // Always update flags and visibility
682 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
683 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
684 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000685 RequestedLayerState::Changes::AffectsChildren |
686 RequestedLayerState::Changes::FrameRate);
Vishnu Nair92990e22023-02-24 20:01:05 +0000687 snapshot.changes |= parentChanges | requested.changes;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000688 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000689 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
690 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000691 snapshot.contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000692 // TODO(b/238781169) scope down the changes to only buffer updates.
Vishnu Naird47bcee2023-02-24 18:08:51 +0000693 snapshot.hasReadyFrame = requested.hasReadyFrame();
694 snapshot.sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000695 updateSurfaceDamage(requested, snapshot.hasReadyFrame, args.forceFullDamage,
696 snapshot.surfaceDamage);
Vishnu Nair92990e22023-02-24 20:01:05 +0000697 snapshot.outputFilter.layerStack = parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
698 ? requested.layerStack
699 : parentSnapshot.outputFilter.layerStack;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000700
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700701 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nair92990e22023-02-24 20:01:05 +0000702 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
703 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
704 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000705
Vishnu Naircfb2d252023-01-19 04:44:02 +0000706 // always update the buffer regardless of visibility
Vishnu Nair80a5a702023-02-11 01:21:51 +0000707 if (forceUpdate || requested.what & layer_state_t::BUFFER_CHANGES || args.displayChanges) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000708 snapshot.acquireFence =
709 (requested.externalTexture &&
710 requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged))
711 ? requested.bufferData->acquireFence
712 : Fence::NO_FENCE;
713 snapshot.buffer =
714 requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr;
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700715 snapshot.bufferSize = requested.getBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000716 snapshot.geomBufferSize = snapshot.bufferSize;
717 snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize);
718 snapshot.dataspace = requested.dataspace;
719 snapshot.externalTexture = requested.externalTexture;
720 snapshot.frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0;
721 snapshot.geomBufferTransform = requested.bufferTransform;
722 snapshot.geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse;
723 snapshot.geomContentCrop = requested.getBufferCrop();
724 snapshot.geomUsesSourceCrop = snapshot.hasBufferOrSidebandStream();
725 snapshot.hasProtectedContent = requested.externalTexture &&
726 requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
727 snapshot.isHdrY410 = requested.dataspace == ui::Dataspace::BT2020_ITU_PQ &&
728 requested.api == NATIVE_WINDOW_API_MEDIA &&
729 requested.bufferData->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102;
730 snapshot.sidebandStream = requested.sidebandStream;
731 snapshot.transparentRegionHint = requested.transparentRegion;
732 snapshot.color.rgb = requested.getColor().rgb;
Sally Qi963049b2023-03-23 14:06:21 -0700733 snapshot.currentHdrSdrRatio = requested.currentHdrSdrRatio;
734 snapshot.desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000735 }
736
Vishnu Nair92990e22023-02-24 20:01:05 +0000737 if (snapshot.isHiddenByPolicyFromParent &&
738 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000739 if (forceUpdate ||
740 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
741 RequestedLayerState::Changes::Geometry |
742 RequestedLayerState::Changes::Input)) {
743 updateInput(snapshot, requested, parentSnapshot, path, args);
744 }
745 return;
746 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000747
748 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren)) {
749 // If root layer, use the layer stack otherwise get the parent's layer stack.
750 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
751 snapshot.alpha = snapshot.color.a;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700752 snapshot.inputInfo.alpha = snapshot.color.a;
753
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000754 snapshot.isSecure =
755 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
756 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000757 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
758 (requested.flags & layer_state_t::eLayerSkipScreenshot);
759 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
760 ? requested.stretchEffect
761 : parentSnapshot.stretchEffect;
762 if (!parentSnapshot.colorTransformIsIdentity) {
763 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
764 snapshot.colorTransformIsIdentity = false;
765 } else {
766 snapshot.colorTransform = requested.colorTransform;
767 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
768 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000769 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
770 ? requested.gameMode
771 : parentSnapshot.gameMode;
Vishnu Naira9c43762023-01-27 19:10:25 +0000772 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
773 // marked as skip capture
774 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
775 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000776 }
777
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700778 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren) ||
779 args.displayChanges) {
780 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
781 ? requested.fixedTransformHint
782 : parentSnapshot.fixedTransformHint;
783
784 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
785 snapshot.transformHint = snapshot.fixedTransformHint;
786 } else {
787 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
788 snapshot.transformHint = display.has_value()
789 ? std::make_optional<>(display->get().transformHint)
790 : std::nullopt;
791 }
792 }
793
Vishnu Naird47bcee2023-02-24 18:08:51 +0000794 if (forceUpdate ||
795 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
796 RequestedLayerState::Changes::Hierarchy)) {
797 snapshot.frameRate = (requested.requestedFrameRate.rate.isValid() ||
798 (requested.requestedFrameRate.type ==
799 scheduler::LayerInfo::FrameRateCompatibility::NoVote))
800 ? requested.requestedFrameRate
801 : parentSnapshot.frameRate;
802 }
803
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000804 if (forceUpdate || requested.what & layer_state_t::eMetadataChanged) {
805 updateMetadata(snapshot, requested, args);
806 }
807
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000808 if (forceUpdate || requested.changes.get() != 0) {
809 snapshot.compositionType = requested.getCompositionType();
810 snapshot.dimmingEnabled = requested.dimmingEnabled;
811 snapshot.layerOpaqueFlagSet =
812 (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
Alec Mourif4af03e2023-02-11 00:25:24 +0000813 snapshot.cachingHint = requested.cachingHint;
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000814 snapshot.frameRateSelectionPriority = requested.frameRateSelectionPriority;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000815 }
816
Vishnu Nair444f3952023-04-11 13:01:02 -0700817 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Content) ||
818 snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000819 snapshot.color.rgb = requested.getColor().rgb;
820 snapshot.isColorspaceAgnostic = requested.colorSpaceAgnostic;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000821 snapshot.backgroundBlurRadius = args.supportsBlur
822 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
823 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000824 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000825 for (auto& region : snapshot.blurRegions) {
826 region.alpha = region.alpha * snapshot.color.a;
827 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000828 snapshot.hdrMetadata = requested.hdrMetadata;
829 }
830
831 if (forceUpdate ||
832 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
833 RequestedLayerState::Changes::Geometry)) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700834 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000835 updateRoundedCorner(snapshot, requested, parentSnapshot);
836 }
837
838 if (forceUpdate ||
839 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
840 RequestedLayerState::Changes::Geometry |
841 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000842 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000843 }
844
845 // computed snapshot properties
846 updateShadows(snapshot, requested, args.globalShadowSettings);
847 if (args.includeMetadata) {
848 snapshot.layerMetadata = parentSnapshot.layerMetadata;
849 snapshot.layerMetadata.merge(requested.metadata);
850 }
851 snapshot.forceClientComposition = snapshot.isHdrY410 || snapshot.shadowSettings.length > 0 ||
852 requested.blurRegions.size() > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000853 snapshot.contentOpaque = snapshot.isContentOpaque();
854 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000855 snapshot.color.a == 1.f;
856 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000857 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000858 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
859 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
860 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
861 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
862 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000863}
864
865void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
866 const RequestedLayerState& requested,
867 const LayerSnapshot& parentSnapshot) {
868 snapshot.roundedCorner = RoundedCornerState();
869 RoundedCornerState parentRoundedCorner;
870 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
871 parentRoundedCorner = parentSnapshot.roundedCorner;
872 ui::Transform t = snapshot.localTransform.inverse();
873 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
874 parentRoundedCorner.radius.x *= t.getScaleX();
875 parentRoundedCorner.radius.y *= t.getScaleY();
876 }
877
878 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
879 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
880 RoundedCornerState layerSettings(layerCropRect, radius);
881 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
882 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
883 if (layerSettingsValid && parentRoundedCornerValid) {
884 // If the parent and the layer have rounded corner settings, use the parent settings if
885 // the parent crop is entirely inside the layer crop. This has limitations and cause
886 // rendering artifacts. See b/200300845 for correct fix.
887 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
888 parentRoundedCorner.cropRect.top > layerCropRect.top &&
889 parentRoundedCorner.cropRect.right < layerCropRect.right &&
890 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
891 snapshot.roundedCorner = parentRoundedCorner;
892 } else {
893 snapshot.roundedCorner = layerSettings;
894 }
895 } else if (layerSettingsValid) {
896 snapshot.roundedCorner = layerSettings;
897 } else if (parentRoundedCornerValid) {
898 snapshot.roundedCorner = parentRoundedCorner;
899 }
900}
901
902void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
903 const RequestedLayerState& requested,
904 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700905 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000906 snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize);
907 snapshot.geomCrop = requested.crop;
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700908 snapshot.localTransform = requested.getTransform(primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000909 snapshot.localTransformInverse = snapshot.localTransform.inverse();
910 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000911 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000912 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
913 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000914 auto& t = snapshot.geomLayerTransform;
915 auto& requestedT = requested.requestedTransform;
916 std::string transformDebug =
917 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
918 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
919 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
920 std::string bufferDebug;
921 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700922 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000923 auto& destFrame = requested.destinationFrame;
924 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
925 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
926 requested.externalTexture->getWidth(),
927 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700928 primaryDisplayRotationFlags, destFrame.left,
929 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000930 unRotBuffer.getHeight(), unRotBuffer.getWidth());
931 }
932 ALOGW("Resetting transform for %s because it is invalid.%s%s",
933 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000934 snapshot.geomLayerTransform.reset();
935 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000936 if (transformWasInvalid != snapshot.invalidTransform) {
937 // If transform is invalid, the layer will be hidden.
938 mResortSnapshots = true;
939 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000940 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
941
942 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
943 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
944 snapshot.geomLayerBounds =
945 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
946 if (!requested.crop.isEmpty()) {
947 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
948 }
949 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
950 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000951 const Rect geomLayerBoundsWithoutTransparentRegion =
952 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
953 requested.transparentRegion);
954 snapshot.transformedBoundsWithoutTransparentRegion =
955 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000956 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
957
958 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000959 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000960 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000961 if (requested.potentialCursor) {
962 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
963 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000964}
965
966void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot,
967 const RequestedLayerState& requested,
968 const renderengine::ShadowSettings& globalShadowSettings) {
969 snapshot.shadowRadius = requested.shadowRadius;
970 snapshot.shadowSettings.length = requested.shadowRadius;
971 if (snapshot.shadowRadius > 0.f) {
972 snapshot.shadowSettings = globalShadowSettings;
973
974 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
975 // it if transparent regions are present. This may not be necessary since shadows are
976 // typically cast by layers without transparent regions.
977 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
978
979 // If the casting layer is translucent, we need to fill in the shadow underneath the
980 // layer. Otherwise the generated shadow will only be shown around the casting layer.
981 snapshot.shadowSettings.casterIsTranslucent =
982 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
983 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
984 snapshot.shadowSettings.spotColor *= snapshot.alpha;
985 }
986}
987
988void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
989 const RequestedLayerState& requested,
990 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000991 const LayerHierarchy::TraversalPath& path,
992 const Args& args) {
993 if (requested.windowInfoHandle) {
994 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
995 } else {
996 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +0000997 // b/271132344 revisit this and see if we can always use the layers uid/pid
998 snapshot.inputInfo.name = requested.name;
999 snapshot.inputInfo.ownerUid = static_cast<int32_t>(requested.ownerUid);
1000 snapshot.inputInfo.ownerPid = requested.ownerPid;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001001 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001002 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001003
Vishnu Nair93b8b792023-02-27 19:40:24 +00001004 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001005 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001006 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001007 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001008 return;
1009 }
1010
Vishnu Naircfb2d252023-01-19 04:44:02 +00001011 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1012 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1013 args.displays.get(snapshot.outputFilter.layerStack);
1014 bool noValidDisplay = !displayInfoOpt.has_value();
1015 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1016
1017 if (!requested.windowInfoHandle) {
1018 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1019 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001020 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1021
1022 if (noValidDisplay) {
1023 // Do not let the window receive touches if it is not associated with a valid display
1024 // transform. We still allow the window to receive keys and prevent ANRs.
1025 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1026 }
1027
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001028 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair40d02282023-02-28 21:11:40 +00001029 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1030 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1031 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001032 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1033 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1034 snapshot.dropInputMode = gui::DropInputMode::ALL;
1035 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1036 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1037 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1038 } else {
1039 snapshot.dropInputMode = gui::DropInputMode::NONE;
1040 }
1041
1042 handleDropInputMode(snapshot, parentSnapshot);
1043
1044 // If the window will be blacked out on a display because the display does not have the secure
1045 // flag and the layer has the secure flag set, then drop input.
1046 if (!displayInfo.isSecure && snapshot.isSecure) {
1047 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1048 }
1049
1050 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001051 if (cropLayerSnapshot) {
1052 mNeedsTouchableRegionCrop.insert(path);
1053 } else if (snapshot.inputInfo.replaceTouchableRegionWithCrop) {
1054 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001055 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001056 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1057 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001058 }
1059
1060 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1061 // if it was set by WM for a known system overlay
1062 if (snapshot.isTrustedOverlay) {
1063 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1064 }
1065
1066 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1067 // touches from going outside the cloned area.
1068 if (path.isClone()) {
1069 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001070 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1071 // WM or the client.
1072 snapshot.inputInfo.inputConfig.clear(gui::WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH);
1073
Vishnu Nair29354ec2023-03-28 18:51:28 -07001074 mNeedsTouchableRegionCrop.insert(path);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001075 }
1076}
1077
1078std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1079 return mSnapshots;
1080}
1081
Vishnu Naircfb2d252023-01-19 04:44:02 +00001082void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1083 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1084 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1085 if (!snapshot.isVisible) continue;
1086 visitor(snapshot);
1087 }
1088}
1089
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001090// Visit each visible snapshot in z-order
1091void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1092 const LayerHierarchy& root) const {
1093 root.traverseInZOrder(
1094 [this, visitor](const LayerHierarchy&,
1095 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1096 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1097 if (snapshot && snapshot->isVisible) {
1098 visitor(*snapshot);
1099 }
1100 return true;
1101 });
1102}
1103
Vishnu Naircfb2d252023-01-19 04:44:02 +00001104void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1105 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1106 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1107 if (!snapshot->isVisible) continue;
1108 visitor(snapshot);
1109 }
1110}
1111
1112void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1113 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1114 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1115 if (!snapshot.hasInputInfo()) continue;
1116 visitor(snapshot);
1117 }
1118}
1119
Vishnu Nair29354ec2023-03-28 18:51:28 -07001120void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1121 if (mNeedsTouchableRegionCrop.empty()) {
1122 return;
1123 }
1124
1125 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1126 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1127 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1128 RequestedLayerState::Changes::Input;
1129
1130 if (args.forceUpdate != ForceUpdateFlags::ALL &&
1131 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT)) {
1132 return;
1133 }
1134
1135 for (auto& path : mNeedsTouchableRegionCrop) {
1136 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1137 if (!snapshot) {
1138 continue;
1139 }
1140 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1141 args.displays.get(snapshot->outputFilter.layerStack);
1142 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1143 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1144
1145 bool needsUpdate =
1146 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1147 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1148 needsUpdate =
1149 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1150 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1151 needsUpdate = needsUpdate ||
1152 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1153
1154 if (!needsUpdate) {
1155 continue;
1156 }
1157
1158 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1159 Rect inputBoundsInDisplaySpace;
1160 if (!cropLayerSnapshot) {
1161 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1162 inputBoundsInDisplaySpace =
1163 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1164 } else {
1165 FloatRect inputBounds =
1166 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1167 inputBoundsInDisplaySpace =
1168 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1169 displayInfo.transform);
1170 }
1171 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1172 } else if (cropLayerSnapshot) {
1173 FloatRect inputBounds =
1174 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1175 Rect inputBoundsInDisplaySpace =
1176 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1177 displayInfo.transform);
1178 snapshot->inputInfo.touchableRegion = snapshot->inputInfo.touchableRegion.intersect(
1179 displayInfo.transform.transform(inputBoundsInDisplaySpace));
1180 }
1181
1182 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1183 // touches from going outside the cloned area.
1184 if (clonedRootSnapshot) {
1185 const Rect rect =
1186 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1187 snapshot->inputInfo.touchableRegion =
1188 snapshot->inputInfo.touchableRegion.intersect(rect);
1189 }
1190 }
1191}
1192
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001193} // namespace android::surfaceflinger::frontend