blob: 7149beef797fbf15db1d3021335aee8ac6f69659 [file] [log] [blame]
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001/*
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"
Adam Lesinski73f6f9d2017-11-14 10:18:05 -080021#include "androidfw/AssetManager2.h"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080022
23namespace android {
24
25void 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 if (config != nullptr) {
37 assetmanager.setConfiguration(*config);
38 }
39
Adam Lesinski7fb38312018-01-23 03:17:26 -080040 const ResTable& table = assetmanager.getResources(true);
41
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080042 Res_value value;
43 ResTable_config selected_config;
44 uint32_t flags;
45
46 while (state.KeepRunning()) {
Adam Lesinskifae57eb2018-01-23 03:16:33 -080047 table.getResource(resid, &value, false /*may_be_bag*/, 0u /*density*/, &flags,
48 &selected_config);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080049 }
50}
51
Adam Lesinski73f6f9d2017-11-14 10:18:05 -080052void GetResourceBenchmark(const std::vector<std::string>& paths, const ResTable_config* config,
53 uint32_t resid, benchmark::State& state) {
54 std::vector<std::unique_ptr<const ApkAssets>> apk_assets;
55 std::vector<const ApkAssets*> apk_assets_ptrs;
56 for (const std::string& path : paths) {
57 std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
58 if (apk == nullptr) {
59 state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
60 return;
61 }
62 apk_assets_ptrs.push_back(apk.get());
63 apk_assets.push_back(std::move(apk));
64 }
65
66 AssetManager2 assetmanager;
67 assetmanager.SetApkAssets(apk_assets_ptrs);
68 if (config != nullptr) {
69 assetmanager.SetConfiguration(*config);
70 }
71
72 Res_value value;
73 ResTable_config selected_config;
74 uint32_t flags;
75
76 while (state.KeepRunning()) {
Adam Lesinskifae57eb2018-01-23 03:16:33 -080077 assetmanager.GetResource(resid, false /* may_be_bag */, 0u /* density_override */, &value,
78 &selected_config, &flags);
Adam Lesinski73f6f9d2017-11-14 10:18:05 -080079 }
80}
81
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080082} // namespace android