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

Commit c797e0c6 authored by Tiger Huang's avatar Tiger Huang
Browse files

Refine logic about compat scale

1. Make mGlobalScale and mInvGlobalScale always up-to-date. It was only
   updated before layout previously. We won't do layout at the server
   side then. So we update the scale when the upstream factors change.

2. Because of 1., we can return WindowState#getCompatInsetsState from
   addWindow and don't need to obtain it from getLayoutHint.

3. The logic in getLayoutHint didn't consider the overrideScale. This CL
   fixes it, and moves the logic to the only caller. We don't need
   getLayoutHint anymore.

Bug: 161810301
Test: Use the following commands to test apps:
  adb shell am compat enable DOWNSCALED $packageName
  adb shell am compat enable DOWNSCALE_70 $packageName
Change-Id: I67430cc61a6c086a8f232c711f63256523c77bb4
parent e45f1e5f
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -7364,10 +7364,14 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A

    @VisibleForTesting
    void clearSizeCompatMode() {
        final float lastSizeCompatScale = mSizeCompatScale;
        mInSizeCompatModeForBounds = false;
        mSizeCompatScale = 1f;
        mSizeCompatBounds = null;
        mCompatDisplayInsets = null;
        if (mSizeCompatScale != lastSizeCompatScale) {
            forAllWindows(WindowState::updateGlobalScale, false /* traverseTopToBottom */);
        }

        // Clear config override in #updateCompatDisplayInsets().
        onRequestedOverrideConfigurationChanged(EMPTY);
@@ -7925,6 +7929,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
        final int contentH = resolvedAppBounds.height();
        final int viewportW = containerAppBounds.width();
        final int viewportH = containerAppBounds.height();
        final float lastSizeCompatScale = mSizeCompatScale;
        // Only allow to scale down.
        mSizeCompatScale = (contentW <= viewportW && contentH <= viewportH)
                ? 1f : Math.min((float) viewportW / contentW, (float) viewportH / contentH);
@@ -7943,6 +7948,9 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
        } else {
            mSizeCompatBounds = null;
        }
        if (mSizeCompatScale != lastSizeCompatScale) {
            forAllWindows(WindowState::updateGlobalScale, false /* traverseTopToBottom */);
        }

        // Vertically center within parent (bounds) - this is a UX choice and exclude the horizontal
        // decor if needed. Horizontal position is adjusted in
+0 −2
Original line number Diff line number Diff line
@@ -840,7 +840,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
            }
            w.mSurfacePlacementNeeded = true;
            w.mLayoutNeeded = false;
            w.prelayout();
            final boolean firstLayout = !w.isLaidOut();
            getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
            w.mLayoutSeq = mLayoutSeq;
@@ -883,7 +882,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
            }
            w.mSurfacePlacementNeeded = true;
            w.mLayoutNeeded = false;
            w.prelayout();
            getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
            w.mLayoutSeq = mLayoutSeq;
            if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrame()
+0 −27
Original line number Diff line number Diff line
@@ -1432,33 +1432,6 @@ public class DisplayPolicy {
        return mForceShowSystemBars;
    }

    // TODO: Should probably be moved into DisplayFrames.
    /**
     * Return the layout hints for a newly added window. These values are computed on the
     * most recent layout, so they are not guaranteed to be correct.
     *
     * @param attrs The LayoutParams of the window.
     * @param windowToken The token of the window.
     * @param outInsetsState The insets state of this display from the client's perspective.
     * @param localClient Whether the client is from the our process.
     * @return Whether to always consume the system bars.
     *         See {@link #areSystemBarsForcedShownLw()}.
     */
    boolean getLayoutHint(LayoutParams attrs, WindowToken windowToken, InsetsState outInsetsState,
            boolean localClient) {
        final InsetsState state =
                mDisplayContent.getInsetsPolicy().getInsetsForWindowMetrics(attrs);
        final boolean hasCompatScale = WindowState.hasCompatScale(attrs, windowToken);
        outInsetsState.set(state, hasCompatScale || localClient);
        if (hasCompatScale) {
            final float compatScale = windowToken != null
                    ? windowToken.getSizeCompatScale()
                    : mDisplayContent.mCompatibleScreenScale;
            outInsetsState.scale(1f / compatScale);
        }
        return mForceShowSystemBars;
    }

    /**
     * Computes the frames of display (its logical size, rotation and cutout should already be set)
     * used to layout window. This method only changes the given display frames, insets state and
+17 −5
Original line number Diff line number Diff line
@@ -1794,8 +1794,7 @@ public class WindowManagerService extends IWindowManager.Stub
                prepareNoneTransitionForRelaunching(activity);
            }

            if (displayPolicy.getLayoutHint(win.mAttrs, token, outInsetsState,
                    win.isClientLocal())) {
            if (displayPolicy.areSystemBarsForcedShownLw()) {
                res |= WindowManagerGlobal.ADD_FLAG_ALWAYS_CONSUME_SYSTEM_BARS;
            }

@@ -1842,6 +1841,7 @@ public class WindowManagerService extends IWindowManager.Stub
            displayContent.getInsetsStateController().updateAboveInsetsState(
                    win, false /* notifyInsetsChanged */);

            outInsetsState.set(win.getCompatInsetsState(), win.isClientLocal());
            getInsetsSourceControls(win, outActiveControls);
        }

