• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 <memory>
18 #include <optional>
19 
20 #include <benchmark/benchmark.h>
21 
22 #include <Client.h> // temporarily needed for LayerCreationArgs
23 #include <FrontEnd/LayerCreationArgs.h>
24 #include <FrontEnd/LayerLifecycleManager.h>
25 #include <LayerLifecycleManagerHelper.h>
26 
27 namespace android::surfaceflinger {
28 
29 namespace {
30 
31 using namespace android::surfaceflinger::frontend;
32 
addRemoveLayers(benchmark::State & state)33 static void addRemoveLayers(benchmark::State& state) {
34     LayerLifecycleManager lifecycleManager;
35     for (auto _ : state) {
36         std::vector<std::unique_ptr<RequestedLayerState>> layers;
37         layers.emplace_back(LayerLifecycleManagerHelper::rootLayer(1));
38         layers.emplace_back(LayerLifecycleManagerHelper::rootLayer(2));
39         layers.emplace_back(LayerLifecycleManagerHelper::rootLayer(3));
40         lifecycleManager.addLayers(std::move(layers));
41         lifecycleManager.onHandlesDestroyed({{1, "1"}, {2, "2"}, {3, "3"}});
42         lifecycleManager.commitChanges();
43     }
44 }
45 BENCHMARK(addRemoveLayers);
46 
updateClientStates(benchmark::State & state)47 static void updateClientStates(benchmark::State& state) {
48     LayerLifecycleManager lifecycleManager;
49     std::vector<std::unique_ptr<RequestedLayerState>> layers;
50     layers.emplace_back(LayerLifecycleManagerHelper::rootLayer(1));
51     lifecycleManager.addLayers(std::move(layers));
52     lifecycleManager.commitChanges();
53     std::vector<QueuedTransactionState> transactions;
54     transactions.emplace_back();
55     transactions.back().states.push_back({});
56     auto& transactionState = transactions.back().states.front();
57     transactionState.state.what = layer_state_t::eColorChanged;
58     transactionState.state.color.rgb = {0.f, 0.f, 0.f};
59     transactionState.layerId = 1;
60     lifecycleManager.applyTransactions(transactions);
61     lifecycleManager.commitChanges();
62     int i = 0;
63     for (auto s : state) {
64         if (i++ % 100 == 0) i = 0;
65         transactionState.state.color.b = static_cast<float>(i / 100.f);
66         lifecycleManager.applyTransactions(transactions);
67         lifecycleManager.commitChanges();
68     }
69 }
70 BENCHMARK(updateClientStates);
71 
updateClientStatesNoChanges(benchmark::State & state)72 static void updateClientStatesNoChanges(benchmark::State& state) {
73     LayerLifecycleManager lifecycleManager;
74     std::vector<std::unique_ptr<RequestedLayerState>> layers;
75     layers.emplace_back(LayerLifecycleManagerHelper::rootLayer(1));
76     lifecycleManager.addLayers(std::move(layers));
77     std::vector<QueuedTransactionState> transactions;
78     transactions.emplace_back();
79     transactions.back().states.push_back({});
80     auto& transactionState = transactions.back().states.front();
81     transactionState.state.what = layer_state_t::eColorChanged;
82     transactionState.state.color.rgb = {0.f, 0.f, 0.f};
83     transactionState.layerId = 1;
84     lifecycleManager.applyTransactions(transactions);
85     lifecycleManager.commitChanges();
86     for (auto _ : state) {
87         lifecycleManager.applyTransactions(transactions);
88         lifecycleManager.commitChanges();
89     }
90 }
91 BENCHMARK(updateClientStatesNoChanges);
92 
propagateManyHiddenChildren(benchmark::State & state)93 static void propagateManyHiddenChildren(benchmark::State& state) {
94     LayerLifecycleManager lifecycleManager;
95     LayerLifecycleManagerHelper helper(lifecycleManager);
96 
97     helper.createRootLayer(0);
98     for (uint32_t i = 1; i < 50; ++i) {
99         helper.createLayer(i, i - 1);
100     }
101 
102     helper.hideLayer(0);
103 
104     LayerHierarchyBuilder hierarchyBuilder;
105     DisplayInfo info;
106     info.info.logicalHeight = 100;
107     info.info.logicalWidth = 100;
108     DisplayInfos displayInfos;
109     displayInfos.emplace_or_replace(ui::LayerStack::fromValue(1), info);
110     ShadowSettings globalShadowSettings;
111 
112     LayerSnapshotBuilder snapshotBuilder;
113 
114     int i = 1;
115     for (auto _ : state) {
116         i++;
117         helper.setAlpha(0, (1 + (i % 255)) / 255.0f);
118 
119         if (lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)) {
120             hierarchyBuilder.update(lifecycleManager);
121         }
122         LayerSnapshotBuilder::Args args{.root = hierarchyBuilder.getHierarchy(),
123                                         .layerLifecycleManager = lifecycleManager,
124                                         .displays = displayInfos,
125                                         .globalShadowSettings = globalShadowSettings,
126                                         .supportedLayerGenericMetadata = {},
127                                         .genericLayerMetadataKeyMap = {}};
128         snapshotBuilder.update(args);
129         lifecycleManager.commitChanges();
130     }
131 }
132 BENCHMARK(propagateManyHiddenChildren);
133 
134 } // namespace
135 } // namespace android::surfaceflinger
136