Skip to content

Commit

Permalink
move YGNode related methods to vanilla jni
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed Oct 8, 2019
1 parent 76af8ee commit 9ea8014
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 14 deletions.
14 changes: 14 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
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);
Expand Down Expand Up @@ -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);
}
53 changes: 40 additions & 13 deletions ReactAndroid/src/main/java/com/facebook/yoga/YogaNodeJNIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -72,7 +72,10 @@ public void reset() {
mHasNewLayout = true;
mLayoutDirection = 0;

YogaNative.jni_YGNodeReset(mNativePointer);
if (useVanillaJNI)
YogaNative.jni_YGNodeResetJNI(mNativePointer);
else
YogaNative.jni_YGNodeReset(mNativePointer);
}

public int getChildCount() {
Expand All @@ -97,22 +100,28 @@ public void addChildAt(YogaNode c, int i) {
}
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();
Expand All @@ -125,7 +134,10 @@ public YogaNodeJNIBase cloneWithoutChildren() {

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) {
Expand All @@ -135,7 +147,10 @@ public YogaNodeJNIBase removeChildAt(int i) {
}
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;
}

Expand Down Expand Up @@ -186,20 +201,29 @@ public void calculateLayout(float width, float height) {
}

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() {
Expand Down Expand Up @@ -633,7 +657,10 @@ public void setData(Object data) {
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public void freeNatives() {
if (mNativePointer != 0) {
long nativePointer = mNativePointer;
mNativePointer = 0;
YogaNative.jni_YGNodeFree(nativePointer);
if (useVanillaJNI)
YogaNative.jni_YGNodeFreeJNI(nativePointer);
else
YogaNative.jni_YGNodeFree(nativePointer);
}
}
}
125 changes: 125 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,94 @@ static inline YGNodeRef _jlong2YGNodeRef(jlong addr) {
return reinterpret_cast<YGNodeRef>(static_cast<intptr_t>(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) { \
Expand Down Expand Up @@ -215,6 +303,24 @@ static void jni_YGNodeStyleSetBorderJNI(
yogaNodeRef, static_cast<YGEdge>(edge), static_cast<float>(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<jlong>(clonedYogaNode);
}

// Yoga specific properties, not compatible with flexbox specification
YG_NODE_JNI_STYLE_PROP(jfloat, float, AspectRatio);

Expand All @@ -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},
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9ea8014

Please sign in to comment.