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

Commit 0f7cc012 authored by Shawn Lin's avatar Shawn Lin
Browse files

Replace DisplayManager.getStableDisplaySize with max Display.Mode

The DisplayManager.getStableDisplaySize is not the right way to get the
size we want for screen decorations and display team is going to get rid
of it.

Instead, we use the maximum display size supported by the display as the
base size to define the cutout and rounded corner configs.

Also fixed a bug that DisplayCutoutBaseView didn't update the
shoudDrawCutout config when display change.

Bug: 230227839
Test: 1. On foldable device, fold and unfold the device and check if the
         cutout is correctly drawn.
      2. On device supporting multiple resolutions, switch resolution
         between FHD & QHD and check if the cutout is correctly drawn.
Test: atest LocalDisplayAdapterTest DisplayCutoutTest
           ScreenDecorationsTest RoundedCornersTest
           DisplayCutoutBaseViewTest
Change-Id: I7a602b5abd7d6a21d17eae4f8e99414eaf765fa5
parent e651a60d
Loading
Loading
Loading
Loading
+24 −5
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.util;

import android.content.res.Resources;
import android.view.Display;

import com.android.internal.R;

@@ -53,15 +54,33 @@ public class DisplayUtils {
    }

    /**
     * Get the display size ratio based on the stable display size.
     * Returns the Display.Mode with maximum resolution.
     */
    public static Display.Mode getMaximumResolutionDisplayMode(Display.Mode[] modes) {
        if (modes == null || modes.length == 0) {
            return null;
        }
        int maxWidth = 0;
        Display.Mode target = null;
        for (Display.Mode mode : modes) {
            if (mode.getPhysicalWidth() > maxWidth) {
                maxWidth = mode.getPhysicalWidth();
                target = mode;
            }
        }
        return target;
    }

    /**
     * Get the display size ratio based on the physical display size.
     */
    public static float getPhysicalPixelDisplaySizeRatio(
            int stableWidth, int stableHeight, int currentWidth, int currentHeight) {
        if (stableWidth == currentWidth && stableHeight == currentHeight) {
            int physicalWidth, int physicalHeight, int currentWidth, int currentHeight) {
        if (physicalWidth == currentWidth && physicalHeight == currentHeight) {
            return 1f;
        }
        final float widthRatio = (float) currentWidth / stableWidth;
        final float heightRatio = (float) currentHeight / stableHeight;
        final float widthRatio = (float) currentWidth / physicalWidth;
        final float heightRatio = (float) currentHeight / physicalHeight;
        return Math.min(widthRatio, heightRatio);
    }
}
+25 −24
Original line number Diff line number Diff line
@@ -203,8 +203,8 @@ public class CutoutSpecification {
    public static class Parser {
        private final boolean mIsShortEdgeOnTop;
        private final float mStableDensity;
        private final int mStableDisplayWidth;
        private final int mStableDisplayHeight;
        private final int mPhysicalDisplayWidth;
        private final int mPhysicalDisplayHeight;
        private final float mPhysicalPixelDisplaySizeRatio;
        private final Matrix mMatrix;
        private Insets mInsets;
@@ -238,25 +238,26 @@ public class CutoutSpecification {
        private boolean mIsCloserToStartSide;

        @VisibleForTesting(visibility = PACKAGE)
        public Parser(float stableDensity, int stableDisplayWidth, int stableDisplayHeight) {
            this(stableDensity, stableDisplayWidth, stableDisplayHeight, 1f);
        public Parser(float stableDensity, int physicalDisplayWidth,
                int physicalDisplayHeight) {
            this(stableDensity, physicalDisplayWidth, physicalDisplayHeight, 1f);
        }

        /**
         * The constructor of the CutoutSpecification parser to parse the specification of cutout.
         * @param stableDensity the display density.
         * @param stableDisplayWidth the display width.
         * @param stableDisplayHeight the display height.
         * @param physicalDisplayWidth the display width.
         * @param physicalDisplayHeight the display height.
         * @param physicalPixelDisplaySizeRatio the display size ratio based on stable display size.
         */
        Parser(float stableDensity, int stableDisplayWidth, int stableDisplayHeight,
        Parser(float stableDensity, int physicalDisplayWidth, int physicalDisplayHeight,
                float physicalPixelDisplaySizeRatio) {
            mStableDensity = stableDensity;
            mStableDisplayWidth = stableDisplayWidth;
            mStableDisplayHeight = stableDisplayHeight;
            mPhysicalDisplayWidth = physicalDisplayWidth;
            mPhysicalDisplayHeight = physicalDisplayHeight;
            mPhysicalPixelDisplaySizeRatio = physicalPixelDisplaySizeRatio;
            mMatrix = new Matrix();
            mIsShortEdgeOnTop = mStableDisplayWidth < mStableDisplayHeight;
            mIsShortEdgeOnTop = mPhysicalDisplayWidth < mPhysicalDisplayHeight;
        }

        private void computeBoundsRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) {
@@ -281,18 +282,18 @@ public class CutoutSpecification {
        private void translateMatrix() {
            final float offsetX;
            if (mPositionFromRight) {
                offsetX = mStableDisplayWidth;
                offsetX = mPhysicalDisplayWidth;
            } else if (mPositionFromLeft) {
                offsetX = 0;
            } else {
                offsetX = mStableDisplayWidth / 2f;
                offsetX = mPhysicalDisplayWidth / 2f;
            }

            final float offsetY;
            if (mPositionFromBottom) {
                offsetY = mStableDisplayHeight;
                offsetY = mPhysicalDisplayHeight;
            } else if (mPositionFromCenterVertical) {
                offsetY = mStableDisplayHeight / 2f;
                offsetY = mPhysicalDisplayHeight / 2f;
            } else {
                offsetY = 0;
            }
@@ -305,14 +306,14 @@ public class CutoutSpecification {
        }

        private int computeSafeInsets(int gravity, Rect rect) {
            if (gravity == LEFT && rect.right > 0 && rect.right < mStableDisplayWidth) {
            if (gravity == LEFT && rect.right > 0 && rect.right < mPhysicalDisplayWidth) {
                return rect.right;
            } else if (gravity == TOP && rect.bottom > 0 && rect.bottom < mStableDisplayHeight) {
            } else if (gravity == TOP && rect.bottom > 0 && rect.bottom < mPhysicalDisplayHeight) {
                return rect.bottom;
            } else if (gravity == RIGHT && rect.left > 0 && rect.left < mStableDisplayWidth) {
                return mStableDisplayWidth - rect.left;
            } else if (gravity == BOTTOM && rect.top > 0 && rect.top < mStableDisplayHeight) {
                return mStableDisplayHeight - rect.top;
            } else if (gravity == RIGHT && rect.left > 0 && rect.left < mPhysicalDisplayWidth) {
                return mPhysicalDisplayWidth - rect.left;
            } else if (gravity == BOTTOM && rect.top > 0 && rect.top < mPhysicalDisplayHeight) {
                return mPhysicalDisplayHeight - rect.top;
            }
            return 0;
        }
@@ -415,12 +416,12 @@ public class CutoutSpecification {

            if (mIsShortEdgeOnTop) {
                mIsTouchShortEdgeStart = mTmpRect.top <= 0;
                mIsTouchShortEdgeEnd = mTmpRect.bottom >= mStableDisplayHeight;
                mIsCloserToStartSide = mTmpRect.centerY() < mStableDisplayHeight / 2;
                mIsTouchShortEdgeEnd = mTmpRect.bottom >= mPhysicalDisplayHeight;
                mIsCloserToStartSide = mTmpRect.centerY() < mPhysicalDisplayHeight / 2;
            } else {
                mIsTouchShortEdgeStart = mTmpRect.left <= 0;
                mIsTouchShortEdgeEnd = mTmpRect.right >= mStableDisplayWidth;
                mIsCloserToStartSide = mTmpRect.centerX() < mStableDisplayWidth / 2;
                mIsTouchShortEdgeEnd = mTmpRect.right >= mPhysicalDisplayWidth;
                mIsCloserToStartSide = mTmpRect.centerX() < mPhysicalDisplayWidth / 2;
            }

            setEdgeCutout(newPath);
+43 −42
Original line number Diff line number Diff line
@@ -77,8 +77,8 @@ public final class DisplayCutout {

    private static final Rect ZERO_RECT = new Rect();
    private static final CutoutPathParserInfo EMPTY_PARSER_INFO = new CutoutPathParserInfo(
            0 /* displayWidth */, 0 /* stableDisplayHeight */,
            0 /* stableDisplayHeight */, 0 /* displayHeight */, 0f /* density */,
            0 /* displayWidth */, 0 /* physicalDisplayHeight */,
            0 /* physicalDisplayHeight */, 0 /* displayHeight */, 0f /* density */,
            "" /* cutoutSpec */, 0 /* ROTATION_0 */, 0f /* scale */,
            0f /* physicalPixelDisplaySizeRatio*/);

@@ -258,21 +258,21 @@ public final class DisplayCutout {
    public static class CutoutPathParserInfo {
        private final int mDisplayWidth;
        private final int mDisplayHeight;
        private final int mStableDisplayWidth;
        private final int mStableDisplayHeight;
        private final int mPhysicalDisplayWidth;
        private final int mPhysicalDisplayHeight;
        private final float mDensity;
        private final String mCutoutSpec;
        private final @Rotation int mRotation;
        private final float mScale;
        private final float mPhysicalPixelDisplaySizeRatio;

        public CutoutPathParserInfo(int displayWidth, int displayHeight, int stableDisplayWidth,
                int stableDisplayHeight, float density, @Nullable String cutoutSpec,
        public CutoutPathParserInfo(int displayWidth, int displayHeight, int physicalDisplayWidth,
                int physicalDisplayHeight, float density, @Nullable String cutoutSpec,
                @Rotation int rotation, float scale, float physicalPixelDisplaySizeRatio) {
            mDisplayWidth = displayWidth;
            mDisplayHeight = displayHeight;
            mStableDisplayWidth = stableDisplayWidth;
            mStableDisplayHeight = stableDisplayHeight;
            mPhysicalDisplayWidth = physicalDisplayWidth;
            mPhysicalDisplayHeight = physicalDisplayHeight;
            mDensity = density;
            mCutoutSpec = cutoutSpec == null ? "" : cutoutSpec;
            mRotation = rotation;
@@ -283,8 +283,8 @@ public final class DisplayCutout {
        public CutoutPathParserInfo(@NonNull CutoutPathParserInfo cutoutPathParserInfo) {
            mDisplayWidth = cutoutPathParserInfo.mDisplayWidth;
            mDisplayHeight = cutoutPathParserInfo.mDisplayHeight;
            mStableDisplayWidth = cutoutPathParserInfo.mStableDisplayWidth;
            mStableDisplayHeight = cutoutPathParserInfo.mStableDisplayHeight;
            mPhysicalDisplayWidth = cutoutPathParserInfo.mPhysicalDisplayWidth;
            mPhysicalDisplayHeight = cutoutPathParserInfo.mPhysicalDisplayHeight;
            mDensity = cutoutPathParserInfo.mDensity;
            mCutoutSpec = cutoutPathParserInfo.mCutoutSpec;
            mRotation = cutoutPathParserInfo.mRotation;
@@ -300,12 +300,12 @@ public final class DisplayCutout {
            return mDisplayHeight;
        }

        public int getStableDisplayWidth() {
            return mStableDisplayWidth;
        public int getPhysicalDisplayWidth() {
            return mPhysicalDisplayWidth;
        }

        public int getStableDisplayHeight() {
            return mStableDisplayHeight;
        public int getPhysicalDisplayHeight() {
            return mPhysicalDisplayHeight;
        }

        public float getDensity() {
@@ -342,8 +342,8 @@ public final class DisplayCutout {
            result = result * 48271 + Integer.hashCode(mRotation);
            result = result * 48271 + Float.hashCode(mScale);
            result = result * 48271 + Float.hashCode(mPhysicalPixelDisplaySizeRatio);
            result = result * 48271 + Integer.hashCode(mStableDisplayWidth);
            result = result * 48271 + Integer.hashCode(mStableDisplayHeight);
            result = result * 48271 + Integer.hashCode(mPhysicalDisplayWidth);
            result = result * 48271 + Integer.hashCode(mPhysicalDisplayHeight);
            return result;
        }

@@ -355,8 +355,8 @@ public final class DisplayCutout {
            if (o instanceof CutoutPathParserInfo) {
                CutoutPathParserInfo c = (CutoutPathParserInfo) o;
                return mDisplayWidth == c.mDisplayWidth && mDisplayHeight == c.mDisplayHeight
                        && mStableDisplayWidth == c.mStableDisplayWidth
                        && mStableDisplayHeight == c.mStableDisplayHeight
                        && mPhysicalDisplayWidth == c.mPhysicalDisplayWidth
                        && mPhysicalDisplayHeight == c.mPhysicalDisplayHeight
                        && mDensity == c.mDensity && mCutoutSpec.equals(c.mCutoutSpec)
                        && mRotation == c.mRotation && mScale == c.mScale
                        && mPhysicalPixelDisplaySizeRatio == c.mPhysicalPixelDisplaySizeRatio;
@@ -368,8 +368,8 @@ public final class DisplayCutout {
        public String toString() {
            return "CutoutPathParserInfo{displayWidth=" + mDisplayWidth
                    + " displayHeight=" + mDisplayHeight
                    + " stableDisplayHeight=" + mStableDisplayWidth
                    + " stableDisplayHeight=" + mStableDisplayHeight
                    + " physicalDisplayWidth=" + mPhysicalDisplayWidth
                    + " physicalDisplayHeight=" + mPhysicalDisplayHeight
                    + " density={" + mDensity + "}"
                    + " cutoutSpec={" + mCutoutSpec + "}"
                    + " rotation={" + mRotation + "}"
@@ -750,8 +750,8 @@ public final class DisplayCutout {
            }
        }
        final CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(
                mCutoutPathParserInfo.getDensity(), mCutoutPathParserInfo.getStableDisplayWidth(),
                mCutoutPathParserInfo.getStableDisplayHeight(),
                mCutoutPathParserInfo.getDensity(), mCutoutPathParserInfo.getPhysicalDisplayWidth(),
                mCutoutPathParserInfo.getPhysicalDisplayHeight(),
                mCutoutPathParserInfo.getPhysicalPixelDisplaySizeRatio())
                .parse(mCutoutPathParserInfo.getCutoutSpec());

@@ -1053,11 +1053,11 @@ public final class DisplayCutout {
     * @hide
     */
    public static DisplayCutout fromResourcesRectApproximation(Resources res,
            String displayUniqueId, int stableDisplayWidth, int stableDisplayHeight,
            String displayUniqueId, int physicalDisplayWidth, int physicalDisplayHeight,
            int displayWidth, int displayHeight) {
        return pathAndDisplayCutoutFromSpec(getDisplayCutoutPath(res, displayUniqueId),
                getDisplayCutoutApproximationRect(res, displayUniqueId), stableDisplayWidth,
                stableDisplayHeight, displayWidth, displayHeight,
                getDisplayCutoutApproximationRect(res, displayUniqueId), physicalDisplayWidth,
                physicalDisplayHeight, displayWidth, displayHeight,
                DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT,
                getWaterfallInsets(res, displayUniqueId)).second;
    }
@@ -1080,8 +1080,8 @@ public final class DisplayCutout {
     *
     * @param pathSpec the spec string read from config_mainBuiltInDisplayCutout.
     * @param rectSpec the spec string read from config_mainBuiltInDisplayCutoutRectApproximation.
     * @param stableDisplayWidth the stable display width.
     * @param stableDisplayHeight the stable display height.
     * @param physicalDisplayWidth the max physical display width the display supports.
     * @param physicalDisplayHeight the max physical display height the display supports.
     * @param displayWidth the display width.
     * @param displayHeight the display height.
     * @param density the display density.
@@ -1089,7 +1089,7 @@ public final class DisplayCutout {
     * @return a Pair contains the cutout path and the corresponding DisplayCutout instance.
     */
    private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(
            String pathSpec, String rectSpec, int stableDisplayWidth, int stableDisplayHeight,
            String pathSpec, String rectSpec, int physicalDisplayWidth, int physicalDisplayHeight,
            int displayWidth, int displayHeight, float density, Insets waterfallInsets) {
        // Always use the rect approximation spec to create the cutout if it's not null because
        // transforming and sending a Region constructed from a path is very costly.
@@ -1099,7 +1099,7 @@ public final class DisplayCutout {
        }

        final float physicalPixelDisplaySizeRatio = DisplayUtils.getPhysicalPixelDisplaySizeRatio(
                stableDisplayWidth, stableDisplayHeight, displayWidth, displayHeight);
                physicalDisplayWidth, physicalDisplayHeight, displayWidth, displayHeight);

        synchronized (CACHE_LOCK) {
            if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
@@ -1114,7 +1114,8 @@ public final class DisplayCutout {
        spec = spec.trim();

        CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(density,
                stableDisplayWidth, stableDisplayHeight, physicalPixelDisplaySizeRatio).parse(spec);
                physicalDisplayWidth, physicalDisplayHeight, physicalPixelDisplaySizeRatio)
                .parse(spec);
        Rect safeInset = cutoutSpec.getSafeInset();
        final Rect boundLeft = cutoutSpec.getLeftBound();
        final Rect boundTop = cutoutSpec.getTopBound();
@@ -1131,7 +1132,7 @@ public final class DisplayCutout {
        }

        final CutoutPathParserInfo cutoutPathParserInfo = new CutoutPathParserInfo(
                displayWidth, displayHeight, stableDisplayWidth, stableDisplayHeight, density,
                displayWidth, displayHeight, physicalDisplayWidth, physicalDisplayHeight, density,
                pathSpec.trim(), ROTATION_0, 1f /* scale */, physicalPixelDisplaySizeRatio);

        final DisplayCutout cutout = new DisplayCutout(
@@ -1182,9 +1183,9 @@ public final class DisplayCutout {
        Collections.rotate(Arrays.asList(newBounds), -rotation);
        final CutoutPathParserInfo info = getCutoutPathParserInfo();
        final CutoutPathParserInfo newInfo = new CutoutPathParserInfo(
                info.getDisplayWidth(), info.getDisplayHeight(), info.getStableDisplayWidth(),
                info.getStableDisplayHeight(), info.getDensity(), info.getCutoutSpec(), toRotation,
                info.getScale(), info.getPhysicalPixelDisplaySizeRatio());
                info.getDisplayWidth(), info.getDisplayHeight(), info.getPhysicalDisplayWidth(),
                info.getPhysicalDisplayHeight(), info.getDensity(), info.getCutoutSpec(),
                toRotation, info.getScale(), info.getPhysicalPixelDisplaySizeRatio());
        final boolean swapAspect = (rotation % 2) != 0;
        final int endWidth = swapAspect ? startHeight : startWidth;
        final int endHeight = swapAspect ? startWidth : startHeight;
@@ -1284,8 +1285,8 @@ public final class DisplayCutout {
                out.writeTypedObject(cutout.mWaterfallInsets, flags);
                out.writeInt(cutout.mCutoutPathParserInfo.getDisplayWidth());
                out.writeInt(cutout.mCutoutPathParserInfo.getDisplayHeight());
                out.writeInt(cutout.mCutoutPathParserInfo.getStableDisplayWidth());
                out.writeInt(cutout.mCutoutPathParserInfo.getStableDisplayHeight());
                out.writeInt(cutout.mCutoutPathParserInfo.getPhysicalDisplayWidth());
                out.writeInt(cutout.mCutoutPathParserInfo.getPhysicalDisplayHeight());
                out.writeFloat(cutout.mCutoutPathParserInfo.getDensity());
                out.writeString(cutout.mCutoutPathParserInfo.getCutoutSpec());
                out.writeInt(cutout.mCutoutPathParserInfo.getRotation());
@@ -1336,16 +1337,16 @@ public final class DisplayCutout {
            Insets waterfallInsets = in.readTypedObject(Insets.CREATOR);
            int displayWidth = in.readInt();
            int displayHeight = in.readInt();
            int stableDisplayWidth = in.readInt();
            int stableDisplayHeight = in.readInt();
            int physicalDisplayWidth = in.readInt();
            int physicalDisplayHeight = in.readInt();
            float density = in.readFloat();
            String cutoutSpec = in.readString();
            int rotation = in.readInt();
            float scale = in.readFloat();
            float physicalPixelDisplaySizeRatio = in.readFloat();
            final CutoutPathParserInfo info = new CutoutPathParserInfo(
                    displayWidth, displayHeight, stableDisplayWidth, stableDisplayHeight, density,
                    cutoutSpec, rotation, scale, physicalPixelDisplaySizeRatio);
                    displayWidth, displayHeight, physicalDisplayWidth, physicalDisplayHeight,
                    density, cutoutSpec, rotation, scale, physicalPixelDisplaySizeRatio);

            return new DisplayCutout(
                    safeInsets, waterfallInsets, bounds, info, false /* copyArguments */);
@@ -1373,8 +1374,8 @@ public final class DisplayCutout {
            final CutoutPathParserInfo info = new CutoutPathParserInfo(
                    mInner.mCutoutPathParserInfo.getDisplayWidth(),
                    mInner.mCutoutPathParserInfo.getDisplayHeight(),
                    mInner.mCutoutPathParserInfo.getStableDisplayWidth(),
                    mInner.mCutoutPathParserInfo.getStableDisplayHeight(),
                    mInner.mCutoutPathParserInfo.getPhysicalDisplayWidth(),
                    mInner.mCutoutPathParserInfo.getPhysicalDisplayHeight(),
                    mInner.mCutoutPathParserInfo.getDensity(),
                    mInner.mCutoutPathParserInfo.getCutoutSpec(),
                    mInner.mCutoutPathParserInfo.getRotation(),
+7 −7
Original line number Diff line number Diff line
@@ -98,10 +98,10 @@ public class RoundedCorners implements Parcelable {
     * @android:dimen/rounded_corner_radius_top and @android:dimen/rounded_corner_radius_bottom
     */
    public static RoundedCorners fromResources(
            Resources res, String displayUniqueId, int stableDisplayWidth, int stableDisplayHeight,
            int displayWidth, int displayHeight) {
        return fromRadii(loadRoundedCornerRadii(res, displayUniqueId), stableDisplayWidth,
                stableDisplayHeight, displayWidth, displayHeight);
            Resources res, String displayUniqueId, int physicalDisplayWidth,
            int physicalDisplayHeight, int displayWidth, int displayHeight) {
        return fromRadii(loadRoundedCornerRadii(res, displayUniqueId), physicalDisplayWidth,
                physicalDisplayHeight, displayWidth, displayHeight);
    }

    /**
@@ -113,14 +113,14 @@ public class RoundedCorners implements Parcelable {
        return fromRadii(radii, displayWidth, displayHeight, displayWidth, displayHeight);
    }

    private static RoundedCorners fromRadii(Pair<Integer, Integer> radii, int stableDisplayWidth,
            int stableDisplayHeight, int displayWidth, int displayHeight) {
    private static RoundedCorners fromRadii(Pair<Integer, Integer> radii, int physicalDisplayWidth,
            int physicalDisplayHeight, int displayWidth, int displayHeight) {
        if (radii == null) {
            return null;
        }

        final float physicalPixelDisplaySizeRatio = DisplayUtils.getPhysicalPixelDisplaySizeRatio(
                stableDisplayWidth, stableDisplayHeight, displayWidth, displayHeight);
                physicalDisplayWidth, physicalDisplayHeight, displayWidth, displayHeight);

        synchronized (CACHE_LOCK) {
            if (radii.equals(sCachedRadii) && sCachedDisplayWidth == displayWidth
+3 −0
Original line number Diff line number Diff line
@@ -3527,6 +3527,9 @@
         appended after the path string to interpret coordinates in dp instead of px units.
         Note that a physical cutout should be configured in pixels for the best results.

         If the display supports multiple resolutions, please define the path config based on the
         highest resolution so that it can be scaled correctly in each resolution.

         Example for a 10px x 10px square top-center cutout:
                <string ...>M -5,0 L -5,10 L 5,10 L 5,0 Z</string>
         Example for a 10dp x 10dp square top-center cutout:
Loading