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<AssetManager2::ApkAssetsPtr> apk_assets;
57   for (const std::string& path : paths) {
58     auto apk = ApkAssets::Load(path);
59     if (apk == nullptr) {
60       state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
61       return;
62     }
63     apk_assets.push_back(std::move(apk));
64   }
65 
66   AssetManager2 assetmanager;
67   assetmanager.SetApkAssets(apk_assets);
68   if (config != nullptr) {
69     assetmanager.SetConfigurations({*config});
70   }
71 
72   while (state.KeepRunning()) {
73     auto value = assetmanager.GetResource(resid);
74     assetmanager.ResolveReference(*value);
75   }
76 }
77 
78 }  // namespace android
79