blob: faddfe599af48374d9ea7d6508121877b84c852c [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
Adam Lesinskidcb3c652017-01-23 12:58:11 -080036 // Make sure to force creation of the ResTable first, or else the configuration doesn't get set.
37 const ResTable& table = assetmanager.getResources(true);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080038 if (config != nullptr) {
39 assetmanager.setConfiguration(*config);
40 }
41
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080042 Res_value value;
43 ResTable_config selected_config;
44 uint32_t flags;
Adam Lesinskid9c809c2017-12-28 13:01:35 -080045 uint32_t last_ref = 0u;
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080046
47 while (state.KeepRunning()) {
Adam Lesinskid9c809c2017-12-28 13:01:35 -080048 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);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080051 }
52}
53
Adam Lesinski73f6f9d2017-11-14 10:18:05 -080054void 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;
Adam Lesinskid9c809c2017-12-28 13:01:35 -080077 uint32_t last_id = 0u;
Adam Lesinski73f6f9d2017-11-14 10:18:05 -080078
79 while (state.KeepRunning()) {
Adam Lesinskid9c809c2017-12-28 13:01:35 -080080 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);
Adam Lesinski73f6f9d2017-11-14 10:18:05 -080083 }
84}
85
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080086} // namespace android