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

Commit c07330f6 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

InputManagerService: Reload pointer icons only when density changes

Reloading pointer icons is a non-trivial task that requires I/O
for reading icons from files. Reduce the number of times icons need to
be reloaded by only reloading icons when the display density changes.
Other display changes, such as orientation changes, do not need the
pointer icons to be reloaded.

Bug: 321324470
Bug: 325133368
Test: adb shell wm density X
Change-Id: I9e502f3ba054da0b74f8006f9efcd53f3552b181
parent 4a18b507
Loading
Loading
Loading
Loading
+41 −5
Original line number Diff line number Diff line
@@ -86,7 +86,9 @@ import android.util.Log;
import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.view.Display;
import android.view.DisplayInfo;
import android.view.IInputFilter;
import android.view.IInputFilterHost;
import android.view.IInputMonitorHost;
@@ -415,11 +417,15 @@ public class InputManagerService extends IInputManager.Stub
    boolean mUseLargePointerIcons = false;
    @GuardedBy("mLoadedPointerIconsByDisplayAndType")
    final SparseArray<Context> mDisplayContexts = new SparseArray<>();
    @GuardedBy("mLoadedPointerIconsByDisplayAndType")
    final SparseIntArray mDisplayDensities = new SparseIntArray();

    final DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {

            synchronized (mLoadedPointerIconsByDisplayAndType) {
                updateDisplayDensity(displayId);
            }
        }

        @Override
@@ -427,14 +433,20 @@ public class InputManagerService extends IInputManager.Stub
            synchronized (mLoadedPointerIconsByDisplayAndType) {
                mLoadedPointerIconsByDisplayAndType.remove(displayId);
                mDisplayContexts.remove(displayId);
                mDisplayDensities.delete(displayId);
            }
        }

        @Override
        public void onDisplayChanged(int displayId) {
            synchronized (mLoadedPointerIconsByDisplayAndType) {
                // The display density could have changed, so force all cached pointer icons to be
                if (!updateDisplayDensity(displayId)) {
                    return;
                }
                // The display density changed, so force all cached pointer icons to be
                // reloaded for the display.
                Slog.i(TAG,
                        "Reloading pointer icons due to density change on display: " + displayId);
                var iconsByType = mLoadedPointerIconsByDisplayAndType.get(displayId);
                if (iconsByType == null) {
                    return;
@@ -444,6 +456,26 @@ public class InputManagerService extends IInputManager.Stub
            }
            mNative.reloadPointerIcons();
        }

        // Updates the cached display density for the given displayId, and returns true if
        // the cached density changed.
        @GuardedBy("mLoadedPointerIconsByDisplayAndType")
        private boolean updateDisplayDensity(int displayId) {
            final DisplayManager displayManager = Objects.requireNonNull(
                    mContext.getSystemService(DisplayManager.class));
            final Display display = displayManager.getDisplay(displayId);
            if (display == null) {
                return false;
            }
            DisplayInfo info = new DisplayInfo();
            display.getDisplayInfo(info);
            final int oldDensity = mDisplayDensities.get(displayId, 0 /* default */);
            if (oldDensity == info.logicalDensityDpi) {
                return false;
            }
            mDisplayDensities.put(displayId, info.logicalDensityDpi);
            return true;
        }
    };

    /** Point of injection for test dependencies. */
@@ -613,9 +645,13 @@ public class InputManagerService extends IInputManager.Stub
            mWiredAccessoryCallbacks.systemReady();
        }

        Objects.requireNonNull(
                mContext.getSystemService(DisplayManager.class)).registerDisplayListener(
                mDisplayListener, mHandler);
        final DisplayManager displayManager = Objects.requireNonNull(
                mContext.getSystemService(DisplayManager.class));
        displayManager.registerDisplayListener(mDisplayListener, mHandler);
        final Display[] displays = displayManager.getDisplays();
        for (int i = 0; i < displays.length; i++) {
            mDisplayListener.onDisplayAdded(displays[i].getDisplayId());
        }

        mKeyboardLayoutManager.systemRunning();
        mBatteryController.systemRunning();