Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +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 | |
Vishnu Nair | 2662ebb | 2023-08-31 23:20:46 +0000 | [diff] [blame] | 20 | #include <renderengine/mock/FakeExternalTexture.h> |
| 21 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 22 | #include "FrontEnd/LayerLifecycleManager.h" |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 23 | #include "LayerHierarchyTest.h" |
| 24 | #include "TransactionState.h" |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace android::surfaceflinger; |
| 27 | |
| 28 | namespace android::surfaceflinger::frontend { |
| 29 | |
Vishnu Nair | 2662ebb | 2023-08-31 23:20:46 +0000 | [diff] [blame] | 30 | using namespace ftl::flag_operators; |
| 31 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 32 | // To run test: |
| 33 | /** |
| 34 | mp :libsurfaceflinger_unittest && adb sync; adb shell \ |
| 35 | /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \ |
| 36 | --gtest_filter="LayerLifecycleManagerTest.*" --gtest_repeat=100 \ |
| 37 | --gtest_shuffle \ |
| 38 | --gtest_brief=1 |
| 39 | */ |
| 40 | class ExpectLayerLifecycleListener : public LayerLifecycleManager::ILifecycleListener { |
| 41 | public: |
| 42 | void onLayerAdded(const RequestedLayerState& layer) override { |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 43 | mActualLayersAdded.push_back(layer.id); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 44 | }; |
| 45 | void onLayerDestroyed(const RequestedLayerState& layer) override { |
| 46 | mActualLayersDestroyed.emplace(layer.id); |
| 47 | }; |
| 48 | |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 49 | void expectLayersAdded(const std::vector<uint32_t>& expectedLayersAdded) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 50 | EXPECT_EQ(expectedLayersAdded, mActualLayersAdded); |
| 51 | mActualLayersAdded.clear(); |
| 52 | } |
| 53 | void expectLayersDestroyed(const std::unordered_set<uint32_t>& expectedLayersDestroyed) { |
| 54 | EXPECT_EQ(expectedLayersDestroyed, mActualLayersDestroyed); |
| 55 | mActualLayersDestroyed.clear(); |
| 56 | } |
| 57 | |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 58 | std::vector<uint32_t> mActualLayersAdded; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 59 | std::unordered_set<uint32_t> mActualLayersDestroyed; |
| 60 | }; |
| 61 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 62 | class LayerLifecycleManagerTest : public LayerHierarchyTestBase { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 63 | protected: |
| 64 | std::unique_ptr<RequestedLayerState> rootLayer(uint32_t id) { |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 65 | return std::make_unique<RequestedLayerState>(createArgs(/*id=*/id, /*canBeRoot=*/true, |
| 66 | /*parent=*/UNASSIGNED_LAYER_ID, |
| 67 | /*mirror=*/UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | std::unique_ptr<RequestedLayerState> childLayer(uint32_t id, uint32_t parentId) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 71 | return std::make_unique<RequestedLayerState>(createArgs(/*id=*/id, /*canBeRoot=*/false, |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 72 | parentId, |
| 73 | /*mirror=*/UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | RequestedLayerState* getRequestedLayerState(LayerLifecycleManager& lifecycleManager, |
| 77 | uint32_t layerId) { |
| 78 | return lifecycleManager.getLayerFromId(layerId); |
| 79 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | TEST_F(LayerLifecycleManagerTest, addLayers) { |
| 83 | LayerLifecycleManager lifecycleManager; |
| 84 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 85 | lifecycleManager.addLifecycleListener(listener); |
| 86 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 87 | layers.emplace_back(rootLayer(1)); |
| 88 | layers.emplace_back(rootLayer(2)); |
| 89 | layers.emplace_back(rootLayer(3)); |
| 90 | lifecycleManager.addLayers(std::move(layers)); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 91 | lifecycleManager.onHandlesDestroyed({{1, "1"}, {2, "2"}, {3, "3"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 92 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 93 | lifecycleManager.commitChanges(); |
| 94 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 95 | listener->expectLayersAdded({1, 2, 3}); |
| 96 | listener->expectLayersDestroyed({1, 2, 3}); |
| 97 | } |
| 98 | |
| 99 | TEST_F(LayerLifecycleManagerTest, updateLayerStates) { |
| 100 | LayerLifecycleManager lifecycleManager; |
| 101 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 102 | layers.emplace_back(rootLayer(1)); |
| 103 | lifecycleManager.addLayers(std::move(layers)); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 104 | lifecycleManager.applyTransactions(setZTransaction(1, 2)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 105 | |
| 106 | auto& managedLayers = lifecycleManager.getLayers(); |
| 107 | ASSERT_EQ(managedLayers.size(), 1u); |
| 108 | |
| 109 | EXPECT_EQ(managedLayers.front()->z, 2); |
| 110 | EXPECT_TRUE(managedLayers.front()->changes.test(RequestedLayerState::Changes::Z)); |
| 111 | |
| 112 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 113 | lifecycleManager.commitChanges(); |
| 114 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 115 | ASSERT_EQ(managedLayers.size(), 1u); |
| 116 | EXPECT_FALSE(managedLayers.front()->changes.test(RequestedLayerState::Changes::Z)); |
| 117 | |
| 118 | // apply transactions that do not affect the hierarchy |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 119 | std::vector<TransactionState> transactions; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 120 | transactions.emplace_back(); |
| 121 | transactions.back().states.push_back({}); |
| 122 | transactions.back().states.front().state.backgroundBlurRadius = 22; |
| 123 | transactions.back().states.front().state.what = layer_state_t::eBackgroundBlurRadiusChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 124 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 125 | lifecycleManager.applyTransactions(transactions); |
| 126 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 127 | lifecycleManager.commitChanges(); |
| 128 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 129 | EXPECT_EQ(managedLayers.front()->backgroundBlurRadius, 22u); |
| 130 | } |
| 131 | |
| 132 | TEST_F(LayerLifecycleManagerTest, layerWithoutHandleIsDestroyed) { |
| 133 | LayerLifecycleManager lifecycleManager; |
| 134 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 135 | lifecycleManager.addLifecycleListener(listener); |
| 136 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 137 | layers.emplace_back(rootLayer(1)); |
| 138 | layers.emplace_back(rootLayer(2)); |
| 139 | lifecycleManager.addLayers(std::move(layers)); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 140 | lifecycleManager.onHandlesDestroyed({{1, "1"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 141 | lifecycleManager.commitChanges(); |
| 142 | |
| 143 | SCOPED_TRACE("layerWithoutHandleIsDestroyed"); |
| 144 | listener->expectLayersAdded({1, 2}); |
| 145 | listener->expectLayersDestroyed({1}); |
| 146 | } |
| 147 | |
| 148 | TEST_F(LayerLifecycleManagerTest, rootLayerWithoutHandleIsDestroyed) { |
| 149 | LayerLifecycleManager lifecycleManager; |
| 150 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 151 | lifecycleManager.addLifecycleListener(listener); |
| 152 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 153 | layers.emplace_back(rootLayer(1)); |
| 154 | layers.emplace_back(rootLayer(2)); |
| 155 | lifecycleManager.addLayers(std::move(layers)); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 156 | lifecycleManager.onHandlesDestroyed({{1, "1"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 157 | lifecycleManager.commitChanges(); |
| 158 | listener->expectLayersAdded({1, 2}); |
| 159 | listener->expectLayersDestroyed({1}); |
| 160 | } |
| 161 | |
| 162 | TEST_F(LayerLifecycleManagerTest, offscreenLayerIsDestroyed) { |
| 163 | LayerLifecycleManager lifecycleManager; |
| 164 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 165 | lifecycleManager.addLifecycleListener(listener); |
| 166 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 167 | layers.emplace_back(rootLayer(1)); |
| 168 | layers.emplace_back(rootLayer(2)); |
| 169 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 170 | lifecycleManager.addLayers(std::move(layers)); |
| 171 | lifecycleManager.commitChanges(); |
| 172 | listener->expectLayersAdded({1, 2, 3}); |
| 173 | listener->expectLayersDestroyed({}); |
| 174 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 175 | lifecycleManager.applyTransactions(reparentLayerTransaction(3, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 176 | lifecycleManager.commitChanges(); |
| 177 | listener->expectLayersAdded({}); |
| 178 | listener->expectLayersDestroyed({}); |
| 179 | |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 180 | lifecycleManager.onHandlesDestroyed({{3, "3"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 181 | lifecycleManager.commitChanges(); |
| 182 | listener->expectLayersAdded({}); |
| 183 | listener->expectLayersDestroyed({3}); |
| 184 | } |
| 185 | |
| 186 | TEST_F(LayerLifecycleManagerTest, offscreenChildLayerWithHandleIsNotDestroyed) { |
| 187 | LayerLifecycleManager lifecycleManager; |
| 188 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 189 | lifecycleManager.addLifecycleListener(listener); |
| 190 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 191 | layers.emplace_back(rootLayer(1)); |
| 192 | layers.emplace_back(rootLayer(2)); |
| 193 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 194 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 195 | lifecycleManager.addLayers(std::move(layers)); |
| 196 | lifecycleManager.commitChanges(); |
| 197 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 198 | listener->expectLayersDestroyed({}); |
| 199 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 200 | lifecycleManager.applyTransactions(reparentLayerTransaction(3, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 201 | lifecycleManager.onHandlesDestroyed({{3, "3"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 202 | lifecycleManager.commitChanges(); |
| 203 | listener->expectLayersAdded({}); |
| 204 | listener->expectLayersDestroyed({3}); |
| 205 | } |
| 206 | |
| 207 | TEST_F(LayerLifecycleManagerTest, offscreenChildLayerWithoutHandleIsDestroyed) { |
| 208 | LayerLifecycleManager lifecycleManager; |
| 209 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 210 | lifecycleManager.addLifecycleListener(listener); |
| 211 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 212 | layers.emplace_back(rootLayer(1)); |
| 213 | layers.emplace_back(rootLayer(2)); |
| 214 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 215 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 216 | lifecycleManager.addLayers(std::move(layers)); |
| 217 | lifecycleManager.commitChanges(); |
| 218 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 219 | listener->expectLayersDestroyed({}); |
| 220 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 221 | lifecycleManager.applyTransactions(reparentLayerTransaction(3, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 222 | lifecycleManager.onHandlesDestroyed({{3, "3"}, {4, "4"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 223 | lifecycleManager.commitChanges(); |
| 224 | listener->expectLayersAdded({}); |
| 225 | listener->expectLayersDestroyed({3, 4}); |
| 226 | } |
| 227 | |
| 228 | TEST_F(LayerLifecycleManagerTest, reparentingDoesNotAffectRelativeZ) { |
| 229 | LayerLifecycleManager lifecycleManager; |
| 230 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 231 | lifecycleManager.addLifecycleListener(listener); |
| 232 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 233 | layers.emplace_back(rootLayer(1)); |
| 234 | layers.emplace_back(rootLayer(2)); |
| 235 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 236 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 237 | |
| 238 | lifecycleManager.addLayers(std::move(layers)); |
| 239 | lifecycleManager.commitChanges(); |
| 240 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 241 | listener->expectLayersDestroyed({}); |
| 242 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 243 | lifecycleManager.applyTransactions(relativeLayerTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 244 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 245 | lifecycleManager.applyTransactions(reparentLayerTransaction(4, 2)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 246 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
| 247 | |
| 248 | lifecycleManager.commitChanges(); |
| 249 | listener->expectLayersAdded({}); |
| 250 | listener->expectLayersDestroyed({}); |
| 251 | } |
| 252 | |
| 253 | TEST_F(LayerLifecycleManagerTest, reparentingToNullRemovesRelativeZ) { |
| 254 | LayerLifecycleManager lifecycleManager; |
| 255 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 256 | lifecycleManager.addLifecycleListener(listener); |
| 257 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 258 | layers.emplace_back(rootLayer(1)); |
| 259 | layers.emplace_back(rootLayer(2)); |
| 260 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 261 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 262 | |
| 263 | lifecycleManager.addLayers(std::move(layers)); |
| 264 | lifecycleManager.commitChanges(); |
| 265 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 266 | listener->expectLayersDestroyed({}); |
| 267 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 268 | lifecycleManager.applyTransactions(relativeLayerTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 269 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 270 | lifecycleManager.applyTransactions(reparentLayerTransaction(4, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 271 | EXPECT_FALSE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
| 272 | |
| 273 | lifecycleManager.commitChanges(); |
| 274 | listener->expectLayersAdded({}); |
| 275 | listener->expectLayersDestroyed({}); |
| 276 | } |
| 277 | |
| 278 | TEST_F(LayerLifecycleManagerTest, setZRemovesRelativeZ) { |
| 279 | LayerLifecycleManager lifecycleManager; |
| 280 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 281 | lifecycleManager.addLifecycleListener(listener); |
| 282 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 283 | layers.emplace_back(rootLayer(1)); |
| 284 | layers.emplace_back(rootLayer(2)); |
| 285 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 286 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 287 | |
| 288 | lifecycleManager.addLayers(std::move(layers)); |
| 289 | lifecycleManager.commitChanges(); |
| 290 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 291 | listener->expectLayersDestroyed({}); |
| 292 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 293 | lifecycleManager.applyTransactions(relativeLayerTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 294 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 295 | lifecycleManager.applyTransactions(setZTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 296 | EXPECT_FALSE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
| 297 | |
| 298 | lifecycleManager.commitChanges(); |
| 299 | listener->expectLayersAdded({}); |
| 300 | listener->expectLayersDestroyed({}); |
| 301 | } |
| 302 | |
| 303 | TEST_F(LayerLifecycleManagerTest, canAddBackgroundLayer) { |
| 304 | LayerLifecycleManager lifecycleManager; |
| 305 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 306 | lifecycleManager.addLifecycleListener(listener); |
| 307 | |
| 308 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 309 | layers.emplace_back(rootLayer(1)); |
| 310 | lifecycleManager.addLayers(std::move(layers)); |
| 311 | |
| 312 | std::vector<TransactionState> transactions; |
| 313 | transactions.emplace_back(); |
| 314 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 315 | transactions.back().states.front().state.bgColor.a = 0.5; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 316 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 317 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 318 | lifecycleManager.applyTransactions(transactions); |
| 319 | |
| 320 | auto& managedLayers = lifecycleManager.getLayers(); |
| 321 | ASSERT_EQ(managedLayers.size(), 2u); |
| 322 | |
| 323 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 324 | lifecycleManager.commitChanges(); |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 325 | ASSERT_EQ(listener->mActualLayersAdded.size(), 2u); |
| 326 | auto bgLayerId = listener->mActualLayersAdded[1]; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 327 | listener->expectLayersAdded({1, bgLayerId}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 328 | listener->expectLayersDestroyed({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 329 | EXPECT_EQ(getRequestedLayerState(lifecycleManager, bgLayerId)->color.a, 0.5_hf); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TEST_F(LayerLifecycleManagerTest, canDestroyBackgroundLayer) { |
| 333 | LayerLifecycleManager lifecycleManager; |
| 334 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 335 | lifecycleManager.addLifecycleListener(listener); |
| 336 | |
| 337 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 338 | layers.emplace_back(rootLayer(1)); |
| 339 | lifecycleManager.addLayers(std::move(layers)); |
| 340 | |
| 341 | std::vector<TransactionState> transactions; |
| 342 | transactions.emplace_back(); |
| 343 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 344 | transactions.back().states.front().state.bgColor.a = 0.5; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 345 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 346 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 347 | transactions.emplace_back(); |
| 348 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 349 | transactions.back().states.front().state.bgColor.a = 0; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 350 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 351 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 352 | |
| 353 | lifecycleManager.applyTransactions(transactions); |
| 354 | |
| 355 | ASSERT_EQ(lifecycleManager.getLayers().size(), 1u); |
| 356 | ASSERT_EQ(lifecycleManager.getDestroyedLayers().size(), 1u); |
| 357 | |
| 358 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 359 | lifecycleManager.commitChanges(); |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 360 | ASSERT_EQ(listener->mActualLayersAdded.size(), 2u); |
| 361 | auto bgLayerId = listener->mActualLayersAdded[1]; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 362 | listener->expectLayersAdded({1, bgLayerId}); |
| 363 | listener->expectLayersDestroyed({bgLayerId}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | TEST_F(LayerLifecycleManagerTest, onParentDestroyDestroysBackgroundLayer) { |
| 367 | LayerLifecycleManager lifecycleManager; |
| 368 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 369 | lifecycleManager.addLifecycleListener(listener); |
| 370 | |
| 371 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 372 | layers.emplace_back(rootLayer(1)); |
| 373 | lifecycleManager.addLayers(std::move(layers)); |
| 374 | |
| 375 | std::vector<TransactionState> transactions; |
| 376 | transactions.emplace_back(); |
| 377 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 378 | transactions.back().states.front().state.bgColor.a = 0.5; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 379 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 380 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 381 | transactions.emplace_back(); |
| 382 | lifecycleManager.applyTransactions(transactions); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 383 | lifecycleManager.onHandlesDestroyed({{1, "1"}}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 384 | |
| 385 | ASSERT_EQ(lifecycleManager.getLayers().size(), 0u); |
| 386 | ASSERT_EQ(lifecycleManager.getDestroyedLayers().size(), 2u); |
| 387 | |
| 388 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 389 | lifecycleManager.commitChanges(); |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 390 | ASSERT_EQ(listener->mActualLayersAdded.size(), 2u); |
| 391 | auto bgLayerId = listener->mActualLayersAdded[1]; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 392 | listener->expectLayersAdded({1, bgLayerId}); |
| 393 | listener->expectLayersDestroyed({1, bgLayerId}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 396 | TEST_F(LayerLifecycleManagerTest, blurSetsVisibilityChangeFlag) { |
| 397 | // clear default color on layer so we start with a layer that does not draw anything. |
| 398 | setColor(1, {-1.f, -1.f, -1.f}); |
| 399 | mLifecycleManager.commitChanges(); |
| 400 | |
| 401 | // layer has something to draw |
| 402 | setBackgroundBlurRadius(1, 2); |
| 403 | EXPECT_TRUE( |
| 404 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 405 | mLifecycleManager.commitChanges(); |
| 406 | |
| 407 | // layer still has something to draw, so visibility shouldn't change |
| 408 | setBackgroundBlurRadius(1, 3); |
| 409 | EXPECT_FALSE( |
| 410 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 411 | mLifecycleManager.commitChanges(); |
| 412 | |
| 413 | // layer has nothing to draw |
| 414 | setBackgroundBlurRadius(1, 0); |
| 415 | EXPECT_TRUE( |
| 416 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 417 | mLifecycleManager.commitChanges(); |
| 418 | } |
| 419 | |
| 420 | TEST_F(LayerLifecycleManagerTest, colorSetsVisibilityChangeFlag) { |
| 421 | // clear default color on layer so we start with a layer that does not draw anything. |
| 422 | setColor(1, {-1.f, -1.f, -1.f}); |
| 423 | mLifecycleManager.commitChanges(); |
| 424 | |
| 425 | // layer has something to draw |
| 426 | setColor(1, {2.f, 3.f, 4.f}); |
| 427 | EXPECT_TRUE( |
| 428 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 429 | mLifecycleManager.commitChanges(); |
| 430 | |
| 431 | // layer still has something to draw, so visibility shouldn't change |
| 432 | setColor(1, {0.f, 0.f, 0.f}); |
| 433 | EXPECT_FALSE( |
| 434 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 435 | mLifecycleManager.commitChanges(); |
| 436 | |
| 437 | // layer has nothing to draw |
| 438 | setColor(1, {-1.f, -1.f, -1.f}); |
| 439 | EXPECT_TRUE( |
| 440 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 441 | mLifecycleManager.commitChanges(); |
| 442 | } |
| 443 | |
Vishnu Nair | 2662ebb | 2023-08-31 23:20:46 +0000 | [diff] [blame] | 444 | TEST_F(LayerLifecycleManagerTest, layerOpacityChangesSetsVisibilityChangeFlag) { |
| 445 | // add a default buffer and make the layer opaque |
| 446 | setFlags(1, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque); |
| 447 | setBuffer(1, |
| 448 | std::make_shared< |
| 449 | renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 450 | 1ULL /* bufferId */, |
| 451 | HAL_PIXEL_FORMAT_RGBA_8888, |
| 452 | GRALLOC_USAGE_PROTECTED /*usage*/)); |
| 453 | |
| 454 | mLifecycleManager.commitChanges(); |
| 455 | |
| 456 | // set new buffer but layer opacity doesn't change |
| 457 | setBuffer(1, |
| 458 | std::make_shared< |
| 459 | renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 460 | 2ULL /* bufferId */, |
| 461 | HAL_PIXEL_FORMAT_RGBA_8888, |
| 462 | GRALLOC_USAGE_PROTECTED /*usage*/)); |
| 463 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().get(), |
| 464 | ftl::Flags<RequestedLayerState::Changes>(RequestedLayerState::Changes::Buffer | |
| 465 | RequestedLayerState::Changes::Content) |
| 466 | .get()); |
| 467 | mLifecycleManager.commitChanges(); |
| 468 | |
| 469 | // change layer flags and confirm visibility flag is set |
| 470 | setFlags(1, layer_state_t::eLayerOpaque, 0); |
| 471 | EXPECT_TRUE( |
| 472 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 473 | mLifecycleManager.commitChanges(); |
| 474 | } |
| 475 | |
| 476 | TEST_F(LayerLifecycleManagerTest, bufferFormatChangesSetsVisibilityChangeFlag) { |
| 477 | // add a default buffer and make the layer opaque |
| 478 | setFlags(1, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque); |
| 479 | setBuffer(1, |
| 480 | std::make_shared< |
| 481 | renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 482 | 1ULL /* bufferId */, |
| 483 | HAL_PIXEL_FORMAT_RGBA_8888, |
| 484 | GRALLOC_USAGE_PROTECTED /*usage*/)); |
| 485 | |
| 486 | mLifecycleManager.commitChanges(); |
| 487 | |
| 488 | // set new buffer with an opaque buffer format |
| 489 | setBuffer(1, |
| 490 | std::make_shared< |
| 491 | renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 492 | 2ULL /* bufferId */, |
| 493 | HAL_PIXEL_FORMAT_RGB_888, |
| 494 | GRALLOC_USAGE_PROTECTED /*usage*/)); |
| 495 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().get(), |
| 496 | ftl::Flags<RequestedLayerState::Changes>(RequestedLayerState::Changes::Buffer | |
| 497 | RequestedLayerState::Changes::Content | |
| 498 | RequestedLayerState::Changes::VisibleRegion | |
| 499 | RequestedLayerState::Changes::Visibility) |
| 500 | .get()); |
| 501 | mLifecycleManager.commitChanges(); |
| 502 | } |
| 503 | |
| 504 | TEST_F(LayerLifecycleManagerTest, roundedCornerChangesSetsVisibilityChangeFlag) { |
| 505 | // add a default buffer and make the layer opaque |
| 506 | setFlags(1, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque); |
| 507 | setBuffer(1, |
| 508 | std::make_shared< |
| 509 | renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 510 | 1ULL /* bufferId */, |
| 511 | HAL_PIXEL_FORMAT_RGBA_8888, |
| 512 | GRALLOC_USAGE_PROTECTED /*usage*/)); |
| 513 | |
| 514 | mLifecycleManager.commitChanges(); |
| 515 | |
| 516 | // add rounded corners which should make the layer translucent |
| 517 | setRoundedCorners(1, 5.f); |
| 518 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().get(), |
| 519 | ftl::Flags<RequestedLayerState::Changes>( |
| 520 | RequestedLayerState::Changes::AffectsChildren | |
| 521 | RequestedLayerState::Changes::Content | |
| 522 | RequestedLayerState::Changes::Geometry | |
| 523 | RequestedLayerState::Changes::VisibleRegion) |
| 524 | .get()); |
| 525 | mLifecycleManager.commitChanges(); |
| 526 | } |
| 527 | |
Vishnu Nair | 93e26b9 | 2023-10-16 21:36:51 -0700 | [diff] [blame] | 528 | // Even when it does not change visible region, we should mark alpha changes as affecting |
| 529 | // visible region because HWC impl depends on it. writeOutputIndependentGeometryStateToHWC |
| 530 | // is only called if we are updating geometry. |
| 531 | TEST_F(LayerLifecycleManagerTest, alphaChangesAlwaysSetsVisibleRegionFlag) { |
| 532 | mLifecycleManager.commitChanges(); |
| 533 | float startingAlpha = 0.5f; |
| 534 | setAlpha(1, startingAlpha); |
| 535 | |
| 536 | // this is expected because layer alpha changes from 1 to 0.5, it may no longer be opaque |
| 537 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().string(), |
| 538 | ftl::Flags<RequestedLayerState::Changes>( |
| 539 | RequestedLayerState::Changes::Content | |
| 540 | RequestedLayerState::Changes::AffectsChildren | |
| 541 | RequestedLayerState::Changes::VisibleRegion) |
| 542 | .string()); |
| 543 | EXPECT_EQ(mLifecycleManager.getChangedLayers()[0]->color.a, static_cast<half>(startingAlpha)); |
| 544 | mLifecycleManager.commitChanges(); |
| 545 | |
| 546 | float endingAlpha = 0.2f; |
| 547 | setAlpha(1, endingAlpha); |
| 548 | |
| 549 | // this is not expected but we should make sure this behavior does not change |
| 550 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().string(), |
| 551 | ftl::Flags<RequestedLayerState::Changes>( |
| 552 | RequestedLayerState::Changes::Content | |
| 553 | RequestedLayerState::Changes::AffectsChildren | |
| 554 | RequestedLayerState::Changes::VisibleRegion) |
| 555 | .string()); |
| 556 | EXPECT_EQ(mLifecycleManager.getChangedLayers()[0]->color.a, static_cast<half>(endingAlpha)); |
| 557 | mLifecycleManager.commitChanges(); |
| 558 | |
| 559 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().string(), |
| 560 | ftl::Flags<RequestedLayerState::Changes>().string()); |
| 561 | } |
| 562 | |
Patrick Williams | d8edec0 | 2024-03-11 18:14:48 -0500 | [diff] [blame^] | 563 | TEST_F(LayerLifecycleManagerTest, layerSecureChangesSetsVisibilityChangeFlag) { |
| 564 | // add a default buffer and make the layer secure |
| 565 | setFlags(1, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure); |
| 566 | setBuffer(1, |
| 567 | std::make_shared<renderengine::mock:: |
| 568 | FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 569 | 1ULL /* bufferId */, |
| 570 | HAL_PIXEL_FORMAT_RGBA_8888, |
| 571 | GRALLOC_USAGE_SW_READ_NEVER /*usage*/)); |
| 572 | |
| 573 | mLifecycleManager.commitChanges(); |
| 574 | |
| 575 | // set new buffer but layer secure doesn't change |
| 576 | setBuffer(1, |
| 577 | std::make_shared<renderengine::mock:: |
| 578 | FakeExternalTexture>(1U /*width*/, 1U /*height*/, |
| 579 | 2ULL /* bufferId */, |
| 580 | HAL_PIXEL_FORMAT_RGBA_8888, |
| 581 | GRALLOC_USAGE_SW_READ_NEVER /*usage*/)); |
| 582 | EXPECT_EQ(mLifecycleManager.getGlobalChanges().get(), |
| 583 | ftl::Flags<RequestedLayerState::Changes>(RequestedLayerState::Changes::Buffer | |
| 584 | RequestedLayerState::Changes::Content) |
| 585 | .get()); |
| 586 | mLifecycleManager.commitChanges(); |
| 587 | |
| 588 | // change layer flags and confirm visibility flag is set |
| 589 | setFlags(1, layer_state_t::eLayerSecure, 0); |
| 590 | EXPECT_TRUE( |
| 591 | mLifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Visibility)); |
| 592 | mLifecycleManager.commitChanges(); |
| 593 | } |
| 594 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 595 | } // namespace android::surfaceflinger::frontend |