[res] Add a Theme::Rebase() benchmark + fix data

- Create a new benchmark targeting the Theme::Rebase() method
  specifically

- Fix the benchmark data so it doesn't try to run every data
  apk as a test on the device, and include the missing projects

- A minor style improvement

Flag: NONE A set of benchmark improvements
Bug: 345562237
Test: build, boot, atest + performance tests

Change-Id: If9d92a288ec9513ef9cefbb77490a49d4fb8d596
diff --git a/libs/androidfw/tests/AndroidTest_Benchmarks.xml b/libs/androidfw/tests/AndroidTest_Benchmarks.xml
new file mode 100644
index 0000000..e61e46f
--- /dev/null
+++ b/libs/androidfw/tests/AndroidTest_Benchmarks.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2024 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs libandroidfw_benchmarks and libandroidfw_tests.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native-metric" />
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push" value="libandroidfw_benchmarks->/data/local/tmp/libandroidfw_benchmarks" />
+    </target_preparer>
+    <test class="com.android.tradefed.testtype.GoogleBenchmarkTest" >
+        <option name="native-benchmark-device-path" value="/data/local/tmp" />
+        <option name="benchmark-module-name" value="libandroidfw_benchmarks" />
+        <!-- The GoogleBenchmarkTest class ordinarily expects every file in the benchmark's
+             directory (recursively) to be a google-benchmark binary, so we need this setting to
+             avoid failing on the test data files. -->
+        <option name="file-exclusion-filter-regex" value=".*\.(apk|config)$"  />
+    </test>
+</configuration>
\ No newline at end of file
diff --git a/libs/androidfw/tests/AssetManager2_bench.cpp b/libs/androidfw/tests/AssetManager2_bench.cpp
index 2caa98c..136f5ea 100644
--- a/libs/androidfw/tests/AssetManager2_bench.cpp
+++ b/libs/androidfw/tests/AssetManager2_bench.cpp
@@ -37,7 +37,7 @@
 
 static void BM_AssetManagerLoadAssets(benchmark::State& state) {
   std::string path = GetTestDataPath() + "/basic/basic.apk";
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     auto apk = ApkAssets::Load(path);
     AssetManager2 assets;
     assets.SetApkAssets({apk});
@@ -47,7 +47,7 @@
 
 static void BM_AssetManagerLoadAssetsOld(benchmark::State& state) {
   String8 path((GetTestDataPath() + "/basic/basic.apk").data());
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     AssetManager assets;
     assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
                         false /* isSystemAsset */);
@@ -60,7 +60,7 @@
 
 static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) {
   std::string path = kFrameworkPath;
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     auto apk = ApkAssets::Load(path);
     AssetManager2 assets;
     assets.SetApkAssets({apk});
@@ -70,7 +70,7 @@
 
 static void BM_AssetManagerLoadFrameworkAssetsOld(benchmark::State& state) {
   String8 path(kFrameworkPath);
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     AssetManager assets;
     assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
                         false /* isSystemAsset */);
@@ -138,7 +138,7 @@
   AssetManager2 assets;
   assets.SetApkAssets({apk});
 
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     auto bag = assets.GetBag(app::R::style::StyleTwo);
     if (!bag.has_value()) {
       state.SkipWithError("Failed to load get bag");
@@ -165,7 +165,7 @@
 
   const ResTable& table = assets.getResources(true);
 
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     const ResTable::bag_entry* bag_begin;
     const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin);
     const ResTable::bag_entry* const bag_end = bag_begin + N;
@@ -190,7 +190,7 @@
   AssetManager2 assets;
   assets.SetApkAssets({apk});
 
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     std::set<std::string> locales =
         assets.GetResourceLocales(false /*exclude_system*/, true /*merge_equivalent_languages*/);
     benchmark::DoNotOptimize(locales);
@@ -208,7 +208,7 @@
 
   const ResTable& table = assets.getResources(true);
 
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     Vector<String8> locales;
     table.getLocales(&locales, true /*includeSystemLocales*/, true /*mergeEquivalentLangs*/);
     benchmark::DoNotOptimize(locales);
@@ -231,7 +231,7 @@
   std::vector<ResTable_config> configs;
   configs.push_back(config);
 
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     configs[0].sdkVersion = ~configs[0].sdkVersion;
     assets.SetConfigurations(configs);
   }
