Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 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" |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 21 | #include "androidfw/AssetManager2.h" |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 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 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 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); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 38 | if (config != nullptr) { |
| 39 | assetmanager.setConfiguration(*config); |
| 40 | } |
| 41 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 42 | Res_value value; |
| 43 | ResTable_config selected_config; |
| 44 | uint32_t flags; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 45 | uint32_t last_ref = 0u; |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 46 | |
| 47 | while (state.KeepRunning()) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 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); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 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; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 77 | uint32_t last_id = 0u; |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 78 | |
| 79 | while (state.KeepRunning()) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 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); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 86 | } // namespace android |