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

Commit 5281b6b4 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Add Context.getDisplayId() to avoid possible IPC

ContextImpl has an internal rule that when ContextImpl#mDisplay is
null the Context is associated with the default display.  The problem
is that, as discussed in Bug 117709581, when ContextImpl#mDisplay is
null ContextImpl#getDisplay() tries to get some non-null Display
object by making an IPC to the system server, which is redundant when
the display ID is the only thing that the caller wants to know.

By having an @hide method Context.getDisplayId(), we can ensure that
display ID can be obtained without any IPC.  This enables us to
re-submit my CL [1] that aimed to instantiate InputMethodManager (IMM)
for each display but then got reverted due to a performance regression
(Bug 117434607).

There should be no developer-observable behavior change.

 [1]: I7242e765426353672823fcc8277f20ac361930d7
      c53d78e9

Fix: 117712745
Test: atest FrameworksCoreTests:android.content.ContextTest
Test: prebuilts/checkstyle/checkstyle.py -f \
      frameworks/base/core/tests/coretests/src/android/content/ContextTest.java
Change-Id: I2534530a5ce90e2620c5039d793a6454a0a1e154
parent 2d3972e2
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -5054,7 +5054,7 @@ public final class ActivityThread extends ClientTransactionHandler {
    private void performConfigurationChangedForActivity(ActivityClientRecord r,
            Configuration newBaseConfig) {
        performConfigurationChangedForActivity(r, newBaseConfig,
                r.activity.getDisplay().getDisplayId(), false /* movedToDifferentDisplay */);
                r.activity.getDisplayId(), false /* movedToDifferentDisplay */);
    }

    /**
@@ -5406,7 +5406,7 @@ public final class ActivityThread extends ClientTransactionHandler {
            return;
        }
        final boolean movedToDifferentDisplay = displayId != INVALID_DISPLAY
                && displayId != r.activity.getDisplay().getDisplayId();
                && displayId != r.activity.getDisplayId();

        // Perform updates.
        r.overrideConfig = overrideConfig;
+9 −7
Original line number Diff line number Diff line
@@ -2088,8 +2088,7 @@ class ContextImpl extends Context {
            ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken,
                    new UserHandle(UserHandle.getUserId(application.uid)), flags, null);

            final int displayId = mDisplay != null
                    ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
            final int displayId = getDisplayId();

            c.setResources(createResources(mActivityToken, pi, null, displayId, null,
                    getDisplayAdjustments(displayId).getCompatibilityInfo()));
@@ -2124,8 +2123,7 @@ class ContextImpl extends Context {
            ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken, user,
                    flags, null);

            final int displayId = mDisplay != null
                    ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
            final int displayId = getDisplayId();

            c.setResources(createResources(mActivityToken, pi, null, displayId, null,
                    getDisplayAdjustments(displayId).getCompatibilityInfo()));
@@ -2152,8 +2150,7 @@ class ContextImpl extends Context {
        final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, splitName,
                mActivityToken, mUser, mFlags, classLoader);

        final int displayId = mDisplay != null
                ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
        final int displayId = getDisplayId();

        context.setResources(ResourcesManager.getInstance().getResources(
                mActivityToken,
@@ -2177,7 +2174,7 @@ class ContextImpl extends Context {
        ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
                mActivityToken, mUser, mFlags, mClassLoader);

        final int displayId = mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
        final int displayId = getDisplayId();
        context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
                overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo()));
        return context;
@@ -2249,6 +2246,11 @@ class ContextImpl extends Context {
        return mDisplay;
    }

    @Override
    public int getDisplayId() {
        return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
    }

    @Override
    public void updateDisplay(int displayId) {
        mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
+8 −0
Original line number Diff line number Diff line
@@ -4981,6 +4981,14 @@ public abstract class Context {
    @UnsupportedAppUsage
    public abstract Display getDisplay();

    /**
     * Gets the display ID.
     *
     * @return display ID associated with this {@link Context}.
     * @hide
     */
    public abstract int getDisplayId();

    /**
     * @hide
     */
+8 −0
Original line number Diff line number Diff line
@@ -917,6 +917,14 @@ public class ContextWrapper extends Context {
        return mBase.getDisplay();
    }

    /**
     * @hide
     */
    @Override
    public int getDisplayId() {
        return mBase.getDisplayId();
    }

    /**
     * @hide
     */
+1 −1
Original line number Diff line number Diff line
@@ -397,7 +397,7 @@ public final class DisplayManager {
        if (display == null) {
            // TODO: We cannot currently provide any override configurations for metrics on displays
            // other than the display the context is associated with.
            final Context context = mContext.getDisplay().getDisplayId() == displayId
            final Context context = mContext.getDisplayId() == displayId
                    ? mContext : mContext.getApplicationContext();

            display = mGlobal.getCompatibleDisplay(displayId, context.getResources());
Loading