Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [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 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 17 | #include "split/TableSplitter.h" |
| 18 | |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <set> |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 22 | #include <unordered_set> |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 23 | #include <unordered_map> |
| 24 | #include <vector> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame^] | 27 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | #include "ResourceTable.h" |
| 30 | #include "util/Util.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 31 | |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame^] | 32 | using ::android::ConfigDescription; |
| 33 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 34 | namespace aapt { |
| 35 | |
| 36 | using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 37 | using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | static ConfigDescription CopyWithoutDensity(const ConfigDescription& config) { |
| 40 | ConfigDescription without_density = config; |
| 41 | without_density.density = 0; |
| 42 | return without_density; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Selects values that match exactly the constraints given. |
| 47 | */ |
| 48 | class SplitValueSelector { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | public: |
| 50 | explicit SplitValueSelector(const SplitConstraints& constraints) { |
| 51 | for (const ConfigDescription& config : constraints.configs) { |
| 52 | if (config.density == 0) { |
| 53 | density_independent_configs_.insert(config); |
| 54 | } else { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 55 | density_dependent_config_to_density_map_[CopyWithoutDensity(config)] = config.density; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | std::vector<ResourceConfigValue*> SelectValues( |
| 61 | const ConfigDensityGroups& density_groups, |
| 62 | ConfigClaimedMap* claimed_values) { |
| 63 | std::vector<ResourceConfigValue*> selected; |
| 64 | |
| 65 | // Select the regular values. |
| 66 | for (auto& entry : *claimed_values) { |
| 67 | // Check if the entry has a density. |
| 68 | ResourceConfigValue* config_value = entry.first; |
| 69 | if (config_value->config.density == 0 && !entry.second) { |
| 70 | // This is still available. |
| 71 | if (density_independent_configs_.find(config_value->config) != |
| 72 | density_independent_configs_.end()) { |
| 73 | selected.push_back(config_value); |
| 74 | |
| 75 | // Mark the entry as taken. |
| 76 | entry.second = true; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 77 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | // Now examine the densities |
| 82 | for (auto& entry : density_groups) { |
| 83 | // We do not care if the value is claimed, since density values can be |
| 84 | // in multiple splits. |
| 85 | const ConfigDescription& config = entry.first; |
| 86 | const std::vector<ResourceConfigValue*>& related_values = entry.second; |
| 87 | auto density_value_iter = |
| 88 | density_dependent_config_to_density_map_.find(config); |
| 89 | if (density_value_iter != |
| 90 | density_dependent_config_to_density_map_.end()) { |
| 91 | // Select the best one! |
| 92 | ConfigDescription target_density = config; |
| 93 | target_density.density = density_value_iter->second; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | ResourceConfigValue* best_value = nullptr; |
| 96 | for (ResourceConfigValue* this_value : related_values) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 97 | if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | best_value = this_value; |
| 99 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 100 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | CHECK(best_value != nullptr); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | // When we select one of these, they are all claimed such that the base |
| 104 | // doesn't include any anymore. |
| 105 | (*claimed_values)[best_value] = true; |
| 106 | selected.push_back(best_value); |
| 107 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 108 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | return selected; |
| 110 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | private: |
| 113 | DISALLOW_COPY_AND_ASSIGN(SplitValueSelector); |
| 114 | |
| 115 | std::set<ConfigDescription> density_independent_configs_; |
| 116 | std::map<ConfigDescription, uint16_t> |
| 117 | density_dependent_config_to_density_map_; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /** |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 121 | * Marking non-preferred densities as claimed will make sure the base doesn't include them, leaving |
| 122 | * only the preferred density behind. |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 123 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | static void MarkNonPreferredDensitiesAsClaimed( |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 125 | const std::vector<uint16_t>& preferred_densities, const ConfigDensityGroups& density_groups, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | ConfigClaimedMap* config_claimed_map) { |
| 127 | for (auto& entry : density_groups) { |
| 128 | const ConfigDescription& config = entry.first; |
| 129 | const std::vector<ResourceConfigValue*>& related_values = entry.second; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 130 | |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 131 | // There can be multiple best values if there are multiple preferred densities. |
| 132 | std::unordered_set<ResourceConfigValue*> best_values; |
| 133 | |
| 134 | // For each preferred density, find the value that is the best. |
| 135 | for (uint16_t preferred_density : preferred_densities) { |
| 136 | ConfigDescription target_density = config; |
| 137 | target_density.density = preferred_density; |
| 138 | ResourceConfigValue* best_value = nullptr; |
| 139 | for (ResourceConfigValue* this_value : related_values) { |
| 140 | if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) { |
| 141 | best_value = this_value; |
| 142 | } |
| 143 | } |
| 144 | CHECK(best_value != nullptr); |
| 145 | best_values.insert(best_value); |
| 146 | } |
| 147 | |
| 148 | // Claim all the values that aren't the best so that they will be removed from the base. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | for (ResourceConfigValue* this_value : related_values) { |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 150 | if (best_values.find(this_value) == best_values.end()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | (*config_claimed_map)[this_value] = true; |
| 152 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 153 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 155 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | bool TableSplitter::VerifySplitConstraints(IAaptContext* context) { |
| 157 | bool error = false; |
| 158 | for (size_t i = 0; i < split_constraints_.size(); i++) { |
| 159 | for (size_t j = i + 1; j < split_constraints_.size(); j++) { |
| 160 | for (const ConfigDescription& config : split_constraints_[i].configs) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 161 | if (split_constraints_[j].configs.find(config) != split_constraints_[j].configs.end()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | context->GetDiagnostics()->Error(DiagMessage() |
| 163 | << "config '" << config |
| 164 | << "' appears in multiple splits, " |
| 165 | << "target split ambiguous"); |
| 166 | error = true; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 167 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 169 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | } |
| 171 | return !error; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | void TableSplitter::SplitTable(ResourceTable* original_table) { |
| 175 | const size_t split_count = split_constraints_.size(); |
| 176 | for (auto& pkg : original_table->packages) { |
| 177 | // Initialize all packages for splits. |
| 178 | for (size_t idx = 0; idx < split_count; idx++) { |
| 179 | ResourceTable* split_table = splits_[idx].get(); |
| 180 | split_table->CreatePackage(pkg->name, pkg->id); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 181 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | |
| 183 | for (auto& type : pkg->types) { |
| 184 | if (type->type == ResourceType::kMipmap) { |
| 185 | // Always keep mipmaps. |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | for (auto& entry : type->entries) { |
| 190 | if (options_.config_filter) { |
| 191 | // First eliminate any resource that we definitely don't want. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 192 | for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | if (!options_.config_filter->Match(config_value->config)) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 194 | // null out the entry. We will clean up and remove nulls at the end for performance |
| 195 | // reasons. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | config_value.reset(); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 201 | // Organize the values into two separate buckets. Those that are density-dependent and those |
| 202 | // that are density-independent. One density technically matches all density, it's just that |
| 203 | // some densities match better. So we need to be aware of the full set of densities to make |
| 204 | // this decision. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | ConfigDensityGroups density_groups; |
| 206 | ConfigClaimedMap config_claimed_map; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 207 | for (const std::unique_ptr<ResourceConfigValue>& config_value : entry->values) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 | if (config_value) { |
| 209 | config_claimed_map[config_value.get()] = false; |
| 210 | |
| 211 | if (config_value->config.density != 0) { |
| 212 | // Create a bucket for this density-dependent config. |
| 213 | density_groups[CopyWithoutDensity(config_value->config)] |
| 214 | .push_back(config_value.get()); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 219 | // First we check all the splits. If it doesn't match one of the splits, we leave it in the |
| 220 | // base. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | for (size_t idx = 0; idx < split_count; idx++) { |
| 222 | const SplitConstraints& split_constraint = split_constraints_[idx]; |
| 223 | ResourceTable* split_table = splits_[idx].get(); |
| 224 | |
| 225 | // Select the values we want from this entry for this split. |
| 226 | SplitValueSelector selector(split_constraint); |
| 227 | std::vector<ResourceConfigValue*> selected_values = |
| 228 | selector.SelectValues(density_groups, &config_claimed_map); |
| 229 | |
| 230 | // No need to do any work if we selected nothing. |
| 231 | if (!selected_values.empty()) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 232 | // Create the same resource structure in the split. We do this lazily because we might |
| 233 | // not have actual values for each type/entry. |
| 234 | ResourceTablePackage* split_pkg = split_table->FindPackage(pkg->name); |
| 235 | ResourceTableType* split_type = split_pkg->FindOrCreateType(type->type); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | if (!split_type->id) { |
| 237 | split_type->id = type->id; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 238 | split_type->visibility_level = type->visibility_level; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 241 | ResourceEntry* split_entry = split_type->FindOrCreateEntry(entry->name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | if (!split_entry->id) { |
| 243 | split_entry->id = entry->id; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 244 | split_entry->visibility = entry->visibility; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Copy the selected values into the new Split Entry. |
| 248 | for (ResourceConfigValue* config_value : selected_values) { |
| 249 | ResourceConfigValue* new_config_value = |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 250 | split_entry->FindOrCreateValue(config_value->config, config_value->product); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | new_config_value->value = std::unique_ptr<Value>( |
| 252 | config_value->value->Clone(&split_table->string_pool)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 257 | if (!options_.preferred_densities.empty()) { |
| 258 | MarkNonPreferredDensitiesAsClaimed(options_.preferred_densities, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 259 | density_groups, |
| 260 | &config_claimed_map); |
| 261 | } |
| 262 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 263 | // All splits are handled, now check to see what wasn't claimed and remove whatever exists |
| 264 | // in other splits. |
| 265 | for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | if (config_value && config_claimed_map[config_value.get()]) { |
| 267 | // Claimed, remove from base. |
| 268 | config_value.reset(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Now erase all nullptrs. |
| 273 | entry->values.erase( |
| 274 | std::remove(entry->values.begin(), entry->values.end(), nullptr), |
| 275 | entry->values.end()); |
| 276 | } |
| 277 | } |
| 278 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | } // namespace aapt |