Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | namespace android { |
| 25 | |
| 26 | constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk"; |
| 27 | constexpr const static uint32_t kStyleId = 0x01030237u; // android:style/Theme.Material.Light |
| 28 | constexpr const static uint32_t kAttrId = 0x01010030u; // android:attr/colorForeground |
| 29 | |
Yurii Zubrytskyi | 7246474 | 2024-06-24 19:16:52 -0700 | [diff] [blame] | 30 | constexpr const static uint32_t kStyle2Id = 0x01030224u; // android:style/Theme.Material |
| 31 | constexpr const static uint32_t kStyle3Id = 0x0103024du; // android:style/Widget.Material |
| 32 | constexpr const static uint32_t kStyle4Id = 0x0103028eu; // android:style/Widget.Material.Light |
| 33 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 34 | static void BM_ThemeApplyStyleFramework(benchmark::State& state) { |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 35 | auto apk = ApkAssets::Load(kFrameworkPath); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 36 | if (apk == nullptr) { |
| 37 | state.SkipWithError("Failed to load assets"); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | AssetManager2 assets; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 42 | assets.SetApkAssets({apk}); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 43 | |
| 44 | while (state.KeepRunning()) { |
| 45 | auto theme = assets.NewTheme(); |
| 46 | theme->ApplyStyle(kStyleId, false /* force */); |
| 47 | } |
| 48 | } |
| 49 | BENCHMARK(BM_ThemeApplyStyleFramework); |
| 50 | |
| 51 | static 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 | } |
| 66 | BENCHMARK(BM_ThemeApplyStyleFrameworkOld); |
| 67 | |
Yurii Zubrytskyi | 7246474 | 2024-06-24 19:16:52 -0700 | [diff] [blame] | 68 | static 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 | } |
| 92 | BENCHMARK(BM_ThemeRebaseFramework); |
| 93 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 94 | static void BM_ThemeGetAttribute(benchmark::State& state) { |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 95 | auto apk = ApkAssets::Load(kFrameworkPath); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 96 | |
| 97 | AssetManager2 assets; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 98 | assets.SetApkAssets({apk}); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 99 | |
| 100 | auto theme = assets.NewTheme(); |
| 101 | theme->ApplyStyle(kStyleId, false /* force */); |
| 102 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 103 | while (state.KeepRunning()) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 104 | theme->GetAttribute(kAttrId); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | BENCHMARK(BM_ThemeGetAttribute); |
| 108 | |
| 109 | static 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 | } |
| 124 | BENCHMARK(BM_ThemeGetAttributeOld); |
| 125 | |
| 126 | } // namespace android |