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
17 #include "split/TableSplitter.h"
18
19 #include <algorithm>
20 #include <map>
21 #include <set>
22 #include <unordered_set>
23 #include <unordered_map>
24 #include <vector>
25
26 #include "android-base/logging.h"
27 #include "androidfw/ConfigDescription.h"
28
29 #include "ResourceTable.h"
30 #include "trace/TraceBuffer.h"
31 #include "util/Util.h"
32
33 using ::android::ConfigDescription;
34
35 namespace aapt {
36
37 using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
38 using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
39
CopyWithoutDensity(const ConfigDescription & config)40 static ConfigDescription CopyWithoutDensity(const ConfigDescription& config) {
41 ConfigDescription without_density = config;
42 without_density.density = 0;
43 return without_density;
44 }
45
46 /**
47 * Selects values that match exactly the constraints given.
48 */
49 class SplitValueSelector {
50 public:
SplitValueSelector(const SplitConstraints & constraints)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 {
56 density_dependent_config_to_density_map_[CopyWithoutDensity(config)] = config.density;
57 }
58 }
59 }
60
SelectValues(const ConfigDensityGroups & density_groups,ConfigClaimedMap * claimed_values)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;
78 }
79 }
80 }
81
82 // 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;
95
96 ResourceConfigValue* best_value = nullptr;
97 for (ResourceConfigValue* this_value : related_values) {
98 if (!best_value || this_value->config.isBetterThan(best_value->config, &target_density)) {
99 best_value = this_value;
100 }
101 }
102 CHECK(best_value != nullptr);
103
104 // 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 }
109 }
110 return selected;
111 }
112
113 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_;
119 };
120
121 /**
122 * Marking non-preferred densities as claimed will make sure the base doesn't include them, leaving
123 * only the preferred density behind.
124 */
MarkNonPreferredDensitiesAsClaimed(const std::vector<uint16_t> & preferred_densities,const ConfigDensityGroups & density_groups,ConfigClaimedMap * config_claimed_map)125 static void MarkNonPreferredDensitiesAsClaimed(
126 const std::vector<uint16_t>& preferred_densities, 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;
131
132 // 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.
150 for (ResourceConfigValue* this_value : related_values) {
151 if (best_values.find(this_value) == best_values.end()) {
152 (*config_claimed_map)[this_value] = true;
153 }
154 }
155 }
156 }
VerifySplitConstraints(IAaptContext * context)157 bool TableSplitter::VerifySplitConstraints(IAaptContext* context) {
158 TRACE_CALL();
159 bool error = false;
160 for (size_t i = 0; i < split_constraints_.size(); i++) {
161 if (split_constraints_[i].configs.size() == 0) {
162 // For now, treat this as a warning. We may consider aborting processing.
163 context->GetDiagnostics()->Warn(android::DiagMessage() << "no configurations for constraint '"
164 << split_constraints_[i].name << "'");
165 }
166 for (size_t j = i + 1; j < split_constraints_.size(); j++) {
167 for (const ConfigDescription& config : split_constraints_[i].configs) {
168 if (split_constraints_[j].configs.find(config) != split_constraints_[j].configs.end()) {
169 context->GetDiagnostics()->Error(
170 android::DiagMessage() << "config '" << config << "' appears in multiple splits, "
171 << "target split ambiguous");
172 error = true;
173 }
174 }
175 }
176 }
177 return !error;
178 }
179
SplitTable(ResourceTable * original_table)180 void 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->FindOrCreatePackage(pkg->name);
187 }
188
189 for (auto& type : pkg->types) {
190 if (type->named_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.
198 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
199 if (!options_.config_filter->Match(config_value->config)) {
200 // null out the entry. We will clean up and remove nulls at the end for performance
201 // reasons.
202 config_value.reset();
203 }
204 }
205 }
206
207 // 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.
211 ConfigDensityGroups density_groups;
212 ConfigClaimedMap config_claimed_map;
213 for (const std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
214 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
225 // First we check all the splits. If it doesn't match one of the splits, we leave it in the
226 // base.
227 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 CloningValueTransformer cloner(&split_table->string_pool);
231
232 // Select the values we want from this entry for this split.
233 SplitValueSelector selector(split_constraint);
234 std::vector<ResourceConfigValue*> selected_values =
235 selector.SelectValues(density_groups, &config_claimed_map);
236
237 // No need to do any work if we selected nothing.
238 if (!selected_values.empty()) {
239 // Create the same resource structure in the split. We do this lazily because we might
240 // not have actual values for each type/entry.
241 ResourceTablePackage* split_pkg = split_table->FindPackage(pkg->name);
242 ResourceTableType* split_type = split_pkg->FindOrCreateType(type->named_type);
243 split_type->visibility_level = type->visibility_level;
244
245 ResourceEntry* split_entry = split_type->FindOrCreateEntry(entry->name);
246 if (!split_entry->id) {
247 split_entry->id = entry->id;
248 split_entry->visibility = entry->visibility;
249 split_entry->overlayable_item = entry->overlayable_item;
250 }
251
252 // Copy the selected values into the new Split Entry.
253 for (ResourceConfigValue* config_value : selected_values) {
254 ResourceConfigValue* new_config_value =
255 split_entry->FindOrCreateValue(config_value->config, config_value->product);
256 new_config_value->value = config_value->value->Transform(cloner);
257 }
258 }
259 }
260
261 if (!options_.preferred_densities.empty()) {
262 MarkNonPreferredDensitiesAsClaimed(options_.preferred_densities,
263 density_groups,
264 &config_claimed_map);
265 }
266
267 // 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) {
270 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 }
283 }
284
285 } // namespace aapt
286