Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
| 19 | #include "androidfw/AssetManager2.h" |
| 20 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 22 | #include <iterator> |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 23 | #include <map> |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 24 | #include <set> |
| 25 | #include <sstream> |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 26 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 27 | #include "android-base/logging.h" |
| 28 | #include "android-base/stringprintf.h" |
| 29 | #include "utils/ByteOrder.h" |
| 30 | #include "utils/Trace.h" |
| 31 | |
| 32 | #ifdef _WIN32 |
| 33 | #ifdef ERROR |
| 34 | #undef ERROR |
| 35 | #endif |
| 36 | #endif |
| 37 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 38 | #include "androidfw/ResourceUtils.h" |
| 39 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 42 | struct FindEntryResult { |
| 43 | // A pointer to the resource table entry for this resource. |
| 44 | // If the size of the entry is > sizeof(ResTable_entry), it can be cast to |
| 45 | // a ResTable_map_entry and processed as a bag/map. |
| 46 | const ResTable_entry* entry; |
| 47 | |
| 48 | // The configuration for which the resulting entry was defined. This is already swapped to host |
| 49 | // endianness. |
| 50 | ResTable_config config; |
| 51 | |
| 52 | // The bitmask of configuration axis with which the resource value varies. |
| 53 | uint32_t type_flags; |
| 54 | |
| 55 | // The dynamic package ID map for the package from which this resource came from. |
| 56 | const DynamicRefTable* dynamic_ref_table; |
| 57 | |
| 58 | // The string pool reference to the type's name. This uses a different string pool than |
| 59 | // the global string pool, but this is hidden from the caller. |
| 60 | StringPoolRef type_string_ref; |
| 61 | |
| 62 | // The string pool reference to the entry's name. This uses a different string pool than |
| 63 | // the global string pool, but this is hidden from the caller. |
| 64 | StringPoolRef entry_string_ref; |
| 65 | }; |
| 66 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 67 | AssetManager2::AssetManager2() { |
| 68 | memset(&configuration_, 0, sizeof(configuration_)); |
| 69 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 70 | |
| 71 | bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets, |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 72 | bool invalidate_caches, bool filter_incompatible_configs) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 73 | apk_assets_ = apk_assets; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 74 | BuildDynamicRefTable(); |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 75 | RebuildFilterList(filter_incompatible_configs); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 76 | if (invalidate_caches) { |
| 77 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 82 | void AssetManager2::BuildDynamicRefTable() { |
| 83 | package_groups_.clear(); |
| 84 | package_ids_.fill(0xff); |
| 85 | |
| 86 | // 0x01 is reserved for the android package. |
| 87 | int next_package_id = 0x02; |
| 88 | const size_t apk_assets_count = apk_assets_.size(); |
| 89 | for (size_t i = 0; i < apk_assets_count; i++) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 90 | const LoadedArsc* loaded_arsc = apk_assets_[i]->GetLoadedArsc(); |
| 91 | |
| 92 | for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 93 | // Get the package ID or assign one if a shared library. |
| 94 | int package_id; |
| 95 | if (package->IsDynamic()) { |
| 96 | package_id = next_package_id++; |
| 97 | } else { |
| 98 | package_id = package->GetPackageId(); |
| 99 | } |
| 100 | |
| 101 | // Add the mapping for package ID to index if not present. |
| 102 | uint8_t idx = package_ids_[package_id]; |
| 103 | if (idx == 0xff) { |
| 104 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
| 105 | package_groups_.push_back({}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 106 | DynamicRefTable& ref_table = package_groups_.back().dynamic_ref_table; |
| 107 | ref_table.mAssignedPackageId = package_id; |
| 108 | ref_table.mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 109 | } |
| 110 | PackageGroup* package_group = &package_groups_[idx]; |
| 111 | |
| 112 | // Add the package and to the set of packages with the same ID. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 113 | package_group->packages_.push_back(ConfiguredPackage{package.get(), {}}); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 114 | package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i)); |
| 115 | |
| 116 | // Add the package name -> build time ID mappings. |
| 117 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 118 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
| 119 | package_group->dynamic_ref_table.mEntries.replaceValueFor( |
| 120 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
| 126 | const auto package_groups_end = package_groups_.end(); |
| 127 | for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 128 | const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 129 | for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) { |
| 130 | iter2->dynamic_ref_table.addMapping(String16(package_name.c_str(), package_name.size()), |
| 131 | iter->dynamic_ref_table.mAssignedPackageId); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void AssetManager2::DumpToLog() const { |
| 137 | base::ScopedLogSeverity _log(base::INFO); |
| 138 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 139 | LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this); |
| 140 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 141 | std::string list; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 142 | for (const auto& apk_assets : apk_assets_) { |
| 143 | base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str()); |
| 144 | } |
| 145 | LOG(INFO) << "ApkAssets: " << list; |
| 146 | |
| 147 | list = ""; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 148 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 149 | if (package_ids_[i] != 0xff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 150 | base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | LOG(INFO) << "Package ID map: " << list; |
| 154 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 155 | for (const auto& package_group: package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 156 | list = ""; |
| 157 | for (const auto& package : package_group.packages_) { |
| 158 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 159 | base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(), |
| 160 | loaded_package->GetPackageId(), |
| 161 | (loaded_package->IsDynamic() ? " dynamic" : "")); |
| 162 | } |
| 163 | LOG(INFO) << base::StringPrintf("PG (%02x): ", |
| 164 | package_group.dynamic_ref_table.mAssignedPackageId) |
| 165 | << list; |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 166 | |
| 167 | for (size_t i = 0; i < 256; i++) { |
| 168 | if (package_group.dynamic_ref_table.mLookupTable[i] != 0) { |
| 169 | LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i, |
| 170 | package_group.dynamic_ref_table.mLookupTable[i]); |
| 171 | } |
| 172 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 173 | } |
| 174 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 175 | |
| 176 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 177 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 178 | return nullptr; |
| 179 | } |
| 180 | return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool(); |
| 181 | } |
| 182 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 183 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 184 | if (package_id >= package_ids_.size()) { |
| 185 | return nullptr; |
| 186 | } |
| 187 | |
| 188 | const size_t idx = package_ids_[package_id]; |
| 189 | if (idx == 0xff) { |
| 190 | return nullptr; |
| 191 | } |
| 192 | return &package_groups_[idx].dynamic_ref_table; |
| 193 | } |
| 194 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 195 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const { |
| 196 | for (const PackageGroup& package_group : package_groups_) { |
| 197 | for (const ApkAssetsCookie& package_cookie : package_group.cookies_) { |
| 198 | if (package_cookie == cookie) { |
| 199 | return &package_group.dynamic_ref_table; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return nullptr; |
| 204 | } |
| 205 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 206 | void AssetManager2::SetConfiguration(const ResTable_config& configuration) { |
| 207 | const int diff = configuration_.diff(configuration); |
| 208 | configuration_ = configuration; |
| 209 | |
| 210 | if (diff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 211 | RebuildFilterList(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 212 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 213 | } |
| 214 | } |
| 215 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 216 | std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 217 | bool exclude_mipmap) const { |
| 218 | ATRACE_NAME("AssetManager::GetResourceConfigurations"); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 219 | std::set<ResTable_config> configurations; |
| 220 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 221 | bool found_system_package = false; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 222 | for (const ConfiguredPackage& package : package_group.packages_) { |
| 223 | if (exclude_system && package.loaded_package_->IsSystem()) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 224 | found_system_package = true; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 225 | continue; |
| 226 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 227 | |
| 228 | if (exclude_system && package.loaded_package_->IsOverlay() && found_system_package) { |
| 229 | // Overlays must appear after the target package to take effect. Any overlay found in the |
| 230 | // same package as a system package is able to overlay system resources. |
| 231 | continue; |
| 232 | } |
| 233 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 234 | package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | return configurations; |
| 238 | } |
| 239 | |
| 240 | std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 241 | bool merge_equivalent_languages) const { |
| 242 | ATRACE_NAME("AssetManager::GetResourceLocales"); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 243 | std::set<std::string> locales; |
| 244 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 245 | bool found_system_package = false; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 246 | for (const ConfiguredPackage& package : package_group.packages_) { |
| 247 | if (exclude_system && package.loaded_package_->IsSystem()) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 248 | found_system_package = true; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 249 | continue; |
| 250 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 251 | |
| 252 | if (exclude_system && package.loaded_package_->IsOverlay() && found_system_package) { |
| 253 | // Overlays must appear after the target package to take effect. Any overlay found in the |
| 254 | // same package as a system package is able to overlay system resources. |
| 255 | continue; |
| 256 | } |
| 257 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 258 | package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | return locales; |
| 262 | } |
| 263 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 264 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, |
| 265 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 266 | const std::string new_path = "assets/" + filename; |
| 267 | return OpenNonAsset(new_path, mode); |
| 268 | } |
| 269 | |
| 270 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 271 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 272 | const std::string new_path = "assets/" + filename; |
| 273 | return OpenNonAsset(new_path, cookie, mode); |
| 274 | } |
| 275 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 276 | std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const { |
| 277 | ATRACE_NAME("AssetManager::OpenDir"); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 278 | |
| 279 | std::string full_path = "assets/" + dirname; |
| 280 | std::unique_ptr<SortedVector<AssetDir::FileInfo>> files = |
| 281 | util::make_unique<SortedVector<AssetDir::FileInfo>>(); |
| 282 | |
| 283 | // Start from the back. |
| 284 | for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) { |
| 285 | const ApkAssets* apk_assets = *iter; |
| 286 | |
| 287 | auto func = [&](const StringPiece& name, FileType type) { |
| 288 | AssetDir::FileInfo info; |
| 289 | info.setFileName(String8(name.data(), name.size())); |
| 290 | info.setFileType(type); |
| 291 | info.setSourceName(String8(apk_assets->GetPath().c_str())); |
| 292 | files->add(info); |
| 293 | }; |
| 294 | |
| 295 | if (!apk_assets->ForEachFile(full_path, func)) { |
| 296 | return {}; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>(); |
| 301 | asset_dir->setFileList(files.release()); |
| 302 | return asset_dir; |
| 303 | } |
| 304 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 305 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 306 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 307 | // is inconsistent for split APKs. |
| 308 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 309 | Asset::AccessMode mode, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 310 | ApkAssetsCookie* out_cookie) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 311 | for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) { |
| 312 | std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode); |
| 313 | if (asset) { |
| 314 | if (out_cookie != nullptr) { |
| 315 | *out_cookie = i; |
| 316 | } |
| 317 | return asset; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (out_cookie != nullptr) { |
| 322 | *out_cookie = kInvalidCookie; |
| 323 | } |
| 324 | return {}; |
| 325 | } |
| 326 | |
| 327 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 328 | ApkAssetsCookie cookie, |
| 329 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 330 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 331 | return {}; |
| 332 | } |
| 333 | return apk_assets_[cookie]->Open(filename, mode); |
| 334 | } |
| 335 | |
| 336 | ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 337 | bool /*stop_at_first_match*/, |
| 338 | FindEntryResult* out_entry) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 339 | // Might use this if density_override != 0. |
| 340 | ResTable_config density_override_config; |
| 341 | |
| 342 | // Select our configuration or generate a density override configuration. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 343 | const ResTable_config* desired_config = &configuration_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 344 | if (density_override != 0 && density_override != configuration_.density) { |
| 345 | density_override_config = configuration_; |
| 346 | density_override_config.density = density_override; |
| 347 | desired_config = &density_override_config; |
| 348 | } |
| 349 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 350 | if (!is_valid_resid(resid)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 351 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 352 | return kInvalidCookie; |
| 353 | } |
| 354 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 355 | const uint32_t package_id = get_package_id(resid); |
| 356 | const uint8_t type_idx = get_type_id(resid) - 1; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 357 | const uint16_t entry_idx = get_entry_id(resid); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 358 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 359 | const uint8_t package_idx = package_ids_[package_id]; |
| 360 | if (package_idx == 0xff) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 361 | LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", package_id, resid); |
| 362 | return kInvalidCookie; |
| 363 | } |
| 364 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 365 | const PackageGroup& package_group = package_groups_[package_idx]; |
Adam Lesinski | b8b3a26 | 2018-02-09 11:01:45 -0800 | [diff] [blame] | 366 | const size_t package_count = package_group.packages_.size(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 367 | |
| 368 | ApkAssetsCookie best_cookie = kInvalidCookie; |
| 369 | const LoadedPackage* best_package = nullptr; |
| 370 | const ResTable_type* best_type = nullptr; |
| 371 | const ResTable_config* best_config = nullptr; |
| 372 | ResTable_config best_config_copy; |
| 373 | uint32_t best_offset = 0u; |
| 374 | uint32_t type_flags = 0u; |
| 375 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 376 | Resolution::Step::Type resolution_type; |
| 377 | std::vector<Resolution::Step> resolution_steps; |
| 378 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 379 | // If desired_config is the same as the set configuration, then we can use our filtered list |
| 380 | // and we don't need to match the configurations, since they already matched. |
| 381 | const bool use_fast_path = desired_config == &configuration_; |
| 382 | |
| 383 | for (size_t pi = 0; pi < package_count; pi++) { |
| 384 | const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi]; |
| 385 | const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_; |
| 386 | ApkAssetsCookie cookie = package_group.cookies_[pi]; |
| 387 | |
| 388 | // If the type IDs are offset in this package, we need to take that into account when searching |
| 389 | // for a type. |
| 390 | const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx); |
| 391 | if (UNLIKELY(type_spec == nullptr)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 392 | continue; |
| 393 | } |
| 394 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 395 | uint16_t local_entry_idx = entry_idx; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 396 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 397 | // If there is an IDMAP supplied with this package, translate the entry ID. |
| 398 | if (type_spec->idmap_entries != nullptr) { |
| 399 | if (!LoadedIdmap::Lookup(type_spec->idmap_entries, local_entry_idx, &local_entry_idx)) { |
| 400 | // There is no mapping, so the resource is not meant to be in this overlay package. |
| 401 | continue; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | type_flags |= type_spec->GetFlagsForEntryIndex(local_entry_idx); |
| 406 | |
| 407 | // If the package is an overlay, then even configurations that are the same MUST be chosen. |
| 408 | const bool package_is_overlay = loaded_package->IsOverlay(); |
| 409 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 410 | if (use_fast_path) { |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 411 | const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 412 | const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations; |
| 413 | const size_t type_count = candidate_configs.size(); |
| 414 | for (uint32_t i = 0; i < type_count; i++) { |
| 415 | const ResTable_config& this_config = candidate_configs[i]; |
| 416 | |
| 417 | // We can skip calling ResTable_config::match() because we know that all candidate |
| 418 | // configurations that do NOT match have been filtered-out. |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 419 | if (best_config == nullptr) { |
| 420 | resolution_type = Resolution::Step::Type::INITIAL; |
| 421 | } else if (this_config.isBetterThan(*best_config, desired_config)) { |
| 422 | resolution_type = Resolution::Step::Type::BETTER_MATCH; |
| 423 | } else if (package_is_overlay && this_config.compare(*best_config) == 0) { |
| 424 | resolution_type = Resolution::Step::Type::OVERLAID; |
| 425 | } else { |
| 426 | continue; |
| 427 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 428 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 429 | // The configuration matches and is better than the previous selection. |
| 430 | // Find the entry value if it exists for this configuration. |
| 431 | const ResTable_type* type = filtered_group.types[i]; |
| 432 | const uint32_t offset = LoadedPackage::GetEntryOffset(type, local_entry_idx); |
| 433 | if (offset == ResTable_type::NO_ENTRY) { |
| 434 | continue; |
| 435 | } |
| 436 | |
| 437 | best_cookie = cookie; |
| 438 | best_package = loaded_package; |
| 439 | best_type = type; |
| 440 | best_config = &this_config; |
| 441 | best_offset = offset; |
| 442 | |
| 443 | if (resource_resolution_logging_enabled_) { |
| 444 | resolution_steps.push_back(Resolution::Step{resolution_type, |
| 445 | this_config.toString(), |
| 446 | &loaded_package->GetPackageName()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } else { |
| 450 | // This is the slower path, which doesn't use the filtered list of configurations. |
| 451 | // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness |
| 452 | // and fill in any new fields that did not exist when the APK was compiled. |
| 453 | // Furthermore when selecting configurations we can't just record the pointer to the |
| 454 | // ResTable_config, we must copy it. |
| 455 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 456 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 457 | ResTable_config this_config; |
| 458 | this_config.copyFromDtoH((*iter)->config); |
| 459 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 460 | if (!this_config.match(*desired_config)) { |
| 461 | continue; |
| 462 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 463 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 464 | if (best_config == nullptr) { |
| 465 | resolution_type = Resolution::Step::Type::INITIAL; |
| 466 | } else if (this_config.isBetterThan(*best_config, desired_config)) { |
| 467 | resolution_type = Resolution::Step::Type::BETTER_MATCH; |
| 468 | } else if (package_is_overlay && this_config.compare(*best_config) == 0) { |
| 469 | resolution_type = Resolution::Step::Type::OVERLAID; |
| 470 | } else { |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | // The configuration matches and is better than the previous selection. |
| 475 | // Find the entry value if it exists for this configuration. |
| 476 | const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, local_entry_idx); |
| 477 | if (offset == ResTable_type::NO_ENTRY) { |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | best_cookie = cookie; |
| 482 | best_package = loaded_package; |
| 483 | best_type = *iter; |
| 484 | best_config_copy = this_config; |
| 485 | best_config = &best_config_copy; |
| 486 | best_offset = offset; |
| 487 | |
| 488 | if (resource_resolution_logging_enabled_) { |
| 489 | resolution_steps.push_back(Resolution::Step{resolution_type, |
| 490 | this_config.toString(), |
| 491 | &loaded_package->GetPackageName()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 492 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 497 | if (UNLIKELY(best_cookie == kInvalidCookie)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 498 | return kInvalidCookie; |
| 499 | } |
| 500 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 501 | const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset); |
| 502 | if (UNLIKELY(best_entry == nullptr)) { |
| 503 | return kInvalidCookie; |
| 504 | } |
| 505 | |
| 506 | out_entry->entry = best_entry; |
| 507 | out_entry->config = *best_config; |
| 508 | out_entry->type_flags = type_flags; |
| 509 | out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1); |
| 510 | out_entry->entry_string_ref = |
| 511 | StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 512 | out_entry->dynamic_ref_table = &package_group.dynamic_ref_table; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 513 | |
| 514 | if (resource_resolution_logging_enabled_) { |
| 515 | last_resolution.resid = resid; |
| 516 | last_resolution.cookie = best_cookie; |
| 517 | last_resolution.steps = resolution_steps; |
| 518 | |
| 519 | // Cache only the type/entry refs since that's all that's needed to build name |
| 520 | last_resolution.type_string_ref = |
| 521 | StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1); |
| 522 | last_resolution.entry_string_ref = |
| 523 | StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index); |
| 524 | } |
| 525 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 526 | return best_cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 529 | void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) { |
| 530 | resource_resolution_logging_enabled_ = enabled; |
| 531 | |
| 532 | if (!enabled) { |
| 533 | last_resolution.cookie = kInvalidCookie; |
| 534 | last_resolution.resid = 0; |
| 535 | last_resolution.steps.clear(); |
| 536 | last_resolution.type_string_ref = StringPoolRef(); |
| 537 | last_resolution.entry_string_ref = StringPoolRef(); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | std::string AssetManager2::GetLastResourceResolution() const { |
| 542 | if (!resource_resolution_logging_enabled_) { |
| 543 | LOG(ERROR) << "Must enable resource resolution logging before getting path."; |
| 544 | return std::string(); |
| 545 | } |
| 546 | |
| 547 | auto cookie = last_resolution.cookie; |
| 548 | if (cookie == kInvalidCookie) { |
| 549 | LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path."; |
| 550 | return std::string(); |
| 551 | } |
| 552 | |
| 553 | uint32_t resid = last_resolution.resid; |
| 554 | std::vector<Resolution::Step>& steps = last_resolution.steps; |
| 555 | |
| 556 | ResourceName resource_name; |
| 557 | std::string resource_name_string; |
| 558 | |
| 559 | const LoadedPackage* package = |
| 560 | apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid)); |
| 561 | |
| 562 | if (package != nullptr) { |
| 563 | ToResourceName(last_resolution.type_string_ref, |
| 564 | last_resolution.entry_string_ref, |
Ryan Mitchell | 741e96f | 2019-01-23 16:56:51 -0800 | [diff] [blame] | 565 | package->GetPackageName(), |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 566 | &resource_name); |
| 567 | resource_name_string = ToFormattedResourceString(&resource_name); |
| 568 | } |
| 569 | |
| 570 | std::stringstream log_stream; |
| 571 | log_stream << base::StringPrintf("Resolution for 0x%08x ", resid) |
| 572 | << resource_name_string |
| 573 | << "\n\tFor config -" |
| 574 | << configuration_.toString(); |
| 575 | |
| 576 | std::string prefix; |
| 577 | for (Resolution::Step step : steps) { |
| 578 | switch (step.type) { |
| 579 | case Resolution::Step::Type::INITIAL: |
| 580 | prefix = "Found initial"; |
| 581 | break; |
| 582 | case Resolution::Step::Type::BETTER_MATCH: |
| 583 | prefix = "Found better"; |
| 584 | break; |
| 585 | case Resolution::Step::Type::OVERLAID: |
| 586 | prefix = "Overlaid"; |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | if (!prefix.empty()) { |
| 591 | log_stream << "\n\t" << prefix << ": " << *step.package_name; |
| 592 | |
| 593 | if (!step.config_name.isEmpty()) { |
| 594 | log_stream << " -" << step.config_name; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | return log_stream.str(); |
| 600 | } |
| 601 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 602 | bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 603 | FindEntryResult entry; |
| 604 | ApkAssetsCookie cookie = |
| 605 | FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 606 | if (cookie == kInvalidCookie) { |
| 607 | return false; |
| 608 | } |
| 609 | |
Ryan Mitchell | 741e96f | 2019-01-23 16:56:51 -0800 | [diff] [blame] | 610 | const uint8_t package_idx = package_ids_[get_package_id(resid)]; |
| 611 | if (package_idx == 0xff) { |
| 612 | LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", |
| 613 | get_package_id(resid), resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | |
Ryan Mitchell | 741e96f | 2019-01-23 16:56:51 -0800 | [diff] [blame] | 617 | const PackageGroup& package_group = package_groups_[package_idx]; |
| 618 | auto cookie_iter = std::find(package_group.cookies_.begin(), |
| 619 | package_group.cookies_.end(), cookie); |
| 620 | if (cookie_iter == package_group.cookies_.end()) { |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | long package_pos = std::distance(package_group.cookies_.begin(), cookie_iter); |
| 625 | const LoadedPackage* package = package_group.packages_[package_pos].loaded_package_; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 626 | return ToResourceName(entry.type_string_ref, |
| 627 | entry.entry_string_ref, |
Ryan Mitchell | 741e96f | 2019-01-23 16:56:51 -0800 | [diff] [blame] | 628 | package->GetPackageName(), |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 629 | out_name); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 632 | bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 633 | FindEntryResult entry; |
| 634 | ApkAssetsCookie cookie = |
| 635 | FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry); |
| 636 | if (cookie != kInvalidCookie) { |
| 637 | *out_flags = entry.type_flags; |
| 638 | return cookie; |
| 639 | } |
| 640 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag, |
| 644 | uint16_t density_override, Res_value* out_value, |
| 645 | ResTable_config* out_selected_config, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 646 | uint32_t* out_flags) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 647 | FindEntryResult entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 648 | ApkAssetsCookie cookie = |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 649 | FindEntry(resid, density_override, false /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 650 | if (cookie == kInvalidCookie) { |
| 651 | return kInvalidCookie; |
| 652 | } |
| 653 | |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 654 | if (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 655 | if (!may_be_bag) { |
| 656 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 657 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 658 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 659 | |
| 660 | // Create a reference since we can't represent this complex type as a Res_value. |
| 661 | out_value->dataType = Res_value::TYPE_REFERENCE; |
| 662 | out_value->data = resid; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 663 | *out_selected_config = entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 664 | *out_flags = entry.type_flags; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 665 | return cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | const Res_value* device_value = reinterpret_cast<const Res_value*>( |
| 669 | reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size)); |
| 670 | out_value->copyFrom_dtoh(*device_value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 671 | |
| 672 | // Convert the package ID to the runtime assigned package ID. |
| 673 | entry.dynamic_ref_table->lookupResourceValue(out_value); |
| 674 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 675 | *out_selected_config = entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 676 | *out_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 677 | return cookie; |
| 678 | } |
| 679 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 680 | ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 681 | ResTable_config* in_out_selected_config, |
| 682 | uint32_t* in_out_flags, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 683 | uint32_t* out_last_reference) const { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 684 | constexpr const int kMaxIterations = 20; |
| 685 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 686 | for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE && |
| 687 | in_out_value->data != 0u && iteration < kMaxIterations; |
| 688 | iteration++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 689 | *out_last_reference = in_out_value->data; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 690 | uint32_t new_flags = 0u; |
| 691 | cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/, |
| 692 | in_out_value, in_out_selected_config, &new_flags); |
| 693 | if (cookie == kInvalidCookie) { |
| 694 | return kInvalidCookie; |
| 695 | } |
| 696 | if (in_out_flags != nullptr) { |
| 697 | *in_out_flags |= new_flags; |
| 698 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 699 | if (*out_last_reference == in_out_value->data) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 700 | // This reference can't be resolved, so exit now and let the caller deal with it. |
| 701 | return cookie; |
| 702 | } |
| 703 | } |
| 704 | return cookie; |
| 705 | } |
| 706 | |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame^] | 707 | const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) { |
| 708 | auto cached_iter = cached_bag_resid_stacks_.find(resid); |
| 709 | if (cached_iter != cached_bag_resid_stacks_.end()) { |
| 710 | return cached_iter->second; |
| 711 | } else { |
| 712 | auto found_resids = std::vector<uint32_t>(); |
| 713 | GetBag(resid, found_resids); |
| 714 | // Cache style stacks if they are not already cached. |
| 715 | cached_bag_resid_stacks_[resid] = found_resids; |
| 716 | return found_resids; |
| 717 | } |
| 718 | } |
| 719 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 720 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid) { |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 721 | auto found_resids = std::vector<uint32_t>(); |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame^] | 722 | auto bag = GetBag(resid, found_resids); |
| 723 | |
| 724 | // Cache style stacks if they are not already cached. |
| 725 | auto cached_iter = cached_bag_resid_stacks_.find(resid); |
| 726 | if (cached_iter == cached_bag_resid_stacks_.end()) { |
| 727 | cached_bag_resid_stacks_[resid] = found_resids; |
| 728 | } |
| 729 | return bag; |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 733 | ATRACE_NAME("AssetManager::GetBag"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 734 | |
| 735 | auto cached_iter = cached_bags_.find(resid); |
| 736 | if (cached_iter != cached_bags_.end()) { |
| 737 | return cached_iter->second.get(); |
| 738 | } |
| 739 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 740 | FindEntryResult entry; |
| 741 | ApkAssetsCookie cookie = |
| 742 | FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 743 | if (cookie == kInvalidCookie) { |
| 744 | return nullptr; |
| 745 | } |
| 746 | |
| 747 | // Check that the size of the entry header is at least as big as |
| 748 | // the desired ResTable_map_entry. Also verify that the entry |
| 749 | // was intended to be a map. |
| 750 | if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) || |
| 751 | (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) { |
| 752 | // Not a bag, nothing to do. |
| 753 | return nullptr; |
| 754 | } |
| 755 | |
| 756 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry); |
| 757 | const ResTable_map* map_entry = |
| 758 | reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size); |
| 759 | const ResTable_map* const map_entry_end = map_entry + dtohl(map->count); |
| 760 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 761 | // Keep track of ids that have already been seen to prevent infinite loops caused by circular |
| 762 | // dependencies between bags |
| 763 | child_resids.push_back(resid); |
| 764 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 765 | uint32_t parent_resid = dtohl(map->parent.ident); |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 766 | if (parent_resid == 0 || std::find(child_resids.begin(), child_resids.end(), parent_resid) |
| 767 | != child_resids.end()) { |
| 768 | // There is no parent or that a circular dependency exist, meaning there is nothing to |
| 769 | // inherit and we can do a simple copy of the entries in the map. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 770 | const size_t entry_count = map_entry_end - map_entry; |
| 771 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 772 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
| 773 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 774 | for (; map_entry != map_entry_end; ++map_entry) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 775 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 776 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 777 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 778 | // other data, which would be wrong to change via a lookup. |
| 779 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 780 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 781 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 782 | return nullptr; |
| 783 | } |
| 784 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 785 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 786 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 787 | new_entry->key_pool = nullptr; |
| 788 | new_entry->type_pool = nullptr; |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 789 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 790 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 791 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 792 | if (err != NO_ERROR) { |
| 793 | LOG(ERROR) << base::StringPrintf( |
| 794 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 795 | new_entry->value.data, new_key); |
| 796 | return nullptr; |
| 797 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 798 | ++new_entry; |
| 799 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 800 | new_bag->type_spec_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 801 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 802 | ResolvedBag* result = new_bag.get(); |
| 803 | cached_bags_[resid] = std::move(new_bag); |
| 804 | return result; |
| 805 | } |
| 806 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 807 | // In case the parent is a dynamic reference, resolve it. |
| 808 | entry.dynamic_ref_table->lookupResourceId(&parent_resid); |
| 809 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 810 | // Get the parent and do a merge of the keys. |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 811 | const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 812 | if (parent_bag == nullptr) { |
| 813 | // Failed to get the parent that should exist. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 814 | LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, |
| 815 | resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 816 | return nullptr; |
| 817 | } |
| 818 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 819 | // Create the max possible entries we can make. Once we construct the bag, |
| 820 | // we will realloc to fit to size. |
| 821 | const size_t max_count = parent_bag->entry_count + dtohl(map->count); |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 822 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 823 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 824 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 825 | |
| 826 | const ResolvedBag::Entry* parent_entry = parent_bag->entries; |
| 827 | const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count; |
| 828 | |
| 829 | // The keys are expected to be in sorted order. Merge the two bags. |
| 830 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 831 | uint32_t child_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 832 | if (!is_internal_resid(child_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 833 | if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 834 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, |
| 835 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 836 | return nullptr; |
| 837 | } |
| 838 | } |
| 839 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 840 | if (child_key <= parent_entry->key) { |
| 841 | // Use the child key if it comes before the parent |
| 842 | // or is equal to the parent (overrides). |
| 843 | new_entry->cookie = cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 844 | new_entry->key = child_key; |
| 845 | new_entry->key_pool = nullptr; |
| 846 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 847 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 848 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 849 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 850 | if (err != NO_ERROR) { |
| 851 | LOG(ERROR) << base::StringPrintf( |
| 852 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 853 | new_entry->value.data, child_key); |
| 854 | return nullptr; |
| 855 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 856 | ++map_entry; |
| 857 | } else { |
| 858 | // Take the parent entry as-is. |
| 859 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 860 | } |
| 861 | |
| 862 | if (child_key >= parent_entry->key) { |
| 863 | // Move to the next parent entry if we used it or it was overridden. |
| 864 | ++parent_entry; |
| 865 | } |
| 866 | // Increment to the next entry to fill. |
| 867 | ++new_entry; |
| 868 | } |
| 869 | |
| 870 | // Finish the child entries if they exist. |
| 871 | while (map_entry != map_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 872 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 873 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 874 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 875 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 876 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 877 | return nullptr; |
| 878 | } |
| 879 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 880 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 881 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 882 | new_entry->key_pool = nullptr; |
| 883 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 884 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 885 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 886 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 887 | if (err != NO_ERROR) { |
| 888 | LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", |
| 889 | new_entry->value.dataType, new_entry->value.data, new_key); |
| 890 | return nullptr; |
| 891 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 892 | ++map_entry; |
| 893 | ++new_entry; |
| 894 | } |
| 895 | |
| 896 | // Finish the parent entries if they exist. |
| 897 | if (parent_entry != parent_entry_end) { |
| 898 | // Take the rest of the parent entries as-is. |
| 899 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 900 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 901 | new_entry += num_entries_to_copy; |
| 902 | } |
| 903 | |
| 904 | // Resize the resulting array to fit. |
| 905 | const size_t actual_count = new_entry - new_bag->entries; |
| 906 | if (actual_count != max_count) { |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 907 | new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc( |
| 908 | new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 911 | // Combine flags from the parent and our own bag. |
| 912 | new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags; |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 913 | new_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 914 | ResolvedBag* result = new_bag.get(); |
| 915 | cached_bags_[resid] = std::move(new_bag); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 916 | return result; |
| 917 | } |
| 918 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 919 | static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) { |
| 920 | ssize_t len = |
| 921 | utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false); |
| 922 | if (len < 0) { |
| 923 | return false; |
| 924 | } |
| 925 | out->resize(static_cast<size_t>(len)); |
| 926 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(), |
| 927 | static_cast<size_t>(len + 1)); |
| 928 | return true; |
| 929 | } |
| 930 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 931 | uint32_t AssetManager2::GetResourceId(const std::string& resource_name, |
| 932 | const std::string& fallback_type, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 933 | const std::string& fallback_package) const { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 934 | StringPiece package_name, type, entry; |
| 935 | if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) { |
| 936 | return 0u; |
| 937 | } |
| 938 | |
| 939 | if (entry.empty()) { |
| 940 | return 0u; |
| 941 | } |
| 942 | |
| 943 | if (package_name.empty()) { |
| 944 | package_name = fallback_package; |
| 945 | } |
| 946 | |
| 947 | if (type.empty()) { |
| 948 | type = fallback_type; |
| 949 | } |
| 950 | |
| 951 | std::u16string type16; |
| 952 | if (!Utf8ToUtf16(type, &type16)) { |
| 953 | return 0u; |
| 954 | } |
| 955 | |
| 956 | std::u16string entry16; |
| 957 | if (!Utf8ToUtf16(entry, &entry16)) { |
| 958 | return 0u; |
| 959 | } |
| 960 | |
| 961 | const StringPiece16 kAttr16 = u"attr"; |
| 962 | const static std::u16string kAttrPrivate16 = u"^attr-private"; |
| 963 | |
| 964 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 965 | for (const ConfiguredPackage& package_impl : package_group.packages_) { |
| 966 | const LoadedPackage* package = package_impl.loaded_package_; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 967 | if (package_name != package->GetPackageName()) { |
| 968 | // All packages in the same group are expected to have the same package name. |
| 969 | break; |
| 970 | } |
| 971 | |
| 972 | uint32_t resid = package->FindEntryByName(type16, entry16); |
| 973 | if (resid == 0u && kAttr16 == type16) { |
| 974 | // Private attributes in libraries (such as the framework) are sometimes encoded |
| 975 | // under the type '^attr-private' in order to leave the ID space of public 'attr' |
| 976 | // free for future additions. Check '^attr-private' for the same name. |
| 977 | resid = package->FindEntryByName(kAttrPrivate16, entry16); |
| 978 | } |
| 979 | |
| 980 | if (resid != 0u) { |
| 981 | return fix_package_id(resid, package_group.dynamic_ref_table.mAssignedPackageId); |
| 982 | } |
| 983 | } |
| 984 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 985 | return 0u; |
| 986 | } |
| 987 | |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 988 | void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 989 | for (PackageGroup& group : package_groups_) { |
| 990 | for (ConfiguredPackage& impl : group.packages_) { |
| 991 | // Destroy it. |
| 992 | impl.filtered_configs_.~ByteBucketArray(); |
| 993 | |
| 994 | // Re-create it. |
| 995 | new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>(); |
| 996 | |
| 997 | // Create the filters here. |
| 998 | impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) { |
| 999 | FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index); |
| 1000 | const auto iter_end = spec->types + spec->type_count; |
| 1001 | for (auto iter = spec->types; iter != iter_end; ++iter) { |
| 1002 | ResTable_config this_config; |
| 1003 | this_config.copyFromDtoH((*iter)->config); |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 1004 | if (!filter_incompatible_configs || this_config.match(configuration_)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1005 | group.configurations.push_back(this_config); |
| 1006 | group.types.push_back(*iter); |
| 1007 | } |
| 1008 | } |
| 1009 | }); |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1014 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
| 1015 | if (diff == 0xffffffffu) { |
| 1016 | // Everything must go. |
| 1017 | cached_bags_.clear(); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 1022 | // variations with respect to what changed (diff) should we remove it. |
| 1023 | for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) { |
| 1024 | if (diff & iter->second->type_spec_flags) { |
| 1025 | iter = cached_bags_.erase(iter); |
| 1026 | } else { |
| 1027 | ++iter; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1032 | uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) { |
| 1033 | for (auto& package_group : package_groups_) { |
| 1034 | for (auto& package2 : package_group.packages_) { |
| 1035 | if (package2.loaded_package_ == package) { |
| 1036 | return package_group.dynamic_ref_table.mAssignedPackageId; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1043 | std::unique_ptr<Theme> AssetManager2::NewTheme() { |
| 1044 | return std::unique_ptr<Theme>(new Theme(this)); |
| 1045 | } |
| 1046 | |
| 1047 | Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) { |
| 1048 | } |
| 1049 | |
| 1050 | Theme::~Theme() = default; |
| 1051 | |
| 1052 | namespace { |
| 1053 | |
| 1054 | struct ThemeEntry { |
| 1055 | ApkAssetsCookie cookie; |
| 1056 | uint32_t type_spec_flags; |
| 1057 | Res_value value; |
| 1058 | }; |
| 1059 | |
| 1060 | struct ThemeType { |
| 1061 | int entry_count; |
| 1062 | ThemeEntry entries[0]; |
| 1063 | }; |
| 1064 | |
| 1065 | constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1; |
| 1066 | |
| 1067 | } // namespace |
| 1068 | |
| 1069 | struct Theme::Package { |
| 1070 | // Each element of Type will be a dynamically sized object |
| 1071 | // allocated to have the entries stored contiguously with the Type. |
| 1072 | std::array<util::unique_cptr<ThemeType>, kTypeCount> types; |
| 1073 | }; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1074 | |
| 1075 | bool Theme::ApplyStyle(uint32_t resid, bool force) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1076 | ATRACE_NAME("Theme::ApplyStyle"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1077 | |
| 1078 | const ResolvedBag* bag = asset_manager_->GetBag(resid); |
| 1079 | if (bag == nullptr) { |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
| 1083 | // Merge the flags from this style. |
| 1084 | type_spec_flags_ |= bag->type_spec_flags; |
| 1085 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1086 | int last_type_idx = -1; |
| 1087 | int last_package_idx = -1; |
| 1088 | Package* last_package = nullptr; |
| 1089 | ThemeType* last_type = nullptr; |
| 1090 | |
| 1091 | // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only |
| 1092 | // need to perform one resize per type. |
| 1093 | using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>; |
| 1094 | const auto bag_iter_end = reverse_bag_iterator(begin(bag)); |
| 1095 | for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1096 | const uint32_t attr_resid = bag_iter->key; |
| 1097 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1098 | // If the resource ID passed in is not a style, the key can be some other identifier that is not |
| 1099 | // a resource ID. We should fail fast instead of operating with strange resource IDs. |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1100 | if (!is_valid_resid(attr_resid)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1101 | return false; |
| 1102 | } |
| 1103 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1104 | // We don't use the 0-based index for the type so that we can avoid doing ID validation |
| 1105 | // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since |
| 1106 | // the construction of this type is guarded with a resource ID check, it will never be |
| 1107 | // populated, and querying type ID 0 will always fail. |
| 1108 | const int package_idx = get_package_id(attr_resid); |
| 1109 | const int type_idx = get_type_id(attr_resid); |
| 1110 | const int entry_idx = get_entry_id(attr_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1111 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1112 | if (last_package_idx != package_idx) { |
| 1113 | std::unique_ptr<Package>& package = packages_[package_idx]; |
| 1114 | if (package == nullptr) { |
| 1115 | package.reset(new Package()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1116 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1117 | last_package_idx = package_idx; |
| 1118 | last_package = package.get(); |
| 1119 | last_type_idx = -1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1120 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1121 | |
| 1122 | if (last_type_idx != type_idx) { |
| 1123 | util::unique_cptr<ThemeType>& type = last_package->types[type_idx]; |
| 1124 | if (type == nullptr) { |
| 1125 | // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over |
| 1126 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 1127 | type.reset(reinterpret_cast<ThemeType*>( |
| 1128 | calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1))); |
| 1129 | type->entry_count = entry_idx + 1; |
| 1130 | } else if (entry_idx >= type->entry_count) { |
| 1131 | // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over |
| 1132 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 1133 | const int new_count = entry_idx + 1; |
| 1134 | type.reset(reinterpret_cast<ThemeType*>( |
| 1135 | realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry))))); |
| 1136 | |
| 1137 | // Clear out the newly allocated space (which isn't zeroed). |
| 1138 | memset(type->entries + type->entry_count, 0, |
| 1139 | (new_count - type->entry_count) * sizeof(ThemeEntry)); |
| 1140 | type->entry_count = new_count; |
| 1141 | } |
| 1142 | last_type_idx = type_idx; |
| 1143 | last_type = type.get(); |
| 1144 | } |
| 1145 | |
| 1146 | ThemeEntry& entry = last_type->entries[entry_idx]; |
| 1147 | if (force || (entry.value.dataType == Res_value::TYPE_NULL && |
| 1148 | entry.value.data != Res_value::DATA_NULL_EMPTY)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1149 | entry.cookie = bag_iter->cookie; |
| 1150 | entry.type_spec_flags |= bag->type_spec_flags; |
| 1151 | entry.value = bag_iter->value; |
| 1152 | } |
| 1153 | } |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value, |
| 1158 | uint32_t* out_flags) const { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1159 | int cnt = 20; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1160 | |
| 1161 | uint32_t type_spec_flags = 0u; |
| 1162 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1163 | do { |
| 1164 | const int package_idx = get_package_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1165 | const Package* package = packages_[package_idx].get(); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1166 | if (package != nullptr) { |
| 1167 | // The themes are constructed with a 1-based type ID, so no need to decrement here. |
| 1168 | const int type_idx = get_type_id(resid); |
| 1169 | const ThemeType* type = package->types[type_idx].get(); |
| 1170 | if (type != nullptr) { |
| 1171 | const int entry_idx = get_entry_id(resid); |
| 1172 | if (entry_idx < type->entry_count) { |
| 1173 | const ThemeEntry& entry = type->entries[entry_idx]; |
| 1174 | type_spec_flags |= entry.type_spec_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1175 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1176 | if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1177 | if (cnt > 0) { |
| 1178 | cnt--; |
| 1179 | resid = entry.value.data; |
| 1180 | continue; |
| 1181 | } |
| 1182 | return kInvalidCookie; |
| 1183 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1184 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1185 | // @null is different than @empty. |
| 1186 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1187 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1188 | return kInvalidCookie; |
| 1189 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1190 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1191 | *out_value = entry.value; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1192 | *out_flags = type_spec_flags; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1193 | return entry.cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1194 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1195 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1196 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1197 | break; |
| 1198 | } while (true); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1199 | return kInvalidCookie; |
| 1200 | } |
| 1201 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1202 | ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 1203 | ResTable_config* in_out_selected_config, |
| 1204 | uint32_t* in_out_type_spec_flags, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1205 | uint32_t* out_last_ref) const { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1206 | if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1207 | uint32_t new_flags; |
| 1208 | cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags); |
| 1209 | if (cookie == kInvalidCookie) { |
| 1210 | return kInvalidCookie; |
| 1211 | } |
| 1212 | |
| 1213 | if (in_out_type_spec_flags != nullptr) { |
| 1214 | *in_out_type_spec_flags |= new_flags; |
| 1215 | } |
| 1216 | } |
| 1217 | return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config, |
| 1218 | in_out_type_spec_flags, out_last_ref); |
| 1219 | } |
| 1220 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1221 | void Theme::Clear() { |
| 1222 | type_spec_flags_ = 0u; |
| 1223 | for (std::unique_ptr<Package>& package : packages_) { |
| 1224 | package.reset(); |
| 1225 | } |
| 1226 | } |
| 1227 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1228 | void Theme::SetTo(const Theme& o) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1229 | if (this == &o) { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1230 | return; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1233 | type_spec_flags_ = o.type_spec_flags_; |
| 1234 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1235 | if (asset_manager_ == o.asset_manager_) { |
| 1236 | // The theme comes from the same asset manager so all theme data can be copied exactly |
| 1237 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1238 | const Package *package = o.packages_[p].get(); |
| 1239 | if (package == nullptr) { |
| 1240 | // The other theme doesn't have this package, clear ours. |
| 1241 | packages_[p].reset(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1242 | continue; |
| 1243 | } |
| 1244 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1245 | if (packages_[p] == nullptr) { |
| 1246 | // The other theme has this package, but we don't. Make one. |
| 1247 | packages_[p].reset(new Package()); |
| 1248 | } |
| 1249 | |
| 1250 | for (size_t t = 0; t < package->types.size(); t++) { |
| 1251 | const ThemeType *type = package->types[t].get(); |
| 1252 | if (type == nullptr) { |
| 1253 | // The other theme doesn't have this type, clear ours. |
| 1254 | packages_[p]->types[t].reset(); |
| 1255 | continue; |
| 1256 | } |
| 1257 | |
| 1258 | // Create a new type and update it to theirs. |
| 1259 | const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry)); |
| 1260 | void *copied_data = malloc(type_alloc_size); |
| 1261 | memcpy(copied_data, type, type_alloc_size); |
| 1262 | packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data)); |
| 1263 | } |
| 1264 | } |
| 1265 | } else { |
| 1266 | std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies; |
| 1267 | typedef std::map<int, int> SourceToDestinationRuntimePackageMap; |
| 1268 | std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map; |
| 1269 | |
| 1270 | // Determine which ApkAssets are loaded in both theme AssetManagers |
| 1271 | std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets(); |
| 1272 | for (size_t i = 0; i < src_assets.size(); i++) { |
| 1273 | const ApkAssets* src_asset = src_assets[i]; |
| 1274 | |
| 1275 | std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets(); |
| 1276 | for (size_t j = 0; j < dest_assets.size(); j++) { |
| 1277 | const ApkAssets* dest_asset = dest_assets[j]; |
| 1278 | |
| 1279 | // Map the runtime package of the source apk asset to the destination apk asset |
| 1280 | if (src_asset->GetPath() == dest_asset->GetPath()) { |
| 1281 | const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages = |
| 1282 | src_asset->GetLoadedArsc()->GetPackages(); |
| 1283 | const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages = |
| 1284 | dest_asset->GetLoadedArsc()->GetPackages(); |
| 1285 | |
| 1286 | SourceToDestinationRuntimePackageMap package_map; |
| 1287 | |
| 1288 | // The source and destination package should have the same number of packages loaded in |
| 1289 | // the same order. |
| 1290 | const size_t N = src_packages.size(); |
| 1291 | CHECK(N == dest_packages.size()) |
| 1292 | << " LoadedArsc " << src_asset->GetPath() << " differs number of packages."; |
| 1293 | for (size_t p = 0; p < N; p++) { |
| 1294 | auto& src_package = src_packages[p]; |
| 1295 | auto& dest_package = dest_packages[p]; |
| 1296 | CHECK(src_package->GetPackageName() == dest_package->GetPackageName()) |
| 1297 | << " Package " << src_package->GetPackageName() << " differs in load order."; |
| 1298 | |
| 1299 | int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get()); |
| 1300 | int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get()); |
| 1301 | package_map[src_package_id] = dest_package_id; |
| 1302 | } |
| 1303 | |
| 1304 | src_to_dest_asset_cookies.insert(std::pair<ApkAssetsCookie, ApkAssetsCookie>(i, j)); |
| 1305 | src_asset_cookie_id_map.insert( |
| 1306 | std::pair<ApkAssetsCookie, SourceToDestinationRuntimePackageMap>(i, package_map)); |
| 1307 | break; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | // Reset the data in the destination theme |
| 1313 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1314 | if (packages_[p] != nullptr) { |
| 1315 | packages_[p].reset(); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1320 | const Package *package = o.packages_[p].get(); |
| 1321 | if (package == nullptr) { |
| 1322 | continue; |
| 1323 | } |
| 1324 | |
| 1325 | for (size_t t = 0; t < package->types.size(); t++) { |
| 1326 | const ThemeType *type = package->types[t].get(); |
| 1327 | if (type == nullptr) { |
| 1328 | continue; |
| 1329 | } |
| 1330 | |
| 1331 | for (size_t e = 0; e < type->entry_count; e++) { |
| 1332 | const ThemeEntry &entry = type->entries[e]; |
| 1333 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1334 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1335 | continue; |
| 1336 | } |
| 1337 | |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1338 | // If the attribute value represents an attribute or reference, the package id of the |
| 1339 | // value needs to be rewritten to the package id of the value in the destination |
| 1340 | uint32_t attribue_data = entry.value.data; |
| 1341 | if ((entry.value.dataType == Res_value::TYPE_ATTRIBUTE |
| 1342 | || entry.value.dataType == Res_value::TYPE_REFERENCE |
| 1343 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE |
| 1344 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) |
| 1345 | && attribue_data != 0x0) { |
| 1346 | |
| 1347 | // Determine the package id of the reference in the destination AssetManager |
| 1348 | auto value_package_map = src_asset_cookie_id_map.find(entry.cookie); |
| 1349 | if (value_package_map == src_asset_cookie_id_map.end()) { |
| 1350 | continue; |
| 1351 | } |
| 1352 | |
| 1353 | auto value_dest_package = value_package_map->second.find( |
| 1354 | get_package_id(entry.value.data)); |
| 1355 | if (value_dest_package == value_package_map->second.end()) { |
| 1356 | continue; |
| 1357 | } |
| 1358 | |
| 1359 | attribue_data = fix_package_id(entry.value.data, value_dest_package->second); |
| 1360 | } |
| 1361 | |
| 1362 | // The package id of the attribute needs to be rewritten to the package id of the |
| 1363 | // attribute in the destination |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1364 | int attribute_dest_package_id = p; |
| 1365 | if (attribute_dest_package_id != 0x01) { |
| 1366 | // Find the cookie of the attribute resource id |
| 1367 | FindEntryResult attribute_entry_result; |
| 1368 | ApkAssetsCookie attribute_cookie = |
| 1369 | o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ , false, |
| 1370 | &attribute_entry_result); |
| 1371 | |
| 1372 | // Determine the package id of the attribute in the destination AssetManager |
| 1373 | auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie); |
| 1374 | if (attribute_package_map == src_asset_cookie_id_map.end()) { |
| 1375 | continue; |
| 1376 | } |
| 1377 | auto attribute_dest_package = attribute_package_map->second.find( |
| 1378 | attribute_dest_package_id); |
| 1379 | if (attribute_dest_package == attribute_package_map->second.end()) { |
| 1380 | continue; |
| 1381 | } |
| 1382 | attribute_dest_package_id = attribute_dest_package->second; |
| 1383 | } |
| 1384 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1385 | // Lazily instantiate the destination package |
| 1386 | std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id]; |
| 1387 | if (dest_package == nullptr) { |
| 1388 | dest_package.reset(new Package()); |
| 1389 | } |
| 1390 | |
| 1391 | // Lazily instantiate and resize the destination type |
| 1392 | util::unique_cptr<ThemeType>& dest_type = dest_package->types[t]; |
| 1393 | if (dest_type == nullptr || dest_type->entry_count < type->entry_count) { |
| 1394 | const size_t type_alloc_size = sizeof(ThemeType) |
| 1395 | + (type->entry_count * sizeof(ThemeEntry)); |
| 1396 | void* dest_data = malloc(type_alloc_size); |
| 1397 | memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry)); |
| 1398 | |
| 1399 | // Copy the existing destination type values if the type is resized |
| 1400 | if (dest_type != nullptr) { |
| 1401 | memcpy(dest_data, type, sizeof(ThemeType) |
| 1402 | + (dest_type->entry_count * sizeof(ThemeEntry))); |
| 1403 | } |
| 1404 | |
| 1405 | dest_type.reset(reinterpret_cast<ThemeType *>(dest_data)); |
| 1406 | dest_type->entry_count = type->entry_count; |
| 1407 | } |
| 1408 | |
| 1409 | // Find the cookie of the value in the destination |
| 1410 | auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie); |
| 1411 | if (value_dest_cookie == src_to_dest_asset_cookies.end()) { |
| 1412 | continue; |
| 1413 | } |
| 1414 | |
| 1415 | dest_type->entries[e].cookie = value_dest_cookie->second; |
| 1416 | dest_type->entries[e].value.dataType = entry.value.dataType; |
| 1417 | dest_type->entries[e].value.data = attribue_data; |
| 1418 | dest_type->entries[e].type_spec_flags = entry.type_spec_flags; |
| 1419 | } |
| 1420 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1421 | } |
| 1422 | } |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | void Theme::Dump() const { |
| 1426 | base::ScopedLogSeverity _log(base::INFO); |
| 1427 | LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_); |
| 1428 | |
| 1429 | for (int p = 0; p < packages_.size(); p++) { |
| 1430 | auto& package = packages_[p]; |
| 1431 | if (package == nullptr) { |
| 1432 | continue; |
| 1433 | } |
| 1434 | |
| 1435 | for (int t = 0; t < package->types.size(); t++) { |
| 1436 | auto& type = package->types[t]; |
| 1437 | if (type == nullptr) { |
| 1438 | continue; |
| 1439 | } |
| 1440 | |
| 1441 | for (int e = 0; e < type->entry_count; e++) { |
| 1442 | auto& entry = type->entries[e]; |
| 1443 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1444 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1445 | continue; |
| 1446 | } |
| 1447 | |
| 1448 | LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)", |
| 1449 | make_resid(p, t, e), entry.value.data, |
| 1450 | entry.value.dataType, entry.cookie); |
| 1451 | } |
| 1452 | } |
| 1453 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | } // namespace android |