Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 17 | #include "optimize/ResourceDeduper.h" |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include "DominatorTree.h" |
| 22 | #include "ResourceTable.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 23 | #include "trace/TraceBuffer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | using android::ConfigDescription; |
| 26 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
| 29 | namespace { |
| 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 | */ |
| 42 | class DominatedKeyValueRemover : public DominatorTree::BottomUpVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | public: |
| 44 | using Node = DominatorTree::Node; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | explicit DominatedKeyValueRemover(IAaptContext* context, ResourceEntry* entry) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | : context_(context), entry_(entry) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | void VisitConfig(Node* node) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | Node* parent = node->parent(); |
| 51 | if (!parent) { |
| 52 | return; |
| 53 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | ResourceConfigValue* node_value = node->value(); |
| 55 | ResourceConfigValue* parent_value = parent->value(); |
| 56 | if (!node_value || !parent_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | return; |
| 58 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | if (!node_value->value->Equals(parent_value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | return; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | // Compare compatible configs for this entry and ensure the values are |
| 64 | // equivalent. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | const ConfigDescription& node_configuration = node_value->config; |
Ryan Mitchell | f64b9d8 | 2019-08-13 15:41:49 -0700 | [diff] [blame] | 66 | for (const auto& sibling : parent->children()) { |
| 67 | ResourceConfigValue* sibling_value = sibling->value(); |
| 68 | if (!sibling_value->value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | // Sibling was already removed. |
| 70 | continue; |
| 71 | } |
Ryan Mitchell | f64b9d8 | 2019-08-13 15:41:49 -0700 | [diff] [blame] | 72 | if (node_configuration.IsCompatibleWith(sibling_value->config) && |
| 73 | !node_value->value->Equals(sibling_value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | // The configurations are compatible, but the value is |
| 75 | // different, so we can't remove this value. |
| 76 | return; |
| 77 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 78 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | if (context_->IsVerbose()) { |
| 80 | context_->GetDiagnostics()->Note( |
| 81 | DiagMessage(node_value->value->GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | << "removing dominated duplicate resource with name \"" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | << entry_->name << "\""); |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 84 | context_->GetDiagnostics()->Note( |
| 85 | DiagMessage(parent_value->value->GetSource()) << "dominated here"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | node_value->value = {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | DISALLOW_COPY_AND_ASSIGN(DominatedKeyValueRemover); |
| 92 | |
| 93 | IAaptContext* context_; |
| 94 | ResourceEntry* entry_; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | static void DedupeEntry(IAaptContext* context, ResourceEntry* entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | DominatorTree tree(entry->values); |
| 99 | DominatedKeyValueRemover remover(context, entry); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | tree.Accept(&remover); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | // Erase the values that were removed. |
| 103 | entry->values.erase( |
| 104 | std::remove_if( |
| 105 | entry->values.begin(), entry->values.end(), |
| 106 | [](const std::unique_ptr<ResourceConfigValue>& val) -> bool { |
| 107 | return val == nullptr || val->value == nullptr; |
| 108 | }), |
| 109 | entry->values.end()); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | } // namespace |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | bool ResourceDeduper::Consume(IAaptContext* context, ResourceTable* table) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 115 | TRACE_CALL(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | for (auto& package : table->packages) { |
| 117 | for (auto& type : package->types) { |
| 118 | for (auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | DedupeEntry(context, entry.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 121 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | } |
| 123 | return true; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | } // namespace aapt |