blob: 37cda3efcb73b533817f7f254ef3597b5fbf06f1 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +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#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19
Vishnu Nair0808ae62023-08-07 21:42:42 -070020#include <gui/fake/BufferData.h>
Vishnu Nair47b7bb42023-09-29 16:27:33 -070021#include <renderengine/mock/FakeExternalTexture.h>
Vishnu Naird9e4f462023-10-06 04:05:45 +000022#include <ui/ShadowSettings.h>
Vishnu Nair0808ae62023-08-07 21:42:42 -070023
Vishnu Nair1391de22023-03-05 19:56:14 -080024#include "Client.h" // temporarily needed for LayerCreationArgs
25#include "FrontEnd/LayerCreationArgs.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000026#include "FrontEnd/LayerHierarchy.h"
27#include "FrontEnd/LayerLifecycleManager.h"
Vishnu Nair532d6452024-07-14 22:05:12 -070028#include "LayerLifecycleManagerHelper.h"
29
Vishnu Nair47b7bb42023-09-29 16:27:33 -070030#include "FrontEnd/LayerSnapshotBuilder.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000031
32namespace android::surfaceflinger::frontend {
33
Vishnu Nair532d6452024-07-14 22:05:12 -070034class LayerHierarchyTestBase : public testing::Test, public LayerLifecycleManagerHelper {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000035protected:
Vishnu Nair532d6452024-07-14 22:05:12 -070036 LayerHierarchyTestBase() : LayerLifecycleManagerHelper(mLifecycleManager) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000037 // 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
Vishnu Nair8fc721b2022-12-22 20:06:32 +000060 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
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
Vishnu Nair8fc721b2022-12-22 20:06:32 +000081 void updateAndVerify(LayerHierarchyBuilder& hierarchyBuilder) {
Vishnu Naira0292282023-12-16 14:32:00 -080082 hierarchyBuilder.update(mLifecycleManager);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000083 mLifecycleManager.commitChanges();
84
85 // rebuild layer hierarchy from scratch and verify that it matches the updated state.
Vishnu Naira0292282023-12-16 14:32:00 -080086 LayerHierarchyBuilder newBuilder;
87 newBuilder.update(mLifecycleManager);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000088 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
Vishnu Nair8fc721b2022-12-22 20:06:32 +000096 LayerLifecycleManager mLifecycleManager;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000097};
98
Vishnu Nair47b7bb42023-09-29 16:27:33 -070099class LayerSnapshotTestBase : public LayerHierarchyTestBase {
100protected:
101 LayerSnapshotTestBase() : LayerHierarchyTestBase() {}
102
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700103 void update(LayerSnapshotBuilder& snapshotBuilder) {
Vishnu Naira0292282023-12-16 14:32:00 -0800104 mHierarchyBuilder.update(mLifecycleManager);
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700105 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
Vishnu Naira0292282023-12-16 14:32:00 -0800119 LayerHierarchyBuilder mHierarchyBuilder;
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700120
121 DisplayInfos mFrontEndDisplayInfos;
122 bool mHasDisplayChanges = false;
123
Vishnu Naird9e4f462023-10-06 04:05:45 +0000124 ShadowSettings globalShadowSettings;
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700125};
126
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000127} // namespace android::surfaceflinger::frontend