Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5ac5c9bc authored by Jeremy Meyer's avatar Jeremy Meyer
Browse files

Revert "Revert "Always select the next higher density bucket when picking resources""

This reverts commit 932970c6.

Reason for revert: roll forward with fix

Change-Id: I42ff7cdee2ed84b55b25fbbaa68525fed43ebd9b
Test: updated and ran the failing test locally to confirm fix
Fixes: 183136881
parent f0d7d734
Loading
Loading
Loading
Loading
+4 −13
Original line number Diff line number Diff line
@@ -2677,30 +2677,21 @@ bool ResTable_config::isBetterThan(const ResTable_config& o,
                // DENSITY_ANY is now dealt with. We should look to
                // pick a density bucket and potentially scale it.
                // Any density is potentially useful
                // because the system will scale it.  Scaling down
                // is generally better than scaling up.
                // because the system will scale it.  Always prefer
                // scaling down.
                int h = thisDensity;
                int l = otherDensity;
                bool bImBigger = true;
                if (l > h) {
                    int t = h;
                    h = l;
                    l = t;
                    std::swap(l, h);
                    bImBigger = false;
                }

                if (requestedDensity >= h) {
                    // requested value higher than both l and h, give h
                    return bImBigger;
                }
                if (l >= requestedDensity) {
                    // requested value lower than both l and h, give l
                    return !bImBigger;
                }
                // saying that scaling down is 2x better than up
                if (((2 * l) - requestedDensity) * h > requestedDensity * requestedDensity) {
                    return !bImBigger;
                } else {
                    // otherwise give h
                    return bImBigger;
                }
            }
+11 −4
Original line number Diff line number Diff line
@@ -27,15 +27,19 @@ namespace android {

static ResTable_config selectBest(const ResTable_config& target,
                                  const Vector<ResTable_config>& configs) {
  ResTable_config bestConfig;
  memset(&bestConfig, 0, sizeof(bestConfig));
  Vector<ResTable_config> matchedConfigs;
  const size_t configCount = configs.size();
  for (size_t i = 0; i < configCount; i++) {
    const ResTable_config& thisConfig = configs[i];
    if (!thisConfig.match(target)) {
      continue;
    if (thisConfig.match(target)) {
      matchedConfigs.add(thisConfig);
    }
  }

  ResTable_config bestConfig = matchedConfigs[0];
  const size_t matchingConfigCount = matchedConfigs.size();
  for (size_t i = 1; i < matchingConfigCount; i++) {
    const ResTable_config& thisConfig = configs[i];
    if (thisConfig.isBetterThan(bestConfig, &target)) {
      bestConfig = thisConfig;
    }
@@ -75,6 +79,9 @@ TEST(ConfigTest, shouldSelectBestDensity) {
  configs.add(buildDensityConfig(int(ResTable_config::DENSITY_HIGH) + 20));
  ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));

  configs.add(buildDensityConfig(int(ResTable_config::DENSITY_XHIGH) - 1));
  ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));

  expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH);
  configs.add(expectedBest);
  ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));