blob: bf89617635cc0f5bdd12ef654b151b9b422d3eab [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
2 * Copyright (C) 2016 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 "benchmark/benchmark.h"
18
19#include "androidfw/ApkAssets.h"
20#include "androidfw/AssetManager.h"
21#include "androidfw/AssetManager2.h"
22#include "androidfw/ResourceTypes.h"
23
24namespace android {
25
26constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk";
27constexpr const static uint32_t kStyleId = 0x01030237u; // android:style/Theme.Material.Light
28constexpr const static uint32_t kAttrId = 0x01010030u; // android:attr/colorForeground
29
Yurii Zubrytskyi72464742024-06-24 19:16:52 -070030constexpr const static uint32_t kStyle2Id = 0x01030224u; // android:style/Theme.Material
31constexpr const static uint32_t kStyle3Id = 0x0103024du; // android:style/Widget.Material
32constexpr const static uint32_t kStyle4Id = 0x0103028eu; // android:style/Widget.Material.Light
33
Adam Lesinski7ad11102016-10-28 16:39:15 -070034static void BM_ThemeApplyStyleFramework(benchmark::State& state) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070035 auto apk = ApkAssets::Load(kFrameworkPath);
Adam Lesinski7ad11102016-10-28 16:39:15 -070036 if (apk == nullptr) {
37 state.SkipWithError("Failed to load assets");
38 return;
39 }
40
41 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070042 assets.SetApkAssets({apk});
Adam Lesinski7ad11102016-10-28 16:39:15 -070043
44 while (state.KeepRunning()) {
45 auto theme = assets.NewTheme();
46 theme->ApplyStyle(kStyleId, false /* force */);
47 }
48}
49BENCHMARK(BM_ThemeApplyStyleFramework);
50
51static void BM_ThemeApplyStyleFrameworkOld(benchmark::State& state) {
52 AssetManager assets;
53 if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /* cookie */, false /* appAsLib */,
54 true /* isSystemAsset */)) {
55 state.SkipWithError("Failed to load assets");
56 return;
57 }
58
59 const ResTable& res_table = assets.getResources(true);
60
61 while (state.KeepRunning()) {
62 std::unique_ptr<ResTable::Theme> theme{new ResTable::Theme(res_table)};
63 theme->applyStyle(kStyleId, false /* force */);
64 }
65}
66BENCHMARK(BM_ThemeApplyStyleFrameworkOld);
67
Yurii Zubrytskyi72464742024-06-24 19:16:52 -070068static void BM_ThemeRebaseFramework(benchmark::State& state) {
69 auto apk = ApkAssets::Load(kFrameworkPath);
70 if (apk == nullptr) {
71 state.SkipWithError("Failed to load assets");
72 return;
73 }
74
75 AssetManager2 assets;
76 assets.SetApkAssets({apk});
77
78 // Create two arrays of styles to switch between back and forth.
79 const uint32_t styles1[] = {kStyle2Id, kStyleId, kStyle3Id};
80 const uint8_t force1[std::size(styles1)] = {false, true, false};
81 const uint32_t styles2[] = {kStyleId, kStyle2Id, kStyle4Id, kStyle3Id};
82 const uint8_t force2[std::size(styles2)] = {false, true, true, false};
83 const auto theme = assets.NewTheme();
84 // Initialize the theme to make the first iteration the same as the rest.
85 theme->Rebase(&assets, styles1, force1, std::size(force1));
86
87 while (state.KeepRunning()) {
88 theme->Rebase(&assets, styles2, force2, std::size(force2));
89 theme->Rebase(&assets, styles1, force1, std::size(force1));
90 }
91}
92BENCHMARK(BM_ThemeRebaseFramework);
93
Adam Lesinski7ad11102016-10-28 16:39:15 -070094static void BM_ThemeGetAttribute(benchmark::State& state) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070095 auto apk = ApkAssets::Load(kFrameworkPath);
Adam Lesinski7ad11102016-10-28 16:39:15 -070096
97 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070098 assets.SetApkAssets({apk});
Adam Lesinski7ad11102016-10-28 16:39:15 -070099
100 auto theme = assets.NewTheme();
101 theme->ApplyStyle(kStyleId, false /* force */);
102
Adam Lesinski7ad11102016-10-28 16:39:15 -0700103 while (state.KeepRunning()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000104 theme->GetAttribute(kAttrId);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700105 }
106}
107BENCHMARK(BM_ThemeGetAttribute);
108
109static void BM_ThemeGetAttributeOld(benchmark::State& state) {
110 AssetManager assets;
111 assets.addAssetPath(String8(kFrameworkPath), nullptr /* cookie */, false /* appAsLib */,
112 true /* isSystemAsset */);
113 const ResTable& res_table = assets.getResources(true);
114 std::unique_ptr<ResTable::Theme> theme{new ResTable::Theme(res_table)};
115 theme->applyStyle(kStyleId, false /* force */);
116
117 Res_value value;
118 uint32_t flags;
119
120 while (state.KeepRunning()) {
121 theme->getAttribute(kAttrId, &value, &flags);
122 }
123}
124BENCHMARK(BM_ThemeGetAttributeOld);
125
126} // namespace android