blob: db0b907256210d39f397049079a60f3cdeff5995 [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); }
Vishnu Nair92990e22023-02-24 20:01:05 +0000103 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath path) {
104 return mSnapshotBuilder.getSnapshot(path);
105 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000106
107 LayerHierarchyBuilder mHierarchyBuilder{{}};
108 LayerSnapshotBuilder mSnapshotBuilder;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000109 display::DisplayMap<ui::LayerStack, frontend::DisplayInfo> mFrontEndDisplayInfos;
110 renderengine::ShadowSettings globalShadowSettings;
111 static const std::vector<uint32_t> STARTING_ZORDER;
112};
113const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1, 11, 111, 12, 121,
114 122, 1221, 13, 2};
115
116TEST_F(LayerSnapshotTest, buildSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000117 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
118 .layerLifecycleManager = mLifecycleManager,
119 .includeMetadata = false,
120 .displays = mFrontEndDisplayInfos,
121 .globalShadowSettings = globalShadowSettings,
122 .supportedLayerGenericMetadata = {},
123 .genericLayerMetadataKeyMap = {}};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000124 LayerSnapshotBuilder builder(args);
125}
126
127TEST_F(LayerSnapshotTest, updateSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000128 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
129 .layerLifecycleManager = mLifecycleManager,
130 .includeMetadata = false,
131 .displays = mFrontEndDisplayInfos,
132 .globalShadowSettings = globalShadowSettings,
133 .supportedLayerGenericMetadata = {},
134 .genericLayerMetadataKeyMap = {}
135
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000136 };
137
138 LayerSnapshotBuilder builder;
139 builder.update(args);
140}
141
142// update using parent snapshot data
143TEST_F(LayerSnapshotTest, croppedByParent) {
144 /// MAKE ALL LAYERS VISIBLE BY DEFAULT
145 DisplayInfo info;
146 info.info.logicalHeight = 100;
147 info.info.logicalWidth = 200;
148 mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info);
149 Rect layerCrop(0, 0, 10, 20);
150 setCrop(11, layerCrop);
151 EXPECT_TRUE(mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Geometry));
152 UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(mSnapshotBuilder, STARTING_ZORDER);
153 EXPECT_EQ(getSnapshot(11)->geomCrop, layerCrop);
154 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, layerCrop.toFloatRect());
155 float maxHeight = static_cast<float>(info.info.logicalHeight * 10);
156 float maxWidth = static_cast<float>(info.info.logicalWidth * 10);
157
158 FloatRect maxDisplaySize(-maxWidth, -maxHeight, maxWidth, maxHeight);
159 EXPECT_EQ(getSnapshot(1)->geomLayerBounds, maxDisplaySize);
160}
161
162// visibility tests
163TEST_F(LayerSnapshotTest, newLayerHiddenByPolicy) {
164 createLayer(112, 11);
165 hideLayer(112);
166 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
167
168 showLayer(112);
169 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 112, 12, 121, 122, 1221, 13, 2});
170}
171
172TEST_F(LayerSnapshotTest, hiddenByParent) {
173 hideLayer(11);
174 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
175}
176
177TEST_F(LayerSnapshotTest, reparentShowsChild) {
178 hideLayer(11);
179 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
180
181 showLayer(11);
182 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
183}
184
185TEST_F(LayerSnapshotTest, reparentHidesChild) {
186 hideLayer(11);
187 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
188
189 reparentLayer(121, 11);
190 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 122, 1221, 13, 2});
191}
192
193TEST_F(LayerSnapshotTest, unHidingUpdatesSnapshot) {
194 hideLayer(11);
195 Rect crop(1, 2, 3, 4);
196 setCrop(111, crop);
197 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
198
199 showLayer(11);
200 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
201 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, crop.toFloatRect());
202}
203
204TEST_F(LayerSnapshotTest, childBehindParentCanBeHiddenByParent) {
205 setZ(111, -1);
206 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 111, 11, 12, 121, 122, 1221, 13, 2});
207
208 hideLayer(11);
209 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
210}
211
212// relative tests
213TEST_F(LayerSnapshotTest, RelativeParentCanHideChild) {
214 reparentRelativeLayer(13, 11);
215 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
216
217 hideLayer(11);
218 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
219}
220
221TEST_F(LayerSnapshotTest, ReparentingToHiddenRelativeParentHidesChild) {
222 hideLayer(11);
223 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
224 reparentRelativeLayer(13, 11);
225 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
226}
227
228TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) {
229 setAlpha(1, 0.5);
230 setAlpha(122, 0.5);
231 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
232 EXPECT_EQ(getSnapshot(12)->alpha, 0.5f);
233 EXPECT_EQ(getSnapshot(1221)->alpha, 0.25f);
234}
235
236// Change states
237TEST_F(LayerSnapshotTest, UpdateClearsPreviousChangeStates) {
238 setCrop(1, Rect(1, 2, 3, 4));
239 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
240 EXPECT_TRUE(getSnapshot(1)->changes.get() != 0);
241 EXPECT_TRUE(getSnapshot(11)->changes.get() != 0);
242 setCrop(2, Rect(1, 2, 3, 4));
243 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
244 EXPECT_TRUE(getSnapshot(2)->changes.get() != 0);
245 EXPECT_TRUE(getSnapshot(1)->changes.get() == 0);
246 EXPECT_TRUE(getSnapshot(11)->changes.get() == 0);
247}
248
249TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) {
250 setColor(11, {1._hf, 0._hf, 0._hf});
251 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
252 EXPECT_TRUE(getSnapshot(11)->changes.get() != 0);
253 EXPECT_TRUE(getSnapshot(1)->changes.get() == 0);
254 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
255 EXPECT_TRUE(getSnapshot(11)->changes.get() == 0);
256}
257
258TEST_F(LayerSnapshotTest, FastPathSetsChangeFlagToContent) {
259 setColor(1, {1._hf, 0._hf, 0._hf});
260 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
261 EXPECT_EQ(getSnapshot(1)->changes, RequestedLayerState::Changes::Content);
262}
263
Vishnu Naircfb2d252023-01-19 04:44:02 +0000264TEST_F(LayerSnapshotTest, GameMode) {
265 std::vector<TransactionState> transactions;
266 transactions.emplace_back();
267 transactions.back().states.push_back({});
268 transactions.back().states.front().state.what = layer_state_t::eMetadataChanged;
269 transactions.back().states.front().state.metadata = LayerMetadata();
270 transactions.back().states.front().state.metadata.setInt32(METADATA_GAME_MODE, 42);
271 transactions.back().states.front().state.surface = mHandles[1];
272 transactions.back().states.front().state.layerId = static_cast<int32_t>(1);
273 mLifecycleManager.applyTransactions(transactions);
274 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
275 EXPECT_EQ(static_cast<int32_t>(getSnapshot(1)->gameMode), 42);
276 EXPECT_EQ(static_cast<int32_t>(getSnapshot(11)->gameMode), 42);
277}
278
279TEST_F(LayerSnapshotTest, NoLayerVoteForParentWithChildVotes) {
280 // ROOT
281 // ├── 1
282 // │ ├── 11 (frame rate set)
283 // │ │ └── 111
284 // │ ├── 12
285 // │ │ ├── 121
286 // │ │ └── 122
287 // │ │ └── 1221
288 // │ └── 13
289 // └── 2
290
291 std::vector<TransactionState> transactions;
292 transactions.emplace_back();
293 transactions.back().states.push_back({});
294 transactions.back().states.front().state.what = layer_state_t::eFrameRateChanged;
295 transactions.back().states.front().state.frameRate = 90.0;
296 transactions.back().states.front().state.frameRateCompatibility =
297 ANATIVEWINDOW_FRAME_RATE_EXACT;
298 transactions.back().states.front().state.changeFrameRateStrategy =
299 ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS;
300 transactions.back().states.front().state.surface = mHandles[11];
301 transactions.back().states.front().state.layerId = static_cast<int32_t>(11);
302 mLifecycleManager.applyTransactions(transactions);
303 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
304
305 EXPECT_EQ(getSnapshot(11)->frameRate.rate.getIntValue(), 90);
306 EXPECT_EQ(getSnapshot(11)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::Exact);
307 EXPECT_EQ(getSnapshot(111)->frameRate.rate.getIntValue(), 90);
308 EXPECT_EQ(getSnapshot(111)->frameRate.type,
309 scheduler::LayerInfo::FrameRateCompatibility::Exact);
310 EXPECT_EQ(getSnapshot(1)->frameRate.rate.getIntValue(), 0);
311 EXPECT_EQ(getSnapshot(1)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::NoVote);
312}
313
Vishnu Naira9c43762023-01-27 19:10:25 +0000314// Display Mirroring Tests
315// tree with 3 levels of children
316// ROOT (DISPLAY 0)
317// ├── 1
318// │ ├── 11
319// │ │ └── 111
320// │ ├── 12 (has skip screenshot flag)
321// │ │ ├── 121
322// │ │ └── 122
323// │ │ └── 1221
324// │ └── 13
325// └── 2
326// ROOT (DISPLAY 1)
327// └── 3 (mirrors display 0)
Vishnu Nair92990e22023-02-24 20:01:05 +0000328TEST_F(LayerSnapshotTest, displayMirrorRespectsLayerSkipScreenshotFlag) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000329 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
330 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
331 setLayerStack(3, 1);
332
333 std::vector<uint32_t> expected = {3, 1, 11, 111, 13, 2, 1, 11, 111, 12, 121, 122, 1221, 13, 2};
334 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
335}
336
Vishnu Nair92990e22023-02-24 20:01:05 +0000337// ROOT (DISPLAY 0)
338// ├── 1
339// │ ├── 11
340// │ │ └── 111
341// │ └── 13
342// └── 2
343// ROOT (DISPLAY 3)
344// └── 3 (mirrors display 0)
345TEST_F(LayerSnapshotTest, mirrorLayerGetsCorrectLayerStack) {
346 reparentLayer(12, UNASSIGNED_LAYER_ID);
347 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
348 setLayerStack(3, 3);
349 createDisplayMirrorLayer(4, ui::LayerStack::fromValue(0));
350 setLayerStack(4, 4);
351
352 std::vector<uint32_t> expected = {4, 1, 11, 111, 13, 2, 3, 1, 11,
353 111, 13, 2, 1, 11, 111, 13, 2};
354 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
355 EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootId = 3})->outputFilter.layerStack.id, 3u);
356 EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootId = 4})->outputFilter.layerStack.id, 4u);
357}
358
359// ROOT (DISPLAY 0)
360// ├── 1 (crop 50x50)
361// │ ├── 11
362// │ │ └── 111
363// │ └── 13
364// └── 2
365// ROOT (DISPLAY 3)
366// └── 3 (mirrors display 0) (crop 100x100)
367TEST_F(LayerSnapshotTest, mirrorLayerTouchIsCroppedByMirrorRoot) {
368 reparentLayer(12, UNASSIGNED_LAYER_ID);
369 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
370 setLayerStack(3, 3);
371 setCrop(1, Rect{50, 50});
372 setCrop(3, Rect{100, 100});
373 setCrop(111, Rect{200, 200});
374 Region touch{Rect{0, 0, 1000, 1000}};
375 setTouchableRegion(111, touch);
376 std::vector<uint32_t> expected = {3, 1, 11, 111, 13, 2, 1, 11, 111, 13, 2};
377 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
378 EXPECT_TRUE(getSnapshot({.id = 111})->inputInfo.touchableRegion.hasSameRects(touch));
379 Region touchCroppedByMirrorRoot{Rect{0, 0, 50, 50}};
380 EXPECT_TRUE(getSnapshot({.id = 111, .mirrorRootId = 3})
381 ->inputInfo.touchableRegion.hasSameRects(touchCroppedByMirrorRoot));
382}
383
384TEST_F(LayerSnapshotTest, canRemoveDisplayMirror) {
385 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
386 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
387 setLayerStack(3, 1);
388 std::vector<uint32_t> expected = {3, 1, 11, 111, 13, 2, 1, 11, 111, 12, 121, 122, 1221, 13, 2};
389 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
390 destroyLayerHandle(3);
391 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
392}
393
Vishnu Nairfccd6362023-02-24 23:39:53 +0000394TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterMirroring) {
395 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
396 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
397 setLayerStack(3, 1);
398 std::vector<uint32_t> expected = {3, 1, 11, 111, 12, 121, 122, 1221, 13, 2,
399 1, 11, 111, 12, 121, 122, 1221, 13, 2};
400 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
401 destroyLayerHandle(3);
402 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
403
404 EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
405}
406
407// Rel z doesn't create duplicate snapshots but this is for completeness
408TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterRelZ) {
409 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
410 reparentRelativeLayer(13, 11);
411 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
412 setZ(13, 0);
413 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
414
415 EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
416}
417
418TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterLayerDestruction) {
419 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
420 destroyLayerHandle(2);
421 destroyLayerHandle(122);
422
423 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13};
424 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
425
426 EXPECT_LE(startingNumSnapshots - 2, mSnapshotBuilder.getSnapshots().size());
427}
428
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000429} // namespace android::surfaceflinger::frontend