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

Commit 8ce76f45 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

Pointer Icon Refactor: Cache all used display contexts

The display context is used to load pointer icons. Before the
pointer icon refactor, the pointer icon could only be present on one
display, so we only cached a single context to load pointer icons.
Additionally, the cached context was not updated when the display
changed.

Update the code to lazily cache the context for all displays, and clear
the cache when the display changes or is removed.

Bug: 293587049
Test: atest InputManagerServiceTests
Change-Id: I5355d1fdde0118b3e85383e7f95475bfccc5e72d
parent 1fbbba06
Loading
Loading
Loading
Loading
+23 −35
Original line number Diff line number Diff line
@@ -170,9 +170,6 @@ public class InputManagerService extends IInputManager.Stub

    private InputMethodManagerInternal mInputMethodManagerInternal;

    // Context cache used for loading pointer resources.
    private Context mPointerIconDisplayContext;

    private final File mDoubleTouchGestureEnableFile;

    private WindowManagerCallbacks mWindowManagerCallbacks;
@@ -416,6 +413,8 @@ public class InputManagerService extends IInputManager.Stub
            new SparseArray<>();
    @GuardedBy("mLoadedPointerIconsByDisplayAndType")
    boolean mUseLargePointerIcons = false;
    @GuardedBy("mLoadedPointerIconsByDisplayAndType")
    final SparseArray<Context> mDisplayContexts = new SparseArray<>();

    final DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
        @Override
@@ -427,6 +426,7 @@ public class InputManagerService extends IInputManager.Stub
        public void onDisplayRemoved(int displayId) {
            synchronized (mLoadedPointerIconsByDisplayAndType) {
                mLoadedPointerIconsByDisplayAndType.remove(displayId);
                mDisplayContexts.remove(displayId);
            }
        }

@@ -440,6 +440,7 @@ public class InputManagerService extends IInputManager.Stub
                    return;
                }
                iconsByType.clear();
                mDisplayContexts.remove(displayId);
            }
            mNative.reloadPointerIcons();
        }
@@ -1323,11 +1324,6 @@ public class InputManagerService extends IInputManager.Stub

    /** Clean up input window handles of the given display. */
    public void onDisplayRemoved(int displayId) {
        if (mPointerIconDisplayContext != null
                && mPointerIconDisplayContext.getDisplay().getDisplayId() == displayId) {
            mPointerIconDisplayContext = null;
        }

        updateAdditionalDisplayInputProperties(displayId, AdditionalDisplayInputProperties::reset);

        // TODO(b/320763728): Rely on WindowInfosListener to determine when a display has been
@@ -2374,6 +2370,7 @@ public class InputManagerService extends IInputManager.Stub
        synchronized (mLidSwitchLock) { /* Test if blocked by lid switch lock. */ }
        synchronized (mInputMonitors) { /* Test if blocked by input monitor lock. */ }
        synchronized (mAdditionalDisplayInputPropertiesLock) { /* Test if blocked by props lock */ }
        synchronized (mLoadedPointerIconsByDisplayAndType) { /* Test if blocked by pointer lock */}
        mBatteryController.monitor();
        mNative.monitor();
    }
@@ -2777,7 +2774,7 @@ public class InputManagerService extends IInputManager.Stub
            }
            PointerIcon icon = iconsByType.get(type);
            if (icon == null) {
                icon = PointerIcon.getLoadedSystemIcon(getContextForPointerIcon(displayId), type,
                icon = PointerIcon.getLoadedSystemIcon(getContextForDisplay(displayId), type,
                        mUseLargePointerIcons);
                iconsByType.put(type, icon);
            }
@@ -2795,40 +2792,31 @@ public class InputManagerService extends IInputManager.Stub
        return sc.mNativeObject;
    }

    @GuardedBy("mLoadedPointerIconsByDisplayAndType")
    @NonNull
    private Context getContextForPointerIcon(int displayId) {
        if (mPointerIconDisplayContext != null
                && mPointerIconDisplayContext.getDisplay().getDisplayId() == displayId) {
            return mPointerIconDisplayContext;
        }

        // Create and cache context for non-default display.
        mPointerIconDisplayContext = getContextForDisplay(displayId);

        // Fall back to default display if the requested displayId does not exist.
        if (mPointerIconDisplayContext == null) {
            mPointerIconDisplayContext = getContextForDisplay(Display.DEFAULT_DISPLAY);
        }
        return mPointerIconDisplayContext;
    }

    @Nullable
    private Context getContextForDisplay(int displayId) {
        if (displayId == Display.INVALID_DISPLAY) {
            return null;
            // Fallback to using the default context.
            return mContext;
        }
        if (mContext.getDisplay().getDisplayId() == displayId) {
        if (displayId == mContext.getDisplay().getDisplayId()) {
            return mContext;
        }

        Context displayContext = mDisplayContexts.get(displayId);
        if (displayContext == null) {
            final DisplayManager displayManager = Objects.requireNonNull(
                    mContext.getSystemService(DisplayManager.class));
            final Display display = displayManager.getDisplay(displayId);
            if (display == null) {
            return null;
                // Fallback to using the default context.
                return mContext;
            }

        return mContext.createDisplayContext(display);
            displayContext = mContext.createDisplayContext(display);
            mDisplayContexts.put(displayId, displayContext);
        }
        return displayContext;
    }

    // Native callback.