Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
| 20 | #include "FrontEnd/LayerHandle.h" |
| 21 | #include "FrontEnd/LayerHierarchy.h" |
| 22 | #include "FrontEnd/LayerLifecycleManager.h" |
| 23 | #include "FrontEnd/LayerSnapshotBuilder.h" |
| 24 | #include "Layer.h" |
| 25 | #include "LayerHierarchyTest.h" |
| 26 | |
| 27 | #define UPDATE_AND_VERIFY(BUILDER, ...) \ |
| 28 | ({ \ |
| 29 | SCOPED_TRACE(""); \ |
| 30 | updateAndVerify((BUILDER), /*displayChanges=*/false, __VA_ARGS__); \ |
| 31 | }) |
| 32 | |
| 33 | #define UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(BUILDER, ...) \ |
| 34 | ({ \ |
| 35 | SCOPED_TRACE(""); \ |
| 36 | updateAndVerify((BUILDER), /*displayChanges=*/true, __VA_ARGS__); \ |
| 37 | }) |
| 38 | |
| 39 | namespace android::surfaceflinger::frontend { |
| 40 | |
| 41 | // To run test: |
| 42 | /** |
| 43 | mp :libsurfaceflinger_unittest && adb sync; adb shell \ |
| 44 | /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \ |
| 45 | --gtest_filter="LayerSnapshotTest.*" --gtest_brief=1 |
| 46 | */ |
| 47 | |
| 48 | class LayerSnapshotTest : public LayerHierarchyTestBase { |
| 49 | protected: |
| 50 | LayerSnapshotTest() : LayerHierarchyTestBase() { |
| 51 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 52 | } |
| 53 | |
| 54 | void createRootLayer(uint32_t id) override { |
| 55 | LayerHierarchyTestBase::createRootLayer(id); |
| 56 | setColor(id); |
| 57 | } |
| 58 | |
| 59 | void createLayer(uint32_t id, uint32_t parentId) override { |
| 60 | LayerHierarchyTestBase::createLayer(id, parentId); |
| 61 | setColor(parentId); |
| 62 | } |
| 63 | |
| 64 | void mirrorLayer(uint32_t id, uint32_t parent, uint32_t layerToMirror) override { |
| 65 | LayerHierarchyTestBase::mirrorLayer(id, parent, layerToMirror); |
| 66 | setColor(id); |
| 67 | } |
| 68 | |
| 69 | void updateAndVerify(LayerSnapshotBuilder& actualBuilder, bool hasDisplayChanges, |
| 70 | const std::vector<uint32_t> expectedVisibleLayerIdsInZOrder) { |
| 71 | if (mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)) { |
| 72 | mHierarchyBuilder.update(mLifecycleManager.getLayers(), |
| 73 | mLifecycleManager.getDestroyedLayers()); |
| 74 | } |
| 75 | LayerSnapshotBuilder::Args args{ |
| 76 | .root = mHierarchyBuilder.getHierarchy(), |
| 77 | .layerLifecycleManager = mLifecycleManager, |
| 78 | .includeMetadata = false, |
| 79 | .displays = mFrontEndDisplayInfos, |
| 80 | .displayChanges = hasDisplayChanges, |
| 81 | .globalShadowSettings = globalShadowSettings, |
| 82 | }; |
| 83 | actualBuilder.update(args); |
| 84 | |
| 85 | // rebuild layer snapshots from scratch and verify that it matches the updated state. |
| 86 | LayerSnapshotBuilder expectedBuilder(args); |
| 87 | mLifecycleManager.commitChanges(); |
| 88 | ASSERT_TRUE(expectedBuilder.getSnapshots().size() > 0); |
| 89 | ASSERT_TRUE(actualBuilder.getSnapshots().size() > 0); |
| 90 | |
| 91 | std::vector<std::unique_ptr<LayerSnapshot>>& snapshots = actualBuilder.getSnapshots(); |
| 92 | std::vector<uint32_t> actualVisibleLayerIdsInZOrder; |
| 93 | for (auto& snapshot : snapshots) { |
| 94 | if (!snapshot->isVisible) { |
| 95 | break; |
| 96 | } |
| 97 | actualVisibleLayerIdsInZOrder.push_back(snapshot->path.id); |
| 98 | } |
| 99 | EXPECT_EQ(expectedVisibleLayerIdsInZOrder, actualVisibleLayerIdsInZOrder); |
| 100 | } |
| 101 | |
| 102 | LayerSnapshot* getSnapshot(uint32_t layerId) { return mSnapshotBuilder.getSnapshot(layerId); } |
| 103 | |
| 104 | LayerHierarchyBuilder mHierarchyBuilder{{}}; |
| 105 | LayerSnapshotBuilder mSnapshotBuilder; |
| 106 | std::unordered_map<uint32_t, sp<LayerHandle>> mHandles; |
| 107 | display::DisplayMap<ui::LayerStack, frontend::DisplayInfo> mFrontEndDisplayInfos; |
| 108 | renderengine::ShadowSettings globalShadowSettings; |
| 109 | static const std::vector<uint32_t> STARTING_ZORDER; |
| 110 | }; |
| 111 | const std::vector<uint32_t> LayerSnapshotTest::STARTING_ZORDER = {1, 11, 111, 12, 121, |
| 112 | 122, 1221, 13, 2}; |
| 113 | |
| 114 | TEST_F(LayerSnapshotTest, buildSnapshot) { |
| 115 | LayerSnapshotBuilder::Args args{ |
| 116 | .root = mHierarchyBuilder.getHierarchy(), |
| 117 | .layerLifecycleManager = mLifecycleManager, |
| 118 | .includeMetadata = false, |
| 119 | .displays = mFrontEndDisplayInfos, |
| 120 | .globalShadowSettings = globalShadowSettings, |
| 121 | }; |
| 122 | LayerSnapshotBuilder builder(args); |
| 123 | } |
| 124 | |
| 125 | TEST_F(LayerSnapshotTest, updateSnapshot) { |
| 126 | LayerSnapshotBuilder::Args args{ |
| 127 | .root = mHierarchyBuilder.getHierarchy(), |
| 128 | .layerLifecycleManager = mLifecycleManager, |
| 129 | .includeMetadata = false, |
| 130 | .displays = mFrontEndDisplayInfos, |
| 131 | .globalShadowSettings = globalShadowSettings, |
| 132 | }; |
| 133 | |
| 134 | LayerSnapshotBuilder builder; |
| 135 | builder.update(args); |
| 136 | } |
| 137 | |
| 138 | // update using parent snapshot data |
| 139 | TEST_F(LayerSnapshotTest, croppedByParent) { |
| 140 | /// MAKE ALL LAYERS VISIBLE BY DEFAULT |
| 141 | DisplayInfo info; |
| 142 | info.info.logicalHeight = 100; |
| 143 | info.info.logicalWidth = 200; |
| 144 | mFrontEndDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info); |
| 145 | Rect layerCrop(0, 0, 10, 20); |
| 146 | setCrop(11, layerCrop); |
| 147 | EXPECT_TRUE(mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Geometry)); |
| 148 | UPDATE_AND_VERIFY_WITH_DISPLAY_CHANGES(mSnapshotBuilder, STARTING_ZORDER); |
| 149 | EXPECT_EQ(getSnapshot(11)->geomCrop, layerCrop); |
| 150 | EXPECT_EQ(getSnapshot(111)->geomLayerBounds, layerCrop.toFloatRect()); |
| 151 | float maxHeight = static_cast<float>(info.info.logicalHeight * 10); |
| 152 | float maxWidth = static_cast<float>(info.info.logicalWidth * 10); |
| 153 | |
| 154 | FloatRect maxDisplaySize(-maxWidth, -maxHeight, maxWidth, maxHeight); |
| 155 | EXPECT_EQ(getSnapshot(1)->geomLayerBounds, maxDisplaySize); |
| 156 | } |
| 157 | |
| 158 | // visibility tests |
| 159 | TEST_F(LayerSnapshotTest, newLayerHiddenByPolicy) { |
| 160 | createLayer(112, 11); |
| 161 | hideLayer(112); |
| 162 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 163 | |
| 164 | showLayer(112); |
| 165 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 111, 112, 12, 121, 122, 1221, 13, 2}); |
| 166 | } |
| 167 | |
| 168 | TEST_F(LayerSnapshotTest, hiddenByParent) { |
| 169 | hideLayer(11); |
| 170 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2}); |
| 171 | } |
| 172 | |
| 173 | TEST_F(LayerSnapshotTest, reparentShowsChild) { |
| 174 | hideLayer(11); |
| 175 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2}); |
| 176 | |
| 177 | showLayer(11); |
| 178 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 179 | } |
| 180 | |
| 181 | TEST_F(LayerSnapshotTest, reparentHidesChild) { |
| 182 | hideLayer(11); |
| 183 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2}); |
| 184 | |
| 185 | reparentLayer(121, 11); |
| 186 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 122, 1221, 13, 2}); |
| 187 | } |
| 188 | |
| 189 | TEST_F(LayerSnapshotTest, unHidingUpdatesSnapshot) { |
| 190 | hideLayer(11); |
| 191 | Rect crop(1, 2, 3, 4); |
| 192 | setCrop(111, crop); |
| 193 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2}); |
| 194 | |
| 195 | showLayer(11); |
| 196 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 197 | EXPECT_EQ(getSnapshot(111)->geomLayerBounds, crop.toFloatRect()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(LayerSnapshotTest, childBehindParentCanBeHiddenByParent) { |
| 201 | setZ(111, -1); |
| 202 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 111, 11, 12, 121, 122, 1221, 13, 2}); |
| 203 | |
| 204 | hideLayer(11); |
| 205 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2}); |
| 206 | } |
| 207 | |
| 208 | // relative tests |
| 209 | TEST_F(LayerSnapshotTest, RelativeParentCanHideChild) { |
| 210 | reparentRelativeLayer(13, 11); |
| 211 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 11, 13, 111, 12, 121, 122, 1221, 2}); |
| 212 | |
| 213 | hideLayer(11); |
| 214 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2}); |
| 215 | } |
| 216 | |
| 217 | TEST_F(LayerSnapshotTest, ReparentingToHiddenRelativeParentHidesChild) { |
| 218 | hideLayer(11); |
| 219 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 13, 2}); |
| 220 | reparentRelativeLayer(13, 11); |
| 221 | UPDATE_AND_VERIFY(mSnapshotBuilder, {1, 12, 121, 122, 1221, 2}); |
| 222 | } |
| 223 | |
| 224 | TEST_F(LayerSnapshotTest, AlphaInheritedByChildren) { |
| 225 | setAlpha(1, 0.5); |
| 226 | setAlpha(122, 0.5); |
| 227 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 228 | EXPECT_EQ(getSnapshot(12)->alpha, 0.5f); |
| 229 | EXPECT_EQ(getSnapshot(1221)->alpha, 0.25f); |
| 230 | } |
| 231 | |
| 232 | // Change states |
| 233 | TEST_F(LayerSnapshotTest, UpdateClearsPreviousChangeStates) { |
| 234 | setCrop(1, Rect(1, 2, 3, 4)); |
| 235 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 236 | EXPECT_TRUE(getSnapshot(1)->changes.get() != 0); |
| 237 | EXPECT_TRUE(getSnapshot(11)->changes.get() != 0); |
| 238 | setCrop(2, Rect(1, 2, 3, 4)); |
| 239 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 240 | EXPECT_TRUE(getSnapshot(2)->changes.get() != 0); |
| 241 | EXPECT_TRUE(getSnapshot(1)->changes.get() == 0); |
| 242 | EXPECT_TRUE(getSnapshot(11)->changes.get() == 0); |
| 243 | } |
| 244 | |
| 245 | TEST_F(LayerSnapshotTest, FastPathClearsPreviousChangeStates) { |
| 246 | setColor(11, {1._hf, 0._hf, 0._hf}); |
| 247 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 248 | EXPECT_TRUE(getSnapshot(11)->changes.get() != 0); |
| 249 | EXPECT_TRUE(getSnapshot(1)->changes.get() == 0); |
| 250 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 251 | EXPECT_TRUE(getSnapshot(11)->changes.get() == 0); |
| 252 | } |
| 253 | |
| 254 | TEST_F(LayerSnapshotTest, FastPathSetsChangeFlagToContent) { |
| 255 | setColor(1, {1._hf, 0._hf, 0._hf}); |
| 256 | UPDATE_AND_VERIFY(mSnapshotBuilder, STARTING_ZORDER); |
| 257 | EXPECT_EQ(getSnapshot(1)->changes, RequestedLayerState::Changes::Content); |
| 258 | } |
| 259 | |
| 260 | } // namespace android::surfaceflinger::frontend |