From 34b68cf1d2b8de9efb84e4d8948e20bbbd1faa3f Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Tue, 8 Oct 2019 14:23:57 -0700 Subject: [PATCH] move YGNode related methods to vanilla jni Summary: This diff moves methods related to actions on YGNode like free node, reset node etc. to vanilla JNI Reviewed By: amir-shalem Differential Revision: D17668008 fbshipit-source-id: 03bfc51ec1fcf06569713400f984d551827e22fe --- java/com/facebook/yoga/YogaNative.java | 14 ++ java/com/facebook/yoga/YogaNodeJNIBase.java | 53 ++++++-- .../facebook/yoga/YogaNodeJNIFinalizer.java | 5 +- java/jni/YGJNIVanilla.cpp | 125 ++++++++++++++++++ 4 files changed, 183 insertions(+), 14 deletions(-) diff --git a/java/com/facebook/yoga/YogaNative.java b/java/com/facebook/yoga/YogaNative.java index 5f0391cb..9e21170c 100644 --- a/java/com/facebook/yoga/YogaNative.java +++ b/java/com/facebook/yoga/YogaNative.java @@ -114,6 +114,17 @@ public class YogaNative { // JNI methods that use Vanilla JNI + static native void jni_YGNodeFreeJNI(long nativePointer); + static native void jni_YGNodeResetJNI(long nativePointer); + static native void jni_YGNodeInsertChildJNI(long nativePointer, long childPointer, int index); + static native void jni_YGNodeSetIsReferenceBaselineJNI(long nativePointer, boolean isReferenceBaseline); + static native boolean jni_YGNodeIsReferenceBaselineJNI(long nativePointer); + static native void jni_YGNodeClearChildrenJNI(long nativePointer); + static native void jni_YGNodeRemoveChildJNI(long nativePointer, long childPointer); + static native void jni_YGNodeMarkDirtyJNI(long nativePointer); + static native void jni_YGNodeMarkDirtyAndPropogateToDescendantsJNI(long nativePointer); + static native boolean jni_YGNodeIsDirtyJNI(long nativePointer); + static native void jni_YGNodeCopyStyleJNI(long dstNativePointer, long srcNativePointer); static native int jni_YGNodeStyleGetDirectionJNI(long nativePointer); static native void jni_YGNodeStyleSetDirectionJNI(long nativePointer, int direction); static native int jni_YGNodeStyleGetFlexDirectionJNI(long nativePointer); @@ -178,4 +189,7 @@ public class YogaNative { static native void jni_YGNodeStyleSetMaxHeightPercentJNI(long nativePointer, float percent); static native float jni_YGNodeStyleGetAspectRatioJNI(long nativePointer); static native void jni_YGNodeStyleSetAspectRatioJNI(long nativePointer, float aspectRatio); + static native void jni_YGNodePrintJNI(long nativePointer); + static native void jni_YGNodeSetStyleInputsJNI(long nativePointer, float[] styleInputsArray, int size); + static native long jni_YGNodeCloneJNI(long nativePointer); } diff --git a/java/com/facebook/yoga/YogaNodeJNIBase.java b/java/com/facebook/yoga/YogaNodeJNIBase.java index 744c5979..a647dcb1 100644 --- a/java/com/facebook/yoga/YogaNodeJNIBase.java +++ b/java/com/facebook/yoga/YogaNodeJNIBase.java @@ -46,7 +46,7 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { private boolean mHasNewLayout = true; - private boolean useVanillaJNI = false; + protected boolean useVanillaJNI = false; private YogaNodeJNIBase(long nativePointer) { if (nativePointer == 0) { @@ -72,7 +72,10 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { mHasNewLayout = true; mLayoutDirection = 0; - YogaNative.jni_YGNodeReset(mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeResetJNI(mNativePointer); + else + YogaNative.jni_YGNodeReset(mNativePointer); } public int getChildCount() { @@ -97,22 +100,28 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { } mChildren.add(i, child); child.mOwner = this; - YogaNative.jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i); + if (useVanillaJNI) + YogaNative.jni_YGNodeInsertChildJNI(mNativePointer, child.mNativePointer, i); + else + YogaNative.jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i); } public void setIsReferenceBaseline(boolean isReferenceBaseline) { - YogaNative.jni_YGNodeSetIsReferenceBaseline(mNativePointer, isReferenceBaseline); + if (useVanillaJNI) + YogaNative.jni_YGNodeSetIsReferenceBaselineJNI(mNativePointer, isReferenceBaseline); + else + YogaNative.jni_YGNodeSetIsReferenceBaseline(mNativePointer, isReferenceBaseline); } public boolean isReferenceBaseline() { - return YogaNative.jni_YGNodeIsReferenceBaseline(mNativePointer); + return useVanillaJNI ? YogaNative.jni_YGNodeIsReferenceBaselineJNI(mNativePointer) : YogaNative.jni_YGNodeIsReferenceBaseline(mNativePointer); } @Override public YogaNodeJNIBase cloneWithoutChildren() { try { YogaNodeJNIBase clonedYogaNode = (YogaNodeJNIBase) super.clone(); - long clonedNativePointer = YogaNative.jni_YGNodeClone(mNativePointer); + long clonedNativePointer = useVanillaJNI ? YogaNative.jni_YGNodeCloneJNI(mNativePointer) : YogaNative.jni_YGNodeClone(mNativePointer);; clonedYogaNode.mOwner = null; clonedYogaNode.mNativePointer = clonedNativePointer; clonedYogaNode.clearChildren(); @@ -125,7 +134,10 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { private void clearChildren() { mChildren = null; - YogaNative.jni_YGNodeClearChildren(mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeClearChildrenJNI(mNativePointer); + else + YogaNative.jni_YGNodeClearChildren(mNativePointer); } public YogaNodeJNIBase removeChildAt(int i) { @@ -135,7 +147,10 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { } final YogaNodeJNIBase child = mChildren.remove(i); child.mOwner = null; - YogaNative.jni_YGNodeRemoveChild(mNativePointer, child.mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeRemoveChildJNI(mNativePointer, child.mNativePointer); + else + YogaNative.jni_YGNodeRemoveChild(mNativePointer, child.mNativePointer); return child; } @@ -186,20 +201,29 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { } public void dirty() { - YogaNative.jni_YGNodeMarkDirty(mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeMarkDirtyJNI(mNativePointer); + else + YogaNative.jni_YGNodeMarkDirty(mNativePointer); } public void dirtyAllDescendants() { - YogaNative.jni_YGNodeMarkDirtyAndPropogateToDescendants(mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeMarkDirtyAndPropogateToDescendantsJNI(mNativePointer); + else + YogaNative.jni_YGNodeMarkDirtyAndPropogateToDescendants(mNativePointer); } public boolean isDirty() { - return YogaNative.jni_YGNodeIsDirty(mNativePointer); + return useVanillaJNI ? YogaNative.jni_YGNodeIsDirtyJNI(mNativePointer) : YogaNative.jni_YGNodeIsDirty(mNativePointer); } @Override public void copyStyle(YogaNode srcNode) { - YogaNative.jni_YGNodeCopyStyle(mNativePointer, ((YogaNodeJNIBase) srcNode).mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeCopyStyleJNI(mNativePointer, ((YogaNodeJNIBase) srcNode).mNativePointer); + else + YogaNative.jni_YGNodeCopyStyle(mNativePointer, ((YogaNodeJNIBase) srcNode).mNativePointer); } public YogaDirection getStyleDirection() { @@ -633,7 +657,10 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { * layout of the tree rooted at this node. */ public void print() { - YogaNative.jni_YGNodePrint(mNativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodePrintJNI(mNativePointer); + else + YogaNative.jni_YGNodePrint(mNativePointer); } public void setStyleInputs(float[] styleInputsArray, int size) { diff --git a/java/com/facebook/yoga/YogaNodeJNIFinalizer.java b/java/com/facebook/yoga/YogaNodeJNIFinalizer.java index cee7b0e8..41986c25 100644 --- a/java/com/facebook/yoga/YogaNodeJNIFinalizer.java +++ b/java/com/facebook/yoga/YogaNodeJNIFinalizer.java @@ -28,7 +28,10 @@ public class YogaNodeJNIFinalizer extends YogaNodeJNIBase { if (mNativePointer != 0) { long nativePointer = mNativePointer; mNativePointer = 0; - YogaNative.jni_YGNodeFree(nativePointer); + if (useVanillaJNI) + YogaNative.jni_YGNodeFreeJNI(nativePointer); + else + YogaNative.jni_YGNodeFree(nativePointer); } } } diff --git a/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp index 395d4526..5308bf8a 100644 --- a/java/jni/YGJNIVanilla.cpp +++ b/java/jni/YGJNIVanilla.cpp @@ -14,6 +14,94 @@ static inline YGNodeRef _jlong2YGNodeRef(jlong addr) { return reinterpret_cast(static_cast(addr)); } +static void jni_YGNodeFreeJNI(JNIEnv* env, jobject obj, jlong nativePointer) { + if (nativePointer == 0) { + return; + } + const YGNodeRef node = _jlong2YGNodeRef(nativePointer); + YGNodeFree(node); +} + +static void jni_YGNodeResetJNI(JNIEnv* env, jobject obj, jlong nativePointer) { + const YGNodeRef node = _jlong2YGNodeRef(nativePointer); + void* context = node->getContext(); + YGNodeReset(node); + node->setContext(context); +} + +static void jni_YGNodeInsertChildJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jlong childPointer, + jint index) { + YGNodeInsertChild( + _jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index); +} + +static void jni_YGNodeSetIsReferenceBaselineJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jboolean isReferenceBaseline) { + YGNodeSetIsReferenceBaseline( + _jlong2YGNodeRef(nativePointer), isReferenceBaseline); +} + +static jboolean jni_YGNodeIsReferenceBaselineJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer) { + return YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer)); +} + +static void jni_YGNodeClearChildrenJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer) { + const YGNodeRef node = _jlong2YGNodeRef(nativePointer); + node->clearChildren(); +} + +static void jni_YGNodeRemoveChildJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jlong childPointer) { + YGNodeRemoveChild( + _jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer)); +} + +static void jni_YGNodeMarkDirtyJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer) { + YGNodeMarkDirty(_jlong2YGNodeRef(nativePointer)); +} + +static void jni_YGNodeMarkDirtyAndPropogateToDescendantsJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer) { + YGNodeMarkDirtyAndPropogateToDescendants(_jlong2YGNodeRef(nativePointer)); +} + +static jboolean jni_YGNodeIsDirtyJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer) { + return (jboolean) _jlong2YGNodeRef(nativePointer)->isDirty(); +} + +static void jni_YGNodeCopyStyleJNI( + JNIEnv* env, + jobject obj, + jlong dstNativePointer, + jlong srcNativePointer) { + YGNodeCopyStyle( + _jlong2YGNodeRef(dstNativePointer), _jlong2YGNodeRef(srcNativePointer)); +} + #define YG_NODE_JNI_STYLE_PROP(javatype, type, name) \ static javatype jni_YGNodeStyleGet##name##JNI( \ JNIEnv* env, jobject obj, jlong nativePointer) { \ @@ -215,6 +303,24 @@ static void jni_YGNodeStyleSetBorderJNI( yogaNodeRef, static_cast(edge), static_cast(border)); } +static void jni_YGNodePrintJNI(JNIEnv* env, jobject obj, jlong nativePointer) { +#ifdef DEBUG + const YGNodeRef node = _jlong2YGNodeRef(nativePointer); + YGNodePrint( + node, + (YGPrintOptions)( + YGPrintOptionsStyle | YGPrintOptionsLayout | YGPrintOptionsChildren)); +#endif +} + +static jlong jni_YGNodeCloneJNI(JNIEnv* env, jobject obj, jlong nativePointer) { + auto node = _jlong2YGNodeRef(nativePointer); + const YGNodeRef clonedYogaNode = YGNodeClone(node); + clonedYogaNode->setContext(node->getContext()); + + return reinterpret_cast(clonedYogaNode); +} + // Yoga specific properties, not compatible with flexbox specification YG_NODE_JNI_STYLE_PROP(jfloat, float, AspectRatio); @@ -241,6 +347,23 @@ void registerNativeMethods( } static JNINativeMethod methods[] = { + {"jni_YGNodeFreeJNI", "(J)V", (void*) jni_YGNodeFreeJNI}, + {"jni_YGNodeResetJNI", "(J)V", (void*) jni_YGNodeResetJNI}, + {"jni_YGNodeInsertChildJNI", "(JJI)V", (void*) jni_YGNodeInsertChildJNI}, + {"jni_YGNodeSetIsReferenceBaselineJNI", + "(JZ)V", + (void*) jni_YGNodeSetIsReferenceBaselineJNI}, + {"jni_YGNodeIsReferenceBaselineJNI", + "(J)Z", + (void*) jni_YGNodeIsReferenceBaselineJNI}, + {"jni_YGNodeClearChildrenJNI", "(J)V", (void*) jni_YGNodeClearChildrenJNI}, + {"jni_YGNodeRemoveChildJNI", "(JJ)V", (void*) jni_YGNodeRemoveChildJNI}, + {"jni_YGNodeMarkDirtyJNI", "(J)V", (void*) jni_YGNodeMarkDirtyJNI}, + {"jni_YGNodeMarkDirtyAndPropogateToDescendantsJNI", + "(J)V", + (void*) jni_YGNodeMarkDirtyAndPropogateToDescendantsJNI}, + {"jni_YGNodeIsDirtyJNI", "(J)Z", (void*) jni_YGNodeIsDirtyJNI}, + {"jni_YGNodeCopyStyleJNI", "(JJ)V", (void*) jni_YGNodeCopyStyleJNI}, {"jni_YGNodeStyleGetDirectionJNI", "(J)I", (void*) jni_YGNodeStyleGetDirectionJNI}, @@ -425,6 +548,8 @@ static JNINativeMethod methods[] = { {"jni_YGNodeStyleSetAspectRatioJNI", "(JF)V", (void*) jni_YGNodeStyleSetAspectRatioJNI}, + {"jni_YGNodePrintJNI", "(J)V", (void*) jni_YGNodePrintJNI}, + {"jni_YGNodeCloneJNI", "(J)J", (void*) jni_YGNodeCloneJNI}, }; void YGJNIVanilla::registerNatives(JNIEnv* env) {