1 /*
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 "SurfaceFlinger"
21 
22 #include <numeric>
23 #include <optional>
24 
25 #include <common/FlagManager.h>
26 #include <ftl/small_map.h>
27 #include <gui/TraceUtils.h>
28 #include <ui/DisplayMap.h>
29 #include <ui/FloatRect.h>
30 
31 #include "DisplayHardware/HWC2.h"
32 #include "DisplayHardware/Hal.h"
33 #include "Layer.h" // eFrameRateSelectionPriority constants
34 #include "LayerLog.h"
35 #include "LayerSnapshotBuilder.h"
36 #include "TimeStats/TimeStats.h"
37 #include "Tracing/TransactionTracing.h"
38 
39 namespace android::surfaceflinger::frontend {
40 
41 using namespace ftl::flag_operators;
42 
43 namespace {
44 
getMaxDisplayBounds(const DisplayInfos & displays)45 FloatRect getMaxDisplayBounds(const DisplayInfos& displays) {
46     const ui::Size maxSize = [&displays] {
47         if (displays.empty()) return ui::Size{5000, 5000};
48 
49         return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize,
50                                [](ui::Size size, const auto& pair) -> ui::Size {
51                                    const auto& display = pair.second;
52                                    return {std::max(size.getWidth(), display.info.logicalWidth),
53                                            std::max(size.getHeight(), display.info.logicalHeight)};
54                                });
55     }();
56 
57     // Ignore display bounds for now since they will be computed later. Use a large Rect bound
58     // to ensure it's bigger than an actual display will be.
59     const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f;
60     const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f;
61 
62     return {-xMax, -yMax, xMax, yMax};
63 }
64 
65 // Applies the given transform to the region, while protecting against overflows caused by any
66 // offsets. If applying the offset in the transform to any of the Rects in the region would result
67 // in an overflow, they are not added to the output Region.
transformTouchableRegionSafely(const ui::Transform & t,const Region & r,const std::string & debugWindowName)68 Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
69                                       const std::string& debugWindowName) {
70     // Round the translation using the same rounding strategy used by ui::Transform.
71     const auto tx = static_cast<int32_t>(t.tx() + 0.5);
72     const auto ty = static_cast<int32_t>(t.ty() + 0.5);
73 
74     ui::Transform transformWithoutOffset = t;
75     transformWithoutOffset.set(0.f, 0.f);
76 
77     const Region transformed = transformWithoutOffset.transform(r);
78 
79     // Apply the translation to each of the Rects in the region while discarding any that overflow.
80     Region ret;
81     for (const auto& rect : transformed) {
82         Rect newRect;
83         if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
84             __builtin_add_overflow(rect.top, ty, &newRect.top) ||
85             __builtin_add_overflow(rect.right, tx, &newRect.right) ||
86             __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
87             ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
88                   debugWindowName.c_str());
89             continue;
90         }
91         ret.orSelf(newRect);
92     }
93     return ret;
94 }
95 
96 /*
97  * We don't want to send the layer's transform to input, but rather the
98  * parent's transform. This is because Layer's transform is
99  * information about how the buffer is placed on screen. The parent's
100  * transform makes more sense to send since it's information about how the
101  * layer is placed on screen. This transform is used by input to determine
102  * how to go from screen space back to window space.
103  */
getInputTransform(const LayerSnapshot & snapshot)104 ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
105     if (!snapshot.hasBufferOrSidebandStream()) {
106         return snapshot.geomLayerTransform;
107     }
108     return snapshot.parentTransform;
109 }
110 
111 /**
112  * Returns the bounds used to fill the input frame and the touchable region.
113  *
114  * Similar to getInputTransform, we need to update the bounds to include the transform.
115  * This is because bounds don't include the buffer transform, where the input assumes
116  * that's already included.
117  */
getInputBounds(const LayerSnapshot & snapshot,bool fillParentBounds)118 std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) {
119     FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect();
120     if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() &&
121         snapshot.localTransform.getType() != ui::Transform::IDENTITY) {
122         inputBounds = snapshot.localTransform.transform(inputBounds);
123     }
124 
125     bool inputBoundsValid = snapshot.croppedBufferSize.isValid();
126     if (!inputBoundsValid) {
127         /**
128          * Input bounds are based on the layer crop or buffer size. But if we are using
129          * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then
130          * we can use the parent bounds as the input bounds if the layer does not have buffer
131          * or a crop. We want to unify this logic but because of compat reasons we cannot always
132          * use the parent bounds. A layer without a buffer can get input. So when a window is
133          * initially added, its touchable region can fill its parent layer bounds and that can
134          * have negative consequences.
135          */
136         inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{};
137     }
138 
139     // Clamp surface inset to the input bounds.
140     const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset);
141     const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f);
142     const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f);
143 
144     // Apply the insets to the input bounds.
145     inputBounds.left += xSurfaceInset;
146     inputBounds.top += ySurfaceInset;
147     inputBounds.right -= xSurfaceInset;
148     inputBounds.bottom -= ySurfaceInset;
149     return {inputBounds, inputBoundsValid};
150 }
151 
getInputBoundsInDisplaySpace(const LayerSnapshot & snapshot,const FloatRect & insetBounds,const ui::Transform & screenToDisplay)152 Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds,
153                                   const ui::Transform& screenToDisplay) {
154     // InputDispatcher works in the display device's coordinate space. Here, we calculate the
155     // frame and transform used for the layer, which determines the bounds and the coordinate space
156     // within which the layer will receive input.
157 
158     // Coordinate space definitions:
159     //   - display: The display device's coordinate space. Correlates to pixels on the display.
160     //   - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
161     //   - layer: The coordinate space of this layer.
162     //   - input: The coordinate space in which this layer will receive input events. This could be
163     //            different than layer space if a surfaceInset is used, which changes the origin
164     //            of the input space.
165 
166     // Crop the input bounds to ensure it is within the parent's bounds.
167     const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds);
168 
169     const ui::Transform layerToScreen = getInputTransform(snapshot);
170     const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
171 
172     return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)};
173 }
174 
fillInputFrameInfo(gui::WindowInfo & info,const ui::Transform & screenToDisplay,const LayerSnapshot & snapshot)175 void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay,
176                         const LayerSnapshot& snapshot) {
177     auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false);
178     if (!inputBoundsValid) {
179         info.touchableRegion.clear();
180     }
181 
182     info.frame = getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
183 
184     ui::Transform inputToLayer;
185     inputToLayer.set(inputBounds.left, inputBounds.top);
186     const ui::Transform layerToScreen = getInputTransform(snapshot);
187     const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
188 
189     // InputDispatcher expects a display-to-input transform.
190     info.transform = inputToDisplay.inverse();
191 
192     // The touchable region is specified in the input coordinate space. Change it to display space.
193     info.touchableRegion =
194             transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
195 }
196 
handleDropInputMode(LayerSnapshot & snapshot,const LayerSnapshot & parentSnapshot)197 void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
198     if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
199         return;
200     }
201 
202     // Check if we need to drop input unconditionally
203     const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
204     if (dropInputMode == gui::DropInputMode::ALL) {
205         snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
206         ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
207         return;
208     }
209 
210     // Check if we need to check if the window is obscured by parent
211     if (dropInputMode != gui::DropInputMode::OBSCURED) {
212         return;
213     }
214 
215     // Check if the parent has set an alpha on the layer
216     if (parentSnapshot.color.a != 1.0_hf) {
217         snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
218         ALOGV("Dropping input for %s as requested by policy because alpha=%f",
219               snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
220     }
221 
222     // Check if the parent has cropped the buffer
223     Rect bufferSize = snapshot.croppedBufferSize;
224     if (!bufferSize.isValid()) {
225         snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
226         return;
227     }
228 
229     // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
230     // To check if the layer has been cropped, we take the buffer bounds, apply the local
231     // layer crop and apply the same set of transforms to move to screenspace. If the bounds
232     // match then the layer has not been cropped by its parents.
233     Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
234     bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
235 
236     if (croppedByParent) {
237         snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
238         ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
239               snapshot.name.c_str());
240     } else {
241         // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
242         // input if the window is obscured. This check should be done in surfaceflinger but the
243         // logic currently resides in inputflinger. So pass the if_obscured check to input to only
244         // drop input events if the window is obscured.
245         snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
246     }
247 }
248 
getBlendMode(const LayerSnapshot & snapshot,const RequestedLayerState & requested)249 auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
250     auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
251     if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
252         blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
253                                                  : Hwc2::IComposerClient::BlendMode::COVERAGE;
254     }
255     return blendMode;
256 }
257 
updateVisibility(LayerSnapshot & snapshot,bool visible)258 void updateVisibility(LayerSnapshot& snapshot, bool visible) {
259     if (snapshot.isVisible != visible) {
260         snapshot.changes |= RequestedLayerState::Changes::Visibility;
261     }
262     snapshot.isVisible = visible;
263 
264     // TODO(b/238781169) we are ignoring this compat for now, since we will have
265     // to remove any optimization based on visibility.
266 
267     // For compatibility reasons we let layers which can receive input
268     // receive input before they have actually submitted a buffer. Because
269     // of this we use canReceiveInput instead of isVisible to check the
270     // policy-visibility, ignoring the buffer state. However for layers with
271     // hasInputInfo()==false we can use the real visibility state.
272     // We are just using these layers for occlusion detection in
273     // InputDispatcher, and obviously if they aren't visible they can't occlude
274     // anything.
275     const bool visibleForInput =
276             snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
277     snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
278     LLOGV(snapshot.sequence, "updating visibility %s %s", visible ? "true" : "false",
279           snapshot.getDebugString().c_str());
280 }
281 
needsInputInfo(const LayerSnapshot & snapshot,const RequestedLayerState & requested)282 bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
283     if (requested.potentialCursor) {
284         return false;
285     }
286 
287     if (snapshot.inputInfo.token != nullptr) {
288         return true;
289     }
290 
291     if (snapshot.hasBufferOrSidebandStream()) {
292         return true;
293     }
294 
295     return requested.windowInfoHandle &&
296             requested.windowInfoHandle->getInfo()->inputConfig.test(
297                     gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
298 }
299 
updateMetadata(LayerSnapshot & snapshot,const RequestedLayerState & requested,const LayerSnapshotBuilder::Args & args)300 void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested,
301                     const LayerSnapshotBuilder::Args& args) {
302     snapshot.metadata.clear();
303     for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) {
304         auto compatIter = args.genericLayerMetadataKeyMap.find(key);
305         if (compatIter == std::end(args.genericLayerMetadataKeyMap)) {
306             continue;
307         }
308         const uint32_t id = compatIter->second;
309         auto it = requested.metadata.mMap.find(id);
310         if (it == std::end(requested.metadata.mMap)) {
311             continue;
312         }
313 
314         snapshot.metadata.emplace(key,
315                                   compositionengine::GenericLayerMetadataEntry{mandatory,
316                                                                                it->second});
317     }
318 }
319 
clearChanges(LayerSnapshot & snapshot)320 void clearChanges(LayerSnapshot& snapshot) {
321     snapshot.changes.clear();
322     snapshot.clientChanges = 0;
323     snapshot.contentDirty = false;
324     snapshot.hasReadyFrame = false;
325     snapshot.sidebandStreamHasFrame = false;
326     snapshot.surfaceDamage.clear();
327 }
328 
329 // TODO (b/259407931): Remove.
getPrimaryDisplayRotationFlags(const ui::DisplayMap<ui::LayerStack,frontend::DisplayInfo> & displays)330 uint32_t getPrimaryDisplayRotationFlags(
331         const ui::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
332     for (auto& [_, display] : displays) {
333         if (display.isPrimary) {
334             return display.rotationFlags;
335         }
336     }
337     return 0;
338 }
339 
340 } // namespace
341 
getRootSnapshot()342 LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
343     LayerSnapshot snapshot;
344     snapshot.path = LayerHierarchy::TraversalPath::ROOT;
345     snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
346     snapshot.clientChanges = 0;
347     snapshot.isHiddenByPolicyFromParent = false;
348     snapshot.isHiddenByPolicyFromRelativeParent = false;
349     snapshot.parentTransform.reset();
350     snapshot.geomLayerTransform.reset();
351     snapshot.geomInverseLayerTransform.reset();
352     snapshot.geomLayerBounds = getMaxDisplayBounds({});
353     snapshot.roundedCorner = RoundedCornerState();
354     snapshot.stretchEffect = {};
355     snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
356     snapshot.outputFilter.toInternalDisplay = false;
357     snapshot.isSecure = false;
358     snapshot.color.a = 1.0_hf;
359     snapshot.colorTransformIsIdentity = true;
360     snapshot.shadowSettings.length = 0.f;
361     snapshot.layerMetadata.mMap.clear();
362     snapshot.relativeLayerMetadata.mMap.clear();
363     snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
364     snapshot.dropInputMode = gui::DropInputMode::NONE;
365     snapshot.trustedOverlay = gui::TrustedOverlay::UNSET;
366     snapshot.gameMode = gui::GameMode::Unsupported;
367     snapshot.frameRate = {};
368     snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
369     snapshot.ignoreLocalTransform = false;
370     return snapshot;
371 }
372 
LayerSnapshotBuilder()373 LayerSnapshotBuilder::LayerSnapshotBuilder() {}
374 
LayerSnapshotBuilder(Args args)375 LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
376     args.forceUpdate = ForceUpdateFlags::ALL;
377     updateSnapshots(args);
378 }
379 
tryFastUpdate(const Args & args)380 bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
381     const bool forceUpdate = args.forceUpdate != ForceUpdateFlags::NONE;
382 
383     if (args.layerLifecycleManager.getGlobalChanges().get() == 0 && !forceUpdate &&
384         !args.displayChanges) {
385         return true;
386     }
387 
388     // There are only content changes which do not require any child layer snapshots to be updated.
389     ALOGV("%s", __func__);
390     ATRACE_NAME("FastPath");
391 
392     uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
393     if (forceUpdate || args.displayChanges) {
394         for (auto& snapshot : mSnapshots) {
395             const RequestedLayerState* requested =
396                     args.layerLifecycleManager.getLayerFromId(snapshot->path.id);
397             if (!requested) continue;
398             snapshot->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
399                             primaryDisplayRotationFlags);
400         }
401         return false;
402     }
403 
404     // Walk through all the updated requested layer states and update the corresponding snapshots.
405     for (const RequestedLayerState* requested : args.layerLifecycleManager.getChangedLayers()) {
406         auto range = mIdToSnapshots.equal_range(requested->id);
407         for (auto it = range.first; it != range.second; it++) {
408             it->second->merge(*requested, forceUpdate, args.displayChanges, args.forceFullDamage,
409                               primaryDisplayRotationFlags);
410         }
411     }
412 
413     if ((args.layerLifecycleManager.getGlobalChanges().get() &
414          ~(RequestedLayerState::Changes::Content | RequestedLayerState::Changes::Buffer).get()) !=
415         0) {
416         // We have changes that require us to walk the hierarchy and update child layers.
417         // No fast path for you.
418         return false;
419     }
420     return true;
421 }
422 
updateSnapshots(const Args & args)423 void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
424     ATRACE_NAME("UpdateSnapshots");
425     LayerSnapshot rootSnapshot = args.rootSnapshot;
426     if (args.parentCrop) {
427         rootSnapshot.geomLayerBounds = *args.parentCrop;
428     } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
429         rootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
430     }
431     if (args.displayChanges) {
432         rootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
433                 RequestedLayerState::Changes::Geometry;
434     }
435     if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
436         rootSnapshot.changes |=
437                 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
438         rootSnapshot.clientChanges |= layer_state_t::eReparent;
439     }
440 
441     for (auto& snapshot : mSnapshots) {
442         if (snapshot->reachablilty == LayerSnapshot::Reachablilty::Reachable) {
443             snapshot->reachablilty = LayerSnapshot::Reachablilty::Unreachable;
444         }
445     }
446 
447     LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
448     if (args.root.getLayer()) {
449         // The hierarchy can have a root layer when used for screenshots otherwise, it will have
450         // multiple children.
451         LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
452                                                                 LayerHierarchy::Variant::Attached);
453         updateSnapshotsInHierarchy(args, args.root, root, rootSnapshot, /*depth=*/0);
454     } else {
455         for (auto& [childHierarchy, variant] : args.root.mChildren) {
456             LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
457                                                                     childHierarchy->getLayer()->id,
458                                                                     variant);
459             updateSnapshotsInHierarchy(args, *childHierarchy, root, rootSnapshot, /*depth=*/0);
460         }
461     }
462 
463     // Update touchable region crops outside the main update pass. This is because a layer could be
464     // cropped by any other layer and it requires both snapshots to be updated.
465     updateTouchableRegionCrop(args);
466 
467     const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
468 
469     // Destroy unreachable snapshots for clone layers. And destroy snapshots for non-clone
470     // layers if the layer have been destroyed.
471     // TODO(b/238781169) consider making clone layer ids stable as well
472     if (!hasUnreachableSnapshots && args.layerLifecycleManager.getDestroyedLayers().empty()) {
473         return;
474     }
475 
476     std::unordered_set<uint32_t> destroyedLayerIds;
477     for (auto& destroyedLayer : args.layerLifecycleManager.getDestroyedLayers()) {
478         destroyedLayerIds.insert(destroyedLayer->id);
479     }
480 
481     auto it = mSnapshots.begin();
482     while (it < mSnapshots.end()) {
483         auto& traversalPath = it->get()->path;
484         const bool unreachable =
485                 it->get()->reachablilty == LayerSnapshot::Reachablilty::Unreachable;
486         const bool isClone = traversalPath.isClone();
487         const bool layerIsDestroyed =
488                 destroyedLayerIds.find(traversalPath.id) != destroyedLayerIds.end();
489         const bool destroySnapshot = (unreachable && isClone) || layerIsDestroyed;
490 
491         if (!destroySnapshot) {
492             it++;
493             continue;
494         }
495 
496         mPathToSnapshot.erase(traversalPath);
497 
498         auto range = mIdToSnapshots.equal_range(traversalPath.id);
499         auto matchingSnapshot =
500                 std::find_if(range.first, range.second, [&traversalPath](auto& snapshotWithId) {
501                     return snapshotWithId.second->path == traversalPath;
502                 });
503         mIdToSnapshots.erase(matchingSnapshot);
504         mNeedsTouchableRegionCrop.erase(traversalPath);
505         mSnapshots.back()->globalZ = it->get()->globalZ;
506         std::iter_swap(it, mSnapshots.end() - 1);
507         mSnapshots.erase(mSnapshots.end() - 1);
508     }
509 }
510 
update(const Args & args)511 void LayerSnapshotBuilder::update(const Args& args) {
512     for (auto& snapshot : mSnapshots) {
513         clearChanges(*snapshot);
514     }
515 
516     if (tryFastUpdate(args)) {
517         return;
518     }
519     updateSnapshots(args);
520 }
521 
updateSnapshotsInHierarchy(const Args & args,const LayerHierarchy & hierarchy,LayerHierarchy::TraversalPath & traversalPath,const LayerSnapshot & parentSnapshot,int depth)522 const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
523         const Args& args, const LayerHierarchy& hierarchy,
524         LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot,
525         int depth) {
526     LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
527                                     "Cycle detected in LayerSnapshotBuilder. See "
528                                     "builder_stack_overflow_transactions.winscope");
529 
530     const RequestedLayerState* layer = hierarchy.getLayer();
531     LayerSnapshot* snapshot = getSnapshot(traversalPath);
532     const bool newSnapshot = snapshot == nullptr;
533     uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
534     if (newSnapshot) {
535         snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
536         snapshot->merge(*layer, /*forceUpdate=*/true, /*displayChanges=*/true, args.forceFullDamage,
537                         primaryDisplayRotationFlags);
538         snapshot->changes |= RequestedLayerState::Changes::Created;
539     }
540 
541     if (traversalPath.isRelative()) {
542         bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
543         updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
544     } else {
545         if (traversalPath.isAttached()) {
546             resetRelativeState(*snapshot);
547         }
548         updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
549     }
550 
551     for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
552         LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
553                                                                 childHierarchy->getLayer()->id,
554                                                                 variant);
555         const LayerSnapshot& childSnapshot =
556                 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot,
557                                            depth + 1);
558         updateFrameRateFromChildSnapshot(*snapshot, childSnapshot, args);
559     }
560 
561     return *snapshot;
562 }
563 
getSnapshot(uint32_t layerId) const564 LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
565     if (layerId == UNASSIGNED_LAYER_ID) {
566         return nullptr;
567     }
568     LayerHierarchy::TraversalPath path{.id = layerId};
569     return getSnapshot(path);
570 }
571 
getSnapshot(const LayerHierarchy::TraversalPath & id) const572 LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
573     auto it = mPathToSnapshot.find(id);
574     return it == mPathToSnapshot.end() ? nullptr : it->second;
575 }
576 
createSnapshot(const LayerHierarchy::TraversalPath & path,const RequestedLayerState & layer,const LayerSnapshot & parentSnapshot)577 LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
578                                                     const RequestedLayerState& layer,
579                                                     const LayerSnapshot& parentSnapshot) {
580     mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
581     LayerSnapshot* snapshot = mSnapshots.back().get();
582     snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
583     if (path.isClone() && !LayerHierarchy::isMirror(path.variant)) {
584         snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
585     }
586     snapshot->ignoreLocalTransform =
587             path.isClone() && path.variant == LayerHierarchy::Variant::Detached_Mirror;
588     mPathToSnapshot[path] = snapshot;
589 
590     mIdToSnapshots.emplace(path.id, snapshot);
591     return snapshot;
592 }
593 
sortSnapshotsByZ(const Args & args)594 bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
595     if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
596         !args.layerLifecycleManager.getGlobalChanges().any(
597                 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility |
598                 RequestedLayerState::Changes::Input)) {
599         // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
600         // the snapshots.
601         return false;
602     }
603     mResortSnapshots = false;
604 
605     size_t globalZ = 0;
606     args.root.traverseInZOrder(
607             [this, &globalZ](const LayerHierarchy&,
608                              const LayerHierarchy::TraversalPath& traversalPath) -> bool {
609                 LayerSnapshot* snapshot = getSnapshot(traversalPath);
610                 if (!snapshot) {
611                     return true;
612                 }
613 
614                 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
615                     updateVisibility(*snapshot, snapshot->getIsVisible());
616                     size_t oldZ = snapshot->globalZ;
617                     size_t newZ = globalZ++;
618                     snapshot->globalZ = newZ;
619                     if (oldZ == newZ) {
620                         return true;
621                     }
622                     mSnapshots[newZ]->globalZ = oldZ;
623                     LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
624                           snapshot->getDebugString().c_str());
625                     std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
626                                    mSnapshots.begin() + static_cast<ssize_t>(newZ));
627                 }
628                 return true;
629             });
630     mNumInterestingSnapshots = (int)globalZ;
631     bool hasUnreachableSnapshots = false;
632     while (globalZ < mSnapshots.size()) {
633         mSnapshots[globalZ]->globalZ = globalZ;
634         /* mark unreachable snapshots as explicitly invisible */
635         updateVisibility(*mSnapshots[globalZ], false);
636         if (mSnapshots[globalZ]->reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
637             hasUnreachableSnapshots = true;
638         }
639         globalZ++;
640     }
641     return hasUnreachableSnapshots;
642 }
643 
updateRelativeState(LayerSnapshot & snapshot,const LayerSnapshot & parentSnapshot,bool parentIsRelative,const Args & args)644 void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
645                                                const LayerSnapshot& parentSnapshot,
646                                                bool parentIsRelative, const Args& args) {
647     if (parentIsRelative) {
648         snapshot.isHiddenByPolicyFromRelativeParent =
649                 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
650         if (args.includeMetadata) {
651             snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
652         }
653     } else {
654         snapshot.isHiddenByPolicyFromRelativeParent =
655                 parentSnapshot.isHiddenByPolicyFromRelativeParent;
656         if (args.includeMetadata) {
657             snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
658         }
659     }
660     if (snapshot.reachablilty == LayerSnapshot::Reachablilty::Unreachable) {
661         snapshot.reachablilty = LayerSnapshot::Reachablilty::ReachableByRelativeParent;
662     }
663 }
664 
updateFrameRateFromChildSnapshot(LayerSnapshot & snapshot,const LayerSnapshot & childSnapshot,const Args & args)665 void LayerSnapshotBuilder::updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
666                                                             const LayerSnapshot& childSnapshot,
667                                                             const Args& args) {
668     if (args.forceUpdate == ForceUpdateFlags::NONE &&
669         !args.layerLifecycleManager.getGlobalChanges().any(
670                 RequestedLayerState::Changes::Hierarchy) &&
671         !childSnapshot.changes.any(RequestedLayerState::Changes::FrameRate) &&
672         !snapshot.changes.any(RequestedLayerState::Changes::FrameRate)) {
673         return;
674     }
675 
676     using FrameRateCompatibility = scheduler::FrameRateCompatibility;
677     if (snapshot.frameRate.isValid()) {
678         // we already have a valid framerate.
679         return;
680     }
681 
682     // We return whether this layer or its children has a vote. We ignore ExactOrMultiple votes
683     // for the same reason we are allowing touch boost for those layers. See
684     // RefreshRateSelector::rankFrameRates for details.
685     const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
686             childSnapshot.frameRate.vote.type == FrameRateCompatibility::Default;
687     const auto layerVotedWithNoVote =
688             childSnapshot.frameRate.vote.type == FrameRateCompatibility::NoVote;
689     const auto layerVotedWithCategory =
690             childSnapshot.frameRate.category != FrameRateCategory::Default;
691     const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.vote.rate.isValid() &&
692             childSnapshot.frameRate.vote.type == FrameRateCompatibility::Exact;
693 
694     bool childHasValidFrameRate = layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
695             layerVotedWithCategory || layerVotedWithExactCompatibility;
696 
697     // If we don't have a valid frame rate, but the children do, we set this
698     // layer as NoVote to allow the children to control the refresh rate
699     if (childHasValidFrameRate) {
700         snapshot.frameRate = scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
701         snapshot.changes |= RequestedLayerState::Changes::FrameRate;
702     }
703 }
704 
resetRelativeState(LayerSnapshot & snapshot)705 void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
706     snapshot.isHiddenByPolicyFromRelativeParent = false;
707     snapshot.relativeLayerMetadata.mMap.clear();
708 }
709 
updateSnapshot(LayerSnapshot & snapshot,const Args & args,const RequestedLayerState & requested,const LayerSnapshot & parentSnapshot,const LayerHierarchy::TraversalPath & path)710 void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
711                                           const RequestedLayerState& requested,
712                                           const LayerSnapshot& parentSnapshot,
713                                           const LayerHierarchy::TraversalPath& path) {
714     // Always update flags and visibility
715     ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
716             (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
717              RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
718              RequestedLayerState::Changes::AffectsChildren | RequestedLayerState::Changes::Input |
719              RequestedLayerState::Changes::FrameRate | RequestedLayerState::Changes::GameMode);
720     snapshot.changes |= parentChanges;
721     if (args.displayChanges) snapshot.changes |= RequestedLayerState::Changes::Geometry;
722     snapshot.reachablilty = LayerSnapshot::Reachablilty::Reachable;
723     snapshot.clientChanges |= (parentSnapshot.clientChanges & layer_state_t::AFFECTS_CHILDREN);
724     snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
725             parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
726             (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
727 
728     const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
729             snapshot.clientChanges & layer_state_t::eReparent ||
730             snapshot.changes.any(RequestedLayerState::Changes::Visibility |
731                                  RequestedLayerState::Changes::Created);
732 
733     if (forceUpdate || snapshot.clientChanges & layer_state_t::eLayerStackChanged) {
734         // If root layer, use the layer stack otherwise get the parent's layer stack.
735         snapshot.outputFilter.layerStack =
736                 parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
737                 ? requested.layerStack
738                 : parentSnapshot.outputFilter.layerStack;
739     }
740 
741     if (forceUpdate || snapshot.clientChanges & layer_state_t::eTrustedOverlayChanged) {
742         switch (requested.trustedOverlay) {
743             case gui::TrustedOverlay::UNSET:
744                 snapshot.trustedOverlay = parentSnapshot.trustedOverlay;
745                 break;
746             case gui::TrustedOverlay::DISABLED:
747                 snapshot.trustedOverlay = FlagManager::getInstance().override_trusted_overlay()
748                         ? requested.trustedOverlay
749                         : parentSnapshot.trustedOverlay;
750                 break;
751             case gui::TrustedOverlay::ENABLED:
752                 snapshot.trustedOverlay = requested.trustedOverlay;
753                 break;
754         }
755     }
756 
757     if (snapshot.isHiddenByPolicyFromParent &&
758         !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
759         if (forceUpdate ||
760             snapshot.changes.any(RequestedLayerState::Changes::Geometry |
761                                  RequestedLayerState::Changes::BufferSize |
762                                  RequestedLayerState::Changes::Input)) {
763             updateInput(snapshot, requested, parentSnapshot, path, args);
764         }
765         return;
766     }
767 
768     if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Mirror)) {
769         // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
770         // marked as skip capture
771         snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
772                 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
773     }
774 
775     if (forceUpdate || snapshot.clientChanges & layer_state_t::eAlphaChanged) {
776         snapshot.color.a = parentSnapshot.color.a * requested.color.a;
777         snapshot.alpha = snapshot.color.a;
778         snapshot.inputInfo.alpha = snapshot.color.a;
779     }
780 
781     if (forceUpdate || snapshot.clientChanges & layer_state_t::eFlagsChanged) {
782         snapshot.isSecure =
783                 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
784         snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
785                 (requested.flags & layer_state_t::eLayerSkipScreenshot);
786     }
787 
788     if (forceUpdate || snapshot.clientChanges & layer_state_t::eStretchChanged) {
789         snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
790                 ? requested.stretchEffect
791                 : parentSnapshot.stretchEffect;
792     }
793 
794     if (forceUpdate || snapshot.clientChanges & layer_state_t::eColorTransformChanged) {
795         if (!parentSnapshot.colorTransformIsIdentity) {
796             snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
797             snapshot.colorTransformIsIdentity = false;
798         } else {
799             snapshot.colorTransform = requested.colorTransform;
800             snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
801         }
802     }
803 
804     if (forceUpdate || snapshot.changes.test(RequestedLayerState::Changes::GameMode)) {
805         snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
806                 ? requested.gameMode
807                 : parentSnapshot.gameMode;
808         updateMetadata(snapshot, requested, args);
809         if (args.includeMetadata) {
810             snapshot.layerMetadata = parentSnapshot.layerMetadata;
811             snapshot.layerMetadata.merge(requested.metadata);
812         }
813     }
814 
815     if (forceUpdate || snapshot.clientChanges & layer_state_t::eFixedTransformHintChanged ||
816         args.displayChanges) {
817         snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
818                 ? requested.fixedTransformHint
819                 : parentSnapshot.fixedTransformHint;
820 
821         if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
822             snapshot.transformHint = snapshot.fixedTransformHint;
823         } else {
824             const auto display = args.displays.get(snapshot.outputFilter.layerStack);
825             snapshot.transformHint = display.has_value()
826                     ? std::make_optional<>(display->get().transformHint)
827                     : std::nullopt;
828         }
829     }
830 
831     if (forceUpdate ||
832         args.layerLifecycleManager.getGlobalChanges().any(
833                 RequestedLayerState::Changes::Hierarchy) ||
834         snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
835                              RequestedLayerState::Changes::Hierarchy)) {
836         const bool shouldOverrideChildren = parentSnapshot.frameRateSelectionStrategy ==
837                 scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
838         const bool propagationAllowed = parentSnapshot.frameRateSelectionStrategy !=
839                 scheduler::LayerInfo::FrameRateSelectionStrategy::Self;
840         if ((!requested.requestedFrameRate.isValid() && propagationAllowed) ||
841             shouldOverrideChildren) {
842             snapshot.inheritedFrameRate = parentSnapshot.inheritedFrameRate;
843         } else {
844             snapshot.inheritedFrameRate = requested.requestedFrameRate;
845         }
846         // Set the framerate as the inherited frame rate and allow children to override it if
847         // needed.
848         snapshot.frameRate = snapshot.inheritedFrameRate;
849         snapshot.changes |= RequestedLayerState::Changes::FrameRate;
850     }
851 
852     if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionStrategyChanged) {
853         if (parentSnapshot.frameRateSelectionStrategy ==
854             scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren) {
855             snapshot.frameRateSelectionStrategy =
856                     scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren;
857         } else {
858             const auto strategy = scheduler::LayerInfo::convertFrameRateSelectionStrategy(
859                     requested.frameRateSelectionStrategy);
860             snapshot.frameRateSelectionStrategy = strategy;
861         }
862     }
863 
864     if (forceUpdate || snapshot.clientChanges & layer_state_t::eFrameRateSelectionPriority) {
865         snapshot.frameRateSelectionPriority =
866                 (requested.frameRateSelectionPriority == Layer::PRIORITY_UNSET)
867                 ? parentSnapshot.frameRateSelectionPriority
868                 : requested.frameRateSelectionPriority;
869     }
870 
871     if (forceUpdate ||
872         snapshot.clientChanges &
873                 (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
874                  layer_state_t::eAlphaChanged)) {
875         snapshot.backgroundBlurRadius = args.supportsBlur
876                 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
877                 : 0;
878         snapshot.blurRegions = requested.blurRegions;
879         for (auto& region : snapshot.blurRegions) {
880             region.alpha = region.alpha * snapshot.color.a;
881         }
882     }
883 
884     if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
885         uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
886         updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
887     }
888 
889     if (forceUpdate || snapshot.clientChanges & layer_state_t::eCornerRadiusChanged ||
890         snapshot.changes.any(RequestedLayerState::Changes::Geometry |
891                              RequestedLayerState::Changes::BufferUsageFlags)) {
892         updateRoundedCorner(snapshot, requested, parentSnapshot, args);
893     }
894 
895     if (forceUpdate || snapshot.clientChanges & layer_state_t::eShadowRadiusChanged ||
896         snapshot.changes.any(RequestedLayerState::Changes::Geometry)) {
897         updateShadows(snapshot, requested, args.globalShadowSettings);
898     }
899 
900     if (forceUpdate ||
901         snapshot.changes.any(RequestedLayerState::Changes::Geometry |
902                              RequestedLayerState::Changes::Input)) {
903         updateInput(snapshot, requested, parentSnapshot, path, args);
904     }
905 
906     // computed snapshot properties
907     snapshot.forceClientComposition =
908             snapshot.shadowSettings.length > 0 || snapshot.stretchEffect.hasEffect();
909     snapshot.contentOpaque = snapshot.isContentOpaque();
910     snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
911             snapshot.color.a == 1.f;
912     snapshot.blendMode = getBlendMode(snapshot, requested);
913     LLOGV(snapshot.sequence,
914           "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
915           args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
916           snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
917           parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
918           std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
919 }
920 
updateRoundedCorner(LayerSnapshot & snapshot,const RequestedLayerState & requested,const LayerSnapshot & parentSnapshot,const Args & args)921 void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
922                                                const RequestedLayerState& requested,
923                                                const LayerSnapshot& parentSnapshot,
924                                                const Args& args) {
925     if (args.skipRoundCornersWhenProtected && requested.isProtected()) {
926         snapshot.roundedCorner = RoundedCornerState();
927         return;
928     }
929     snapshot.roundedCorner = RoundedCornerState();
930     RoundedCornerState parentRoundedCorner;
931     if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
932         parentRoundedCorner = parentSnapshot.roundedCorner;
933         ui::Transform t = snapshot.localTransform.inverse();
934         parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
935         parentRoundedCorner.radius.x *= t.getScaleX();
936         parentRoundedCorner.radius.y *= t.getScaleY();
937     }
938 
939     FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
940     const vec2 radius(requested.cornerRadius, requested.cornerRadius);
941     RoundedCornerState layerSettings(layerCropRect, radius);
942     const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
943     const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
944     if (layerSettingsValid && parentRoundedCornerValid) {
945         // If the parent and the layer have rounded corner settings, use the parent settings if
946         // the parent crop is entirely inside the layer crop. This has limitations and cause
947         // rendering artifacts. See b/200300845 for correct fix.
948         if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
949             parentRoundedCorner.cropRect.top > layerCropRect.top &&
950             parentRoundedCorner.cropRect.right < layerCropRect.right &&
951             parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
952             snapshot.roundedCorner = parentRoundedCorner;
953         } else {
954             snapshot.roundedCorner = layerSettings;
955         }
956     } else if (layerSettingsValid) {
957         snapshot.roundedCorner = layerSettings;
958     } else if (parentRoundedCornerValid) {
959         snapshot.roundedCorner = parentRoundedCorner;
960     }
961 }
962 
updateLayerBounds(LayerSnapshot & snapshot,const RequestedLayerState & requested,const LayerSnapshot & parentSnapshot,uint32_t primaryDisplayRotationFlags)963 void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
964                                              const RequestedLayerState& requested,
965                                              const LayerSnapshot& parentSnapshot,
966                                              uint32_t primaryDisplayRotationFlags) {
967     snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
968     const bool transformWasInvalid = snapshot.invalidTransform;
969     snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
970     if (snapshot.invalidTransform) {
971         auto& t = snapshot.geomLayerTransform;
972         auto& requestedT = requested.requestedTransform;
973         std::string transformDebug =
974                 base::StringPrintf(" transform={%f,%f,%f,%f}  requestedTransform={%f,%f,%f,%f}",
975                                    t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
976                                    requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
977         std::string bufferDebug;
978         if (requested.externalTexture) {
979             auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
980             auto& destFrame = requested.destinationFrame;
981             bufferDebug = base::StringPrintf(" buffer={%d,%d}  displayRot=%d"
982                                              " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
983                                              requested.externalTexture->getWidth(),
984                                              requested.externalTexture->getHeight(),
985                                              primaryDisplayRotationFlags, destFrame.left,
986                                              destFrame.top, destFrame.right, destFrame.bottom,
987                                              unRotBuffer.getHeight(), unRotBuffer.getWidth());
988         }
989         ALOGW("Resetting transform for %s because it is invalid.%s%s",
990               snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
991         snapshot.geomLayerTransform.reset();
992     }
993     if (transformWasInvalid != snapshot.invalidTransform) {
994         // If transform is invalid, the layer will be hidden.
995         mResortSnapshots = true;
996     }
997     snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
998 
999     FloatRect parentBounds = parentSnapshot.geomLayerBounds;
1000     parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
1001     snapshot.geomLayerBounds =
1002             (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
1003     if (!requested.crop.isEmpty()) {
1004         snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
1005     }
1006     snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
1007     snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
1008     const Rect geomLayerBoundsWithoutTransparentRegion =
1009             RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
1010                                         requested.transparentRegion);
1011     snapshot.transformedBoundsWithoutTransparentRegion =
1012             snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
1013     snapshot.parentTransform = parentSnapshot.geomLayerTransform;
1014 
1015     // Subtract the transparent region and snap to the bounds
1016     const Rect bounds =
1017             RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
1018     if (requested.potentialCursor) {
1019         snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
1020     }
1021 }
1022 
updateShadows(LayerSnapshot & snapshot,const RequestedLayerState &,const ShadowSettings & globalShadowSettings)1023 void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot, const RequestedLayerState&,
1024                                          const ShadowSettings& globalShadowSettings) {
1025     if (snapshot.shadowSettings.length > 0.f) {
1026         snapshot.shadowSettings.ambientColor = globalShadowSettings.ambientColor;
1027         snapshot.shadowSettings.spotColor = globalShadowSettings.spotColor;
1028         snapshot.shadowSettings.lightPos = globalShadowSettings.lightPos;
1029         snapshot.shadowSettings.lightRadius = globalShadowSettings.lightRadius;
1030 
1031         // Note: this preserves existing behavior of shadowing the entire layer and not cropping
1032         // it if transparent regions are present. This may not be necessary since shadows are
1033         // typically cast by layers without transparent regions.
1034         snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
1035 
1036         // If the casting layer is translucent, we need to fill in the shadow underneath the
1037         // layer. Otherwise the generated shadow will only be shown around the casting layer.
1038         snapshot.shadowSettings.casterIsTranslucent =
1039                 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
1040         snapshot.shadowSettings.ambientColor *= snapshot.alpha;
1041         snapshot.shadowSettings.spotColor *= snapshot.alpha;
1042     }
1043 }
1044 
updateInput(LayerSnapshot & snapshot,const RequestedLayerState & requested,const LayerSnapshot & parentSnapshot,const LayerHierarchy::TraversalPath & path,const Args & args)1045 void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
1046                                        const RequestedLayerState& requested,
1047                                        const LayerSnapshot& parentSnapshot,
1048                                        const LayerHierarchy::TraversalPath& path,
1049                                        const Args& args) {
1050     using InputConfig = gui::WindowInfo::InputConfig;
1051 
1052     if (requested.windowInfoHandle) {
1053         snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
1054     } else {
1055         snapshot.inputInfo = {};
1056         // b/271132344 revisit this and see if we can always use the layers uid/pid
1057         snapshot.inputInfo.name = requested.name;
1058         snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid};
1059         snapshot.inputInfo.ownerPid = gui::Pid{requested.ownerPid};
1060     }
1061     snapshot.touchCropId = requested.touchCropId;
1062 
1063     snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
1064     snapshot.inputInfo.displayId =
1065             ui::LogicalDisplayId{static_cast<int32_t>(snapshot.outputFilter.layerStack.id)};
1066     snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1067             ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1068             : parentSnapshot.inputInfo.touchOcclusionMode;
1069     snapshot.inputInfo.canOccludePresentation = parentSnapshot.inputInfo.canOccludePresentation ||
1070             (requested.flags & layer_state_t::eCanOccludePresentation);
1071     if (requested.dropInputMode == gui::DropInputMode::ALL ||
1072         parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1073         snapshot.dropInputMode = gui::DropInputMode::ALL;
1074     } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1075                parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1076         snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1077     } else {
1078         snapshot.dropInputMode = gui::DropInputMode::NONE;
1079     }
1080 
1081     if (snapshot.isSecure ||
1082         parentSnapshot.inputInfo.inputConfig.test(InputConfig::SENSITIVE_FOR_PRIVACY)) {
1083         snapshot.inputInfo.inputConfig |= InputConfig::SENSITIVE_FOR_PRIVACY;
1084     }
1085 
1086     updateVisibility(snapshot, snapshot.isVisible);
1087     if (!needsInputInfo(snapshot, requested)) {
1088         return;
1089     }
1090 
1091     static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1092     const std::optional<frontend::DisplayInfo> displayInfoOpt =
1093             args.displays.get(snapshot.outputFilter.layerStack);
1094     bool noValidDisplay = !displayInfoOpt.has_value();
1095     auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1096 
1097     if (!requested.windowInfoHandle) {
1098         snapshot.inputInfo.inputConfig = InputConfig::NO_INPUT_CHANNEL;
1099     }
1100     fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1101 
1102     if (noValidDisplay) {
1103         // Do not let the window receive touches if it is not associated with a valid display
1104         // transform. We still allow the window to receive keys and prevent ANRs.
1105         snapshot.inputInfo.inputConfig |= InputConfig::NOT_TOUCHABLE;
1106     }
1107 
1108     snapshot.inputInfo.alpha = snapshot.color.a;
1109 
1110     handleDropInputMode(snapshot, parentSnapshot);
1111 
1112     // If the window will be blacked out on a display because the display does not have the secure
1113     // flag and the layer has the secure flag set, then drop input.
1114     if (!displayInfo.isSecure && snapshot.isSecure) {
1115         snapshot.inputInfo.inputConfig |= InputConfig::DROP_INPUT;
1116     }
1117 
1118     if (requested.touchCropId != UNASSIGNED_LAYER_ID || path.isClone()) {
1119         mNeedsTouchableRegionCrop.insert(path);
1120     }
1121     auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1122     if (!cropLayerSnapshot && snapshot.inputInfo.replaceTouchableRegionWithCrop) {
1123         FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
1124         Rect inputBoundsInDisplaySpace =
1125                 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1126         snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1127     }
1128 
1129     // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1130     // if it was set by WM for a known system overlay
1131     if (snapshot.trustedOverlay == gui::TrustedOverlay::ENABLED) {
1132         snapshot.inputInfo.inputConfig |= InputConfig::TRUSTED_OVERLAY;
1133     }
1134 
1135     snapshot.inputInfo.contentSize = snapshot.croppedBufferSize.getSize();
1136 
1137     // If the layer is a clone, we need to crop the input region to cloned root to prevent
1138     // touches from going outside the cloned area.
1139     if (path.isClone()) {
1140         snapshot.inputInfo.inputConfig |= InputConfig::CLONE;
1141         // Cloned layers shouldn't handle watch outside since their z order is not determined by
1142         // WM or the client.
1143         snapshot.inputInfo.inputConfig.clear(InputConfig::WATCH_OUTSIDE_TOUCH);
1144     }
1145 }
1146 
getSnapshots()1147 std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1148     return mSnapshots;
1149 }
1150 
forEachVisibleSnapshot(const ConstVisitor & visitor) const1151 void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1152     for (int i = 0; i < mNumInterestingSnapshots; i++) {
1153         LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1154         if (!snapshot.isVisible) continue;
1155         visitor(snapshot);
1156     }
1157 }
1158 
1159 // Visit each visible snapshot in z-order
forEachVisibleSnapshot(const ConstVisitor & visitor,const LayerHierarchy & root) const1160 void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1161                                                   const LayerHierarchy& root) const {
1162     root.traverseInZOrder(
1163             [this, visitor](const LayerHierarchy&,
1164                             const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1165                 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1166                 if (snapshot && snapshot->isVisible) {
1167                     visitor(*snapshot);
1168                 }
1169                 return true;
1170             });
1171 }
1172 
forEachVisibleSnapshot(const Visitor & visitor)1173 void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1174     for (int i = 0; i < mNumInterestingSnapshots; i++) {
1175         std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1176         if (!snapshot->isVisible) continue;
1177         visitor(snapshot);
1178     }
1179 }
1180 
forEachInputSnapshot(const ConstVisitor & visitor) const1181 void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1182     for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1183         LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1184         if (!snapshot.hasInputInfo()) continue;
1185         visitor(snapshot);
1186     }
1187 }
1188 
updateTouchableRegionCrop(const Args & args)1189 void LayerSnapshotBuilder::updateTouchableRegionCrop(const Args& args) {
1190     if (mNeedsTouchableRegionCrop.empty()) {
1191         return;
1192     }
1193 
1194     static constexpr ftl::Flags<RequestedLayerState::Changes> AFFECTS_INPUT =
1195             RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Created |
1196             RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
1197             RequestedLayerState::Changes::Input;
1198 
1199     if (args.forceUpdate != ForceUpdateFlags::ALL &&
1200         !args.layerLifecycleManager.getGlobalChanges().any(AFFECTS_INPUT) && !args.displayChanges) {
1201         return;
1202     }
1203 
1204     for (auto& path : mNeedsTouchableRegionCrop) {
1205         frontend::LayerSnapshot* snapshot = getSnapshot(path);
1206         if (!snapshot) {
1207             continue;
1208         }
1209         LLOGV(snapshot->sequence, "updateTouchableRegionCrop=%s",
1210               snapshot->getDebugString().c_str());
1211         const std::optional<frontend::DisplayInfo> displayInfoOpt =
1212                 args.displays.get(snapshot->outputFilter.layerStack);
1213         static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
1214         auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1215 
1216         bool needsUpdate =
1217                 args.forceUpdate == ForceUpdateFlags::ALL || snapshot->changes.any(AFFECTS_INPUT);
1218         auto cropLayerSnapshot = getSnapshot(snapshot->touchCropId);
1219         needsUpdate =
1220                 needsUpdate || (cropLayerSnapshot && cropLayerSnapshot->changes.any(AFFECTS_INPUT));
1221         auto clonedRootSnapshot = path.isClone() ? getSnapshot(snapshot->mirrorRootPath) : nullptr;
1222         needsUpdate = needsUpdate ||
1223                 (clonedRootSnapshot && clonedRootSnapshot->changes.any(AFFECTS_INPUT));
1224 
1225         if (!needsUpdate) {
1226             continue;
1227         }
1228 
1229         if (snapshot->inputInfo.replaceTouchableRegionWithCrop) {
1230             Rect inputBoundsInDisplaySpace;
1231             if (!cropLayerSnapshot) {
1232                 FloatRect inputBounds = getInputBounds(*snapshot, /*fillParentBounds=*/true).first;
1233                 inputBoundsInDisplaySpace =
1234                         getInputBoundsInDisplaySpace(*snapshot, inputBounds, displayInfo.transform);
1235             } else {
1236                 FloatRect inputBounds =
1237                         getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1238                 inputBoundsInDisplaySpace =
1239                         getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1240                                                      displayInfo.transform);
1241             }
1242             snapshot->inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
1243         } else if (cropLayerSnapshot) {
1244             FloatRect inputBounds =
1245                     getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1246             Rect inputBoundsInDisplaySpace =
1247                     getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1248                                                  displayInfo.transform);
1249             snapshot->inputInfo.touchableRegion =
1250                     snapshot->inputInfo.touchableRegion.intersect(inputBoundsInDisplaySpace);
1251         }
1252 
1253         // If the layer is a clone, we need to crop the input region to cloned root to prevent
1254         // touches from going outside the cloned area.
1255         if (clonedRootSnapshot) {
1256             const Rect rect =
1257                     displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1258             snapshot->inputInfo.touchableRegion =
1259                     snapshot->inputInfo.touchableRegion.intersect(rect);
1260         }
1261     }
1262 }
1263 
1264 } // namespace android::surfaceflinger::frontend
1265