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<TransactionState> 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<TransactionState> 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 
93 } // namespace
94 } // namespace android::surfaceflinger
95