blob: cc1fc1e6910b35bde20289442ff06a9a9794e689 [file] [log] [blame]
Adam Lesinskifb6312f2016-06-28 14:40:32 -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/VersionCollapser.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070018
19#include <algorithm>
20#include <vector>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "ResourceTable.h"
23
Adam Lesinskifb6312f2016-06-28 14:40:32 -070024namespace aapt {
25
26template <typename Iterator, typename Pred>
27class FilterIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 public:
29 FilterIterator(Iterator begin, Iterator end, Pred pred = Pred())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 : current_(begin), end_(end), pred_(pred) {
31 Advance();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070033
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 bool HasNext() { return current_ != end_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 Iterator NextIter() {
37 Iterator iter = current_;
38 ++current_;
39 Advance();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return iter;
41 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 typename Iterator::reference Next() { return *NextIter(); }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 void Advance() {
47 for (; current_ != end_; ++current_) {
48 if (pred_(*current_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 return;
50 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070051 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 Iterator current_, end_;
55 Pred pred_;
Adam Lesinskifb6312f2016-06-28 14:40:32 -070056};
57
58template <typename Iterator, typename Pred>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin,
60 Iterator end = Iterator(),
61 Pred pred = Pred()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return FilterIterator<Iterator, Pred>(begin, end, pred);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070063}
64
65/**
Shane Farmerefe45392017-08-21 14:39:28 -070066 * Every Configuration with an SDK version specified that is less than minSdk will be removed. The
67 * exception is when there is no exact matching resource for the minSdk. The next smallest one will
68 * be kept.
Adam Lesinskifb6312f2016-06-28 14:40:32 -070069 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 // First look for all sdks less than minSdk.
72 for (auto iter = entry->values.rbegin(); iter != entry->values.rend();
73 ++iter) {
74 // Check if the item was already marked for removal.
75 if (!(*iter)) {
76 continue;
Adam Lesinskifb6312f2016-06-28 14:40:32 -070077 }
78
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 const ConfigDescription& config = (*iter)->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 if (config.sdkVersion <= min_sdk) {
Shane Farmerefe45392017-08-21 14:39:28 -070081 // This is the first configuration we've found with a smaller or equal SDK level to the
82 // minimum. We MUST keep this one, but remove all others we find, which get overridden by this
83 // one.
Adam Lesinski87675ad2016-07-15 17:03:03 -070084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
87 // Check that the value hasn't already been marked for removal.
88 if (!val) {
89 return false;
Adam Lesinski87675ad2016-07-15 17:03:03 -070090 }
Adam Lesinski87675ad2016-07-15 17:03:03 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 // Only return Configs that differ in SDK version.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 config_without_sdk.sdkVersion = val->config.sdkVersion;
94 return config_without_sdk == val->config &&
95 val->config.sdkVersion <= min_sdk;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 };
97
98 // Remove the rest that match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 auto filter_iter =
100 make_filter_iterator(iter + 1, entry->values.rend(), pred);
101 while (filter_iter.HasNext()) {
102 filter_iter.Next() = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 }
Adam Lesinski87675ad2016-07-15 17:03:03 -0700104 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 }
106
107 // Now erase the nullptr values.
108 entry->values.erase(
109 std::remove_if(entry->values.begin(), entry->values.end(),
110 [](const std::unique_ptr<ResourceConfigValue>& val)
111 -> bool { return val == nullptr; }),
112 entry->values.end());
113
Shane Farmerefe45392017-08-21 14:39:28 -0700114 // Strip the version qualifiers for every resource with version <= minSdk. This will ensure that
115 // the resource entries are all packed together in the same ResTable_type struct and take up less
116 // space in the resources.arsc table.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 bool modified = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
119 if (config_value->config.sdkVersion != 0 &&
120 config_value->config.sdkVersion <= min_sdk) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // Override the resource with a Configuration without an SDK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 std::unique_ptr<ResourceConfigValue> new_value =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 util::make_unique<ResourceConfigValue>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 config_value->config.CopyWithoutSdkVersion(),
125 config_value->product);
126 new_value->value = std::move(config_value->value);
127 config_value = std::move(new_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128
129 modified = true;
130 }
131 }
132
133 if (modified) {
Shane Farmerefe45392017-08-21 14:39:28 -0700134 // We've modified the keys (ConfigDescription) by changing the sdkVersion to 0. We MUST re-sort
135 // to ensure ordering guarantees hold.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 std::sort(entry->values.begin(), entry->values.end(),
137 [](const std::unique_ptr<ResourceConfigValue>& a,
138 const std::unique_ptr<ResourceConfigValue>& b) -> bool {
139 return a->config.compare(b->config) < 0;
140 });
141 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700142}
143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144bool VersionCollapser::Consume(IAaptContext* context, ResourceTable* table) {
145 const int min_sdk = context->GetMinSdkVersion();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 for (auto& package : table->packages) {
147 for (auto& type : package->types) {
148 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 CollapseVersions(min_sdk, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700151 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 }
153 return true;
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700154}
155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156} // namespace aapt