Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 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/LayerCreationArgs.h" |
| 20 | #include "RequestedLayerState.h" |
| 21 | #include "ftl/small_vector.h" |
| 22 | |
| 23 | namespace android::surfaceflinger::frontend { |
| 24 | class 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. |
| 42 | class LayerHierarchy { |
| 43 | public: |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 44 | enum Variant : uint32_t { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 45 | Attached, |
| 46 | Detached, |
| 47 | Relative, |
| 48 | Mirror, |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 49 | ftl_first = Attached, |
| 50 | ftl_last = Mirror, |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 51 | }; |
| 52 | // Represents a unique path to a node. |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame^] | 53 | // 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} |
| 60 | // │ └─ E {Traversal path id = 5} |
| 61 | // ├─ F (Mirrors B) {Traversal path id = 6} |
| 62 | // └─ G (Mirrors F) {Traversal path id = 7} |
| 63 | // |
| 64 | // C, D and E can be traversed via B or via F then B or via G then F then B. |
| 65 | // Depending on how the node is reached, its properties such as geometry or visibility might be |
| 66 | // different. And we can uniquely identify the node by keeping track of the nodes leading up to |
| 67 | // it. But to be more efficient we only need to track the nodes id and the top mirror root path. |
| 68 | // So C for example, would have the following unique traversal paths: |
| 69 | // - {Traversal path id = 3} |
| 70 | // - {Traversal path id = 3, mirrorRootId = 6} |
| 71 | // - {Traversal path id = 3, mirrorRootId = 7} |
| 72 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 73 | 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 Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame^] | 78 | uint32_t mirrorRootId = UNASSIGNED_LAYER_ID; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 79 | // 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 Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 88 | // See isAttached() |
| 89 | bool detached = false; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 90 | bool hasRelZLoop() const { return invalidRelativeRootId != UNASSIGNED_LAYER_ID; } |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 91 | // 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 Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame^] | 96 | bool isClone() const { return mirrorRootId != UNASSIGNED_LAYER_ID; } |
| 97 | TraversalPath getMirrorRoot() const; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 98 | |
| 99 | bool operator==(const TraversalPath& other) const { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame^] | 100 | return id == other.id && mirrorRootId == other.mirrorRootId; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 101 | } |
| 102 | std::string toString() const; |
| 103 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 104 | static const TraversalPath ROOT; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // Helper class to add nodes to an existing traversal id and removes the |
| 108 | // node when it goes out of scope. |
| 109 | class ScopedAddToTraversalPath { |
| 110 | public: |
| 111 | ScopedAddToTraversalPath(TraversalPath& traversalPath, uint32_t layerId, |
| 112 | LayerHierarchy::Variant variantArg); |
| 113 | ~ScopedAddToTraversalPath(); |
| 114 | |
| 115 | private: |
| 116 | TraversalPath& mTraversalPath; |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame^] | 117 | TraversalPath mParentPath; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 118 | }; |
| 119 | LayerHierarchy(RequestedLayerState* layer); |
| 120 | |
| 121 | // Visitor function that provides the hierarchy node and a traversal id which uniquely |
| 122 | // identifies how was visited. The hierarchy contains a pointer to the RequestedLayerState. |
| 123 | // Return false to stop traversing down the hierarchy. |
| 124 | typedef std::function<bool(const LayerHierarchy& hierarchy, |
| 125 | const LayerHierarchy::TraversalPath& traversalPath)> |
| 126 | Visitor; |
| 127 | |
| 128 | // Traverse the hierarchy and visit all child variants. |
| 129 | void traverse(const Visitor& visitor) const { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 130 | TraversalPath root = TraversalPath::ROOT; |
| 131 | traverse(visitor, root); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Traverse the hierarchy in z-order, skipping children that have relative parents. |
| 135 | void traverseInZOrder(const Visitor& visitor) const { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 136 | TraversalPath root = TraversalPath::ROOT; |
| 137 | traverseInZOrder(visitor, root); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | const RequestedLayerState* getLayer() const; |
| 141 | std::string getDebugString(const char* prefix = "") const; |
| 142 | std::string getDebugStringShort() const; |
| 143 | // Traverse the hierarchy and return true if loops are found. The outInvalidRelativeRoot |
| 144 | // will contain the first relative root that was visited twice in a traversal. |
| 145 | bool hasRelZLoop(uint32_t& outInvalidRelativeRoot) const; |
| 146 | std::vector<std::pair<LayerHierarchy*, Variant>> mChildren; |
| 147 | |
| 148 | private: |
| 149 | friend LayerHierarchyBuilder; |
| 150 | LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly); |
| 151 | void addChild(LayerHierarchy*, LayerHierarchy::Variant); |
| 152 | void removeChild(LayerHierarchy*); |
| 153 | void sortChildrenByZOrder(); |
| 154 | void updateChild(LayerHierarchy*, LayerHierarchy::Variant); |
| 155 | void traverseInZOrder(const Visitor& visitor, LayerHierarchy::TraversalPath& parent) const; |
| 156 | void traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& parent) const; |
| 157 | |
| 158 | const RequestedLayerState* mLayer; |
| 159 | LayerHierarchy* mParent = nullptr; |
| 160 | LayerHierarchy* mRelativeParent = nullptr; |
| 161 | }; |
| 162 | |
| 163 | // Given a list of RequestedLayerState, this class will build a root hierarchy and an |
| 164 | // offscreen hierarchy. The builder also has an update method which can update an existing |
| 165 | // hierarchy from a list of RequestedLayerState and associated change flags. |
| 166 | class LayerHierarchyBuilder { |
| 167 | public: |
| 168 | LayerHierarchyBuilder(const std::vector<std::unique_ptr<RequestedLayerState>>&); |
| 169 | void update(const std::vector<std::unique_ptr<RequestedLayerState>>& layers, |
| 170 | const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers); |
| 171 | LayerHierarchy getPartialHierarchy(uint32_t, bool childrenOnly) const; |
| 172 | const LayerHierarchy& getHierarchy() const; |
| 173 | const LayerHierarchy& getOffscreenHierarchy() const; |
| 174 | std::string getDebugString(uint32_t layerId, uint32_t depth = 0) const; |
| 175 | |
| 176 | private: |
| 177 | void onLayerAdded(RequestedLayerState* layer); |
| 178 | void attachToParent(LayerHierarchy*); |
| 179 | void detachFromParent(LayerHierarchy*); |
| 180 | void attachToRelativeParent(LayerHierarchy*); |
| 181 | void detachFromRelativeParent(LayerHierarchy*); |
| 182 | void attachHierarchyToRelativeParent(LayerHierarchy*); |
| 183 | void detachHierarchyFromRelativeParent(LayerHierarchy*); |
| 184 | |
| 185 | void onLayerDestroyed(RequestedLayerState* layer); |
| 186 | void updateMirrorLayer(RequestedLayerState* layer); |
| 187 | LayerHierarchy* getHierarchyFromId(uint32_t layerId, bool crashOnFailure = true); |
| 188 | std::unordered_map<uint32_t, LayerHierarchy*> mLayerIdToHierarchy; |
| 189 | std::vector<std::unique_ptr<LayerHierarchy>> mHierarchies; |
| 190 | LayerHierarchy mRoot{nullptr}; |
| 191 | LayerHierarchy mOffscreenRoot{nullptr}; |
| 192 | }; |
| 193 | |
| 194 | } // namespace android::surfaceflinger::frontend |