From 089a1015443b614096945f5a14016aa86e5c7d10 Mon Sep 17 00:00:00 2001 From: Yevhenii Date: Thu, 15 Oct 2026 10:23:26 -0700 Subject: [PATCH] Read only the drawn child's mix-blend-mode in Android drawChild (#59016) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `ReactViewGroup.drawChild` and `ReactRootView.drawChild` call `BlendModeHelper.needsIsolatedLayer(this)` before they read the drawn child's `mix_blend_mode` tag. `needsIsolatedLayer` reads that tag on every child of the group, and `dispatchDraw` calls `drawChild` once per child, so recording the display list of a group with N children reads N² tags. When no child sets a blend mode — the common case — `any` never short-circuits, so the whole scan runs for every child of every group that records. I found it profiling flings over a grid of a few hundred views on two release builds: sampling the main thread, `needsIsolatedLayer` under `drawChild` was 37% of display-list recording on a Snapdragon 845 (Android 10) and 46% on a Snapdragon 8 Gen 2 (Android 14). The guard adds nothing to the decision `drawChild` makes. A child that carries the tag makes `needsIsolatedLayer` true by definition, and a child without it draws with no layer whatever the guard says. So both overrides now read the drawn child's own tag. `draw()` keeps its group-wide check, which decides whether the whole group renders offscreen and runs once per recording. I searched the tracker for `needsIsolatedLayer`, `BlendModeHelper`, `mix_blend_mode` and `drawChild` and found no existing report or PR. ## Changelog: [ANDROID] [FIXED] - Drawing a view group no longer reads every sibling's mix-blend-mode for each child it draws Pull Request resolved: https://github.com/react/react-native/pull/59016 Test Plan: - `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.views.view.ReactViewGroupTest" -Preact.internal.useHermesStable=true`: on `main` the new `drawing a child reads no sibling's blend mode` test fails with `expected:<[3]> but was:<[21]>` — each child of a 20-child group has its tag read 21 times, against 3 in a 2-child group; with the fix both groups read each child equally often, and all 19 tests in the class pass. - The new `a child with a blend mode draws into a layer with that blend mode` test records `[null, MULTIPLY]` — the group's isolated layer, then the child's — before and after the change; with the child's layer skipped it fails with `Expecting actual: [null] to contain exactly (and in same order): [null, MULTIPLY]`. - `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest -PenableWarningsAsErrors=true -Preact.internal.useHermesStable=true`: 707 tests, 0 failures. - `node ./scripts/format-kotlin.js --check` and `node ./scripts/format-java.js --check` pass. - The same change on 0.86.3, in an app with a method trace over those flings: `needsIsolatedLayer` ran 1,608 times for 1,400 `drawChild` calls before and 182 times for 3,234 after, every one of them from `draw()`. A `mixBlendMode: 'multiply'` view in the same app still blends. - Not run: the RNTester build and Android lint (`build_android`). Reviewed By: andrewdacenko Differential Revision: D124965507 Pulled By: javache fbshipit-source-id: e0155f6de592f9d14e8cb17a6b092b5fda711d04 --- .../com/facebook/react/ReactRootView.java | 3 +- .../react/views/view/ReactViewGroup.kt | 2 +- .../react/views/view/ReactViewGroupTest.kt | 50 +++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index 27091027b25..34fcb26db22 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -8,7 +8,6 @@ package com.facebook.react; import static com.facebook.infer.annotation.ThreadConfined.UI; -import static com.facebook.react.uimanager.BlendModeHelper.needsIsolatedLayer; import static com.facebook.react.uimanager.common.UIManagerType.FABRIC; import static com.facebook.systrace.Systrace.TRACE_TAG_REACT; @@ -308,7 +307,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot { protected boolean drawChild(Canvas canvas, View child, long drawingTime) { BlendMode mixBlendMode = null; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && needsIsolatedLayer(this)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { mixBlendMode = (BlendMode) child.getTag(R.id.mix_blend_mode); if (mixBlendMode != null) { Paint p = new Paint(); 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 363f1336d00..c861a961cf3 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 @@ -1003,7 +1003,7 @@ public open class ReactViewGroup public constructor(context: Context?) : } var mixBlendMode: BlendMode? = null - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && needsIsolatedLayer(this)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { mixBlendMode = child.getTag(R.id.mix_blend_mode) as? BlendMode if (mixBlendMode != null) { val p = Paint() diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt index 56383ff7efe..4bc27295114 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt @@ -9,9 +9,13 @@ package com.facebook.react.views.view import android.app.Activity import android.content.Context +import android.graphics.BlendMode +import android.graphics.Canvas +import android.graphics.Paint import android.view.View import android.view.ViewGroup import android.widget.FrameLayout +import com.facebook.react.R import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests @@ -67,6 +71,32 @@ class ReactViewGroupTest { assertThat(rvg.childCount).isEqualTo(20) } + @Test + fun `mix blend mode - drawing a child reads no sibling's blend mode`() { + fun blendModeReadsPerChild(childCount: Int): Int { + val rvg = ReactViewGroup(context) + val children = List(childCount) { BlendModeReadCountingView(context) } + children.forEach { rvg.addView(it) } + rvg.layout(0, 0, 100, 100) + rvg.draw(Canvas()) + return children.maxOf { it.blendModeReads } + } + + assertThat(blendModeReadsPerChild(20)).isEqualTo(blendModeReadsPerChild(2)) + } + + @Test + fun `mix blend mode - a child with a blend mode draws into a layer with that blend mode`() { + val rvg = ReactViewGroup(context) + rvg.addView(View(context)) + rvg.addView(View(context).apply { setTag(R.id.mix_blend_mode, BlendMode.MULTIPLY) }) + rvg.addView(View(context)) + rvg.layout(0, 0, 100, 100) + val canvas = LayerRecordingCanvas() + rvg.draw(canvas) + assertThat(canvas.layerBlendModes).containsExactly(null, BlendMode.MULTIPLY) + } + @Test fun `auto offscreen compositing - true when flag on, alpha lt 1, and elevated descendant`() { overrideAutoOffscreenFlag(true) @@ -294,3 +324,23 @@ class TestView(context: Context, yPos: Int) : View(context) { class TestParent(context: Context) : ViewGroup(context) { override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) = Unit } + +private class BlendModeReadCountingView(context: Context) : View(context) { + var blendModeReads = 0 + + override fun getTag(key: Int): Any? { + if (key == R.id.mix_blend_mode) { + blendModeReads++ + } + return super.getTag(key) + } +} + +private class LayerRecordingCanvas : Canvas() { + val layerBlendModes = mutableListOf() + + override fun saveLayer(left: Float, top: Float, right: Float, bottom: Float, paint: Paint?): Int { + layerBlendModes.add(paint?.blendMode) + return super.saveLayer(left, top, right, bottom, paint) + } +}