From 1b63da753fa736ddb7fe8f7ede2416388a856622 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 23 Feb 2018 17:28:32 -0800 Subject: [PATCH] Implement Cloning for ART Views Reviewed By: achen1 Differential Revision: D7058410 fbshipit-source-id: 394330654be1ab70853f78580c2543e04a3efb7c --- .../com/facebook/react/common/ArrayUtils.java | 11 +++++++ .../react/uimanager/ReactShadowNodeImpl.java | 1 + .../react/views/art/ARTGroupShadowNode.java | 13 ++++++++ .../react/views/art/ARTShapeShadowNode.java | 30 +++++++++++++++---- .../react/views/art/ARTTextShadowNode.java | 13 ++++++++ .../react/views/art/ARTVirtualNode.java | 7 +++++ 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/common/ArrayUtils.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/ArrayUtils.java b/ReactAndroid/src/main/java/com/facebook/react/common/ArrayUtils.java new file mode 100644 index 000000000..0036e9bac --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/common/ArrayUtils.java @@ -0,0 +1,11 @@ +package com.facebook.react.common; + +import java.util.Arrays; + +public class ArrayUtils { + + public static float[] copyArray(float[] array) { + return array == null ? null : Arrays.copyOf(array, array.length); + } + +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java index c12f5fb9f..59ab9b69e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNodeImpl.java @@ -123,6 +123,7 @@ public class ReactShadowNodeImpl implements ReactShadowNode } } + @Override public ReactShadowNodeImpl mutableCopy() { return new ReactShadowNodeImpl(this); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTGroupShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTGroupShadowNode.java index 780147294..35cf8169c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTGroupShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTGroupShadowNode.java @@ -7,6 +7,7 @@ package com.facebook.react.views.art; +import com.facebook.react.uimanager.ReactShadowNodeImpl; import javax.annotation.Nullable; import android.graphics.Canvas; @@ -25,6 +26,13 @@ public class ARTGroupShadowNode extends ARTVirtualNode { protected @Nullable RectF mClipping; + public ARTGroupShadowNode() { } + + public ARTGroupShadowNode(ARTGroupShadowNode node) { + super(node); + this.mClipping = new RectF(node.mClipping); + } + @ReactProp(name = "clipping") public void setClipping(@Nullable ReadableArray clippingDims) { float[] clippingData = PropHelper.toFloatArray(clippingDims); @@ -34,6 +42,11 @@ public class ARTGroupShadowNode extends ARTVirtualNode { } } + @Override + public ReactShadowNodeImpl mutableCopy() { + return new ARTGroupShadowNode(this); + } + @Override public boolean isVirtual() { return true; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTShapeShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTShapeShadowNode.java index dc8980ae2..e60ad8803 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTShapeShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTShapeShadowNode.java @@ -7,22 +7,22 @@ package com.facebook.react.views.art; -import javax.annotation.Nullable; - import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.DashPathEffect; +import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; -import android.graphics.DashPathEffect; -import android.graphics.LinearGradient; import android.graphics.Shader; -import android.graphics.Color; - import com.facebook.common.logging.FLog; import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.common.ReactConstants; import com.facebook.react.uimanager.annotations.ReactProp; +import javax.annotation.Nullable; + +import static com.facebook.react.common.ArrayUtils.copyArray; /** * Shadow node for virtual ARTShape view @@ -58,6 +58,24 @@ public class ARTShapeShadowNode extends ARTVirtualNode { private int mStrokeCap = CAP_ROUND; private int mStrokeJoin = JOIN_ROUND; + public ARTShapeShadowNode() { } + + public ARTShapeShadowNode(ARTShapeShadowNode node) { + super(node); + mPath = new Path(node.mPath); + mStrokeColor = copyArray(node.mStrokeColor); + mBrushData = copyArray(node.mBrushData); + mStrokeDash = copyArray(node.mStrokeDash); + mStrokeWidth = node.mStrokeWidth; + mStrokeCap = node.mStrokeCap; + mStrokeJoin = node.mStrokeJoin; + } + + @Override + public ARTShapeShadowNode mutableCopy() { + return new ARTShapeShadowNode(this); + } + @ReactProp(name = "d") public void setShapePath(@Nullable ReadableArray shapePath) { float[] pathData = PropHelper.toFloatArray(shapePath); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTTextShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTTextShadowNode.java index b10f59bff..4a52e7a28 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTTextShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTTextShadowNode.java @@ -40,6 +40,19 @@ public class ARTTextShadowNode extends ARTShapeShadowNode { private @Nullable ReadableMap mFrame; private int mTextAlignment = TEXT_ALIGNMENT_LEFT; + public ARTTextShadowNode() { } + + public ARTTextShadowNode(ARTTextShadowNode node) { + super(node); + mTextAlignment = node.mTextAlignment; + mFrame = node.mFrame; // copy reference as mFrame is already immutable + } + + @Override + public ARTShapeShadowNode mutableCopy() { + return new ARTTextShadowNode(this); + } + @ReactProp(name = "frame") public void setFrame(@Nullable ReadableMap frame) { mFrame = frame; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTVirtualNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTVirtualNode.java index 5cc9d49f6..158afd16e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTVirtualNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTVirtualNode.java @@ -37,6 +37,13 @@ public abstract class ARTVirtualNode extends ReactShadowNodeImpl { mScale = DisplayMetricsHolder.getWindowDisplayMetrics().density; } + protected ARTVirtualNode(ARTVirtualNode artVirtualNode) { + super(artVirtualNode); + mScale = artVirtualNode.mScale; + mOpacity = artVirtualNode.mOpacity; + mMatrix = new Matrix(artVirtualNode.mMatrix); + } + @Override public boolean isVirtual() { return true;