From b1b118d0f6755c6d3c757d64bf1f8d220ac2e291 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 30 Mar 2023 23:58:25 -0700 Subject: [PATCH] Refactor of IntBufferBatchMountItem class (#36728) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36728 This diff refactors the creation and visibility of IntBufferBatchMountItem This is not a breaking compatibility change because nobody should be using this class changelog: [internal] internal Reviewed By: javache Differential Revision: D44115916 fbshipit-source-id: f0b0464483cafe55ffaf661b18c692be0df41199 --- .../com/facebook/react/fabric/FabricUIManager.java | 9 +++++---- .../fabric/mounting/mountitems/BatchMountItem.java | 14 ++++++++++++++ .../mountitems/DispatchIntCommandMountItem.java | 2 +- .../mountitems/DispatchStringCommandMountItem.java | 2 +- .../mountitems/IntBufferBatchMountItem.java | 9 +++++---- .../mounting/mountitems/MountItemFactory.java | 8 ++++++++ .../mountitems/PreAllocateViewMountItem.java | 2 +- .../SendAccessibilityEventMountItem.java | 2 +- 8 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchMountItem.java 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 328fe08cf03..997e21fb987 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 @@ -59,7 +59,7 @@ 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.SurfaceMountingManager.ViewEvent; -import com.facebook.react.fabric.mounting.mountitems.IntBufferBatchMountItem; +import com.facebook.react.fabric.mounting.mountitems.BatchMountItem; import com.facebook.react.fabric.mounting.mountitems.MountItem; import com.facebook.react.fabric.mounting.mountitems.MountItemFactory; import com.facebook.react.modules.core.ReactChoreographer; @@ -724,7 +724,8 @@ public class FabricUIManager implements UIManager, LifecycleEventListener { @ThreadConfined(ANY) private MountItem createIntBufferBatchMountItem( int rootTag, int[] intBuffer, Object[] objBuffer, int commitNumber) { - return new IntBufferBatchMountItem(rootTag, intBuffer, objBuffer, commitNumber); + return MountItemFactory.createIntBufferBatchMountItem( + rootTag, intBuffer, objBuffer, commitNumber); } /** @@ -749,9 +750,9 @@ public class FabricUIManager implements UIManager, LifecycleEventListener { // a BatchMountItem. No other sites call into this with a BatchMountItem, and Binding.cpp only // calls scheduleMountItems with a BatchMountItem. long scheduleMountItemStartTime = SystemClock.uptimeMillis(); - boolean isBatchMountItem = mountItem instanceof IntBufferBatchMountItem; + boolean isBatchMountItem = mountItem instanceof BatchMountItem; boolean shouldSchedule = - (isBatchMountItem && ((IntBufferBatchMountItem) mountItem).shouldSchedule()) + (isBatchMountItem && !((BatchMountItem) mountItem).isBatchEmpty()) || (!isBatchMountItem && mountItem != null); // In case of sync rendering, this could be called on the UI thread. Otherwise, diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchMountItem.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchMountItem.java new file mode 100644 index 00000000000..97c0f4f37b9 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchMountItem.java @@ -0,0 +1,14 @@ +/* + * 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.mountitems; + +public interface BatchMountItem extends MountItem { + + /** @return if the BatchMountItem contains any MountItem */ + boolean isBatchEmpty(); +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchIntCommandMountItem.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchIntCommandMountItem.java index 23bae74ef15..89f69e12888 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchIntCommandMountItem.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchIntCommandMountItem.java @@ -12,7 +12,7 @@ import androidx.annotation.Nullable; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.fabric.mounting.MountingManager; -class DispatchIntCommandMountItem extends DispatchCommandMountItem { +final class DispatchIntCommandMountItem extends DispatchCommandMountItem { private final int mSurfaceId; private final int mReactTag; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchStringCommandMountItem.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchStringCommandMountItem.java index b686935cfbc..c86abde48e9 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchStringCommandMountItem.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/DispatchStringCommandMountItem.java @@ -12,7 +12,7 @@ import androidx.annotation.Nullable; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.fabric.mounting.MountingManager; -class DispatchStringCommandMountItem extends DispatchCommandMountItem { +final class DispatchStringCommandMountItem extends DispatchCommandMountItem { private final int mSurfaceId; private final int mReactTag; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/IntBufferBatchMountItem.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/IntBufferBatchMountItem.java index 84bcbeec909..05b5a5a2f48 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/IntBufferBatchMountItem.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/IntBufferBatchMountItem.java @@ -34,7 +34,7 @@ import com.facebook.systrace.Systrace; * allocations in C++ and JNI round-trips. */ @DoNotStrip -public class IntBufferBatchMountItem implements MountItem { +final class IntBufferBatchMountItem implements BatchMountItem { static final String TAG = IntBufferBatchMountItem.class.getSimpleName(); static final int INSTRUCTION_FLAG_MULTIPLE = 1; @@ -60,7 +60,7 @@ public class IntBufferBatchMountItem implements MountItem { private final int mIntBufferLen; private final int mObjBufferLen; - public IntBufferBatchMountItem(int surfaceId, int[] intBuf, Object[] objBuf, int commitNumber) { + IntBufferBatchMountItem(int surfaceId, int[] intBuf, Object[] objBuf, int commitNumber) { mSurfaceId = surfaceId; mCommitNumber = commitNumber; @@ -193,8 +193,9 @@ public class IntBufferBatchMountItem implements MountItem { return mSurfaceId; } - public boolean shouldSchedule() { - return mIntBufferLen != 0; + @Override + public boolean isBatchEmpty() { + return mIntBufferLen == 0; } @Override diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/MountItemFactory.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/MountItemFactory.java index 9d03af98ff2..b68adff5111 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/MountItemFactory.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/MountItemFactory.java @@ -46,4 +46,12 @@ public class MountItemFactory { return new PreAllocateViewMountItem( surfaceId, reactTag, component, props, stateWrapper, eventEmitterWrapper, isLayoutable); } + /** + * @return a {@link MountItem} that will be read and execute a collection of MountItems serialized + * in the int[] and Object[] received by parameter + */ + public static MountItem createIntBufferBatchMountItem( + int surfaceId, int[] intBuf, Object[] objBuf, int commitNumber) { + return new IntBufferBatchMountItem(surfaceId, intBuf, objBuf, commitNumber); + } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/PreAllocateViewMountItem.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/PreAllocateViewMountItem.java index 28cf1d1c32d..8f4f6feab3c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/PreAllocateViewMountItem.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/PreAllocateViewMountItem.java @@ -20,7 +20,7 @@ import com.facebook.react.fabric.mounting.SurfaceMountingManager; import com.facebook.react.uimanager.StateWrapper; /** {@link MountItem} that is used to pre-allocate views for JS components. */ -public class PreAllocateViewMountItem implements MountItem { +final class PreAllocateViewMountItem implements MountItem { private final @NonNull String mComponent; private final int mSurfaceId; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/SendAccessibilityEventMountItem.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/SendAccessibilityEventMountItem.java index 8ed05f8e6f8..13e90ada6dd 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/SendAccessibilityEventMountItem.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/SendAccessibilityEventMountItem.java @@ -12,7 +12,7 @@ import com.facebook.react.bridge.ReactSoftExceptionLogger; import com.facebook.react.bridge.RetryableMountingLayerException; import com.facebook.react.fabric.mounting.MountingManager; -class SendAccessibilityEventMountItem implements MountItem { +final class SendAccessibilityEventMountItem implements MountItem { private final String TAG = "Fabric.SendAccessibilityEvent";