blob: 78ebcb97b8111c149a84cae2064f252b4aac77cd [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"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080023#include "trace/TraceBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020025using android::ConfigDescription;
26
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070027namespace aapt {
28
29namespace {
30
31/**
32 * Remove duplicated key-value entries from dominated resources.
33 *
34 * Based on the dominator tree, we can remove a value of an entry if:
35 *
36 * 1. The configuration for the entry's value is dominated by a configuration
37 * with an equivalent entry value.
38 * 2. All compatible configurations for the entry (those not in conflict and
39 * unrelated by domination with the configuration for the entry's value) have
40 * an equivalent entry value.
41 */
42class DominatedKeyValueRemover : public DominatorTree::BottomUpVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 public:
44 using Node = DominatorTree::Node;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 explicit DominatedKeyValueRemover(IAaptContext* context, ResourceEntry* entry)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 : context_(context), entry_(entry) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 void VisitConfig(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 Node* parent = node->parent();
51 if (!parent) {
52 return;
53 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 ResourceConfigValue* node_value = node->value();
55 ResourceConfigValue* parent_value = parent->value();
56 if (!node_value || !parent_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return;
58 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 if (!node_value->value->Equals(parent_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070061 }
62
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 // Compare compatible configs for this entry and ensure the values are
64 // equivalent.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 const ConfigDescription& node_configuration = node_value->config;
66 for (const auto& sibling : entry_->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 if (!sibling->value) {
68 // Sibling was already removed.
69 continue;
70 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 if (node_configuration.IsCompatibleWith(sibling->config) &&
72 !node_value->value->Equals(sibling->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 // The configurations are compatible, but the value is
74 // different, so we can't remove this value.
75 return;
76 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070077 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 if (context_->IsVerbose()) {
79 context_->GetDiagnostics()->Note(
80 DiagMessage(node_value->value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 << "removing dominated duplicate resource with name \""
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 << entry_->name << "\"");
Adam Lesinski8f7c5502017-03-02 17:45:01 -080083 context_->GetDiagnostics()->Note(
84 DiagMessage(parent_value->value->GetSource()) << "dominated here");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 node_value->value = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 DISALLOW_COPY_AND_ASSIGN(DominatedKeyValueRemover);
91
92 IAaptContext* context_;
93 ResourceEntry* entry_;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070094};
95
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096static void DedupeEntry(IAaptContext* context, ResourceEntry* entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 DominatorTree tree(entry->values);
98 DominatedKeyValueRemover remover(context, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 tree.Accept(&remover);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 // Erase the values that were removed.
102 entry->values.erase(
103 std::remove_if(
104 entry->values.begin(), entry->values.end(),
105 [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
106 return val == nullptr || val->value == nullptr;
107 }),
108 entry->values.end());
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700109}
110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113bool ResourceDeduper::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800114 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 for (auto& package : table->packages) {
116 for (auto& type : package->types) {
117 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 DedupeEntry(context, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700120 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 }
122 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700123}
124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125} // namespace aapt