Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>>
*/

/**
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ 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
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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ 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
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
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<!-- tag is used to store graphical filter effects to apply to the view -->
<item type="id" name="filter"/>

<!-- tag is set while BackgroundStyleApplicator clips the view's padding box with its outline -->
<item type="id" name="padding_box_outline_clip"/>

<!-- tag is used to store mixBlendMode effects to apply to the view -->
<item type="id" name="mix_blend_mode"/>

Expand Down
Loading
Loading