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