blob: 65bac00461d5b8b2cf34d30626e9692651050c16 [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 <renderengine/mock/FakeExternalTexture.h>
21
Vishnu Nair8fc721b2022-12-22 20:06:32 +000022#include "FrontEnd/LayerHierarchy.h"
23#include "FrontEnd/LayerLifecycleManager.h"
24#include "FrontEnd/LayerSnapshotBuilder.h"
Vishnu Nair3d8565a2023-06-30 07:23:24 +000025#include "Layer.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000026#include "LayerHierarchyTest.h"
27
28#define UPDATE_AND_VERIFY(BUILDER, ...) \
29 ({ \
30 SCOPED_TRACE(""); \
31 updateAndVerify((BUILDER), /*displayChanges=*/false, __VA_ARGS__); \
32 })
33
34#define UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(BUILDER, ...) \
35 ({ \
36 SCOPED_TRACE(""); \
37 updateAndVerify((BUILDER), /*displayChanges=*/true, __VA_ARGS__); \
38 })
39
40namespace android::surfaceflinger::frontend {
41
Vishnu Naircfb2d252023-01-19 04:44:02 +000042using ftl::Flags;
43using namespace ftl::flag_operators;
44
Vishnu Nair8fc721b2022-12-22 20:06:32 +000045// To run test:
46/**
47 mp :libsurfaceflinger_unittest && adb sync; adb shell \
48 /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \
49 --gtest_filter="LayerSnapshotTest.*" --gtest_brief=1
50*/
51
52class LayerSnapshotTest : public LayerHierarchyTestBase {
53protected:
54 LayerSnapshotTest() : LayerHierarchyTestBase() {
55 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
56 }
57
58 void createRootLayer(uint32_t id) override {
59 LayerHierarchyTestBase::createRootLayer(id);
60 setColor(id);
61 }
62
63 void createLayer(uint32_t id, uint32_t parentId) override {
64 LayerHierarchyTestBase::createLayer(id, parentId);
65 setColor(parentId);
66 }
67
68 void mirrorLayer(uint32_t id, uint32_t parent, uint32_t layerToMirror) override {
69 LayerHierarchyTestBase::mirrorLayer(id, parent, layerToMirror);
70 setColor(id);
71 }
72
Vishnu Nair0808ae62023-08-07 21:42:42 -070073 void update(LayerSnapshotBuilder& actualBuilder, LayerSnapshotBuilder::Args& args) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +000074 if (mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)) {
75 mHierarchyBuilder.update(mLifecycleManager.getLayers(),
76 mLifecycleManager.getDestroyedLayers());
77 }
Vishnu Nair0808ae62023-08-07 21:42:42 -070078 args.root = mHierarchyBuilder.getHierarchy();
79 actualBuilder.update(args);
80 }
81
82 void updateAndVerify(LayerSnapshotBuilder& actualBuilder, bool hasDisplayChanges,
83 const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +000084 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
85 .layerLifecycleManager = mLifecycleManager,
86 .includeMetadata = false,
87 .displays = mFrontEndDisplayInfos,
88 .displayChanges = hasDisplayChanges,
89 .globalShadowSettings = globalShadowSettings,
Vishnu Nair444f3952023-04-11 13:01:02 -070090 .supportsBlur = true,
Vishnu Nairc765c6c2023-02-23 00:08:01 +000091 .supportedLayerGenericMetadata = {},
92 .genericLayerMetadataKeyMap = {}};
Vishnu Nair0808ae62023-08-07 21:42:42 -070093 update(actualBuilder, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000094
95 // rebuild layer snapshots from scratch and verify that it matches the updated state.
96 LayerSnapshotBuilder expectedBuilder(args);
97 mLifecycleManager.commitChanges();
98 ASSERT_TRUE(expectedBuilder.getSnapshots().size() > 0);
99 ASSERT_TRUE(actualBuilder.getSnapshots().size() > 0);
100
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000101 std::vector<uint32_t> actualVisibleLayerIdsInZOrder;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000102 actualBuilder.forEachVisibleSnapshot(
103 [&actualVisibleLayerIdsInZOrder](const LayerSnapshot& snapshot) {
104 actualVisibleLayerIdsInZOrder.push_back(snapshot.path.id);
105 });
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000106 EXPECT_EQ(expectedVisibleLayerIdsInZOrder, actualVisibleLayerIdsInZOrder);
107 }
108
109 LayerSnapshot* getSnapshot(uint32_t layerId) { return mSnapshotBuilder.getSnapshot(layerId); }
Vishnu Nair92990e22023-02-24 20:01:05 +0000110 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath path) {
111 return mSnapshotBuilder.getSnapshot(path);
112 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000113
114 LayerHierarchyBuilder mHierarchyBuilder{{}};
115 LayerSnapshotBuilder mSnapshotBuilder;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500116 DisplayInfos mFrontEndDisplayInfos;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000117 renderengine::ShadowSettings globalShadowSettings;
118 static const std::vector<uint32_t> STARTING_ZORDER;
119};
120const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1, 11, 111, 12, 121,
121 122, 1221, 13, 2};
122
123TEST_F(LayerSnapshotTest, buildSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000124 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
125 .layerLifecycleManager = mLifecycleManager,
126 .includeMetadata = false,
127 .displays = mFrontEndDisplayInfos,
128 .globalShadowSettings = globalShadowSettings,
129 .supportedLayerGenericMetadata = {},
130 .genericLayerMetadataKeyMap = {}};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000131 LayerSnapshotBuilder builder(args);
132}
133
134TEST_F(LayerSnapshotTest, updateSnapshot) {
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000135 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
136 .layerLifecycleManager = mLifecycleManager,
137 .includeMetadata = false,
138 .displays = mFrontEndDisplayInfos,
139 .globalShadowSettings = globalShadowSettings,
140 .supportedLayerGenericMetadata = {},
141 .genericLayerMetadataKeyMap = {}
142
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000143 };
144
145 LayerSnapshotBuilder builder;
146 builder.update(args);
147}
148
149// update using parent snapshot data
150TEST_F(LayerSnapshotTest, croppedByParent) {
151 /// MAKE ALL LAYERS VISIBLE BY DEFAULT
152 DisplayInfo info;
153 info.info.logicalHeight = 100;
154 info.info.logicalWidth = 200;
155 mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info);
156 Rect layerCrop(0, 0, 10, 20);
157 setCrop(11, layerCrop);
158 EXPECT_TRUE(mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Geometry));
159 UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(mSnapshotBuilder, STARTING_ZORDER);
160 EXPECT_EQ(getSnapshot(11)->geomCrop, layerCrop);
161 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, layerCrop.toFloatRect());
162 float maxHeight = static_cast<float>(info.info.logicalHeight * 10);
163 float maxWidth = static_cast<float>(info.info.logicalWidth * 10);
164
165 FloatRect maxDisplaySize(-maxWidth, -maxHeight, maxWidth, maxHeight);
166 EXPECT_EQ(getSnapshot(1)->geomLayerBounds, maxDisplaySize);
167}
168
169// visibility tests
170TEST_F(LayerSnapshotTest, newLayerHiddenByPolicy) {
171 createLayer(112, 11);
172 hideLayer(112);
173 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
174
175 showLayer(112);
176 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 112, 12, 121, 122, 1221, 13, 2});
177}
178
179TEST_F(LayerSnapshotTest, hiddenByParent) {
180 hideLayer(11);
181 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
182}
183
184TEST_F(LayerSnapshotTest, reparentShowsChild) {
185 hideLayer(11);
186 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
187
188 showLayer(11);
189 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
190}
191
192TEST_F(LayerSnapshotTest, reparentHidesChild) {
193 hideLayer(11);
194 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
195
196 reparentLayer(121, 11);
197 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 122, 1221, 13, 2});
198}
199
200TEST_F(LayerSnapshotTest, unHidingUpdatesSnapshot) {
201 hideLayer(11);
202 Rect crop(1, 2, 3, 4);
203 setCrop(111, crop);
204 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
205
206 showLayer(11);
207 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
208 EXPECT_EQ(getSnapshot(111)->geomLayerBounds, crop.toFloatRect());
209}
210
211TEST_F(LayerSnapshotTest, childBehindParentCanBeHiddenByParent) {
212 setZ(111, -1);
213 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 111, 11, 12, 121, 122, 1221, 13, 2});
214
215 hideLayer(11);
216 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
217}
218
219// relative tests
220TEST_F(LayerSnapshotTest, RelativeParentCanHideChild) {
221 reparentRelativeLayer(13, 11);
222 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
223
224 hideLayer(11);
225 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
226}
227
228TEST_F(LayerSnapshotTest, ReparentingToHiddenRelativeParentHidesChild) {
229 hideLayer(11);
230 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
231 reparentRelativeLayer(13, 11);
232 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
233}
234
235TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) {
236 setAlpha(1, 0.5);
237 setAlpha(122, 0.5);
238 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700239 EXPECT_EQ(getSnapshot(1)->alpha, 0.5f);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000240 EXPECT_EQ(getSnapshot(12)->alpha, 0.5f);
241 EXPECT_EQ(getSnapshot(1221)->alpha, 0.25f);
242}
243
244// Change states
245TEST_F(LayerSnapshotTest, UpdateClearsPreviousChangeStates) {
246 setCrop(1, Rect(1, 2, 3, 4));
247 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700248 EXPECT_TRUE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
249 EXPECT_TRUE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000250 setCrop(2, Rect(1, 2, 3, 4));
251 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700252 EXPECT_TRUE(getSnapshot(2)->changes.test(RequestedLayerState::Changes::Geometry));
253 EXPECT_FALSE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
254 EXPECT_FALSE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000255}
256
257TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) {
258 setColor(11, {1._hf, 0._hf, 0._hf});
259 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700260 EXPECT_EQ(getSnapshot(11)->changes, RequestedLayerState::Changes::Content);
261 EXPECT_EQ(getSnapshot(11)->clientChanges, layer_state_t::eColorChanged);
262 EXPECT_EQ(getSnapshot(1)->changes.get(), 0u);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000263 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700264 EXPECT_EQ(getSnapshot(11)->changes.get(), 0u);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000265}
266
267TEST_F(LayerSnapshotTest, FastPathSetsChangeFlagToContent) {
268 setColor(1, {1._hf, 0._hf, 0._hf});
269 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
270 EXPECT_EQ(getSnapshot(1)->changes, RequestedLayerState::Changes::Content);
Vishnu Naira02943f2023-06-03 13:44:46 -0700271 EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eColorChanged);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000272}
273
Vishnu Naircfb2d252023-01-19 04:44:02 +0000274TEST_F(LayerSnapshotTest, GameMode) {
275 std::vector<TransactionState> transactions;
276 transactions.emplace_back();
277 transactions.back().states.push_back({});
278 transactions.back().states.front().state.what = layer_state_t::eMetadataChanged;
279 transactions.back().states.front().state.metadata = LayerMetadata();
280 transactions.back().states.front().state.metadata.setInt32(METADATA_GAME_MODE, 42);
Vishnu Nair1391de22023-03-05 19:56:14 -0800281 transactions.back().states.front().layerId = 1;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000282 transactions.back().states.front().state.layerId = static_cast<int32_t>(1);
283 mLifecycleManager.applyTransactions(transactions);
Vishnu Naira02943f2023-06-03 13:44:46 -0700284 EXPECT_EQ(mLifecycleManager.getGlobalChanges(), RequestedLayerState::Changes::GameMode);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000285 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
Vishnu Naira02943f2023-06-03 13:44:46 -0700286 EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eMetadataChanged);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000287 EXPECT_EQ(static_cast<int32_t>(getSnapshot(1)->gameMode), 42);
288 EXPECT_EQ(static_cast<int32_t>(getSnapshot(11)->gameMode), 42);
289}
290
291TEST_F(LayerSnapshotTest, NoLayerVoteForParentWithChildVotes) {
292 // ROOT
293 // ├── 1
294 // │ ├── 11 (frame rate set)
295 // │ │ └── 111
296 // │ ├── 12
297 // │ │ ├── 121
298 // │ │ └── 122
299 // │ │ └── 1221
300 // │ └── 13
301 // └── 2
302
303 std::vector<TransactionState> transactions;
304 transactions.emplace_back();
305 transactions.back().states.push_back({});
306 transactions.back().states.front().state.what = layer_state_t::eFrameRateChanged;
307 transactions.back().states.front().state.frameRate = 90.0;
308 transactions.back().states.front().state.frameRateCompatibility =
309 ANATIVEWINDOW_FRAME_RATE_EXACT;
310 transactions.back().states.front().state.changeFrameRateStrategy =
311 ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS;
Vishnu Nair1391de22023-03-05 19:56:14 -0800312 transactions.back().states.front().layerId = 11;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000313 mLifecycleManager.applyTransactions(transactions);
314 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
315
316 EXPECT_EQ(getSnapshot(11)->frameRate.rate.getIntValue(), 90);
317 EXPECT_EQ(getSnapshot(11)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::Exact);
318 EXPECT_EQ(getSnapshot(111)->frameRate.rate.getIntValue(), 90);
319 EXPECT_EQ(getSnapshot(111)->frameRate.type,
320 scheduler::LayerInfo::FrameRateCompatibility::Exact);
321 EXPECT_EQ(getSnapshot(1)->frameRate.rate.getIntValue(), 0);
322 EXPECT_EQ(getSnapshot(1)->frameRate.type, scheduler::LayerInfo::FrameRateCompatibility::NoVote);
323}
324
Vishnu Naira02943f2023-06-03 13:44:46 -0700325TEST_F(LayerSnapshotTest, CanCropTouchableRegion) {
Vishnu Nair29354ec2023-03-28 18:51:28 -0700326 // ROOT
327 // ├── 1
328 // │ ├── 11
329 // │ │ └── 111 (touchregion set to touch but cropped by layer 13)
330 // │ ├── 12
331 // │ │ ├── 121
332 // │ │ └── 122
333 // │ │ └── 1221
334 // │ └── 13 (crop set to touchCrop)
335 // └── 2
336
337 Rect touchCrop{300, 300, 400, 500};
338 setCrop(13, touchCrop);
339 Region touch{Rect{0, 0, 1000, 1000}};
340 setTouchableRegionCrop(111, touch, /*touchCropId=*/13, /*replaceTouchableRegionWithCrop=*/true);
341 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
342 EXPECT_EQ(getSnapshot({.id = 111})->inputInfo.touchableRegion.bounds(), touchCrop);
343
344 Rect modifiedTouchCrop{100, 300, 400, 700};
345 setCrop(13, modifiedTouchCrop);
346 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
347 EXPECT_EQ(getSnapshot({.id = 111})->inputInfo.touchableRegion.bounds(), modifiedTouchCrop);
348}
349
Vishnu Nair444f3952023-04-11 13:01:02 -0700350TEST_F(LayerSnapshotTest, blurUpdatesWhenAlphaChanges) {
351 static constexpr int blurRadius = 42;
352 setBackgroundBlurRadius(1221, blurRadius);
353
354 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
355 EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius, blurRadius);
356
357 static constexpr float alpha = 0.5;
358 setAlpha(12, alpha);
359 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
360 EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius, blurRadius * alpha);
361}
362
Vishnu Naira9c43762023-01-27 19:10:25 +0000363// Display Mirroring Tests
364// tree with 3 levels of children
365// ROOT (DISPLAY 0)
366// ├── 1
367// │ ├── 11
368// │ │ └── 111
369// │ ├── 12 (has skip screenshot flag)
370// │ │ ├── 121
371// │ │ └── 122
372// │ │ └── 1221
373// │ └── 13
374// └── 2
375// ROOT (DISPLAY 1)
376// └── 3 (mirrors display 0)
Vishnu Nair92990e22023-02-24 20:01:05 +0000377TEST_F(LayerSnapshotTest, displayMirrorRespectsLayerSkipScreenshotFlag) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000378 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
379 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
380 setLayerStack(3, 1);
381
Vishnu Nair444f3952023-04-11 13:01:02 -0700382 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 +0000383 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
384}
385
Vishnu Nair92990e22023-02-24 20:01:05 +0000386// ROOT (DISPLAY 0)
387// ├── 1
388// │ ├── 11
389// │ │ └── 111
390// │ └── 13
391// └── 2
392// ROOT (DISPLAY 3)
393// └── 3 (mirrors display 0)
394TEST_F(LayerSnapshotTest, mirrorLayerGetsCorrectLayerStack) {
395 reparentLayer(12, UNASSIGNED_LAYER_ID);
396 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
397 setLayerStack(3, 3);
398 createDisplayMirrorLayer(4, ui::LayerStack::fromValue(0));
399 setLayerStack(4, 4);
400
Vishnu Nair444f3952023-04-11 13:01:02 -0700401 std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111,
402 13, 2, 4, 1, 11, 111, 13, 2};
Vishnu Nair92990e22023-02-24 20:01:05 +0000403 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
404 EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootId = 3})->outputFilter.layerStack.id, 3u);
405 EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootId = 4})->outputFilter.layerStack.id, 4u);
406}
407
408// ROOT (DISPLAY 0)
409// ├── 1 (crop 50x50)
410// │ ├── 11
411// │ │ └── 111
412// │ └── 13
413// └── 2
414// ROOT (DISPLAY 3)
415// └── 3 (mirrors display 0) (crop 100x100)
416TEST_F(LayerSnapshotTest, mirrorLayerTouchIsCroppedByMirrorRoot) {
417 reparentLayer(12, UNASSIGNED_LAYER_ID);
418 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
419 setLayerStack(3, 3);
420 setCrop(1, Rect{50, 50});
421 setCrop(3, Rect{100, 100});
422 setCrop(111, Rect{200, 200});
423 Region touch{Rect{0, 0, 1000, 1000}};
424 setTouchableRegion(111, touch);
Vishnu Nair444f3952023-04-11 13:01:02 -0700425 std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111, 13, 2};
Vishnu Nair92990e22023-02-24 20:01:05 +0000426 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
427 EXPECT_TRUE(getSnapshot({.id = 111})->inputInfo.touchableRegion.hasSameRects(touch));
428 Region touchCroppedByMirrorRoot{Rect{0, 0, 50, 50}};
429 EXPECT_TRUE(getSnapshot({.id = 111, .mirrorRootId = 3})
430 ->inputInfo.touchableRegion.hasSameRects(touchCroppedByMirrorRoot));
431}
432
433TEST_F(LayerSnapshotTest, canRemoveDisplayMirror) {
434 setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
435 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
436 setLayerStack(3, 1);
Vishnu Nair444f3952023-04-11 13:01:02 -0700437 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 +0000438 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
439 destroyLayerHandle(3);
440 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
441}
442
Vishnu Nairfccd6362023-02-24 23:39:53 +0000443TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterMirroring) {
444 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
445 createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
446 setLayerStack(3, 1);
Vishnu Nair444f3952023-04-11 13:01:02 -0700447 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3,
448 1, 11, 111, 12, 121, 122, 1221, 13, 2};
Vishnu Nairfccd6362023-02-24 23:39:53 +0000449 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
450 destroyLayerHandle(3);
451 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
452
453 EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
454}
455
456// Rel z doesn't create duplicate snapshots but this is for completeness
457TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterRelZ) {
458 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
459 reparentRelativeLayer(13, 11);
460 UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
461 setZ(13, 0);
462 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
463
464 EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
465}
466
467TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterLayerDestruction) {
468 size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
469 destroyLayerHandle(2);
470 destroyLayerHandle(122);
471
472 std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13};
473 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
474
475 EXPECT_LE(startingNumSnapshots - 2, mSnapshotBuilder.getSnapshots().size());
476}
477
Vishnu Nair3d8565a2023-06-30 07:23:24 +0000478TEST_F(LayerSnapshotTest, snashotContainsMetadataFromLayerCreationArgs) {
479 LayerCreationArgs args(std::make_optional<uint32_t>(200));
480 args.name = "testlayer";
481 args.addToRoot = true;
482 args.metadata.setInt32(42, 24);
483
484 std::vector<std::unique_ptr<RequestedLayerState>> layers;
485 layers.emplace_back(std::make_unique<RequestedLayerState>(args));
486 EXPECT_TRUE(layers.back()->metadata.has(42));
487 EXPECT_EQ(layers.back()->metadata.getInt32(42, 0), 24);
488 mLifecycleManager.addLayers(std::move(layers));
489
490 std::vector<uint32_t> expected = STARTING_ZORDER;
491 expected.push_back(200);
492 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
493
494 EXPECT_TRUE(mSnapshotBuilder.getSnapshot(200)->layerMetadata.has(42));
495 EXPECT_EQ(mSnapshotBuilder.getSnapshot(200)->layerMetadata.getInt32(42, 0), 24);
496}
497
498TEST_F(LayerSnapshotTest, frameRateSelectionPriorityPassedToChildLayers) {
499 setFrameRateSelectionPriority(11, 1);
500
501 setFrameRateSelectionPriority(12, 2);
502
503 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
504 EXPECT_EQ(getSnapshot({.id = 1})->frameRateSelectionPriority, Layer::PRIORITY_UNSET);
505 EXPECT_EQ(getSnapshot({.id = 11})->frameRateSelectionPriority, 1);
506 EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionPriority, 2);
507 EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionPriority, 2);
508 EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionPriority, 2);
509
510 // reparent and verify the child gets the new parent's framerate selection priority
511 reparentLayer(122, 11);
512
513 std::vector<uint32_t> expected = {1, 11, 111, 122, 1221, 12, 121, 13, 2};
514 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
515 EXPECT_EQ(getSnapshot({.id = 1})->frameRateSelectionPriority, Layer::PRIORITY_UNSET);
516 EXPECT_EQ(getSnapshot({.id = 11})->frameRateSelectionPriority, 1);
517 EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionPriority, 2);
518 EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionPriority, 1);
519 EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionPriority, 1);
520}
521
Vishnu Nair52d56fd2023-07-20 17:02:43 +0000522TEST_F(LayerSnapshotTest, framerate) {
523 setFrameRate(11, 244.f, 0, 0);
524
525 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
526 // verify parent is gets no vote
527 EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.rate.isValid());
528 EXPECT_EQ(getSnapshot({.id = 1})->frameRate.type,
529 scheduler::LayerInfo::FrameRateCompatibility::NoVote);
530 EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
531
532 // verify layer and children get the requested votes
533 EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.rate.isValid());
534 EXPECT_EQ(getSnapshot({.id = 11})->frameRate.rate.getValue(), 244.f);
535 EXPECT_EQ(getSnapshot({.id = 11})->frameRate.type,
536 scheduler::LayerInfo::FrameRateCompatibility::Default);
537 EXPECT_TRUE(getSnapshot({.id = 11})->changes.test(RequestedLayerState::Changes::FrameRate));
538
539 EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.rate.isValid());
540 EXPECT_EQ(getSnapshot({.id = 111})->frameRate.rate.getValue(), 244.f);
541 EXPECT_EQ(getSnapshot({.id = 111})->frameRate.type,
542 scheduler::LayerInfo::FrameRateCompatibility::Default);
543 EXPECT_TRUE(getSnapshot({.id = 111})->changes.test(RequestedLayerState::Changes::FrameRate));
544
545 // reparent and verify the child gets the new parent's framerate
546 reparentLayer(122, 11);
547
548 std::vector<uint32_t> expected = {1, 11, 111, 122, 1221, 12, 121, 13, 2};
549 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
550 // verify parent is gets no vote
551 EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.rate.isValid());
552 EXPECT_EQ(getSnapshot({.id = 1})->frameRate.type,
553 scheduler::LayerInfo::FrameRateCompatibility::NoVote);
554
555 // verify layer and children get the requested votes
556 EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.rate.isValid());
557 EXPECT_EQ(getSnapshot({.id = 11})->frameRate.rate.getValue(), 244.f);
558 EXPECT_EQ(getSnapshot({.id = 11})->frameRate.type,
559 scheduler::LayerInfo::FrameRateCompatibility::Default);
560
561 EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.rate.isValid());
562 EXPECT_EQ(getSnapshot({.id = 111})->frameRate.rate.getValue(), 244.f);
563 EXPECT_EQ(getSnapshot({.id = 111})->frameRate.type,
564 scheduler::LayerInfo::FrameRateCompatibility::Default);
565
566 EXPECT_TRUE(getSnapshot({.id = 122})->frameRate.rate.isValid());
567 EXPECT_EQ(getSnapshot({.id = 122})->frameRate.rate.getValue(), 244.f);
568 EXPECT_EQ(getSnapshot({.id = 122})->frameRate.type,
569 scheduler::LayerInfo::FrameRateCompatibility::Default);
570 EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
571
572 // reparent and verify the new parent gets no vote
573 reparentLayer(11, 2);
574 expected = {1, 12, 121, 13, 2, 11, 111, 122, 1221};
575 UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
576
577 // verify old parent has invalid framerate (default)
578 EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.rate.isValid());
579 EXPECT_EQ(getSnapshot({.id = 1})->frameRate.type,
580 scheduler::LayerInfo::FrameRateCompatibility::Default);
581 EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
582
583 // verify new parent get no vote
584 EXPECT_FALSE(getSnapshot({.id = 2})->frameRate.rate.isValid());
585 EXPECT_EQ(getSnapshot({.id = 2})->frameRate.type,
586 scheduler::LayerInfo::FrameRateCompatibility::NoVote);
587 EXPECT_TRUE(getSnapshot({.id = 2})->changes.test(RequestedLayerState::Changes::FrameRate));
588
589 // verify layer and children get the requested votes (unchanged)
590 EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.rate.isValid());
591 EXPECT_EQ(getSnapshot({.id = 11})->frameRate.rate.getValue(), 244.f);
592 EXPECT_EQ(getSnapshot({.id = 11})->frameRate.type,
593 scheduler::LayerInfo::FrameRateCompatibility::Default);
594
595 EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.rate.isValid());
596 EXPECT_EQ(getSnapshot({.id = 111})->frameRate.rate.getValue(), 244.f);
597 EXPECT_EQ(getSnapshot({.id = 111})->frameRate.type,
598 scheduler::LayerInfo::FrameRateCompatibility::Default);
599
600 EXPECT_TRUE(getSnapshot({.id = 122})->frameRate.rate.isValid());
601 EXPECT_EQ(getSnapshot({.id = 122})->frameRate.rate.getValue(), 244.f);
602 EXPECT_EQ(getSnapshot({.id = 122})->frameRate.type,
603 scheduler::LayerInfo::FrameRateCompatibility::Default);
604}
605
Vishnu Nair0808ae62023-08-07 21:42:42 -0700606TEST_F(LayerSnapshotTest, skipRoundCornersWhenProtected) {
607 setRoundedCorners(1, 42.f);
608 setRoundedCorners(2, 42.f);
609 setCrop(1, Rect{1000, 1000});
610 setCrop(2, Rect{1000, 1000});
611
612 UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
613 EXPECT_TRUE(getSnapshot({.id = 1})->roundedCorner.hasRoundedCorners());
614 EXPECT_EQ(getSnapshot({.id = 1})->roundedCorner.radius.x, 42.f);
615 EXPECT_TRUE(getSnapshot({.id = 2})->roundedCorner.hasRoundedCorners());
616
617 // add a buffer with the protected bit, check rounded corners are not set when
618 // skipRoundCornersWhenProtected == true
619 setBuffer(1,
620 std::make_shared<
621 renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
622 1ULL /* bufferId */,
623 HAL_PIXEL_FORMAT_RGBA_8888,
624 GRALLOC_USAGE_PROTECTED /*usage*/));
625
626 LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
627 .layerLifecycleManager = mLifecycleManager,
628 .includeMetadata = false,
629 .displays = mFrontEndDisplayInfos,
630 .displayChanges = false,
631 .globalShadowSettings = globalShadowSettings,
632 .supportsBlur = true,
633 .supportedLayerGenericMetadata = {},
634 .genericLayerMetadataKeyMap = {},
635 .skipRoundCornersWhenProtected = true};
636 update(mSnapshotBuilder, args);
637 EXPECT_FALSE(getSnapshot({.id = 1})->roundedCorner.hasRoundedCorners());
638 // layer 2 doesn't have a buffer and should be unaffected
639 EXPECT_TRUE(getSnapshot({.id = 2})->roundedCorner.hasRoundedCorners());
640
641 // remove protected bit, check rounded corners are set
642 setBuffer(1,
643 std::make_shared<renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
644 2ULL /* bufferId */,
645 HAL_PIXEL_FORMAT_RGBA_8888,
646 0 /*usage*/));
647 update(mSnapshotBuilder, args);
648 EXPECT_TRUE(getSnapshot({.id = 1})->roundedCorner.hasRoundedCorners());
649 EXPECT_EQ(getSnapshot({.id = 1})->roundedCorner.radius.x, 42.f);
650}
651
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000652} // namespace android::surfaceflinger::frontend