From ee73f556b491fd2ee7d0ce58c114459c46a8317d Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Tue, 8 Oct 2019 14:23:57 -0700 Subject: [PATCH] move config jni methods to vanilla jni Summary: Move yoga node config related jni methods to vanilla jni Reviewed By: amir-shalem Differential Revision: D17684821 fbshipit-source-id: 31a667b3ad67501aaef83a132971e4e0826cacd4 --- java/com/facebook/yoga/YogaConfig.java | 2 - java/com/facebook/yoga/YogaConfigFactory.java | 4 + java/com/facebook/yoga/YogaConfigJNIBase.java | 43 ++++++--- .../facebook/yoga/YogaConfigJNIFinalizer.java | 4 + java/com/facebook/yoga/YogaNative.java | 11 +++ java/jni/YGJNIVanilla.cpp | 93 +++++++++++++++++++ 6 files changed, 143 insertions(+), 14 deletions(-) diff --git a/java/com/facebook/yoga/YogaConfig.java b/java/com/facebook/yoga/YogaConfig.java index 501a3cca52..d44aec2932 100644 --- a/java/com/facebook/yoga/YogaConfig.java +++ b/java/com/facebook/yoga/YogaConfig.java @@ -38,7 +38,5 @@ public abstract void setShouldDiffLayoutWithoutLegacyStretchBehaviour( abstract long getNativePointer(); - public abstract void setUseVanillaJNI(boolean useVanillaJNI); - public abstract boolean useVanillaJNI(); } diff --git a/java/com/facebook/yoga/YogaConfigFactory.java b/java/com/facebook/yoga/YogaConfigFactory.java index fca4dc9008..e7dcedd21b 100644 --- a/java/com/facebook/yoga/YogaConfigFactory.java +++ b/java/com/facebook/yoga/YogaConfigFactory.java @@ -4,4 +4,8 @@ public abstract class YogaConfigFactory { public static YogaConfig create() { return new YogaConfigJNIFinalizer(); } + + public static YogaConfig create(boolean useVanillaJNI) { + return new YogaConfigJNIFinalizer(useVanillaJNI); + } } diff --git a/java/com/facebook/yoga/YogaConfigJNIBase.java b/java/com/facebook/yoga/YogaConfigJNIBase.java index 3fbd664681..1f3ab04c01 100644 --- a/java/com/facebook/yoga/YogaConfigJNIBase.java +++ b/java/com/facebook/yoga/YogaConfigJNIBase.java @@ -23,20 +23,37 @@ private YogaConfigJNIBase(long nativePointer) { this(YogaNative.jni_YGConfigNew()); } + YogaConfigJNIBase(boolean useVanillaJNI) { + this(useVanillaJNI ? YogaNative.jni_YGConfigNewJNI() : YogaNative.jni_YGConfigNew()); + this.useVanillaJNI = useVanillaJNI; + } + public void setExperimentalFeatureEnabled(YogaExperimentalFeature feature, boolean enabled) { - YogaNative.jni_YGConfigSetExperimentalFeatureEnabled(mNativePointer, feature.intValue(), enabled); + if (useVanillaJNI) + YogaNative.jni_YGConfigSetExperimentalFeatureEnabledJNI(mNativePointer, feature.intValue(), enabled); + else + YogaNative.jni_YGConfigSetExperimentalFeatureEnabled(mNativePointer, feature.intValue(), enabled); } public void setUseWebDefaults(boolean useWebDefaults) { - YogaNative.jni_YGConfigSetUseWebDefaults(mNativePointer, useWebDefaults); + if (useVanillaJNI) + YogaNative.jni_YGConfigSetUseWebDefaultsJNI(mNativePointer, useWebDefaults); + else + YogaNative.jni_YGConfigSetUseWebDefaults(mNativePointer, useWebDefaults); } public void setPrintTreeFlag(boolean enable) { - YogaNative.jni_YGConfigSetPrintTreeFlag(mNativePointer, enable); + if (useVanillaJNI) + YogaNative.jni_YGConfigSetPrintTreeFlagJNI(mNativePointer, enable); + else + YogaNative.jni_YGConfigSetPrintTreeFlag(mNativePointer, enable); } public void setPointScaleFactor(float pixelsInPoint) { - YogaNative.jni_YGConfigSetPointScaleFactor(mNativePointer, pixelsInPoint); + if (useVanillaJNI) + YogaNative.jni_YGConfigSetPointScaleFactorJNI(mNativePointer, pixelsInPoint); + else + YogaNative.jni_YGConfigSetPointScaleFactor(mNativePointer, pixelsInPoint); } /** @@ -45,7 +62,10 @@ public void setPointScaleFactor(float pixelsInPoint) { * Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour. */ public void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour) { - YogaNative.jni_YGConfigSetUseLegacyStretchBehaviour(mNativePointer, useLegacyStretchBehaviour); + if (useVanillaJNI) + YogaNative.jni_YGConfigSetUseLegacyStretchBehaviourJNI(mNativePointer, useLegacyStretchBehaviour); + else + YogaNative.jni_YGConfigSetUseLegacyStretchBehaviour(mNativePointer, useLegacyStretchBehaviour); } /** @@ -55,8 +75,12 @@ public void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour) { */ public void setShouldDiffLayoutWithoutLegacyStretchBehaviour( boolean shouldDiffLayoutWithoutLegacyStretchBehaviour) { - YogaNative.jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour( - mNativePointer, shouldDiffLayoutWithoutLegacyStretchBehaviour); + if (useVanillaJNI) + YogaNative.jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI( + mNativePointer, shouldDiffLayoutWithoutLegacyStretchBehaviour); + else + YogaNative.jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour( + mNativePointer, shouldDiffLayoutWithoutLegacyStretchBehaviour); } public void setLogger(YogaLogger logger) { @@ -72,11 +96,6 @@ long getNativePointer() { return mNativePointer; } - @Override - public void setUseVanillaJNI(boolean useVanillaJNI) { - this.useVanillaJNI = useVanillaJNI; - } - @Override public boolean useVanillaJNI() { return this.useVanillaJNI; diff --git a/java/com/facebook/yoga/YogaConfigJNIFinalizer.java b/java/com/facebook/yoga/YogaConfigJNIFinalizer.java index aa7fb364fd..1d6fed645b 100644 --- a/java/com/facebook/yoga/YogaConfigJNIFinalizer.java +++ b/java/com/facebook/yoga/YogaConfigJNIFinalizer.java @@ -11,6 +11,10 @@ public YogaConfigJNIFinalizer() { super(); } + public YogaConfigJNIFinalizer(boolean useVanillaJNI) { + super(useVanillaJNI); + } + @Override protected void finalize() throws Throwable { try { diff --git a/java/com/facebook/yoga/YogaNative.java b/java/com/facebook/yoga/YogaNative.java index 9e21170c94..b951fa3a73 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 + // YGConfig related + static native long jni_YGConfigNewJNI(); +// static native void jni_YGConfigFreeJNI(long nativePointer); + static native void jni_YGConfigSetExperimentalFeatureEnabledJNI(long nativePointer, int feature, boolean enabled); + static native void jni_YGConfigSetUseWebDefaultsJNI(long nativePointer, boolean useWebDefaults); + static native void jni_YGConfigSetPrintTreeFlagJNI(long nativePointer, boolean enable); + static native void jni_YGConfigSetPointScaleFactorJNI(long nativePointer, float pixelsInPoint); + static native void jni_YGConfigSetUseLegacyStretchBehaviourJNI(long nativePointer, boolean useLegacyStretchBehaviour); + static native void jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI(long nativePointer, boolean shouldDiffLayoutWithoutLegacyStretchBehaviour); +// static native void jni_YGConfigSetLoggerJNI(long nativePointer, Object logger); + 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); diff --git a/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp index 5308bf8a35..9c50bce599 100644 --- a/java/jni/YGJNIVanilla.cpp +++ b/java/jni/YGJNIVanilla.cpp @@ -14,6 +14,78 @@ static inline YGNodeRef _jlong2YGNodeRef(jlong addr) { return reinterpret_cast(static_cast(addr)); } +static inline YGConfigRef _jlong2YGConfigRef(jlong addr) { + return reinterpret_cast(static_cast(addr)); +} + +static jlong jni_YGConfigNewJNI(JNIEnv* env, jobject obj) { + return reinterpret_cast(YGConfigNew()); +} + +// void jni_YGConfigFreeJNI(JNIEnv* env, jobject obj, jlong nativePointer) { +// const YGConfigRef config = _jlong2YGConfigRef(nativePointer); +// // unique_ptr will destruct the underlying global_ref, if present. +// auto context = std::unique_ptr>{ +// static_cast*>(YGConfigGetContext(config))}; +// YGConfigFree(config); +// } + +static void jni_YGConfigSetExperimentalFeatureEnabledJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jint feature, + jboolean enabled) { + const YGConfigRef config = _jlong2YGConfigRef(nativePointer); + YGConfigSetExperimentalFeatureEnabled( + config, static_cast(feature), enabled); +} + +static void jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jboolean enabled) { + const YGConfigRef config = _jlong2YGConfigRef(nativePointer); + YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(config, enabled); +} + +static void jni_YGConfigSetUseWebDefaultsJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jboolean useWebDefaults) { + const YGConfigRef config = _jlong2YGConfigRef(nativePointer); + YGConfigSetUseWebDefaults(config, useWebDefaults); +} + +static void jni_YGConfigSetPrintTreeFlagJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jboolean enable) { + const YGConfigRef config = _jlong2YGConfigRef(nativePointer); + YGConfigSetPrintTreeFlag(config, enable); +} + +static void jni_YGConfigSetPointScaleFactorJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jfloat pixelsInPoint) { + const YGConfigRef config = _jlong2YGConfigRef(nativePointer); + YGConfigSetPointScaleFactor(config, pixelsInPoint); +} + +static void jni_YGConfigSetUseLegacyStretchBehaviourJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jboolean useLegacyStretchBehaviour) { + const YGConfigRef config = _jlong2YGConfigRef(nativePointer); + YGConfigSetUseLegacyStretchBehaviour(config, useLegacyStretchBehaviour); +} + static void jni_YGNodeFreeJNI(JNIEnv* env, jobject obj, jlong nativePointer) { if (nativePointer == 0) { return; @@ -347,6 +419,27 @@ void registerNativeMethods( } static JNINativeMethod methods[] = { + {"jni_YGConfigNewJNI", "()J", (void*) jni_YGConfigNewJNI}, + // {"jni_YGConfigFreeJNI", "(J)V", (void*) jni_YGConfigFreeJNI}, + {"jni_YGConfigSetExperimentalFeatureEnabledJNI", + "(JIZ)V", + (void*) jni_YGConfigSetExperimentalFeatureEnabledJNI}, + {"jni_YGConfigSetUseWebDefaultsJNI", + "(JZ)V", + (void*) jni_YGConfigSetUseWebDefaultsJNI}, + {"jni_YGConfigSetPrintTreeFlagJNI", + "(JZ)V", + (void*) jni_YGConfigSetPrintTreeFlagJNI}, + {"jni_YGConfigSetPointScaleFactorJNI", + "(JF)V", + (void*) jni_YGConfigSetPointScaleFactorJNI}, + {"jni_YGConfigSetUseLegacyStretchBehaviourJNI", + "(JZ)V", + (void*) jni_YGConfigSetUseLegacyStretchBehaviourJNI}, + {"jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI", + "(JZ)V", + (void*) jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI}, + // {"jni_YGConfigSetLoggerJNI", "(JO)V", (void*) jni_YGConfigSetLoggerJNI}, {"jni_YGNodeFreeJNI", "(J)V", (void*) jni_YGNodeFreeJNI}, {"jni_YGNodeResetJNI", "(J)V", (void*) jni_YGNodeResetJNI}, {"jni_YGNodeInsertChildJNI", "(JJI)V", (void*) jni_YGNodeInsertChildJNI},