Skip to content

Commit aa37081

Browse files
committed
refactor: run canvas save/restore conditionally
1 parent 295c373 commit aa37081

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BackgroundStyleApplicator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,12 @@ public object BackgroundStyleApplicator {
480480
view.invalidate()
481481
}
482482

483+
@JvmStatic
484+
public fun getClipPath(view: View): ClipPath? = view.getTag(R.id.clip_path) as? ClipPath
485+
483486
@JvmStatic
484487
public fun applyClipPathIfPresent(view: View, canvas: Canvas) {
485-
val clipPath = view.getTag(R.id.clip_path) as? ClipPath ?: return
488+
val clipPath = getClipPath(view) ?: return
486489
val bounds = getGeometryBoxBounds(view, clipPath.geometryBox, getComputedBorderInsets(view))
487490
val drawingRect = Rect()
488491
view.getDrawingRect(drawingRect)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/ClipPath.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public enum class GeometryBox {
226226
}
227227
}
228228

229-
internal data class ClipPath(
229+
public data class ClipPath(
230230
val shape: BasicShape? = null,
231231
val geometryBox: GeometryBox? = null,
232232
) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import android.graphics.Shader.TileMode
2222
import android.graphics.drawable.Animatable
2323
import android.graphics.drawable.Drawable
2424
import android.net.Uri
25-
import androidx.core.graphics.withSave
2625
import com.facebook.common.references.CloseableReference
2726
import com.facebook.common.util.UriUtil
2827
import com.facebook.drawee.backends.pipeline.Fresco
@@ -373,9 +372,16 @@ public class ReactImageView(
373372
public override fun hasOverlappingRendering(): Boolean = false
374373

375374
public override fun draw(canvas: Canvas) {
376-
canvas.withSave {
377-
BackgroundStyleApplicator.applyClipPathIfPresent(this@ReactImageView, this)
378-
super.draw(this)
375+
val clipPath = BackgroundStyleApplicator.getClipPath(this)
376+
if (clipPath != null) {
377+
canvas.save()
378+
BackgroundStyleApplicator.applyClipPathIfPresent(this, canvas)
379+
}
380+
381+
super.onDraw(canvas)
382+
383+
if (clipPath != null) {
384+
canvas.restore()
379385
}
380386
}
381387

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import android.view.ViewGroup
2222
import androidx.annotation.ColorInt
2323
import androidx.annotation.DoNotInline
2424
import androidx.annotation.RequiresApi
25-
import androidx.core.graphics.withSave
2625
import androidx.core.view.ViewCompat
2726
import com.facebook.proguard.annotations.DoNotStrip
2827
import com.facebook.react.uimanager.BackgroundStyleApplicator
@@ -103,9 +102,16 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
103102
}
104103

105104
override fun draw(canvas: Canvas) {
106-
canvas.withSave {
107-
BackgroundStyleApplicator.applyClipPathIfPresent(this@PreparedLayoutTextView, this)
108-
super.onDraw(canvas)
105+
val clipPath = BackgroundStyleApplicator.getClipPath(this)
106+
if (clipPath != null) {
107+
canvas.save()
108+
BackgroundStyleApplicator.applyClipPathIfPresent(this, canvas)
109+
}
110+
111+
super.draw(canvas)
112+
113+
if (clipPath != null) {
114+
canvas.restore()
109115
}
110116
}
111117

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import com.facebook.react.views.text.internal.span.TextInlineImageSpan
9191
import java.util.concurrent.CopyOnWriteArrayList
9292
import kotlin.math.max
9393
import kotlin.math.min
94-
import androidx.core.graphics.withSave
9594

9695
/**
9796
* A wrapper around the EditText that lets us better control what happens when an EditText gets
@@ -1206,9 +1205,16 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
12061205
}
12071206

12081207
public override fun draw(canvas: Canvas) {
1209-
canvas.withSave {
1210-
BackgroundStyleApplicator.applyClipPathIfPresent(this@ReactEditText, this)
1211-
super.draw(this)
1208+
val clipPath = BackgroundStyleApplicator.getClipPath(this)
1209+
if (clipPath != null) {
1210+
canvas.save()
1211+
BackgroundStyleApplicator.applyClipPathIfPresent(this, canvas)
1212+
}
1213+
1214+
super.draw(canvas)
1215+
1216+
if (clipPath != null) {
1217+
canvas.restore()
12121218
}
12131219
}
12141220

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,17 @@ public open class ReactViewGroup public constructor(context: Context?) :
928928
clipToPaddingBox(this, canvas)
929929
}
930930

931-
BackgroundStyleApplicator.applyClipPathIfPresent(this, canvas)
931+
val clipPath = BackgroundStyleApplicator.getClipPath(this)
932+
if (clipPath != null) {
933+
canvas.save()
934+
BackgroundStyleApplicator.applyClipPathIfPresent(this, canvas)
935+
}
936+
932937
super.dispatchDraw(canvas)
938+
939+
if (clipPath != null) {
940+
canvas.restore()
941+
}
933942
}
934943

935944
override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {

0 commit comments

Comments
 (0)