@@ -251,7 +251,7 @@
   ResTable_config config;
   memset(&config, 0, sizeof(config));
 
-  while (state.KeepRunning()) {
+  for (auto&& _ : state) {
     config.sdkVersion = ~config.sdkVersion;
     assets.setConfiguration(config);
   }
diff --git a/libs/androidfw/tests/BenchmarkHelpers.cpp b/libs/androidfw/tests/BenchmarkHelpers.cpp
index 8b883f4..e3fc0a0 100644
--- a/libs/androidfw/tests/BenchmarkHelpers.cpp
+++ b/libs/androidfw/tests/BenchmarkHelpers.cpp
@@ -28,7 +28,7 @@
   for (const std::string& path : paths) {
     if (!assetmanager.addAssetPath(String8(path.c_str()), nullptr /* cookie */,
                                    false /* appAsLib */, false /* isSystemAssets */)) {
-      state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
+      state.SkipWithError(base::StringPrintf("Failed to old-load assets %s", path.c_str()).c_str());
       return;
     }
   }
@@ -57,7 +57,7 @@
   for (const std::string& path : paths) {
     auto apk = ApkAssets::Load(path);
     if (apk == nullptr) {
-      state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
+      state.SkipWithError(base::StringPrintf("Failed to new-load assets %s", path.c_str()).c_str());
       return;
     }
     apk_assets.push_back(std::move(apk));
diff --git a/libs/androidfw/tests/Theme_bench.cpp b/libs/androidfw/tests/Theme_bench.cpp
index dfbb5a7..bf89617 100644
--- a/libs/androidfw/tests/Theme_bench.cpp
+++ b/libs/androidfw/tests/Theme_bench.cpp
@@ -27,6 +27,10 @@
 constexpr const static uint32_t kStyleId = 0x01030237u;  // android:style/Theme.Material.Light
 constexpr const static uint32_t kAttrId = 0x01010030u;   // android:attr/colorForeground
 
+constexpr const static uint32_t kStyle2Id = 0x01030224u;  // android:style/Theme.Material
+constexpr const static uint32_t kStyle3Id = 0x0103024du;  // android:style/Widget.Material
+constexpr const static uint32_t kStyle4Id = 0x0103028eu;  // android:style/Widget.Material.Light
+
 static void BM_ThemeApplyStyleFramework(benchmark::State& state) {
   auto apk = ApkAssets::Load(kFrameworkPath);
   if (apk == nullptr) {
@@ -61,6 +65,32 @@
 }
 BENCHMARK(BM_ThemeApplyStyleFrameworkOld);
 
+static void BM_ThemeRebaseFramework(benchmark::State& state) {
+  auto apk = ApkAssets::Load(kFrameworkPath);
+  if (apk == nullptr) {
+    state.SkipWithError("Failed to load assets");
+    return;
+  }
+
+  AssetManager2 assets;
+  assets.SetApkAssets({apk});
+
+  // Create two arrays of styles to switch between back and forth.
+  const uint32_t styles1[] = {kStyle2Id, kStyleId, kStyle3Id};
+  const uint8_t force1[std::size(styles1)] = {false, true, false};
+  const uint32_t styles2[] = {kStyleId, kStyle2Id, kStyle4Id, kStyle3Id};
+  const uint8_t force2[std::size(styles2)] = {false, true, true, false};
+  const auto theme = assets.NewTheme();
+  // Initialize the theme to make the first iteration the same as the rest.
+  theme->Rebase(&assets, styles1, force1, std::size(force1));
+
+  while (state.KeepRunning()) {
+    theme->Rebase(&assets, styles2, force2, std::size(force2));
+    theme->Rebase(&assets, styles1, force1, std::size(force1));
+  }
+}
+BENCHMARK(BM_ThemeRebaseFramework);
+
 static void BM_ThemeGetAttribute(benchmark::State& state) {
   auto apk = ApkAssets::Load(kFrameworkPath);