1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ConfigDescription.h"
#include "ResourceTable.h"
#include "split/TableSplitter.h"
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
namespace aapt {
using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
static ConfigDescription copyWithoutDensity(const ConfigDescription& config) {
ConfigDescription withoutDensity = config;
withoutDensity.density = 0;
return withoutDensity;
}
/**
* Selects values that match exactly the constraints given.
*/
class SplitValueSelector {
public:
SplitValueSelector(const SplitConstraints& constraints) {
for (const ConfigDescription& config : constraints.configs) {
if (config.density == 0) {
mDensityIndependentConfigs.insert(config);
} else {
mDensityDependentConfigToDensityMap[copyWithoutDensity(config)] = config.density;
}
}
}
std::vector<ResourceConfigValue*> selectValues(const ConfigDensityGroups& densityGroups,
ConfigClaimedMap* claimedValues) {
std::vector<ResourceConfigValue*> selected;
// Select the regular values.
for (auto& entry : *claimedValues) {
// Check if the entry has a density.
ResourceConfigValue* configValue = entry.first;
if (configValue->config.density == 0 && !entry.second) {
// This is still available.
if (mDensityIndependentConfigs.find(configValue->config) !=
mDensityIndependentConfigs.end()) {
selected.push_back(configValue);
// Mark the entry as taken.
entry.second = true;
}
}
}
// Now examine the densities
for (auto& entry : densityGroups) {
// We do not care if the value is claimed, since density values can be
// in multiple splits.
const ConfigDescription& config = entry.first;
const std::vector<ResourceConfigValue*>& relatedValues = entry.second;
auto densityValueIter = mDensityDependentConfigToDensityMap.find(config);
if (densityValueIter != mDensityDependentConfigToDensityMap.end()) {
// Select the best one!
ConfigDescription targetDensity = config;
targetDensity.density = densityValueIter->second;
ResourceConfigValue* bestValue = nullptr;
for (ResourceConfigValue* thisValue : relatedValues) {
if (!bestValue ||
thisValue->config.isBetterThan(bestValue->config, &targetDensity)) {
bestValue = thisValue;
}
// When we select one of these, they are all claimed such that the base
// doesn't include any anymore.
(*claimedValues)[thisValue] = true;
}
assert(bestValue);
selected.push_back(bestValue);
}
}
return selected;
}
private:
std::set<ConfigDescription> mDensityIndependentConfigs;
std::map<ConfigDescription, uint16_t> mDensityDependentConfigToDensityMap;
};
/**
* Marking non-preferred densities as claimed will make sure the base doesn't include them,
* leaving only the preferred density behind.
*/
static void markNonPreferredDensitiesAsClaimed(uint16_t preferredDensity,
const ConfigDensityGroups& densityGroups,
ConfigClaimedMap* configClaimedMap) {
for (auto& entry : densityGroups) {
const ConfigDescription& config = entry.first;
const std::vector<ResourceConfigValue*>& relatedValues = entry.second;
ConfigDescription targetDensity = config;
targetDensity.density = preferredDensity;
ResourceConfigValue* bestValue = nullptr;
for (ResourceConfigValue* thisValue : relatedValues) {
if (!bestValue) {
bestValue = thisValue;
} else if (thisValue->config.isBetterThan(bestValue->config, &targetDensity)) {
// Claim the previous value so that it is not included in the base.
(*configClaimedMap)[bestValue] = true;
bestValue = thisValue;
} else {
// Claim this value so that it is not included in the base.
(*configClaimedMap)[thisValue] = true;
}
}
assert(bestValue);
}
}
bool TableSplitter::verifySplitConstraints(IAaptContext* context) {
bool error = false;
for (size_t i = 0; i < mSplitConstraints.size(); i++) {
for (size_t j = i + 1; j < mSplitConstraints.size(); j++) {
for (const ConfigDescription& config : mSplitConstraints[i].configs) {
if (mSplitConstraints[j].configs.find(config) !=
mSplitConstraints[j].configs.end()) {
context->getDiagnostics()->error(DiagMessage() << "config '" << config
<< "' appears in multiple splits, "
<< "target split ambiguous");
error = true;
}
}
}
}
return !error;
}
void TableSplitter::splitTable(ResourceTable* originalTable) {
const size_t splitCount = mSplitConstraints.size();
for (auto& pkg : originalTable->packages) {
// Initialize all packages for splits.
for (size_t idx = 0; idx < splitCount; idx++) {
ResourceTable* splitTable = mSplits[idx].get();
splitTable->createPackage(pkg->name, pkg->id);
}
for (auto& type : pkg->types) {
if (type->type == ResourceType::kMipmap) {
// Always keep mipmaps.
continue;
}
for (auto& entry : type->entries) {
if (mConfigFilter) {
// First eliminate any resource that we definitely don't want.
for (std::unique_ptr<ResourceConfigValue>& configValue : entry->values) {
if (!mConfigFilter->match(configValue->config)) {
// null out the entry. We will clean up and remove nulls at the end
// for performance reasons.
configValue.reset();
}
}
}
// Organize the values into two separate buckets. Those that are density-dependent
// and those that are density-independent.
// One density technically matches all density, it's just that some densities
// match better. So we need to be aware of the full set of densities to make this
// decision.
ConfigDensityGroups densityGroups;
ConfigClaimedMap configClaimedMap;
for (const std::unique_ptr<ResourceConfigValue>& configValue : entry->values) {
if (configValue) {
configClaimedMap[configValue.get()] = false;
if (configValue->config.density != 0) {
// Create a bucket for this density-dependent config.
densityGroups[copyWithoutDensity(configValue->config)]
.push_back(configValue.get());
}
}
}
// First we check all the splits. If it doesn't match one of the splits, we
// leave it in the base.
for (size_t idx = 0; idx < splitCount; idx++) {
const SplitConstraints& splitConstraint = mSplitConstraints[idx];
ResourceTable* splitTable = mSplits[idx].get();
// Select the values we want from this entry for this split.
SplitValueSelector selector(splitConstraint);
std::vector<ResourceConfigValue*> selectedValues =
selector.selectValues(densityGroups, &configClaimedMap);
// No need to do any work if we selected nothing.
if (!selectedValues.empty()) {
// Create the same resource structure in the split. We do this lazily
// because we might not have actual values for each type/entry.
ResourceTablePackage* splitPkg = splitTable->findPackage(pkg->name);
ResourceTableType* splitType = splitPkg->findOrCreateType(type->type);
if (!splitType->id) {
splitType->id = type->id;
splitType->symbolStatus = type->symbolStatus;
}
ResourceEntry* splitEntry = splitType->findOrCreateEntry(entry->name);
if (!splitEntry->id) {
splitEntry->id = entry->id;
splitEntry->symbolStatus = entry->symbolStatus;
}
// Copy the selected values into the new Split Entry.
for (ResourceConfigValue* configValue : selectedValues) {
ResourceConfigValue* newConfigValue = splitEntry->findOrCreateValue(
configValue->config, configValue->product);
newConfigValue->value = std::unique_ptr<Value>(
configValue->value->clone(&splitTable->stringPool));
}
}
}
if (mPreferredDensity) {
markNonPreferredDensitiesAsClaimed(mPreferredDensity.value(),
densityGroups,
&configClaimedMap);
}
// All splits are handled, now check to see what wasn't claimed and remove
// whatever exists in other splits.
for (std::unique_ptr<ResourceConfigValue>& configValue : entry->values) {
if (configValue && configClaimedMap[configValue.get()]) {
// Claimed, remove from base.
configValue.reset();
}
}
// Now erase all nullptrs.
entry->values.erase(
std::remove(entry->values.begin(), entry->values.end(), nullptr),
entry->values.end());
}
}
}
}
} // namespace aapt
|