xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/unittests/LayerHierarchyTest.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 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include <gui/fake/BufferData.h>
21 #include <renderengine/mock/FakeExternalTexture.h>
22 #include <ui/ShadowSettings.h>
23 
24 #include "Client.h" // temporarily needed for LayerCreationArgs
25 #include "FrontEnd/LayerCreationArgs.h"
26 #include "FrontEnd/LayerHierarchy.h"
27 #include "FrontEnd/LayerLifecycleManager.h"
28 #include "LayerLifecycleManagerHelper.h"
29 
30 #include "FrontEnd/LayerSnapshotBuilder.h"
31 
32 namespace android::surfaceflinger::frontend {
33 
34 class LayerHierarchyTestBase : public testing::Test, public LayerLifecycleManagerHelper {
35 protected:
LayerHierarchyTestBase()36     LayerHierarchyTestBase() : LayerLifecycleManagerHelper(mLifecycleManager) {
37         // tree with 3 levels of children
38         // ROOT
39         // ├── 1
40         // │   ├── 11
41         // │   │   └── 111
42         // │   ├── 12
43         // │   │   ├── 121
44         // │   │   └── 122
45         // │   │       └── 1221
46         // │   └── 13
47         // └── 2
48 
49         createRootLayer(1);
50         createRootLayer(2);
51         createLayer(11, 1);
52         createLayer(12, 1);
53         createLayer(13, 1);
54         createLayer(111, 11);
55         createLayer(121, 12);
56         createLayer(122, 12);
57         createLayer(1221, 122);
58     }
59 
getTraversalPath(const LayerHierarchy & hierarchy)60     std::vector<uint32_t> getTraversalPath(const LayerHierarchy& hierarchy) const {
61         std::vector<uint32_t> layerIds;
62         hierarchy.traverse([&layerIds = layerIds](const LayerHierarchy& hierarchy,
63                                                   const LayerHierarchy::TraversalPath&) -> bool {
64             layerIds.emplace_back(hierarchy.getLayer()->id);
65             return true;
66         });
67         return layerIds;
68     }
69 
getTraversalPathInZOrder(const LayerHierarchy & hierarchy)70     std::vector<uint32_t> getTraversalPathInZOrder(const LayerHierarchy& hierarchy) const {
71         std::vector<uint32_t> layerIds;
72         hierarchy.traverseInZOrder(
73                 [&layerIds = layerIds](const LayerHierarchy& hierarchy,
74                                        const LayerHierarchy::TraversalPath&) -> bool {
75                     layerIds.emplace_back(hierarchy.getLayer()->id);
76                     return true;
77                 });
78         return layerIds;
79     }
80 
updateAndVerify(LayerHierarchyBuilder & hierarchyBuilder)81     void updateAndVerify(LayerHierarchyBuilder& hierarchyBuilder) {
82         hierarchyBuilder.update(mLifecycleManager);
83         mLifecycleManager.commitChanges();
84 
85         // rebuild layer hierarchy from scratch and verify that it matches the updated state.
86         LayerHierarchyBuilder newBuilder;
87         newBuilder.update(mLifecycleManager);
88         EXPECT_EQ(getTraversalPath(hierarchyBuilder.getHierarchy()),
89                   getTraversalPath(newBuilder.getHierarchy()));
90         EXPECT_EQ(getTraversalPathInZOrder(hierarchyBuilder.getHierarchy()),
91                   getTraversalPathInZOrder(newBuilder.getHierarchy()));
92         EXPECT_FALSE(
93                 mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy));
94     }
95 
96     LayerLifecycleManager mLifecycleManager;
97 };
98 
99 class LayerSnapshotTestBase : public LayerHierarchyTestBase {
100 protected:
LayerSnapshotTestBase()101     LayerSnapshotTestBase() : LayerHierarchyTestBase() {}
102 
update(LayerSnapshotBuilder & snapshotBuilder)103     void update(LayerSnapshotBuilder& snapshotBuilder) {
104         mHierarchyBuilder.update(mLifecycleManager);
105         LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
106                                         .layerLifecycleManager = mLifecycleManager,
107                                         .includeMetadata = false,
108                                         .displays = mFrontEndDisplayInfos,
109                                         .displayChanges = mHasDisplayChanges,
110                                         .globalShadowSettings = globalShadowSettings,
111                                         .supportsBlur = true,
112                                         .supportedLayerGenericMetadata = {},
113                                         .genericLayerMetadataKeyMap = {}};
114         snapshotBuilder.update(args);
115 
116         mLifecycleManager.commitChanges();
117     }
118 
119     LayerHierarchyBuilder mHierarchyBuilder;
120 
121     DisplayInfos mFrontEndDisplayInfos;
122     bool mHasDisplayChanges = false;
123 
124     ShadowSettings globalShadowSettings;
125 };
126 
127 } // namespace android::surfaceflinger::frontend
128