blob: ba2e262bafecd129f46f0ea69d4eaa9b6cef05f4 [file] [log] [blame]
Vishnu Nair04f89692022-11-16 23:21:05 +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#pragma once
18
19#include "FrontEnd/LayerCreationArgs.h"
20#include "RequestedLayerState.h"
21#include "ftl/small_vector.h"
22
23namespace android::surfaceflinger::frontend {
24class LayerHierarchyBuilder;
25
26// LayerHierarchy allows us to navigate the layer hierarchy in z-order, or depth first traversal.
27// The hierarchy is created from a set of RequestedLayerStates. The hierarchy itself does not
28// contain additional states. Instead, it is a representation of RequestedLayerStates as a graph.
29//
30// Each node in the hierarchy can be visited by multiple parents (making this a graph). While
31// traversing the hierarchy, a new concept called Variant can be used to understand the
32// relationship of the layer to its parent. The following variants are possible:
33// Attached - child of the parent
34// Detached - child of the parent but currently relative parented to another layer
35// Relative - relative child of the parent
36// Mirror - mirrored from another layer
37//
38// By representing the hierarchy as a graph, we can represent mirrored layer hierarchies without
39// cloning the layer requested state. The mirrored hierarchy and its corresponding
40// RequestedLayerStates are kept in sync because the mirrored hierarchy does not clone any
41// states.
42class LayerHierarchy {
43public:
Vishnu Naircfb2d252023-01-19 04:44:02 +000044 enum Variant : uint32_t {
Vishnu Naira02943f2023-06-03 13:44:46 -070045 Attached, // child of the parent
46 Detached, // child of the parent but currently relative parented to another layer
47 Relative, // relative child of the parent
48 Mirror, // mirrored from another layer
Vishnu Naircfb2d252023-01-19 04:44:02 +000049 ftl_first = Attached,
50 ftl_last = Mirror,
Vishnu Nair04f89692022-11-16 23:21:05 +000051 };
52 // Represents a unique path to a node.
Vishnu Nair80a5a702023-02-11 01:21:51 +000053 // The layer hierarchy is represented as a graph. Each node can be visited by multiple parents.
54 // This allows us to represent mirroring in an efficient way. See the example below:
55 // root
56 // ├─ A {Traversal path id = 1}
57 // ├─ B {Traversal path id = 2}
58 // │ ├─ C {Traversal path id = 3}
59 // │ ├─ D {Traversal path id = 4}
Vishnu Nair6f878312023-09-08 11:05:01 -070060 // │ └─ E (Mirrors C) {Traversal path id = 5}
61 // └─ F (Mirrors B) {Traversal path id = 6}
Vishnu Nair80a5a702023-02-11 01:21:51 +000062 //
Vishnu Nair6f878312023-09-08 11:05:01 -070063 // C can be traversed via B or E or F and or via F then E.
Vishnu Nair80a5a702023-02-11 01:21:51 +000064 // Depending on how the node is reached, its properties such as geometry or visibility might be
65 // different. And we can uniquely identify the node by keeping track of the nodes leading up to
66 // it. But to be more efficient we only need to track the nodes id and the top mirror root path.
67 // So C for example, would have the following unique traversal paths:
68 // - {Traversal path id = 3}
Vishnu Nair6f878312023-09-08 11:05:01 -070069 // - {Traversal path id = 3, mirrorRootIds = 5}
70 // - {Traversal path id = 3, mirrorRootIds = 6}
71 // - {Traversal path id = 3, mirrorRootIds = 6, 5}
Vishnu Nair80a5a702023-02-11 01:21:51 +000072
Vishnu Nair04f89692022-11-16 23:21:05 +000073 struct TraversalPath {
74 uint32_t id;
75 LayerHierarchy::Variant variant;
76 // Mirrored layers can have a different geometry than their parents so we need to track
77 // the mirror roots in the traversal.
Vishnu Nair6f878312023-09-08 11:05:01 -070078 ftl::SmallVector<uint32_t, 5> mirrorRootIds;
Vishnu Nair04f89692022-11-16 23:21:05 +000079 // Relative layers can be visited twice, once by their parent and then once again by
80 // their relative parent. We keep track of the roots here to detect any loops in the
81 // hierarchy. If a relative root already exists in the list while building the
82 // TraversalPath, it means that somewhere in the hierarchy two layers are relatively
83 // parented to each other.
84 ftl::SmallVector<uint32_t, 5> relativeRootIds;
85 // First duplicate relative root id found. If this is a valid layer id that means we are
86 // in a loop.
87 uint32_t invalidRelativeRootId = UNASSIGNED_LAYER_ID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000088 // See isAttached()
89 bool detached = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000090 bool hasRelZLoop() const { return invalidRelativeRootId != UNASSIGNED_LAYER_ID; }
Vishnu Nair8fc721b2022-12-22 20:06:32 +000091 // Returns true if this node is reached via one or more relative parents.
92 bool isRelative() const { return !relativeRootIds.empty(); }
93 // Returns true if the node or its parents are not Detached.
94 bool isAttached() const { return !detached; }
95 // Returns true if the node is a clone.
Vishnu Nair6f878312023-09-08 11:05:01 -070096 bool isClone() const { return !mirrorRootIds.empty(); }
Vishnu Nair04f89692022-11-16 23:21:05 +000097
98 bool operator==(const TraversalPath& other) const {
Vishnu Nair6f878312023-09-08 11:05:01 -070099 return id == other.id && mirrorRootIds == other.mirrorRootIds;
Vishnu Nair04f89692022-11-16 23:21:05 +0000100 }
101 std::string toString() const;
102
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000103 static const TraversalPath ROOT;
Vishnu Nair04f89692022-11-16 23:21:05 +0000104 };
105
Vishnu Naird0183602023-03-16 18:52:15 +0000106 struct TraversalPathHash {
107 std::size_t operator()(const LayerHierarchy::TraversalPath& key) const {
108 uint32_t hashCode = key.id * 31;
Vishnu Nair6f878312023-09-08 11:05:01 -0700109 for (uint32_t mirrorRootId : key.mirrorRootIds) {
110 hashCode += mirrorRootId * 31;
Vishnu Naird0183602023-03-16 18:52:15 +0000111 }
112 return std::hash<size_t>{}(hashCode);
113 }
114 };
115
Vishnu Nair04f89692022-11-16 23:21:05 +0000116 // Helper class to add nodes to an existing traversal id and removes the
117 // node when it goes out of scope.
118 class ScopedAddToTraversalPath {
119 public:
120 ScopedAddToTraversalPath(TraversalPath& traversalPath, uint32_t layerId,
121 LayerHierarchy::Variant variantArg);
122 ~ScopedAddToTraversalPath();
123
124 private:
125 TraversalPath& mTraversalPath;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000126 TraversalPath mParentPath;
Vishnu Nair04f89692022-11-16 23:21:05 +0000127 };
128 LayerHierarchy(RequestedLayerState* layer);
129
130 // Visitor function that provides the hierarchy node and a traversal id which uniquely
131 // identifies how was visited. The hierarchy contains a pointer to the RequestedLayerState.
132 // Return false to stop traversing down the hierarchy.
133 typedef std::function<bool(const LayerHierarchy& hierarchy,
134 const LayerHierarchy::TraversalPath& traversalPath)>
135 Visitor;
136
137 // Traverse the hierarchy and visit all child variants.
138 void traverse(const Visitor& visitor) const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000139 TraversalPath root = TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000140 if (mLayer) {
141 root.id = mLayer->id;
142 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000143 traverse(visitor, root);
Vishnu Nair04f89692022-11-16 23:21:05 +0000144 }
145
146 // Traverse the hierarchy in z-order, skipping children that have relative parents.
147 void traverseInZOrder(const Visitor& visitor) const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000148 TraversalPath root = TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000149 if (mLayer) {
150 root.id = mLayer->id;
151 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000152 traverseInZOrder(visitor, root);
Vishnu Nair04f89692022-11-16 23:21:05 +0000153 }
154
155 const RequestedLayerState* getLayer() const;
Vishnu Nairea6ff812023-02-27 17:41:39 +0000156 const LayerHierarchy* getRelativeParent() const;
157 const LayerHierarchy* getParent() const;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000158 friend std::ostream& operator<<(std::ostream& os, const LayerHierarchy& obj) {
159 std::string prefix = " ";
Vishnu Nair6f878312023-09-08 11:05:01 -0700160 obj.dump(os, prefix, LayerHierarchy::Variant::Attached, /*isLastChild=*/false,
161 /*includeMirroredHierarchy*/ false);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000162 return os;
163 }
Vishnu Nair6f878312023-09-08 11:05:01 -0700164 std::string dump() const {
165 std::string prefix = " ";
166 std::ostringstream os;
167 dump(os, prefix, LayerHierarchy::Variant::Attached, /*isLastChild=*/false,
168 /*includeMirroredHierarchy*/ true);
169 return os.str();
170 }
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000171
Vishnu Nair04f89692022-11-16 23:21:05 +0000172 std::string getDebugStringShort() const;
173 // Traverse the hierarchy and return true if loops are found. The outInvalidRelativeRoot
174 // will contain the first relative root that was visited twice in a traversal.
175 bool hasRelZLoop(uint32_t& outInvalidRelativeRoot) const;
176 std::vector<std::pair<LayerHierarchy*, Variant>> mChildren;
177
178private:
179 friend LayerHierarchyBuilder;
180 LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly);
181 void addChild(LayerHierarchy*, LayerHierarchy::Variant);
182 void removeChild(LayerHierarchy*);
183 void sortChildrenByZOrder();
184 void updateChild(LayerHierarchy*, LayerHierarchy::Variant);
185 void traverseInZOrder(const Visitor& visitor, LayerHierarchy::TraversalPath& parent) const;
186 void traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& parent) const;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000187 void dump(std::ostream& out, const std::string& prefix, LayerHierarchy::Variant variant,
Vishnu Nair6f878312023-09-08 11:05:01 -0700188 bool isLastChild, bool includeMirroredHierarchy) const;
Vishnu Nair04f89692022-11-16 23:21:05 +0000189
190 const RequestedLayerState* mLayer;
191 LayerHierarchy* mParent = nullptr;
192 LayerHierarchy* mRelativeParent = nullptr;
193};
194
195// Given a list of RequestedLayerState, this class will build a root hierarchy and an
196// offscreen hierarchy. The builder also has an update method which can update an existing
197// hierarchy from a list of RequestedLayerState and associated change flags.
198class LayerHierarchyBuilder {
199public:
200 LayerHierarchyBuilder(const std::vector<std::unique_ptr<RequestedLayerState>>&);
201 void update(const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
202 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers);
203 LayerHierarchy getPartialHierarchy(uint32_t, bool childrenOnly) const;
204 const LayerHierarchy& getHierarchy() const;
205 const LayerHierarchy& getOffscreenHierarchy() const;
206 std::string getDebugString(uint32_t layerId, uint32_t depth = 0) const;
207
208private:
209 void onLayerAdded(RequestedLayerState* layer);
210 void attachToParent(LayerHierarchy*);
211 void detachFromParent(LayerHierarchy*);
212 void attachToRelativeParent(LayerHierarchy*);
213 void detachFromRelativeParent(LayerHierarchy*);
214 void attachHierarchyToRelativeParent(LayerHierarchy*);
215 void detachHierarchyFromRelativeParent(LayerHierarchy*);
216
217 void onLayerDestroyed(RequestedLayerState* layer);
218 void updateMirrorLayer(RequestedLayerState* layer);
219 LayerHierarchy* getHierarchyFromId(uint32_t layerId, bool crashOnFailure = true);
220 std::unordered_map<uint32_t, LayerHierarchy*> mLayerIdToHierarchy;
221 std::vector<std::unique_ptr<LayerHierarchy>> mHierarchies;
222 LayerHierarchy mRoot{nullptr};
223 LayerHierarchy mOffscreenRoot{nullptr};
224};
225
226} // namespace android::surfaceflinger::frontend