1 /*
2 * Copyright (C) 2017 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 "BenchmarkHelpers.h"
18
19 #include "android-base/stringprintf.h"
20 #include "androidfw/AssetManager.h"
21 #include "androidfw/AssetManager2.h"
22
23 namespace android {
24
GetResourceBenchmarkOld(const std::vector<std::string> & paths,const ResTable_config * config,uint32_t resid,benchmark::State & state)25 void GetResourceBenchmarkOld(const std::vector<std::string>& paths, const ResTable_config* config,
26 uint32_t resid, benchmark::State& state) {
27 AssetManager assetmanager;
28 for (const std::string& path : paths) {
29 if (!assetmanager.addAssetPath(String8(path.c_str()), nullptr /* cookie */,
30 false /* appAsLib */, false /* isSystemAssets */)) {
31 state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
32 return;
33 }
34 }
35
36 // Make sure to force creation of the ResTable first, or else the configuration doesn't get set.
37 const ResTable& table = assetmanager.getResources(true);
38 if (config != nullptr) {
39 assetmanager.setConfiguration(*config);
40 }
41
42 Res_value value;
43 ResTable_config selected_config;
44 uint32_t flags;
45 uint32_t last_ref = 0u;
46
47 while (state.KeepRunning()) {
48 ssize_t block = table.getResource(resid, &value, false /*may_be_bag*/, 0u /*density*/, &flags,
49 &selected_config);
50 table.resolveReference(&value, block, &last_ref, &flags, &selected_config);
51 }
52 }
53
GetResourceBenchmark(const std::vector<std::string> & paths,const ResTable_config * config,uint32_t resid,benchmark::State & state)54 void GetResourceBenchmark(const std::vector<std::string>& paths, const ResTable_config* config,
55 uint32_t resid, benchmark::State& state) {
56 std::vector<std::unique_ptr<const ApkAssets>> apk_assets;
57 std::vector<const ApkAssets*> apk_assets_ptrs;
58 for (const std::string& path : paths) {
59 std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
60 if (apk == nullptr) {
61 state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
62 return;
63 }
64 apk_assets_ptrs.push_back(apk.get());
65 apk_assets.push_back(std::move(apk));
66 }
67
68 AssetManager2 assetmanager;
69 assetmanager.SetApkAssets(apk_assets_ptrs);
70 if (config != nullptr) {
71 assetmanager.SetConfiguration(*config);
72 }
73
74 Res_value value;
75 ResTable_config selected_config;
76 uint32_t flags;
77 uint32_t last_id = 0u;
78
79 while (state.KeepRunning()) {
80 ApkAssetsCookie cookie = assetmanager.GetResource(
81 resid, false /* may_be_bag */, 0u /* density_override */, &value, &selected_config, &flags);
82 assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_id);
83 }
84 }
85
86 } // namespace android
87