blob: 5da893ee62d45aa94f448b4374b3e9906a992f46 [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 Nair8fc721b2022-12-22 20:06:32 +000020#include "FrontEnd/LayerHierarchy.h"
21#include "FrontEnd/LayerLifecycleManager.h"
22#include "FrontEnd/LayerSnapshotBuilder.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000023#include "LayerHierarchyTest.h"
24
25#define UPDATE_AND_VERIFY(BUILDER, ...) \
26 ({ \
27 SCOPED_TRACE(""); \
28 updateAndVerify((BUILDER), /*displayChanges=*/false, __VA_ARGS__); \
29 })
30
31#define UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(BUILDER, ...) \
32 ({ \
33 SCOPED_TRACE(""); \
34 updateAndVerify((BUILDER), /*displayChanges=*/true, __VA_ARGS__); \
35 })
36
37namespace android::surfaceflinger::frontend {
38
Vishnu Naircfb2d252023-01-19 04:44:02 +000039using ftl::Flags;
40using namespace ftl::flag_operators;
41
Vishnu Nair8fc721b2022-12-22 20:06:32 +000042// To run test:
43/**
44 mp :libsurfaceflinger_unittest && adb sync; adb shell \
45 /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \
46 --gtest_filter="LayerSnapshotTest.*" --gtest_brief=1
47*/
48
49class LayerSnapshotTest : public LayerHierarchyTestBase {
50protected:
51 LayerSnapshotTest() : LayerHierarchyTestBase() {
52 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
53 }
54
55 void createRootLayer(uint32_t id) override {
56 LayerHierarchyTestBase::createRootLayer(id);
57 setColor(id);
58 }
59
60 void createLayer(uint32_t id, uint32_t parentId) override {
61 LayerHierarchyTestBase::createLayer(id, parentId);
62 setColor(parentId);
63 }
64
65 void mirrorLayer(uint32_t id, uint32_t parent, uint32_t layerToMirror) override {
66 LayerHierarchyTestBase::mirrorLayer(id, parent, layerToMirror);
67 setColor(id);
68 }
69
70 void updateAndVerify(LayerSnapshotBuilder& actualBuilder, bool hasDisplayChanges,
71 const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder) {
72 if (mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)) {
73 mHierarchyBuilder.update(mLifecycleManager.getLayers(),
74 mLifecycleManager.getDestroyedLayers());
75 }
Vishnu Nairc765c6c2023-02-23 00:08:01 +000076 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
77 .layerLifecycleManager = mLifecycleManager,
78 .includeMetadata = false,
79 .displays = mFrontEndDisplayInfos,
80 .displayChanges = hasDisplayChanges,
81 .globalShadowSettings = globalShadowSettings,
Vishnu Nair444f3952023-04-11 13:01:02 -070082 .supportsBlur = true,
Vishnu Nairc765c6c2023-02-23 00:08:01 +000083 .supportedLayerGenericMetadata = {},
84 .genericLayerMetadataKeyMap = {}};
Vishnu Nair8fc721b2022-12-22 20:06:32 +000085 actualBuilder.update(args);
86
87 // rebuild layer snapshots from scratch and verify that it matches the updated state.
88 LayerSnapshotBuilder expectedBuilder(args);
89 mLifecycleManager.commitChanges();
90 ASSERT_TRUE(expectedBuilder.getSnapshots().size() > 0);
91 ASSERT_TRUE(actualBuilder.getSnapshots().size() > 0);
92
Vishnu Nair8fc721b2022-12-22 20:06:32 +000093 std::vector<uint32_t> actualVisibleLayerIdsInZOrder;
Vishnu Naircfb2d252023-01-19 04:44:02 +000094 actualBuilder.forEachVisibleSnapshot(
95 [&actualVisibleLayerIdsInZOrder](const LayerSnapshot& snapshot) {
96 actualVisibleLayerIdsInZOrder.push_back(snapshot.path.id);
97 });
Vishnu Nair8fc721b2022-12-22 20:06:32 +000098 EXPECT_EQ(expectedVisibleLayerIdsInZOrder, actualVisibleLayerIdsInZOrder);
99 }
100
101 LayerSnapshot* getSnapshot(uint32_t layerId) { return mSnapshotBuilder.getSnapshot(layerId); }
Vishnu Nair92990e22023-02-24 20:01:05 +0000102 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath path) {
103 return mSnapshotBuilder.getSnapshot(path);
104 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000105
106 LayerHierarchyBuilder mHierarchyBuilder{{}};
107 LayerSnapshotBuilder mSnapshotBuilder;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500108 DisplayInfos mFrontEndDisplayInfos;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000109 renderengine::ShadowSettings globalShadowSettings;
110 static const std::vector<uint32_t> STARTING_ZORDER;
111};
112const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1, 11, 111, 12, 121,
113 122, 1221, 13, 2};
114
115TEST_F(LayerSnapshotTest, buildSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000116 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
117 .layerLifecycleManager = mLifecycleManager,
118 .includeMetadata = false,
119 .displays = mFrontEndDisplayInfos,
120 .globalShadowSettings = globalShadowSettings,
121 .supportedLayerGenericMetadata = {},
122 .genericLayerMetadataKeyMap = {}};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000123 LayerSnapshotBuilder builder(args);
124}
125
126TEST_F(LayerSnapshotTest, updateSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000127 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
128 .layerLifecycleManager = mLifecycleManager,
129 .includeMetadata = false,
130 .displays = mFrontEndDisplayInfos,
131 .globalShadowSettings = globalShadowSettings,
132 .supportedLayerGenericMetadata = {},
133 .genericLayerMetadataKeyMap = {}
134
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000135 };
136
137 LayerSnapshotBuilder builder;
138 builder.update(args);
139}
140
141// update using parent snapshot data
142TEST_F(LayerSnapshotTest, croppedByParent) {
143 /// MAKE ALL LAYERS VISIBLE BY DEFAULT
144 DisplayInfo info;
145 info.info.logicalHeight = 100;
146 info.info.logicalWidth = 200;
147 mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info);
148 Rect layerCrop(0, 0, 10, 20);
149 setCrop(11, layerCrop);
150 EXPECT_TRUE(mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Geometry));
151 UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(mSnapshotBuilder, STARTING_ZORDER);
152 EXPECT_EQ(getSnapshot(11)->geomCrop, layerCrop);
153 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, layerCrop.toFloatRect());
154 float maxHeight = static_cast<float>(info.info.logicalHeight * 10);
155 float maxWidth = static_cast<float>(info.info.logicalWidth * 10);
156
157 FloatRect maxDisplaySize(-maxWidth, -maxHeight, maxWidth, maxHeight);
158 EXPECT_EQ(getSnapshot(1)->geomLayerBounds, maxDisplaySize);
159}
160
161// visibility tests
162TEST_F(LayerSnapshotTest, newLayerHiddenByPolicy) {
163 createLayer(112, 11);
164 hideLayer(112);
165 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
166
167 showLayer(112);
168 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 112, 12, 121, 122, 1221, 13, 2});
169}
170
171TEST_F(LayerSnapshotTest, hiddenByParent) {
172 hideLayer(11);
173 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
174}
175
176TEST_F(LayerSnapshotTest, reparentShowsChild) {
177 hideLayer(11);
178 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
179
180 showLayer(11);
181 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
182}
183
184TEST_F(LayerSnapshotTest, reparentHidesChild) {
185 hideLayer(11);
186 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
187
188 reparentLayer(121, 11);
189 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 122, 1221, 13, 2});
190}
191
192TEST_F(LayerSnapshotTest, unHidingUpdatesSnapshot) {
193 hideLayer(11);
194 Rect crop(1, 2, 3, 4);
195 setCrop(111, crop);
196 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
197
198 showLayer(11);
199 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
200 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, crop.toFloatRect());
201}
202
203TEST_F(LayerSnapshotTest, childBehindParentCanBeHiddenByParent) {
204 setZ(111, -1);
205 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 111, 11, 12, 121, 122, 1221, 13, 2});
206
207 hideLayer(11);
208 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
209}
210
211// relative tests
212TEST_F(LayerSnapshotTest, RelativeParentCanHideChild) {
213 reparentRelativeLayer(13, 11);
214 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
215
216 hideLayer(11);
217 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
218}
219
220TEST_F(LayerSnapshotTest, ReparentingToHiddenRelativeParentHidesChild) {
221 hideLayer(11);
222 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
223 reparentRelativeLayer(13, 11);
224 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
225}
226
227TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) {
228 setAlpha(1, 0.5);
229 setAlpha(122, 0.5);
230 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700231 EXPECT_EQ(getSnapshot(1)->alpha, 0.5f);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000232 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);
Vishnu Naira02943f2023-06-03 13:44:46 -0700240 EXPECT_TRUE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
241 EXPECT_TRUE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000242 setCrop(2, Rect(1, 2, 3, 4));
243 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700244 EXPECT_TRUE(getSnapshot(2)->changes.test(RequestedLayerState::Changes::Geometry));
245 EXPECT_FALSE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
246 EXPECT_FALSE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000247}
248
249TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) {
250 setColor(11, {1._hf, 0._hf, 0._hf});
251 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700252 EXPECT_EQ(getSnapshot(11)->changes, RequestedLayerState::Changes::Content);
253 EXPECT_EQ(getSnapshot(11)->clientChanges, layer_state_t::eColorChanged);
254 EXPECT_EQ(getSnapshot(1)->changes.get(), 0u);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000255 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700256 EXPECT_EQ(getSnapshot(11)->changes.get(), 0u);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000257}
258
259TEST_F(LayerSnapshotTest, FastPathSetsChangeFlagToContent) {
260 setColor(1, {1._hf, 0._hf, 0._hf});
261 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
262 EXPECT_EQ(getSnapshot(1)->changes, RequestedLayerState::Changes::Content);
Vishnu Naira02943f2023-06-03 13:44:46 -0700263 EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eColorChanged);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000264}
265
Vishnu Naircfb2d252023-01-19 04:44:02 +0000266TEST_F(LayerSnapshotTest, GameMode) {
267 std::vector<TransactionState> transactions;
268 transactions.emplace_back();
269 transactions.back().states.push_back({});
270 transactions.back().states.front().state.what = layer_state_t::eMetadataChanged;
271 transactions.back().states.front().state.metadata = LayerMetadata();
272 transactions.back().states.front().state.metadata.setInt32(METADATA_GAME_MODE, 42);
Vishnu Nair1391de22023-03-05 19:56:14 -0800273 transactions.back().states.front().layerId = 1;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000274 transactions.back().states.front().state.layerId = static_cast<int32_t>(1);
275 mLifecycleManager.applyTransactions(transactions);
Vishnu Naira02943f2023-06-03 13:44:46 -0700276 EXPECT_EQ(mLifecycleManager.getGlobalChanges(), RequestedLayerState::Changes::GameMode);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000277 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700278 EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eMetadataChanged);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000279 EXPECT_EQ(static_cast<int32_t>(getSnapshot(1)->gameMode), 42);
280 EXPECT_EQ(static_cast<int32_t>(getSnapshot(11)->gameMode), 42);
281}
282
283TEST_F(LayerSnapshotTest, NoLayerVoteForParentWithChildVotes) {
284 // ROOT
285 // ├── 1
286 // │ ├── 11 (frame rate set)
287 // │ │ └── 111
288 // │ ├── 12
289 // │ │ ├── 121
290 // │ │ └── 122
291 // │ │ └── 1221
292 // │ └── 13
293 // └── 2
294
295 std::vector<TransactionState> transactions;
296 transactions.emplace_back();
297 transactions.back().states.push_back({});
298 transactions.back().states.front().state.what = layer_state_t::eFrameRateChanged;
299 transactions.back().states.front().state.frameRate = 90.0;
300 transactions.back().states.front().state.frameRateCompatibility =
301 ANATIVEWINDOW_FRAME_RATE_EXACT;
302 transactions.back().states.front().state.changeFrameRateStrategy =
303 ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS;
Vishnu Nair1391de22023-03-05 19:56:14 -0800304 transactions.back().states.front().layerId = 11;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000305 mLifecycleManager.applyTransactions(transactions);
306 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
307
308 EXPECT_EQ(getSnapshot(11)->frameRate.rate.getIntValue(), 90);
309 EXPECT_EQ(getSnapshot(11)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::Exact);
310 EXPECT_EQ(getSnapshot(111)->frameRate.rate.getIntValue(), 90);
311 EXPECT_EQ(getSnapshot(111)->frameRate.type,
312 scheduler::LayerInfo::FrameRateCompatibility::Exact);
313 EXPECT_EQ(getSnapshot(1)->frameRate.rate.getIntValue(), 0);
314 EXPECT_EQ(getSnapshot(1)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::NoVote);
315}
316
Vishnu Naira02943f2023-06-03 13:44:46 -0700317TEST_F(LayerSnapshotTest, CanCropTouchableRegion) {
Vishnu Nair29354ec2023-03-28 18:51:28 -0700318 // ROOT
319 // ├── 1
320 // │ ├── 11
321 // │ │ └── 111 (touchregion set to touch but cropped by layer 13)
322 // │ ├── 12
323 // │ │ ├── 121
324 // │ │ └── 122
325 // │ │ └── 1221
326 // │ └── 13 (crop set to touchCrop)
327 // └── 2
328
329 Rect touchCrop{300, 300, 400, 500};
330 setCrop(13, touchCrop);
331 Region touch{Rect{0, 0, 1000, 1000}};
332 setTouchableRegionCrop(111, touch, /*touchCropId=*/13, /*replaceTouchableRegionWithCrop=*/true);
333 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
334 EXPECT_EQ(getSnapshot({.id = 111})->inputInfo.touchableRegion.bounds(), touchCrop);
335
336 Rect modifiedTouchCrop{100, 300, 400, 700};
337 setCrop(13, modifiedTouchCrop);
338 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
339 EXPECT_EQ(getSnapshot({.id = 111})->inputInfo.touchableRegion.bounds(), modifiedTouchCrop);
340}
341
Vishnu Nair444f3952023-04-11 13:01:02 -0700342TEST_F(LayerSnapshotTest, blurUpdatesWhenAlphaChanges) {
343 static constexpr int blurRadius = 42;
344 setBackgroundBlurRadius(1221, blurRadius);
345
346 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
347 EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius, blurRadius);
348
349 static constexpr float alpha = 0.5;
350 setAlpha(12, alpha);
351 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
352 EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius, blurRadius * alpha);
353}
354
Vishnu Naira9c43762023-01-27 19:10:25 +0000355// Display Mirroring Tests
356// tree with 3 levels of children
357// ROOT (DISPLAY 0)
358// ├── 1
359// │ ├── 11
360// │ │ └── 111
361// │ ├── 12 (has skip screenshot flag)
362// │ │ ├── 121
363// │ │ └── 122
364// │ │ └── 1221
365// │ └── 13
366// └── 2
367// ROOT (DISPLAY 1)
368// └── 3 (mirrors display 0)
Vishnu Nair92990e22023-02-24 20:01:05 +0000369TEST_F(LayerSnapshotTest, displayMirrorRespectsLayerSkipScreenshotFlag) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000370 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
371 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
372 setLayerStack(3, 1);
373
Vishnu Nair444f3952023-04-11 13:01:02 -0700374 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3, 1, 11, 111, 13, 2};
Vishnu Naira9c43762023-01-27 19:10:25 +0000375 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
376}
377
Vishnu Nair92990e22023-02-24 20:01:05 +0000378// ROOT (DISPLAY 0)
379// ├── 1
380// │ ├── 11
381// │ │ └── 111
382// │ └── 13
383// └── 2
384// ROOT (DISPLAY 3)
385// └── 3 (mirrors display 0)
386TEST_F(LayerSnapshotTest, mirrorLayerGetsCorrectLayerStack) {
387 reparentLayer(12, UNASSIGNED_LAYER_ID);
388 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
389 setLayerStack(3, 3);
390 createDisplayMirrorLayer(4, ui::LayerStack::fromValue(0));
391 setLayerStack(4, 4);
392
Vishnu Nair444f3952023-04-11 13:01:02 -0700393 std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111,
394 13, 2, 4, 1, 11, 111, 13, 2};
Vishnu Nair92990e22023-02-24 20:01:05 +0000395 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
396 EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootId = 3})->outputFilter.layerStack.id, 3u);
397 EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootId = 4})->outputFilter.layerStack.id, 4u);
398}
399
400// ROOT (DISPLAY 0)
401// ├── 1 (crop 50x50)
402// │ ├── 11
403// │ │ └── 111
404// │ └── 13
405// └── 2
406// ROOT (DISPLAY 3)
407// └── 3 (mirrors display 0) (crop 100x100)
408TEST_F(LayerSnapshotTest, mirrorLayerTouchIsCroppedByMirrorRoot) {
409 reparentLayer(12, UNASSIGNED_LAYER_ID);
410 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
411 setLayerStack(3, 3);
412 setCrop(1, Rect{50, 50});
413 setCrop(3, Rect{100, 100});
414 setCrop(111, Rect{200, 200});
415 Region touch{Rect{0, 0, 1000, 1000}};
416 setTouchableRegion(111, touch);
Vishnu Nair444f3952023-04-11 13:01:02 -0700417 std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111, 13, 2};
Vishnu Nair92990e22023-02-24 20:01:05 +0000418 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
419 EXPECT_TRUE(getSnapshot({.id = 111})->inputInfo.touchableRegion.hasSameRects(touch));
420 Region touchCroppedByMirrorRoot{Rect{0, 0, 50, 50}};
421 EXPECT_TRUE(getSnapshot({.id = 111, .mirrorRootId = 3})
422 ->inputInfo.touchableRegion.hasSameRects(touchCroppedByMirrorRoot));
423}
424
425TEST_F(LayerSnapshotTest, canRemoveDisplayMirror) {
426 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
427 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
428 setLayerStack(3, 1);
Vishnu Nair444f3952023-04-11 13:01:02 -0700429 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3, 1, 11, 111, 13, 2};
Vishnu Nair92990e22023-02-24 20:01:05 +0000430 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
431 destroyLayerHandle(3);
432 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
433}
434
Vishnu Nairfccd6362023-02-24 23:39:53 +0000435TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterMirroring) {
436 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
437 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
438 setLayerStack(3, 1);
Vishnu Nair444f3952023-04-11 13:01:02 -0700439 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3,
440 1, 11, 111, 12, 121, 122, 1221, 13, 2};
Vishnu Nairfccd6362023-02-24 23:39:53 +0000441 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
442 destroyLayerHandle(3);
443 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
444
445 EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
446}
447
448// Rel z doesn't create duplicate snapshots but this is for completeness
449TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterRelZ) {
450 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
451 reparentRelativeLayer(13, 11);
452 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
453 setZ(13, 0);
454 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
455
456 EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
457}
458
459TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterLayerDestruction) {
460 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
461 destroyLayerHandle(2);
462 destroyLayerHandle(122);
463
464 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13};
465 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
466
467 EXPECT_LE(startingNumSnapshots - 2, mSnapshotBuilder.getSnapshots().size());
468}
469
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000470} // namespace android::surfaceflinger::frontend