blob: c7f9aa7b42a640b0021880cf08a5ec5592524a58 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// #define LOG_NDEBUG 0
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
20#define LOG_TAG "LayerSnapshotBuilder"
21
22#include "LayerSnapshotBuilder.h"
23#include <gui/TraceUtils.h>
24#include <numeric>
25#include "DisplayHardware/HWC2.h"
26#include "DisplayHardware/Hal.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000027#include "LayerLog.h"
28#include "TimeStats/TimeStats.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000029#include "ftl/small_map.h"
30
31namespace android::surfaceflinger::frontend {
32
33using namespace ftl::flag_operators;
34
35namespace {
36FloatRect getMaxDisplayBounds(
37 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
38 const ui::Size maxSize = [&displays] {
39 if (displays.empty()) return ui::Size{5000, 5000};
40
41 return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize,
42 [](ui::Size size, const auto& pair) -> ui::Size {
43 const auto& display = pair.second;
44 return {std::max(size.getWidth(), display.info.logicalWidth),
45 std::max(size.getHeight(), display.info.logicalHeight)};
46 });
47 }();
48
49 // Ignore display bounds for now since they will be computed later. Use a large Rect bound
50 // to ensure it's bigger than an actual display will be.
51 const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f;
52 const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f;
53
54 return {-xMax, -yMax, xMax, yMax};
55}
56
57// Applies the given transform to the region, while protecting against overflows caused by any
58// offsets. If applying the offset in the transform to any of the Rects in the region would result
59// in an overflow, they are not added to the output Region.
60Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
61 const std::string& debugWindowName) {
62 // Round the translation using the same rounding strategy used by ui::Transform.
63 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
64 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
65
66 ui::Transform transformWithoutOffset = t;
67 transformWithoutOffset.set(0.f, 0.f);
68
69 const Region transformed = transformWithoutOffset.transform(r);
70
71 // Apply the translation to each of the Rects in the region while discarding any that overflow.
72 Region ret;
73 for (const auto& rect : transformed) {
74 Rect newRect;
75 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
76 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
77 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
78 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
79 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
80 debugWindowName.c_str());
81 continue;
82 }
83 ret.orSelf(newRect);
84 }
85 return ret;
86}
87
88/*
89 * We don't want to send the layer's transform to input, but rather the
90 * parent's transform. This is because Layer's transform is
91 * information about how the buffer is placed on screen. The parent's
92 * transform makes more sense to send since it's information about how the
93 * layer is placed on screen. This transform is used by input to determine
94 * how to go from screen space back to window space.
95 */
96ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
97 if (!snapshot.hasBufferOrSidebandStream()) {
98 return snapshot.geomLayerTransform;
99 }
100 return snapshot.parentTransform;
101}
102
103/**
104 * Similar to getInputTransform, we need to update the bounds to include the transform.
105 * This is because bounds don't include the buffer transform, where the input assumes
106 * that's already included.
107 */
108Rect getInputBounds(const LayerSnapshot& snapshot) {
109 if (!snapshot.hasBufferOrSidebandStream()) {
110 return snapshot.croppedBufferSize;
111 }
112
113 if (snapshot.localTransform.getType() == ui::Transform::IDENTITY ||
114 !snapshot.croppedBufferSize.isValid()) {
115 return snapshot.croppedBufferSize;
116 }
117 return snapshot.localTransform.transform(snapshot.croppedBufferSize);
118}
119
120void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay,
121 const LayerSnapshot& snapshot) {
122 Rect tmpBounds = getInputBounds(snapshot);
123 if (!tmpBounds.isValid()) {
124 info.touchableRegion.clear();
125 // A layer could have invalid input bounds and still expect to receive touch input if it has
126 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
127 // correctly to determine the coordinate space for input events. Use an empty rect so that
128 // the layer will receive input in its own layer space.
129 tmpBounds = Rect::EMPTY_RECT;
130 }
131
132 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
133 // frame and transform used for the layer, which determines the bounds and the coordinate space
134 // within which the layer will receive input.
135 //
136 // The coordinate space within which each of the bounds are specified is explicitly documented
137 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
138 // Transform converts one coordinate space to another, which is apparent in its naming. For
139 // example, "layerToDisplay" transforms layer space to display space.
140 //
141 // Coordinate space definitions:
142 // - display: The display device's coordinate space. Correlates to pixels on the display.
143 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
144 // - layer: The coordinate space of this layer.
145 // - input: The coordinate space in which this layer will receive input events. This could be
146 // different than layer space if a surfaceInset is used, which changes the origin
147 // of the input space.
148 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
149
150 // Clamp surface inset to the input bounds.
151 const auto surfaceInset = static_cast<float>(info.surfaceInset);
152 const float xSurfaceInset =
153 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
154 const float ySurfaceInset =
155 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
156
157 // Apply the insets to the input bounds.
158 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
159 inputBoundsInLayer.top + ySurfaceInset,
160 inputBoundsInLayer.right - xSurfaceInset,
161 inputBoundsInLayer.bottom - ySurfaceInset);
162
163 // Crop the input bounds to ensure it is within the parent's bounds.
164 const FloatRect croppedInsetBoundsInLayer =
165 snapshot.geomLayerBounds.intersect(insetBoundsInLayer);
166
167 const ui::Transform layerToScreen = getInputTransform(snapshot);
168 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
169
170 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
171 info.frameLeft = roundedFrameInDisplay.left;
172 info.frameTop = roundedFrameInDisplay.top;
173 info.frameRight = roundedFrameInDisplay.right;
174 info.frameBottom = roundedFrameInDisplay.bottom;
175
176 ui::Transform inputToLayer;
177 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
178 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
179
180 // InputDispatcher expects a display-to-input transform.
181 info.transform = inputToDisplay.inverse();
182
183 // The touchable region is specified in the input coordinate space. Change it to display space.
184 info.touchableRegion =
185 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
186}
187
188void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
189 if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
190 return;
191 }
192
193 // Check if we need to drop input unconditionally
194 const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
195 if (dropInputMode == gui::DropInputMode::ALL) {
196 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
197 ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
198 return;
199 }
200
201 // Check if we need to check if the window is obscured by parent
202 if (dropInputMode != gui::DropInputMode::OBSCURED) {
203 return;
204 }
205
206 // Check if the parent has set an alpha on the layer
207 if (parentSnapshot.color.a != 1.0_hf) {
208 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
209 ALOGV("Dropping input for %s as requested by policy because alpha=%f",
210 snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
211 }
212
213 // Check if the parent has cropped the buffer
214 Rect bufferSize = snapshot.croppedBufferSize;
215 if (!bufferSize.isValid()) {
216 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
217 return;
218 }
219
220 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
221 // To check if the layer has been cropped, we take the buffer bounds, apply the local
222 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
223 // match then the layer has not been cropped by its parents.
224 Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
225 bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
226
227 if (croppedByParent) {
228 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
229 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
230 snapshot.name.c_str());
231 } else {
232 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
233 // input if the window is obscured. This check should be done in surfaceflinger but the
234 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
235 // drop input events if the window is obscured.
236 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
237 }
238}
239
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000240auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
241 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
242 if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
243 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
244 : Hwc2::IComposerClient::BlendMode::COVERAGE;
245 }
246 return blendMode;
247}
248
Vishnu Naircfb2d252023-01-19 04:44:02 +0000249void updateSurfaceDamage(const RequestedLayerState& requested, bool hasReadyFrame,
250 bool forceFullDamage, Region& outSurfaceDamageRegion) {
251 if (!hasReadyFrame) {
252 outSurfaceDamageRegion.clear();
253 return;
254 }
255 if (forceFullDamage) {
256 outSurfaceDamageRegion = Region::INVALID_REGION;
257 } else {
258 outSurfaceDamageRegion = requested.surfaceDamageRegion;
259 }
260}
261
Vishnu Nair80a5a702023-02-11 01:21:51 +0000262void updateVisibility(LayerSnapshot& snapshot, bool visible) {
263 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000264
265 // TODO(b/238781169) we are ignoring this compat for now, since we will have
266 // to remove any optimization based on visibility.
267
268 // For compatibility reasons we let layers which can receive input
269 // receive input before they have actually submitted a buffer. Because
270 // of this we use canReceiveInput instead of isVisible to check the
271 // policy-visibility, ignoring the buffer state. However for layers with
272 // hasInputInfo()==false we can use the real visibility state.
273 // We are just using these layers for occlusion detection in
274 // InputDispatcher, and obviously if they aren't visible they can't occlude
275 // anything.
Vishnu Nair80a5a702023-02-11 01:21:51 +0000276 const bool visibleForInput =
Vishnu Naircfb2d252023-01-19 04:44:02 +0000277 (snapshot.inputInfo.token != nullptr) ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000278 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000279}
280
281bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
282 if (requested.potentialCursor) {
283 return false;
284 }
285
286 if (snapshot.inputInfo.token != nullptr) {
287 return true;
288 }
289
290 if (snapshot.hasBufferOrSidebandStream()) {
291 return true;
292 }
293
294 return requested.windowInfoHandle &&
295 requested.windowInfoHandle->getInfo()->inputConfig.test(
296 gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
297}
298
299void clearChanges(LayerSnapshot& snapshot) {
300 snapshot.changes.clear();
301 snapshot.contentDirty = false;
302 snapshot.hasReadyFrame = false;
303 snapshot.sidebandStreamHasFrame = false;
304 snapshot.surfaceDamage.clear();
305}
306
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000307} // namespace
308
309LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
310 LayerSnapshot snapshot;
311 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
312 snapshot.isHiddenByPolicyFromParent = false;
313 snapshot.isHiddenByPolicyFromRelativeParent = false;
314 snapshot.parentTransform.reset();
315 snapshot.geomLayerTransform.reset();
316 snapshot.geomInverseLayerTransform.reset();
317 snapshot.geomLayerBounds = getMaxDisplayBounds({});
318 snapshot.roundedCorner = RoundedCornerState();
319 snapshot.stretchEffect = {};
320 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
321 snapshot.outputFilter.toInternalDisplay = false;
322 snapshot.isSecure = false;
323 snapshot.color.a = 1.0_hf;
324 snapshot.colorTransformIsIdentity = true;
325 snapshot.shadowRadius = 0.f;
326 snapshot.layerMetadata.mMap.clear();
327 snapshot.relativeLayerMetadata.mMap.clear();
328 snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
329 snapshot.dropInputMode = gui::DropInputMode::NONE;
330 snapshot.isTrustedOverlay = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000331 snapshot.gameMode = gui::GameMode::Unsupported;
332 snapshot.frameRate = {};
333 snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000334 return snapshot;
335}
336
337LayerSnapshotBuilder::LayerSnapshotBuilder() : mRootSnapshot(getRootSnapshot()) {}
338
339LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000340 args.forceUpdate = ForceUpdateFlags::ALL;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000341 updateSnapshots(args);
342}
343
344bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000345 if (args.forceUpdate != ForceUpdateFlags::NONE || args.displayChanges) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000346 // force update requested, or we have display changes, so skip the fast path
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000347 return false;
348 }
349
350 if (args.layerLifecycleManager.getGlobalChanges().get() == 0) {
351 // there are no changes, so just clear the change flags from before.
352 for (auto& snapshot : mSnapshots) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000353 clearChanges(*snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000354 }
355 return true;
356 }
357
358 if (args.layerLifecycleManager.getGlobalChanges() != RequestedLayerState::Changes::Content) {
359 // We have changes that require us to walk the hierarchy and update child layers.
360 // No fast path for you.
361 return false;
362 }
363
364 // There are only content changes which do not require any child layer snapshots to be updated.
365 ALOGV("%s", __func__);
366 ATRACE_NAME("FastPath");
367
368 // Collect layers with changes
369 ftl::SmallMap<uint32_t, RequestedLayerState*, 10> layersWithChanges;
370 for (auto& layer : args.layerLifecycleManager.getLayers()) {
371 if (layer->changes.test(RequestedLayerState::Changes::Content)) {
372 layersWithChanges.emplace_or_replace(layer->id, layer.get());
373 }
374 }
375
376 // Walk through the snapshots, clearing previous change flags and updating the snapshots
377 // if needed.
378 for (auto& snapshot : mSnapshots) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000379 clearChanges(*snapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000380 auto it = layersWithChanges.find(snapshot->path.id);
381 if (it != layersWithChanges.end()) {
382 ALOGV("%s fast path snapshot changes = %s", __func__,
383 mRootSnapshot.changes.string().c_str());
384 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000385 updateSnapshot(*snapshot, args, *it->second, mRootSnapshot, root,
386 /*newSnapshot=*/false);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000387 }
388 }
389 return true;
390}
391
392void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
393 ATRACE_NAME("UpdateSnapshots");
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000394 if (args.parentCrop) {
395 mRootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000396 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000397 mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
398 }
399 if (args.displayChanges) {
400 mRootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
401 RequestedLayerState::Changes::Geometry;
402 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000403 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
404 mRootSnapshot.changes |=
405 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
406 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000407 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000408 if (args.root.getLayer()) {
409 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
410 // multiple children.
411 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
412 LayerHierarchy::Variant::Attached);
413 updateSnapshotsInHierarchy(args, args.root, root, mRootSnapshot);
414 } else {
415 for (auto& [childHierarchy, variant] : args.root.mChildren) {
416 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
417 childHierarchy->getLayer()->id,
418 variant);
419 updateSnapshotsInHierarchy(args, *childHierarchy, root, mRootSnapshot);
420 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000421 }
422
423 sortSnapshotsByZ(args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000424 clearChanges(mRootSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000425
426 // Destroy unreachable snapshots
427 if (args.layerLifecycleManager.getDestroyedLayers().empty()) {
428 return;
429 }
430
431 std::unordered_set<uint32_t> destroyedLayerIds;
432 for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) {
433 destroyedLayerIds.emplace(destroyedLayer->id);
434 }
435 auto it = mSnapshots.begin();
436 while (it < mSnapshots.end()) {
437 auto& traversalPath = it->get()->path;
438 if (destroyedLayerIds.find(traversalPath.id) == destroyedLayerIds.end()) {
439 it++;
440 continue;
441 }
442
443 mIdToSnapshot.erase(traversalPath);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000444 mSnapshots.back()->globalZ = it->get()->globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000445 std::iter_swap(it, mSnapshots.end() - 1);
446 mSnapshots.erase(mSnapshots.end() - 1);
447 }
448}
449
450void LayerSnapshotBuilder::update(const Args& args) {
451 if (tryFastUpdate(args)) {
452 return;
453 }
454 updateSnapshots(args);
455}
456
Vishnu Naircfb2d252023-01-19 04:44:02 +0000457const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
458 const Args& args, const LayerHierarchy& hierarchy,
459 LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000460 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000461 LayerSnapshot* snapshot = getSnapshot(traversalPath);
462 const bool newSnapshot = snapshot == nullptr;
463 if (newSnapshot) {
464 snapshot = createSnapshot(traversalPath, *layer);
465 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000466 scheduler::LayerInfo::FrameRate oldFrameRate = snapshot->frameRate;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000467 if (traversalPath.isRelative()) {
468 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
469 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
470 } else {
471 if (traversalPath.isAttached()) {
472 resetRelativeState(*snapshot);
473 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000474 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath, newSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000475 }
476
477 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
478 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
479 childHierarchy->getLayer()->id,
480 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000481 const LayerSnapshot& childSnapshot =
482 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot);
483 updateChildState(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000484 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000485
486 if (oldFrameRate == snapshot->frameRate) {
487 snapshot->changes.clear(RequestedLayerState::Changes::FrameRate);
488 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000489 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000490}
491
492LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
493 if (layerId == UNASSIGNED_LAYER_ID) {
494 return nullptr;
495 }
496 LayerHierarchy::TraversalPath path{.id = layerId};
497 return getSnapshot(path);
498}
499
500LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
501 auto it = mIdToSnapshot.find(id);
502 return it == mIdToSnapshot.end() ? nullptr : it->second;
503}
504
Vishnu Naircfb2d252023-01-19 04:44:02 +0000505LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& id,
506 const RequestedLayerState& layer) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000507 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, id));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000508 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000509 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
510 mIdToSnapshot[id] = snapshot;
511 return snapshot;
512}
513
514void LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000515 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000516 !args.layerLifecycleManager.getGlobalChanges().any(
517 RequestedLayerState::Changes::Hierarchy |
518 RequestedLayerState::Changes::Visibility)) {
519 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
520 // the snapshots.
521 return;
522 }
523
Vishnu Naircfb2d252023-01-19 04:44:02 +0000524 mResortSnapshots = false;
525
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000526 size_t globalZ = 0;
527 args.root.traverseInZOrder(
528 [this, &globalZ](const LayerHierarchy&,
529 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
530 LayerSnapshot* snapshot = getSnapshot(traversalPath);
531 if (!snapshot) {
532 return false;
533 }
534
535 if (snapshot->isHiddenByPolicy() &&
536 !snapshot->changes.test(RequestedLayerState::Changes::Visibility)) {
537 return false;
538 }
539
Vishnu Naircfb2d252023-01-19 04:44:02 +0000540 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000541 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000542 size_t oldZ = snapshot->globalZ;
543 size_t newZ = globalZ++;
544 snapshot->globalZ = newZ;
545 if (oldZ == newZ) {
546 return true;
547 }
548 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000549 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
550 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000551 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
552 mSnapshots.begin() + static_cast<ssize_t>(newZ));
553 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000554 return true;
555 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000556 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000557 while (globalZ < mSnapshots.size()) {
558 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000559 /* mark unreachable snapshots as explicitly invisible */
560 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000561 globalZ++;
562 }
563}
564
565void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
566 const LayerSnapshot& parentSnapshot,
567 bool parentIsRelative, const Args& args) {
568 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000569 snapshot.isHiddenByPolicyFromRelativeParent =
570 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000571 if (args.includeMetadata) {
572 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
573 }
574 } else {
575 snapshot.isHiddenByPolicyFromRelativeParent =
576 parentSnapshot.isHiddenByPolicyFromRelativeParent;
577 if (args.includeMetadata) {
578 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
579 }
580 }
581 snapshot.isVisible = snapshot.getIsVisible();
582}
583
Vishnu Naircfb2d252023-01-19 04:44:02 +0000584void LayerSnapshotBuilder::updateChildState(LayerSnapshot& snapshot,
585 const LayerSnapshot& childSnapshot, const Args& args) {
586 if (snapshot.childState.hasValidFrameRate) {
587 return;
588 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000589 if (args.forceUpdate == ForceUpdateFlags::ALL ||
590 childSnapshot.changes.test(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000591 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes
592 // for the same reason we are allowing touch boost for those layers. See
593 // RefreshRateSelector::rankFrameRates for details.
594 using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility;
595 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.rate.isValid() &&
596 childSnapshot.frameRate.type == FrameRateCompatibility::Default;
597 const auto layerVotedWithNoVote =
598 childSnapshot.frameRate.type == FrameRateCompatibility::NoVote;
599 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.rate.isValid() &&
600 childSnapshot.frameRate.type == FrameRateCompatibility::Exact;
601
602 snapshot.childState.hasValidFrameRate |= layerVotedWithDefaultCompatibility ||
603 layerVotedWithNoVote || layerVotedWithExactCompatibility;
604
605 // If we don't have a valid frame rate, but the children do, we set this
606 // layer as NoVote to allow the children to control the refresh rate
607 if (!snapshot.frameRate.rate.isValid() &&
608 snapshot.frameRate.type != FrameRateCompatibility::NoVote &&
609 snapshot.childState.hasValidFrameRate) {
610 snapshot.frameRate =
611 scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
612 snapshot.changes |= childSnapshot.changes & RequestedLayerState::Changes::FrameRate;
613 }
614 }
615}
616
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000617void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
618 snapshot.isHiddenByPolicyFromRelativeParent = false;
619 snapshot.relativeLayerMetadata.mMap.clear();
620}
621
622uint32_t getDisplayRotationFlags(
623 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays,
624 const ui::LayerStack& layerStack) {
625 static frontend::DisplayInfo sDefaultDisplayInfo = {.isPrimary = false};
626 auto display = displays.get(layerStack).value_or(sDefaultDisplayInfo).get();
627 return display.isPrimary ? display.rotationFlags : 0;
628}
629
630void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
631 const RequestedLayerState& requested,
632 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000633 const LayerHierarchy::TraversalPath& path,
634 bool newSnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000635 // Always update flags and visibility
636 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
637 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
638 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000639 RequestedLayerState::Changes::AffectsChildren |
640 RequestedLayerState::Changes::FrameRate);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000641 snapshot.changes = parentChanges | requested.changes;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000642 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000643 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
644 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000645 snapshot.contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000646 // TODO(b/238781169) scope down the changes to only buffer updates.
Vishnu Naird47bcee2023-02-24 18:08:51 +0000647 snapshot.hasReadyFrame = requested.hasReadyFrame();
648 snapshot.sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000649 updateSurfaceDamage(requested, snapshot.hasReadyFrame, args.forceFullDamage,
650 snapshot.surfaceDamage);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000651
Vishnu Naird47bcee2023-02-24 18:08:51 +0000652 const bool forceUpdate = newSnapshot || args.forceUpdate == ForceUpdateFlags::ALL ||
Vishnu Naircfb2d252023-01-19 04:44:02 +0000653 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
654 RequestedLayerState::Changes::Created);
Vishnu Nair80a5a702023-02-11 01:21:51 +0000655 snapshot.outputFilter.layerStack = requested.parentId != UNASSIGNED_LAYER_ID
656 ? parentSnapshot.outputFilter.layerStack
657 : requested.layerStack;
658
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000659 uint32_t displayRotationFlags =
660 getDisplayRotationFlags(args.displays, snapshot.outputFilter.layerStack);
661
Vishnu Naircfb2d252023-01-19 04:44:02 +0000662 // always update the buffer regardless of visibility
Vishnu Nair80a5a702023-02-11 01:21:51 +0000663 if (forceUpdate || requested.what & layer_state_t::BUFFER_CHANGES || args.displayChanges) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000664 snapshot.acquireFence =
665 (requested.externalTexture &&
666 requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged))
667 ? requested.bufferData->acquireFence
668 : Fence::NO_FENCE;
669 snapshot.buffer =
670 requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr;
671 snapshot.bufferSize = requested.getBufferSize(displayRotationFlags);
672 snapshot.geomBufferSize = snapshot.bufferSize;
673 snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize);
674 snapshot.dataspace = requested.dataspace;
675 snapshot.externalTexture = requested.externalTexture;
676 snapshot.frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0;
677 snapshot.geomBufferTransform = requested.bufferTransform;
678 snapshot.geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse;
679 snapshot.geomContentCrop = requested.getBufferCrop();
680 snapshot.geomUsesSourceCrop = snapshot.hasBufferOrSidebandStream();
681 snapshot.hasProtectedContent = requested.externalTexture &&
682 requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
683 snapshot.isHdrY410 = requested.dataspace == ui::Dataspace::BT2020_ITU_PQ &&
684 requested.api == NATIVE_WINDOW_API_MEDIA &&
685 requested.bufferData->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102;
686 snapshot.sidebandStream = requested.sidebandStream;
687 snapshot.transparentRegionHint = requested.transparentRegion;
688 snapshot.color.rgb = requested.getColor().rgb;
John Reck68796592023-01-25 13:47:12 -0500689 snapshot.currentSdrHdrRatio = requested.currentSdrHdrRatio;
690 snapshot.desiredSdrHdrRatio = requested.desiredSdrHdrRatio;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000691 }
692
693 if (snapshot.isHiddenByPolicyFromParent && !newSnapshot) {
694 if (forceUpdate ||
695 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
696 RequestedLayerState::Changes::Geometry |
697 RequestedLayerState::Changes::Input)) {
698 updateInput(snapshot, requested, parentSnapshot, path, args);
699 }
700 return;
701 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000702
703 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren)) {
704 // If root layer, use the layer stack otherwise get the parent's layer stack.
705 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
706 snapshot.alpha = snapshot.color.a;
707 snapshot.isSecure =
708 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
709 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
710 snapshot.outputFilter.layerStack = requested.parentId != UNASSIGNED_LAYER_ID
711 ? parentSnapshot.outputFilter.layerStack
712 : requested.layerStack;
713 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
714 (requested.flags & layer_state_t::eLayerSkipScreenshot);
715 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
716 ? requested.stretchEffect
717 : parentSnapshot.stretchEffect;
718 if (!parentSnapshot.colorTransformIsIdentity) {
719 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
720 snapshot.colorTransformIsIdentity = false;
721 } else {
722 snapshot.colorTransform = requested.colorTransform;
723 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
724 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000725 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
726 ? requested.gameMode
727 : parentSnapshot.gameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000728 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
729 ? requested.fixedTransformHint
730 : parentSnapshot.fixedTransformHint;
Vishnu Naira9c43762023-01-27 19:10:25 +0000731 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
732 // marked as skip capture
733 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
734 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000735 }
736
Vishnu Naird47bcee2023-02-24 18:08:51 +0000737 if (forceUpdate ||
738 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
739 RequestedLayerState::Changes::Hierarchy)) {
740 snapshot.frameRate = (requested.requestedFrameRate.rate.isValid() ||
741 (requested.requestedFrameRate.type ==
742 scheduler::LayerInfo::FrameRateCompatibility::NoVote))
743 ? requested.requestedFrameRate
744 : parentSnapshot.frameRate;
745 }
746
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000747 if (forceUpdate || requested.changes.get() != 0) {
748 snapshot.compositionType = requested.getCompositionType();
749 snapshot.dimmingEnabled = requested.dimmingEnabled;
750 snapshot.layerOpaqueFlagSet =
751 (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
Alec Mourif4af03e2023-02-11 00:25:24 +0000752 snapshot.cachingHint = requested.cachingHint;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000753 }
754
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000755 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Content)) {
756 snapshot.color.rgb = requested.getColor().rgb;
757 snapshot.isColorspaceAgnostic = requested.colorSpaceAgnostic;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000758 snapshot.backgroundBlurRadius = args.supportsBlur
759 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
760 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000761 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000762 for (auto& region : snapshot.blurRegions) {
763 region.alpha = region.alpha * snapshot.color.a;
764 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000765 snapshot.hdrMetadata = requested.hdrMetadata;
766 }
767
768 if (forceUpdate ||
769 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
770 RequestedLayerState::Changes::Geometry)) {
771 updateLayerBounds(snapshot, requested, parentSnapshot, displayRotationFlags);
772 updateRoundedCorner(snapshot, requested, parentSnapshot);
773 }
774
775 if (forceUpdate ||
776 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
777 RequestedLayerState::Changes::Geometry |
778 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000779 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000780 }
781
782 // computed snapshot properties
783 updateShadows(snapshot, requested, args.globalShadowSettings);
784 if (args.includeMetadata) {
785 snapshot.layerMetadata = parentSnapshot.layerMetadata;
786 snapshot.layerMetadata.merge(requested.metadata);
787 }
788 snapshot.forceClientComposition = snapshot.isHdrY410 || snapshot.shadowSettings.length > 0 ||
789 requested.blurRegions.size() > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000790 snapshot.isOpaque = snapshot.isContentOpaque() && !snapshot.roundedCorner.hasRoundedCorners() &&
791 snapshot.color.a == 1.f;
792 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000793 // TODO(b/238781169) pass this from flinger
794 // snapshot.fps;
795 // snapshot.metadata;
796 LLOGV(snapshot.sequence,
797 "%supdated [%d]%s changes parent:%s global:%s local:%s requested:%s %s from parent %s",
Vishnu Naird47bcee2023-02-24 18:08:51 +0000798 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "", requested.id,
799 requested.name.c_str(), parentSnapshot.changes.string().c_str(),
800 snapshot.changes.string().c_str(), requested.changes.string().c_str(),
801 std::to_string(requested.what).c_str(), snapshot.getDebugString().c_str(),
802 parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000803}
804
805void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
806 const RequestedLayerState& requested,
807 const LayerSnapshot& parentSnapshot) {
808 snapshot.roundedCorner = RoundedCornerState();
809 RoundedCornerState parentRoundedCorner;
810 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
811 parentRoundedCorner = parentSnapshot.roundedCorner;
812 ui::Transform t = snapshot.localTransform.inverse();
813 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
814 parentRoundedCorner.radius.x *= t.getScaleX();
815 parentRoundedCorner.radius.y *= t.getScaleY();
816 }
817
818 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
819 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
820 RoundedCornerState layerSettings(layerCropRect, radius);
821 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
822 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
823 if (layerSettingsValid && parentRoundedCornerValid) {
824 // If the parent and the layer have rounded corner settings, use the parent settings if
825 // the parent crop is entirely inside the layer crop. This has limitations and cause
826 // rendering artifacts. See b/200300845 for correct fix.
827 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
828 parentRoundedCorner.cropRect.top > layerCropRect.top &&
829 parentRoundedCorner.cropRect.right < layerCropRect.right &&
830 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
831 snapshot.roundedCorner = parentRoundedCorner;
832 } else {
833 snapshot.roundedCorner = layerSettings;
834 }
835 } else if (layerSettingsValid) {
836 snapshot.roundedCorner = layerSettings;
837 } else if (parentRoundedCornerValid) {
838 snapshot.roundedCorner = parentRoundedCorner;
839 }
840}
841
842void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
843 const RequestedLayerState& requested,
844 const LayerSnapshot& parentSnapshot,
845 uint32_t displayRotationFlags) {
846 snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize);
847 snapshot.geomCrop = requested.crop;
848 snapshot.localTransform = requested.getTransform(displayRotationFlags);
849 snapshot.localTransformInverse = snapshot.localTransform.inverse();
850 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000851 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000852 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
853 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000854 auto& t = snapshot.geomLayerTransform;
855 auto& requestedT = requested.requestedTransform;
856 std::string transformDebug =
857 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
858 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
859 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
860 std::string bufferDebug;
861 if (requested.externalTexture) {
862 auto unRotBuffer = requested.getUnrotatedBufferSize(displayRotationFlags);
863 auto& destFrame = requested.destinationFrame;
864 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
865 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
866 requested.externalTexture->getWidth(),
867 requested.externalTexture->getHeight(),
868 displayRotationFlags, destFrame.left, destFrame.top,
869 destFrame.right, destFrame.bottom,
870 unRotBuffer.getHeight(), unRotBuffer.getWidth());
871 }
872 ALOGW("Resetting transform for %s because it is invalid.%s%s",
873 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000874 snapshot.geomLayerTransform.reset();
875 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000876 if (transformWasInvalid != snapshot.invalidTransform) {
877 // If transform is invalid, the layer will be hidden.
878 mResortSnapshots = true;
879 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000880 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
881
882 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
883 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
884 snapshot.geomLayerBounds =
885 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
886 if (!requested.crop.isEmpty()) {
887 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
888 }
889 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
890 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000891 const Rect geomLayerBoundsWithoutTransparentRegion =
892 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
893 requested.transparentRegion);
894 snapshot.transformedBoundsWithoutTransparentRegion =
895 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000896 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
897
898 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000899 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000900 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000901 if (requested.potentialCursor) {
902 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
903 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000904}
905
906void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot,
907 const RequestedLayerState& requested,
908 const renderengine::ShadowSettings& globalShadowSettings) {
909 snapshot.shadowRadius = requested.shadowRadius;
910 snapshot.shadowSettings.length = requested.shadowRadius;
911 if (snapshot.shadowRadius > 0.f) {
912 snapshot.shadowSettings = globalShadowSettings;
913
914 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
915 // it if transparent regions are present. This may not be necessary since shadows are
916 // typically cast by layers without transparent regions.
917 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
918
919 // If the casting layer is translucent, we need to fill in the shadow underneath the
920 // layer. Otherwise the generated shadow will only be shown around the casting layer.
921 snapshot.shadowSettings.casterIsTranslucent =
922 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
923 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
924 snapshot.shadowSettings.spotColor *= snapshot.alpha;
925 }
926}
927
928void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
929 const RequestedLayerState& requested,
930 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000931 const LayerHierarchy::TraversalPath& path,
932 const Args& args) {
933 if (requested.windowInfoHandle) {
934 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
935 } else {
936 snapshot.inputInfo = {};
937 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000938
Vishnu Naird47bcee2023-02-24 18:08:51 +0000939 snapshot.inputInfo.name = requested.name;
940 snapshot.inputInfo.id = static_cast<int32_t>(requested.id);
941 snapshot.inputInfo.ownerUid = static_cast<int32_t>(requested.ownerUid);
942 snapshot.inputInfo.ownerPid = requested.ownerPid;
943 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000944 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000945 return;
946 }
947
Vishnu Naircfb2d252023-01-19 04:44:02 +0000948 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
949 const std::optional<frontend::DisplayInfo> displayInfoOpt =
950 args.displays.get(snapshot.outputFilter.layerStack);
951 bool noValidDisplay = !displayInfoOpt.has_value();
952 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
953
954 if (!requested.windowInfoHandle) {
955 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
956 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000957 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
958
959 if (noValidDisplay) {
960 // Do not let the window receive touches if it is not associated with a valid display
961 // transform. We still allow the window to receive keys and prevent ANRs.
962 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
963 }
964
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000965 snapshot.inputInfo.alpha = snapshot.color.a;
966 snapshot.inputInfo.touchOcclusionMode = parentSnapshot.inputInfo.touchOcclusionMode;
967 if (requested.dropInputMode == gui::DropInputMode::ALL ||
968 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
969 snapshot.dropInputMode = gui::DropInputMode::ALL;
970 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
971 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
972 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
973 } else {
974 snapshot.dropInputMode = gui::DropInputMode::NONE;
975 }
976
977 handleDropInputMode(snapshot, parentSnapshot);
978
979 // If the window will be blacked out on a display because the display does not have the secure
980 // flag and the layer has the secure flag set, then drop input.
981 if (!displayInfo.isSecure && snapshot.isSecure) {
982 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
983 }
984
985 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
986 if (snapshot.inputInfo.replaceTouchableRegionWithCrop) {
987 const Rect bounds(cropLayerSnapshot ? cropLayerSnapshot->transformedBounds
988 : snapshot.transformedBounds);
989 snapshot.inputInfo.touchableRegion = Region(displayInfo.transform.transform(bounds));
990 } else if (cropLayerSnapshot) {
991 snapshot.inputInfo.touchableRegion = snapshot.inputInfo.touchableRegion.intersect(
992 displayInfo.transform.transform(Rect{cropLayerSnapshot->transformedBounds}));
993 }
994
995 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
996 // if it was set by WM for a known system overlay
997 if (snapshot.isTrustedOverlay) {
998 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
999 }
1000
1001 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1002 // touches from going outside the cloned area.
1003 if (path.isClone()) {
1004 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair80a5a702023-02-11 01:21:51 +00001005 auto clonedRootSnapshot = getSnapshot(path.getMirrorRoot());
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001006 if (clonedRootSnapshot) {
1007 const Rect rect =
1008 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1009 snapshot.inputInfo.touchableRegion = snapshot.inputInfo.touchableRegion.intersect(rect);
1010 }
1011 }
1012}
1013
1014std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1015 return mSnapshots;
1016}
1017
Vishnu Naircfb2d252023-01-19 04:44:02 +00001018void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1019 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1020 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1021 if (!snapshot.isVisible) continue;
1022 visitor(snapshot);
1023 }
1024}
1025
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001026// Visit each visible snapshot in z-order
1027void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1028 const LayerHierarchy& root) const {
1029 root.traverseInZOrder(
1030 [this, visitor](const LayerHierarchy&,
1031 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1032 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1033 if (snapshot && snapshot->isVisible) {
1034 visitor(*snapshot);
1035 }
1036 return true;
1037 });
1038}
1039
Vishnu Naircfb2d252023-01-19 04:44:02 +00001040void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1041 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1042 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1043 if (!snapshot->isVisible) continue;
1044 visitor(snapshot);
1045 }
1046}
1047
1048void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1049 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1050 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1051 if (!snapshot.hasInputInfo()) continue;
1052 visitor(snapshot);
1053 }
1054}
1055
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001056} // namespace android::surfaceflinger::frontend