blob: 2ff0facdbac63d1d76e7992e11f03cdacab46d79 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// #define LOG_NDEBUG 0
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070020#define LOG_TAG "SurfaceFlinger"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000021
Vishnu Nair8fc721b2022-12-22 20:06:32 +000022#include <numeric>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070023#include <optional>
24
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050025#include <ftl/small_map.h>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070026#include <gui/TraceUtils.h>
Vishnu Naira02943f2023-06-03 13:44:46 -070027#include <ui/DisplayMap.h>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050028#include <ui/FloatRect.h>
29
Vishnu Nair8fc721b2022-12-22 20:06:32 +000030#include "DisplayHardware/HWC2.h"
31#include "DisplayHardware/Hal.h"
Vishnu Nair3d8565a2023-06-30 07:23:24 +000032#include "Layer.h" // eFrameRateSelectionPriority constants
Vishnu Naircfb2d252023-01-19 04:44:02 +000033#include "LayerLog.h"
Vishnu Nairb76d99a2023-03-19 18:22:31 -070034#include "LayerSnapshotBuilder.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000035#include "TimeStats/TimeStats.h"
Vishnu Naird1f74982023-06-15 20:16:51 -070036#include "Tracing/TransactionTracing.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000037
38namespace android::surfaceflinger::frontend {
39
40using namespace ftl::flag_operators;
41
42namespace {
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050043
44FloatRect getMaxDisplayBounds(const DisplayInfos& displays) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000045 const ui::Size maxSize = [&displays] {
46 if (displays.empty()) return ui::Size{5000, 5000};
47
48 return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize,
49 [](ui::Size size, const auto& pair) -> ui::Size {
50 const auto& display = pair.second;
51 return {std::max(size.getWidth(), display.info.logicalWidth),
52 std::max(size.getHeight(), display.info.logicalHeight)};
53 });
54 }();
55
56 // Ignore display bounds for now since they will be computed later. Use a large Rect bound
57 // to ensure it's bigger than an actual display will be.
58 const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f;
59 const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f;
60
61 return {-xMax, -yMax, xMax, yMax};
62}
63
64// Applies the given transform to the region, while protecting against overflows caused by any
65// offsets. If applying the offset in the transform to any of the Rects in the region would result
66// in an overflow, they are not added to the output Region.
67Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
68 const std::string& debugWindowName) {
69 // Round the translation using the same rounding strategy used by ui::Transform.
70 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
71 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
72
73 ui::Transform transformWithoutOffset = t;
74 transformWithoutOffset.set(0.f, 0.f);
75
76 const Region transformed = transformWithoutOffset.transform(r);
77
78 // Apply the translation to each of the Rects in the region while discarding any that overflow.
79 Region ret;
80 for (const auto& rect : transformed) {
81 Rect newRect;
82 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
83 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
84 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
85 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
86 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
87 debugWindowName.c_str());
88 continue;
89 }
90 ret.orSelf(newRect);
91 }
92 return ret;
93}
94
95/*
96 * We don't want to send the layer's transform to input, but rather the
97 * parent's transform. This is because Layer's transform is
98 * information about how the buffer is placed on screen. The parent's
99 * transform makes more sense to send since it's information about how the
100 * layer is placed on screen. This transform is used by input to determine
101 * how to go from screen space back to window space.
102 */
103ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
104 if (!snapshot.hasBufferOrSidebandStream()) {
105 return snapshot.geomLayerTransform;
106 }
107 return snapshot.parentTransform;
108}
109
110/**
Vishnu Nairfed7c122023-03-18 01:54:43 +0000111 * Returns the bounds used to fill the input frame and the touchable region.
112 *
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000113 * Similar to getInputTransform, we need to update the bounds to include the transform.
114 * This is because bounds don't include the buffer transform, where the input assumes
115 * that's already included.
116 */
Vishnu Nairfed7c122023-03-18 01:54:43 +0000117std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) {
118 FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect();
119 if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() &&
120 snapshot.localTransform.getType() != ui::Transform::IDENTITY) {
121 inputBounds = snapshot.localTransform.transform(inputBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000122 }
123
Vishnu Nairfed7c122023-03-18 01:54:43 +0000124 bool inputBoundsValid = snapshot.croppedBufferSize.isValid();
125 if (!inputBoundsValid) {
126 /**
127 * Input bounds are based on the layer crop or buffer size. But if we are using
128 * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then
129 * we can use the parent bounds as the input bounds if the layer does not have buffer
130 * or a crop. We want to unify this logic but because of compat reasons we cannot always
131 * use the parent bounds. A layer without a buffer can get input. So when a window is
132 * initially added, its touchable region can fill its parent layer bounds and that can
133 * have negative consequences.
134 */
135 inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000136 }
Vishnu Nairfed7c122023-03-18 01:54:43 +0000137
138 // Clamp surface inset to the input bounds.
139 const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset);
140 const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f);
141 const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f);
142
143 // Apply the insets to the input bounds.
144 inputBounds.left += xSurfaceInset;
145 inputBounds.top += ySurfaceInset;
146 inputBounds.right -= xSurfaceInset;
147 inputBounds.bottom -= ySurfaceInset;
148 return {inputBounds, inputBoundsValid};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000149}
150
Vishnu Nairfed7c122023-03-18 01:54:43 +0000151Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds,
152 const ui::Transform& screenToDisplay) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000153 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
154 // frame and transform used for the layer, which determines the bounds and the coordinate space
155 // within which the layer will receive input.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000156
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000157 // Coordinate space definitions:
158 // - display: The display device's coordinate space. Correlates to pixels on the display.
159 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
160 // - layer: The coordinate space of this layer.
161 // - input: The coordinate space in which this layer will receive input events. This could be
162 // different than layer space if a surfaceInset is used, which changes the origin
163 // of the input space.
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000164
165 // Crop the input bounds to ensure it is within the parent's bounds.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000166 const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000167
168 const ui::Transform layerToScreen = getInputTransform(snapshot);
169 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
170
Vishnu Nairfed7c122023-03-18 01:54:43 +0000171 return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)};
172}
173
174void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay,
175 const LayerSnapshot& snapshot) {
176 auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false);
177 if (!inputBoundsValid) {
178 info.touchableRegion.clear();
179 }
180
Chavi Weingarten7f019192023-08-08 20:39:01 +0000181 info.frame = getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000182
183 ui::Transform inputToLayer;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000184 inputToLayer.set(inputBounds.left, inputBounds.top);
185 const ui::Transform layerToScreen = getInputTransform(snapshot);
186 const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000187
188 // InputDispatcher expects a display-to-input transform.
189 info.transform = inputToDisplay.inverse();
190
191 // The touchable region is specified in the input coordinate space. Change it to display space.
192 info.touchableRegion =
193 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
194}
195
196void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
197 if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
198 return;
199 }
200
201 // Check if we need to drop input unconditionally
202 const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
203 if (dropInputMode == gui::DropInputMode::ALL) {
204 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
205 ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
206 return;
207 }
208
209 // Check if we need to check if the window is obscured by parent
210 if (dropInputMode != gui::DropInputMode::OBSCURED) {
211 return;
212 }
213
214 // Check if the parent has set an alpha on the layer
215 if (parentSnapshot.color.a != 1.0_hf) {
216 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
217 ALOGV("Dropping input for %s as requested by policy because alpha=%f",
218 snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
219 }
220
221 // Check if the parent has cropped the buffer
222 Rect bufferSize = snapshot.croppedBufferSize;
223 if (!bufferSize.isValid()) {
224 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
225 return;
226 }
227
228 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
229 // To check if the layer has been cropped, we take the buffer bounds, apply the local
230 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
231 // match then the layer has not been cropped by its parents.
232 Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
233 bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
234
235 if (croppedByParent) {
236 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
237 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
238 snapshot.name.c_str());
239 } else {
240 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
241 // input if the window is obscured. This check should be done in surfaceflinger but the
242 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
243 // drop input events if the window is obscured.
244 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
245 }
246}
247
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000248auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
249 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
250 if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
251 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
252 : Hwc2::IComposerClient::BlendMode::COVERAGE;
253 }
254 return blendMode;
255}
256
Vishnu Nair80a5a702023-02-11 01:21:51 +0000257void updateVisibility(LayerSnapshot& snapshot, bool visible) {
258 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000259
260 // TODO(b/238781169) we are ignoring this compat for now, since we will have
261 // to remove any optimization based on visibility.
262
263 // For compatibility reasons we let layers which can receive input
264 // receive input before they have actually submitted a buffer. Because
265 // of this we use canReceiveInput instead of isVisible to check the
266 // policy-visibility, ignoring the buffer state. However for layers with
267 // hasInputInfo()==false we can use the real visibility state.
268 // We are just using these layers for occlusion detection in
269 // InputDispatcher, and obviously if they aren't visible they can't occlude
270 // anything.
Vishnu Nair80a5a702023-02-11 01:21:51 +0000271 const bool visibleForInput =
Vishnu Nair40d02282023-02-28 21:11:40 +0000272 snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000273 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naira02943f2023-06-03 13:44:46 -0700274 LLOGV(snapshot.sequence, "updating visibility %s %s", visible ? "true" : "false",
275 snapshot.getDebugString().c_str());
Vishnu Naircfb2d252023-01-19 04:44:02 +0000276}
277
278bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
279 if (requested.potentialCursor) {
280 return false;
281 }
282
283 if (snapshot.inputInfo.token != nullptr) {
284 return true;
285 }
286
287 if (snapshot.hasBufferOrSidebandStream()) {
288 return true;
289 }
290
291 return requested.windowInfoHandle &&
292 requested.windowInfoHandle->getInfo()->inputConfig.test(
293 gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
294}
295
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000296void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested,
297 const LayerSnapshotBuilder::Args& args) {
298 snapshot.metadata.clear();
299 for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) {
300 auto compatIter = args.genericLayerMetadataKeyMap.find(key);
301 if (compatIter == std::end(args.genericLayerMetadataKeyMap)) {
302 continue;
303 }
304 const uint32_t id = compatIter->second;
305 auto it = requested.metadata.mMap.find(id);
306 if (it == std::end(requested.metadata.mMap)) {
307 continue;
308 }
309
310 snapshot.metadata.emplace(key,
311 compositionengine::GenericLayerMetadataEntry{mandatory,
312 it->second});
313 }
314}
315
Vishnu Naircfb2d252023-01-19 04:44:02 +0000316void clearChanges(LayerSnapshot& snapshot) {
317 snapshot.changes.clear();
Vishnu Naira02943f2023-06-03 13:44:46 -0700318 snapshot.clientChanges = 0;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000319 snapshot.contentDirty = false;
320 snapshot.hasReadyFrame = false;
321 snapshot.sidebandStreamHasFrame = false;
322 snapshot.surfaceDamage.clear();
323}
324
Vishnu Naira02943f2023-06-03 13:44:46 -0700325// TODO (b/259407931): Remove.
326uint32_t getPrimaryDisplayRotationFlags(
327 const ui::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
328 for (auto& [_, display] : displays) {
329 if (display.isPrimary) {
330 return display.rotationFlags;
331 }
332 }
333 return 0;
334}
335
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000336} // namespace
337
338LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
339 LayerSnapshot snapshot;
Vishnu Nair92990e22023-02-24 20:01:05 +0000340 snapshot.path = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000341 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
Vishnu Naira02943f2023-06-03 13:44:46 -0700342 snapshot.clientChanges = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000343 snapshot.isHiddenByPolicyFromParent = false;
344 snapshot.isHiddenByPolicyFromRelativeParent = false;
345 snapshot.parentTransform.reset();
346 snapshot.geomLayerTransform.reset();
347 snapshot.geomInverseLayerTransform.reset();
348 snapshot.geomLayerBounds = getMaxDisplayBounds({});
349 snapshot.roundedCorner = RoundedCornerState();
350 snapshot.stretchEffect = {};
351 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
352 snapshot.outputFilter.toInternalDisplay = false;
353 snapshot.isSecure = false;
354 snapshot.color.a = 1.0_hf;
355 snapshot.colorTransformIsIdentity = true;
Vishnu Naird9e4f462023-10-06 04:05:45 +0000356 snapshot.shadowSettings.length = 0.f;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000357 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
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000368LayerSnapshotBuilder::LayerSnapshotBuilder() {}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000369
370LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000371 args.forceUpdate = ForceUpdateFlags::ALL;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000372 updateSnapshots(args);
373}
374
375bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700376 const bool forceUpdate = args.forceUpdate != ForceUpdateFlags::NONE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000377
Vishnu Naira02943f2023-06-03 13:44:46 -0700378 if (args.layerLifecycleManager.getGlobalChanges().get() == 0 && !forceUpdate &&
379 !args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000380 return true;
381 }
382
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000383 // There are only content changes which do not require any child layer snapshots to be updated.
384 ALOGV("%s", __func__);
385 ATRACE_NAME("FastPath");
386
Vishnu Naira02943f2023-06-03 13:44:46 -0700387 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
388 if (forceUpdate || args.displayChanges) {
389 for (auto& snapshot : mSnapshots) {
390 const RequestedLayerState* requested =
391 args.layerLifecycleManager.getLayerFromId(snapshot->path.id);
392 if (!requested) continue;
393 snapshot->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
394 primaryDisplayRotationFlags);
395 }
396 return false;
397 }
398
399 // Walk through all the updated requested layer states and update the corresponding snapshots.
400 for (const RequestedLayerState* requested : args.layerLifecycleManager.getChangedLayers()) {
401 auto range = mIdToSnapshots.equal_range(requested->id);
402 for (auto it = range.first; it != range.second; it++) {
403 it->second->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
404 primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000405 }
406 }
407
Vishnu Naira02943f2023-06-03 13:44:46 -0700408 if ((args.layerLifecycleManager.getGlobalChanges().get() &
409 ~(RequestedLayerState::Changes::Content | RequestedLayerState::Changes::Buffer).get()) !=
410 0) {
411 // We have changes that require us to walk the hierarchy and update child layers.
412 // No fast path for you.
413 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000414 }
415 return true;
416}
417
418void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
419 ATRACE_NAME("UpdateSnapshots");
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000420 LayerSnapshot rootSnapshot = args.rootSnapshot;
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000421 if (args.parentCrop) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000422 rootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000423 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000424 rootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000425 }
426 if (args.displayChanges) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000427 rootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000428 RequestedLayerState::Changes::Geometry;
429 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000430 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000431 rootSnapshot.changes |=
Vishnu Naird47bcee2023-02-24 18:08:51 +0000432 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000433 rootSnapshot.clientChanges |= layer_state_t::eReparent;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000434 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700435
436 for (auto& snapshot : mSnapshots) {
437 if (snapshot->reachablilty == LayerSnapshot::Reachablilty::Reachable) {
438 snapshot->reachablilty = LayerSnapshot::Reachablilty::Unreachable;
439 }
440 }
441
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000442 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000443 if (args.root.getLayer()) {
444 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
445 // multiple children.
446 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
447 LayerHierarchy::Variant::Attached);
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000448 updateSnapshotsInHierarchy(args, args.root, root, rootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000449 } else {
450 for (auto& [childHierarchy, variant] : args.root.mChildren) {
451 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
452 childHierarchy->getLayer()->id,
453 variant);
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000454 updateSnapshotsInHierarchy(args, *childHierarchy, root, rootSnapshot, /*depth=*/0);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000455 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000456 }
457
Vishnu Nair29354ec2023-03-28 18:51:28 -0700458 // Update touchable region crops outside the main update pass. This is because a layer could be
459 // cropped by any other layer and it requires both snapshots to be updated.
460 updateTouchableRegionCrop(args);
461
Vishnu Nairfccd6362023-02-24 23:39:53 +0000462 const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000463
Vishnu Nair29354ec2023-03-28 18:51:28 -0700464 // Destroy unreachable snapshots for clone layers. And destroy snapshots for non-clone
465 // layers if the layer have been destroyed.
466 // TODO(b/238781169) consider making clone layer ids stable as well
467 if (!hasUnreachableSnapshots && args.layerLifecycleManager.getDestroyedLayers().empty()) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000468 return;
469 }
470
Vishnu Nair29354ec2023-03-28 18:51:28 -0700471 std::unordered_set<uint32_t> destroyedLayerIds;
472 for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) {
473 destroyedLayerIds.insert(destroyedLayer->id);
474 }
475
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000476 auto it = mSnapshots.begin();
477 while (it < mSnapshots.end()) {
478 auto& traversalPath = it->get()->path;
Vishnu Naira02943f2023-06-03 13:44:46 -0700479 const bool unreachable =
480 it->get()->reachablilty == LayerSnapshot::Reachablilty::Unreachable;
481 const bool isClone = traversalPath.isClone();
482 const bool layerIsDestroyed =
483 destroyedLayerIds.find(traversalPath.id) != destroyedLayerIds.end();
484 const bool destroySnapshot = (unreachable && isClone) || layerIsDestroyed;
485
486 if (!destroySnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000487 it++;
488 continue;
489 }
490
Vishnu Naira02943f2023-06-03 13:44:46 -0700491 mPathToSnapshot.erase(traversalPath);
492
493 auto range = mIdToSnapshots.equal_range(traversalPath.id);
494 auto matchingSnapshot =
495 std::find_if(range.first, range.second, [&traversalPath](auto& snapshotWithId) {
496 return snapshotWithId.second->path == traversalPath;
497 });
498 mIdToSnapshots.erase(matchingSnapshot);
Vishnu Nair29354ec2023-03-28 18:51:28 -0700499 mNeedsTouchableRegionCrop.erase(traversalPath);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000500 mSnapshots.back()->globalZ = it->get()->globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000501 std::iter_swap(it, mSnapshots.end() - 1);
502 mSnapshots.erase(mSnapshots.end() - 1);
503 }
504}
505
506void LayerSnapshotBuilder::update(const Args& args) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000507 for (auto& snapshot : mSnapshots) {
508 clearChanges(*snapshot);
509 }
510
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000511 if (tryFastUpdate(args)) {
512 return;
513 }
514 updateSnapshots(args);
515}
516
Vishnu Naircfb2d252023-01-19 04:44:02 +0000517const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
518 const Args& args, const LayerHierarchy& hierarchy,
Vishnu Naird1f74982023-06-15 20:16:51 -0700519 LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot,
520 int depth) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700521 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
522 "Cycle detected in LayerSnapshotBuilder. See "
523 "builder_stack_overflow_transactions.winscope");
Vishnu Naird1f74982023-06-15 20:16:51 -0700524
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000525 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000526 LayerSnapshot* snapshot = getSnapshot(traversalPath);
527 const bool newSnapshot = snapshot == nullptr;
Vishnu Naira02943f2023-06-03 13:44:46 -0700528 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000529 if (newSnapshot) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000530 snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700531 snapshot->merge(*layer, /*forceUpdate=*/true, /*displayChanges=*/true, args.forceFullDamage,
532 primaryDisplayRotationFlags);
533 snapshot->changes |= RequestedLayerState::Changes::Created;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000534 }
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000535
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000536 if (traversalPath.isRelative()) {
537 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
538 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
539 } else {
540 if (traversalPath.isAttached()) {
541 resetRelativeState(*snapshot);
542 }
Vishnu Nair92990e22023-02-24 20:01:05 +0000543 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000544 }
545
546 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
547 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
548 childHierarchy->getLayer()->id,
549 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000550 const LayerSnapshot& childSnapshot =
Vishnu Naird1f74982023-06-15 20:16:51 -0700551 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot,
552 depth + 1);
Vishnu Nair42b918e2023-07-18 20:05:29 +0000553 updateFrameRateFromChildSnapshot(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000554 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000555
Vishnu Naircfb2d252023-01-19 04:44:02 +0000556 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000557}
558
559LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
560 if (layerId == UNASSIGNED_LAYER_ID) {
561 return nullptr;
562 }
563 LayerHierarchy::TraversalPath path{.id = layerId};
564 return getSnapshot(path);
565}
566
567LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700568 auto it = mPathToSnapshot.find(id);
569 return it == mPathToSnapshot.end() ? nullptr : it->second;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000570}
571
Vishnu Nair92990e22023-02-24 20:01:05 +0000572LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
573 const RequestedLayerState& layer,
574 const LayerSnapshot& parentSnapshot) {
575 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000576 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000577 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
Vishnu Nair491827d2024-04-29 23:43:26 +0000578 if (path.isClone() && !LayerHierarchy::isMirror(path.variant)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000579 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
580 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000581 snapshot->ignoreLocalTransform =
582 path.isClone() && path.variant == LayerHierarchy::Variant::Detached_Mirror;
Vishnu Naira02943f2023-06-03 13:44:46 -0700583 mPathToSnapshot[path] = snapshot;
584
585 mIdToSnapshots.emplace(path.id, snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000586 return snapshot;
587}
588
Vishnu Nairfccd6362023-02-24 23:39:53 +0000589bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000590 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000591 !args.layerLifecycleManager.getGlobalChanges().any(
Chavi Weingarten92c7d8c2024-01-19 23:25:45 +0000592 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility |
593 RequestedLayerState::Changes::Input)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000594 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
595 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000596 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000597 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000598 mResortSnapshots = false;
599
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000600 size_t globalZ = 0;
601 args.root.traverseInZOrder(
602 [this, &globalZ](const LayerHierarchy&,
603 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
604 LayerSnapshot* snapshot = getSnapshot(traversalPath);
605 if (!snapshot) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700606 return true;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000607 }
608
Vishnu Naircfb2d252023-01-19 04:44:02 +0000609 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000610 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000611 size_t oldZ = snapshot->globalZ;
612 size_t newZ = globalZ++;
613 snapshot->globalZ = newZ;
614 if (oldZ == newZ) {
615 return true;
616 }
617 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000618 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
619 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000620 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
621 mSnapshots.begin() + static_cast<ssize_t>(newZ));
622 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000623 return true;
624 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000625 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000626 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000627 while (globalZ < mSnapshots.size()) {
628 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000629 /* mark unreachable snapshots as explicitly invisible */
630 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Naira02943f2023-06-03 13:44:46 -0700631 if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
Vishnu Nairfccd6362023-02-24 23:39:53 +0000632 hasUnreachableSnapshots = true;
633 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000634 globalZ++;
635 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000636 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000637}
638
639void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
640 const LayerSnapshot& parentSnapshot,
641 bool parentIsRelative, const Args& args) {
642 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000643 snapshot.isHiddenByPolicyFromRelativeParent =
644 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000645 if (args.includeMetadata) {
646 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
647 }
648 } else {
649 snapshot.isHiddenByPolicyFromRelativeParent =
650 parentSnapshot.isHiddenByPolicyFromRelativeParent;
651 if (args.includeMetadata) {
652 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
653 }
654 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700655 if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
656 snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
657 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000658}
659
Vishnu Nair42b918e2023-07-18 20:05:29 +0000660void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
661 const LayerSnapshot& childSnapshot,
662 const Args& args) {
663 if (args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000664 !args.layerLifecycleManager.getGlobalChanges().any(
665 RequestedLayerState::Changes::Hierarchy) &&
666 !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) &&
667 !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000668 return;
669 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000670
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700671 using FrameRateCompatibility = scheduler::FrameRateCompatibility;
Rachel Leece6e0042023-06-27 11:22:54 -0700672 if (snapshot.frameRate.isValid()) {
Vishnu Nair42b918e2023-07-18 20:05:29 +0000673 // we already have a valid framerate.
674 return;
675 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000676
Vishnu Nair42b918e2023-07-18 20:05:29 +0000677 // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes
678 // for the same reason we are allowing touch boost for those layers. See
679 // RefreshRateSelector::rankFrameRates for details.
Rachel Leece6e0042023-06-27 11:22:54 -0700680 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
681 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Default;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000682 const auto layerVotedWithNoVote =
Rachel Leece6e0042023-06-27 11:22:54 -0700683 childSnapshot.frameRate.vote.type == FrameRateCompatibility::NoVote;
684 const auto layerVotedWithCategory =
685 childSnapshot.frameRate.category != FrameRateCategory::Default;
686 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
687 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Exact;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000688
689 bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
Rachel Leece6e0042023-06-27 11:22:54 -0700690 layerVotedWithCategory || layerVotedWithExactCompatibility;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000691
692 // If we don't have a valid frame rate, but the children do, we set this
693 // layer as NoVote to allow the children to control the refresh rate
694 if (childHasValidFrameRate) {
695 snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
696 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000697 }
698}
699
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000700void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
701 snapshot.isHiddenByPolicyFromRelativeParent = false;
702 snapshot.relativeLayerMetadata.mMap.clear();
703}
704
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000705void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
706 const RequestedLayerState& requested,
707 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000708 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000709 // Always update flags and visibility
710 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
711 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
712 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Nairf13c8982023-12-02 11:26:09 -0800713 RequestedLayerState::Changes::AffectsChildren | RequestedLayerState::Changes::Input |
Vishnu Naira02943f2023-06-03 13:44:46 -0700714 RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode);
715 snapshot.changes |= parentChanges;
716 if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry;
717 snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable;
718 snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000719 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000720 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
721 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair80a5a702023-02-11 01:21:51 +0000722
Vishnu Nair92990e22023-02-24 20:01:05 +0000723 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700724 snapshot.clientChanges & layer_state_t::eReparent ||
Vishnu Nair92990e22023-02-24 20:01:05 +0000725 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
726 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000727
Vishnu Naira02943f2023-06-03 13:44:46 -0700728 if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) {
729 // If root layer, use the layer stack otherwise get the parent's layer stack.
730 snapshot.outputFilter.layerStack =
731 parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
732 ? requested.layerStack
733 : parentSnapshot.outputFilter.layerStack;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000734 }
735
Chavi Weingartenb74093a2023-10-11 20:29:59 +0000736 if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) {
737 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
738 }
739
Vishnu Nair92990e22023-02-24 20:01:05 +0000740 if (snapshot.isHiddenByPolicyFromParent &&
741 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000742 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700743 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair494a2e42023-11-10 17:21:19 -0800744 RequestedLayerState::Changes::BufferSize |
Vishnu Naircfb2d252023-01-19 04:44:02 +0000745 RequestedLayerState::Changes::Input)) {
746 updateInput(snapshot, requested, parentSnapshot, path, args);
747 }
748 return;
749 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000750
Vishnu Naira02943f2023-06-03 13:44:46 -0700751 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) {
752 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
753 // marked as skip capture
754 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
755 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
756 }
757
758 if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000759 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
760 snapshot.alpha = snapshot.color.a;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700761 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Naira02943f2023-06-03 13:44:46 -0700762 }
Vishnu Nair29354ec2023-03-28 18:51:28 -0700763
Vishnu Naira02943f2023-06-03 13:44:46 -0700764 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000765 snapshot.isSecure =
766 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000767 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
768 (requested.flags & layer_state_t::eLayerSkipScreenshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700769 }
770
Vishnu Naira02943f2023-06-03 13:44:46 -0700771 if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000772 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
773 ? requested.stretchEffect
774 : parentSnapshot.stretchEffect;
Vishnu Naira02943f2023-06-03 13:44:46 -0700775 }
776
777 if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000778 if (!parentSnapshot.colorTransformIsIdentity) {
779 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
780 snapshot.colorTransformIsIdentity = false;
781 } else {
782 snapshot.colorTransform = requested.colorTransform;
783 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
784 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700785 }
786
787 if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::GameMode)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000788 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
789 ? requested.gameMode
790 : parentSnapshot.gameMode;
Vishnu Naira02943f2023-06-03 13:44:46 -0700791 updateMetadata(snapshot, requested, args);
792 if (args.includeMetadata) {
793 snapshot.layerMetadata = parentSnapshot.layerMetadata;
794 snapshot.layerMetadata.merge(requested.metadata);
795 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000796 }
797
Vishnu Naira02943f2023-06-03 13:44:46 -0700798 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged ||
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700799 args.displayChanges) {
800 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
801 ? requested.fixedTransformHint
802 : parentSnapshot.fixedTransformHint;
803
804 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
805 snapshot.transformHint = snapshot.fixedTransformHint;
806 } else {
807 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
808 snapshot.transformHint = display.has_value()
809 ? std::make_optional<>(display->get().transformHint)
810 : std::nullopt;
811 }
812 }
813
Vishnu Nair42b918e2023-07-18 20:05:29 +0000814 if (forceUpdate ||
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000815 args.layerLifecycleManager.getGlobalChanges().any(
816 RequestedLayerState::Changes::Hierarchy) ||
Vishnu Nair42b918e2023-07-18 20:05:29 +0000817 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
818 RequestedLayerState::Changes::Hierarchy)) {
Rachel Leea021bb02023-11-20 21:51:09 -0800819 const bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy ==
Rachel Lee58cc90d2023-09-05 18:50:20 -0700820 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
Rachel Leea021bb02023-11-20 21:51:09 -0800821 const bool propagationAllowed = parentSnapshot.frameRateSelectionStrategy !=
Rachel Lee70f7b692023-11-22 11:24:02 -0800822 scheduler::LayerInfo::FrameRateSelectionStrategy::Self;
Rachel Leea021bb02023-11-20 21:51:09 -0800823 if ((!requested.requestedFrameRate.isValid() && propagationAllowed) ||
824 shouldOverrideChildren) {
Vishnu Nair30515cb2023-10-19 21:54:08 -0700825 snapshot.inheritedFrameRate = parentSnapshot.inheritedFrameRate;
826 } else {
827 snapshot.inheritedFrameRate = requested.requestedFrameRate;
828 }
829 // Set the framerate as the inherited frame rate and allow children to override it if
830 // needed.
831 snapshot.frameRate = snapshot.inheritedFrameRate;
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000832 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000833 }
834
Rachel Lee58cc90d2023-09-05 18:50:20 -0700835 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) {
Rachel Leea021bb02023-11-20 21:51:09 -0800836 if (parentSnapshot.frameRateSelectionStrategy ==
837 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren) {
838 snapshot.frameRateSelectionStrategy =
839 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
840 } else {
841 const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy(
842 requested.frameRateSelectionStrategy);
843 snapshot.frameRateSelectionStrategy = strategy;
844 }
Rachel Lee58cc90d2023-09-05 18:50:20 -0700845 }
846
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000847 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
848 snapshot.frameRateSelectionPriority =
849 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
850 ? parentSnapshot.frameRateSelectionPriority
851 : requested.frameRateSelectionPriority;
852 }
853
Vishnu Naira02943f2023-06-03 13:44:46 -0700854 if (forceUpdate ||
855 snapshot.clientChanges &
856 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
857 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000858 snapshot.backgroundBlurRadius = args.supportsBlur
859 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
860 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000861 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000862 for (auto& region : snapshot.blurRegions) {
863 region.alpha = region.alpha * snapshot.color.a;
864 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000865 }
866
Vishnu Naira02943f2023-06-03 13:44:46 -0700867 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
868 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700869 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700870 }
871
872 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
Vishnu Nair0808ae62023-08-07 21:42:42 -0700873 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
874 RequestedLayerState::Changes::BufferUsageFlags)) {
875 updateRoundedCorner(snapshot, requested, parentSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000876 }
877
Vishnu Naira02943f2023-06-03 13:44:46 -0700878 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
879 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
880 updateShadows(snapshot, requested, args.globalShadowSettings);
881 }
882
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000883 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700884 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000885 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000886 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000887 }
888
889 // computed snapshot properties
Alec Mouri994761f2023-08-04 21:50:55 +0000890 snapshot.forceClientComposition =
891 snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000892 snapshot.contentOpaque = snapshot.isContentOpaque();
893 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000894 snapshot.color.a == 1.f;
895 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000896 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000897 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
898 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
899 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
900 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
901 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000902}
903
904void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
905 const RequestedLayerState& requested,
Vishnu Nair0808ae62023-08-07 21:42:42 -0700906 const LayerSnapshot& parentSnapshot,
907 const Args& args) {
908 if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
909 snapshot.roundedCorner = RoundedCornerState();
910 return;
911 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000912 snapshot.roundedCorner = RoundedCornerState();
913 RoundedCornerState parentRoundedCorner;
914 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
915 parentRoundedCorner = parentSnapshot.roundedCorner;
916 ui::Transform t = snapshot.localTransform.inverse();
917 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
918 parentRoundedCorner.radius.x *= t.getScaleX();
919 parentRoundedCorner.radius.y *= t.getScaleY();
920 }
921
922 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
923 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
924 RoundedCornerState layerSettings(layerCropRect, radius);
925 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
926 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
927 if (layerSettingsValid && parentRoundedCornerValid) {
928 // If the parent and the layer have rounded corner settings, use the parent settings if
929 // the parent crop is entirely inside the layer crop. This has limitations and cause
930 // rendering artifacts. See b/200300845 for correct fix.
931 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
932 parentRoundedCorner.cropRect.top > layerCropRect.top &&
933 parentRoundedCorner.cropRect.right < layerCropRect.right &&
934 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
935 snapshot.roundedCorner = parentRoundedCorner;
936 } else {
937 snapshot.roundedCorner = layerSettings;
938 }
939 } else if (layerSettingsValid) {
940 snapshot.roundedCorner = layerSettings;
941 } else if (parentRoundedCornerValid) {
942 snapshot.roundedCorner = parentRoundedCorner;
943 }
944}
945
946void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
947 const RequestedLayerState& requested,
948 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700949 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000950 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000951 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000952 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
953 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000954 auto& t = snapshot.geomLayerTransform;
955 auto& requestedT = requested.requestedTransform;
956 std::string transformDebug =
957 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
958 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
959 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
960 std::string bufferDebug;
961 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700962 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000963 auto& destFrame = requested.destinationFrame;
964 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
965 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
966 requested.externalTexture->getWidth(),
967 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700968 primaryDisplayRotationFlags, destFrame.left,
969 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000970 unRotBuffer.getHeight(), unRotBuffer.getWidth());
971 }
972 ALOGW("Resetting transform for %s because it is invalid.%s%s",
973 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000974 snapshot.geomLayerTransform.reset();
975 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000976 if (transformWasInvalid != snapshot.invalidTransform) {
977 // If transform is invalid, the layer will be hidden.
978 mResortSnapshots = true;
979 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000980 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
981
982 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
983 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
984 snapshot.geomLayerBounds =
985 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
986 if (!requested.crop.isEmpty()) {
987 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
988 }
989 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
990 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000991 const Rect geomLayerBoundsWithoutTransparentRegion =
992 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
993 requested.transparentRegion);
994 snapshot.transformedBoundsWithoutTransparentRegion =
995 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000996 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
997
998 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000999 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001000 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001001 if (requested.potentialCursor) {
1002 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
1003 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001004}
1005
Vishnu Naira02943f2023-06-03 13:44:46 -07001006void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Naird9e4f462023-10-06 04:05:45 +00001007 const ShadowSettings& globalShadowSettings) {
1008 if (snapshot.shadowSettings.length > 0.f) {
1009 snapshot.shadowSettings.ambientColor = globalShadowSettings.ambientColor;
1010 snapshot.shadowSettings.spotColor = globalShadowSettings.spotColor;
1011 snapshot.shadowSettings.lightPos = globalShadowSettings.lightPos;
1012 snapshot.shadowSettings.lightRadius = globalShadowSettings.lightRadius;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001013
1014 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
1015 // it if transparent regions are present. This may not be necessary since shadows are
1016 // typically cast by layers without transparent regions.
1017 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
1018
1019 // If the casting layer is translucent, we need to fill in the shadow underneath the
1020 // layer. Otherwise the generated shadow will only be shown around the casting layer.
1021 snapshot.shadowSettings.casterIsTranslucent =
1022 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1023 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1024 snapshot.shadowSettings.spotColor *= snapshot.alpha;
1025 }
1026}
1027
1028void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1029 const RequestedLayerState& requested,
1030 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +00001031 const LayerHierarchy::TraversalPath& path,
1032 const Args& args) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001033 using InputConfig = gui::WindowInfo::InputConfig;
1034
Vishnu Naircfb2d252023-01-19 04:44:02 +00001035 if (requested.windowInfoHandle) {
1036 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1037 } else {
1038 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001039 // b/271132344 revisit this and see if we can always use the layers uid/pid
1040 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001041 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001042 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001043 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001044 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001045
Vishnu Nair93b8b792023-02-27 19:40:24 +00001046 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001047 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nairf13c8982023-12-02 11:26:09 -08001048 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1049 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1050 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair59a6be32024-01-29 10:26:21 -08001051 snapshot.inputInfo.canOccludePresentation = parentSnapshot.inputInfo.canOccludePresentation ||
1052 (requested.flags & layer_state_t::eCanOccludePresentation);
Vishnu Nairf13c8982023-12-02 11:26:09 -08001053 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1054 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1055 snapshot.dropInputMode = gui::DropInputMode::ALL;
1056 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1057 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1058 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1059 } else {
1060 snapshot.dropInputMode = gui::DropInputMode::NONE;
1061 }
1062
Prabir Pradhancf359192024-03-20 00:42:57 +00001063 if (snapshot.isSecure ||
Arpit Singh490ccc92024-04-30 14:26:21 +00001064 parentSnapshot.inputInfo.inputConfig.test(InputConfig::SENSITIVE_FOR_PRIVACY)) {
1065 snapshot.inputInfo.inputConfig |= InputConfig::SENSITIVE_FOR_PRIVACY;
Prabir Pradhancf359192024-03-20 00:42:57 +00001066 }
1067
Vishnu Nair29354ec2023-03-28 18:51:28 -07001068 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001069 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001070 return;
1071 }
1072
Vishnu Naircfb2d252023-01-19 04:44:02 +00001073 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1074 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1075 args.displays.get(snapshot.outputFilter.layerStack);
1076 bool noValidDisplay = !displayInfoOpt.has_value();
1077 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1078
1079 if (!requested.windowInfoHandle) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001080 snapshot.inputInfo.inputConfig = InputConfig::NO_INPUT_CHANNEL;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001081 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001082 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1083
1084 if (noValidDisplay) {
1085 // Do not let the window receive touches if it is not associated with a valid display
1086 // transform. We still allow the window to receive keys and prevent ANRs.
Prabir Pradhancf359192024-03-20 00:42:57 +00001087 snapshot.inputInfo.inputConfig |= InputConfig::NOT_TOUCHABLE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001088 }
1089
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001090 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001091
1092 handleDropInputMode(snapshot, parentSnapshot);
1093
1094 // If the window will be blacked out on a display because the display does not have the secure
1095 // flag and the layer has the secure flag set, then drop input.
1096 if (!displayInfo.isSecure && snapshot.isSecure) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001097 snapshot.inputInfo.inputConfig |= InputConfig::DROP_INPUT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001098 }
1099
Vishnu Naira02943f2023-06-03 13:44:46 -07001100 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001101 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001102 }
1103 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1104 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001105 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001106 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001107 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1108 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001109 }
1110
1111 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1112 // if it was set by WM for a known system overlay
1113 if (snapshot.isTrustedOverlay) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001114 snapshot.inputInfo.inputConfig |= InputConfig::TRUSTED_OVERLAY;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001115 }
1116
Vishnu Nair494a2e42023-11-10 17:21:19 -08001117 snapshot.inputInfo.contentSize = snapshot.croppedBufferSize.getSize();
1118
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001119 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1120 // touches from going outside the cloned area.
1121 if (path.isClone()) {
Prabir Pradhancf359192024-03-20 00:42:57 +00001122 snapshot.inputInfo.inputConfig |= InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001123 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1124 // WM or the client.
Prabir Pradhancf359192024-03-20 00:42:57 +00001125 snapshot.inputInfo.inputConfig.clear(InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001126 }
1127}
1128
1129std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1130 return mSnapshots;
1131}
1132
Vishnu Naircfb2d252023-01-19 04:44:02 +00001133void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1134 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1135 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1136 if (!snapshot.isVisible) continue;
1137 visitor(snapshot);
1138 }
1139}
1140
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001141// Visit each visible snapshot in z-order
1142void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1143 const LayerHierarchy& root) const {
1144 root.traverseInZOrder(
1145 [this, visitor](const LayerHierarchy&,
1146 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1147 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1148 if (snapshot && snapshot->isVisible) {
1149 visitor(*snapshot);
1150 }
1151 return true;
1152 });
1153}
1154
Vishnu Naircfb2d252023-01-19 04:44:02 +00001155void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1156 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1157 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1158 if (!snapshot->isVisible) continue;
1159 visitor(snapshot);
1160 }
1161}
1162
1163void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1164 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1165 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1166 if (!snapshot.hasInputInfo()) continue;
1167 visitor(snapshot);
1168 }
1169}
1170
Vishnu Nair29354ec2023-03-28 18:51:28 -07001171void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1172 if (mNeedsTouchableRegionCrop.empty()) {
1173 return;
1174 }
1175
1176 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1177 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1178 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1179 RequestedLayerState::Changes::Input;
1180
1181 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001182 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001183 return;
1184 }
1185
1186 for (auto& path : mNeedsTouchableRegionCrop) {
1187 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1188 if (!snapshot) {
1189 continue;
1190 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001191 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1192 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001193 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1194 args.displays.get(snapshot->outputFilter.layerStack);
1195 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1196 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1197
1198 bool needsUpdate =
1199 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1200 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1201 needsUpdate =
1202 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1203 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1204 needsUpdate = needsUpdate ||
1205 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1206
1207 if (!needsUpdate) {
1208 continue;
1209 }
1210
1211 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1212 Rect inputBoundsInDisplaySpace;
1213 if (!cropLayerSnapshot) {
1214 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1215 inputBoundsInDisplaySpace =
1216 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1217 } else {
1218 FloatRect inputBounds =
1219 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1220 inputBoundsInDisplaySpace =
1221 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1222 displayInfo.transform);
1223 }
1224 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1225 } else if (cropLayerSnapshot) {
1226 FloatRect inputBounds =
1227 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1228 Rect inputBoundsInDisplaySpace =
1229 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1230 displayInfo.transform);
Chavi Weingarten1ba381e2024-01-09 21:54:11 +00001231 snapshot->inputInfo.touchableRegion =
1232 snapshot->inputInfo.touchableRegion.intersect(inputBoundsInDisplaySpace);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001233 }
1234
1235 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1236 // touches from going outside the cloned area.
1237 if (clonedRootSnapshot) {
1238 const Rect rect =
1239 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1240 snapshot->inputInfo.touchableRegion =
1241 snapshot->inputInfo.touchableRegion.intersect(rect);
1242 }
1243 }
1244}
1245
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001246} // namespace android::surfaceflinger::frontend