blob: 7aad86fa72577ed180c4a25028746e3c73db9efd [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>
22#include <unordered_map>
23#include <vector>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "android-base/logging.h"
25
26#include "ConfigDescription.h"
27#include "ResourceTable.h"
28#include "util/Util.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080029
30namespace aapt {
31
32using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033using ConfigDensityGroups =
34 std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
Adam Lesinski355f2852016-02-13 20:26:45 -080035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036static ConfigDescription CopyWithoutDensity(const ConfigDescription& config) {
37 ConfigDescription without_density = config;
38 without_density.density = 0;
39 return without_density;
Adam Lesinski355f2852016-02-13 20:26:45 -080040}
41
42/**
43 * Selects values that match exactly the constraints given.
44 */
45class SplitValueSelector {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 public:
47 explicit SplitValueSelector(const SplitConstraints& constraints) {
48 for (const ConfigDescription& config : constraints.configs) {
49 if (config.density == 0) {
50 density_independent_configs_.insert(config);
51 } else {
52 density_dependent_config_to_density_map_[CopyWithoutDensity(config)] =
53 config.density;
54 }
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) {
95 if (!best_value ||
96 this_value->config.isBetterThan(best_value->config,
97 &target_density)) {
98 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/**
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 * Marking non-preferred densities as claimed will make sure the base doesn't
122 * include them,
Adam Lesinski355f2852016-02-13 20:26:45 -0800123 * leaving only the preferred density behind.
124 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125static void MarkNonPreferredDensitiesAsClaimed(
126 uint16_t preferred_density, const ConfigDensityGroups& density_groups,
127 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 ConfigDescription target_density = config;
133 target_density.density = preferred_density;
134 ResourceConfigValue* best_value = nullptr;
135 for (ResourceConfigValue* this_value : related_values) {
136 if (!best_value) {
137 best_value = this_value;
138 } else if (this_value->config.isBetterThan(best_value->config,
139 &target_density)) {
140 // Claim the previous value so that it is not included in the base.
141 (*config_claimed_map)[best_value] = true;
142 best_value = this_value;
143 } else {
144 // Claim this value so that it is not included in the base.
145 (*config_claimed_map)[this_value] = true;
146 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800147 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 CHECK(best_value != nullptr);
149 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800150}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151bool TableSplitter::VerifySplitConstraints(IAaptContext* context) {
152 bool error = false;
153 for (size_t i = 0; i < split_constraints_.size(); i++) {
154 for (size_t j = i + 1; j < split_constraints_.size(); j++) {
155 for (const ConfigDescription& config : split_constraints_[i].configs) {
156 if (split_constraints_[j].configs.find(config) !=
157 split_constraints_[j].configs.end()) {
158 context->GetDiagnostics()->Error(DiagMessage()
159 << "config '" << config
160 << "' appears in multiple splits, "
161 << "target split ambiguous");
162 error = true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800163 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800165 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 }
167 return !error;
Adam Lesinski355f2852016-02-13 20:26:45 -0800168}
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170void TableSplitter::SplitTable(ResourceTable* original_table) {
171 const size_t split_count = split_constraints_.size();
172 for (auto& pkg : original_table->packages) {
173 // Initialize all packages for splits.
174 for (size_t idx = 0; idx < split_count; idx++) {
175 ResourceTable* split_table = splits_[idx].get();
176 split_table->CreatePackage(pkg->name, pkg->id);
Adam Lesinski355f2852016-02-13 20:26:45 -0800177 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178
179 for (auto& type : pkg->types) {
180 if (type->type == ResourceType::kMipmap) {
181 // Always keep mipmaps.
182 continue;
183 }
184
185 for (auto& entry : type->entries) {
186 if (options_.config_filter) {
187 // First eliminate any resource that we definitely don't want.
188 for (std::unique_ptr<ResourceConfigValue>& config_value :
189 entry->values) {
190 if (!options_.config_filter->Match(config_value->config)) {
191 // null out the entry. We will clean up and remove nulls at the
192 // end for performance reasons.
193 config_value.reset();
194 }
195 }
196 }
197
198 // Organize the values into two separate buckets. Those that are
199 // density-dependent
200 // and those that are density-independent.
201 // One density technically matches all density, it's just that some
202 // densities
203 // match better. So we need to be aware of the full set of densities to
204 // make this
205 // decision.
206 ConfigDensityGroups density_groups;
207 ConfigClaimedMap config_claimed_map;
208 for (const std::unique_ptr<ResourceConfigValue>& config_value :
209 entry->values) {
210 if (config_value) {
211 config_claimed_map[config_value.get()] = false;
212
213 if (config_value->config.density != 0) {
214 // Create a bucket for this density-dependent config.
215 density_groups[CopyWithoutDensity(config_value->config)]
216 .push_back(config_value.get());
217 }
218 }
219 }
220
221 // First we check all the splits. If it doesn't match one of the splits,
222 // we
223 // leave it in the base.
224 for (size_t idx = 0; idx < split_count; idx++) {
225 const SplitConstraints& split_constraint = split_constraints_[idx];
226 ResourceTable* split_table = splits_[idx].get();
227
228 // Select the values we want from this entry for this split.
229 SplitValueSelector selector(split_constraint);
230 std::vector<ResourceConfigValue*> selected_values =
231 selector.SelectValues(density_groups, &config_claimed_map);
232
233 // No need to do any work if we selected nothing.
234 if (!selected_values.empty()) {
235 // Create the same resource structure in the split. We do this
236 // lazily because we might not have actual values for each
237 // type/entry.
238 ResourceTablePackage* split_pkg =
239 split_table->FindPackage(pkg->name);
240 ResourceTableType* split_type =
241 split_pkg->FindOrCreateType(type->type);
242 if (!split_type->id) {
243 split_type->id = type->id;
244 split_type->symbol_status = type->symbol_status;
245 }
246
247 ResourceEntry* split_entry =
248 split_type->FindOrCreateEntry(entry->name);
249 if (!split_entry->id) {
250 split_entry->id = entry->id;
251 split_entry->symbol_status = entry->symbol_status;
252 }
253
254 // Copy the selected values into the new Split Entry.
255 for (ResourceConfigValue* config_value : selected_values) {
256 ResourceConfigValue* new_config_value =
257 split_entry->FindOrCreateValue(config_value->config,
258 config_value->product);
259 new_config_value->value = std::unique_ptr<Value>(
260 config_value->value->Clone(&split_table->string_pool));
261 }
262 }
263 }
264
265 if (options_.preferred_density) {
266 MarkNonPreferredDensitiesAsClaimed(options_.preferred_density.value(),
267 density_groups,
268 &config_claimed_map);
269 }
270
271 // All splits are handled, now check to see what wasn't claimed and
272 // remove
273 // whatever exists in other splits.
274 for (std::unique_ptr<ResourceConfigValue>& config_value :
275 entry->values) {
276 if (config_value && config_claimed_map[config_value.get()]) {
277 // Claimed, remove from base.
278 config_value.reset();
279 }
280 }
281
282 // Now erase all nullptrs.
283 entry->values.erase(
284 std::remove(entry->values.begin(), entry->values.end(), nullptr),
285 entry->values.end());
286 }
287 }
288 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800289}
290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291} // namespace aapt