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

Commit 4d66e544 authored by Shawn Lin's avatar Shawn Lin Committed by Automerger Merge Worker
Browse files

Merge "Support scaled cutout & roundedcorners when resolution is overridden"...

Merge "Support scaled cutout & roundedcorners when resolution is overridden" into tm-dev am: 88ed5ca1 am: feac07a0

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17232290



Change-Id: I58559a9487443d2bbabd976a56e37077b063f0e3
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents aca85d41 feac07a0
Loading
Loading
Loading
Loading
+14 −1
Original line number Original line Diff line number Diff line
@@ -21,7 +21,7 @@ import android.content.res.Resources;
import com.android.internal.R;
import com.android.internal.R;


/**
/**
 * Utils for loading resources for multi-display.
 * Utils for loading display related resources and calculations.
 *
 *
 * @hide
 * @hide
 */
 */
@@ -51,4 +51,17 @@ public class DisplayUtils {
        }
        }
        return index;
        return index;
    }
    }

    /**
     * Get the display size ratio based on the stable display size.
     */
    public static float getPhysicalPixelDisplaySizeRatio(
            int stableWidth, int stableHeight, int currentWidth, int currentHeight) {
        if (stableWidth == currentWidth && stableHeight == currentHeight) {
            return 1f;
        }
        final float widthRatio = (float) currentWidth / stableWidth;
        final float heightRatio = (float) currentHeight / stableHeight;
        return Math.min(widthRatio, heightRatio);
    }
}
}
+70 −29
Original line number Original line Diff line number Diff line
@@ -94,7 +94,7 @@ public class CutoutSpecification {
    private final Rect mTopBound;
    private final Rect mTopBound;
    private final Rect mRightBound;
    private final Rect mRightBound;
    private final Rect mBottomBound;
    private final Rect mBottomBound;
    private final Insets mInsets;
    private Insets mInsets;


    private CutoutSpecification(@NonNull Parser parser) {
    private CutoutSpecification(@NonNull Parser parser) {
        mPath = parser.mPath;
        mPath = parser.mPath;
@@ -104,6 +104,8 @@ public class CutoutSpecification {
        mBottomBound = parser.mBottomBound;
        mBottomBound = parser.mBottomBound;
        mInsets = parser.mInsets;
        mInsets = parser.mInsets;


        applyPhysicalPixelDisplaySizeRatio(parser.mPhysicalPixelDisplaySizeRatio);

        if (DEBUG) {
        if (DEBUG) {
            Log.d(TAG, String.format(Locale.ENGLISH,
            Log.d(TAG, String.format(Locale.ENGLISH,
                    "left cutout = %s, top cutout = %s, right cutout = %s, bottom cutout = %s",
                    "left cutout = %s, top cutout = %s, right cutout = %s, bottom cutout = %s",
@@ -114,6 +116,38 @@ public class CutoutSpecification {
        }
        }
    }
    }


    private void applyPhysicalPixelDisplaySizeRatio(float physicalPixelDisplaySizeRatio) {
        if (physicalPixelDisplaySizeRatio == 1f) {
            return;
        }

        if (mPath != null && !mPath.isEmpty()) {
            final Matrix matrix = new Matrix();
            matrix.postScale(physicalPixelDisplaySizeRatio, physicalPixelDisplaySizeRatio);
            mPath.transform(matrix);
        }

        scaleBounds(mLeftBound, physicalPixelDisplaySizeRatio);
        scaleBounds(mTopBound, physicalPixelDisplaySizeRatio);
        scaleBounds(mRightBound, physicalPixelDisplaySizeRatio);
        scaleBounds(mBottomBound, physicalPixelDisplaySizeRatio);
        mInsets = scaleInsets(mInsets, physicalPixelDisplaySizeRatio);
    }

    private void scaleBounds(Rect r, float ratio) {
        if (r != null && !r.isEmpty()) {
            r.scale(ratio);
        }
    }

    private Insets scaleInsets(Insets insets, float ratio) {
        return Insets.of(
                (int) (insets.left * ratio + 0.5f),
                (int) (insets.top * ratio + 0.5f),
                (int) (insets.right * ratio + 0.5f),
                (int) (insets.bottom * ratio + 0.5f));
    }

    @VisibleForTesting(visibility = PACKAGE)
    @VisibleForTesting(visibility = PACKAGE)
    @Nullable
    @Nullable
    public Path getPath() {
    public Path getPath() {
@@ -168,9 +202,10 @@ public class CutoutSpecification {
    @VisibleForTesting(visibility = PACKAGE)
    @VisibleForTesting(visibility = PACKAGE)
    public static class Parser {
    public static class Parser {
        private final boolean mIsShortEdgeOnTop;
        private final boolean mIsShortEdgeOnTop;
        private final float mDensity;
        private final float mStableDensity;
        private final int mDisplayWidth;
        private final int mStableDisplayWidth;
        private final int mDisplayHeight;
        private final int mStableDisplayHeight;
        private final float mPhysicalPixelDisplaySizeRatio;
        private final Matrix mMatrix;
        private final Matrix mMatrix;
        private Insets mInsets;
        private Insets mInsets;
        private int mSafeInsetLeft;
        private int mSafeInsetLeft;
@@ -202,19 +237,26 @@ public class CutoutSpecification {
        private boolean mIsTouchShortEdgeEnd;
        private boolean mIsTouchShortEdgeEnd;
        private boolean mIsCloserToStartSide;
        private boolean mIsCloserToStartSide;


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

        /**
        /**
         * The constructor of the CutoutSpecification parser to parse the specification of cutout.
         * The constructor of the CutoutSpecification parser to parse the specification of cutout.
         * @param density the display density.
         * @param stableDensity the display density.
         * @param displayWidth the display width.
         * @param stableDisplayWidth the display width.
         * @param displayHeight the display height.
         * @param stableDisplayHeight the display height.
         * @param physicalPixelDisplaySizeRatio the display size ratio based on stable display size.
         */
         */
        @VisibleForTesting(visibility = PACKAGE)
        Parser(float stableDensity, int stableDisplayWidth, int stableDisplayHeight,
        public Parser(float density, int displayWidth, int displayHeight) {
                float physicalPixelDisplaySizeRatio) {
            mDensity = density;
            mStableDensity = stableDensity;
            mDisplayWidth = displayWidth;
            mStableDisplayWidth = stableDisplayWidth;
            mDisplayHeight = displayHeight;
            mStableDisplayHeight = stableDisplayHeight;
            mPhysicalPixelDisplaySizeRatio = physicalPixelDisplaySizeRatio;
            mMatrix = new Matrix();
            mMatrix = new Matrix();
            mIsShortEdgeOnTop = mDisplayWidth < mDisplayHeight;
            mIsShortEdgeOnTop = mStableDisplayWidth < mStableDisplayHeight;
        }
        }


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


            final float offsetY;
            final float offsetY;
            if (mPositionFromBottom) {
            if (mPositionFromBottom) {
                offsetY = mDisplayHeight;
                offsetY = mStableDisplayHeight;
            } else if (mPositionFromCenterVertical) {
            } else if (mPositionFromCenterVertical) {
                offsetY = mDisplayHeight / 2f;
                offsetY = mStableDisplayHeight / 2f;
            } else {
            } else {
                offsetY = 0;
                offsetY = 0;
            }
            }


            mMatrix.reset();
            mMatrix.reset();
            if (mInDp) {
            if (mInDp) {
                mMatrix.postScale(mDensity, mDensity);
                mMatrix.postScale(mStableDensity, mStableDensity);
            }
            }
            mMatrix.postTranslate(offsetX, offsetY);
            mMatrix.postTranslate(offsetX, offsetY);
        }
        }


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


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


            setEdgeCutout(newPath);
            setEdgeCutout(newPath);
@@ -476,7 +518,6 @@ public class CutoutSpecification {
            }
            }


            parseSpecWithoutDp(spec);
            parseSpecWithoutDp(spec);

            mInsets = Insets.of(mSafeInsetLeft, mSafeInsetTop, mSafeInsetRight, mSafeInsetBottom);
            mInsets = Insets.of(mSafeInsetLeft, mSafeInsetTop, mSafeInsetRight, mSafeInsetBottom);
            return new CutoutSpecification(this);
            return new CutoutSpecification(this);
        }
        }
+80 −36
Original line number Original line Diff line number Diff line
@@ -77,8 +77,10 @@ public final class DisplayCutout {


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


    /**
    /**
     * An instance where {@link #isEmpty()} returns {@code true}.
     * An instance where {@link #isEmpty()} returns {@code true}.
@@ -105,6 +107,8 @@ public final class DisplayCutout {
    private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR;
    private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR;
    @GuardedBy("CACHE_LOCK")
    @GuardedBy("CACHE_LOCK")
    private static Insets sCachedWaterfallInsets;
    private static Insets sCachedWaterfallInsets;
    @GuardedBy("CACHE_LOCK")
    private static float sCachedPhysicalPixelDisplaySizeRatio;


    @GuardedBy("CACHE_LOCK")
    @GuardedBy("CACHE_LOCK")
    private static CutoutPathParserInfo sCachedCutoutPathParserInfo;
    private static CutoutPathParserInfo sCachedCutoutPathParserInfo;
@@ -254,28 +258,38 @@ public final class DisplayCutout {
    public static class CutoutPathParserInfo {
    public static class CutoutPathParserInfo {
        private final int mDisplayWidth;
        private final int mDisplayWidth;
        private final int mDisplayHeight;
        private final int mDisplayHeight;
        private final int mStableDisplayWidth;
        private final int mStableDisplayHeight;
        private final float mDensity;
        private final float mDensity;
        private final String mCutoutSpec;
        private final String mCutoutSpec;
        private final @Rotation int mRotation;
        private final @Rotation int mRotation;
        private final float mScale;
        private final float mScale;
        private final float mPhysicalPixelDisplaySizeRatio;


        public CutoutPathParserInfo(int displayWidth, int displayHeight, float density,
        public CutoutPathParserInfo(int displayWidth, int displayHeight, int stableDisplayWidth,
                @Nullable String cutoutSpec, @Rotation int rotation, float scale) {
                int stableDisplayHeight, float density, @Nullable String cutoutSpec,
                @Rotation int rotation, float scale, float physicalPixelDisplaySizeRatio) {
            mDisplayWidth = displayWidth;
            mDisplayWidth = displayWidth;
            mDisplayHeight = displayHeight;
            mDisplayHeight = displayHeight;
            mStableDisplayWidth = stableDisplayWidth;
            mStableDisplayHeight = stableDisplayHeight;
            mDensity = density;
            mDensity = density;
            mCutoutSpec = cutoutSpec == null ? "" : cutoutSpec;
            mCutoutSpec = cutoutSpec == null ? "" : cutoutSpec;
            mRotation = rotation;
            mRotation = rotation;
            mScale = scale;
            mScale = scale;
            mPhysicalPixelDisplaySizeRatio = physicalPixelDisplaySizeRatio;
        }
        }


        public CutoutPathParserInfo(@NonNull CutoutPathParserInfo cutoutPathParserInfo) {
        public CutoutPathParserInfo(@NonNull CutoutPathParserInfo cutoutPathParserInfo) {
            mDisplayWidth = cutoutPathParserInfo.mDisplayWidth;
            mDisplayWidth = cutoutPathParserInfo.mDisplayWidth;
            mDisplayHeight = cutoutPathParserInfo.mDisplayHeight;
            mDisplayHeight = cutoutPathParserInfo.mDisplayHeight;
            mStableDisplayWidth = cutoutPathParserInfo.mStableDisplayWidth;
            mStableDisplayHeight = cutoutPathParserInfo.mStableDisplayHeight;
            mDensity = cutoutPathParserInfo.mDensity;
            mDensity = cutoutPathParserInfo.mDensity;
            mCutoutSpec = cutoutPathParserInfo.mCutoutSpec;
            mCutoutSpec = cutoutPathParserInfo.mCutoutSpec;
            mRotation = cutoutPathParserInfo.mRotation;
            mRotation = cutoutPathParserInfo.mRotation;
            mScale = cutoutPathParserInfo.mScale;
            mScale = cutoutPathParserInfo.mScale;
            mPhysicalPixelDisplaySizeRatio = cutoutPathParserInfo.mPhysicalPixelDisplaySizeRatio;
        }
        }


        public int getDisplayWidth() {
        public int getDisplayWidth() {
@@ -286,6 +300,14 @@ public final class DisplayCutout {
            return mDisplayHeight;
            return mDisplayHeight;
        }
        }


        public int getStableDisplayWidth() {
            return mStableDisplayWidth;
        }

        public int getStableDisplayHeight() {
            return mStableDisplayHeight;
        }

        public float getDensity() {
        public float getDensity() {
            return mDensity;
            return mDensity;
        }
        }
@@ -302,6 +324,10 @@ public final class DisplayCutout {
            return mScale;
            return mScale;
        }
        }


        public float getPhysicalPixelDisplaySizeRatio() {
            return mPhysicalPixelDisplaySizeRatio;
        }

        private boolean hasCutout() {
        private boolean hasCutout() {
            return !mCutoutSpec.isEmpty();
            return !mCutoutSpec.isEmpty();
        }
        }
@@ -315,6 +341,9 @@ public final class DisplayCutout {
            result = result * 48271 + mCutoutSpec.hashCode();
            result = result * 48271 + mCutoutSpec.hashCode();
            result = result * 48271 + Integer.hashCode(mRotation);
            result = result * 48271 + Integer.hashCode(mRotation);
            result = result * 48271 + Float.hashCode(mScale);
            result = result * 48271 + Float.hashCode(mScale);
            result = result * 48271 + Float.hashCode(mPhysicalPixelDisplaySizeRatio);
            result = result * 48271 + Integer.hashCode(mStableDisplayWidth);
            result = result * 48271 + Integer.hashCode(mStableDisplayHeight);
            return result;
            return result;
        }
        }


@@ -326,8 +355,11 @@ public final class DisplayCutout {
            if (o instanceof CutoutPathParserInfo) {
            if (o instanceof CutoutPathParserInfo) {
                CutoutPathParserInfo c = (CutoutPathParserInfo) o;
                CutoutPathParserInfo c = (CutoutPathParserInfo) o;
                return mDisplayWidth == c.mDisplayWidth && mDisplayHeight == c.mDisplayHeight
                return mDisplayWidth == c.mDisplayWidth && mDisplayHeight == c.mDisplayHeight
                        && mStableDisplayWidth == c.mStableDisplayWidth
                        && mStableDisplayHeight == c.mStableDisplayHeight
                        && mDensity == c.mDensity && mCutoutSpec.equals(c.mCutoutSpec)
                        && mDensity == c.mDensity && mCutoutSpec.equals(c.mCutoutSpec)
                        && mRotation == c.mRotation && mScale == c.mScale;
                        && mRotation == c.mRotation && mScale == c.mScale
                        && mPhysicalPixelDisplaySizeRatio == c.mPhysicalPixelDisplaySizeRatio;
            }
            }
            return false;
            return false;
        }
        }
@@ -336,10 +368,13 @@ public final class DisplayCutout {
        public String toString() {
        public String toString() {
            return "CutoutPathParserInfo{displayWidth=" + mDisplayWidth
            return "CutoutPathParserInfo{displayWidth=" + mDisplayWidth
                    + " displayHeight=" + mDisplayHeight
                    + " displayHeight=" + mDisplayHeight
                    + " stableDisplayHeight=" + mStableDisplayWidth
                    + " stableDisplayHeight=" + mStableDisplayHeight
                    + " density={" + mDensity + "}"
                    + " density={" + mDensity + "}"
                    + " cutoutSpec={" + mCutoutSpec + "}"
                    + " cutoutSpec={" + mCutoutSpec + "}"
                    + " rotation={" + mRotation + "}"
                    + " rotation={" + mRotation + "}"
                    + " scale={" + mScale + "}"
                    + " scale={" + mScale + "}"
                    + " physicalPixelDisplaySizeRatio={" + mPhysicalPixelDisplaySizeRatio + "}"
                    + "}";
                    + "}";
        }
        }
    }
    }
@@ -715,8 +750,9 @@ public final class DisplayCutout {
            }
            }
        }
        }
        final CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(
        final CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(
                mCutoutPathParserInfo.getDensity(), mCutoutPathParserInfo.getDisplayWidth(),
                mCutoutPathParserInfo.getDensity(), mCutoutPathParserInfo.getStableDisplayWidth(),
                mCutoutPathParserInfo.getDisplayHeight())
                mCutoutPathParserInfo.getStableDisplayHeight(),
                mCutoutPathParserInfo.getPhysicalPixelDisplaySizeRatio())
                .parse(mCutoutPathParserInfo.getCutoutSpec());
                .parse(mCutoutPathParserInfo.getCutoutSpec());


        final Path cutoutPath = cutoutSpec.getPath();
        final Path cutoutPath = cutoutSpec.getPath();
@@ -1014,29 +1050,18 @@ public final class DisplayCutout {
     * Creates the display cutout according to
     * Creates the display cutout according to
     * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest
     * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest
     * rectangle-base approximation of the cutout.
     * rectangle-base approximation of the cutout.
     *
     * @hide
     * @hide
     */
     */
    public static DisplayCutout fromResourcesRectApproximation(Resources res,
    public static DisplayCutout fromResourcesRectApproximation(Resources res,
            String displayUniqueId, int displayWidth, int displayHeight) {
            String displayUniqueId, int stableDisplayWidth, int stableDisplayHeight,
            int displayWidth, int displayHeight) {
        return pathAndDisplayCutoutFromSpec(getDisplayCutoutPath(res, displayUniqueId),
        return pathAndDisplayCutoutFromSpec(getDisplayCutoutPath(res, displayUniqueId),
                getDisplayCutoutApproximationRect(res, displayUniqueId),
                getDisplayCutoutApproximationRect(res, displayUniqueId), stableDisplayWidth,
                displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT,
                stableDisplayHeight, displayWidth, displayHeight,
                DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT,
                getWaterfallInsets(res, displayUniqueId)).second;
                getWaterfallInsets(res, displayUniqueId)).second;
    }
    }


    /**
     * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout.
     *
     * @hide
     */
    public static Path pathFromResources(Resources res, String displayUniqueId, int displayWidth,
            int displayHeight) {
        return pathAndDisplayCutoutFromSpec(getDisplayCutoutPath(res, displayUniqueId), null,
                displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT,
                getWaterfallInsets(res, displayUniqueId)).first;
    }

    /**
    /**
     * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec.
     * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec.
     *
     *
@@ -1046,8 +1071,8 @@ public final class DisplayCutout {
    public static DisplayCutout fromSpec(String pathSpec, int displayWidth,
    public static DisplayCutout fromSpec(String pathSpec, int displayWidth,
            int displayHeight, float density, Insets waterfallInsets) {
            int displayHeight, float density, Insets waterfallInsets) {
        return pathAndDisplayCutoutFromSpec(
        return pathAndDisplayCutoutFromSpec(
                pathSpec, null, displayWidth, displayHeight, density, waterfallInsets)
                pathSpec, null, displayWidth, displayHeight, displayWidth, displayHeight, density,
                .second;
                waterfallInsets).second;
    }
    }


    /**
    /**
@@ -1055,6 +1080,8 @@ public final class DisplayCutout {
     *
     *
     * @param pathSpec the spec string read from config_mainBuiltInDisplayCutout.
     * @param pathSpec the spec string read from config_mainBuiltInDisplayCutout.
     * @param rectSpec the spec string read from config_mainBuiltInDisplayCutoutRectApproximation.
     * @param rectSpec the spec string read from config_mainBuiltInDisplayCutoutRectApproximation.
     * @param stableDisplayWidth the stable display width.
     * @param stableDisplayHeight the stable display height.
     * @param displayWidth the display width.
     * @param displayWidth the display width.
     * @param displayHeight the display height.
     * @param displayHeight the display height.
     * @param density the display density.
     * @param density the display density.
@@ -1062,8 +1089,8 @@ public final class DisplayCutout {
     * @return a Pair contains the cutout path and the corresponding DisplayCutout instance.
     * @return a Pair contains the cutout path and the corresponding DisplayCutout instance.
     */
     */
    private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(
    private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(
            String pathSpec, String rectSpec, int displayWidth, int displayHeight, float density,
            String pathSpec, String rectSpec, int stableDisplayWidth, int stableDisplayHeight,
            Insets waterfallInsets) {
            int displayWidth, int displayHeight, float density, Insets waterfallInsets) {
        // Always use the rect approximation spec to create the cutout if it's not null because
        // 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.
        // transforming and sending a Region constructed from a path is very costly.
        String spec = rectSpec != null ? rectSpec : pathSpec;
        String spec = rectSpec != null ? rectSpec : pathSpec;
@@ -1071,11 +1098,15 @@ public final class DisplayCutout {
            return NULL_PAIR;
            return NULL_PAIR;
        }
        }


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

        synchronized (CACHE_LOCK) {
        synchronized (CACHE_LOCK) {
            if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
            if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
                    && sCachedDisplayHeight == displayHeight
                    && sCachedDisplayHeight == displayHeight
                    && sCachedDensity == density
                    && sCachedDensity == density
                    && waterfallInsets.equals(sCachedWaterfallInsets)) {
                    && waterfallInsets.equals(sCachedWaterfallInsets)
                    && sCachedPhysicalPixelDisplaySizeRatio == physicalPixelDisplaySizeRatio) {
                return sCachedCutout;
                return sCachedCutout;
            }
            }
        }
        }
@@ -1083,7 +1114,7 @@ public final class DisplayCutout {
        spec = spec.trim();
        spec = spec.trim();


        CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(density,
        CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(density,
                displayWidth, displayHeight).parse(spec);
                stableDisplayWidth, stableDisplayHeight, physicalPixelDisplaySizeRatio).parse(spec);
        Rect safeInset = cutoutSpec.getSafeInset();
        Rect safeInset = cutoutSpec.getSafeInset();
        final Rect boundLeft = cutoutSpec.getLeftBound();
        final Rect boundLeft = cutoutSpec.getLeftBound();
        final Rect boundTop = cutoutSpec.getTopBound();
        final Rect boundTop = cutoutSpec.getTopBound();
@@ -1099,8 +1130,9 @@ public final class DisplayCutout {
                    Math.max(waterfallInsets.bottom, safeInset.bottom));
                    Math.max(waterfallInsets.bottom, safeInset.bottom));
        }
        }


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


        final DisplayCutout cutout = new DisplayCutout(
        final DisplayCutout cutout = new DisplayCutout(
                safeInset, waterfallInsets, boundLeft, boundTop, boundRight, boundBottom,
                safeInset, waterfallInsets, boundLeft, boundTop, boundRight, boundBottom,
@@ -1113,6 +1145,7 @@ public final class DisplayCutout {
            sCachedDensity = density;
            sCachedDensity = density;
            sCachedCutout = result;
            sCachedCutout = result;
            sCachedWaterfallInsets = waterfallInsets;
            sCachedWaterfallInsets = waterfallInsets;
            sCachedPhysicalPixelDisplaySizeRatio = physicalPixelDisplaySizeRatio;
        }
        }
        return result;
        return result;
    }
    }
@@ -1149,8 +1182,9 @@ public final class DisplayCutout {
        Collections.rotate(Arrays.asList(newBounds), -rotation);
        Collections.rotate(Arrays.asList(newBounds), -rotation);
        final CutoutPathParserInfo info = getCutoutPathParserInfo();
        final CutoutPathParserInfo info = getCutoutPathParserInfo();
        final CutoutPathParserInfo newInfo = new CutoutPathParserInfo(
        final CutoutPathParserInfo newInfo = new CutoutPathParserInfo(
                info.getDisplayWidth(), info.getDisplayHeight(), info.getDensity(),
                info.getDisplayWidth(), info.getDisplayHeight(), info.getStableDisplayWidth(),
                info.getCutoutSpec(), toRotation, info.getScale());
                info.getStableDisplayHeight(), info.getDensity(), info.getCutoutSpec(), toRotation,
                info.getScale(), info.getPhysicalPixelDisplaySizeRatio());
        final boolean swapAspect = (rotation % 2) != 0;
        final boolean swapAspect = (rotation % 2) != 0;
        final int endWidth = swapAspect ? startHeight : startWidth;
        final int endWidth = swapAspect ? startHeight : startWidth;
        final int endHeight = swapAspect ? startWidth : startHeight;
        final int endHeight = swapAspect ? startWidth : startHeight;
@@ -1250,10 +1284,13 @@ public final class DisplayCutout {
                out.writeTypedObject(cutout.mWaterfallInsets, flags);
                out.writeTypedObject(cutout.mWaterfallInsets, flags);
                out.writeInt(cutout.mCutoutPathParserInfo.getDisplayWidth());
                out.writeInt(cutout.mCutoutPathParserInfo.getDisplayWidth());
                out.writeInt(cutout.mCutoutPathParserInfo.getDisplayHeight());
                out.writeInt(cutout.mCutoutPathParserInfo.getDisplayHeight());
                out.writeInt(cutout.mCutoutPathParserInfo.getStableDisplayWidth());
                out.writeInt(cutout.mCutoutPathParserInfo.getStableDisplayHeight());
                out.writeFloat(cutout.mCutoutPathParserInfo.getDensity());
                out.writeFloat(cutout.mCutoutPathParserInfo.getDensity());
                out.writeString(cutout.mCutoutPathParserInfo.getCutoutSpec());
                out.writeString(cutout.mCutoutPathParserInfo.getCutoutSpec());
                out.writeInt(cutout.mCutoutPathParserInfo.getRotation());
                out.writeInt(cutout.mCutoutPathParserInfo.getRotation());
                out.writeFloat(cutout.mCutoutPathParserInfo.getScale());
                out.writeFloat(cutout.mCutoutPathParserInfo.getScale());
                out.writeFloat(cutout.mCutoutPathParserInfo.getPhysicalPixelDisplaySizeRatio());
            }
            }
        }
        }


@@ -1299,12 +1336,16 @@ public final class DisplayCutout {
            Insets waterfallInsets = in.readTypedObject(Insets.CREATOR);
            Insets waterfallInsets = in.readTypedObject(Insets.CREATOR);
            int displayWidth = in.readInt();
            int displayWidth = in.readInt();
            int displayHeight = in.readInt();
            int displayHeight = in.readInt();
            int stableDisplayWidth = in.readInt();
            int stableDisplayHeight = in.readInt();
            float density = in.readFloat();
            float density = in.readFloat();
            String cutoutSpec = in.readString();
            String cutoutSpec = in.readString();
            int rotation = in.readInt();
            int rotation = in.readInt();
            float scale = in.readFloat();
            float scale = in.readFloat();
            float physicalPixelDisplaySizeRatio = in.readFloat();
            final CutoutPathParserInfo info = new CutoutPathParserInfo(
            final CutoutPathParserInfo info = new CutoutPathParserInfo(
                    displayWidth, displayHeight, density, cutoutSpec, rotation, scale);
                    displayWidth, displayHeight, stableDisplayWidth, stableDisplayHeight, density,
                    cutoutSpec, rotation, scale, physicalPixelDisplaySizeRatio);


            return new DisplayCutout(
            return new DisplayCutout(
                    safeInsets, waterfallInsets, bounds, info, false /* copyArguments */);
                    safeInsets, waterfallInsets, bounds, info, false /* copyArguments */);
@@ -1332,10 +1373,13 @@ public final class DisplayCutout {
            final CutoutPathParserInfo info = new CutoutPathParserInfo(
            final CutoutPathParserInfo info = new CutoutPathParserInfo(
                    mInner.mCutoutPathParserInfo.getDisplayWidth(),
                    mInner.mCutoutPathParserInfo.getDisplayWidth(),
                    mInner.mCutoutPathParserInfo.getDisplayHeight(),
                    mInner.mCutoutPathParserInfo.getDisplayHeight(),
                    mInner.mCutoutPathParserInfo.getStableDisplayWidth(),
                    mInner.mCutoutPathParserInfo.getStableDisplayHeight(),
                    mInner.mCutoutPathParserInfo.getDensity(),
                    mInner.mCutoutPathParserInfo.getDensity(),
                    mInner.mCutoutPathParserInfo.getCutoutSpec(),
                    mInner.mCutoutPathParserInfo.getCutoutSpec(),
                    mInner.mCutoutPathParserInfo.getRotation(),
                    mInner.mCutoutPathParserInfo.getRotation(),
                    scale);
                    scale,
                    mInner.mCutoutPathParserInfo.getPhysicalPixelDisplaySizeRatio());


            mInner = new DisplayCutout(safeInsets, Insets.of(waterfallInsets), bounds, info);
            mInner = new DisplayCutout(safeInsets, Insets.of(waterfallInsets), bounds, info);
        }
        }
@@ -1387,7 +1431,7 @@ public final class DisplayCutout {
            if (mCutoutPath != null) {
            if (mCutoutPath != null) {
                // Create a fake CutoutPathParserInfo and set it to sCachedCutoutPathParserInfo so
                // Create a fake CutoutPathParserInfo and set it to sCachedCutoutPathParserInfo so
                // that when getCutoutPath() is called, it will return the cached Path.
                // that when getCutoutPath() is called, it will return the cached Path.
                info = new CutoutPathParserInfo(0, 0, 0, "test", 0, 1f);
                info = new CutoutPathParserInfo(0, 0, 0, 0, 0, "test", ROTATION_0, 1f, 1f);
                synchronized (CACHE_LOCK) {
                synchronized (CACHE_LOCK) {
                    DisplayCutout.sCachedCutoutPathParserInfo = info;
                    DisplayCutout.sCachedCutoutPathParserInfo = info;
                    DisplayCutout.sCachedCutoutPath = mCutoutPath;
                    DisplayCutout.sCachedCutoutPath = mCutoutPath;
+23 −5

File changed.

Preview size limit exceeded, changes collapsed.

+3 −2
Original line number Original line Diff line number Diff line
@@ -609,7 +609,8 @@ public class DisplayCutoutTest {
    private static DisplayCutout.CutoutPathParserInfo createParserInfo(
    private static DisplayCutout.CutoutPathParserInfo createParserInfo(
            @Surface.Rotation int rotation) {
            @Surface.Rotation int rotation) {
        return new DisplayCutout.CutoutPathParserInfo(
        return new DisplayCutout.CutoutPathParserInfo(
                0 /* displayWidth */, 0 /* displayHeight */, 0f /* density */, "" /* cutoutSpec */,
                0 /* displayWidth */, 0 /* displayHeight */, 0 /* displayWidth */,
                rotation, 0f /* scale */);
                0 /* displayHeight */, 0f /* density */, "" /* cutoutSpec */,
                rotation, 0f /* scale */, 0f /* displaySizeRatio */);
    }
    }
}
}
Loading