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 <common/test/FlagUtils.h>
21 #include <renderengine/mock/FakeExternalTexture.h>
22 
23 #include "FrontEnd/LayerHierarchy.h"
24 #include "FrontEnd/LayerLifecycleManager.h"
25 #include "FrontEnd/LayerSnapshotBuilder.h"
26 #include "Layer.h"
27 #include "LayerHierarchyTest.h"
28 #include "ui/GraphicTypes.h"
29 
30 #include <com_android_graphics_surfaceflinger_flags.h>
31 
32 #define UPDATE_AND_VERIFY(BUILDER, ...)                                    \
33     ({                                                                     \
34         SCOPED_TRACE("");                                                  \
35         updateAndVerify((BUILDER), /*displayChanges=*/false, __VA_ARGS__); \
36     })
37 
38 #define UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(BUILDER, ...)              \
39     ({                                                                    \
40         SCOPED_TRACE("");                                                 \
41         updateAndVerify((BUILDER), /*displayChanges=*/true, __VA_ARGS__); \
42     })
43 
44 namespace android::surfaceflinger::frontend {
45 
46 using ftl::Flags;
47 using namespace ftl::flag_operators;
48 using namespace com::android::graphics::surfaceflinger;
49 
50 // To run test:
51 /**
52  mp :libsurfaceflinger_unittest && adb sync; adb shell \
53     /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \
54     --gtest_filter="LayerSnapshotTest.*" --gtest_brief=1
55 */
56 
57 class LayerSnapshotTest : public LayerSnapshotTestBase {
58 protected:
LayerSnapshotTest()59     LayerSnapshotTest() : LayerSnapshotTestBase() {
60         UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
61     }
62 
update(LayerSnapshotBuilder & actualBuilder,LayerSnapshotBuilder::Args & args)63     void update(LayerSnapshotBuilder& actualBuilder, LayerSnapshotBuilder::Args& args) {
64         if (mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)) {
65             mHierarchyBuilder.update(mLifecycleManager);
66         }
67         args.root = mHierarchyBuilder.getHierarchy();
68         actualBuilder.update(args);
69     }
70 
update(LayerSnapshotBuilder & actualBuilder)71     void update(LayerSnapshotBuilder& actualBuilder) {
72         LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
73                                         .layerLifecycleManager = mLifecycleManager,
74                                         .includeMetadata = false,
75                                         .displays = mFrontEndDisplayInfos,
76                                         .globalShadowSettings = globalShadowSettings,
77                                         .supportsBlur = true,
78                                         .supportedLayerGenericMetadata = {},
79                                         .genericLayerMetadataKeyMap = {}};
80         update(actualBuilder, args);
81     }
82 
updateAndVerify(LayerSnapshotBuilder & actualBuilder,bool hasDisplayChanges,const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder)83     void updateAndVerify(LayerSnapshotBuilder& actualBuilder, bool hasDisplayChanges,
84                          const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder) {
85         LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
86                                         .layerLifecycleManager = mLifecycleManager,
87                                         .includeMetadata = false,
88                                         .displays = mFrontEndDisplayInfos,
89                                         .displayChanges = hasDisplayChanges,
90                                         .globalShadowSettings = globalShadowSettings,
91                                         .supportsBlur = true,
92                                         .supportedLayerGenericMetadata = {},
93                                         .genericLayerMetadataKeyMap = {}};
94         update(actualBuilder, args);
95 
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 
102         std::vector<uint32_t> actualVisibleLayerIdsInZOrder;
103         actualBuilder.forEachVisibleSnapshot(
104                 [&actualVisibleLayerIdsInZOrder](const LayerSnapshot& snapshot) {
105                     actualVisibleLayerIdsInZOrder.push_back(snapshot.path.id);
106                 });
107         EXPECT_EQ(expectedVisibleLayerIdsInZOrder, actualVisibleLayerIdsInZOrder);
108     }
109 
getSnapshot(uint32_t layerId)110     LayerSnapshot* getSnapshot(uint32_t layerId) { return mSnapshotBuilder.getSnapshot(layerId); }
getSnapshot(const LayerHierarchy::TraversalPath path)111     LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath path) {
112         return mSnapshotBuilder.getSnapshot(path);
113     }
114     LayerSnapshotBuilder mSnapshotBuilder;
115     static const std::vector<uint32_t> STARTING_ZORDER;
116 };
117 const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1,   11,   111, 12, 121,
118                                                                   122, 1221, 13,  2};
119 
TEST_F(LayerSnapshotTest,buildSnapshot)120 TEST_F(LayerSnapshotTest, buildSnapshot) {
121     LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
122                                     .layerLifecycleManager = mLifecycleManager,
123                                     .includeMetadata = false,
124                                     .displays = mFrontEndDisplayInfos,
125                                     .globalShadowSettings = globalShadowSettings,
126                                     .supportedLayerGenericMetadata = {},
127                                     .genericLayerMetadataKeyMap = {}};
128     LayerSnapshotBuilder builder(args);
129 }
130 
TEST_F(LayerSnapshotTest,updateSnapshot)131 TEST_F(LayerSnapshotTest, updateSnapshot) {
132     LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
133                                     .layerLifecycleManager = mLifecycleManager,
134                                     .includeMetadata = false,
135                                     .displays = mFrontEndDisplayInfos,
136                                     .globalShadowSettings = globalShadowSettings,
137                                     .supportedLayerGenericMetadata = {},
138                                     .genericLayerMetadataKeyMap = {}
139 
140     };
141 
142     LayerSnapshotBuilder builder;
143     builder.update(args);
144 }
145 
146 // update using parent snapshot data
TEST_F(LayerSnapshotTest,croppedByParent)147 TEST_F(LayerSnapshotTest, croppedByParent) {
148     /// MAKE ALL LAYERS VISIBLE BY DEFAULT
149     DisplayInfo info;
150     info.info.logicalHeight = 100;
151     info.info.logicalWidth = 200;
152     mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info);
153     Rect layerCrop(0, 0, 10, 20);
154     setCrop(11, layerCrop);
155     EXPECT_TRUE(mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Geometry));
156     UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(mSnapshotBuilder, STARTING_ZORDER);
157     EXPECT_EQ(getSnapshot(11)->geomCrop, layerCrop);
158     EXPECT_EQ(getSnapshot(111)->geomLayerBounds, layerCrop.toFloatRect());
159     float maxHeight = static_cast<float>(info.info.logicalHeight * 10);
160     float maxWidth = static_cast<float>(info.info.logicalWidth * 10);
161 
162     FloatRect maxDisplaySize(-maxWidth, -maxHeight, maxWidth, maxHeight);
163     EXPECT_EQ(getSnapshot(1)->geomLayerBounds, maxDisplaySize);
164 }
165 
166 // visibility tests
TEST_F(LayerSnapshotTest,newLayerHiddenByPolicy)167 TEST_F(LayerSnapshotTest, newLayerHiddenByPolicy) {
168     createLayer(112, 11);
169     hideLayer(112);
170     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
171 
172     showLayer(112);
173     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 112, 12, 121, 122, 1221, 13, 2});
174 }
175 
TEST_F(LayerSnapshotTest,hiddenByParent)176 TEST_F(LayerSnapshotTest, hiddenByParent) {
177     hideLayer(11);
178     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
179 }
180 
TEST_F(LayerSnapshotTest,reparentShowsChild)181 TEST_F(LayerSnapshotTest, reparentShowsChild) {
182     hideLayer(11);
183     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
184 
185     showLayer(11);
186     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
187 }
188 
TEST_F(LayerSnapshotTest,reparentHidesChild)189 TEST_F(LayerSnapshotTest, reparentHidesChild) {
190     hideLayer(11);
191     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
192 
193     reparentLayer(121, 11);
194     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 122, 1221, 13, 2});
195 }
196 
TEST_F(LayerSnapshotTest,unHidingUpdatesSnapshot)197 TEST_F(LayerSnapshotTest, unHidingUpdatesSnapshot) {
198     hideLayer(11);
199     Rect crop(1, 2, 3, 4);
200     setCrop(111, crop);
201     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
202 
203     showLayer(11);
204     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
205     EXPECT_EQ(getSnapshot(111)->geomLayerBounds, crop.toFloatRect());
206 }
207 
TEST_F(LayerSnapshotTest,childBehindParentCanBeHiddenByParent)208 TEST_F(LayerSnapshotTest, childBehindParentCanBeHiddenByParent) {
209     setZ(111, -1);
210     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 111, 11, 12, 121, 122, 1221, 13, 2});
211 
212     hideLayer(11);
213     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
214 }
215 
TEST_F(LayerSnapshotTest,offscreenLayerSnapshotIsInvisible)216 TEST_F(LayerSnapshotTest, offscreenLayerSnapshotIsInvisible) {
217     EXPECT_EQ(getSnapshot(111)->isVisible, true);
218 
219     reparentLayer(11, UNASSIGNED_LAYER_ID);
220     destroyLayerHandle(11);
221     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
222 
223     EXPECT_EQ(getSnapshot(111)->isVisible, false);
224     EXPECT_TRUE(getSnapshot(111)->changes.test(RequestedLayerState::Changes::Visibility));
225 }
226 
227 // relative tests
TEST_F(LayerSnapshotTest,RelativeParentCanHideChild)228 TEST_F(LayerSnapshotTest, RelativeParentCanHideChild) {
229     reparentRelativeLayer(13, 11);
230     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
231 
232     hideLayer(11);
233     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
234 }
235 
TEST_F(LayerSnapshotTest,ReparentingToHiddenRelativeParentHidesChild)236 TEST_F(LayerSnapshotTest, ReparentingToHiddenRelativeParentHidesChild) {
237     hideLayer(11);
238     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2});
239     reparentRelativeLayer(13, 11);
240     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2});
241 }
242 
TEST_F(LayerSnapshotTest,AlphaInheritedByChildren)243 TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) {
244     setAlpha(1, 0.5);
245     setAlpha(122, 0.5);
246     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
247     EXPECT_EQ(getSnapshot(1)->alpha, 0.5f);
248     EXPECT_EQ(getSnapshot(12)->alpha, 0.5f);
249     EXPECT_EQ(getSnapshot(1221)->alpha, 0.25f);
250 }
251 
252 // Change states
TEST_F(LayerSnapshotTest,UpdateClearsPreviousChangeStates)253 TEST_F(LayerSnapshotTest, UpdateClearsPreviousChangeStates) {
254     setCrop(1, Rect(1, 2, 3, 4));
255     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
256     EXPECT_TRUE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
257     EXPECT_TRUE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
258     setCrop(2, Rect(1, 2, 3, 4));
259     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
260     EXPECT_TRUE(getSnapshot(2)->changes.test(RequestedLayerState::Changes::Geometry));
261     EXPECT_FALSE(getSnapshot(1)->changes.test(RequestedLayerState::Changes::Geometry));
262     EXPECT_FALSE(getSnapshot(11)->changes.test(RequestedLayerState::Changes::Geometry));
263 }
264 
TEST_F(LayerSnapshotTest,FastPathClearsPreviousChangeStates)265 TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) {
266     setColor(11, {1._hf, 0._hf, 0._hf});
267     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
268     EXPECT_EQ(getSnapshot(11)->changes,
269               RequestedLayerState::Changes::Content);
270     EXPECT_EQ(getSnapshot(11)->clientChanges, layer_state_t::eColorChanged);
271     EXPECT_EQ(getSnapshot(1)->changes.get(), 0u);
272     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
273     EXPECT_EQ(getSnapshot(11)->changes.get(), 0u);
274 }
275 
TEST_F(LayerSnapshotTest,FastPathSetsChangeFlagToContent)276 TEST_F(LayerSnapshotTest, FastPathSetsChangeFlagToContent) {
277     setColor(1, {1._hf, 0._hf, 0._hf});
278     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
279     EXPECT_EQ(getSnapshot(1)->changes,
280               RequestedLayerState::Changes::Content);
281     EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eColorChanged);
282 }
283 
TEST_F(LayerSnapshotTest,GameMode)284 TEST_F(LayerSnapshotTest, GameMode) {
285     std::vector<TransactionState> transactions;
286     transactions.emplace_back();
287     transactions.back().states.push_back({});
288     transactions.back().states.front().state.what = layer_state_t::eMetadataChanged;
289     transactions.back().states.front().state.metadata = LayerMetadata();
290     transactions.back().states.front().state.metadata.setInt32(METADATA_GAME_MODE, 42);
291     transactions.back().states.front().layerId = 1;
292     transactions.back().states.front().state.layerId = static_cast<int32_t>(1);
293     mLifecycleManager.applyTransactions(transactions);
294     EXPECT_EQ(mLifecycleManager.getGlobalChanges(), RequestedLayerState::Changes::GameMode);
295     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
296     EXPECT_EQ(getSnapshot(1)->clientChanges, layer_state_t::eMetadataChanged);
297     EXPECT_EQ(static_cast<int32_t>(getSnapshot(1)->gameMode), 42);
298     EXPECT_EQ(static_cast<int32_t>(getSnapshot(11)->gameMode), 42);
299 }
300 
TEST_F(LayerSnapshotTest,NoLayerVoteForParentWithChildVotes)301 TEST_F(LayerSnapshotTest, NoLayerVoteForParentWithChildVotes) {
302     // ROOT
303     // ├── 1
304     // │   ├── 11 (frame rate set)
305     // │   │   └── 111
306     // │   ├── 12
307     // │   │   ├── 121
308     // │   │   └── 122
309     // │   │       └── 1221
310     // │   └── 13
311     // └── 2
312 
313     setFrameRate(11, 90.0, ANATIVEWINDOW_FRAME_RATE_EXACT, ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS);
314     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
315 
316     EXPECT_EQ(getSnapshot(11)->frameRate.vote.rate.getIntValue(), 90);
317     EXPECT_EQ(getSnapshot(11)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
318     EXPECT_EQ(getSnapshot(111)->frameRate.vote.rate.getIntValue(), 90);
319     EXPECT_EQ(getSnapshot(111)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
320     EXPECT_EQ(getSnapshot(1)->frameRate.vote.rate.getIntValue(), 0);
321     EXPECT_EQ(getSnapshot(1)->frameRate.vote.type, scheduler::FrameRateCompatibility::NoVote);
322 }
323 
TEST_F(LayerSnapshotTest,NoLayerVoteForParentWithChildVotesDoesNotAffectSiblings)324 TEST_F(LayerSnapshotTest, NoLayerVoteForParentWithChildVotesDoesNotAffectSiblings) {
325     // ROOT
326     // ├── 1 (verify layer has no vote)
327     // │   ├── 11 (frame rate set)
328     // │   │   └── 111
329     // │   ├── 12 (frame rate set)
330     // │   │   ├── 121
331     // │   │   └── 122
332     // │   │       └── 1221
333     // │   └── 13 (verify layer has default vote)
334     // └── 2
335 
336     setFrameRate(11, 90.0, ANATIVEWINDOW_FRAME_RATE_EXACT, ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS);
337     setFrameRate(12, 45.0, ANATIVEWINDOW_FRAME_RATE_EXACT, ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS);
338 
339     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
340 
341     EXPECT_EQ(getSnapshot(11)->frameRate.vote.rate.getIntValue(), 90);
342     EXPECT_EQ(getSnapshot(11)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
343     EXPECT_EQ(getSnapshot(111)->frameRate.vote.rate.getIntValue(), 90);
344     EXPECT_EQ(getSnapshot(111)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
345     EXPECT_EQ(getSnapshot(12)->frameRate.vote.rate.getIntValue(), 45);
346     EXPECT_EQ(getSnapshot(12)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
347     EXPECT_EQ(getSnapshot(121)->frameRate.vote.rate.getIntValue(), 45);
348     EXPECT_EQ(getSnapshot(121)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
349     EXPECT_EQ(getSnapshot(1221)->frameRate.vote.rate.getIntValue(), 45);
350     EXPECT_EQ(getSnapshot(1221)->frameRate.vote.type, scheduler::FrameRateCompatibility::Exact);
351 
352     EXPECT_EQ(getSnapshot(1)->frameRate.vote.rate.getIntValue(), 0);
353     EXPECT_EQ(getSnapshot(1)->frameRate.vote.type, scheduler::FrameRateCompatibility::NoVote);
354     EXPECT_EQ(getSnapshot(13)->frameRate.vote.rate.getIntValue(), 0);
355     EXPECT_EQ(getSnapshot(13)->frameRate.vote.type, scheduler::FrameRateCompatibility::Default);
356     EXPECT_EQ(getSnapshot(2)->frameRate.vote.rate.getIntValue(), 0);
357     EXPECT_EQ(getSnapshot(2)->frameRate.vote.type, scheduler::FrameRateCompatibility::Default);
358 }
359 
TEST_F(LayerSnapshotTest,CanCropTouchableRegion)360 TEST_F(LayerSnapshotTest, CanCropTouchableRegion) {
361     // ROOT
362     // ├── 1
363     // │   ├── 11
364     // │   │   └── 111 (touchregion set to touch but cropped by layer 13)
365     // │   ├── 12
366     // │   │   ├── 121
367     // │   │   └── 122
368     // │   │       └── 1221
369     // │   └── 13 (crop set to touchCrop)
370     // └── 2
371 
372     Rect touchCrop{300, 300, 400, 500};
373     setCrop(13, touchCrop);
374     Region touch{Rect{0, 0, 1000, 1000}};
375     setTouchableRegionCrop(111, touch, /*touchCropId=*/13, /*replaceTouchableRegionWithCrop=*/true);
376     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
377     EXPECT_EQ(getSnapshot({.id = 111})->inputInfo.touchableRegion.bounds(), touchCrop);
378 
379     Rect modifiedTouchCrop{100, 300, 400, 700};
380     setCrop(13, modifiedTouchCrop);
381     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
382     EXPECT_EQ(getSnapshot({.id = 111})->inputInfo.touchableRegion.bounds(), modifiedTouchCrop);
383 }
384 
TEST_F(LayerSnapshotTest,CanCropTouchableRegionWithDisplayTransform)385 TEST_F(LayerSnapshotTest, CanCropTouchableRegionWithDisplayTransform) {
386     DisplayInfo displayInfo;
387     displayInfo.transform = ui::Transform(ui::Transform::RotationFlags::ROT_90, 1000, 1000);
388     mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), displayInfo);
389 
390     Rect touchCrop{300, 300, 400, 500};
391     createRootLayer(3);
392     setCrop(3, touchCrop);
393     setLayerStack(3, 1);
394     Region touch{Rect{0, 0, 1000, 1000}};
395     setTouchableRegionCrop(3, touch, /*touchCropId=*/3, /*replaceTouchableRegionWithCrop=*/false);
396 
397     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3});
398     Rect rotatedCrop = {500, 300, 700, 400};
399     EXPECT_EQ(getSnapshot({.id = 3})->inputInfo.touchableRegion.bounds(), rotatedCrop);
400 }
401 
TEST_F(LayerSnapshotTest,blurUpdatesWhenAlphaChanges)402 TEST_F(LayerSnapshotTest, blurUpdatesWhenAlphaChanges) {
403     int blurRadius = 42;
404     setBackgroundBlurRadius(1221, static_cast<uint32_t>(blurRadius));
405 
406     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
407     EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius, blurRadius);
408 
409     blurRadius = 21;
410     setBackgroundBlurRadius(1221, static_cast<uint32_t>(blurRadius));
411     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
412     EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius, blurRadius);
413 
414     static constexpr float alpha = 0.5;
415     setAlpha(12, alpha);
416     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
417     EXPECT_EQ(getSnapshot({.id = 1221})->backgroundBlurRadius,
418               static_cast<int>(static_cast<float>(blurRadius) * alpha));
419 }
420 
421 // Display Mirroring Tests
422 // tree with 3 levels of children
423 // ROOT (DISPLAY 0)
424 // ├── 1
425 // │   ├── 11
426 // │   │   └── 111
427 // │   ├── 12 (has skip screenshot flag)
428 // │   │   ├── 121
429 // │   │   └── 122
430 // │   │       └── 1221
431 // │   └── 13
432 // └── 2
433 // ROOT (DISPLAY 1)
434 // └── 3 (mirrors display 0)
TEST_F(LayerSnapshotTest,displayMirrorRespectsLayerSkipScreenshotFlag)435 TEST_F(LayerSnapshotTest, displayMirrorRespectsLayerSkipScreenshotFlag) {
436     setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
437     createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
438     setLayerStack(3, 1);
439 
440     std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3, 1, 11, 111, 13, 2};
441     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
442 }
443 
444 // ROOT (DISPLAY 0)
445 // ├── 1
446 // │   ├── 11
447 // │   │   └── 111
448 // │   └── 13
449 // └── 2
450 // ROOT (DISPLAY 3)
451 // └── 3 (mirrors display 0)
TEST_F(LayerSnapshotTest,mirrorLayerGetsCorrectLayerStack)452 TEST_F(LayerSnapshotTest, mirrorLayerGetsCorrectLayerStack) {
453     reparentLayer(12, UNASSIGNED_LAYER_ID);
454     createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
455     setLayerStack(3, 3);
456     createDisplayMirrorLayer(4, ui::LayerStack::fromValue(0));
457     setLayerStack(4, 4);
458 
459     std::vector<uint32_t> expected = {1,  11, 111, 13, 2,  3,   1,  11, 111,
460                                       13, 2,  4,   1,  11, 111, 13, 2};
461     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
462     EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootIds = 3u})->outputFilter.layerStack.id, 3u);
463     EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootIds = 4u})->outputFilter.layerStack.id, 4u);
464 }
465 
466 // ROOT (DISPLAY 0)
467 // ├── 1 (crop 50x50)
468 // │   ├── 11
469 // │   │   └── 111
470 // │   └── 13
471 // └── 2
472 // ROOT (DISPLAY 3)
473 // └── 3 (mirrors display 0) (crop 100x100)
TEST_F(LayerSnapshotTest,mirrorLayerTouchIsCroppedByMirrorRoot)474 TEST_F(LayerSnapshotTest, mirrorLayerTouchIsCroppedByMirrorRoot) {
475     reparentLayer(12, UNASSIGNED_LAYER_ID);
476     createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
477     setLayerStack(3, 3);
478     setCrop(1, Rect{50, 50});
479     setCrop(3, Rect{100, 100});
480     setCrop(111, Rect{200, 200});
481     Region touch{Rect{0, 0, 1000, 1000}};
482     setTouchableRegion(111, touch);
483     std::vector<uint32_t> expected = {1, 11, 111, 13, 2, 3, 1, 11, 111, 13, 2};
484     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
485     EXPECT_TRUE(getSnapshot({.id = 111})->inputInfo.touchableRegion.hasSameRects(touch));
486     Region touchCroppedByMirrorRoot{Rect{0, 0, 50, 50}};
487     EXPECT_TRUE(getSnapshot({.id = 111, .mirrorRootIds = 3u})
488                         ->inputInfo.touchableRegion.hasSameRects(touchCroppedByMirrorRoot));
489 }
490 
TEST_F(LayerSnapshotTest,canRemoveDisplayMirror)491 TEST_F(LayerSnapshotTest, canRemoveDisplayMirror) {
492     setFlags(12, layer_state_t::eLayerSkipScreenshot, layer_state_t::eLayerSkipScreenshot);
493     createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
494     setLayerStack(3, 1);
495     std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3, 1, 11, 111, 13, 2};
496     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
497     destroyLayerHandle(3);
498     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
499 }
500 
TEST_F(LayerSnapshotTest,cleanUpUnreachableSnapshotsAfterMirroring)501 TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterMirroring) {
502     size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
503     createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
504     setLayerStack(3, 1);
505     std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13, 2, 3,
506                                       1, 11, 111, 12, 121, 122, 1221, 13, 2};
507     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
508     destroyLayerHandle(3);
509     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
510 
511     EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
512 }
513 
TEST_F(LayerSnapshotTest,canMirrorDisplayWithMirrors)514 TEST_F(LayerSnapshotTest, canMirrorDisplayWithMirrors) {
515     reparentLayer(12, UNASSIGNED_LAYER_ID);
516     mirrorLayer(/*layer*/ 14, /*parent*/ 1, /*layerToMirror*/ 11);
517     std::vector<uint32_t> expected = {1, 11, 111, 13, 14, 11, 111, 2};
518     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
519 
520     createDisplayMirrorLayer(3, ui::LayerStack::fromValue(0));
521     setLayerStack(3, 3);
522     expected = {1, 11, 111, 13, 14, 11, 111, 2, 3, 1, 11, 111, 13, 14, 11, 111, 2};
523     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
524     EXPECT_EQ(getSnapshot({.id = 11, .mirrorRootIds = 14u})->outputFilter.layerStack.id, 0u);
525     EXPECT_EQ(getSnapshot({.id = 11, .mirrorRootIds = 3u})->outputFilter.layerStack.id, 3u);
526     EXPECT_EQ(getSnapshot({.id = 11, .mirrorRootIds = 3u, 14u})->outputFilter.layerStack.id, 3u);
527 }
528 
529 // Rel z doesn't create duplicate snapshots but this is for completeness
TEST_F(LayerSnapshotTest,cleanUpUnreachableSnapshotsAfterRelZ)530 TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterRelZ) {
531     size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
532     reparentRelativeLayer(13, 11);
533     UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2});
534     setZ(13, 0);
535     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
536 
537     EXPECT_EQ(startingNumSnapshots, mSnapshotBuilder.getSnapshots().size());
538 }
539 
TEST_F(LayerSnapshotTest,cleanUpUnreachableSnapshotsAfterLayerDestruction)540 TEST_F(LayerSnapshotTest, cleanUpUnreachableSnapshotsAfterLayerDestruction) {
541     size_t startingNumSnapshots = mSnapshotBuilder.getSnapshots().size();
542     destroyLayerHandle(2);
543     destroyLayerHandle(122);
544 
545     std::vector<uint32_t> expected = {1, 11, 111, 12, 121, 122, 1221, 13};
546     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
547 
548     EXPECT_LE(startingNumSnapshots - 2, mSnapshotBuilder.getSnapshots().size());
549 }
550 
TEST_F(LayerSnapshotTest,snashotContainsMetadataFromLayerCreationArgs)551 TEST_F(LayerSnapshotTest, snashotContainsMetadataFromLayerCreationArgs) {
552     LayerCreationArgs args(std::make_optional<uint32_t>(200));
553     args.name = "testlayer";
554     args.addToRoot = true;
555     args.metadata.setInt32(42, 24);
556 
557     std::vector<std::unique_ptr<RequestedLayerState>> layers;
558     layers.emplace_back(std::make_unique<RequestedLayerState>(args));
559     EXPECT_TRUE(layers.back()->metadata.has(42));
560     EXPECT_EQ(layers.back()->metadata.getInt32(42, 0), 24);
561     mLifecycleManager.addLayers(std::move(layers));
562 
563     std::vector<uint32_t> expected = STARTING_ZORDER;
564     expected.push_back(200);
565     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
566 
567     EXPECT_TRUE(mSnapshotBuilder.getSnapshot(200)->layerMetadata.has(42));
568     EXPECT_EQ(mSnapshotBuilder.getSnapshot(200)->layerMetadata.getInt32(42, 0), 24);
569 }
570 
TEST_F(LayerSnapshotTest,frameRateSelectionPriorityPassedToChildLayers)571 TEST_F(LayerSnapshotTest, frameRateSelectionPriorityPassedToChildLayers) {
572     setFrameRateSelectionPriority(11, 1);
573 
574     setFrameRateSelectionPriority(12, 2);
575 
576     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
577     EXPECT_EQ(getSnapshot({.id = 1})->frameRateSelectionPriority, Layer::PRIORITY_UNSET);
578     EXPECT_EQ(getSnapshot({.id = 11})->frameRateSelectionPriority, 1);
579     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionPriority, 2);
580     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionPriority, 2);
581     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionPriority, 2);
582 
583     // reparent and verify the child gets the new parent's framerate selection priority
584     reparentLayer(122, 11);
585 
586     std::vector<uint32_t> expected = {1, 11, 111, 122, 1221, 12, 121, 13, 2};
587     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
588     EXPECT_EQ(getSnapshot({.id = 1})->frameRateSelectionPriority, Layer::PRIORITY_UNSET);
589     EXPECT_EQ(getSnapshot({.id = 11})->frameRateSelectionPriority, 1);
590     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionPriority, 2);
591     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionPriority, 1);
592     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionPriority, 1);
593 }
594 
TEST_F(LayerSnapshotTest,framerate)595 TEST_F(LayerSnapshotTest, framerate) {
596     setFrameRate(11, 244.f, 0, 0);
597 
598     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
599     // verify parent is gets no vote
600     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
601     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
602               scheduler::FrameRateCompatibility::NoVote);
603     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
604 
605     // verify layer and children get the requested votes
606     EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.vote.rate.isValid());
607     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 244.f);
608     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.type,
609               scheduler::FrameRateCompatibility::Default);
610     EXPECT_TRUE(getSnapshot({.id = 11})->changes.test(RequestedLayerState::Changes::FrameRate));
611 
612     EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.vote.rate.isValid());
613     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.rate.getValue(), 244.f);
614     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.type,
615               scheduler::FrameRateCompatibility::Default);
616     EXPECT_TRUE(getSnapshot({.id = 111})->changes.test(RequestedLayerState::Changes::FrameRate));
617 
618     // reparent and verify the child gets the new parent's framerate
619     reparentLayer(122, 11);
620 
621     std::vector<uint32_t> expected = {1, 11, 111, 122, 1221, 12, 121, 13, 2};
622     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
623     // verify parent is gets no vote
624     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
625     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
626               scheduler::FrameRateCompatibility::NoVote);
627 
628     // verify layer and children get the requested votes
629     EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.vote.rate.isValid());
630     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 244.f);
631     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.type,
632               scheduler::FrameRateCompatibility::Default);
633 
634     EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.vote.rate.isValid());
635     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.rate.getValue(), 244.f);
636     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.type,
637               scheduler::FrameRateCompatibility::Default);
638 
639     EXPECT_TRUE(getSnapshot({.id = 122})->frameRate.vote.rate.isValid());
640     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.rate.getValue(), 244.f);
641     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.type,
642               scheduler::FrameRateCompatibility::Default);
643     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
644 
645     // reparent and verify the new parent gets no vote
646     reparentLayer(11, 2);
647     expected = {1, 12, 121, 13, 2, 11, 111, 122, 1221};
648     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
649 
650     // verify old parent has invalid framerate (default)
651     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
652     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
653               scheduler::FrameRateCompatibility::Default);
654     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
655 
656     // verify new parent get no vote
657     EXPECT_FALSE(getSnapshot({.id = 2})->frameRate.vote.rate.isValid());
658     EXPECT_EQ(getSnapshot({.id = 2})->frameRate.vote.type,
659               scheduler::FrameRateCompatibility::NoVote);
660     EXPECT_TRUE(getSnapshot({.id = 2})->changes.test(RequestedLayerState::Changes::FrameRate));
661 
662     // verify layer and children get the requested votes (unchanged)
663     EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.vote.rate.isValid());
664     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 244.f);
665     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.type,
666               scheduler::FrameRateCompatibility::Default);
667 
668     EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.vote.rate.isValid());
669     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.rate.getValue(), 244.f);
670     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.type,
671               scheduler::FrameRateCompatibility::Default);
672 
673     EXPECT_TRUE(getSnapshot({.id = 122})->frameRate.vote.rate.isValid());
674     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.rate.getValue(), 244.f);
675     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.type,
676               scheduler::FrameRateCompatibility::Default);
677 }
678 
TEST_F(LayerSnapshotTest,translateDataspace)679 TEST_F(LayerSnapshotTest, translateDataspace) {
680     setDataspace(1, ui::Dataspace::UNKNOWN);
681     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
682     EXPECT_EQ(getSnapshot({.id = 1})->dataspace, ui::Dataspace::V0_SRGB);
683 }
684 
685 // This test is similar to "frameRate" test case but checks that the setFrameRateCategory API
686 // interaction also works correctly with the setFrameRate API within SF frontend.
TEST_F(LayerSnapshotTest,frameRateWithCategory)687 TEST_F(LayerSnapshotTest, frameRateWithCategory) {
688     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
689 
690     // ROOT
691     // ├── 1
692     // │   ├── 11 (frame rate set to 244.f)
693     // │   │   └── 111
694     // │   ├── 12
695     // │   │   ├── 121
696     // │   │   └── 122 (frame rate category set to Normal)
697     // │   │       └── 1221
698     // │   └── 13
699     // └── 2
700     setFrameRate(11, 244.f, 0, 0);
701     setFrameRateCategory(122, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NORMAL);
702 
703     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
704     // verify parent 1 gets no vote
705     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
706     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
707               scheduler::FrameRateCompatibility::NoVote);
708     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
709 
710     // verify layer 11 and children 111 get the requested votes
711     EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.vote.rate.isValid());
712     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 244.f);
713     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.type,
714               scheduler::FrameRateCompatibility::Default);
715     EXPECT_TRUE(getSnapshot({.id = 11})->changes.test(RequestedLayerState::Changes::FrameRate));
716 
717     EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.vote.rate.isValid());
718     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.rate.getValue(), 244.f);
719     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.type,
720               scheduler::FrameRateCompatibility::Default);
721     EXPECT_TRUE(getSnapshot({.id = 111})->changes.test(RequestedLayerState::Changes::FrameRate));
722 
723     // verify parent 12 gets no vote
724     EXPECT_FALSE(getSnapshot({.id = 12})->frameRate.vote.rate.isValid());
725     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.vote.type,
726               scheduler::FrameRateCompatibility::NoVote);
727     EXPECT_TRUE(getSnapshot({.id = 12})->changes.test(RequestedLayerState::Changes::FrameRate));
728 
729     // verify layer 122 and children 1221 get the requested votes
730     EXPECT_FALSE(getSnapshot({.id = 122})->frameRate.vote.rate.isValid());
731     EXPECT_TRUE(getSnapshot({.id = 122})->frameRate.isValid());
732     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.type,
733               scheduler::FrameRateCompatibility::Default);
734     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.category, FrameRateCategory::Normal);
735     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
736     EXPECT_TRUE(
737             getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::AffectsChildren));
738 
739     EXPECT_FALSE(getSnapshot({.id = 1221})->frameRate.vote.rate.isValid());
740     EXPECT_TRUE(getSnapshot({.id = 1221})->frameRate.isValid());
741     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.vote.type,
742               scheduler::FrameRateCompatibility::Default);
743     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.category, FrameRateCategory::Normal);
744     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
745     EXPECT_TRUE(
746             getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::AffectsChildren));
747 
748     // reparent and verify the child does NOT get the new parent's framerate because it already has
749     // the frame rate category specified.
750     // ROOT
751     //  ├─1
752     //  │  ├─11 (frame rate set to 244.f)
753     //  │  │  ├─111
754     //  │  │  └─122 (frame rate category set to Normal)
755     //  │  │     └─1221
756     //  │  ├─12
757     //  │  │  └─121
758     //  │  └─13
759     //  └─2
760     reparentLayer(122, 11);
761 
762     std::vector<uint32_t> expected = {1, 11, 111, 122, 1221, 12, 121, 13, 2};
763     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
764     // verify parent is gets no vote
765     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
766     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
767               scheduler::FrameRateCompatibility::NoVote);
768 
769     // verify layer 11 and children 111 get the requested votes
770     EXPECT_TRUE(getSnapshot({.id = 11})->frameRate.vote.rate.isValid());
771     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 244.f);
772     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.type,
773               scheduler::FrameRateCompatibility::Default);
774 
775     EXPECT_TRUE(getSnapshot({.id = 111})->frameRate.vote.rate.isValid());
776     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.rate.getValue(), 244.f);
777     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.type,
778               scheduler::FrameRateCompatibility::Default);
779 
780     // verify layer 122 and children 1221 get the requested category vote (unchanged from
781     // reparenting)
782     EXPECT_FALSE(getSnapshot({.id = 122})->frameRate.vote.rate.isValid());
783     EXPECT_TRUE(getSnapshot({.id = 122})->frameRate.isValid());
784     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.type,
785               scheduler::FrameRateCompatibility::Default);
786     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.category, FrameRateCategory::Normal);
787     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
788 
789     EXPECT_FALSE(getSnapshot({.id = 1221})->frameRate.vote.rate.isValid());
790     EXPECT_TRUE(getSnapshot({.id = 1221})->frameRate.isValid());
791     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.vote.type,
792               scheduler::FrameRateCompatibility::Default);
793     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.category, FrameRateCategory::Normal);
794     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
795 }
796 
TEST_F(LayerSnapshotTest,frameRateSelectionStrategy)797 TEST_F(LayerSnapshotTest, frameRateSelectionStrategy) {
798     // ROOT
799     // ├── 1
800     // │   ├── 11
801     // │   │   └── 111
802     // │   ├── 12 (frame rate set to 244.f with strategy OverrideChildren)
803     // │   │   ├── 121
804     // │   │   └── 122 (frame rate set to 123.f but should be overridden by layer 12)
805     // │   │       └── 1221
806     // │   └── 13
807     // └── 2
808     setFrameRate(12, 244.f, 0, 0);
809     setFrameRate(122, 123.f, 0, 0);
810     setFrameRateSelectionStrategy(12, 1 /* OverrideChildren */);
811 
812     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
813     // verify parent 1 gets no vote
814     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
815     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
816               scheduler::FrameRateCompatibility::NoVote);
817     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
818 
819     // verify layer 12 and all descendants (121, 122, 1221) get the requested vote
820     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.vote.rate.getValue(), 244.f);
821     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionStrategy,
822               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
823     EXPECT_TRUE(getSnapshot({.id = 12})->changes.test(RequestedLayerState::Changes::FrameRate));
824 
825     EXPECT_EQ(getSnapshot({.id = 121})->frameRate.vote.rate.getValue(), 244.f);
826     EXPECT_EQ(getSnapshot({.id = 121})->frameRateSelectionStrategy,
827               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
828     EXPECT_TRUE(getSnapshot({.id = 121})->changes.test(RequestedLayerState::Changes::FrameRate));
829 
830     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.rate.getValue(), 244.f);
831     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionStrategy,
832               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
833     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
834 
835     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.vote.rate.getValue(), 244.f);
836     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionStrategy,
837               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
838     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
839 
840     // ROOT
841     // ├── 1
842     // │   ├── 11
843     // │   │   └── 111
844     // │   ├── 12 (frame rate set to default with strategy default)
845     // │   │   ├── 121
846     // │   │   └── 122 (frame rate set to 123.f)
847     // │   │       └── 1221
848     // │   └── 13
849     // └── 2
850     setFrameRate(12, -1.f, 0, 0);
851     setFrameRateSelectionStrategy(12, 0 /* Default */);
852     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
853     // verify parent 1 gets no vote
854     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
855     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
856               scheduler::FrameRateCompatibility::NoVote);
857     EXPECT_FALSE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
858 
859     // verify layer 12 and all descendants (121, 122, 1221) get the requested vote
860     EXPECT_FALSE(getSnapshot({.id = 12})->frameRate.vote.rate.isValid());
861     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.vote.type,
862               scheduler::FrameRateCompatibility::NoVote);
863     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionStrategy,
864               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
865     EXPECT_TRUE(getSnapshot({.id = 12})->changes.test(RequestedLayerState::Changes::FrameRate));
866 
867     EXPECT_FALSE(getSnapshot({.id = 121})->frameRate.vote.rate.isValid());
868     EXPECT_EQ(getSnapshot({.id = 121})->frameRateSelectionStrategy,
869               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
870     EXPECT_EQ(getSnapshot({.id = 121})->frameRate.vote.type,
871               scheduler::FrameRateCompatibility::Default);
872     EXPECT_TRUE(getSnapshot({.id = 121})->changes.test(RequestedLayerState::Changes::FrameRate));
873 
874     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.rate.getValue(), 123.f);
875     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionStrategy,
876               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
877     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
878 
879     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.vote.rate.getValue(), 123.f);
880     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionStrategy,
881               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
882     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
883 }
884 
TEST_F(LayerSnapshotTest,frameRateSelectionStrategyWithCategory)885 TEST_F(LayerSnapshotTest, frameRateSelectionStrategyWithCategory) {
886     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
887 
888     // ROOT
889     // ├── 1
890     // │   ├── 11
891     // │   │   └── 111
892     // │   ├── 12 (frame rate category set to high with strategy OverrideChildren)
893     // │   │   ├── 121
894     // │   │   └── 122 (frame rate set to 123.f but should be overridden by layer 12)
895     // │   │       └── 1221
896     // │   └── 13
897     // └── 2
898     setFrameRateCategory(12, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);
899     setFrameRate(122, 123.f, 0, 0);
900     setFrameRateSelectionStrategy(12, 1 /* OverrideChildren */);
901 
902     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
903     // verify parent 1 gets no vote
904     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
905     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
906               scheduler::FrameRateCompatibility::NoVote);
907     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
908 
909     // verify layer 12 and all descendants (121, 122, 1221) get the requested vote
910     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.category, FrameRateCategory::High);
911     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionStrategy,
912               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
913     EXPECT_TRUE(getSnapshot({.id = 12})->changes.test(RequestedLayerState::Changes::FrameRate));
914 
915     EXPECT_EQ(getSnapshot({.id = 121})->frameRate.category, FrameRateCategory::High);
916     EXPECT_EQ(getSnapshot({.id = 121})->frameRateSelectionStrategy,
917               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
918     EXPECT_TRUE(getSnapshot({.id = 121})->changes.test(RequestedLayerState::Changes::FrameRate));
919 
920     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.category, FrameRateCategory::High);
921     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionStrategy,
922               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
923     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
924 
925     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.category, FrameRateCategory::High);
926     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionStrategy,
927               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
928     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
929 
930     // ROOT
931     // ├── 1
932     // │   ├── 11
933     // │   │   └── 111
934     // │   ├── 12 (frame rate category to default with strategy default)
935     // │   │   ├── 121
936     // │   │   └── 122 (frame rate set to 123.f)
937     // │   │       └── 1221
938     // │   └── 13
939     // └── 2
940     setFrameRateCategory(12, ANATIVEWINDOW_FRAME_RATE_CATEGORY_DEFAULT);
941     setFrameRateSelectionStrategy(12, 0 /* Default */);
942     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
943     // verify parent 1 gets no vote
944     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
945     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
946               scheduler::FrameRateCompatibility::NoVote);
947     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.category, FrameRateCategory::Default);
948     EXPECT_FALSE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
949 
950     // verify layer 12 and all descendants (121, 122, 1221) get the requested vote
951     EXPECT_FALSE(getSnapshot({.id = 12})->frameRate.vote.rate.isValid());
952     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.vote.type,
953               scheduler::FrameRateCompatibility::NoVote);
954     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.category, FrameRateCategory::Default);
955     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionStrategy,
956               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
957     EXPECT_TRUE(getSnapshot({.id = 12})->changes.test(RequestedLayerState::Changes::FrameRate));
958 
959     EXPECT_FALSE(getSnapshot({.id = 12})->frameRate.vote.rate.isValid());
960     EXPECT_EQ(getSnapshot({.id = 121})->frameRateSelectionStrategy,
961               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
962     EXPECT_EQ(getSnapshot({.id = 121})->frameRate.category, FrameRateCategory::Default);
963     EXPECT_EQ(getSnapshot({.id = 121})->frameRate.vote.type,
964               scheduler::FrameRateCompatibility::Default);
965     EXPECT_TRUE(getSnapshot({.id = 121})->changes.test(RequestedLayerState::Changes::FrameRate));
966 
967     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.rate.getValue(), 123.f);
968     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionStrategy,
969               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
970     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.category, FrameRateCategory::Default);
971     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
972 
973     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.vote.rate.getValue(), 123.f);
974     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionStrategy,
975               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
976     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.category, FrameRateCategory::Default);
977     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
978 }
979 
TEST_F(LayerSnapshotTest,frameRateSelectionStrategyWithOverrideChildrenAndSelf)980 TEST_F(LayerSnapshotTest, frameRateSelectionStrategyWithOverrideChildrenAndSelf) {
981     // ROOT
982     // ├── 1
983     // │   ├── 11 (frame rate set to 11.f with strategy Self)
984     // │   │   └── 111 (frame rate is not inherited)
985     // │   ├── 12 (frame rate set to 244.f)
986     // │   │   ├── 121
987     // │   │   └── 122 (strategy OverrideChildren and inherits frame rate 244.f)
988     // │   │       └── 1221 (frame rate set to 123.f but should be overridden by layer 122)
989     // │   └── 13
990     // └── 2
991     setFrameRate(11, 11.f, 0, 0);
992     setFrameRateSelectionStrategy(11, 2 /* Self */);
993     setFrameRate(12, 244.f, 0, 0);
994     setFrameRateSelectionStrategy(122, 1 /* OverrideChildren */);
995     setFrameRate(1221, 123.f, 0, 0);
996 
997     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
998     // verify parent 1 gets no vote
999     EXPECT_FALSE(getSnapshot({.id = 1})->frameRate.vote.rate.isValid());
1000     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
1001               scheduler::FrameRateCompatibility::NoVote);
1002     EXPECT_EQ(getSnapshot({.id = 1})->frameRateSelectionStrategy,
1003               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
1004     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
1005 
1006     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 11.f);
1007     EXPECT_EQ(getSnapshot({.id = 11})->frameRateSelectionStrategy,
1008               scheduler::LayerInfo::FrameRateSelectionStrategy::Self);
1009     EXPECT_TRUE(getSnapshot({.id = 11})->changes.test(RequestedLayerState::Changes::FrameRate));
1010 
1011     // verify layer 11 does does not propagate its framerate to 111.
1012     EXPECT_FALSE(getSnapshot({.id = 111})->frameRate.vote.rate.isValid());
1013     EXPECT_EQ(getSnapshot({.id = 111})->frameRateSelectionStrategy,
1014               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
1015     EXPECT_TRUE(getSnapshot({.id = 111})->changes.test(RequestedLayerState::Changes::FrameRate));
1016 
1017     // verify layer 12 and all descendants (121, 122, 1221) get the requested vote
1018     EXPECT_EQ(getSnapshot({.id = 12})->frameRate.vote.rate.getValue(), 244.f);
1019     EXPECT_EQ(getSnapshot({.id = 12})->frameRateSelectionStrategy,
1020               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
1021     EXPECT_TRUE(getSnapshot({.id = 12})->changes.test(RequestedLayerState::Changes::FrameRate));
1022 
1023     EXPECT_EQ(getSnapshot({.id = 121})->frameRate.vote.rate.getValue(), 244.f);
1024     EXPECT_EQ(getSnapshot({.id = 121})->frameRateSelectionStrategy,
1025               scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
1026     EXPECT_TRUE(getSnapshot({.id = 121})->changes.test(RequestedLayerState::Changes::FrameRate));
1027 
1028     EXPECT_EQ(getSnapshot({.id = 122})->frameRate.vote.rate.getValue(), 244.f);
1029     EXPECT_EQ(getSnapshot({.id = 122})->frameRateSelectionStrategy,
1030               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
1031     EXPECT_TRUE(getSnapshot({.id = 122})->changes.test(RequestedLayerState::Changes::FrameRate));
1032 
1033     EXPECT_EQ(getSnapshot({.id = 1221})->frameRate.vote.rate.getValue(), 244.f);
1034     EXPECT_EQ(getSnapshot({.id = 1221})->frameRateSelectionStrategy,
1035               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
1036     EXPECT_TRUE(getSnapshot({.id = 1221})->changes.test(RequestedLayerState::Changes::FrameRate));
1037 
1038     // ROOT
1039     // ├── 1 (frame rate set to 1.f with strategy OverrideChildren)
1040     // │   ├── 11 (frame rate set to 11.f with strategy Self, but overridden by 1)
1041     // │   │   └── 111 (frame rate inherited from 11 due to override from 1)
1042     // ⋮   ⋮
1043     setFrameRate(1, 1.f, 0, 0);
1044     setFrameRateSelectionStrategy(1, 1 /* OverrideChildren */);
1045     setFrameRate(11, 11.f, 0, 0);
1046     setFrameRateSelectionStrategy(11, 2 /* Self */);
1047     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1048 
1049     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.rate.getValue(), 1.f);
1050     EXPECT_EQ(getSnapshot({.id = 1})->frameRate.vote.type,
1051               scheduler::FrameRateCompatibility::Default);
1052     EXPECT_EQ(getSnapshot({.id = 1})->frameRateSelectionStrategy,
1053               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
1054     EXPECT_TRUE(getSnapshot({.id = 1})->changes.test(RequestedLayerState::Changes::FrameRate));
1055 
1056     EXPECT_EQ(getSnapshot({.id = 11})->frameRate.vote.rate.getValue(), 1.f);
1057     EXPECT_EQ(getSnapshot({.id = 11})->frameRateSelectionStrategy,
1058               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
1059     EXPECT_TRUE(getSnapshot({.id = 11})->changes.test(RequestedLayerState::Changes::FrameRate));
1060 
1061     // verify layer 11 does does not propagate its framerate to 111.
1062     EXPECT_EQ(getSnapshot({.id = 111})->frameRate.vote.rate.getValue(), 1.f);
1063     EXPECT_EQ(getSnapshot({.id = 111})->frameRateSelectionStrategy,
1064               scheduler::LayerInfo::FrameRateSelectionStrategy::OverrideChildren);
1065     EXPECT_TRUE(getSnapshot({.id = 111})->changes.test(RequestedLayerState::Changes::FrameRate));
1066 }
1067 
TEST_F(LayerSnapshotTest,skipRoundCornersWhenProtected)1068 TEST_F(LayerSnapshotTest, skipRoundCornersWhenProtected) {
1069     setRoundedCorners(1, 42.f);
1070     setRoundedCorners(2, 42.f);
1071     setCrop(1, Rect{1000, 1000});
1072     setCrop(2, Rect{1000, 1000});
1073 
1074     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1075     EXPECT_TRUE(getSnapshot({.id = 1})->roundedCorner.hasRoundedCorners());
1076     EXPECT_EQ(getSnapshot({.id = 1})->roundedCorner.radius.x, 42.f);
1077     EXPECT_TRUE(getSnapshot({.id = 2})->roundedCorner.hasRoundedCorners());
1078 
1079     // add a buffer with the protected bit, check rounded corners are not set when
1080     // skipRoundCornersWhenProtected == true
1081     setBuffer(1,
1082               std::make_shared<
1083                       renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
1084                                                                1ULL /* bufferId */,
1085                                                                HAL_PIXEL_FORMAT_RGBA_8888,
1086                                                                GRALLOC_USAGE_PROTECTED /*usage*/));
1087 
1088     LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
1089                                     .layerLifecycleManager = mLifecycleManager,
1090                                     .includeMetadata = false,
1091                                     .displays = mFrontEndDisplayInfos,
1092                                     .displayChanges = false,
1093                                     .globalShadowSettings = globalShadowSettings,
1094                                     .supportsBlur = true,
1095                                     .supportedLayerGenericMetadata = {},
1096                                     .genericLayerMetadataKeyMap = {},
1097                                     .skipRoundCornersWhenProtected = true};
1098     update(mSnapshotBuilder, args);
1099     EXPECT_FALSE(getSnapshot({.id = 1})->roundedCorner.hasRoundedCorners());
1100     // layer 2 doesn't have a buffer and should be unaffected
1101     EXPECT_TRUE(getSnapshot({.id = 2})->roundedCorner.hasRoundedCorners());
1102 
1103     // remove protected bit, check rounded corners are set
1104     setBuffer(1,
1105               std::make_shared<renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
1106                                                                         2ULL /* bufferId */,
1107                                                                         HAL_PIXEL_FORMAT_RGBA_8888,
1108                                                                         0 /*usage*/));
1109     update(mSnapshotBuilder, args);
1110     EXPECT_TRUE(getSnapshot({.id = 1})->roundedCorner.hasRoundedCorners());
1111     EXPECT_EQ(getSnapshot({.id = 1})->roundedCorner.radius.x, 42.f);
1112 }
1113 
TEST_F(LayerSnapshotTest,setRefreshRateIndicatorCompositionType)1114 TEST_F(LayerSnapshotTest, setRefreshRateIndicatorCompositionType) {
1115     setFlags(1, layer_state_t::eLayerIsRefreshRateIndicator,
1116              layer_state_t::eLayerIsRefreshRateIndicator);
1117     setBuffer(1,
1118               std::make_shared<renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
1119                                                                         42ULL /* bufferId */,
1120                                                                         HAL_PIXEL_FORMAT_RGBA_8888,
1121                                                                         0 /*usage*/));
1122     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1123     EXPECT_EQ(getSnapshot({.id = 1})->compositionType,
1124               aidl::android::hardware::graphics::composer3::Composition::REFRESH_RATE_INDICATOR);
1125 }
1126 
TEST_F(LayerSnapshotTest,setBufferCrop)1127 TEST_F(LayerSnapshotTest, setBufferCrop) {
1128     // validate no buffer but has crop
1129     Rect crop = Rect(0, 0, 50, 50);
1130     setBufferCrop(1, crop);
1131     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1132     EXPECT_EQ(getSnapshot(1)->geomContentCrop, crop);
1133 
1134     setBuffer(1,
1135               std::make_shared<renderengine::mock::FakeExternalTexture>(100U /*width*/,
1136                                                                         100U /*height*/,
1137                                                                         42ULL /* bufferId */,
1138                                                                         HAL_PIXEL_FORMAT_RGBA_8888,
1139                                                                         0 /*usage*/));
1140     // validate a buffer crop within the buffer bounds
1141     setBufferCrop(1, crop);
1142     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1143     EXPECT_EQ(getSnapshot(1)->geomContentCrop, crop);
1144 
1145     // validate a buffer crop outside the buffer bounds
1146     crop = Rect(0, 0, 150, 150);
1147     setBufferCrop(1, crop);
1148     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1149     EXPECT_EQ(getSnapshot(1)->geomContentCrop, Rect(0, 0, 100, 100));
1150 
1151     // validate no buffer crop
1152     setBufferCrop(1, Rect());
1153     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1154     EXPECT_EQ(getSnapshot(1)->geomContentCrop, Rect(0, 0, 100, 100));
1155 }
1156 
TEST_F(LayerSnapshotTest,setShadowRadius)1157 TEST_F(LayerSnapshotTest, setShadowRadius) {
1158     static constexpr float SHADOW_RADIUS = 123.f;
1159     setShadowRadius(1, SHADOW_RADIUS);
1160     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1161     EXPECT_EQ(getSnapshot(1)->shadowSettings.length, SHADOW_RADIUS);
1162 }
1163 
TEST_F(LayerSnapshotTest,setTrustedOverlayForNonVisibleInput)1164 TEST_F(LayerSnapshotTest, setTrustedOverlayForNonVisibleInput) {
1165     hideLayer(1);
1166     setTrustedOverlay(1, gui::TrustedOverlay::ENABLED);
1167     Region touch{Rect{0, 0, 1000, 1000}};
1168     setTouchableRegion(1, touch);
1169 
1170     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1171     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1172             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1173 }
1174 
TEST_F(LayerSnapshotTest,alphaChangesPropagateToInput)1175 TEST_F(LayerSnapshotTest, alphaChangesPropagateToInput) {
1176     Region touch{Rect{0, 0, 1000, 1000}};
1177     setTouchableRegion(1, touch);
1178     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1179 
1180     setAlpha(1, 0.5f);
1181     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1182     EXPECT_EQ(getSnapshot(1)->inputInfo.alpha, 0.5f);
1183 }
1184 
TEST_F(LayerSnapshotTest,isFrontBuffered)1185 TEST_F(LayerSnapshotTest, isFrontBuffered) {
1186     setBuffer(1,
1187               std::make_shared<renderengine::mock::FakeExternalTexture>(
1188                       1U /*width*/, 1U /*height*/, 1ULL /* bufferId */, HAL_PIXEL_FORMAT_RGBA_8888,
1189                       GRALLOC_USAGE_HW_TEXTURE | AHARDWAREBUFFER_USAGE_FRONT_BUFFER /*usage*/));
1190 
1191     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1192     EXPECT_TRUE(getSnapshot(1)->isFrontBuffered());
1193 
1194     setBuffer(1,
1195               std::make_shared<
1196                       renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
1197                                                                1ULL /* bufferId */,
1198                                                                HAL_PIXEL_FORMAT_RGBA_8888,
1199                                                                GRALLOC_USAGE_HW_TEXTURE /*usage*/));
1200 
1201     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1202     EXPECT_FALSE(getSnapshot(1)->isFrontBuffered());
1203 }
1204 
TEST_F(LayerSnapshotTest,setSecureRootSnapshot)1205 TEST_F(LayerSnapshotTest, setSecureRootSnapshot) {
1206     setFlags(1, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1207     LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
1208                                     .layerLifecycleManager = mLifecycleManager,
1209                                     .includeMetadata = false,
1210                                     .displays = mFrontEndDisplayInfos,
1211                                     .displayChanges = false,
1212                                     .globalShadowSettings = globalShadowSettings,
1213                                     .supportsBlur = true,
1214                                     .supportedLayerGenericMetadata = {},
1215                                     .genericLayerMetadataKeyMap = {}};
1216     args.rootSnapshot.isSecure = true;
1217     update(mSnapshotBuilder, args);
1218 
1219     EXPECT_TRUE(getSnapshot(1)->isSecure);
1220     // Ensure child is also marked as secure
1221     EXPECT_TRUE(getSnapshot(11)->isSecure);
1222 }
1223 
TEST_F(LayerSnapshotTest,setSensitiveForTracingConfigForSecureLayers)1224 TEST_F(LayerSnapshotTest, setSensitiveForTracingConfigForSecureLayers) {
1225     setFlags(11, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1226 
1227     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1228 
1229     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1230             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1231     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1232             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1233     EXPECT_FALSE(getSnapshot(1)->inputInfo.inputConfig.test(
1234             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1235     EXPECT_FALSE(getSnapshot(12)->inputInfo.inputConfig.test(
1236             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1237     EXPECT_FALSE(getSnapshot(2)->inputInfo.inputConfig.test(
1238             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1239 }
1240 
TEST_F(LayerSnapshotTest,setSensitiveForTracingFromInputWindowHandle)1241 TEST_F(LayerSnapshotTest, setSensitiveForTracingFromInputWindowHandle) {
1242     setInputInfo(11, [](auto& inputInfo) {
1243         inputInfo.inputConfig |= gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY;
1244     });
1245 
1246     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1247 
1248     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1249             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1250     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1251             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1252     EXPECT_FALSE(getSnapshot(1)->inputInfo.inputConfig.test(
1253             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1254     EXPECT_FALSE(getSnapshot(12)->inputInfo.inputConfig.test(
1255             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1256     EXPECT_FALSE(getSnapshot(2)->inputInfo.inputConfig.test(
1257             gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY));
1258 }
1259 
1260 // b/314350323
TEST_F(LayerSnapshotTest,propagateDropInputMode)1261 TEST_F(LayerSnapshotTest, propagateDropInputMode) {
1262     setDropInputMode(1, gui::DropInputMode::ALL);
1263     LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
1264                                     .layerLifecycleManager = mLifecycleManager,
1265                                     .includeMetadata = false,
1266                                     .displays = mFrontEndDisplayInfos,
1267                                     .displayChanges = false,
1268                                     .globalShadowSettings = globalShadowSettings,
1269                                     .supportsBlur = true,
1270                                     .supportedLayerGenericMetadata = {},
1271                                     .genericLayerMetadataKeyMap = {}};
1272     args.rootSnapshot.isSecure = true;
1273     update(mSnapshotBuilder, args);
1274 
1275     EXPECT_EQ(getSnapshot(1)->dropInputMode, gui::DropInputMode::ALL);
1276     // Ensure child also has the correct drop input mode regardless of whether either layer has
1277     // an input channel
1278     EXPECT_EQ(getSnapshot(11)->dropInputMode, gui::DropInputMode::ALL);
1279 }
1280 
TEST_F(LayerSnapshotTest,NonVisibleLayerWithInput)1281 TEST_F(LayerSnapshotTest, NonVisibleLayerWithInput) {
1282     LayerHierarchyTestBase::createRootLayer(3);
1283     setColor(3, {-1._hf, -1._hf, -1._hf});
1284     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1285 
1286     std::vector<TransactionState> transactions;
1287     transactions.emplace_back();
1288     transactions.back().states.push_back({});
1289     transactions.back().states.front().state.what = layer_state_t::eInputInfoChanged;
1290     transactions.back().states.front().layerId = 3;
1291     transactions.back().states.front().state.windowInfoHandle = sp<gui::WindowInfoHandle>::make();
1292     auto inputInfo = transactions.back().states.front().state.windowInfoHandle->editInfo();
1293     inputInfo->token = sp<BBinder>::make();
1294     mLifecycleManager.applyTransactions(transactions);
1295 
1296     update(mSnapshotBuilder);
1297 
1298     bool foundInputLayer = false;
1299     mSnapshotBuilder.forEachInputSnapshot([&](const frontend::LayerSnapshot& snapshot) {
1300         if (snapshot.uniqueSequence == 3) {
1301             foundInputLayer = true;
1302         }
1303     });
1304     EXPECT_TRUE(foundInputLayer);
1305 }
1306 
TEST_F(LayerSnapshotTest,canOccludePresentation)1307 TEST_F(LayerSnapshotTest, canOccludePresentation) {
1308     setFlags(12, layer_state_t::eCanOccludePresentation, layer_state_t::eCanOccludePresentation);
1309     LayerSnapshotBuilder::Args args{.root = mHierarchyBuilder.getHierarchy(),
1310                                     .layerLifecycleManager = mLifecycleManager,
1311                                     .includeMetadata = false,
1312                                     .displays = mFrontEndDisplayInfos,
1313                                     .displayChanges = false,
1314                                     .globalShadowSettings = globalShadowSettings,
1315                                     .supportsBlur = true,
1316                                     .supportedLayerGenericMetadata = {},
1317                                     .genericLayerMetadataKeyMap = {}};
1318     UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER);
1319 
1320     EXPECT_EQ(getSnapshot(1)->inputInfo.canOccludePresentation, false);
1321 
1322     // ensure we can set the property on the window info for layer and all its children
1323     EXPECT_EQ(getSnapshot(12)->inputInfo.canOccludePresentation, true);
1324     EXPECT_EQ(getSnapshot(121)->inputInfo.canOccludePresentation, true);
1325     EXPECT_EQ(getSnapshot(1221)->inputInfo.canOccludePresentation, true);
1326 }
1327 
TEST_F(LayerSnapshotTest,mirroredHierarchyIgnoresLocalTransform)1328 TEST_F(LayerSnapshotTest, mirroredHierarchyIgnoresLocalTransform) {
1329     SET_FLAG_FOR_TEST(flags::detached_mirror, true);
1330     reparentLayer(12, UNASSIGNED_LAYER_ID);
1331     setPosition(11, 2, 20);
1332     setPosition(111, 20, 200);
1333     mirrorLayer(/*layer*/ 14, /*parent*/ 1, /*layerToMirror*/ 11);
1334     std::vector<uint32_t> expected = {1, 11, 111, 13, 14, 11, 111, 2};
1335     UPDATE_AND_VERIFY(mSnapshotBuilder, expected);
1336 
1337     // mirror root has no position set
1338     EXPECT_EQ(getSnapshot({.id = 11, .mirrorRootIds = 14u})->localTransform.tx(), 0);
1339     EXPECT_EQ(getSnapshot({.id = 11, .mirrorRootIds = 14u})->localTransform.ty(), 0);
1340     // original root still has a position
1341     EXPECT_EQ(getSnapshot({.id = 11})->localTransform.tx(), 2);
1342     EXPECT_EQ(getSnapshot({.id = 11})->localTransform.ty(), 20);
1343 
1344     // mirror child still has the correct position
1345     EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootIds = 14u})->localTransform.tx(), 20);
1346     EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootIds = 14u})->localTransform.ty(), 200);
1347     EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootIds = 14u})->geomLayerTransform.tx(), 20);
1348     EXPECT_EQ(getSnapshot({.id = 111, .mirrorRootIds = 14u})->geomLayerTransform.ty(), 200);
1349 
1350     // original child still has the correct position including its parent's position
1351     EXPECT_EQ(getSnapshot({.id = 111})->localTransform.tx(), 20);
1352     EXPECT_EQ(getSnapshot({.id = 111})->localTransform.ty(), 200);
1353     EXPECT_EQ(getSnapshot({.id = 111})->geomLayerTransform.tx(), 22);
1354     EXPECT_EQ(getSnapshot({.id = 111})->geomLayerTransform.ty(), 220);
1355 }
1356 
TEST_F(LayerSnapshotTest,overrideParentTrustedOverlayState)1357 TEST_F(LayerSnapshotTest, overrideParentTrustedOverlayState) {
1358     SET_FLAG_FOR_TEST(flags::override_trusted_overlay, true);
1359     hideLayer(1);
1360     setTrustedOverlay(1, gui::TrustedOverlay::ENABLED);
1361 
1362     Region touch{Rect{0, 0, 1000, 1000}};
1363     setTouchableRegion(1, touch);
1364     setTouchableRegion(11, touch);
1365     setTouchableRegion(111, touch);
1366 
1367     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1368     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1369             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1370     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1371             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1372     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1373             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1374 
1375     // disable trusted overlay and override parent state
1376     setTrustedOverlay(11, gui::TrustedOverlay::DISABLED);
1377     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1378     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1379             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1380     EXPECT_FALSE(getSnapshot(11)->inputInfo.inputConfig.test(
1381             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1382     EXPECT_FALSE(getSnapshot(111)->inputInfo.inputConfig.test(
1383             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1384 
1385     // unset state and go back to default behavior of inheriting
1386     // state
1387     setTrustedOverlay(11, gui::TrustedOverlay::UNSET);
1388     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1389     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1390             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1391     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1392             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1393     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1394             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1395 }
1396 
TEST_F(LayerSnapshotTest,doNotOverrideParentTrustedOverlayState)1397 TEST_F(LayerSnapshotTest, doNotOverrideParentTrustedOverlayState) {
1398     SET_FLAG_FOR_TEST(flags::override_trusted_overlay, false);
1399     hideLayer(1);
1400     setTrustedOverlay(1, gui::TrustedOverlay::ENABLED);
1401 
1402     Region touch{Rect{0, 0, 1000, 1000}};
1403     setTouchableRegion(1, touch);
1404     setTouchableRegion(11, touch);
1405     setTouchableRegion(111, touch);
1406 
1407     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1408     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1409             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1410     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1411             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1412     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1413             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1414 
1415     // disable trusted overlay but flag is disabled so this behaves
1416     // as UNSET
1417     setTrustedOverlay(11, gui::TrustedOverlay::DISABLED);
1418     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1419     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1420             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1421     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1422             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1423     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1424             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1425 
1426     // unset state and go back to default behavior of inheriting
1427     // state
1428     setTrustedOverlay(11, gui::TrustedOverlay::UNSET);
1429     UPDATE_AND_VERIFY(mSnapshotBuilder, {2});
1430     EXPECT_TRUE(getSnapshot(1)->inputInfo.inputConfig.test(
1431             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1432     EXPECT_TRUE(getSnapshot(11)->inputInfo.inputConfig.test(
1433             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1434     EXPECT_TRUE(getSnapshot(111)->inputInfo.inputConfig.test(
1435             gui::WindowInfo::InputConfig::TRUSTED_OVERLAY));
1436 }
1437 
1438 } // namespace android::surfaceflinger::frontend
1439