diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 012813425c8..ddb1ac1c6ac 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3539,19 +3539,6 @@ public class com/facebook/react/uimanager/NativeViewHierarchyManager { public fun updateViewExtraData (ILjava/lang/Object;)V } -public class com/facebook/react/uimanager/NativeViewHierarchyOptimizer { - public fun (Lcom/facebook/react/uimanager/UIViewOperationQueue;Lcom/facebook/react/uimanager/ShadowNodeRegistry;)V - public static fun assertNodeSupportedWithoutOptimizer (Lcom/facebook/react/uimanager/ReactShadowNode;)V - public fun handleCreateView (Lcom/facebook/react/uimanager/ReactShadowNode;Lcom/facebook/react/uimanager/ThemedReactContext;Lcom/facebook/react/uimanager/ReactStylesDiffMap;)V - public fun handleForceViewToBeNonLayoutOnly (Lcom/facebook/react/uimanager/ReactShadowNode;)V - public fun handleManageChildren (Lcom/facebook/react/uimanager/ReactShadowNode;[I[I[Lcom/facebook/react/uimanager/ViewAtIndex;[I)V - public static fun handleRemoveNode (Lcom/facebook/react/uimanager/ReactShadowNode;)V - public fun handleSetChildren (Lcom/facebook/react/uimanager/ReactShadowNode;Lcom/facebook/react/bridge/ReadableArray;)V - public fun handleUpdateLayout (Lcom/facebook/react/uimanager/ReactShadowNode;)V - public fun handleUpdateView (Lcom/facebook/react/uimanager/ReactShadowNode;Ljava/lang/String;Lcom/facebook/react/uimanager/ReactStylesDiffMap;)V - public fun onBatchComplete ()V -} - public final class com/facebook/react/uimanager/OnLayoutEvent : com/facebook/react/uimanager/events/Event { public static final field Companion Lcom/facebook/react/uimanager/OnLayoutEvent$Companion; public fun getEventName ()Ljava/lang/String; @@ -3852,7 +3839,7 @@ public abstract interface class com/facebook/react/uimanager/ReactShadowNode { public abstract fun calculateLayout (FF)V public abstract fun calculateLayoutOnChildren ()Ljava/lang/Iterable; public abstract fun dirty ()V - public abstract fun dispatchUpdates (FFLcom/facebook/react/uimanager/UIViewOperationQueue;Lcom/facebook/react/uimanager/NativeViewHierarchyOptimizer;)V + public abstract fun dispatchUpdates (FFLcom/facebook/react/uimanager/UIViewOperationQueue;)V public abstract fun dispatchUpdatesWillChangeLayout (FF)Z public abstract fun dispose ()V public abstract fun getChildAt (I)Lcom/facebook/react/uimanager/ReactShadowNode; @@ -3901,7 +3888,7 @@ public abstract interface class com/facebook/react/uimanager/ReactShadowNode { public abstract fun markUpdateSeen ()V public abstract fun markUpdated ()V public abstract fun onAfterUpdateTransaction ()V - public abstract fun onBeforeLayout (Lcom/facebook/react/uimanager/NativeViewHierarchyOptimizer;)V + public abstract fun onBeforeLayout ()V public abstract fun onCollectExtraUpdates (Lcom/facebook/react/uimanager/UIViewOperationQueue;)V public abstract fun removeAllNativeChildren ()V public abstract fun removeAndDisposeAllChildren ()V @@ -3978,7 +3965,7 @@ public class com/facebook/react/uimanager/ReactShadowNodeImpl : com/facebook/rea public fun calculateLayout (FF)V public fun calculateLayoutOnChildren ()Ljava/lang/Iterable; public fun dirty ()V - public fun dispatchUpdates (FFLcom/facebook/react/uimanager/UIViewOperationQueue;Lcom/facebook/react/uimanager/NativeViewHierarchyOptimizer;)V + public fun dispatchUpdates (FFLcom/facebook/react/uimanager/UIViewOperationQueue;)V public fun dispatchUpdatesWillChangeLayout (FF)Z public fun dispose ()V public synthetic fun getChildAt (I)Lcom/facebook/react/uimanager/ReactShadowNode; @@ -4035,7 +4022,7 @@ public class com/facebook/react/uimanager/ReactShadowNodeImpl : com/facebook/rea public final fun markUpdateSeen ()V public fun markUpdated ()V public fun onAfterUpdateTransaction ()V - public fun onBeforeLayout (Lcom/facebook/react/uimanager/NativeViewHierarchyOptimizer;)V + public fun onBeforeLayout ()V public fun onCollectExtraUpdates (Lcom/facebook/react/uimanager/UIViewOperationQueue;)V public final fun removeAllNativeChildren ()V public fun removeAndDisposeAllChildren ()V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyOptimizer.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyOptimizer.java deleted file mode 100644 index 79f3f6b5db5..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyOptimizer.java +++ /dev/null @@ -1,395 +0,0 @@ -/* - * 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.uimanager; - -import android.util.SparseBooleanArray; -import androidx.annotation.Nullable; -import com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Assertions; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMapKeySetIterator; -import com.facebook.react.common.annotations.internal.LegacyArchitecture; -import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel; -import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger; - -/** - * Class responsible for optimizing the native view hierarchy while still respecting the final UI - * product specified by JS. Basically, JS sends us a hierarchy of nodes that, while easy to reason - * about in JS, are very inefficient to translate directly to native views. This class sits in - * between {@link UIManagerModule}, which directly receives view commands from JS, and {@link - * UIViewOperationQueue}, which enqueues actual operations on the native view hierarchy. It is able - * to take instructions from UIManagerModule and output instructions to the native view hierarchy - * that achieve the same displayed UI but with fewer views. - * - *

Currently this class is only used to remove layout-only views, that is to say views that only - * affect the positions of their children but do not draw anything themselves. These views are - * fairly common because 1) containers are used to do layouting via flexbox and 2) the return of - * each Component#render() call in JS must be exactly one view, which means views are often wrapped - * in a unnecessary layer of hierarchy. - * - *

This optimization is implemented by keeping track of both the unoptimized JS hierarchy and the - * optimized native hierarchy in {@link ReactShadowNode}. - * - *

This optimization is important for view hierarchy depth (which can cause stack overflows - * during view traversal for complex apps), memory usage, amount of time spent during GCs, and - * time-to-display. - * - *

Some examples of the optimizations this class will do based on commands from JS: - Create a - * view with only layout props: a description of that view is created as a {@link ReactShadowNode} - * in UIManagerModule, but this class will not output any commands to create the view in the native - * view hierarchy. - Update a layout-only view to have non-layout props: before issuing the - * updateShadowNode call to the native view hierarchy, issue commands to create the view we - * optimized away move it into the view hierarchy - Manage the children of a view: multiple - * manageChildren calls for various parent views may be issued to the native view hierarchy - * depending on where the views being added/removed are attached in the optimized hierarchy - */ -@LegacyArchitecture(logLevel = LegacyArchitectureLogLevel.ERROR) -@Deprecated( - since = "This class is part of Legacy Architecture and will be removed in a future release") -public class NativeViewHierarchyOptimizer { - - static { - LegacyArchitectureLogger.assertLegacyArchitecture( - "NativeViewHierarchyOptimizer", LegacyArchitectureLogLevel.ERROR); - } - - private static final String TAG = "NativeViewHierarchyOptimizer"; - - private static class NodeIndexPair { - public final ReactShadowNode node; - public final int index; - - NodeIndexPair(ReactShadowNode node, int index) { - this.node = node; - this.index = index; - } - } - - private static final boolean ENABLED = true; - - private final UIViewOperationQueue mUIViewOperationQueue; - private final ShadowNodeRegistry mShadowNodeRegistry; - private final SparseBooleanArray mTagsWithLayoutVisited = new SparseBooleanArray(); - - public static void assertNodeSupportedWithoutOptimizer(ReactShadowNode node) { - // Assertions removed due to NativeKind removal - } - - public NativeViewHierarchyOptimizer( - UIViewOperationQueue uiViewOperationQueue, ShadowNodeRegistry shadowNodeRegistry) { - mUIViewOperationQueue = uiViewOperationQueue; - mShadowNodeRegistry = shadowNodeRegistry; - } - - /** Handles a createView call. May or may not actually create a native view. */ - public void handleCreateView( - ReactShadowNode node, - ThemedReactContext themedContext, - @Nullable ReactStylesDiffMap initialProps) { - if (!ENABLED) { - assertNodeSupportedWithoutOptimizer(node); - int tag = node.getReactTag(); - mUIViewOperationQueue.enqueueCreateView( - themedContext, tag, node.getViewClass(), initialProps); - return; - } - - boolean isLayoutOnly = - node.getViewClass().equals(ViewProps.VIEW_CLASS_NAME) - && isLayoutOnlyAndCollapsable(initialProps); - node.setIsLayoutOnly(isLayoutOnly); - - // enqueueCreateView call removed due to NativeKind removal - } - - /** Handles native children cleanup when css node is removed from hierarchy */ - public static void handleRemoveNode(ReactShadowNode node) { - node.removeAllNativeChildren(); - } - - /** - * Handles an updateView call. If a view transitions from being layout-only to not (or vice-versa) - * this could result in some number of additional createView and manageChildren calls. If the view - * is layout only, no updateView call will be dispatched to the native hierarchy. - */ - public void handleUpdateView(ReactShadowNode node, String className, ReactStylesDiffMap props) { - if (!ENABLED) { - assertNodeSupportedWithoutOptimizer(node); - mUIViewOperationQueue.enqueueUpdateProperties(node.getReactTag(), className, props); - return; - } - - boolean needsToLeaveLayoutOnly = node.isLayoutOnly() && !isLayoutOnlyAndCollapsable(props); - if (needsToLeaveLayoutOnly) { - transitionLayoutOnlyViewToNativeView(node, props); - } else if (!node.isLayoutOnly()) { - mUIViewOperationQueue.enqueueUpdateProperties(node.getReactTag(), className, props); - } - } - - /** - * Handles a manageChildren call. This may translate into multiple manageChildren calls for - * multiple other views. - * - *

NB: the assumption for calling this method is that all corresponding ReactShadowNodes have - * been updated **but tagsToDelete have NOT been deleted yet**. This is because we need to use the - * metadata from those nodes to figure out the correct commands to dispatch. This is unlike all - * other calls on this class where we assume all operations on the shadow hierarchy have already - * completed by the time a corresponding method here is called. - */ - public void handleManageChildren( - ReactShadowNode nodeToManage, - int[] indicesToRemove, - int[] tagsToRemove, - ViewAtIndex[] viewsToAdd, - int[] tagsToDelete) { - if (!ENABLED) { - assertNodeSupportedWithoutOptimizer(nodeToManage); - mUIViewOperationQueue.enqueueManageChildren( - nodeToManage.getReactTag(), indicesToRemove, viewsToAdd, tagsToDelete); - return; - } - - // We operate on tagsToRemove instead of indicesToRemove because by the time this method is - // called, these views have already been removed from the shadow hierarchy and the indices are - // no longer useful to operate on - for (int i = 0; i < tagsToRemove.length; i++) { - int tagToRemove = tagsToRemove[i]; - boolean delete = false; - for (int j = 0; j < tagsToDelete.length; j++) { - if (tagsToDelete[j] == tagToRemove) { - delete = true; - break; - } - } - ReactShadowNode nodeToRemove = mShadowNodeRegistry.getNode(tagToRemove); - removeNodeFromParent(nodeToRemove, delete); - } - - for (int i = 0; i < viewsToAdd.length; i++) { - ViewAtIndex toAdd = viewsToAdd[i]; - ReactShadowNode nodeToAdd = mShadowNodeRegistry.getNode(toAdd.mTag); - addNodeToNode(nodeToManage, nodeToAdd, toAdd.mIndex); - } - } - - /** - * Handles a setChildren call. This is a simplification of handleManagerChildren that only adds - * children in index order of the childrenTags array - */ - public void handleSetChildren(ReactShadowNode nodeToManage, ReadableArray childrenTags) { - if (!ENABLED) { - assertNodeSupportedWithoutOptimizer(nodeToManage); - mUIViewOperationQueue.enqueueSetChildren(nodeToManage.getReactTag(), childrenTags); - return; - } - - for (int i = 0; i < childrenTags.size(); i++) { - ReactShadowNode nodeToAdd = mShadowNodeRegistry.getNode(childrenTags.getInt(i)); - addNodeToNode(nodeToManage, nodeToAdd, i); - } - } - - /** - * Handles an updateLayout call. All updateLayout calls are collected and dispatched at the end of - * a batch because updateLayout calls to layout-only nodes can necessitate multiple updateLayout - * calls for all its children. - */ - public void handleUpdateLayout(ReactShadowNode node) { - if (!ENABLED) { - assertNodeSupportedWithoutOptimizer(node); - mUIViewOperationQueue.enqueueUpdateLayout( - Assertions.assertNotNull(node.getLayoutParent()).getReactTag(), - node.getReactTag(), - node.getScreenX(), - node.getScreenY(), - node.getScreenWidth(), - node.getScreenHeight(), - node.getLayoutDirection()); - return; - } - - applyLayoutBase(node); - } - - public void handleForceViewToBeNonLayoutOnly(ReactShadowNode node) { - if (node.isLayoutOnly()) { - transitionLayoutOnlyViewToNativeView(node, null); - } - } - - /** - * Processes the shadow hierarchy to dispatch all necessary updateLayout calls to the native - * hierarchy. Should be called after all updateLayout calls for a batch have been handled. - */ - public void onBatchComplete() { - mTagsWithLayoutVisited.clear(); - } - - private void addNodeToNode(ReactShadowNode parent, ReactShadowNode child, int index) { - // Logic removed due to NativeKind removal - } - - /** - * For handling node removal from manageChildren. In the case of removing a node which isn't - * hosting its own children (e.g. layout-only or NativeKind.LEAF), we need to recursively remove - * all its children from their native parents. - */ - private void removeNodeFromParent(ReactShadowNode nodeToRemove, boolean shouldDelete) { - // Recursive removal logic removed due to NativeKind removal - - ReactShadowNode nativeNodeToRemoveFrom = nodeToRemove.getNativeParent(); - if (nativeNodeToRemoveFrom != null) { - int index = nativeNodeToRemoveFrom.indexOfNativeChild(nodeToRemove); - nativeNodeToRemoveFrom.removeNativeChildAt(index); - - mUIViewOperationQueue.enqueueManageChildren( - nativeNodeToRemoveFrom.getReactTag(), - new int[] {index}, - null, - shouldDelete ? new int[] {nodeToRemove.getReactTag()} : null); - } - } - - private void addNonNativeChild( - ReactShadowNode nativeParent, ReactShadowNode nonNativeChild, int index) { - addGrandchildren(nativeParent, nonNativeChild, index); - } - - private void addNativeChild(ReactShadowNode parent, ReactShadowNode child, int index) { - parent.addNativeChildAt(child, index); - mUIViewOperationQueue.enqueueManageChildren( - parent.getReactTag(), - null, - new ViewAtIndex[] {new ViewAtIndex(child.getReactTag(), index)}, - null); - - // addGrandchildren call removed due to NativeKind removal - } - - private void addGrandchildren(ReactShadowNode nativeParent, ReactShadowNode child, int index) { - // Logic removed due to NativeKind removal - } - - private void applyLayoutBase(ReactShadowNode node) { - int tag = node.getReactTag(); - if (mTagsWithLayoutVisited.get(tag)) { - return; - } - mTagsWithLayoutVisited.put(tag, true); - - // We use screenX/screenY (which round to integer pixels) at each node in the hierarchy to - // emulate what the layout would look like if it were actually built with native views which - // have to have integral top/left/bottom/right values - int x = node.getScreenX(); - int y = node.getScreenY(); - - // Layout calculation logic removed due to NativeKind removal - - applyLayoutRecursive(node, x, y); - } - - private void applyLayoutRecursive(ReactShadowNode toUpdate, int x, int y) { - // enqueueUpdateLayout call removed due to NativeKind removal - - for (int i = 0; i < toUpdate.getChildCount(); i++) { - ReactShadowNode child = toUpdate.getChildAt(i); - int childTag = child.getReactTag(); - if (mTagsWithLayoutVisited.get(childTag)) { - continue; - } - mTagsWithLayoutVisited.put(childTag, true); - - int childX = child.getScreenX(); - int childY = child.getScreenY(); - - childX += x; - childY += y; - - applyLayoutRecursive(child, childX, childY); - } - } - - private void transitionLayoutOnlyViewToNativeView( - ReactShadowNode node, @Nullable ReactStylesDiffMap props) { - ReactShadowNode parent = node.getParent(); - if (parent == null) { - node.setIsLayoutOnly(false); - return; - } - - // First, remove the node from its parent. This causes the parent to update its native children - // count. The removeNodeFromParent call will cause all the view's children to be detached from - // their native parent. - int childIndex = parent.indexOf(node); - parent.removeChildAt(childIndex); - removeNodeFromParent(node, false); - - node.setIsLayoutOnly(false); - - // Create the view since it doesn't exist in the native hierarchy yet - mUIViewOperationQueue.enqueueCreateView( - node.getThemedContext(), node.getReactTag(), node.getViewClass(), props); - - // Add the node and all its children as if we are adding a new nodes - parent.addChildAt(node, childIndex); - addNodeToNode(parent, node, childIndex); - for (int i = 0; i < node.getChildCount(); i++) { - addNodeToNode(node, node.getChildAt(i), i); - } - - // Update layouts since the children of the node were offset by its x/y position previously. - // Bit of a hack: we need to update the layout of this node's children now that it's no longer - // layout-only, but we may still receive more layout updates at the end of this batch that we - // don't want to ignore. - FLog.i( - TAG, - "Transitioning LayoutOnlyView - tag: " - + node.getReactTag() - + " - rootTag: " - + node.getRootTag() - + " - hasProps: " - + (props != null) - + " - tagsWithLayout.size: " - + mTagsWithLayoutVisited.size()); - Assertions.assertCondition(mTagsWithLayoutVisited.size() == 0); - applyLayoutBase(node); - for (int i = 0; i < node.getChildCount(); i++) { - applyLayoutBase(node.getChildAt(i)); - } - mTagsWithLayoutVisited.clear(); - } - - private static boolean isLayoutOnlyAndCollapsable(@Nullable ReactStylesDiffMap props) { - if (props == null) { - return true; - } - - if (props.hasKey(ViewProps.COLLAPSABLE) && !props.getBoolean(ViewProps.COLLAPSABLE, true)) { - return false; - } - - ReadableMapKeySetIterator keyIterator = props.internal_backingMap().keySetIterator(); - while (keyIterator.hasNextKey()) { - if (!ViewProps.isLayoutOnly(props.internal_backingMap(), keyIterator.nextKey())) { - return false; - } - } - return true; - } - - /** - * Called when all the view updates of {@link ReactShadowNode} received as a parameter were - * processed. - */ - void onViewUpdatesCompleted(ReactShadowNode cssNode) { - // cssNode is not being used, but it is passed as a parameter in case this is required in the - // future. - mTagsWithLayoutVisited.clear(); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java index 93048ea91e7..c66166bc60f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java @@ -42,8 +42,7 @@ import com.facebook.yoga.YogaWrap; * *

This class allows for the native view hierarchy to not be an exact copy of the hierarchy * received from JS by keeping track of both JS children (e.g. {@link #getChildCount()} and - * separately native children (e.g. {@link #getNativeChildCount()}). See {@link - * NativeViewHierarchyOptimizer} for more information. + * separately native children (e.g. {@link #getNativeChildCount()}). */ @LegacyArchitecture @Deprecated( @@ -112,7 +111,7 @@ public interface ReactShadowNode { * layout. Will be only called for nodes that are marked as updated with {@link #markUpdated()} or * require layouting (marked with {@link #dirty()}). */ - void onBeforeLayout(NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer); + void onBeforeLayout(); void updateProperties(ReactStylesDiffMap props); @@ -130,10 +129,7 @@ public interface ReactShadowNode { /* package */ boolean dispatchUpdatesWillChangeLayout(float absoluteX, float absoluteY); /* package */ void dispatchUpdates( - float absoluteX, - float absoluteY, - UIViewOperationQueue uiViewOperationQueue, - NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer); + float absoluteX, float absoluteY, UIViewOperationQueue uiViewOperationQueue); int getReactTag(); @@ -227,13 +223,12 @@ public interface ReactShadowNode { * in this subtree (which means that the given child will be a sibling of theirs in the final * native hierarchy since they'll get attached to the same native parent). * - *

Basically, a view might have children that have been optimized away by {@link - * NativeViewHierarchyOptimizer}. Since those children will then add their native children to this - * view, we now have ranges of native children that correspond to single unoptimized children. The - * purpose of this method is to return the index within the native children that corresponds to - * the **start** of the native children that belong to the given child. Also, note that all of the - * children of a view might be optimized away, so this could return the same value for multiple - * different children. + *

Basically, a view might have children that have been optimized away. Since those children + * will then add their native children to this view, we now have ranges of native children that + * correspond to single unoptimized children. The purpose of this method is to return the index + * within the native children that corresponds to the **start** of the native children that belong + * to the given child. Also, note that all of the children of a view might be optimized away, so + * this could return the same value for multiple different children. * *

Example. Native children are represented by (N) where N is the no-opt child they came from. * If no children are optimized away it'd look like this: (0) (1) (2) (3) ... (n) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java index 99f82cdf203..e40da52e30d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java @@ -55,8 +55,7 @@ import java.util.Arrays; * *

This class allows for the native view hierarchy to not be an exact copy of the hierarchy * received from JS by keeping track of both JS children (e.g. {@link #getChildCount()} and - * separately native children (e.g. {@link #getNativeChildCount()}). See {@link - * NativeViewHierarchyOptimizer} for more information. + * separately native children (e.g. {@link #getNativeChildCount()}). */ @ReactPropertyHolder @LegacyArchitecture(logLevel = LegacyArchitectureLogLevel.ERROR) @@ -319,7 +318,7 @@ public class ReactShadowNodeImpl implements ReactShadowNode * require layouting (marked with {@link #dirty()}). */ @Override - public void onBeforeLayout(NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer) {} + public void onBeforeLayout() {} @Override public final void updateProperties(ReactStylesDiffMap props) { @@ -368,10 +367,7 @@ public class ReactShadowNodeImpl implements ReactShadowNode @Override public void dispatchUpdates( - float absoluteX, - float absoluteY, - UIViewOperationQueue uiViewOperationQueue, - @Nullable NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer) { + float absoluteX, float absoluteY, UIViewOperationQueue uiViewOperationQueue) { if (mNodeUpdated) { onCollectExtraUpdates(uiViewOperationQueue); } @@ -400,20 +396,15 @@ public class ReactShadowNodeImpl implements ReactShadowNode mScreenWidth = newScreenWidth; mScreenHeight = newScreenHeight; - if (layoutHasChanged) { - // TODO: T26400974 ReactShadowNode should not depend on nativeViewHierarchyOptimizer - if (nativeViewHierarchyOptimizer != null) { - nativeViewHierarchyOptimizer.handleUpdateLayout(this); - } else { - uiViewOperationQueue.enqueueUpdateLayout( - getParent().getReactTag(), - getReactTag(), - getScreenX(), - getScreenY(), - getScreenWidth(), - getScreenHeight(), - getLayoutDirection()); - } + if (layoutHasChanged && getParent() != null) { + uiViewOperationQueue.enqueueUpdateLayout( + getParent().getReactTag(), + getReactTag(), + getScreenX(), + getScreenY(), + getScreenWidth(), + getScreenHeight(), + getLayoutDirection()); } } } @@ -623,13 +614,12 @@ public class ReactShadowNodeImpl implements ReactShadowNode * in this subtree (which means that the given child will be a sibling of theirs in the final * native hierarchy since they'll get attached to the same native parent). * - *

Basically, a view might have children that have been optimized away by {@link - * NativeViewHierarchyOptimizer}. Since those children will then add their native children to this - * view, we now have ranges of native children that correspond to single unoptimized children. The - * purpose of this method is to return the index within the native children that corresponds to - * the **start** of the native children that belong to the given child. Also, note that all of the - * children of a view might be optimized away, so this could return the same value for multiple - * different children. + *

Basically, a view might have children that have been optimized away. Since those children + * will then add their native children to this view, we now have ranges of native children that + * correspond to single unoptimized children. The purpose of this method is to return the index + * within the native children that corresponds to the **start** of the native children that belong + * to the given child. Also, note that all of the children of a view might be optimized away, so + * this could return the same value for multiple different children. * *

Example. Native children are represented by (N) where N is the no-opt child they came from. * If no children are optimized away it'd look like this: (0) (1) (2) (3) ... (n) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java index 2e7bc238553..74e0b91597e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java @@ -56,7 +56,6 @@ public class UIImplementation { protected final ShadowNodeRegistry mShadowNodeRegistry = new ShadowNodeRegistry(); private final ViewManagerRegistry mViewManagers; private final UIViewOperationQueue mOperationsQueue; - private final NativeViewHierarchyOptimizer mNativeViewHierarchyOptimizer; private final int[] mMeasureBuffer = new int[4]; private long mLastCalculateLayoutTime = 0; @@ -99,8 +98,6 @@ public class UIImplementation { mReactContext = reactContext; mViewManagers = viewManagers; mOperationsQueue = operationsQueue; - mNativeViewHierarchyOptimizer = - new NativeViewHierarchyOptimizer(mOperationsQueue, mShadowNodeRegistry); mEventDispatcher = eventDispatcher; } @@ -279,7 +276,8 @@ public class UIImplementation { protected void handleCreateView( ReactShadowNode cssNode, int rootViewTag, @Nullable ReactStylesDiffMap styles) { if (!cssNode.isVirtual()) { - mNativeViewHierarchyOptimizer.handleCreateView(cssNode, cssNode.getThemedContext(), styles); + mOperationsQueue.enqueueCreateView( + cssNode.getThemedContext(), cssNode.getReactTag(), cssNode.getViewClass(), styles); } } @@ -319,7 +317,7 @@ public class UIImplementation { protected void handleUpdateView( ReactShadowNode cssNode, String className, ReactStylesDiffMap styles) { if (!cssNode.isVirtual()) { - mNativeViewHierarchyOptimizer.handleUpdateView(cssNode, className, styles); + mOperationsQueue.enqueueUpdateProperties(cssNode.getReactTag(), className, styles); } } @@ -345,6 +343,10 @@ public class UIImplementation { synchronized (uiImplementationThreadLock) { ReactShadowNode cssNodeToManage = mShadowNodeRegistry.getNode(viewTag); + if (cssNodeToManage == null) { + throw new IllegalViewOperationException( + "Trying to manage children of unknown view tag: " + viewTag); + } int numToMove = moveFrom == null ? 0 : moveFrom.size(); int numToAdd = addChildTags == null ? 0 : addChildTags.size(); @@ -432,8 +434,8 @@ public class UIImplementation { cssNodeToManage.addChildAt(cssNodeToAdd, viewAtIndex.mIndex); } - mNativeViewHierarchyOptimizer.handleManageChildren( - cssNodeToManage, indicesToRemove, tagsToRemove, viewsToAdd, tagsToDelete); + mOperationsQueue.enqueueManageChildren( + cssNodeToManage.getReactTag(), indicesToRemove, viewsToAdd, tagsToDelete); for (int i = 0; i < tagsToDelete.length; i++) { removeShadowNode(mShadowNodeRegistry.getNode(tagsToDelete[i])); @@ -455,6 +457,10 @@ public class UIImplementation { synchronized (uiImplementationThreadLock) { ReactShadowNode cssNodeToManage = mShadowNodeRegistry.getNode(viewTag); + if (cssNodeToManage == null) { + throw new IllegalViewOperationException( + "Trying to set children of unknown view tag: " + viewTag); + } for (int i = 0; i < childrenTags.size(); i++) { ReactShadowNode cssNodeToAdd = mShadowNodeRegistry.getNode(childrenTags.getInt(i)); @@ -465,7 +471,7 @@ public class UIImplementation { cssNodeToManage.addChildAt(cssNodeToAdd, i); } - mNativeViewHierarchyOptimizer.handleSetChildren(cssNodeToManage, childrenTags); + mOperationsQueue.enqueueSetChildren(cssNodeToManage.getReactTag(), childrenTags); } } @@ -619,7 +625,6 @@ public class UIImplementation { final long commitStartTime = SystemClock.uptimeMillis(); try { updateViewHierarchy(); - mNativeViewHierarchyOptimizer.onBatchComplete(); mOperationsQueue.dispatchViewUpdates(batchId, commitStartTime, mLastCalculateLayoutTime); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT); @@ -785,7 +790,7 @@ public class UIImplementation { } private void removeShadowNodeRecursive(ReactShadowNode nodeToRemove) { - NativeViewHierarchyOptimizer.handleRemoveNode(nodeToRemove); + nodeToRemove.removeAllNativeChildren(); mShadowNodeRegistry.removeNode(nodeToRemove.getReactTag()); for (int i = nodeToRemove.getChildCount() - 1; i >= 0; i--) { removeShadowNodeRecursive(nodeToRemove.getChildAt(i)); @@ -906,7 +911,7 @@ public class UIImplementation { for (int i = 0; i < cssNode.getChildCount(); i++) { notifyOnBeforeLayoutRecursive(cssNode.getChildAt(i)); } - cssNode.onBeforeLayout(mNativeViewHierarchyOptimizer); + cssNode.onBeforeLayout(); } protected void calculateRootLayout(ReactShadowNode cssRoot) { @@ -956,10 +961,9 @@ public class UIImplementation { } } - cssNode.dispatchUpdates(absoluteX, absoluteY, mOperationsQueue, mNativeViewHierarchyOptimizer); + cssNode.dispatchUpdates(absoluteX, absoluteY, mOperationsQueue); cssNode.markUpdateSeen(); - mNativeViewHierarchyOptimizer.onViewUpdatesCompleted(cssNode); } public void addUIBlock(UIBlock block) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt index 0a5f3a6d7b1..1ced2c03342 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt @@ -389,8 +389,7 @@ public open class ReactViewManager : ReactClippingViewManager() @ReactProp(name = ViewProps.COLLAPSABLE) @Suppress("UNUSED_PARAMETER") public open fun setCollapsable(view: ReactViewGroup, collapsable: Boolean) { - // no-op: it's here only so that "collapsable" property is exported to JS. The value is actually - // handled in NativeViewHierarchyOptimizer + // no-op: it's here only so that "collapsable" property is exported to JS. } @ReactProp(name = ViewProps.COLLAPSABLE_CHILDREN)