diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api
index a5fa388742a7..62e7a67bf2ab 100644
--- a/packages/react-native/ReactAndroid/api/ReactAndroid.api
+++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api
@@ -3189,6 +3189,8 @@ public final class com/facebook/react/uimanager/BackgroundStyleApplicator {
public static final fun setOutlineOffset (Landroid/view/View;F)V
public static final fun setOutlineStyle (Landroid/view/View;Lcom/facebook/react/uimanager/style/OutlineStyle;)V
public static final fun setOutlineWidth (Landroid/view/View;F)V
+ public static final fun updatePaddingBoxClip (Landroid/view/View;Landroid/graphics/Canvas;Z)V
+ public static final fun updatePaddingBoxOutlineClip (Landroid/view/View;Landroid/graphics/Canvas;Z)Z
}
public abstract class com/facebook/react/uimanager/BaseViewManager : com/facebook/react/uimanager/ViewManager, android/view/View$OnLayoutChangeListener {
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BackgroundStyleApplicator.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BackgroundStyleApplicator.kt
index c3af3626ae6e..09465171f59c 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BackgroundStyleApplicator.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BackgroundStyleApplicator.kt
@@ -18,8 +18,10 @@ import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
+import android.view.ViewOutlineProvider
import android.widget.ImageView
import androidx.annotation.ColorInt
+import com.facebook.react.R
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.uimanager.PixelUtil.dpToPx
@@ -465,6 +467,88 @@ public object BackgroundStyleApplicator {
clipToPaddingBoxWithAntiAliasing(view, canvas, null)
}
+ /**
+ * Clips [view]'s content to its padding box when [clip] is true, like [clipToPaddingBox], and
+ * removes any clip applied by an earlier call when [clip] is false. Views call it on every draw,
+ * before drawing what the clip applies to.
+ *
+ * See [updatePaddingBoxOutlineClip]; when the outline clip does not apply, the canvas is clipped
+ * as [clipToPaddingBox] does.
+ */
+ @JvmStatic
+ public fun updatePaddingBoxClip(view: View, canvas: Canvas, clip: Boolean) {
+ if (!updatePaddingBoxOutlineClip(view, canvas, clip) && clip) {
+ clipToPaddingBox(view, canvas)
+ }
+ }
+
+ /**
+ * Clips [view] to its padding box with its outline ([View.setClipToOutline]) when [clip] is true
+ * and the outline can describe the padding box, and removes an outline clip set by an earlier
+ * call otherwise. Returns whether the outline clip is in effect; when it is not, the caller clips
+ * the canvas itself.
+ *
+ * Before Android 11, HWUI's Canvas.clipPath is not anti-aliased, so a rounded padding box clipped
+ * with it has stepped corners. On Android 9 and 10 the outline clip is anti-aliased and draws no
+ * offscreen layer, but describes only a round rect, so it is used when the padding box is one: a
+ * single circular radius, no border, and nothing drawn outside the bounds (box shadows, outline,
+ * a feedback ripple) for it to cut off. Any other padding box keeps the canvas clip: masking it
+ * with a layer, as [clipToPaddingBoxWithAntiAliasing] does for images, costs a frame's budget on
+ * these devices for a screen of bordered cards.
+ */
+ @JvmStatic
+ public fun updatePaddingBoxOutlineClip(view: View, canvas: Canvas, clip: Boolean): Boolean {
+ // The outline clip is a RenderNode property, so software drawing (e.g. view.draw into a
+ // bitmap) ignores it. Leave it for the next hardware draw: clearing it here would not re-record
+ // the display list it was recorded for.
+ if (!canvas.isHardwareAccelerated) {
+ return false
+ }
+ val ownsOutlineClip = view.getTag(R.id.padding_box_outline_clip) == true
+ val useOutlineClip = clip && canClipPaddingBoxWithOutline(view, ownsOutlineClip)
+ if (useOutlineClip && !ownsOutlineClip) {
+ view.setTag(R.id.padding_box_outline_clip, true)
+ view.clipToOutline = true
+ } else if (!useOutlineClip) {
+ clearPaddingBoxOutlineClip(view)
+ }
+ return useOutlineClip
+ }
+
+ /**
+ * Android 9 and 10 clip with a non-anti-aliased Canvas.clipPath. Earlier versions are left alone:
+ * their OpenGL renderer replaces, rather than intersects, an inherited round-rect outline clip,
+ * so nested rounded clips would escape their parent.
+ */
+ private fun usesOutlineForRoundedClip(): Boolean =
+ Build.VERSION.SDK_INT in Build.VERSION_CODES.P..Build.VERSION_CODES.Q
+
+ private fun canClipPaddingBoxWithOutline(view: View, ownsOutlineClip: Boolean): Boolean {
+ if (
+ !usesOutlineForRoundedClip() ||
+ !(ownsOutlineClip || !view.clipToOutline) ||
+ view.outlineProvider !== ViewOutlineProvider.BACKGROUND ||
+ view.getTag(R.id.filter) != null
+ ) {
+ return false
+ }
+
+ val composite = getCompositeBackgroundDrawable(view) ?: return false
+ // The outline clip also clips the view's own background, so a feedback ripple drawn there
+ // (bounded or borderless in place) would be cut where the canvas clip never cut it.
+ return composite.outerShadows.isEmpty() &&
+ composite.feedbackUnderlay == null &&
+ (composite.outline?.outlineWidth ?: 0f) == 0f &&
+ composite.getUniformBorderRadiusWithoutBorder() != null
+ }
+
+ private fun clearPaddingBoxOutlineClip(view: View) {
+ if (view.getTag(R.id.padding_box_outline_clip) == true) {
+ view.setTag(R.id.padding_box_outline_clip, null)
+ view.clipToOutline = false
+ }
+ }
+
/**
* Populates [outRect] with the padding box rect of the view.
*
@@ -544,10 +628,11 @@ public object BackgroundStyleApplicator {
)
paddingBoxPath.offset(drawingRect.left.toFloat(), drawingRect.top.toFloat())
- // On Android 28 and below, use antialiased clipping with Porter-Duff compositing. On newer
- // Android versions, use the standard clipPath.
+ // HWUI's clipPath is not anti-aliased before Android 11, so on Android 10 and below use
+ // antialiased clipping with Porter-Duff compositing. Newer versions use the standard
+ // clipPath.
if (
- Build.VERSION.SDK_INT <= Build.VERSION_CODES.P &&
+ Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q &&
view.width > 0 &&
view.height > 0 &&
drawContent != null
@@ -634,6 +719,7 @@ public object BackgroundStyleApplicator {
*/
@JvmStatic
public fun reset(view: View) {
+ clearPaddingBoxOutlineClip(view)
if (view.background is CompositeBackgroundDrawable) {
view.background = (view.background as CompositeBackgroundDrawable).originalBackground
}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CompositeBackgroundDrawable.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CompositeBackgroundDrawable.kt
index b42948166d46..eefabdeb8317 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CompositeBackgroundDrawable.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CompositeBackgroundDrawable.kt
@@ -19,6 +19,7 @@ import android.os.Build
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.uimanager.PixelUtil.dpToPx
+import com.facebook.react.uimanager.PixelUtil.pxToDp
import com.facebook.react.uimanager.style.BorderInsets
import com.facebook.react.uimanager.style.BorderRadiusStyle
@@ -207,8 +208,8 @@ internal class CompositeBackgroundDrawable(
borderRadius?.resolve(
layoutDirection,
context,
- bounds.width().toFloat(),
- bounds.height().toFloat(),
+ bounds.width().pxToDp(),
+ bounds.height().pxToDp(),
)
val computedBorderInsets = borderInsets?.resolve(layoutDirection, context)
@@ -248,6 +249,41 @@ internal class CompositeBackgroundDrawable(
}
}
+ /**
+ * Returns the corner radius, in pixels, when every corner shares one circular radius and no
+ * border is set, so that the border box and the padding box are the same round rect, and the
+ * round rect [getOutline] describes. Returns null otherwise.
+ */
+ fun getUniformBorderRadiusWithoutBorder(): Float? {
+ val computedBorderInsets = borderInsets?.resolve(layoutDirection, context)
+ if (
+ computedBorderInsets != null &&
+ (computedBorderInsets.left != 0f ||
+ computedBorderInsets.top != 0f ||
+ computedBorderInsets.right != 0f ||
+ computedBorderInsets.bottom != 0f)
+ ) {
+ return null
+ }
+
+ val computedBorderRadius =
+ borderRadius?.resolve(
+ layoutDirection,
+ context,
+ bounds.width().pxToDp(),
+ bounds.height().pxToDp(),
+ ) ?: return null
+ if (!computedBorderRadius.isUniform()) {
+ return null
+ }
+
+ val corner = computedBorderRadius.topLeft
+ if (corner.horizontal <= 0f || corner.horizontal != corner.vertical) {
+ return null
+ }
+ return corner.horizontal.dpToPx()
+ }
+
companion object {
private fun createLayersArray(
originalBackground: Drawable?,
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.kt
index b894ac8129f6..25920e503419 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.kt
@@ -382,7 +382,7 @@ public class ReactImageView(
public override fun hasOverlappingRendering(): Boolean = false
public override fun onDraw(canvas: Canvas) {
- BackgroundStyleApplicator.clipToPaddingBoxWithAntiAliasing(this, canvas) {
+ val drawImage = {
try {
super.onDraw(canvas)
} catch (e: RuntimeException) {
@@ -395,6 +395,13 @@ public class ReactImageView(
}
}
}
+ // On Android 9 and 10 an avatar-like padding box takes the outline clip, which needs no layer;
+ // anything else is clipped as before.
+ if (BackgroundStyleApplicator.updatePaddingBoxOutlineClip(this, canvas, true)) {
+ drawImage()
+ } else {
+ BackgroundStyleApplicator.clipToPaddingBoxWithAntiAliasing(this, canvas, drawImage)
+ }
}
public fun maybeUpdateView() {
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt
index 8714a01fa84a..3fdca89ab4ff 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt
@@ -454,9 +454,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
}
public override fun onDraw(canvas: Canvas) {
- if (_overflow != Overflow.VISIBLE) {
- BackgroundStyleApplicator.clipToPaddingBox(this, canvas)
- }
+ BackgroundStyleApplicator.updatePaddingBoxClip(this, canvas, _overflow != Overflow.VISIBLE)
super.onDraw(canvas)
}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt
index 01cb197ff7b5..0ea0f373f0bf 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
- * @generated SignedSource<<5d7adbc00208fc9995b1c3e69c677a38>>
+ * @generated SignedSource<<2d0623d35595eba002baeb40062c5ed3>>
*/
/**
@@ -797,9 +797,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
}
override fun onDraw(canvas: Canvas) {
- if (_overflow != Overflow.VISIBLE) {
- BackgroundStyleApplicator.clipToPaddingBox(this, canvas)
- }
+ BackgroundStyleApplicator.updatePaddingBoxClip(this, canvas, _overflow != Overflow.VISIBLE)
super.onDraw(canvas)
}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt
index fca6538d0774..ee5dd24bb986 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt
@@ -789,9 +789,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
}
public override fun onDraw(canvas: Canvas) {
- if (_overflow != Overflow.VISIBLE) {
- BackgroundStyleApplicator.clipToPaddingBox(this, canvas)
- }
+ BackgroundStyleApplicator.updatePaddingBoxClip(this, canvas, _overflow != Overflow.VISIBLE)
super.onDraw(canvas)
}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt
index 86b91ed41650..5dff56aec10f 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt
@@ -121,9 +121,7 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
@OptIn(UnstableReactNativeAPI::class)
override fun onDraw(canvas: Canvas) {
- if (overflow != Overflow.VISIBLE) {
- BackgroundStyleApplicator.clipToPaddingBox(this, canvas)
- }
+ BackgroundStyleApplicator.updatePaddingBoxClip(this, canvas, overflow != Overflow.VISIBLE)
super.onDraw(canvas)
canvas.translate(
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java
index 451677c8d1f1..156393ffcc93 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java
@@ -283,9 +283,7 @@ protected void onDraw(Canvas canvas) {
setText(spanned);
}
- if (mOverflow != Overflow.VISIBLE) {
- BackgroundStyleApplicator.clipToPaddingBox(this, canvas);
- }
+ BackgroundStyleApplicator.updatePaddingBoxClip(this, canvas, mOverflow != Overflow.VISIBLE);
if (spanned != null) {
Layout layout = getLayout();
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt
index 807740e806db..fa4e44d8da11 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt
@@ -59,7 +59,6 @@ import com.facebook.react.common.assets.ReactFontManager
import com.facebook.react.common.build.ReactBuildConfig
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlags
-import com.facebook.react.uimanager.BackgroundStyleApplicator.clipToPaddingBox
import com.facebook.react.uimanager.BackgroundStyleApplicator.getBackgroundColor
import com.facebook.react.uimanager.BackgroundStyleApplicator.getBorderColor
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBackgroundColor
@@ -67,6 +66,7 @@ import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderColor
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderRadius
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderStyle
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderWidth
+import com.facebook.react.uimanager.BackgroundStyleApplicator.updatePaddingBoxClip
import com.facebook.react.uimanager.LengthPercentage
import com.facebook.react.uimanager.LengthPercentageType
import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel
@@ -1270,9 +1270,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
}
public override fun onDraw(canvas: Canvas) {
- if (overflow != Overflow.VISIBLE) {
- clipToPaddingBox(this, canvas)
- }
+ updatePaddingBoxClip(this, canvas, overflow != Overflow.VISIBLE)
val spanned = text as? Spanned
if (spanned != null) {
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt
index cccf2b546b71..76093bf0fa07 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt
@@ -38,7 +38,6 @@ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.touch.OnInterceptTouchEventListener
import com.facebook.react.touch.ReactHitSlopView
import com.facebook.react.touch.ReactInterceptingViewGroup
-import com.facebook.react.uimanager.BackgroundStyleApplicator.clipToPaddingBox
import com.facebook.react.uimanager.BackgroundStyleApplicator.getPaddingBoxRect
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBackgroundColor
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderColor
@@ -46,6 +45,7 @@ import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderRadius
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderStyle
import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderWidth
import com.facebook.react.uimanager.BackgroundStyleApplicator.setFeedbackUnderlay
+import com.facebook.react.uimanager.BackgroundStyleApplicator.updatePaddingBoxClip
import com.facebook.react.uimanager.BlendModeHelper.needsIsolatedLayer
import com.facebook.react.uimanager.HasElevatedDescendantCache
import com.facebook.react.uimanager.LengthPercentage
@@ -990,9 +990,7 @@ public open class ReactViewGroup public constructor(context: Context?) :
}
override fun dispatchDraw(canvas: Canvas) {
- if (_overflow != Overflow.VISIBLE || getTag(R.id.filter) != null) {
- clipToPaddingBox(this, canvas)
- }
+ updatePaddingBoxClip(this, canvas, _overflow != Overflow.VISIBLE || getTag(R.id.filter) != null)
super.dispatchDraw(canvas)
}
diff --git a/packages/react-native/ReactAndroid/src/main/res/views/uimanager/values/ids.xml b/packages/react-native/ReactAndroid/src/main/res/views/uimanager/values/ids.xml
index d03b719892c1..91110f0246e3 100644
--- a/packages/react-native/ReactAndroid/src/main/res/views/uimanager/values/ids.xml
+++ b/packages/react-native/ReactAndroid/src/main/res/views/uimanager/values/ids.xml
@@ -80,6 +80,9 @@
+
+
+
diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/BackgroundStyleApplicatorClipTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/BackgroundStyleApplicatorClipTest.kt
new file mode 100644
index 000000000000..b6ccf9e6e476
--- /dev/null
+++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/BackgroundStyleApplicatorClipTest.kt
@@ -0,0 +1,299 @@
+/*
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+package com.facebook.react.uimanager
+
+import android.content.Context
+import android.graphics.Canvas
+import android.graphics.Outline
+import android.graphics.Path
+import android.graphics.Rect
+import android.graphics.drawable.ColorDrawable
+import android.os.Build
+import android.view.View
+import android.view.ViewOutlineProvider
+import com.facebook.react.R
+import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
+import com.facebook.react.uimanager.style.BorderRadiusProp
+import com.facebook.react.uimanager.style.LogicalEdge
+import com.facebook.react.views.view.ReactViewGroup
+import org.assertj.core.api.Assertions.assertThat
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.any
+import org.mockito.kotlin.anyOrNull
+import org.mockito.kotlin.atLeastOnce
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.never
+import org.mockito.kotlin.verify
+import org.mockito.kotlin.whenever
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.RuntimeEnvironment
+import org.robolectric.annotation.Config
+import org.robolectric.util.ReflectionHelpers
+
+/**
+ * Tests for the anti-aliased clips [BackgroundStyleApplicator.updatePaddingBoxClip] uses on API
+ * 28-29.
+ *
+ * The clip choice depends only on `Build.VERSION.SDK_INT`, so the tests set it rather than running
+ * in older Robolectric SDKs: each extra SDK loads its own android-all runtime into the test JVM.
+ */
+@RunWith(RobolectricTestRunner::class)
+@Config(qualifiers = "xhdpi")
+class BackgroundStyleApplicatorClipTest {
+
+ private lateinit var context: Context
+ private val sdkInt = Build.VERSION.SDK_INT
+
+ @Before
+ fun setUp() {
+ ReactNativeFeatureFlagsForTests.setUp()
+ context = RuntimeEnvironment.getApplication()
+ DisplayMetricsHolder.setScreenDisplayMetrics(context.resources.displayMetrics)
+ setSdkInt(Build.VERSION_CODES.P)
+ }
+
+ @After
+ fun tearDown() {
+ setSdkInt(sdkInt)
+ }
+
+ @Test
+ fun `uniform radius without border clips with a round rect outline`() {
+ val view = roundedView()
+ val canvas = hardwareCanvas()
+
+ BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true)
+
+ assertThat(view.clipToOutline).isTrue()
+ verify(canvas, never()).clipPath(any())
+ val outline = Outline()
+ view.background.getOutline(outline)
+ val rect = Rect()
+ assertThat(outline.getRect(rect)).isTrue()
+ assertThat(rect).isEqualTo(Rect(0, 0, 200, 200))
+ // 8dp at xhdpi
+ assertThat(outline.radius).isEqualTo(16f)
+ }
+
+ @Test
+ fun `border falls back to the canvas clip`() {
+ val view = roundedView()
+ BackgroundStyleApplicator.setBorderWidth(view, LogicalEdge.ALL, 2f)
+ val canvas = hardwareCanvas()
+
+ BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true)
+
+ assertThat(view.clipToOutline).isFalse()
+ verify(canvas).clipPath(any())
+ }
+
+ @Test
+ fun `different corner radii fall back to the canvas clip`() {
+ val view = roundedView()
+ BackgroundStyleApplicator.setBorderRadius(
+ view,
+ BorderRadiusProp.BORDER_TOP_LEFT_RADIUS,
+ LengthPercentage(4f, LengthPercentageType.POINT),
+ )
+ val canvas = hardwareCanvas()
+
+ BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true)
+
+ assertThat(view.clipToOutline).isFalse()
+ verify(canvas).clipPath(any())
+ }
+
+ @Test
+ fun `software draw clips the canvas and keeps the outline clip`() {
+ val view = roundedView()
+ BackgroundStyleApplicator.updatePaddingBoxClip(view, hardwareCanvas(), true)
+ val softwareCanvas = mock