@@ -8472,6 +8472,7 @@ public class WindowManagerService extends IWindowManager.Stub
    public boolean getWindowInsets(WindowManager.LayoutParams attrs, int displayId,
            InsetsState outInsetsState) {
        final boolean fromLocal = Binder.getCallingPid() == myPid();
        final int uid = Binder.getCallingUid();
        final long origId = Binder.clearCallingIdentity();
        try {
            synchronized (mGlobalLock) {
@@ -8480,9 +8481,20 @@ public class WindowManagerService extends IWindowManager.Stub
                    throw new WindowManager.InvalidDisplayException("Display#" + displayId
                            + "could not be found!");
                }
                final WindowToken windowToken = dc.getWindowToken(attrs.token);
                return dc.getDisplayPolicy().getLayoutHint(attrs, windowToken, outInsetsState,
                        fromLocal);
                final WindowToken token = dc.getWindowToken(attrs.token);
                final float overrideScale = mAtmService.mCompatModePackages.getCompatScale(
                        attrs.packageName, uid);
                final InsetsState state = dc.getInsetsPolicy().getInsetsForWindowMetrics(attrs);
                final boolean hasCompatScale =
                        WindowState.hasCompatScale(attrs, token, overrideScale);
                outInsetsState.set(state, hasCompatScale || fromLocal);
                if (hasCompatScale) {
                    final float compatScale = token != null && token.hasSizeCompatBounds()
                            ? token.getSizeCompatScale() * overrideScale
                            : overrideScale;
                    outInsetsState.scale(1f / compatScale);
                }
                return dc.getDisplayPolicy().areSystemBarsForcedShownLw();
            }
        } finally {
            Binder.restoreCallingIdentity(origId);
+31 −24
Original line number Diff line number Diff line
@@ -444,9 +444,8 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP

    // Current transformation being applied.
    float mGlobalScale=1;
    float mLastGlobalScale=1;
    float mInvGlobalScale=1;
    float mOverrideScale = 1;
    final float mOverrideScale;
    float mHScale=1, mVScale=1;
    float mLastHScale=1, mLastVScale=1;

@@ -1148,6 +1147,7 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
            mSubLayer = 0;
            mWinAnimator = null;
            mWpcForDisplayAreaConfigChanges = null;
            mOverrideScale = 1f;
            return;
        }
        mDeathRecipient = deathRecipient;
@@ -1195,6 +1195,7 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
        mLayer = 0;
        mOverrideScale = mWmService.mAtmService.mCompatModePackages.getCompatScale(
                mAttrs.packageName, s.mUid);
        updateGlobalScale();

        // Make sure we initial all fields before adding to parentWindow, to prevent exception
        // during onDisplayChanged.
@@ -1224,6 +1225,23 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
        mSession.windowAddedLocked();
    }

    boolean updateGlobalScale() {
        if (hasCompatScale()) {
            if (mOverrideScale != 1f) {
                mGlobalScale = mToken.hasSizeCompatBounds()
                        ? mToken.getSizeCompatScale() * mOverrideScale
                        : mOverrideScale;
            } else {
                mGlobalScale = mToken.getSizeCompatScale();
            }
            mInvGlobalScale = 1f / mGlobalScale;
            return true;
        }

        mGlobalScale = mInvGlobalScale = 1f;
        return false;
    }

    /**
     * @return {@code true} if the application runs in size compatibility mode or has an app level
     * scaling override set.
@@ -1232,7 +1250,7 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
     * @see ActivityRecord#hasSizeCompatBounds()
     */
    boolean hasCompatScale() {
        return mOverrideScale != 1f || hasCompatScale(mAttrs, mActivityRecord);
        return hasCompatScale(mAttrs, mActivityRecord, mOverrideScale);
    }

    /**
@@ -1240,11 +1258,16 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
     * @see android.content.res.CompatibilityInfo#supportsScreen
     * @see ActivityRecord#hasSizeCompatBounds()
     */
    static boolean hasCompatScale(WindowManager.LayoutParams attrs, WindowToken windowToken) {
        return (attrs.privateFlags & PRIVATE_FLAG_COMPATIBLE_WINDOW) != 0
                || (windowToken != null && windowToken.hasSizeCompatBounds()
    static boolean hasCompatScale(WindowManager.LayoutParams attrs, WindowToken token,
            float overrideScale) {
        if ((attrs.privateFlags & PRIVATE_FLAG_COMPATIBLE_WINDOW) != 0) {
            return true;
        }
        if (attrs.type == TYPE_APPLICATION_STARTING) {
            // Exclude starting window because it is not displayed by the application.
                && attrs.type != TYPE_APPLICATION_STARTING);
            return false;
        }
        return token != null && token.hasSizeCompatBounds() || overrideScale != 1f;
    }

    /**
@@ -1748,21 +1771,6 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
                && (mActivityRecord.firstWindowDrawn || mActivityRecord.startingDisplayed);
    }

    void prelayout() {
        if (hasCompatScale()) {
            if (mOverrideScale != 1f) {
                mGlobalScale = mToken.hasSizeCompatBounds()
                        ? mToken.getSizeCompatScale() * mOverrideScale
                        : mOverrideScale;
            } else {
                mGlobalScale = mToken.getSizeCompatScale();
            }
            mInvGlobalScale = 1 / mGlobalScale;
        } else {
            mGlobalScale = mInvGlobalScale = 1;
        }
    }

    @Override
    boolean hasContentToDisplay() {
        if (!mAppFreezing && isDrawn() && (mViewVisibility == View.VISIBLE
@@ -5320,7 +5328,6 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
            mLastVScale != newVScale ) {
            getPendingTransaction().setMatrix(getSurfaceControl(),
                newHScale, 0, 0, newVScale);
            mLastGlobalScale = mGlobalScale;
            mLastHScale = newHScale;
            mLastVScale = newVScale;
        }
Loading