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
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
30static void BM_ThemeApplyStyleFramework(benchmark::State& state) {
31  std::unique_ptr<const ApkAssets> 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.get()});
39
40  while (state.KeepRunning()) {
41    auto theme = assets.NewTheme();
42    theme->ApplyStyle(kStyleId, false /* force */);
43  }
44}
45BENCHMARK(BM_ThemeApplyStyleFramework);
46
47static 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}
62BENCHMARK(BM_ThemeApplyStyleFrameworkOld);
63
64static void BM_ThemeGetAttribute(benchmark::State& state) {
65  std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
66
67  AssetManager2 assets;
68  assets.SetApkAssets({apk.get()});
69
70  auto theme = assets.NewTheme();
71  theme->ApplyStyle(kStyleId, false /* force */);
72
73  Res_value value;
74  uint32_t flags;
75
76  while (state.KeepRunning()) {
77    theme->GetAttribute(kAttrId, &value, &flags);
78  }
79}
80BENCHMARK(BM_ThemeGetAttribute);
81
82static void BM_ThemeGetAttributeOld(benchmark::State& state) {
83  AssetManager assets;
84  assets.addAssetPath(String8(kFrameworkPath), nullptr /* cookie */, false /* appAsLib */,
85                      true /* isSystemAsset */);
86  const ResTable& res_table = assets.getResources(true);
87  std::unique_ptr<ResTable::Theme> theme{new ResTable::Theme(res_table)};
88  theme->applyStyle(kStyleId, false /* force */);
89
90  Res_value value;
91  uint32_t flags;
92
93  while (state.KeepRunning()) {
94    theme->getAttribute(kAttrId, &value, &flags);
95  }
96}
97BENCHMARK(BM_ThemeGetAttributeOld);
98
99}  // namespace android
100