blob: 4e2a25fce522c6670a5514e4c03a717bfb34a303 [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(
590 RequestedLayerState::Changes::Hierarchy |
591 RequestedLayerState::Changes::Visibility)) {
592 // 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 Lee58cc90d2023-09-05 18:50:20 -0700817 bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy ==
818 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
Vishnu Nair30515cb2023-10-19 21:54:08 -0700819 if (!requested.requestedFrameRate.isValid() || shouldOverrideChildren) {
820 snapshot.inheritedFrameRate = parentSnapshot.inheritedFrameRate;
821 } else {
822 snapshot.inheritedFrameRate = requested.requestedFrameRate;
823 }
824 // Set the framerate as the inherited frame rate and allow children to override it if
825 // needed.
826 snapshot.frameRate = snapshot.inheritedFrameRate;
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000827 snapshot.changes |= RequestedLayerState::Changes::FrameRate;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000828 }
829
Rachel Lee58cc90d2023-09-05 18:50:20 -0700830 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) {
831 const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy(
832 requested.frameRateSelectionStrategy);
833 snapshot.frameRateSelectionStrategy =
834 strategy == scheduler::LayerInfo::FrameRateSelectionStrategy::Self
835 ? parentSnapshot.frameRateSelectionStrategy
836 : strategy;
837 }
838
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000839 if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
840 snapshot.frameRateSelectionPriority =
841 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
842 ? parentSnapshot.frameRateSelectionPriority
843 : requested.frameRateSelectionPriority;
844 }
845
Vishnu Naira02943f2023-06-03 13:44:46 -0700846 if (forceUpdate ||
847 snapshot.clientChanges &
848 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
849 layer_state_t::eAlphaChanged)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000850 snapshot.backgroundBlurRadius = args.supportsBlur
851 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
852 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000853 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000854 for (auto& region : snapshot.blurRegions) {
855 region.alpha = region.alpha * snapshot.color.a;
856 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000857 }
858
Vishnu Naira02943f2023-06-03 13:44:46 -0700859 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
860 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700861 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Naira02943f2023-06-03 13:44:46 -0700862 }
863
864 if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
Vishnu Nair0808ae62023-08-07 21:42:42 -0700865 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
866 RequestedLayerState::Changes::BufferUsageFlags)) {
867 updateRoundedCorner(snapshot, requested, parentSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000868 }
869
Vishnu Naira02943f2023-06-03 13:44:46 -0700870 if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
871 snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
872 updateShadows(snapshot, requested, args.globalShadowSettings);
873 }
874
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000875 if (forceUpdate ||
Vishnu Naira02943f2023-06-03 13:44:46 -0700876 snapshot.changes.any(RequestedLayerState::Changes::Geometry |
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000877 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000878 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000879 }
880
881 // computed snapshot properties
Alec Mouri994761f2023-08-04 21:50:55 +0000882 snapshot.forceClientComposition =
883 snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000884 snapshot.contentOpaque = snapshot.isContentOpaque();
885 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000886 snapshot.color.a == 1.f;
887 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000888 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000889 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
890 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
891 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
892 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
893 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000894}
895
896void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
897 const RequestedLayerState& requested,
Vishnu Nair0808ae62023-08-07 21:42:42 -0700898 const LayerSnapshot& parentSnapshot,
899 const Args& args) {
900 if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
901 snapshot.roundedCorner = RoundedCornerState();
902 return;
903 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000904 snapshot.roundedCorner = RoundedCornerState();
905 RoundedCornerState parentRoundedCorner;
906 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
907 parentRoundedCorner = parentSnapshot.roundedCorner;
908 ui::Transform t = snapshot.localTransform.inverse();
909 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
910 parentRoundedCorner.radius.x *= t.getScaleX();
911 parentRoundedCorner.radius.y *= t.getScaleY();
912 }
913
914 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
915 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
916 RoundedCornerState layerSettings(layerCropRect, radius);
917 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
918 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
919 if (layerSettingsValid && parentRoundedCornerValid) {
920 // If the parent and the layer have rounded corner settings, use the parent settings if
921 // the parent crop is entirely inside the layer crop. This has limitations and cause
922 // rendering artifacts. See b/200300845 for correct fix.
923 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
924 parentRoundedCorner.cropRect.top > layerCropRect.top &&
925 parentRoundedCorner.cropRect.right < layerCropRect.right &&
926 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
927 snapshot.roundedCorner = parentRoundedCorner;
928 } else {
929 snapshot.roundedCorner = layerSettings;
930 }
931 } else if (layerSettingsValid) {
932 snapshot.roundedCorner = layerSettings;
933 } else if (parentRoundedCornerValid) {
934 snapshot.roundedCorner = parentRoundedCorner;
935 }
936}
937
938void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
939 const RequestedLayerState& requested,
940 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700941 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000942 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000943 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000944 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
945 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000946 auto& t = snapshot.geomLayerTransform;
947 auto& requestedT = requested.requestedTransform;
948 std::string transformDebug =
949 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
950 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
951 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
952 std::string bufferDebug;
953 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700954 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000955 auto& destFrame = requested.destinationFrame;
956 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
957 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
958 requested.externalTexture->getWidth(),
959 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700960 primaryDisplayRotationFlags, destFrame.left,
961 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000962 unRotBuffer.getHeight(), unRotBuffer.getWidth());
963 }
964 ALOGW("Resetting transform for %s because it is invalid.%s%s",
965 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000966 snapshot.geomLayerTransform.reset();
967 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000968 if (transformWasInvalid != snapshot.invalidTransform) {
969 // If transform is invalid, the layer will be hidden.
970 mResortSnapshots = true;
971 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000972 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
973
974 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
975 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
976 snapshot.geomLayerBounds =
977 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
978 if (!requested.crop.isEmpty()) {
979 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
980 }
981 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
982 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000983 const Rect geomLayerBoundsWithoutTransparentRegion =
984 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
985 requested.transparentRegion);
986 snapshot.transformedBoundsWithoutTransparentRegion =
987 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000988 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
989
990 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000991 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000992 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000993 if (requested.potentialCursor) {
994 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
995 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000996}
997
Vishnu Naira02943f2023-06-03 13:44:46 -0700998void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
Vishnu Naird9e4f462023-10-06 04:05:45 +0000999 const ShadowSettings& globalShadowSettings) {
1000 if (snapshot.shadowSettings.length > 0.f) {
1001 snapshot.shadowSettings.ambientColor = globalShadowSettings.ambientColor;
1002 snapshot.shadowSettings.spotColor = globalShadowSettings.spotColor;
1003 snapshot.shadowSettings.lightPos = globalShadowSettings.lightPos;
1004 snapshot.shadowSettings.lightRadius = globalShadowSettings.lightRadius;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001005
1006 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
1007 // it if transparent regions are present. This may not be necessary since shadows are
1008 // typically cast by layers without transparent regions.
1009 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
1010
1011 // If the casting layer is translucent, we need to fill in the shadow underneath the
1012 // layer. Otherwise the generated shadow will only be shown around the casting layer.
1013 snapshot.shadowSettings.casterIsTranslucent =
1014 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1015 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1016 snapshot.shadowSettings.spotColor *= snapshot.alpha;
1017 }
1018}
1019
1020void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1021 const RequestedLayerState& requested,
1022 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +00001023 const LayerHierarchy::TraversalPath& path,
1024 const Args& args) {
1025 if (requested.windowInfoHandle) {
1026 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1027 } else {
1028 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +00001029 // b/271132344 revisit this and see if we can always use the layers uid/pid
1030 snapshot.inputInfo.name = requested.name;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001031 snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001032 snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
Vishnu Naircfb2d252023-01-19 04:44:02 +00001033 }
Vishnu Nair29354ec2023-03-28 18:51:28 -07001034 snapshot.touchCropId = requested.touchCropId;
Vishnu Naircfb2d252023-01-19 04:44:02 +00001035
Vishnu Nair93b8b792023-02-27 19:40:24 +00001036 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +00001037 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Nairf13c8982023-12-02 11:26:09 -08001038 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1039 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1040 : parentSnapshot.inputInfo.touchOcclusionMode;
1041 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1042 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1043 snapshot.dropInputMode = gui::DropInputMode::ALL;
1044 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1045 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1046 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1047 } else {
1048 snapshot.dropInputMode = gui::DropInputMode::NONE;
1049 }
1050
Vishnu Nair29354ec2023-03-28 18:51:28 -07001051 updateVisibility(snapshot, snapshot.isVisible);
Vishnu Naircfb2d252023-01-19 04:44:02 +00001052 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001053 return;
1054 }
1055
Vishnu Naircfb2d252023-01-19 04:44:02 +00001056 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1057 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1058 args.displays.get(snapshot.outputFilter.layerStack);
1059 bool noValidDisplay = !displayInfoOpt.has_value();
1060 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1061
1062 if (!requested.windowInfoHandle) {
1063 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1064 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001065 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1066
1067 if (noValidDisplay) {
1068 // Do not let the window receive touches if it is not associated with a valid display
1069 // transform. We still allow the window to receive keys and prevent ANRs.
1070 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1071 }
1072
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001073 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001074
1075 handleDropInputMode(snapshot, parentSnapshot);
1076
1077 // If the window will be blacked out on a display because the display does not have the secure
1078 // flag and the layer has the secure flag set, then drop input.
1079 if (!displayInfo.isSecure && snapshot.isSecure) {
1080 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1081 }
1082
Vishnu Naira02943f2023-06-03 13:44:46 -07001083 if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001084 mNeedsTouchableRegionCrop.insert(path);
Vishnu Naira02943f2023-06-03 13:44:46 -07001085 }
1086 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1087 if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001088 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
Vishnu Nairfed7c122023-03-18 01:54:43 +00001089 Rect inputBoundsInDisplaySpace =
Vishnu Nair29354ec2023-03-28 18:51:28 -07001090 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1091 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001092 }
1093
1094 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1095 // if it was set by WM for a known system overlay
1096 if (snapshot.isTrustedOverlay) {
1097 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1098 }
1099
Vishnu Nair494a2e42023-11-10 17:21:19 -08001100 snapshot.inputInfo.contentSize = snapshot.croppedBufferSize.getSize();
1101
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001102 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1103 // touches from going outside the cloned area.
1104 if (path.isClone()) {
1105 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair444f3952023-04-11 13:01:02 -07001106 // Cloned layers shouldn't handle watch outside since their z order is not determined by
1107 // WM or the client.
1108 snapshot.inputInfo.inputConfig.clear(gui::WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001109 }
1110}
1111
1112std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1113 return mSnapshots;
1114}
1115
Vishnu Naircfb2d252023-01-19 04:44:02 +00001116void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1117 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1118 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1119 if (!snapshot.isVisible) continue;
1120 visitor(snapshot);
1121 }
1122}
1123
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001124// Visit each visible snapshot in z-order
1125void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1126 const LayerHierarchy& root) const {
1127 root.traverseInZOrder(
1128 [this, visitor](const LayerHierarchy&,
1129 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1130 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1131 if (snapshot && snapshot->isVisible) {
1132 visitor(*snapshot);
1133 }
1134 return true;
1135 });
1136}
1137
Vishnu Naircfb2d252023-01-19 04:44:02 +00001138void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1139 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1140 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1141 if (!snapshot->isVisible) continue;
1142 visitor(snapshot);
1143 }
1144}
1145
1146void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1147 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1148 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1149 if (!snapshot.hasInputInfo()) continue;
1150 visitor(snapshot);
1151 }
1152}
1153
Vishnu Nair29354ec2023-03-28 18:51:28 -07001154void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1155 if (mNeedsTouchableRegionCrop.empty()) {
1156 return;
1157 }
1158
1159 static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1160 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1161 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1162 RequestedLayerState::Changes::Input;
1163
1164 if (args.forceUpdate != ForceUpdateFlags::ALL &&
Vishnu Naira02943f2023-06-03 13:44:46 -07001165 !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
Vishnu Nair29354ec2023-03-28 18:51:28 -07001166 return;
1167 }
1168
1169 for (auto& path : mNeedsTouchableRegionCrop) {
1170 frontend::LayerSnapshot* snapshot = getSnapshot(path);
1171 if (!snapshot) {
1172 continue;
1173 }
Vishnu Naira02943f2023-06-03 13:44:46 -07001174 LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1175 snapshot->getDebugString().c_str());
Vishnu Nair29354ec2023-03-28 18:51:28 -07001176 const std::optional<frontend::DisplayInfo> displayInfoOpt =
1177 args.displays.get(snapshot->outputFilter.layerStack);
1178 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1179 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1180
1181 bool needsUpdate =
1182 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1183 auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1184 needsUpdate =
1185 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1186 auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1187 needsUpdate = needsUpdate ||
1188 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1189
1190 if (!needsUpdate) {
1191 continue;
1192 }
1193
1194 if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1195 Rect inputBoundsInDisplaySpace;
1196 if (!cropLayerSnapshot) {
1197 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1198 inputBoundsInDisplaySpace =
1199 getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1200 } else {
1201 FloatRect inputBounds =
1202 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1203 inputBoundsInDisplaySpace =
1204 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1205 displayInfo.transform);
1206 }
1207 snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1208 } else if (cropLayerSnapshot) {
1209 FloatRect inputBounds =
1210 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1211 Rect inputBoundsInDisplaySpace =
1212 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1213 displayInfo.transform);
1214 snapshot->inputInfo.touchableRegion = snapshot->inputInfo.touchableRegion.intersect(
1215 displayInfo.transform.transform(inputBoundsInDisplaySpace));
1216 }
1217
1218 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1219 // touches from going outside the cloned area.
1220 if (clonedRootSnapshot) {
1221 const Rect rect =
1222 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1223 snapshot->inputInfo.touchableRegion =
1224 snapshot->inputInfo.touchableRegion.intersect(rect);
1225 }
1226 }
1227}
1228
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001229} // namespace android::surfaceflinger::frontend