blob: 5c8bd6f95d78bee7e0ab9aa5b3942e1ed5db20f3 [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
20#include "FrontEnd/LayerHandle.h"
21#include "FrontEnd/LayerHierarchy.h"
22#include "FrontEnd/LayerLifecycleManager.h"
23#include "FrontEnd/LayerSnapshotBuilder.h"
24#include "Layer.h"
25#include "LayerHierarchyTest.h"
26
27#define UPDATE_AND_VERIFY(BUILDER, ...) \
28 ({ \
29 SCOPED_TRACE(""); \
30 updateAndVerify((BUILDER), /*displayChanges=*/false, __VA_ARGS__); \
31 })
32
33#define UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(BUILDER, ...) \
34 ({ \
35 SCOPED_TRACE(""); \
36 updateAndVerify((BUILDER), /*displayChanges=*/true, __VA_ARGS__); \
37 })
38
39namespace android::surfaceflinger::frontend {
40
Vishnu Naircfb2d252023-01-19 04:44:02 +000041using ftl::Flags;
42using namespace ftl::flag_operators;
43
Vishnu Nair8fc721b2022-12-22 20:06:32 +000044// To run test:
45/**
46 mp :libsurfaceflinger_unittest && adb sync; adb shell \
47 /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \
48 --gtest_filter="LayerSnapshotTest.*" --gtest_brief=1
49*/
50
51class LayerSnapshotTest : public LayerHierarchyTestBase {
52protected:
53 LayerSnapshotTest() : LayerHierarchyTestBase() {
54 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
55 }
56
57 void createRootLayer(uint32_t id) override {
58 LayerHierarchyTestBase::createRootLayer(id);
59 setColor(id);
60 }
61
62 void createLayer(uint32_t id, uint32_t parentId) override {
63 LayerHierarchyTestBase::createLayer(id, parentId);
64 setColor(parentId);
65 }
66
67 void mirrorLayer(uint32_t id, uint32_t parent, uint32_t layerToMirror) override {
68 LayerHierarchyTestBase::mirrorLayer(id, parent, layerToMirror);
69 setColor(id);
70 }
71
72 void updateAndVerify(LayerSnapshotBuilder& actualBuilder, bool hasDisplayChanges,
73 const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder) {
74 if (mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)) {
75 mHierarchyBuilder.update(mLifecycleManager.getLayers(),
76 mLifecycleManager.getDestroyedLayers());
77 }
Vishnu Nairc765c6c2023-02-23 00:08:01 +000078 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
79 .layerLifecycleManager = mLifecycleManager,
80 .includeMetadata = false,
81 .displays = mFrontEndDisplayInfos,
82 .displayChanges = hasDisplayChanges,
83 .globalShadowSettings = globalShadowSettings,
84 .supportedLayerGenericMetadata = {},
85 .genericLayerMetadataKeyMap = {}};
Vishnu Nair8fc721b2022-12-22 20:06:32 +000086 actualBuilder.update(args);
87
88 // rebuild layer snapshots from scratch and verify that it matches the updated state.
89 LayerSnapshotBuilder expectedBuilder(args);
90 mLifecycleManager.commitChanges();
91 ASSERT_TRUE(expectedBuilder.getSnapshots().size() > 0);
92 ASSERT_TRUE(actualBuilder.getSnapshots().size() > 0);
93
Vishnu Nair8fc721b2022-12-22 20:06:32 +000094 std::vector<uint32_t> actualVisibleLayerIdsInZOrder;
Vishnu Naircfb2d252023-01-19 04:44:02 +000095 actualBuilder.forEachVisibleSnapshot(
96 [&actualVisibleLayerIdsInZOrder](const LayerSnapshot& snapshot) {
97 actualVisibleLayerIdsInZOrder.push_back(snapshot.path.id);
98 });
Vishnu Nair8fc721b2022-12-22 20:06:32 +000099 EXPECT_EQ(expectedVisibleLayerIdsInZOrder, actualVisibleLayerIdsInZOrder);
100 }
101
102 LayerSnapshot* getSnapshot(uint32_t layerId) { return mSnapshotBuilder.getSnapshot(layerId); }
103
104 LayerHierarchyBuilder mHierarchyBuilder{{}};
105 LayerSnapshotBuilder mSnapshotBuilder;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000106 display::DisplayMap<ui::LayerStack, frontend::DisplayInfo> mFrontEndDisplayInfos;
107 renderengine::ShadowSettings globalShadowSettings;
108 static const std::vector<uint32_t> STARTING_ZORDER;
109};
110const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1, 11, 111, 12, 121,
111 122, 1221, 13, 2};
112
113TEST_F(LayerSnapshotTest, buildSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000114 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
115 .layerLifecycleManager = mLifecycleManager,
116 .includeMetadata = false,
117 .displays = mFrontEndDisplayInfos,
118 .globalShadowSettings = globalShadowSettings,
119 .supportedLayerGenericMetadata = {},
120 .genericLayerMetadataKeyMap = {}};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000121 LayerSnapshotBuilder builder(args);
122}
123
124TEST_F(LayerSnapshotTest, updateSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000125 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
126 .layerLifecycleManager = mLifecycleManager,
127 .includeMetadata = false,
128 .displays = mFrontEndDisplayInfos,
129 .globalShadowSettings = globalShadowSettings,
130 .supportedLayerGenericMetadata = {},
131 .genericLayerMetadataKeyMap = {}
132
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000133 };
134
135 LayerSnapshotBuilder builder;
136 builder.update(args);
137}
138
139// update using parent snapshot data
140TEST_F(LayerSnapshotTest, croppedByParent) {
141 /// MAKE ALL LAYERS VISIBLE BY DEFAULT
142 DisplayInfo info;
143 info.info.logicalHeight = 100;
144 info.info.logicalWidth = 200;
145 mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info);
146 Rect layerCrop(0, 0, 10, 20);
147 setCrop(11, layerCrop);
148 EXPECT_TRUE(mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Geometry));
149 UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(mSnapshotBuilder, STARTING_ZORDER);
150 EXPECT_EQ(getSnapshot(11)->geomCrop, layerCrop);
151 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, layerCrop.toFloatRect());
152 float maxHeight = static_cast<float>(info.info.logicalHeight * 10);
153 float maxWidth = static_cast<float>(info.info.logicalWidth * 10);
154
155 FloatRect maxDisplaySize(-maxWidth, -maxHeight, maxWidth, maxHeight);
156 EXPECT_EQ(getSnapshot(1)->geomLayerBounds, maxDisplaySize);
157}
158
159// visibility tests
160TEST_F(LayerSnapshotTest, newLayerHiddenByPolicy) {
161 createLayer(112, 11);
162 hideLayer(112);
163 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
164
165 showLayer(112);
166 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 112, 12, 121, 122, 1221, 13, 2});
167}
168
169TEST_F(LayerSnapshotTest, hiddenByParent) {
170 hideLayer(11);
171 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
172}
173
174TEST_F(LayerSnapshotTest, reparentShowsChild) {
175 hideLayer(11);
176 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
177
178 showLayer(11);
179 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
180}
181
182TEST_F(LayerSnapshotTest, reparentHidesChild) {
183 hideLayer(11);
184 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
185
186 reparentLayer(121, 11);
187 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 122, 1221, 13, 2});
188}
189
190TEST_F(LayerSnapshotTest, unHidingUpdatesSnapshot) {
191 hideLayer(11);
192 Rect crop(1, 2, 3, 4);
193 setCrop(111, crop);
194 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
195
196 showLayer(11);
197 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
198 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, crop.toFloatRect());
199}
200
201TEST_F(LayerSnapshotTest, childBehindParentCanBeHiddenByParent) {
202 setZ(111, -1);
203 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 111, 11, 12, 121, 122, 1221, 13, 2});
204
205 hideLayer(11);
206 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
207}
208
209// relative tests
210TEST_F(LayerSnapshotTest, RelativeParentCanHideChild) {
211 reparentRelativeLayer(13, 11);
212 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
213
214 hideLayer(11);
215 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
216}
217
218TEST_F(LayerSnapshotTest, ReparentingToHiddenRelativeParentHidesChild) {
219 hideLayer(11);
220 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
221 reparentRelativeLayer(13, 11);
222 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
223}
224
225TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) {
226 setAlpha(1, 0.5);
227 setAlpha(122, 0.5);
228 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
229 EXPECT_EQ(getSnapshot(12)->alpha, 0.5f);
230 EXPECT_EQ(getSnapshot(1221)->alpha, 0.25f);
231}
232
233// Change states
234TEST_F(LayerSnapshotTest, UpdateClearsPreviousChangeStates) {
235 setCrop(1, Rect(1, 2, 3, 4));
236 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
237 EXPECT_TRUE(getSnapshot(1)->changes.get() != 0);
238 EXPECT_TRUE(getSnapshot(11)->changes.get() != 0);
239 setCrop(2, Rect(1, 2, 3, 4));
240 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
241 EXPECT_TRUE(getSnapshot(2)->changes.get() != 0);
242 EXPECT_TRUE(getSnapshot(1)->changes.get() == 0);
243 EXPECT_TRUE(getSnapshot(11)->changes.get() == 0);
244}
245
246TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) {
247 setColor(11, {1._hf, 0._hf, 0._hf});
248 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
249 EXPECT_TRUE(getSnapshot(11)->changes.get() != 0);
250 EXPECT_TRUE(getSnapshot(1)->changes.get() == 0);
251 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
252 EXPECT_TRUE(getSnapshot(11)->changes.get() == 0);
253}
254
255TEST_F(LayerSnapshotTest, FastPathSetsChangeFlagToContent) {
256 setColor(1, {1._hf, 0._hf, 0._hf});
257 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
258 EXPECT_EQ(getSnapshot(1)->changes, RequestedLayerState::Changes::Content);
259}
260
Vishnu Naircfb2d252023-01-19 04:44:02 +0000261TEST_F(LayerSnapshotTest, GameMode) {
262 std::vector<TransactionState> transactions;
263 transactions.emplace_back();
264 transactions.back().states.push_back({});
265 transactions.back().states.front().state.what = layer_state_t::eMetadataChanged;
266 transactions.back().states.front().state.metadata = LayerMetadata();
267 transactions.back().states.front().state.metadata.setInt32(METADATA_GAME_MODE, 42);
268 transactions.back().states.front().state.surface = mHandles[1];
269 transactions.back().states.front().state.layerId = static_cast<int32_t>(1);
270 mLifecycleManager.applyTransactions(transactions);
271 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
272 EXPECT_EQ(static_cast<int32_t>(getSnapshot(1)->gameMode), 42);
273 EXPECT_EQ(static_cast<int32_t>(getSnapshot(11)->gameMode), 42);
274}
275
276TEST_F(LayerSnapshotTest, NoLayerVoteForParentWithChildVotes) {
277 // ROOT
278 // ├── 1
279 // │ ├── 11 (frame rate set)
280 // │ │ └── 111
281 // │ ├── 12
282 // │ │ ├── 121
283 // │ │ └── 122
284 // │ │ └── 1221
285 // │ └── 13
286 // └── 2
287
288 std::vector<TransactionState> transactions;
289 transactions.emplace_back();
290 transactions.back().states.push_back({});
291 transactions.back().states.front().state.what = layer_state_t::eFrameRateChanged;
292 transactions.back().states.front().state.frameRate = 90.0;
293 transactions.back().states.front().state.frameRateCompatibility =
294 ANATIVEWINDOW_FRAME_RATE_EXACT;
295 transactions.back().states.front().state.changeFrameRateStrategy =
296 ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS;
297 transactions.back().states.front().state.surface = mHandles[11];
298 transactions.back().states.front().state.layerId = static_cast<int32_t>(11);
299 mLifecycleManager.applyTransactions(transactions);
300 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
301
302 EXPECT_EQ(getSnapshot(11)->frameRate.rate.getIntValue(), 90);
303 EXPECT_EQ(getSnapshot(11)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::Exact);
304 EXPECT_EQ(getSnapshot(111)->frameRate.rate.getIntValue(), 90);
305 EXPECT_EQ(getSnapshot(111)->frameRate.type,
306 scheduler::LayerInfo::FrameRateCompatibility::Exact);
307 EXPECT_EQ(getSnapshot(1)->frameRate.rate.getIntValue(), 0);
308 EXPECT_EQ(getSnapshot(1)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::NoVote);
309}
310
Vishnu Naira9c43762023-01-27 19:10:25 +0000311// Display Mirroring Tests
312// tree with 3 levels of children
313// ROOT (DISPLAY 0)
314// ├── 1
315// │ ├── 11
316// │ │ └── 111
317// │ ├── 12 (has skip screenshot flag)
318// │ │ ├── 121
319// │ │ └── 122
320// │ │ └── 1221
321// │ └── 13
322// └── 2
323// ROOT (DISPLAY 1)
324// └── 3 (mirrors display 0)
325TEST_F(LayerSnapshotTest, displayMirrorRespects) {
326 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
327 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
328 setLayerStack(3, 1);
329
330 std::vector<uint32_t> expected = {3, 1, 11, 111, 13, 2, 1, 11, 111, 12, 121, 122, 1221, 13, 2};
331 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
332}
333
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000334} // namespace android::surfaceflinger::frontend