blob: ee2dfbce7c06ba0f1bfdd50b41fde8299318f547 [file] [log] [blame]
Alexandria Cornwall77788eb2016-09-06 15:16:49 -07001/*
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 Lesinskid48944a2017-02-21 14:22:30 -080017#include "optimize/ResourceDeduper.h"
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070018
19#include <algorithm>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "DominatorTree.h"
22#include "ResourceTable.h"
23
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020024using android::ConfigDescription;
25
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070026namespace aapt {
27
28namespace {
29
30/**
31 * Remove duplicated key-value entries from dominated resources.
32 *
33 * Based on the dominator tree, we can remove a value of an entry if:
34 *
35 * 1. The configuration for the entry's value is dominated by a configuration
36 * with an equivalent entry value.
37 * 2. All compatible configurations for the entry (those not in conflict and
38 * unrelated by domination with the configuration for the entry's value) have
39 * an equivalent entry value.
40 */
41class DominatedKeyValueRemover : public DominatorTree::BottomUpVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 using Node = DominatorTree::Node;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 explicit DominatedKeyValueRemover(IAaptContext* context, ResourceEntry* entry)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 : context_(context), entry_(entry) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 void VisitConfig(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 Node* parent = node->parent();
50 if (!parent) {
51 return;
52 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 ResourceConfigValue* node_value = node->value();
54 ResourceConfigValue* parent_value = parent->value();
55 if (!node_value || !parent_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return;
57 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 if (!node_value->value->Equals(parent_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070060 }
61
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 // Compare compatible configs for this entry and ensure the values are
63 // equivalent.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 const ConfigDescription& node_configuration = node_value->config;
65 for (const auto& sibling : entry_->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 if (!sibling->value) {
67 // Sibling was already removed.
68 continue;
69 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 if (node_configuration.IsCompatibleWith(sibling->config) &&
71 !node_value->value->Equals(sibling->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 // The configurations are compatible, but the value is
73 // different, so we can't remove this value.
74 return;
75 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070076 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 if (context_->IsVerbose()) {
78 context_->GetDiagnostics()->Note(
79 DiagMessage(node_value->value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 << "removing dominated duplicate resource with name \""
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 << entry_->name << "\"");
Adam Lesinski8f7c5502017-03-02 17:45:01 -080082 context_->GetDiagnostics()->Note(
83 DiagMessage(parent_value->value->GetSource()) << "dominated here");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 node_value->value = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 DISALLOW_COPY_AND_ASSIGN(DominatedKeyValueRemover);
90
91 IAaptContext* context_;
92 ResourceEntry* entry_;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070093};
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095static void DedupeEntry(IAaptContext* context, ResourceEntry* entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 DominatorTree tree(entry->values);
97 DominatedKeyValueRemover remover(context, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 tree.Accept(&remover);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 // Erase the values that were removed.
101 entry->values.erase(
102 std::remove_if(
103 entry->values.begin(), entry->values.end(),
104 [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
105 return val == nullptr || val->value == nullptr;
106 }),
107 entry->values.end());
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700108}
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112bool ResourceDeduper::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 for (auto& package : table->packages) {
114 for (auto& type : package->types) {
115 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 DedupeEntry(context, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700118 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
120 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700121}
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123} // namespace aapt