blob: cd791bda250bf9e53954bc50410ea744ee801328 [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"
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
Adam Lesinskifb6312f2016-06-28 14:40:32 -070027namespace aapt {
28
29template <typename Iterator, typename Pred>
30class FilterIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 public:
32 FilterIterator(Iterator begin, Iterator end, Pred pred = Pred())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 : current_(begin), end_(end), pred_(pred) {
34 Advance();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 bool HasNext() { return current_ != end_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 Iterator NextIter() {
40 Iterator iter = current_;
41 ++current_;
42 Advance();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 return iter;
44 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 typename Iterator::reference Next() { return *NextIter(); }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 void Advance() {
50 for (; current_ != end_; ++current_) {
51 if (pred_(*current_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 return;
53 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070054 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 Iterator current_, end_;
58 Pred pred_;
Adam Lesinskifb6312f2016-06-28 14:40:32 -070059};
60
61template <typename Iterator, typename Pred>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin,
63 Iterator end = Iterator(),
64 Pred pred = Pred()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return FilterIterator<Iterator, Pred>(begin, end, pred);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070066}
67
68/**
Shane Farmerefe45392017-08-21 14:39:28 -070069 * Every Configuration with an SDK version specified that is less than minSdk will be removed. The
70 * exception is when there is no exact matching resource for the minSdk. The next smallest one will
71 * be kept.
Adam Lesinskifb6312f2016-06-28 14:40:32 -070072 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 // First look for all sdks less than minSdk.
75 for (auto iter = entry->values.rbegin(); iter != entry->values.rend();
76 ++iter) {
77 // Check if the item was already marked for removal.
78 if (!(*iter)) {
79 continue;
Adam Lesinskifb6312f2016-06-28 14:40:32 -070080 }
81
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 const ConfigDescription& config = (*iter)->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 if (config.sdkVersion <= min_sdk) {
Shane Farmerefe45392017-08-21 14:39:28 -070084 // This is the first configuration we've found with a smaller or equal SDK level to the
85 // minimum. We MUST keep this one, but remove all others we find, which get overridden by this
86 // one.
Adam Lesinski87675ad2016-07-15 17:03:03 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
90 // Check that the value hasn't already been marked for removal.
91 if (!val) {
92 return false;
Adam Lesinski87675ad2016-07-15 17:03:03 -070093 }
Adam Lesinski87675ad2016-07-15 17:03:03 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 // Only return Configs that differ in SDK version.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 config_without_sdk.sdkVersion = val->config.sdkVersion;
97 return config_without_sdk == val->config &&
98 val->config.sdkVersion <= min_sdk;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 };
100
101 // Remove the rest that match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 auto filter_iter =
103 make_filter_iterator(iter + 1, entry->values.rend(), pred);
104 while (filter_iter.HasNext()) {
105 filter_iter.Next() = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 }
Adam Lesinski87675ad2016-07-15 17:03:03 -0700107 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 }
109
110 // Now erase the nullptr values.
111 entry->values.erase(
112 std::remove_if(entry->values.begin(), entry->values.end(),
113 [](const std::unique_ptr<ResourceConfigValue>& val)
114 -> bool { return val == nullptr; }),
115 entry->values.end());
116
Shane Farmerefe45392017-08-21 14:39:28 -0700117 // Strip the version qualifiers for every resource with version <= minSdk. This will ensure that
118 // the resource entries are all packed together in the same ResTable_type struct and take up less
119 // space in the resources.arsc table.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 bool modified = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
122 if (config_value->config.sdkVersion != 0 &&
123 config_value->config.sdkVersion <= min_sdk) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 // Override the resource with a Configuration without an SDK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 std::unique_ptr<ResourceConfigValue> new_value =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 util::make_unique<ResourceConfigValue>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 config_value->config.CopyWithoutSdkVersion(),
128 config_value->product);
129 new_value->value = std::move(config_value->value);
130 config_value = std::move(new_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131
132 modified = true;
133 }
134 }
135
136 if (modified) {
Shane Farmerefe45392017-08-21 14:39:28 -0700137 // We've modified the keys (ConfigDescription) by changing the sdkVersion to 0. We MUST re-sort
138 // to ensure ordering guarantees hold.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 std::sort(entry->values.begin(), entry->values.end(),
140 [](const std::unique_ptr<ResourceConfigValue>& a,
141 const std::unique_ptr<ResourceConfigValue>& b) -> bool {
142 return a->config.compare(b->config) < 0;
143 });
144 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700145}
146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147bool VersionCollapser::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800148 TRACE_NAME("VersionCollapser::Consume");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 const int min_sdk = context->GetMinSdkVersion();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 for (auto& package : table->packages) {
151 for (auto& type : package->types) {
152 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 CollapseVersions(min_sdk, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700155 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157 return true;
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700158}
159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160} // namespace aapt