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 
BM_ThemeApplyStyleFramework(benchmark::State & state)30 static void BM_ThemeApplyStyleFramework(benchmark::State& state) {
31   auto apk = ApkAssets::Load(kFrameworkPath);
32   if (apk == nullptr) {
33     state.SkipWithError("Failed to load assets");
34     return;
35   }
36 
37   AssetManager2 assets;
38   assets.SetApkAssets({apk});
39 
40   while (state.KeepRunning()) {
41     auto theme = assets.NewTheme();
42     theme->ApplyStyle(kStyleId, false /* force */);
43   }
44 }
45 BENCHMARK(BM_ThemeApplyStyleFramework);
46 
BM_ThemeApplyStyleFrameworkOld(benchmark::State & state)47 static void BM_ThemeApplyStyleFrameworkOld(benchmark::State& state) {
48   AssetManager assets;
49   if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /* cookie */, false /* appAsLib */,
50                            true /* isSystemAsset */)) {
51     state.SkipWithError("Failed to load assets");
52     return;
53   }
54 
55   const ResTable& res_table = assets.getResources(true);
56 
57   while (state.KeepRunning()) {
58     std::unique_ptr<ResTable::Theme> theme{new ResTable::Theme(res_table)};
59     theme->applyStyle(kStyleId, false /* force */);
60   }
61 }
62 BENCHMARK(BM_ThemeApplyStyleFrameworkOld);
63 
BM_ThemeGetAttribute(benchmark::State & state)64 static void BM_ThemeGetAttribute(benchmark::State& state) {
65   auto apk = ApkAssets::Load(kFrameworkPath);
66 
67   AssetManager2 assets;
68   assets.SetApkAssets({apk});
69 
70   auto theme = assets.NewTheme();
71   theme->ApplyStyle(kStyleId, false /* force */);
72 
73   while (state.KeepRunning()) {
74     theme->GetAttribute(kAttrId);
75   }
76 }
77 BENCHMARK(BM_ThemeGetAttribute);
78 
BM_ThemeGetAttributeOld(benchmark::State & state)79 static void BM_ThemeGetAttributeOld(benchmark::State& state) {
80   AssetManager assets;
81   assets.addAssetPath(String8(kFrameworkPath), nullptr /* cookie */, false /* appAsLib */,
82                       true /* isSystemAsset */);
83   const ResTable& res_table = assets.getResources(true);
84   std::unique_ptr<ResTable::Theme> theme{new ResTable::Theme(res_table)};
85   theme->applyStyle(kStyleId, false /* force */);
86 
87   Res_value value;
88   uint32_t flags;
89 
90   while (state.KeepRunning()) {
91     theme->getAttribute(kAttrId, &value, &flags);
92   }
93 }
94 BENCHMARK(BM_ThemeGetAttributeOld);
95 
96 }  // namespace android
97