mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix nougat blank rendering with antialiased border radius clipping (#56336)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56336 Moves `clipRect` inside the outer `saveLayer` in `clipWithAntiAliasing()` to fix blank rendering on API 24 (Nougat) devices. https://github.com/facebook/react-native/pull/55762 added `canvas.withClip()` to fix black pixels on partially off-screen views. However, it wrapped the entire compositing chain in an extra `save()`/`clipRect`/`restore()`, creating a triple-nested save/restore stack (`save` -> `saveLayer` -> `saveLayer(DST_IN)`) that breaks Porter-Duff compositing on API 24's HWUI renderer. Moving `clipRect` inside the `saveLayer` reduces nesting from 3 to 2 levels while preserving the black-pixel mitigation. The `saveLayer` already saves and restores clip state, so a separate `save()`/`restore()` wrapper is unnecessary. Changelog: [Android][Fixed] - Fix image content disappearing on API 24 (Nougat) when antialiased border radius clipping is applied Reviewed By: NickGerleman Differential Revision: D99677658 fbshipit-source-id: 55a9c84655cc08b05b95c29725492b9b8f759331
This commit is contained in:
1 parent
a937ad4509
commit
fdd6ca5bb1
1 file changed
+38
-38
+38
-38
@@ -20,7 +20,6 @@ import android.os.Build
|
||||
import android.view.View
|
||||
import android.widget.ImageView
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.core.graphics.withClip
|
||||
import com.facebook.react.bridge.ReadableArray
|
||||
import com.facebook.react.common.annotations.UnstableReactNativeAPI
|
||||
import com.facebook.react.uimanager.PixelUtil.dpToPx
|
||||
@@ -573,49 +572,50 @@ public object BackgroundStyleApplicator {
|
||||
paddingBoxPath: Path,
|
||||
drawContent: () -> Unit,
|
||||
) {
|
||||
// Clip to the view's own bounds before saveLayer. On API <= 28 hardware-accelerated canvases,
|
||||
// Save the layer for Porter-Duff compositing
|
||||
val saveCount = canvas.saveLayer(0f, 0f, view.width.toFloat(), view.height.toFloat(), null)
|
||||
|
||||
// Clip to the view's own bounds inside the layer. On API <= 28 hardware-accelerated canvases,
|
||||
// the window boundary is tracked by the GPU scissor but not reflected in the canvas clip stack.
|
||||
// Without an explicit software clip, saveLayer may allocate a buffer with uninitialized pixels
|
||||
// beyond the GPU scissor. Adding clipRect in the view's local coordinate space forces HWUI to
|
||||
// include it in the clip stack, ensuring saveLayer properly constrains its buffer. This clip is
|
||||
// stable across parent transform animations since it's in the view's own coordinate space.
|
||||
canvas.withClip(0, 0, view.width, view.height) {
|
||||
// Save the layer for Porter-Duff compositing
|
||||
val saveCount = canvas.saveLayer(0f, 0f, view.width.toFloat(), view.height.toFloat(), null)
|
||||
// beyond the GPU scissor. Adding clipRect inside the layer (rather than wrapping it with
|
||||
// canvas.withClip) avoids an extra save/restore nesting level that breaks Porter-Duff
|
||||
// compositing on API 24's HWUI renderer. The saveLayer already saves and restores the clip
|
||||
// state, so a separate save/restore wrapper is unnecessary.
|
||||
canvas.clipRect(0, 0, view.width, view.height)
|
||||
|
||||
// Draw the content first
|
||||
drawContent()
|
||||
// Draw the content first
|
||||
drawContent()
|
||||
|
||||
val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
maskPaint.style = Paint.Style.FILL
|
||||
val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
maskPaint.style = Paint.Style.FILL
|
||||
|
||||
// Transparent pixels with INVERSE_WINDING only works on API 28
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
maskPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
|
||||
maskPaint.color = Color.TRANSPARENT
|
||||
paddingBoxPath.setFillType(Path.FillType.INVERSE_WINDING)
|
||||
canvas.drawPath(paddingBoxPath, maskPaint)
|
||||
} else {
|
||||
// API < 28: Use a nested saveLayer with DST_IN compositing to mask content to the
|
||||
// padding box path. EVEN_ODD fill + DST_OUT has rendering bugs on API 24's hardware
|
||||
// renderer, so we avoid that technique. Instead, draw the mask shape into a separate
|
||||
// layer; when restored with DST_IN, content is preserved only where the mask is opaque.
|
||||
val dstInPaint = Paint()
|
||||
dstInPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
|
||||
val maskSave =
|
||||
canvas.saveLayer(0f, 0f, view.width.toFloat(), view.height.toFloat(), dstInPaint)
|
||||
// Clear the layer to ensure it starts fully transparent. On API 24, saveLayer may not
|
||||
// initialize the buffer to transparent, causing DST_IN to see non-zero alpha everywhere.
|
||||
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
|
||||
maskPaint.xfermode = null
|
||||
maskPaint.color = Color.BLACK
|
||||
canvas.drawPath(paddingBoxPath, maskPaint)
|
||||
canvas.restoreToCount(maskSave)
|
||||
}
|
||||
|
||||
// Restore the layer
|
||||
canvas.restoreToCount(saveCount)
|
||||
// Transparent pixels with INVERSE_WINDING only works on API 28
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
maskPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
|
||||
maskPaint.color = Color.TRANSPARENT
|
||||
paddingBoxPath.setFillType(Path.FillType.INVERSE_WINDING)
|
||||
canvas.drawPath(paddingBoxPath, maskPaint)
|
||||
} else {
|
||||
// API < 28: Use a nested saveLayer with DST_IN compositing to mask content to the
|
||||
// padding box path. EVEN_ODD fill + DST_OUT has rendering bugs on API 24's hardware
|
||||
// renderer, so we avoid that technique. Instead, draw the mask shape into a separate
|
||||
// layer; when restored with DST_IN, content is preserved only where the mask is opaque.
|
||||
val dstInPaint = Paint()
|
||||
dstInPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
|
||||
val maskSave =
|
||||
canvas.saveLayer(0f, 0f, view.width.toFloat(), view.height.toFloat(), dstInPaint)
|
||||
// Clear the layer to ensure it starts fully transparent. On API 24, saveLayer may not
|
||||
// initialize the buffer to transparent, causing DST_IN to see non-zero alpha everywhere.
|
||||
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
|
||||
maskPaint.xfermode = null
|
||||
maskPaint.color = Color.BLACK
|
||||
canvas.drawPath(paddingBoxPath, maskPaint)
|
||||
canvas.restoreToCount(maskSave)
|
||||
}
|
||||
|
||||
// Restore the layer
|
||||
canvas.restoreToCount(saveCount)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user