Skip to content

Commit

Permalink
move config jni methods to vanilla jni
Browse files Browse the repository at this point in the history
Summary: Move yoga node config related jni methods to vanilla jni

Reviewed By: amir-shalem

Differential Revision: D17684821

fbshipit-source-id: 31a667b3ad67501aaef83a132971e4e0826cacd4
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed Oct 8, 2019
1 parent 9ea8014 commit e3919b5
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 14 deletions.
2 changes: 0 additions & 2 deletions ReactAndroid/src/main/java/com/facebook/yoga/YogaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ public abstract void setShouldDiffLayoutWithoutLegacyStretchBehaviour(

abstract long getNativePointer();

public abstract void setUseVanillaJNI(boolean useVanillaJNI);

public abstract boolean useVanillaJNI();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
43 changes: 31 additions & 12 deletions ReactAndroid/src/main/java/com/facebook/yoga/YogaConfigJNIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -72,11 +96,6 @@ long getNativePointer() {
return mNativePointer;
}

@Override
public void setUseVanillaJNI(boolean useVanillaJNI) {
this.useVanillaJNI = useVanillaJNI;
}

@Override
public boolean useVanillaJNI() {
return this.useVanillaJNI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public YogaConfigJNIFinalizer() {
super();
}

public YogaConfigJNIFinalizer(boolean useVanillaJNI) {
super(useVanillaJNI);
}

@Override
protected void finalize() throws Throwable {
try {
Expand Down
11 changes: 11 additions & 0 deletions ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
93 changes: 93 additions & 0 deletions ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,78 @@ static inline YGNodeRef _jlong2YGNodeRef(jlong addr) {
return reinterpret_cast<YGNodeRef>(static_cast<intptr_t>(addr));
}

static inline YGConfigRef _jlong2YGConfigRef(jlong addr) {
return reinterpret_cast<YGConfigRef>(static_cast<intptr_t>(addr));
}

static jlong jni_YGConfigNewJNI(JNIEnv* env, jobject obj) {
return reinterpret_cast<jlong>(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<global_ref<JYogaLogger>>{
// static_cast<global_ref<JYogaLogger>*>(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<YGExperimentalFeature>(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;
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit e3919b5

Please sign in to comment.