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