Skip to content

Commit

Permalink
move yg node new and ygnode new with config
Browse files Browse the repository at this point in the history
Summary: YGNode creation methods to vanilla JNI

Reviewed By: amir-shalem

Differential Revision: D17714230

fbshipit-source-id: 74e14e876ab7efc67771d92091c2a78f09072b83
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed Oct 9, 2019
1 parent 34739ec commit 22a60e8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions java/com/facebook/yoga/YogaNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public class YogaNative {
static native void jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI(long nativePointer, boolean shouldDiffLayoutWithoutLegacyStretchBehaviour);
// static native void jni_YGConfigSetLoggerJNI(long nativePointer, Object logger);

// YGNode related
static native long jni_YGNodeNewJNI();
static native long jni_YGNodeNewWithConfigJNI(long configPointer);
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
4 changes: 4 additions & 0 deletions java/com/facebook/yoga/YogaNodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public static YogaNode create() {
return new YogaNodeJNIFinalizer();
}

public static YogaNode create(boolean useVanillaJNI) {
return new YogaNodeJNIFinalizer(useVanillaJNI);
}

public static YogaNode create(YogaConfig config) {
return new YogaNodeJNIFinalizer(config);
}
Expand Down
6 changes: 5 additions & 1 deletion java/com/facebook/yoga/YogaNodeJNIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ private YogaNodeJNIBase(long nativePointer) {
this(YogaNative.jni_YGNodeNew());
}

YogaNodeJNIBase(boolean useVanillaJNI) {
this(useVanillaJNI ? YogaNative.jni_YGNodeNewJNI() : YogaNative.jni_YGNodeNew());
}

YogaNodeJNIBase(YogaConfig config) {
this(YogaNative.jni_YGNodeNewWithConfig(((YogaConfigJNIBase)config).mNativePointer));
this(config.useVanillaJNI() ? YogaNative.jni_YGNodeNewWithConfigJNI(((YogaConfigJNIBase)config).mNativePointer) : YogaNative.jni_YGNodeNewWithConfig(((YogaConfigJNIBase)config).mNativePointer));
this.useVanillaJNI = config.useVanillaJNI();
}

Expand Down
4 changes: 4 additions & 0 deletions java/com/facebook/yoga/YogaNodeJNIFinalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public YogaNodeJNIFinalizer() {
super();
}

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

public YogaNodeJNIFinalizer(YogaConfig config) {
super(config);
}
Expand Down
31 changes: 31 additions & 0 deletions java/jni/YGJNIVanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "common.h"
#include "YGJTypesVanilla.h"
#include <yoga/log.h>
#include <iostream>

using namespace facebook::yoga::vanillajni;
using facebook::yoga::detail::Log;
Expand Down Expand Up @@ -90,6 +91,18 @@ static void jni_YGConfigSetPointScaleFactorJNI(
YGConfigSetPointScaleFactor(config, pixelsInPoint);
}

static void YGPrint(YGNodeRef node, void* layoutContext) {
if (auto obj = YGNodeJobject(node, layoutContext)) {
// TODO cout << obj.get()->toString() << endl;
} else {
Log::log(
node,
YGLogLevelError,
nullptr,
"Java YGNode was GCed during layout calculation\n");
}
}

static void jni_YGConfigSetUseLegacyStretchBehaviourJNI(
JNIEnv* env,
jobject obj,
Expand All @@ -99,6 +112,22 @@ static void jni_YGConfigSetUseLegacyStretchBehaviourJNI(
YGConfigSetUseLegacyStretchBehaviour(config, useLegacyStretchBehaviour);
}

static jlong jni_YGNodeNewJNI(JNIEnv* env, jobject obj) {
const YGNodeRef node = YGNodeNew();
node->setContext(YGNodeContext{}.asVoidPtr);
node->setPrintFunc(YGPrint);
return reinterpret_cast<jlong>(node);
}

static jlong jni_YGNodeNewWithConfigJNI(
JNIEnv* env,
jobject obj,
jlong configPointer) {
const YGNodeRef node = YGNodeNewWithConfig(_jlong2YGConfigRef(configPointer));
node->setContext(YGNodeContext{}.asVoidPtr);
return reinterpret_cast<jlong>(node);
}

static void jni_YGNodeFreeJNI(JNIEnv* env, jobject obj, jlong nativePointer) {
if (nativePointer == 0) {
return;
Expand Down Expand Up @@ -558,6 +587,8 @@ static JNINativeMethod methods[] = {
"(JZ)V",
(void*) jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourJNI},
// {"jni_YGConfigSetLoggerJNI", "(JO)V", (void*) jni_YGConfigSetLoggerJNI},
{"jni_YGNodeNewJNI", "()J", (void*) jni_YGNodeNewJNI},
{"jni_YGNodeNewWithConfigJNI", "(J)J", (void*) jni_YGNodeNewWithConfigJNI},
{"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 22a60e8

Please sign in to comment.