From de36e85004dce6ef0f2cee9208bfee9036db7e04 Mon Sep 17 00:00:00 2001 From: Aditya Sharat Date: Tue, 16 Feb 2021 10:17:36 -0800 Subject: [PATCH] Adds InternalNode#freeze() API Summary: Litho needs a new API which is called immediately before yoga begins layout calculations so that the InternalNode gets the opportunity to finalise itself; i.e. perform the last mutations and in effect avoid any more mutations to the hierarchy. See D26373731 where the mutations from `Layout#collectResults` is moved back into the InternalNode. Changelog: [Internal] Adds new API to YogaNodeJNIBase Reviewed By: SidharthGuglani Differential Revision: D26373730 fbshipit-source-id: 471346d3444986ada91e86c95f5f9fb98bcd2fa6 --- java/com/facebook/yoga/YogaNode.java | 18 ++++++++++++------ java/com/facebook/yoga/YogaNodeJNIBase.java | 14 +++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/java/com/facebook/yoga/YogaNode.java b/java/com/facebook/yoga/YogaNode.java index 9fc8bac9..dfec8ec5 100644 --- a/java/com/facebook/yoga/YogaNode.java +++ b/java/com/facebook/yoga/YogaNode.java @@ -10,6 +10,14 @@ package com.facebook.yoga; import javax.annotation.Nullable; public abstract class YogaNode { + + /** The interface the {@link #getData()} object can optionally implement. */ + public interface Inputs { + + /** Requests the data object to disable mutations of its inputs. */ + void freeze(); + } + public abstract void reset(); public abstract int getChildCount(); @@ -25,12 +33,10 @@ public abstract class YogaNode { public abstract YogaNode removeChildAt(int i); /** - * @returns the {@link YogaNode} that owns this {@link YogaNode}. - * The owner is used to identify the YogaTree that a {@link YogaNode} belongs - * to. - * This method will return the parent of the {@link YogaNode} when the - * {@link YogaNode} only belongs to one YogaTree or null when the - * {@link YogaNode} is shared between two or more YogaTrees. + * @returns the {@link YogaNode} that owns this {@link YogaNode}. The owner is used to identify + * the YogaTree that a {@link YogaNode} belongs to. This method will return the parent of the + * {@link YogaNode} when the {@link YogaNode} only belongs to one YogaTree or null when the + * {@link YogaNode} is shared between two or more YogaTrees. */ @Nullable public abstract YogaNode getOwner(); diff --git a/java/com/facebook/yoga/YogaNodeJNIBase.java b/java/com/facebook/yoga/YogaNodeJNIBase.java index 6b3dcd26..03354556 100644 --- a/java/com/facebook/yoga/YogaNodeJNIBase.java +++ b/java/com/facebook/yoga/YogaNodeJNIBase.java @@ -197,12 +197,17 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { long[] nativePointers = null; YogaNodeJNIBase[] nodes = null; + freeze(); + ArrayList n = new ArrayList<>(); n.add(this); for (int i = 0; i < n.size(); ++i) { List children = n.get(i).mChildren; if (children != null) { - n.addAll(children); + for (YogaNodeJNIBase child : children) { + child.freeze(); + n.add(child); + } } } @@ -215,6 +220,13 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { YogaNative.jni_YGNodeCalculateLayoutJNI(mNativePointer, width, height, nativePointers, nodes); } + private void freeze() { + Object data = getData(); + if (data instanceof Inputs) { + ((Inputs) data).freeze(); + } + } + public void dirty() { YogaNative.jni_YGNodeMarkDirtyJNI(mNativePointer); }