blob: 24cd5ba302ea514e654ac2f21b43af567f138114 [file] [log] [blame]
Adam Lesinski355f2852016-02-13 20:26:45 -08001/*
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 Lesinski355f2852016-02-13 20:26:45 -080017#include "split/TableSplitter.h"
18
Adam Lesinski803c7c82016-04-06 16:09:43 -070019#include <algorithm>
Adam Lesinski355f2852016-02-13 20:26:45 -080020#include <map>
21#include <set>
Pierre Lecesne672384b2017-02-06 10:29:02 +000022#include <unordered_set>
Adam Lesinski355f2852016-02-13 20:26:45 -080023#include <unordered_map>
24#include <vector>
Adam Lesinskid5083f62017-01-16 15:07:21 -080025
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "android-base/logging.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020027#include "androidfw/ConfigDescription.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "ResourceTable.h"
30#include "util/Util.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080031
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
33
Adam Lesinski355f2852016-02-13 20:26:45 -080034namespace aapt {
35
36using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
Shane Farmer0a5b2012017-06-22 12:24:12 -070037using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
Adam Lesinski355f2852016-02-13 20:26:45 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039static ConfigDescription CopyWithoutDensity(const ConfigDescription& config) {
40 ConfigDescription without_density = config;
41 without_density.density = 0;
42 return without_density;
Adam Lesinski355f2852016-02-13 20:26:45 -080043}
44
45/**
46 * Selects values that match exactly the constraints given.
47 */
48class SplitValueSelector {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 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 Farmer0a5b2012017-06-22 12:24:12 -070055 density_dependent_config_to_density_map_[CopyWithoutDensity(config)] = config.density;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 }
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 Lesinski355f2852016-02-13 20:26:45 -080077 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 }
Adam Lesinski355f2852016-02-13 20:26:45 -080079 }
80
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 // 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 Lesinski355f2852016-02-13 20:26:45 -080094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 ResourceConfigValue* best_value = nullptr;
96 for (ResourceConfigValue* this_value : related_values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -070097 if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 best_value = this_value;
99 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800100 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 CHECK(best_value != nullptr);
Adam Lesinski355f2852016-02-13 20:26:45 -0800102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 // 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 Lesinski355f2852016-02-13 20:26:45 -0800108 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 return selected;
110 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 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 Lesinski355f2852016-02-13 20:26:45 -0800118};
119
120/**
Shane Farmer0a5b2012017-06-22 12:24:12 -0700121 * Marking non-preferred densities as claimed will make sure the base doesn't include them, leaving
122 * only the preferred density behind.
Adam Lesinski355f2852016-02-13 20:26:45 -0800123 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124static void MarkNonPreferredDensitiesAsClaimed(
Pierre Lecesne672384b2017-02-06 10:29:02 +0000125 const std::vector<uint16_t>& preferred_densities, const ConfigDensityGroups& density_groups,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 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 Lesinski355f2852016-02-13 20:26:45 -0800130
Pierre Lecesne672384b2017-02-06 10:29:02 +0000131 // 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 Lesinskice5e56e2016-10-21 17:56:45 -0700149 for (ResourceConfigValue* this_value : related_values) {
Pierre Lecesne672384b2017-02-06 10:29:02 +0000150 if (best_values.find(this_value) == best_values.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 (*config_claimed_map)[this_value] = true;
152 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800153 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800155}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156bool TableSplitter::VerifySplitConstraints(IAaptContext* context) {
157 bool error = false;
158 for (size_t i = 0; i < split_constraints_.size(); i++) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700159 if (split_constraints_[i].configs.size() == 0) {
160 // For now, treat this as a warning. We may consider aborting processing.
161 context->GetDiagnostics()->Warn(DiagMessage()
162 << "no configurations for constraint '"
163 << split_constraints_[i].name << "'");
164 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 for (size_t j = i + 1; j < split_constraints_.size(); j++) {
166 for (const ConfigDescription& config : split_constraints_[i].configs) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700167 if (split_constraints_[j].configs.find(config) != split_constraints_[j].configs.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 context->GetDiagnostics()->Error(DiagMessage()
169 << "config '" << config
170 << "' appears in multiple splits, "
171 << "target split ambiguous");
172 error = true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800173 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800175 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 }
177 return !error;
Adam Lesinski355f2852016-02-13 20:26:45 -0800178}
179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180void TableSplitter::SplitTable(ResourceTable* original_table) {
181 const size_t split_count = split_constraints_.size();
182 for (auto& pkg : original_table->packages) {
183 // Initialize all packages for splits.
184 for (size_t idx = 0; idx < split_count; idx++) {
185 ResourceTable* split_table = splits_[idx].get();
186 split_table->CreatePackage(pkg->name, pkg->id);
Adam Lesinski355f2852016-02-13 20:26:45 -0800187 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188
189 for (auto& type : pkg->types) {
190 if (type->type == ResourceType::kMipmap) {
191 // Always keep mipmaps.
192 continue;
193 }
194
195 for (auto& entry : type->entries) {
196 if (options_.config_filter) {
197 // First eliminate any resource that we definitely don't want.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700198 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 if (!options_.config_filter->Match(config_value->config)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700200 // null out the entry. We will clean up and remove nulls at the end for performance
201 // reasons.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 config_value.reset();
203 }
204 }
205 }
206
Shane Farmer0a5b2012017-06-22 12:24:12 -0700207 // Organize the values into two separate buckets. Those that are density-dependent and those
208 // that are density-independent. One density technically matches all density, it's just that
209 // some densities match better. So we need to be aware of the full set of densities to make
210 // this decision.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 ConfigDensityGroups density_groups;
212 ConfigClaimedMap config_claimed_map;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700213 for (const std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 if (config_value) {
215 config_claimed_map[config_value.get()] = false;
216
217 if (config_value->config.density != 0) {
218 // Create a bucket for this density-dependent config.
219 density_groups[CopyWithoutDensity(config_value->config)]
220 .push_back(config_value.get());
221 }
222 }
223 }
224
Shane Farmer0a5b2012017-06-22 12:24:12 -0700225 // First we check all the splits. If it doesn't match one of the splits, we leave it in the
226 // base.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 for (size_t idx = 0; idx < split_count; idx++) {
228 const SplitConstraints& split_constraint = split_constraints_[idx];
229 ResourceTable* split_table = splits_[idx].get();
230
231 // Select the values we want from this entry for this split.
232 SplitValueSelector selector(split_constraint);
233 std::vector<ResourceConfigValue*> selected_values =
234 selector.SelectValues(density_groups, &config_claimed_map);
235
236 // No need to do any work if we selected nothing.
237 if (!selected_values.empty()) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700238 // Create the same resource structure in the split. We do this lazily because we might
239 // not have actual values for each type/entry.
240 ResourceTablePackage* split_pkg = split_table->FindPackage(pkg->name);
241 ResourceTableType* split_type = split_pkg->FindOrCreateType(type->type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 if (!split_type->id) {
243 split_type->id = type->id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800244 split_type->visibility_level = type->visibility_level;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 }
246
Shane Farmer0a5b2012017-06-22 12:24:12 -0700247 ResourceEntry* split_entry = split_type->FindOrCreateEntry(entry->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 if (!split_entry->id) {
249 split_entry->id = entry->id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800250 split_entry->visibility = entry->visibility;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800251 split_entry->overlayable_item = entry->overlayable_item;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 }
253
254 // Copy the selected values into the new Split Entry.
255 for (ResourceConfigValue* config_value : selected_values) {
256 ResourceConfigValue* new_config_value =
Shane Farmer0a5b2012017-06-22 12:24:12 -0700257 split_entry->FindOrCreateValue(config_value->config, config_value->product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 new_config_value->value = std::unique_ptr<Value>(
259 config_value->value->Clone(&split_table->string_pool));
260 }
261 }
262 }
263
Pierre Lecesne672384b2017-02-06 10:29:02 +0000264 if (!options_.preferred_densities.empty()) {
265 MarkNonPreferredDensitiesAsClaimed(options_.preferred_densities,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 density_groups,
267 &config_claimed_map);
268 }
269
Shane Farmer0a5b2012017-06-22 12:24:12 -0700270 // All splits are handled, now check to see what wasn't claimed and remove whatever exists
271 // in other splits.
272 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (config_value && config_claimed_map[config_value.get()]) {
274 // Claimed, remove from base.
275 config_value.reset();
276 }
277 }
278
279 // Now erase all nullptrs.
280 entry->values.erase(
281 std::remove(entry->values.begin(), entry->values.end(), nullptr),
282 entry->values.end());
283 }
284 }
285 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800286}
287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288} // namespace aapt