blob: 8df5d8c679b251ef93326545b74d189123f2198b [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 Nair92990e22023-02-24 20:01:05 +0000578 if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) {
579 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
580 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700581 mPathToSnapshot[path] = snapshot;
582
583 mIdToSnapshots.emplace(path.id, snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000584 return snapshot;
585}
586
Vishnu Nairfccd6362023-02-24 23:39:53 +0000587bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000588 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000589 !args.layerLifecycleManager.getGlobalChanges().any(
Chavi Weingarten92c7d8c2024-01-19 23:25:45 +0000590 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility |
591 RequestedLayerState::Changes::Input)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000592 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
593 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000594 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000595 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000596 mResortSnapshots = false;
597
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000598 size_t globalZ = 0;
599 args.root.traverseInZOrder(
600 [this, &globalZ](const LayerHierarchy&,
601 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
602 LayerSnapshot* snapshot = getSnapshot(traversalPath);
603 if (!snapshot) {
Vishnu Naira02943f2023-06-03 13:44:46 -0700604 return true;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000605 }
606
Vishnu Naircfb2d252023-01-19 04:44:02 +0000607 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000608 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000609 size_t oldZ = snapshot->globalZ;
610 size_t newZ = globalZ++;
611 snapshot->globalZ = newZ;
612 if (oldZ == newZ) {
613 return true;
614 }
615 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000616 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
617 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000618 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
619 mSnapshots.begin() + static_cast<ssize_t>(newZ));
620 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000621 return true;
622 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000623 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000624 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000625 while (globalZ < mSnapshots.size()) {
626 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000627 /* mark unreachable snapshots as explicitly invisible */
628 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Naira02943f2023-06-03 13:44:46 -0700629 if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
Vishnu Nairfccd6362023-02-24 23:39:53 +0000630 hasUnreachableSnapshots = true;
631 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000632 globalZ++;
633 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000634 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000635}
636
637void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
638 const LayerSnapshot& parentSnapshot,
639 bool parentIsRelative, const Args& args) {
640 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000641 snapshot.isHiddenByPolicyFromRelativeParent =
642 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000643 if (args.includeMetadata) {
644 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
645 }
646 } else {
647 snapshot.isHiddenByPolicyFromRelativeParent =
648 parentSnapshot.isHiddenByPolicyFromRelativeParent;
649 if (args.includeMetadata) {
650 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
651 }
652 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700653 if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
654 snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
655 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000656}
657
Vishnu Nair42b918e2023-07-18 20:05:29 +0000658void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
659 const LayerSnapshot& childSnapshot,
660 const Args& args) {
661 if (args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000662 !args.layerLifecycleManager.getGlobalChanges().any(
663 RequestedLayerState::Changes::Hierarchy) &&
664 !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) &&
665 !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000666 return;
667 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000668
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700669 using FrameRateCompatibility = scheduler::FrameRateCompatibility;
Rachel Leece6e0042023-06-27 11:22:54 -0700670 if (snapshot.frameRate.isValid()) {
Vishnu Nair42b918e2023-07-18 20:05:29 +0000671 // we already have a valid framerate.
672 return;
673 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000674
Vishnu Nair42b918e2023-07-18 20:05:29 +0000675 // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes
676 // for the same reason we are allowing touch boost for those layers. See
677 // RefreshRateSelector::rankFrameRates for details.
Rachel Leece6e0042023-06-27 11:22:54 -0700678 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
679 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Default;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000680 const auto layerVotedWithNoVote =
Rachel Leece6e0042023-06-27 11:22:54 -0700681 childSnapshot.frameRate.vote.type == FrameRateCompatibility::NoVote;
682 const auto layerVotedWithCategory =
683 childSnapshot.frameRate.category != FrameRateCategory::Default;
684 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
685 childSnapshot.frameRate.vote.type == FrameRateCompatibility::Exact;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000686
687 bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
Rachel Leece6e0042023-06-27 11:22:54 -0700688 layerVotedWithCategory || layerVotedWithExactCompatibility;
Vishnu Nair42b918e2023-07-18 20:05:29 +0000689
690 // If we don't have a valid frame rate, but the children do, we set this
691 // layer as NoVote to allow the children to control the refresh rate
692 if (childHasValidFrameRate) {
693 snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
694 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000695 }
696}
697
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000698void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
699 snapshot.isHiddenByPolicyFromRelativeParent = false;
700 snapshot.relativeLayerMetadata.mMap.clear();
701}
702
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000703void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
704 const RequestedLayerState& requested,
705 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000706 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000707 // Always update flags and visibility
708 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
709 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
710 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Nairf13c8982023-12-02 11:26:09 -0800711 RequestedLayerState::Changes::AffectsChildren | RequestedLayerState::Changes::Input |
Vishnu Naira02943f2023-06-03 13:44:46 -0700712 RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode);
713 snapshot.changes |= parentChanges;
714 if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry;
715 snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable;
716 snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000717 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000718 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
719 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair80a5a702023-02-11 01:21:51 +0000720
Vishnu Nair92990e22023-02-24 20:01:05 +0000721 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700722 snapshot.clientChanges & layer_state_t::eReparent ||
Vishnu Nair92990e22023-02-24 20:01:05 +0000723 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
724 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000725
Vishnu Naira02943f2023-06-03 13:44:46 -0700726 if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) {
727 // If root layer, use the layer stack otherwise get the parent's layer stack.
728 snapshot.outputFilter.layerStack =
729 parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
730 ? requested.layerStack
731 : parentSnapshot.outputFilter.layerStack;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000732 }
733
Chavi Weingartenb74093a2023-10-11 20:29:59 +0000734 if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) {
735 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
736 }
737
Vishnu Nair92990e22023-02-24 20:01:05 +0000738 if (snapshot.isHiddenByPolicyFromParent &&
739 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000740 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700741 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair494a2e42023-11-10 17:21:19 -0800742 RequestedLayerState::Changes::BufferSize |
Vishnu Naircfb2d252023-01-19 04:44:02 +0000743 RequestedLayerState::Changes::Input)) {
744 updateInput(snapshot, requested, parentSnapshot, path, args);
745 }
746 return;
747 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000748
Vishnu Naira02943f2023-06-03 13:44:46 -0700749 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) {
750 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
751 // marked as skip capture
752 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
753 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
754 }
755
756 if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000757 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
758 snapshot.alpha = snapshot.color.a;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700759 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Naira02943f2023-06-03 13:44:46 -0700760 }
Vishnu Nair29354ec2023-03-28 18:51:28 -0700761
Vishnu Naira02943f2023-06-03 13:44:46 -0700762 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000763 snapshot.isSecure =
764 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000765 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
766 (requested.flags & layer_state_t::eLayerSkipScreenshot);
Vishnu Naira02943f2023-06-03 13:44:46 -0700767 }
768
Vishnu Naira02943f2023-06-03 13:44:46 -0700769 if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000770 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
771 ? requested.stretchEffect
772 : parentSnapshot.stretchEffect;
Vishnu Naira02943f2023-06-03 13:44:46 -0700773 }
774
775 if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000776 if (!parentSnapshot.colorTransformIsIdentity) {
777 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
778 snapshot.colorTransformIsIdentity = false;
779 } else {
780 snapshot.colorTransform = requested.colorTransform;
781 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
782 }
Vishnu Naira02943f2023-06-03 13:44:46 -0700783 }
784
785 if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::GameMode)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000786 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
787 ? requested.gameMode
788 : parentSnapshot.gameMode;
Vishnu Naira02943f2023-06-03 13:44:46 -0700789 updateMetadata(snapshot, requested, args);
790 if (args.includeMetadata) {
791 snapshot.layerMetadata = parentSnapshot.layerMetadata;
792 snapshot.layerMetadata.merge(requested.metadata);
793 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000794 }
795
Vishnu Naira02943f2023-06-03 13:44:46 -0700796 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged ||
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700797 args.displayChanges) {
798 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
799 ? requested.fixedTransformHint
800 : parentSnapshot.fixedTransformHint;
801
802 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
803 snapshot.transformHint = snapshot.fixedTransformHint;
804 } else {
805 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
806 snapshot.transformHint = display.has_value()
807 ? std::make_optional<>(display->get().transformHint)
808 : std::nullopt;
809 }
810 }
811
Vishnu Nair42b918e2023-07-18 20:05:29 +0000812 if (forceUpdate ||
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000813 args.layerLifecycleManager.getGlobalChanges().any(
814 RequestedLayerState::Changes::Hierarchy) ||
Vishnu Nair42b918e2023-07-18 20:05:29 +0000815 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
816 RequestedLayerState::Changes::Hierarchy)) {
Rachel Leea021bb02023-11-20 21:51:09 -0800817 const bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy ==
Rachel Lee58cc90d2023-09-05 18:50:20 -0700818 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
Rachel Leea021bb02023-11-20 21:51:09 -0800819 const bool propagationAllowed = parentSnapshot.frameRateSelectionStrategy !=
Rachel Lee70f7b692023-11-22 11:24:02 -0800820 scheduler::LayerInfo::FrameRateSelectionStrategy::Self;
Rachel Leea021bb02023-11-20 21:51:09 -0800821 if ((!requested.requestedFrameRate.isValid() && propagationAllowed) ||
822 shouldOverrideChildren) {
Vishnu Nair30515cb2023-10-19 21:54:08 -0700823 snapshot.inheritedFrameRate = parentSnapshot.inheritedFrameRate;
824 } else {
825 snapshot.inheritedFrameRate = requested.requestedFrameRate;
826 }
827 // Set the framerate as the inherited frame rate and allow children to override it if
828 // needed.
829 snapshot.frameRate = snapshot.inheritedFrameRate;
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000830 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000831 }
832
Rachel Lee58cc90d2023-09-05 18:50:20 -0700833 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) {
Rachel Leea021bb02023-11-20 21:51:09 -0800834 if (parentSnapshot.frameRateSelectionStrategy ==
835 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren) {
836 snapshot.frameRateSelectionStrategy =
837 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
838 } else {
839 const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy(
840 requested.frameRateSelectionStrategy);
841 snapshot.frameRateSelectionStrategy = strategy;
842 }
Rachel Lee58cc90d2023-09-05 18:50:20 -0700843 }
844
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000845 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
846 snapshot.frameRateSelectionPriority =
847 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
848 ? parentSnapshot.frameRateSelectionPriority
849 : requested.frameRateSelectionPriority;
850 }
851
Vishnu Naira02943f2023-06-03 13:44:46 -0700852 if (forceUpdate ||
853 snapshot.clientChanges &
854 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
855 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000856 snapshot.backgroundBlurRadius = args.supportsBlur
857 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
858 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000859 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000860 for (auto& region : snapshot.blurRegions) {
861 region.alpha = region.alpha * snapshot.color.a;
862 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000863 }
864
Vishnu Naira02943f2023-06-03 13:44:46 -0700865 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
866 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700867 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700868 }
869
870 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
Vishnu Nair0808ae62023-08-07 21:42:42 -0700871 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
872 RequestedLayerState::Changes::BufferUsageFlags)) {
873 updateRoundedCorner(snapshot, requested, parentSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000874 }
875
Vishnu Naira02943f2023-06-03 13:44:46 -0700876 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
877 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
878 updateShadows(snapshot, requested, args.globalShadowSettings);
879 }
880
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000881 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700882 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000883 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000884 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000885 }
886
887 // computed snapshot properties
Alec Mouri994761f2023-08-04 21:50:55 +0000888 snapshot.forceClientComposition =
889 snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000890 snapshot.contentOpaque = snapshot.isContentOpaque();
891 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000892 snapshot.color.a == 1.f;
893 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000894 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000895 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
896 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
897 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
898 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
899 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000900}
901
902void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
903 const RequestedLayerState& requested,
Vishnu Nair0808ae62023-08-07 21:42:42 -0700904 const LayerSnapshot& parentSnapshot,
905 const Args& args) {
906 if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
907 snapshot.roundedCorner = RoundedCornerState();
908 return;
909 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000910 snapshot.roundedCorner = RoundedCornerState();
911 RoundedCornerState parentRoundedCorner;
912 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
913 parentRoundedCorner = parentSnapshot.roundedCorner;
914 ui::Transform t = snapshot.localTransform.inverse();
915 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
916 parentRoundedCorner.radius.x *= t.getScaleX();
917 parentRoundedCorner.radius.y *= t.getScaleY();
918 }
919
920 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
921 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
922 RoundedCornerState layerSettings(layerCropRect, radius);
923 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
924 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
925 if (layerSettingsValid && parentRoundedCornerValid) {
926 // If the parent and the layer have rounded corner settings, use the parent settings if
927 // the parent crop is entirely inside the layer crop. This has limitations and cause
928 // rendering artifacts. See b/200300845 for correct fix.
929 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
930 parentRoundedCorner.cropRect.top > layerCropRect.top &&
931 parentRoundedCorner.cropRect.right < layerCropRect.right &&
932 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
933 snapshot.roundedCorner = parentRoundedCorner;
934 } else {
935 snapshot.roundedCorner = layerSettings;
936 }
937 } else if (layerSettingsValid) {
938 snapshot.roundedCorner = layerSettings;
939 } else if (parentRoundedCornerValid) {
940 snapshot.roundedCorner = parentRoundedCorner;
941 }
942}
943
944void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
945 const RequestedLayerState& requested,
946 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700947 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000948 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000949 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000950 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
951 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000952 auto& t = snapshot.geomLayerTransform;
953 auto& requestedT = requested.requestedTransform;
954 std::string transformDebug =
955 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
956 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
957 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
958 std::string bufferDebug;
959 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700960 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000961 auto& destFrame = requested.destinationFrame;
962 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
963 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
964 requested.externalTexture->getWidth(),
965 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700966 primaryDisplayRotationFlags, destFrame.left,
967 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000968 unRotBuffer.getHeight(), unRotBuffer.getWidth());
969 }
970 ALOGW("Resetting transform for %s because it is invalid.%s%s",
971 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000972 snapshot.geomLayerTransform.reset();
973 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000974 if (transformWasInvalid != snapshot.invalidTransform) {
975 // If transform is invalid, the layer will be hidden.
976 mResortSnapshots = true;
977 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000978 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
979
980 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
981 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
982 snapshot.geomLayerBounds =
983 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
984 if (!requested.crop.isEmpty()) {
985 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
986 }
987 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
988 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000989 const Rect geomLayerBoundsWithoutTransparentRegion =
990 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
991 requested.transparentRegion);
992 snapshot.transformedBoundsWithoutTransparentRegion =
993 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000994 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
995
996 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000997 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000998 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000999 if (requested.potentialCursor) {
1000 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
1001 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001002}
1003
Vishnu Naira02943f2023-06-03 13:44:46 -07001004void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Naird9e4f462023-10-06 04:05:45 +00001005 const ShadowSettings& globalShadowSettings) {
1006 if (snapshot.shadowSettings.length > 0.f) {
1007 snapshot.shadowSettings.ambientColor = globalShadowSettings.ambientColor;
1008 snapshot.shadowSettings.spotColor = globalShadowSettings.spotColor;
1009 snapshot.shadowSettings.lightPos = globalShadowSettings.lightPos;
1010 snapshot.shadowSettings.lightRadius = globalShadowSettings.lightRadius;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001011
1012 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
1013 // it if transparent regions are present. This may not be necessary since shadows are
1014 // typically cast by layers without transparent regions.
1015 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
1016
1017 // If the casting layer is translucent, we need to fill in the shadow underneath the
1018 // layer. Otherwise the generated shadow will only be shown around the casting layer.
1019 snapshot.shadowSettings.casterIsTranslucent =
1020 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1021 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1022 snapshot.shadowSettings.spotColor *= snapshot.alpha;
1023 }
1024}
1025
1026void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1027 const RequestedLayerState& requested,
1028 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +00001029 const LayerHierarchy::TraversalPath& path,
1030 const Args& args) {
1031 if (requested.windowInfoHandle) {
1032 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1033 } else {
1034 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001035 // b/271132344 revisit this and see if we can always use the layers uid/pid
1036 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001037 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001038 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001039 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001040 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001041
Vishnu Nair93b8b792023-02-27 19:40:24 +00001042 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001043 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nairf13c8982023-12-02 11:26:09 -08001044 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1045 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1046 : parentSnapshot.inputInfo.touchOcclusionMode;
1047 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1048 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1049 snapshot.dropInputMode = gui::DropInputMode::ALL;
1050 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1051 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1052 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1053 } else {
1054 snapshot.dropInputMode = gui::DropInputMode::NONE;
1055 }
1056
Vishnu Nair29354ec2023-03-28 18:51:28 -07001057 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001058 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001059 return;
1060 }
1061
Vishnu Naircfb2d252023-01-19 04:44:02 +00001062 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1063 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1064 args.displays.get(snapshot.outputFilter.layerStack);
1065 bool noValidDisplay = !displayInfoOpt.has_value();
1066 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1067
1068 if (!requested.windowInfoHandle) {
1069 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1070 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001071 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1072
1073 if (noValidDisplay) {
1074 // Do not let the window receive touches if it is not associated with a valid display
1075 // transform. We still allow the window to receive keys and prevent ANRs.
1076 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1077 }
1078
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001079 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001080
1081 handleDropInputMode(snapshot, parentSnapshot);
1082
1083 // If the window will be blacked out on a display because the display does not have the secure
1084 // flag and the layer has the secure flag set, then drop input.
1085 if (!displayInfo.isSecure && snapshot.isSecure) {
1086 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1087 }
1088
Vishnu Naira02943f2023-06-03 13:44:46 -07001089 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001090 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001091 }
1092 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1093 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001094 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001095 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001096 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1097 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001098 }
1099
1100 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1101 // if it was set by WM for a known system overlay
1102 if (snapshot.isTrustedOverlay) {
1103 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1104 }
1105
Vishnu Nair494a2e42023-11-10 17:21:19 -08001106 snapshot.inputInfo.contentSize = snapshot.croppedBufferSize.getSize();
1107
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001108 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1109 // touches from going outside the cloned area.
1110 if (path.isClone()) {
1111 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001112 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1113 // WM or the client.
1114 snapshot.inputInfo.inputConfig.clear(gui::WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001115 }
1116}
1117
1118std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1119 return mSnapshots;
1120}
1121
Vishnu Naircfb2d252023-01-19 04:44:02 +00001122void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1123 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1124 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1125 if (!snapshot.isVisible) continue;
1126 visitor(snapshot);
1127 }
1128}
1129
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001130// Visit each visible snapshot in z-order
1131void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1132 const LayerHierarchy& root) const {
1133 root.traverseInZOrder(
1134 [this, visitor](const LayerHierarchy&,
1135 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1136 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1137 if (snapshot && snapshot->isVisible) {
1138 visitor(*snapshot);
1139 }
1140 return true;
1141 });
1142}
1143
Vishnu Naircfb2d252023-01-19 04:44:02 +00001144void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1145 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1146 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1147 if (!snapshot->isVisible) continue;
1148 visitor(snapshot);
1149 }
1150}
1151
1152void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1153 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1154 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1155 if (!snapshot.hasInputInfo()) continue;
1156 visitor(snapshot);
1157 }
1158}
1159
Vishnu Nair29354ec2023-03-28 18:51:28 -07001160void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1161 if (mNeedsTouchableRegionCrop.empty()) {
1162 return;
1163 }
1164
1165 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1166 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1167 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1168 RequestedLayerState::Changes::Input;
1169
1170 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001171 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001172 return;
1173 }
1174
1175 for (auto& path : mNeedsTouchableRegionCrop) {
1176 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1177 if (!snapshot) {
1178 continue;
1179 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001180 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1181 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001182 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1183 args.displays.get(snapshot->outputFilter.layerStack);
1184 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1185 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1186
1187 bool needsUpdate =
1188 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1189 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1190 needsUpdate =
1191 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1192 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1193 needsUpdate = needsUpdate ||
1194 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1195
1196 if (!needsUpdate) {
1197 continue;
1198 }
1199
1200 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1201 Rect inputBoundsInDisplaySpace;
1202 if (!cropLayerSnapshot) {
1203 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1204 inputBoundsInDisplaySpace =
1205 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1206 } else {
1207 FloatRect inputBounds =
1208 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1209 inputBoundsInDisplaySpace =
1210 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1211 displayInfo.transform);
1212 }
1213 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1214 } else if (cropLayerSnapshot) {
1215 FloatRect inputBounds =
1216 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1217 Rect inputBoundsInDisplaySpace =
1218 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1219 displayInfo.transform);
Chavi Weingarten1ba381e2024-01-09 21:54:11 +00001220 snapshot->inputInfo.touchableRegion =
1221 snapshot->inputInfo.touchableRegion.intersect(inputBoundsInDisplaySpace);
Vishnu Nair29354ec2023-03-28 18:51:28 -07001222 }
1223
1224 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1225 // touches from going outside the cloned area.
1226 if (clonedRootSnapshot) {
1227 const Rect rect =
1228 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1229 snapshot->inputInfo.touchableRegion =
1230 snapshot->inputInfo.touchableRegion.intersect(rect);
1231 }
1232 }
1233}
1234
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001235} // namespace android::surfaceflinger::frontend