blob: 6a672717f38ee6336df6289c89e7f7cc4a1ece68 [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"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080030#include "trace/TraceBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#include "util/Util.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080032
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020033using ::android::ConfigDescription;
34
Adam Lesinski355f2852016-02-13 20:26:45 -080035namespace aapt {
36
37using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
Shane Farmer0a5b2012017-06-22 12:24:12 -070038using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
Adam Lesinski355f2852016-02-13 20:26:45 -080039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040static ConfigDescription CopyWithoutDensity(const ConfigDescription& config) {
41 ConfigDescription without_density = config;
42 without_density.density = 0;
43 return without_density;
Adam Lesinski355f2852016-02-13 20:26:45 -080044}
45
46/**
47 * Selects values that match exactly the constraints given.
48 */
49class SplitValueSelector {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 public:
51 explicit SplitValueSelector(const SplitConstraints& constraints) {
52 for (const ConfigDescription& config : constraints.configs) {
53 if (config.density == 0) {
54 density_independent_configs_.insert(config);
55 } else {
Shane Farmer0a5b2012017-06-22 12:24:12 -070056 density_dependent_config_to_density_map_[CopyWithoutDensity(config)] = config.density;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 }
58 }
59 }
60
61 std::vector<ResourceConfigValue*> SelectValues(
62 const ConfigDensityGroups& density_groups,
63 ConfigClaimedMap* claimed_values) {
64 std::vector<ResourceConfigValue*> selected;
65
66 // Select the regular values.
67 for (auto& entry : *claimed_values) {
68 // Check if the entry has a density.
69 ResourceConfigValue* config_value = entry.first;
70 if (config_value->config.density == 0 && !entry.second) {
71 // This is still available.
72 if (density_independent_configs_.find(config_value->config) !=
73 density_independent_configs_.end()) {
74 selected.push_back(config_value);
75
76 // Mark the entry as taken.
77 entry.second = true;
Adam Lesinski355f2852016-02-13 20:26:45 -080078 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 }
Adam Lesinski355f2852016-02-13 20:26:45 -080080 }
81
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 // Now examine the densities
83 for (auto& entry : density_groups) {
84 // We do not care if the value is claimed, since density values can be
85 // in multiple splits.
86 const ConfigDescription& config = entry.first;
87 const std::vector<ResourceConfigValue*>& related_values = entry.second;
88 auto density_value_iter =
89 density_dependent_config_to_density_map_.find(config);
90 if (density_value_iter !=
91 density_dependent_config_to_density_map_.end()) {
92 // Select the best one!
93 ConfigDescription target_density = config;
94 target_density.density = density_value_iter->second;
Adam Lesinski355f2852016-02-13 20:26:45 -080095
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 ResourceConfigValue* best_value = nullptr;
97 for (ResourceConfigValue* this_value : related_values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -070098 if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 best_value = this_value;
100 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800101 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 CHECK(best_value != nullptr);
Adam Lesinski355f2852016-02-13 20:26:45 -0800103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 // When we select one of these, they are all claimed such that the base
105 // doesn't include any anymore.
106 (*claimed_values)[best_value] = true;
107 selected.push_back(best_value);
108 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800109 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 return selected;
111 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 private:
114 DISALLOW_COPY_AND_ASSIGN(SplitValueSelector);
115
116 std::set<ConfigDescription> density_independent_configs_;
117 std::map<ConfigDescription, uint16_t>
118 density_dependent_config_to_density_map_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800119};
120
121/**
Shane Farmer0a5b2012017-06-22 12:24:12 -0700122 * Marking non-preferred densities as claimed will make sure the base doesn't include them, leaving
123 * only the preferred density behind.
Adam Lesinski355f2852016-02-13 20:26:45 -0800124 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125static void MarkNonPreferredDensitiesAsClaimed(
Pierre Lecesne672384b2017-02-06 10:29:02 +0000126 const std::vector<uint16_t>& preferred_densities, const ConfigDensityGroups& density_groups,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 ConfigClaimedMap* config_claimed_map) {
128 for (auto& entry : density_groups) {
129 const ConfigDescription& config = entry.first;
130 const std::vector<ResourceConfigValue*>& related_values = entry.second;
Adam Lesinski355f2852016-02-13 20:26:45 -0800131
Pierre Lecesne672384b2017-02-06 10:29:02 +0000132 // There can be multiple best values if there are multiple preferred densities.
133 std::unordered_set<ResourceConfigValue*> best_values;
134
135 // For each preferred density, find the value that is the best.
136 for (uint16_t preferred_density : preferred_densities) {
137 ConfigDescription target_density = config;
138 target_density.density = preferred_density;
139 ResourceConfigValue* best_value = nullptr;
140 for (ResourceConfigValue* this_value : related_values) {
141 if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) {
142 best_value = this_value;
143 }
144 }
145 CHECK(best_value != nullptr);
146 best_values.insert(best_value);
147 }
148
149 // 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 -0700150 for (ResourceConfigValue* this_value : related_values) {
Pierre Lecesne672384b2017-02-06 10:29:02 +0000151 if (best_values.find(this_value) == best_values.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 (*config_claimed_map)[this_value] = true;
153 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800154 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800156}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157bool TableSplitter::VerifySplitConstraints(IAaptContext* context) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800158 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 bool error = false;
160 for (size_t i = 0; i < split_constraints_.size(); i++) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700161 if (split_constraints_[i].configs.size() == 0) {
162 // For now, treat this as a warning. We may consider aborting processing.
163 context->GetDiagnostics()->Warn(DiagMessage()
164 << "no configurations for constraint '"
165 << split_constraints_[i].name << "'");
166 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 for (size_t j = i + 1; j < split_constraints_.size(); j++) {
168 for (const ConfigDescription& config : split_constraints_[i].configs) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700169 if (split_constraints_[j].configs.find(config) != split_constraints_[j].configs.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 context->GetDiagnostics()->Error(DiagMessage()
171 << "config '" << config
172 << "' appears in multiple splits, "
173 << "target split ambiguous");
174 error = true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800175 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800177 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 }
179 return !error;
Adam Lesinski355f2852016-02-13 20:26:45 -0800180}
181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182void TableSplitter::SplitTable(ResourceTable* original_table) {
183 const size_t split_count = split_constraints_.size();
184 for (auto& pkg : original_table->packages) {
185 // Initialize all packages for splits.
186 for (size_t idx = 0; idx < split_count; idx++) {
187 ResourceTable* split_table = splits_[idx].get();
188 split_table->CreatePackage(pkg->name, pkg->id);
Adam Lesinski355f2852016-02-13 20:26:45 -0800189 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190
191 for (auto& type : pkg->types) {
192 if (type->type == ResourceType::kMipmap) {
193 // Always keep mipmaps.
194 continue;
195 }
196
197 for (auto& entry : type->entries) {
198 if (options_.config_filter) {
199 // First eliminate any resource that we definitely don't want.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700200 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (!options_.config_filter->Match(config_value->config)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700202 // null out the entry. We will clean up and remove nulls at the end for performance
203 // reasons.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 config_value.reset();
205 }
206 }
207 }
208
Shane Farmer0a5b2012017-06-22 12:24:12 -0700209 // Organize the values into two separate buckets. Those that are density-dependent and those
210 // that are density-independent. One density technically matches all density, it's just that
211 // some densities match better. So we need to be aware of the full set of densities to make
212 // this decision.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 ConfigDensityGroups density_groups;
214 ConfigClaimedMap config_claimed_map;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700215 for (const std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 if (config_value) {
217 config_claimed_map[config_value.get()] = false;
218
219 if (config_value->config.density != 0) {
220 // Create a bucket for this density-dependent config.
221 density_groups[CopyWithoutDensity(config_value->config)]
222 .push_back(config_value.get());
223 }
224 }
225 }
226
Shane Farmer0a5b2012017-06-22 12:24:12 -0700227 // First we check all the splits. If it doesn't match one of the splits, we leave it in the
228 // base.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 for (size_t idx = 0; idx < split_count; idx++) {
230 const SplitConstraints& split_constraint = split_constraints_[idx];
231 ResourceTable* split_table = splits_[idx].get();
232
233 // Select the values we want from this entry for this split.
234 SplitValueSelector selector(split_constraint);
235 std::vector<ResourceConfigValue*> selected_values =
236 selector.SelectValues(density_groups, &config_claimed_map);
237
238 // No need to do any work if we selected nothing.
239 if (!selected_values.empty()) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700240 // Create the same resource structure in the split. We do this lazily because we might
241 // not have actual values for each type/entry.
242 ResourceTablePackage* split_pkg = split_table->FindPackage(pkg->name);
243 ResourceTableType* split_type = split_pkg->FindOrCreateType(type->type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 if (!split_type->id) {
245 split_type->id = type->id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800246 split_type->visibility_level = type->visibility_level;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 }
248
Shane Farmer0a5b2012017-06-22 12:24:12 -0700249 ResourceEntry* split_entry = split_type->FindOrCreateEntry(entry->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 if (!split_entry->id) {
251 split_entry->id = entry->id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800252 split_entry->visibility = entry->visibility;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800253 split_entry->overlayable_item = entry->overlayable_item;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 }
255
256 // Copy the selected values into the new Split Entry.
257 for (ResourceConfigValue* config_value : selected_values) {
258 ResourceConfigValue* new_config_value =
Shane Farmer0a5b2012017-06-22 12:24:12 -0700259 split_entry->FindOrCreateValue(config_value->config, config_value->product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 new_config_value->value = std::unique_ptr<Value>(
261 config_value->value->Clone(&split_table->string_pool));
262 }
263 }
264 }
265
Pierre Lecesne672384b2017-02-06 10:29:02 +0000266 if (!options_.preferred_densities.empty()) {
267 MarkNonPreferredDensitiesAsClaimed(options_.preferred_densities,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 density_groups,
269 &config_claimed_map);
270 }
271
Shane Farmer0a5b2012017-06-22 12:24:12 -0700272 // All splits are handled, now check to see what wasn't claimed and remove whatever exists
273 // in other splits.
274 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 if (config_value && config_claimed_map[config_value.get()]) {
276 // Claimed, remove from base.
277 config_value.reset();
278 }
279 }
280
281 // Now erase all nullptrs.
282 entry->values.erase(
283 std::remove(entry->values.begin(), entry->values.end(), nullptr),
284 entry->values.end());
285 }
286 }
287 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800288}
289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290} // namespace aapt