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

Commit 38b16837 authored by Charles Chen's avatar Charles Chen
Browse files

Obtain DisplayMetrics from DisplaymanagerGlobal directly

Previously we use cache to store value returned from
DisplayManagerGlobal#getAdjustedDisplay(int, DisplayAdjustments)
in RM#getAdjustedDisplay(int, DisplayAdjustments).
Since RM#getAdjustedDisplay(int, DisplayAdjustments) is just
used in RM#getDisplayMetrics(int, DisplayAdjustments), we can
furthur call getDisplayInfo(int) from DisplayManagerGlobal and call
DisplayInfo#getAppMetrics to obtain DisplayMetrics directly.

Also, #getDisplayInfo has binder cache, so we don't need another cache
to store returned DisplayMetrics value from DisplayManagerGlobal.

Before this CL
[1/1] android.app.ResourcesManagerPerfTest#getDisplayMetrics: PASSED
(10.844s)
	getDisplayMetrics_median: 1840
	perfetto_file_path:
/sdcard/test_results/android.app.ResourcesManagerPerfTest_getDisplayMetrics/PerfettoListener/perfetto_android.app.ResourcesManagerPerfTest_getDisplayMetrics-1.pb
	getDisplayMetrics_mean: 1857
	getDisplayMetrics_min: 1817
	getDisplayMetrics_standardDeviation: 35

After this CL,
com.android.perftests.core (1 Test)
[1/1] android.app.ResourcesManagerPerfTest#getDisplayMetrics: PASSED
(11.185s)
	getDisplayMetrics_median: 733
	perfetto_file_path:
/sdcard/test_results/android.app.ResourcesManagerPerfTest_getDisplayMetrics/PerfettoListener/perfetto_android.app.ResourcesManagerPerfTest_getDisplayMetrics-1.pb
	getDisplayMetrics_mean: 733
	getDisplayMetrics_min: 725
	getDisplayMetrics_standardDeviation: 5

Test: atest android.app.ResourcesManagerPerfTest#getDisplayMetrics
fixes: 191662456

Change-Id: I934ecae19431800a9a4e0f3e3821e80ef4b5d8cf
parent f137ba98
Loading
Loading
Loading
Loading
+7 −54
Original line number Diff line number Diff line
@@ -42,10 +42,10 @@ import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;
import android.view.Display;
import android.view.DisplayAdjustments;
import android.view.DisplayInfo;
import android.window.WindowContext;

import com.android.internal.annotations.VisibleForTesting;
@@ -56,7 +56,6 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
@@ -250,12 +249,6 @@ public class ResourcesManager {
    private final WeakHashMap<IBinder, ActivityResources> mActivityResourceReferences =
            new WeakHashMap<>();

    /**
     * A cache of DisplayId, DisplayAdjustments to Display.
     */
    private final ArrayMap<Pair<Integer, DisplayAdjustments>, SoftReference<Display>>
            mAdjustedDisplays = new ArrayMap<>();

    /**
     * Callback implementation for handling updates to Resources objects.
     */
@@ -331,10 +324,12 @@ public class ResourcesManager {
     */
    @VisibleForTesting
    protected @NonNull DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments da) {
        DisplayMetrics dm = new DisplayMetrics();
        final Display display = getAdjustedDisplay(displayId, da);
        if (display != null) {
            display.getMetrics(dm);
        final DisplayManagerGlobal displayManagerGlobal = DisplayManagerGlobal.getInstance();
        final DisplayMetrics dm = new DisplayMetrics();
        final DisplayInfo displayInfo = displayManagerGlobal != null
                ? displayManagerGlobal.getDisplayInfo(displayId) : null;
        if (displayInfo != null) {
            displayInfo.getAppMetrics(dm, da);
        } else {
            dm.setToDefaults();
        }
@@ -374,45 +369,6 @@ public class ResourcesManager {
        }
    }

    /**
     * Returns an adjusted {@link Display} object based on the inputs or null if display isn't
     * available. This method is only used within {@link ResourcesManager} to calculate display
     * metrics based on a set {@link DisplayAdjustments}. All other usages should instead call
     * {@link ResourcesManager#getAdjustedDisplay(int, Resources)}.
     *
     * @param displayId display Id.
     * @param displayAdjustments display adjustments.
     */
    private Display getAdjustedDisplay(final int displayId,
            @Nullable DisplayAdjustments displayAdjustments) {
        final DisplayAdjustments displayAdjustmentsCopy = (displayAdjustments != null)
                ? new DisplayAdjustments(displayAdjustments) : new DisplayAdjustments();
        final Pair<Integer, DisplayAdjustments> key =
                Pair.create(displayId, displayAdjustmentsCopy);
        SoftReference<Display> sd;
        synchronized (mLock) {
            sd = mAdjustedDisplays.get(key);
        }
        if (sd != null) {
            final Display display = sd.get();
            if (display != null) {
                return display;
            }
        }
        final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
        if (dm == null) {
            // may be null early in system startup
            return null;
        }
        final Display display = dm.getCompatibleDisplay(displayId, key.second);
        if (display != null) {
            synchronized (mLock) {
                mAdjustedDisplays.put(key, new SoftReference<>(display));
            }
        }
        return display;
    }

    /**
     * Returns an adjusted {@link Display} object based on the inputs or null if display isn't
     * available.
@@ -1332,9 +1288,6 @@ public class ResourcesManager {
                    return false;
                }

                // Things might have changed in display manager, so clear the cached displays.
                mAdjustedDisplays.clear();

                int changes = mResConfiguration.updateFrom(config);
                if (compat != null && (mResCompatibilityInfo == null
                        || !mResCompatibilityInfo.equals(compat))) {