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 "FrontEnd/LayerHierarchy.h"
21 #include "FrontEnd/LayerLifecycleManager.h"
22 #include "FrontEnd/LayerSnapshotBuilder.h"
23 #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
37 namespace android::surfaceflinger::frontend {
38
39 using ftl::Flags;
40 using namespace ftl::flag_operators;
41
42 // 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
49 class LayerSnapshotTest : public LayerHierarchyTestBase {
50 protected:
LayerSnapshotTest()51 LayerSnapshotTest() : LayerHierarchyTestBase() {
52 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
53 }
54
createRootLayer(uint32_t id)55 void createRootLayer(uint32_t id) override {
56 LayerHierarchyTestBase::createRootLayer(id);
57 setColor(id);
58 }
59
createLayer(uint32_t id,uint32_t parentId)60 void createLayer(uint32_t id, uint32_t parentId) override {
61 LayerHierarchyTestBase::createLayer(id, parentId);
62 setColor(parentId);
63 }
64
mirrorLayer(uint32_t id,uint32_t parent,uint32_t layerToMirror)65 void mirrorLayer(uint32_t id, uint32_t parent, uint32_t layerToMirror) override {
66 LayerHierarchyTestBase::mirrorLayer(id, parent, layerToMirror);
67 setColor(id);
68 }
69
updateAndVerify(LayerSnapshotBuilder & actualBuilder,bool hasDisplayChanges,const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder)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 }
76 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
77 .layerLifecycleManager = mLifecycleManager,
78 .includeMetadata = false,
79 .displays = mFrontEndDisplayInfos,
80 .displayChanges = hasDisplayChanges,
81 .globalShadowSettings = globalShadowSettings,
82 .supportsBlur = true,
83 .supportedLayerGenericMetadata = {},
84 .genericLayerMetadataKeyMap = {}};
85 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
93 std::vector<uint32_t> actualVisibleLayerIdsInZOrder;
94 actualBuilder.forEachVisibleSnapshot(
95 [&actualVisibleLayerIdsInZOrder](const LayerSnapshot& snapshot) {
96 actualVisibleLayerIdsInZOrder.push_back(snapshot.path.id);
97 });
98 EXPECT_EQ(expectedVisibleLayerIdsInZOrder, actualVisibleLayerIdsInZOrder);
99 }
100
getSnapshot(uint32_t layerId)101 LayerSnapshot* getSnapshot(uint32_t layerId) { return mSnapshotBuilder.getSnapshot(layerId); }
getSnapshot(const LayerHierarchy::TraversalPath path)102 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath path) {
103 return mSnapshotBuilder.getSnapshot(path);
104 }
105
106 LayerHierarchyBuilder mHierarchyBuilder{{}};
107 LayerSnapshotBuilder mSnapshotBuilder;
108 DisplayInfos mFrontEndDisplayInfos;
109 renderengine::ShadowSettings globalShadowSettings;
110 static const std::vector<uint32_t> STARTING_ZORDER;
111 };
112 const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1, 11, 111, 12, 121,
113 122, 1221, 13, 2};
114
TEST_F(LayerSnapshotTest,buildSnapshot)115 TEST_F(LayerSnapshotTest, buildSnapshot) {
116 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
117 .layerLifecycleManager = mLifecycleManager,
118 .includeMetadata = false,
119 .displays = mFrontEndDisplayInfos,
120 .globalShadowSettings = globalShadowSettings,
121 .supportedLayerGenericMetadata = {},
122 .genericLayerMetadataKeyMap = {}};
123 LayerSnapshotBuilder builder(args);
124 }
125
TEST_F(LayerSnapshotTest,updateSnapshot)126 TEST_F(LayerSnapshotTest, updateSnapshot) {
127 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
128 .layerLifecycleManager = mLifecycleManager,
129 .includeMetadata = false,
130 .displays = mFrontEndDisplayInfos,
131 .globalShadowSettings = globalShadowSettings,
132 .supportedLayerGenericMetadata = {},
133 .genericLayerMetadataKeyMap = {}
134
135 };
136
137 LayerSnapshotBuilder builder;
138 builder.update(args);
139 }
140
141 // update using parent snapshot data
TEST_F(LayerSnapshotTest,croppedByParent)142 TEST_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
TEST_F(LayerSnapshotTest,newLayerHiddenByPolicy)162 TEST_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
TEST_F(LayerSnapshotTest,hiddenByParent)171 TEST_F(LayerSnapshotTest, hiddenByParent) {
172 hideLayer(11);
173 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
174 }
175
TEST_F(LayerSnapshotTest,reparentShowsChild)176 TEST_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
TEST_F(LayerSnapshotTest,reparentHidesChild)184 TEST_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
TEST_F(LayerSnapshotTest,unHidingUpdatesSnapshot)192 TEST_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
TEST_F(LayerSnapshotTest,childBehindParentCanBeHiddenByParent)203 TEST_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
TEST_F(LayerSnapshotTest,RelativeParentCanHideChild)212 TEST_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
TEST_F(LayerSnapshotTest,ReparentingToHiddenRelativeParentHidesChild)220 TEST_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
TEST_F(LayerSnapshotTest,AlphaInheritedByChildren)227 TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) {
228 setAlpha(1, 0.5);
229 setAlpha(122, 0.5);
230 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
231 EXPECT_EQ(getSnapshot(1)->alpha, 0.5f);
232 EXPECT_EQ(getSnapshot(12)->alpha, 0.5f);
233 EXPECT_EQ(getSnapshot(1221)->alpha, 0.25f);
234 }
235
236 // Change states
TEST_F(LayerSnapshotTest,UpdateClearsPreviousChangeStates)237 TEST_F(LayerSnapshotTest, UpdateClearsPreviousChangeStates) {
238 setCrop(1, Rect(1, 2, 3, 4));
239 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
240 EXPECT_TRUE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
241 EXPECT_TRUE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
242 setCrop(2, Rect(1, 2, 3, 4));
243 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
244 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));
247 }
248
TEST_F(LayerSnapshotTest,FastPathClearsPreviousChangeStates)249 TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) {
250 setColor(11, {1._hf, 0._hf, 0._hf});
251 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
252 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);
255 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
256 EXPECT_EQ(getSnapshot(11)->changes.get(), 0u);
257 }
258
TEST_F(LayerSnapshotTest,FastPathSetsChangeFlagToContent)259 TEST_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);
263 EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eColorChanged);
264 }
265
TEST_F(LayerSnapshotTest,GameMode)266 TEST_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);
273 transactions.back().states.front().layerId = 1;
274 transactions.back().states.front().state.layerId = static_cast<int32_t>(1);
275 mLifecycleManager.applyTransactions(transactions);
276 EXPECT_EQ(mLifecycleManager.getGlobalChanges(), RequestedLayerState::Changes::GameMode);
277 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
278 EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eMetadataChanged);
279 EXPECT_EQ(static_cast<int32_t>(getSnapshot(1)->gameMode), 42);
280 EXPECT_EQ(static_cast<int32_t>(getSnapshot(11)->gameMode), 42);
281 }
282
TEST_F(LayerSnapshotTest,NoLayerVoteForParentWithChildVotes)283 TEST_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;
304 transactions.back().states.front().layerId = 11;
305 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
TEST_F(LayerSnapshotTest,CanCropTouchableRegion)317 TEST_F(LayerSnapshotTest, CanCropTouchableRegion) {
318 // 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
TEST_F(LayerSnapshotTest,blurUpdatesWhenAlphaChanges)342 TEST_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
355 // 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)
TEST_F(LayerSnapshotTest,displayMirrorRespectsLayerSkipScreenshotFlag)369 TEST_F(LayerSnapshotTest, displayMirrorRespectsLayerSkipScreenshotFlag) {
370 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
371 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
372 setLayerStack(3, 1);
373
374 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3, 1, 11, 111, 13, 2};
375 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
376 }
377
378 // ROOT (DISPLAY 0)
379 // ├── 1
380 // │ ├── 11
381 // │ │ └── 111
382 // │ └── 13
383 // └── 2
384 // ROOT (DISPLAY 3)
385 // └── 3 (mirrors display 0)
TEST_F(LayerSnapshotTest,mirrorLayerGetsCorrectLayerStack)386 TEST_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
393 std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111,
394 13, 2, 4, 1, 11, 111, 13, 2};
395 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)
TEST_F(LayerSnapshotTest,mirrorLayerTouchIsCroppedByMirrorRoot)408 TEST_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);
417 std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111, 13, 2};
418 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
TEST_F(LayerSnapshotTest,canRemoveDisplayMirror)425 TEST_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);
429 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3, 1, 11, 111, 13, 2};
430 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
431 destroyLayerHandle(3);
432 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
433 }
434
TEST_F(LayerSnapshotTest,cleanUpUnreachableSnapshotsAfterMirroring)435 TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterMirroring) {
436 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
437 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
438 setLayerStack(3, 1);
439 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};
441 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
TEST_F(LayerSnapshotTest,cleanUpUnreachableSnapshotsAfterRelZ)449 TEST_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
TEST_F(LayerSnapshotTest,cleanUpUnreachableSnapshotsAfterLayerDestruction)459 TEST_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
470 } // namespace android::surfaceflinger::frontend
471