xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 #pragma once
18 
19 #include "FrontEnd/DisplayInfo.h"
20 #include "FrontEnd/LayerLifecycleManager.h"
21 #include "LayerHierarchy.h"
22 #include "LayerSnapshot.h"
23 #include "RequestedLayerState.h"
24 
25 namespace android::surfaceflinger::frontend {
26 
27 // Walks through the layer hierarchy to build an ordered list
28 // of LayerSnapshots that can be passed on to CompositionEngine.
29 // This builder does a minimum amount of work to update
30 // an existing set of snapshots based on hierarchy changes
31 // and RequestedLayerState changes.
32 
33 // The builder also uses a fast path to update
34 // snapshots when there are only buffer updates.
35 class LayerSnapshotBuilder {
36 private:
37     static LayerSnapshot getRootSnapshot();
38 
39 public:
40     enum class ForceUpdateFlags {
41         NONE,
42         ALL,
43         HIERARCHY,
44     };
45     struct Args {
46         LayerHierarchy root;
47         const LayerLifecycleManager& layerLifecycleManager;
48         ForceUpdateFlags forceUpdate = ForceUpdateFlags::NONE;
49         bool includeMetadata = false;
50         const DisplayInfos& displays;
51         // Set to true if there were display changes since last update.
52         bool displayChanges = false;
53         const ShadowSettings& globalShadowSettings;
54         bool supportsBlur = true;
55         bool forceFullDamage = false;
56         std::optional<FloatRect> parentCrop = std::nullopt;
57         std::unordered_set<uint32_t> excludeLayerIds;
58         const std::unordered_map<std::string, bool>& supportedLayerGenericMetadata;
59         const std::unordered_map<std::string, uint32_t>& genericLayerMetadataKeyMap;
60         bool skipRoundCornersWhenProtected = false;
61         LayerSnapshot rootSnapshot = getRootSnapshot();
62     };
63     LayerSnapshotBuilder();
64 
65     // Rebuild the snapshots from scratch.
66     LayerSnapshotBuilder(Args);
67 
68     // Update an existing set of snapshot using change flags in RequestedLayerState
69     // and LayerLifecycleManager. This needs to be called before
70     // LayerLifecycleManager.commitChanges is called as that function will clear all
71     // change flags.
72     void update(const Args&);
73     std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots();
74     LayerSnapshot* getSnapshot(uint32_t layerId) const;
75     LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
76 
77     typedef std::function<void(const LayerSnapshot& snapshot)> ConstVisitor;
78 
79     // Visit each visible snapshot in z-order
80     void forEachVisibleSnapshot(const ConstVisitor& visitor) const;
81 
82     // Visit each visible snapshot in z-order
83     void forEachVisibleSnapshot(const ConstVisitor& visitor, const LayerHierarchy& root) const;
84 
85     typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
86     // Visit each visible snapshot in z-order and move the snapshot if needed
87     void forEachVisibleSnapshot(const Visitor& visitor);
88 
89     typedef std::function<bool(const LayerSnapshot& snapshot)> ConstPredicate;
90     // Visit each snapshot that satisfies the predicate and move the snapshot if needed with visible
91     // snapshots in z-order
92     void forEachSnapshot(const Visitor& visitor, const ConstPredicate& predicate);
93 
94     // Visit each snapshot
95     void forEachSnapshot(const ConstVisitor& visitor) const;
96 
97     // Visit each snapshot interesting to input reverse z-order
98     void forEachInputSnapshot(const ConstVisitor& visitor) const;
99 
100 private:
101     friend class LayerSnapshotTest;
102 
103     // return true if we were able to successfully update the snapshots via
104     // the fast path.
105     bool tryFastUpdate(const Args& args);
106 
107     void updateSnapshots(const Args& args);
108 
109     const LayerSnapshot& updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy,
110                                                     LayerHierarchy::TraversalPath& traversalPath,
111                                                     const LayerSnapshot& parentSnapshot, int depth);
112     void updateSnapshot(LayerSnapshot&, const Args&, const RequestedLayerState&,
113                         const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&);
114     static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
115                                     bool parentIsRelative, const Args& args);
116     static void resetRelativeState(LayerSnapshot& snapshot);
117     static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
118                                     const LayerSnapshot& parentSnapshot, const Args& args);
119     static bool extensionEdgeSharedWithParent(LayerSnapshot& snapshot,
120                                               const RequestedLayerState& requested,
121                                               const LayerSnapshot& parentSnapshot);
122     static void updateBoundsForEdgeExtension(LayerSnapshot& snapshot);
123     void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
124                            const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags);
125     static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
126                               const ShadowSettings& globalShadowSettings);
127     void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
128                      const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path,
129                      const Args& args);
130     // Return true if there are unreachable snapshots
131     bool sortSnapshotsByZ(const Args& args);
132     LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id,
133                                   const RequestedLayerState& layer,
134                                   const LayerSnapshot& parentSnapshot);
135     void updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot,
136                                           const LayerSnapshot& childSnapshot,
137                                           const RequestedLayerState& requestedCHildState,
138                                           const Args& args, bool* outChildHasValidFrameRate);
139     void updateTouchableRegionCrop(const Args& args);
140 
141     std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*,
142                        LayerHierarchy::TraversalPathHash>
143             mPathToSnapshot;
144     std::multimap<uint32_t, LayerSnapshot*> mIdToSnapshots;
145 
146     // Track snapshots that needs touchable region crop from other snapshots
147     std::unordered_set<LayerHierarchy::TraversalPath, LayerHierarchy::TraversalPathHash>
148             mNeedsTouchableRegionCrop;
149     std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots;
150     bool mResortSnapshots = false;
151     int mNumInterestingSnapshots = 0;
152 };
153 
154 } // namespace android::surfaceflinger::frontend
155