blob: b5c330622c720dce596a3b05cc6a9ddb14eed1bc [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"
27
28#include "ConfigDescription.h"
29#include "ResourceTable.h"
30#include "util/Util.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080031
32namespace aapt {
33
34using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
Shane Farmer0a5b2012017-06-22 12:24:12 -070035using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
Adam Lesinski355f2852016-02-13 20:26:45 -080036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037static ConfigDescription CopyWithoutDensity(const ConfigDescription& config) {
38 ConfigDescription without_density = config;
39 without_density.density = 0;
40 return without_density;
Adam Lesinski355f2852016-02-13 20:26:45 -080041}
42
43/**
44 * Selects values that match exactly the constraints given.
45 */
46class SplitValueSelector {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 public:
48 explicit SplitValueSelector(const SplitConstraints& constraints) {
49 for (const ConfigDescription& config : constraints.configs) {
50 if (config.density == 0) {
51 density_independent_configs_.insert(config);
52 } else {
Shane Farmer0a5b2012017-06-22 12:24:12 -070053 density_dependent_config_to_density_map_[CopyWithoutDensity(config)] = config.density;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 }
55 }
56 }
57
58 std::vector<ResourceConfigValue*> SelectValues(
59 const ConfigDensityGroups& density_groups,
60 ConfigClaimedMap* claimed_values) {
61 std::vector<ResourceConfigValue*> selected;
62
63 // Select the regular values.
64 for (auto& entry : *claimed_values) {
65 // Check if the entry has a density.
66 ResourceConfigValue* config_value = entry.first;
67 if (config_value->config.density == 0 && !entry.second) {
68 // This is still available.
69 if (density_independent_configs_.find(config_value->config) !=
70 density_independent_configs_.end()) {
71 selected.push_back(config_value);
72
73 // Mark the entry as taken.
74 entry.second = true;
Adam Lesinski355f2852016-02-13 20:26:45 -080075 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 }
Adam Lesinski355f2852016-02-13 20:26:45 -080077 }
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 // Now examine the densities
80 for (auto& entry : density_groups) {
81 // We do not care if the value is claimed, since density values can be
82 // in multiple splits.
83 const ConfigDescription& config = entry.first;
84 const std::vector<ResourceConfigValue*>& related_values = entry.second;
85 auto density_value_iter =
86 density_dependent_config_to_density_map_.find(config);
87 if (density_value_iter !=
88 density_dependent_config_to_density_map_.end()) {
89 // Select the best one!
90 ConfigDescription target_density = config;
91 target_density.density = density_value_iter->second;
Adam Lesinski355f2852016-02-13 20:26:45 -080092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 ResourceConfigValue* best_value = nullptr;
94 for (ResourceConfigValue* this_value : related_values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -070095 if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 best_value = this_value;
97 }
Adam Lesinski355f2852016-02-13 20:26:45 -080098 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 CHECK(best_value != nullptr);
Adam Lesinski355f2852016-02-13 20:26:45 -0800100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 // When we select one of these, they are all claimed such that the base
102 // doesn't include any anymore.
103 (*claimed_values)[best_value] = true;
104 selected.push_back(best_value);
105 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800106 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 return selected;
108 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 private:
111 DISALLOW_COPY_AND_ASSIGN(SplitValueSelector);
112
113 std::set<ConfigDescription> density_independent_configs_;
114 std::map<ConfigDescription, uint16_t>
115 density_dependent_config_to_density_map_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800116};
117
118/**
Shane Farmer0a5b2012017-06-22 12:24:12 -0700119 * Marking non-preferred densities as claimed will make sure the base doesn't include them, leaving
120 * only the preferred density behind.
Adam Lesinski355f2852016-02-13 20:26:45 -0800121 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122static void MarkNonPreferredDensitiesAsClaimed(
Pierre Lecesne672384b2017-02-06 10:29:02 +0000123 const std::vector<uint16_t>& preferred_densities, const ConfigDensityGroups& density_groups,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 ConfigClaimedMap* config_claimed_map) {
125 for (auto& entry : density_groups) {
126 const ConfigDescription& config = entry.first;
127 const std::vector<ResourceConfigValue*>& related_values = entry.second;
Adam Lesinski355f2852016-02-13 20:26:45 -0800128
Pierre Lecesne672384b2017-02-06 10:29:02 +0000129 // There can be multiple best values if there are multiple preferred densities.
130 std::unordered_set<ResourceConfigValue*> best_values;
131
132 // For each preferred density, find the value that is the best.
133 for (uint16_t preferred_density : preferred_densities) {
134 ConfigDescription target_density = config;
135 target_density.density = preferred_density;
136 ResourceConfigValue* best_value = nullptr;
137 for (ResourceConfigValue* this_value : related_values) {
138 if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) {
139 best_value = this_value;
140 }
141 }
142 CHECK(best_value != nullptr);
143 best_values.insert(best_value);
144 }
145
146 // 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 -0700147 for (ResourceConfigValue* this_value : related_values) {
Pierre Lecesne672384b2017-02-06 10:29:02 +0000148 if (best_values.find(this_value) == best_values.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 (*config_claimed_map)[this_value] = true;
150 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800151 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800153}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154bool TableSplitter::VerifySplitConstraints(IAaptContext* context) {
155 bool error = false;
156 for (size_t i = 0; i < split_constraints_.size(); i++) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700157 if (split_constraints_[i].configs.size() == 0) {
158 // For now, treat this as a warning. We may consider aborting processing.
159 context->GetDiagnostics()->Warn(DiagMessage()
160 << "no configurations for constraint '"
161 << split_constraints_[i].name << "'");
162 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 for (size_t j = i + 1; j < split_constraints_.size(); j++) {
164 for (const ConfigDescription& config : split_constraints_[i].configs) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700165 if (split_constraints_[j].configs.find(config) != split_constraints_[j].configs.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 context->GetDiagnostics()->Error(DiagMessage()
167 << "config '" << config
168 << "' appears in multiple splits, "
169 << "target split ambiguous");
170 error = true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800171 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800173 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 }
175 return !error;
Adam Lesinski355f2852016-02-13 20:26:45 -0800176}
177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178void TableSplitter::SplitTable(ResourceTable* original_table) {
179 const size_t split_count = split_constraints_.size();
180 for (auto& pkg : original_table->packages) {
181 // Initialize all packages for splits.
182 for (size_t idx = 0; idx < split_count; idx++) {
183 ResourceTable* split_table = splits_[idx].get();
184 split_table->CreatePackage(pkg->name, pkg->id);
Adam Lesinski355f2852016-02-13 20:26:45 -0800185 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186
187 for (auto& type : pkg->types) {
188 if (type->type == ResourceType::kMipmap) {
189 // Always keep mipmaps.
190 continue;
191 }
192
193 for (auto& entry : type->entries) {
194 if (options_.config_filter) {
195 // First eliminate any resource that we definitely don't want.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700196 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (!options_.config_filter->Match(config_value->config)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700198 // null out the entry. We will clean up and remove nulls at the end for performance
199 // reasons.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 config_value.reset();
201 }
202 }
203 }
204
Shane Farmer0a5b2012017-06-22 12:24:12 -0700205 // Organize the values into two separate buckets. Those that are density-dependent and those
206 // that are density-independent. One density technically matches all density, it's just that
207 // some densities match better. So we need to be aware of the full set of densities to make
208 // this decision.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 ConfigDensityGroups density_groups;
210 ConfigClaimedMap config_claimed_map;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700211 for (const std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 if (config_value) {
213 config_claimed_map[config_value.get()] = false;
214
215 if (config_value->config.density != 0) {
216 // Create a bucket for this density-dependent config.
217 density_groups[CopyWithoutDensity(config_value->config)]
218 .push_back(config_value.get());
219 }
220 }
221 }
222
Shane Farmer0a5b2012017-06-22 12:24:12 -0700223 // First we check all the splits. If it doesn't match one of the splits, we leave it in the
224 // base.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 for (size_t idx = 0; idx < split_count; idx++) {
226 const SplitConstraints& split_constraint = split_constraints_[idx];
227 ResourceTable* split_table = splits_[idx].get();
228
229 // Select the values we want from this entry for this split.
230 SplitValueSelector selector(split_constraint);
231 std::vector<ResourceConfigValue*> selected_values =
232 selector.SelectValues(density_groups, &config_claimed_map);
233
234 // No need to do any work if we selected nothing.
235 if (!selected_values.empty()) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700236 // Create the same resource structure in the split. We do this lazily because we might
237 // not have actual values for each type/entry.
238 ResourceTablePackage* split_pkg = split_table->FindPackage(pkg->name);
239 ResourceTableType* split_type = split_pkg->FindOrCreateType(type->type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 if (!split_type->id) {
241 split_type->id = type->id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800242 split_type->visibility_level = type->visibility_level;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 }
244
Shane Farmer0a5b2012017-06-22 12:24:12 -0700245 ResourceEntry* split_entry = split_type->FindOrCreateEntry(entry->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 if (!split_entry->id) {
247 split_entry->id = entry->id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800248 split_entry->visibility = entry->visibility;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 }
250
251 // Copy the selected values into the new Split Entry.
252 for (ResourceConfigValue* config_value : selected_values) {
253 ResourceConfigValue* new_config_value =
Shane Farmer0a5b2012017-06-22 12:24:12 -0700254 split_entry->FindOrCreateValue(config_value->config, config_value->product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 new_config_value->value = std::unique_ptr<Value>(
256 config_value->value->Clone(&split_table->string_pool));
257 }
258 }
259 }
260
Pierre Lecesne672384b2017-02-06 10:29:02 +0000261 if (!options_.preferred_densities.empty()) {
262 MarkNonPreferredDensitiesAsClaimed(options_.preferred_densities,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 density_groups,
264 &config_claimed_map);
265 }
266
Shane Farmer0a5b2012017-06-22 12:24:12 -0700267 // All splits are handled, now check to see what wasn't claimed and remove whatever exists
268 // in other splits.
269 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 if (config_value && config_claimed_map[config_value.get()]) {
271 // Claimed, remove from base.
272 config_value.reset();
273 }
274 }
275
276 // Now erase all nullptrs.
277 entry->values.erase(
278 std::remove(entry->values.begin(), entry->values.end(), nullptr),
279 entry->values.end());
280 }
281 }
282 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285} // namespace aapt