diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index 2727e880d083..e9137940991d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -60,6 +60,7 @@ import com.facebook.react.fabric.interop.UIBlockViewResolver; import com.facebook.react.fabric.mounting.MountItemDispatcher; import com.facebook.react.fabric.mounting.MountingManager; +import com.facebook.react.fabric.mounting.PendingSynchronousProps; import com.facebook.react.fabric.mounting.SurfaceMountingManager; import com.facebook.react.fabric.mounting.mountitems.BatchMountItem; import com.facebook.react.fabric.mounting.mountitems.BatchedAnimatedPropsMountItem; @@ -229,6 +230,9 @@ public class FabricUIManager // from C++ to exceed 9,999 and it should be obvious what's going on when analyzing performance. private int mCurrentSynchronousCommitNumber = 10000; + @ThreadConfined(UI) + private final PendingSynchronousProps mPendingSynchronousProps; + private final MountingManager.MountItemExecutor mMountItemExecutor = new MountingManager.MountItemExecutor() { @Override @@ -258,6 +262,7 @@ public FabricUIManager( mMountingManager = new MountingManager(viewManagerRegistry, mMountItemExecutor); mMountItemDispatcher = new MountItemDispatcher(mMountingManager, new MountItemDispatchListener()); + mPendingSynchronousProps = new PendingSynchronousProps(mMountItemDispatcher); mEventDispatcher = new FabricEventDispatcher(reactContext, new FabricEventEmitter(this)); mBatchEventDispatchedListener = batchEventDispatchedListener; mReactApplicationContext.addLifecycleEventListener(this); @@ -830,12 +835,10 @@ public void synchronouslyUpdateViewOnUIThread(final int reactTag, final Readable // android.view.View.updateDisplayListIfDirty(View.java:20466) // 3. A view is deleted while its parent is being drawn, causing a crash. - MountItem synchronousMountItem = new SynchronousMountItem(reactTag, props); - // If the reactTag exists, we assume that it might at the end of the next // batch of MountItems. Otherwise, we try to execute immediately. if (!mMountingManager.getViewExists(reactTag)) { - mMountItemDispatcher.addMountItem(synchronousMountItem); + mPendingSynchronousProps.add(reactTag, props); return; } @@ -850,7 +853,8 @@ public void synchronouslyUpdateViewOnUIThread(final int reactTag, final Readable (IS_DEVELOPMENT_ENVIRONMENT ? props.toHashMap().toString() : "")); } - synchronousMountItem.execute(mMountingManager); + new SynchronousMountItem(reactTag, mPendingSynchronousProps.takeMergedWith(reactTag, props)) + .execute(mMountingManager); ReactMarker.logFabricMarker( ReactMarkerConstants.FABRIC_UPDATE_UI_MAIN_THREAD_END, null, commitNumber); @@ -878,7 +882,8 @@ private void synchronouslyUpdateViewBatch( rawPropsBuffer.length); } - new BatchedAnimatedPropsMountItem(intBuffer, doubleBuffer, rawPropsBuffer, mMountItemDispatcher) + new BatchedAnimatedPropsMountItem( + intBuffer, doubleBuffer, rawPropsBuffer, mPendingSynchronousProps) .execute(mMountingManager); ReactMarker.logFabricMarker( diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/PendingSynchronousProps.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/PendingSynchronousProps.kt new file mode 100644 index 000000000000..c5ed5a4674ec --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/PendingSynchronousProps.kt @@ -0,0 +1,75 @@ +/* + * 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.fabric.mounting + +import android.view.View +import androidx.annotation.UiThread +import androidx.collection.SparseArrayCompat +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReadableMap +import com.facebook.react.fabric.mounting.mountitems.MountItem +import com.facebook.react.fabric.mounting.mountitems.SynchronousMountItem + +/** + * Synchronous props updates for views that don't exist yet. Updates for the same view merge into + * one map: a newer value wins, and a newer null resets the prop. Applying the merged map once gives + * the same result as applying the updates in order. + */ +internal class PendingSynchronousProps(private val mountItemDispatcher: MountItemDispatcher) { + private val pendingByTag = SparseArrayCompat() + + /** Returns true if props are pending for the view. */ + @UiThread fun hasPending(reactTag: Int): Boolean = pendingByTag.containsKey(reactTag) + + /** + * Keeps [props] until the view mounts, and queues a mount item behind the pending mount items + * that applies everything pending for the view. Only the item queued for the newest update + * applies them, so they also wait for mount items queued after an earlier update, such as the one + * that creates the view. + */ + @UiThread + fun add(reactTag: Int, props: ReadableMap) { + val applyItem = ApplyPendingPropsMountItem(reactTag) + val pending = pendingByTag[reactTag] + if (pending == null) { + pendingByTag.put(reactTag, Pending(JavaOnlyMap.deepClone(props), applyItem)) + } else { + pending.props.merge(JavaOnlyMap.deepClone(props)) + pending.applyItem = applyItem + } + mountItemDispatcher.addMountItem(applyItem) + } + + /** Returns the props pending for the view with [props] merged over them, and clears them. */ + @UiThread + fun takeMergedWith(reactTag: Int, props: ReadableMap): ReadableMap { + val pending = pendingByTag[reactTag] ?: return props + pendingByTag.remove(reactTag) + pending.props.merge(JavaOnlyMap.deepClone(props)) + return pending.props + } + + private class Pending(val props: JavaOnlyMap, var applyItem: MountItem) + + private inner class ApplyPendingPropsMountItem(private val reactTag: Int) : MountItem { + override fun execute(mountingManager: MountingManager) { + // Nothing to do if an update applied directly already took the props, or if a newer update + // queued a later item. If the view still doesn't exist, the props are dropped. + val pending = pendingByTag[reactTag] + if (pending == null || pending.applyItem !== this) { + return + } + pendingByTag.remove(reactTag) + SynchronousMountItem(reactTag, pending.props).execute(mountingManager) + } + + override fun getSurfaceId(): Int = View.NO_ID + + override fun toString(): String = "SYNC UPDATE PENDING PROPS [$reactTag]" + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchedAnimatedPropsMountItem.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchedAnimatedPropsMountItem.kt index 21807a3ade44..84e74b095fc1 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchedAnimatedPropsMountItem.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchedAnimatedPropsMountItem.kt @@ -12,22 +12,28 @@ import com.facebook.react.bridge.JavaOnlyArray import com.facebook.react.bridge.JavaOnlyMap import com.facebook.react.bridge.ReadableMap import com.facebook.react.fabric.FabricUIManager.IS_DEVELOPMENT_ENVIRONMENT -import com.facebook.react.fabric.mounting.MountItemDispatcher import com.facebook.react.fabric.mounting.MountingManager +import com.facebook.react.fabric.mounting.PendingSynchronousProps import com.facebook.react.uimanager.ViewProps internal class BatchedAnimatedPropsMountItem( private val intBuffer: IntArray, private val doubleBuffer: DoubleArray, private val rawPropsBuffer: Array, - private val mountItemDispatcher: MountItemDispatcher, + private val pendingProps: PendingSynchronousProps, ) : MountItem { override fun execute(mountingManager: MountingManager) { decode { viewTag, props -> try { - if (!mountingManager.updateAnimatedPropsSynchronously(viewTag, props)) { - mountItemDispatcher.addMountItem(SynchronousMountItem(viewTag, props)) + val effectiveProps = + if (pendingProps.hasPending(viewTag) && mountingManager.getViewExists(viewTag)) { + pendingProps.takeMergedWith(viewTag, props) + } else { + props + } + if (!mountingManager.updateAnimatedPropsSynchronously(viewTag, effectiveProps)) { + pendingProps.add(viewTag, props) } } catch (ex: Exception) { // Match SynchronousMountItem's handling of view-manager failures. diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/BatchedAnimatedPropsMountItemTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/BatchedAnimatedPropsMountItemTest.kt index 7c13e89b833a..67e7866a3b48 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/BatchedAnimatedPropsMountItemTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/BatchedAnimatedPropsMountItemTest.kt @@ -11,6 +11,7 @@ import com.facebook.react.bridge.JavaOnlyMap import com.facebook.react.bridge.ReadableMap import com.facebook.react.fabric.mounting.MountItemDispatcher import com.facebook.react.fabric.mounting.MountingManager +import com.facebook.react.fabric.mounting.PendingSynchronousProps import com.facebook.react.fabric.mounting.mountitems.BatchedAnimatedPropsMountItem import org.assertj.core.api.Assertions.assertThat import org.junit.Test @@ -287,11 +288,12 @@ class BatchedAnimatedPropsMountItemTest { ), doubleArrayOf(0.25, 0.75), emptyArray(), - dispatcher, + PendingSynchronousProps(dispatcher), ) .execute(manager) verify(dispatcher).addMountItem(org.mockito.kotlin.any()) verify(manager).updateAnimatedPropsSynchronously(12, second) + verify(manager, org.mockito.kotlin.never()).getViewExists(org.mockito.kotlin.any()) } @Test @@ -313,7 +315,7 @@ class BatchedAnimatedPropsMountItemTest { ), doubleArrayOf(0.25, 0.75), emptyArray(), - dispatcher, + PendingSynchronousProps(dispatcher), ) .execute(manager) verify(manager).updateAnimatedPropsSynchronously(12, JavaOnlyMap.of("opacity", 0.75)) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerSynchronousPropsTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerSynchronousPropsTest.kt new file mode 100644 index 000000000000..e33a29231b7b --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerSynchronousPropsTest.kt @@ -0,0 +1,185 @@ +/* + * 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. + */ + +@file:Suppress("DEPRECATION") + +package com.facebook.react.fabric + +import android.view.View +import com.facebook.react.ReactRootView +import com.facebook.react.bridge.JavaOnlyArray +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReactTestHelper +import com.facebook.react.fabric.mounting.MountItemDispatcher +import com.facebook.react.fabric.mounting.MountingManager +import com.facebook.react.fabric.mounting.SurfaceMountingManager +import com.facebook.react.fabric.mounting.mountitems.MountItem +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.uimanager.ViewManager +import com.facebook.react.uimanager.ViewManagerRegistry +import com.facebook.react.views.view.ReactViewManager +import com.facebook.testutils.fakes.FakeBatchEventDispatchedListener +import com.facebook.testutils.shadows.ShadowNativeLoader +import com.facebook.testutils.shadows.ShadowNativeMap +import com.facebook.testutils.shadows.ShadowPerformanceTracer +import com.facebook.testutils.shadows.ShadowReadableNativeArray +import com.facebook.testutils.shadows.ShadowReadableNativeMap +import com.facebook.testutils.shadows.ShadowSoLoader +import com.facebook.testutils.shadows.ShadowWritableNativeArray +import com.facebook.testutils.shadows.ShadowWritableNativeMap +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.util.ReflectionHelpers + +/** + * Synchronous props updates for a view that doesn't exist yet wait for the next batch of mount + * items. The view can mount and get a newer update applied directly before that batch runs. + */ +@RunWith(RobolectricTestRunner::class) +@Config( + shadows = + [ + ShadowSoLoader::class, + ShadowNativeLoader::class, + ShadowNativeMap::class, + ShadowWritableNativeMap::class, + ShadowReadableNativeMap::class, + ShadowWritableNativeArray::class, + ShadowReadableNativeArray::class, + ShadowPerformanceTracer::class, + ], +) +class FabricUIManagerSynchronousPropsTest { + private val surfaceId = 1 + private val tag = 42 + private lateinit var themedReactContext: ThemedReactContext + private lateinit var underTest: FabricUIManager + + private val mountingManager: MountingManager + get() = ReflectionHelpers.getField(underTest, "mMountingManager") + + private val dispatcher: MountItemDispatcher + get() = ReflectionHelpers.getField(underTest, "mMountItemDispatcher") + + @Before + fun setUp() { + ReactNativeFeatureFlagsForTests.setUp() + val reactContext = ReactTestHelper.createTestReactApplicationContext() + themedReactContext = ThemedReactContext(reactContext, reactContext, null, -1) + underTest = + FabricUIManager( + reactContext, + ViewManagerRegistry(listOf>(ReactViewManager())), + FakeBatchEventDispatchedListener(), + ) + } + + private fun mountView(): SurfaceMountingManager { + mountingManager.startSurface(surfaceId, themedReactContext, ReactRootView(themedReactContext)) + val smm = mountingManager.getSurfaceManagerEnforced(surfaceId, "test") + smm.preallocateView("RCTView", tag, JavaOnlyMap.of(), null, true) + smm.addViewAt(surfaceId, tag, 0) + return smm + } + + private fun updateViewBatch(opacity: Double) { + val method = + FabricUIManager::class + .java + .getDeclaredMethod( + "synchronouslyUpdateViewBatch", + IntArray::class.java, + DoubleArray::class.java, + Array::class.java, + ) + method.isAccessible = true + method.invoke( + underTest, + intArrayOf(CMD_START_OF_VIEW, tag, CMD_OPACITY, CMD_END_OF_VIEW), + doubleArrayOf(opacity), + arrayOf(), + ) + } + + @Test + fun queuedUpdate_doesNotOverwriteNewerDirectUpdate() { + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.3)) + val smm = mountView() + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.6)) + + dispatcher.tryDispatchMountItems() + + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) + // The synchronous mount props override keeps the newer value over a Fabric commit. + smm.updateProps(tag, JavaOnlyMap.of("opacity", 1.0)) + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) + } + + @Test + fun queuedBatchedUpdate_doesNotOverwriteNewerDirectUpdate() { + updateViewBatch(opacity = 0.3) + val smm = mountView() + updateViewBatch(opacity = 0.6) + + dispatcher.tryDispatchMountItems() + + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) + smm.updateProps(tag, JavaOnlyMap.of("opacity", 1.0)) + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) + } + + @Test + fun queuedUpdates_waitForTheMountItemThatCreatesTheView() { + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.3)) + lateinit var smm: SurfaceMountingManager + dispatcher.addMountItem( + object : MountItem { + override fun execute(mountingManager: MountingManager) { + smm = mountView() + } + + override fun getSurfaceId(): Int = View.NO_ID + } + ) + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.6)) + + dispatcher.tryDispatchMountItems() + + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) + } + + @Test + fun queuedUpdates_mergeInOrderAndKeepNullResets() { + underTest.synchronouslyUpdateViewOnUIThread( + tag, + JavaOnlyMap.of("opacity", 0.3, "transform", JavaOnlyArray.of(JavaOnlyMap.of("scale", 2.0))), + ) + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", null)) + val smm = mountView() + + dispatcher.tryDispatchMountItems() + + val view = smm.getView(tag) + assertThat(view.alpha).isEqualTo(1f) + assertThat(view.scaleX).isEqualTo(2f) + // The reset also cleared the stored opacity, so a Fabric commit applies its own value. + smm.updateProps(tag, JavaOnlyMap.of("opacity", 0.5)) + assertThat(view.alpha).isEqualTo(0.5f) + } + + private companion object { + // Keep in sync with BatchedAnimatedPropsMountItem. + const val CMD_START_OF_VIEW = 1 + const val CMD_END_OF_VIEW = 4 + const val CMD_OPACITY = 10 + } +}