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(DiagMessage()
164                                        << "no configurations for constraint '"
165                                        << split_constraints_[i].name << "'");
166     }
167     for (size_t j = i + 1; j < split_constraints_.size(); j++) {
168       for (const ConfigDescription& config : split_constraints_[i].configs) {
169         if (split_constraints_[j].configs.find(config) != split_constraints_[j].configs.end()) {
170           context->GetDiagnostics()->Error(DiagMessage()
171                                            << "config '" << config
172                                            << "' appears in multiple splits, "
173                                            << "target split ambiguous");
174           error = true;
175         }
176       }
177     }
178   }
179   return !error;
180 }
181 
SplitTable(ResourceTable * original_table)182 void 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);
189     }
190 
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.
200           for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
201             if (!options_.config_filter->Match(config_value->config)) {
202               // null out the entry. We will clean up and remove nulls at the end for performance
203               // reasons.
204               config_value.reset();
205             }
206           }
207         }
208 
209         // 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.
213         ConfigDensityGroups density_groups;
214         ConfigClaimedMap config_claimed_map;
215         for (const std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
216           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 
227         // First we check all the splits. If it doesn't match one of the splits, we leave it in the
228         // base.
229         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()) {
240             // 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);
244             if (!split_type->id) {
245               split_type->id = type->id;
246               split_type->visibility_level = type->visibility_level;
247             }
248 
249             ResourceEntry* split_entry = split_type->FindOrCreateEntry(entry->name);
250             if (!split_entry->id) {
251               split_entry->id = entry->id;
252               split_entry->visibility = entry->visibility;
253               split_entry->overlayable_item = entry->overlayable_item;
254             }
255 
256             // Copy the selected values into the new Split Entry.
257             for (ResourceConfigValue* config_value : selected_values) {
258               ResourceConfigValue* new_config_value =
259                   split_entry->FindOrCreateValue(config_value->config, config_value->product);
260               new_config_value->value = std::unique_ptr<Value>(
261                   config_value->value->Clone(&split_table->string_pool));
262             }
263           }
264         }
265 
266         if (!options_.preferred_densities.empty()) {
267           MarkNonPreferredDensitiesAsClaimed(options_.preferred_densities,
268                                              density_groups,
269                                              &config_claimed_map);
270         }
271 
272         // 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) {
275           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   }
288 }
289 
290 }  // namespace aapt
291