blob: ca8d301879ad5723ee4e94dbb03114130fbb778f [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 Nair04f89692022-11-16 23:21:05 +000045 Attached,
46 Detached,
47 Relative,
48 Mirror,
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.
53 struct TraversalPath {
54 uint32_t id;
55 LayerHierarchy::Variant variant;
56 // Mirrored layers can have a different geometry than their parents so we need to track
57 // the mirror roots in the traversal.
58 ftl::SmallVector<uint32_t, 5> mirrorRootIds;
59 // Relative layers can be visited twice, once by their parent and then once again by
60 // their relative parent. We keep track of the roots here to detect any loops in the
61 // hierarchy. If a relative root already exists in the list while building the
62 // TraversalPath, it means that somewhere in the hierarchy two layers are relatively
63 // parented to each other.
64 ftl::SmallVector<uint32_t, 5> relativeRootIds;
65 // First duplicate relative root id found. If this is a valid layer id that means we are
66 // in a loop.
67 uint32_t invalidRelativeRootId = UNASSIGNED_LAYER_ID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000068 // See isAttached()
69 bool detached = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000070 bool hasRelZLoop() const { return invalidRelativeRootId != UNASSIGNED_LAYER_ID; }
Vishnu Nair8fc721b2022-12-22 20:06:32 +000071 // Returns true if this node is reached via one or more relative parents.
72 bool isRelative() const { return !relativeRootIds.empty(); }
73 // Returns true if the node or its parents are not Detached.
74 bool isAttached() const { return !detached; }
75 // Returns true if the node is a clone.
76 bool isClone() const { return !mirrorRootIds.empty(); }
Vishnu Nair04f89692022-11-16 23:21:05 +000077
78 bool operator==(const TraversalPath& other) const {
79 return id == other.id && mirrorRootIds == other.mirrorRootIds;
80 }
81 std::string toString() const;
82
Vishnu Nair8fc721b2022-12-22 20:06:32 +000083 static const TraversalPath ROOT;
Vishnu Nair04f89692022-11-16 23:21:05 +000084 };
85
86 // Helper class to add nodes to an existing traversal id and removes the
87 // node when it goes out of scope.
88 class ScopedAddToTraversalPath {
89 public:
90 ScopedAddToTraversalPath(TraversalPath& traversalPath, uint32_t layerId,
91 LayerHierarchy::Variant variantArg);
92 ~ScopedAddToTraversalPath();
93
94 private:
95 TraversalPath& mTraversalPath;
96 uint32_t mParentId;
97 LayerHierarchy::Variant mParentVariant;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000098 bool mParentDetached;
Vishnu Nair04f89692022-11-16 23:21:05 +000099 };
100 LayerHierarchy(RequestedLayerState* layer);
101
102 // Visitor function that provides the hierarchy node and a traversal id which uniquely
103 // identifies how was visited. The hierarchy contains a pointer to the RequestedLayerState.
104 // Return false to stop traversing down the hierarchy.
105 typedef std::function<bool(const LayerHierarchy& hierarchy,
106 const LayerHierarchy::TraversalPath& traversalPath)>
107 Visitor;
108
109 // Traverse the hierarchy and visit all child variants.
110 void traverse(const Visitor& visitor) const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000111 TraversalPath root = TraversalPath::ROOT;
112 traverse(visitor, root);
Vishnu Nair04f89692022-11-16 23:21:05 +0000113 }
114
115 // Traverse the hierarchy in z-order, skipping children that have relative parents.
116 void traverseInZOrder(const Visitor& visitor) const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000117 TraversalPath root = TraversalPath::ROOT;
118 traverseInZOrder(visitor, root);
Vishnu Nair04f89692022-11-16 23:21:05 +0000119 }
120
121 const RequestedLayerState* getLayer() const;
122 std::string getDebugString(const char* prefix = "") const;
123 std::string getDebugStringShort() const;
124 // Traverse the hierarchy and return true if loops are found. The outInvalidRelativeRoot
125 // will contain the first relative root that was visited twice in a traversal.
126 bool hasRelZLoop(uint32_t& outInvalidRelativeRoot) const;
127 std::vector<std::pair<LayerHierarchy*, Variant>> mChildren;
128
129private:
130 friend LayerHierarchyBuilder;
131 LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly);
132 void addChild(LayerHierarchy*, LayerHierarchy::Variant);
133 void removeChild(LayerHierarchy*);
134 void sortChildrenByZOrder();
135 void updateChild(LayerHierarchy*, LayerHierarchy::Variant);
136 void traverseInZOrder(const Visitor& visitor, LayerHierarchy::TraversalPath& parent) const;
137 void traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& parent) const;
138
139 const RequestedLayerState* mLayer;
140 LayerHierarchy* mParent = nullptr;
141 LayerHierarchy* mRelativeParent = nullptr;
142};
143
144// Given a list of RequestedLayerState, this class will build a root hierarchy and an
145// offscreen hierarchy. The builder also has an update method which can update an existing
146// hierarchy from a list of RequestedLayerState and associated change flags.
147class LayerHierarchyBuilder {
148public:
149 LayerHierarchyBuilder(const std::vector<std::unique_ptr<RequestedLayerState>>&);
150 void update(const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
151 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers);
152 LayerHierarchy getPartialHierarchy(uint32_t, bool childrenOnly) const;
153 const LayerHierarchy& getHierarchy() const;
154 const LayerHierarchy& getOffscreenHierarchy() const;
155 std::string getDebugString(uint32_t layerId, uint32_t depth = 0) const;
156
157private:
158 void onLayerAdded(RequestedLayerState* layer);
159 void attachToParent(LayerHierarchy*);
160 void detachFromParent(LayerHierarchy*);
161 void attachToRelativeParent(LayerHierarchy*);
162 void detachFromRelativeParent(LayerHierarchy*);
163 void attachHierarchyToRelativeParent(LayerHierarchy*);
164 void detachHierarchyFromRelativeParent(LayerHierarchy*);
165
166 void onLayerDestroyed(RequestedLayerState* layer);
167 void updateMirrorLayer(RequestedLayerState* layer);
168 LayerHierarchy* getHierarchyFromId(uint32_t layerId, bool crashOnFailure = true);
169 std::unordered_map<uint32_t, LayerHierarchy*> mLayerIdToHierarchy;
170 std::vector<std::unique_ptr<LayerHierarchy>> mHierarchies;
171 LayerHierarchy mRoot{nullptr};
172 LayerHierarchy mOffscreenRoot{nullptr};
173};
174
175} // namespace android::surfaceflinger::frontend