From a087a596d7e0238aaf024d1f3214d1821121ae18 Mon Sep 17 00:00:00 2001 From: knappa Date: Wed, 28 Oct 2026 14:21:41 -0400 Subject: [PATCH 1/3] Anti-aliased rounded overflow clips on Android 9 and 10 Before Android 11, HWUI's Canvas.clipPath is not anti-aliased, so a view with overflow: hidden and a borderRadius has stepped corners on Android 9 and 10. There, clip with the view's outline when the padding box is a round rect with one circular radius and no border, and nothing is drawn outside the bounds for the outline to cut off. HWUI clips outlines with an anti-aliased round rect and no offscreen layer. ReactImageView takes the same path when it can. Every other padding box keeps the canvas clip: masking it with a layer cost 99% janky frames for a screen of bordered cards on Android 9. BackgroundStyleApplicator.updatePaddingBoxClip is the new per-draw entry point for RN's own views; clipToPaddingBox keeps its stateless contract. getOutline resolves percentage radii against the bounds in dp, as the padding box does. Assisted-by: Claude-Code:claude-opus-5-5 --- .../ReactAndroid/api/ReactAndroid.api | 2 + .../uimanager/BackgroundStyleApplicator.kt | 92 +++++- .../drawable/CompositeBackgroundDrawable.kt | 40 ++- .../react/views/image/ReactImageView.kt | 9 +- .../views/scroll/ReactHorizontalScrollView.kt | 4 +- .../views/scroll/ReactNestedScrollView.kt | 6 +- .../react/views/scroll/ReactScrollView.kt | 4 +- .../views/text/PreparedLayoutTextView.kt | 4 +- .../react/views/text/ReactTextView.java | 4 +- .../react/views/textinput/ReactEditText.kt | 6 +- .../react/views/view/ReactViewGroup.kt | 6 +- .../main/res/views/uimanager/values/ids.xml | 3 + .../BackgroundStyleApplicatorClipTest.kt | 274 ++++++++++++++++++ 13 files changed, 424 insertions(+), 30 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/BackgroundStyleApplicatorClipTest.kt 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..47dda8b5ae71 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/BackgroundStyleApplicatorClipTest.kt @@ -0,0 +1,274 @@ +/* + * 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.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.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 + +/** + * Tests for the anti-aliased clips [BackgroundStyleApplicator.updatePaddingBoxClip] uses on API + * 28-29 + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], qualifiers = "xhdpi") +class BackgroundStyleApplicatorClipTest { + + private lateinit var context: Context + + @Before + fun setUp() { + ReactNativeFeatureFlagsForTests.setUp() + context = RuntimeEnvironment.getApplication() + DisplayMetricsHolder.setScreenDisplayMetrics(context.resources.displayMetrics) + } + + @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() + + BackgroundStyleApplicator.updatePaddingBoxClip(view, softwareCanvas, true) + + verify(softwareCanvas).clipPath(any()) + assertThat(view.clipToOutline).isTrue() + } + + @Test + fun `clip false removes the outline clip`() { + val view = roundedView() + BackgroundStyleApplicator.updatePaddingBoxClip(view, hardwareCanvas(), true) + val canvas = hardwareCanvas() + + BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, false) + + assertThat(view.clipToOutline).isFalse() + verify(canvas, never()).clipPath(any()) + } + + @Test + fun `clipToOutline set by someone else is left alone`() { + val view = roundedView() + view.clipToOutline = true + val canvas = hardwareCanvas() + + BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true) + BackgroundStyleApplicator.updatePaddingBoxClip(view, hardwareCanvas(), false) + + assertThat(view.clipToOutline).isTrue() + verify(canvas).clipPath(any()) + } + + @Test + fun `a bordered ReactViewGroup keeps the canvas clip, with no layer`() { + val view = ReactViewGroup(context) + view.setBorderRadius(16f) + view.setBorderWidth(8, 2f) + view.layout(0, 0, 200, 200) + view.background.setBounds(0, 0, 200, 200) + view.overflow = "hidden" + val canvas = hardwareCanvas() + + view.draw(canvas) + + assertThat(view.clipToOutline).isFalse() + // The rounded border draws through clipPath too, so only require at least one. + verify(canvas, atLeastOnce()).clipPath(any()) + verify(canvas, never()).saveLayer(any(), any(), any(), any(), anyOrNull()) + } + + @Test + fun `updatePaddingBoxOutlineClip reports whether the outline clips, without clipping the canvas`() { + val rounded = roundedView() + val bordered = roundedView() + BackgroundStyleApplicator.setBorderWidth(bordered, LogicalEdge.ALL, 2f) + val canvas = hardwareCanvas() + + assertThat(BackgroundStyleApplicator.updatePaddingBoxOutlineClip(rounded, canvas, true)) + .isTrue() + assertThat(BackgroundStyleApplicator.updatePaddingBoxOutlineClip(bordered, canvas, true)) + .isFalse() + verify(canvas, never()).clipPath(any()) + } + + @Test + fun `ReactViewGroup switches the outline clip with overflow`() { + val view = ReactViewGroup(context) + view.setBorderRadius(16f) + view.layout(0, 0, 200, 200) + view.background.setBounds(0, 0, 200, 200) + + view.overflow = "hidden" + view.draw(hardwareCanvas()) + assertThat(view.clipToOutline).isTrue() + + view.overflow = "visible" + view.draw(hardwareCanvas()) + assertThat(view.clipToOutline).isFalse() + } + + @Test + @Config(sdk = [29]) + fun `API 29 clips with the outline too`() { + val view = roundedView() + val canvas = hardwareCanvas() + + BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true) + + assertThat(view.clipToOutline).isTrue() + verify(canvas, never()).clipPath(any()) + } + + @Test + @Config(sdk = [33]) + fun `a percentage radius resolves against the bounds in dp`() { + val view = View(context) + BackgroundStyleApplicator.setBorderRadius( + view, + BorderRadiusProp.BORDER_RADIUS, + LengthPercentage(10f, LengthPercentageType.PERCENT), + ) + view.layout(0, 0, 200, 200) + view.background.setBounds(0, 0, 200, 200) + val outline = Outline() + + view.background.getOutline(outline) + + // 10% of 100dp is 10dp, 20px at xhdpi + assertThat(outline.radius).isEqualTo(20f) + } + + @Test + fun `a feedback ripple, a filter or a custom outline provider keeps the canvas clip`() { + val rippled = roundedView() + BackgroundStyleApplicator.setFeedbackUnderlay(rippled, ColorDrawable()) + rippled.background.setBounds(0, 0, 200, 200) + val filtered = roundedView() + filtered.setTag(R.id.filter, Any()) + val outlined = roundedView() + outlined.outlineProvider = ViewOutlineProvider.BOUNDS + + for (view in listOf(rippled, filtered, outlined)) { + val canvas = hardwareCanvas() + BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true) + assertThat(view.clipToOutline).isFalse() + verify(canvas).clipPath(any()) + } + } + + @Test + @Config(sdk = [27]) + fun `API 27 keeps the canvas clip`() { + assertCanvasClip() + } + + @Test + @Config(sdk = [30]) + fun `anti-aliased clipPath on API 30 keeps the canvas clip`() { + assertCanvasClip() + } + + private fun assertCanvasClip() { + val view = roundedView() + val canvas = hardwareCanvas() + + BackgroundStyleApplicator.updatePaddingBoxClip(view, canvas, true) + + assertThat(view.clipToOutline).isFalse() + verify(canvas).clipPath(any()) + } + + private fun roundedView(): View { + val view = View(context) + BackgroundStyleApplicator.setBorderRadius( + view, + BorderRadiusProp.BORDER_RADIUS, + LengthPercentage(8f, LengthPercentageType.POINT), + ) + view.layout(0, 0, 200, 200) + view.background.setBounds(0, 0, 200, 200) + return view + } + + private fun hardwareCanvas(): Canvas { + val canvas = mock() + whenever(canvas.isHardwareAccelerated).thenReturn(true) + return canvas + } +} From c52258233e25c9df714b374723c5e2f1da503c78 Mon Sep 17 00:00:00 2001 From: knappa Date: Wed, 28 Oct 2026 14:39:16 -0400 Subject: [PATCH 2/3] Use BackgroundStyleApplicator.setBorderRadius in the clip tests ReactViewGroup.setBorderRadius(Float) is deprecated, which fails the unit test compile under -PenableWarningsAsErrors=true. Assisted-by: Claude-Code:claude-opus-5-5 --- .../uimanager/BackgroundStyleApplicatorClipTest.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 index 47dda8b5ae71..dcfdbccc517b 100644 --- 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 @@ -138,7 +138,11 @@ class BackgroundStyleApplicatorClipTest { @Test fun `a bordered ReactViewGroup keeps the canvas clip, with no layer`() { val view = ReactViewGroup(context) - view.setBorderRadius(16f) + BackgroundStyleApplicator.setBorderRadius( + view, + BorderRadiusProp.BORDER_RADIUS, + LengthPercentage(16f, LengthPercentageType.POINT), + ) view.setBorderWidth(8, 2f) view.layout(0, 0, 200, 200) view.background.setBounds(0, 0, 200, 200) @@ -170,7 +174,11 @@ class BackgroundStyleApplicatorClipTest { @Test fun `ReactViewGroup switches the outline clip with overflow`() { val view = ReactViewGroup(context) - view.setBorderRadius(16f) + BackgroundStyleApplicator.setBorderRadius( + view, + BorderRadiusProp.BORDER_RADIUS, + LengthPercentage(16f, LengthPercentageType.POINT), + ) view.layout(0, 0, 200, 200) view.background.setBounds(0, 0, 200, 200) From 900b43e10ed8869ab1551ed95e1eb8c360f62cf1 Mon Sep 17 00:00:00 2001 From: knappa Date: Thu, 29 Oct 2026 00:12:31 -0400 Subject: [PATCH 3/3] Run the clip tests in the default Robolectric SDK The tests ran in Robolectric SDKs 27-30, which nothing else in the suite uses. Each SDK loads its own android-all runtime into the test JVM, and build_android ran out of memory in Robolectric and hung until the job timed out. The clip choice depends only on Build.VERSION.SDK_INT, so the tests now set it instead. Assisted-by: Claude-Code:claude-opus-5-5 --- .../BackgroundStyleApplicatorClipTest.kt | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) 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 index dcfdbccc517b..b6ccf9e6e476 100644 --- 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 @@ -13,6 +13,7 @@ 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 @@ -21,6 +22,7 @@ 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 @@ -34,22 +36,33 @@ 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 + * 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(sdk = [28], qualifiers = "xhdpi") +@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 @@ -192,8 +205,8 @@ class BackgroundStyleApplicatorClipTest { } @Test - @Config(sdk = [29]) fun `API 29 clips with the outline too`() { + setSdkInt(Build.VERSION_CODES.Q) val view = roundedView() val canvas = hardwareCanvas() @@ -204,8 +217,8 @@ class BackgroundStyleApplicatorClipTest { } @Test - @Config(sdk = [33]) fun `a percentage radius resolves against the bounds in dp`() { + setSdkInt(sdkInt) val view = View(context) BackgroundStyleApplicator.setBorderRadius( view, @@ -241,14 +254,14 @@ class BackgroundStyleApplicatorClipTest { } @Test - @Config(sdk = [27]) fun `API 27 keeps the canvas clip`() { + setSdkInt(Build.VERSION_CODES.O_MR1) assertCanvasClip() } @Test - @Config(sdk = [30]) fun `anti-aliased clipPath on API 30 keeps the canvas clip`() { + setSdkInt(Build.VERSION_CODES.R) assertCanvasClip() } @@ -274,6 +287,10 @@ class BackgroundStyleApplicatorClipTest { return view } + private fun setSdkInt(sdk: Int) { + ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", sdk) + } + private fun hardwareCanvas(): Canvas { val canvas = mock() whenever(canvas.isHardwareAccelerated).thenReturn(true)