diff --git a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java index 3772c4f11..b34524a98 100644 --- a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java +++ b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java @@ -81,6 +81,8 @@ public class YogaNode implements YogaNodeAPI { private float mBorderBottom = 0; @DoNotStrip private int mLayoutDirection = 0; + @DoNotStrip + private boolean mHasNewLayout = true; private native long jni_YGNodeNew(); public YogaNode() { @@ -115,6 +117,7 @@ public class YogaNode implements YogaNodeAPI { mHasSetMargin = false; mHasSetBorder = false; mHasSetPosition = false; + mHasNewLayout = true; mWidth = YogaConstants.UNDEFINED; mHeight = YogaConstants.UNDEFINED; @@ -180,10 +183,9 @@ public class YogaNode implements YogaNodeAPI { jni_YGNodeCalculateLayout(mNativePointer, width, height); } - private native boolean jni_YGNodeHasNewLayout(long nativePointer); @Override public boolean hasNewLayout() { - return jni_YGNodeHasNewLayout(mNativePointer); + return mHasNewLayout; } private native void jni_YGNodeMarkDirty(long nativePointer); @@ -198,18 +200,17 @@ public class YogaNode implements YogaNodeAPI { return jni_YGNodeIsDirty(mNativePointer); } - private native void jni_YGNodeMarkLayoutSeen(long nativePointer); - @Override - public void markLayoutSeen() { - jni_YGNodeMarkLayoutSeen(mNativePointer); - } - private native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer); @Override public void copyStyle(YogaNode srcNode) { jni_YGNodeCopyStyle(mNativePointer, srcNode.mNativePointer); } + @Override + public void markLayoutSeen() { + mHasNewLayout = false; + } + private native int jni_YGNodeStyleGetDirection(long nativePointer); @Override public YogaDirection getStyleDirection() { diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp index 5e3c0b56c..9a376861e 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp @@ -24,54 +24,59 @@ static void YGTransferLayoutDirection(YGNodeRef node, alias_ref javaNod } static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { - if (auto obj = YGNodeJobject(root)->lockLocal()) { - static auto widthField = obj->getClass()->getField("mWidth"); - static auto heightField = obj->getClass()->getField("mHeight"); - static auto leftField = obj->getClass()->getField("mLeft"); - static auto topField = obj->getClass()->getField("mTop"); + if(YGNodeGetHasNewLayout(root)){ + if (auto obj = YGNodeJobject(root)->lockLocal()) { + static auto widthField = obj->getClass()->getField("mWidth"); + static auto heightField = obj->getClass()->getField("mHeight"); + static auto leftField = obj->getClass()->getField("mLeft"); + static auto topField = obj->getClass()->getField("mTop"); - static auto marginLeftField = obj->getClass()->getField("mMarginLeft"); - static auto marginTopField = obj->getClass()->getField("mMarginTop"); - static auto marginRightField = obj->getClass()->getField("mMarginRight"); - static auto marginBottomField = obj->getClass()->getField("mMarginBottom"); + static auto marginLeftField = obj->getClass()->getField("mMarginLeft"); + static auto marginTopField = obj->getClass()->getField("mMarginTop"); + static auto marginRightField = obj->getClass()->getField("mMarginRight"); + static auto marginBottomField = obj->getClass()->getField("mMarginBottom"); - static auto paddingLeftField = obj->getClass()->getField("mPaddingLeft"); - static auto paddingTopField = obj->getClass()->getField("mPaddingTop"); - static auto paddingRightField = obj->getClass()->getField("mPaddingRight"); - static auto paddingBottomField = obj->getClass()->getField("mPaddingBottom"); + static auto paddingLeftField = obj->getClass()->getField("mPaddingLeft"); + static auto paddingTopField = obj->getClass()->getField("mPaddingTop"); + static auto paddingRightField = obj->getClass()->getField("mPaddingRight"); + static auto paddingBottomField = obj->getClass()->getField("mPaddingBottom"); - static auto borderLeftField = obj->getClass()->getField("mBorderLeft"); - static auto borderTopField = obj->getClass()->getField("mBorderTop"); - static auto borderRightField = obj->getClass()->getField("mBorderRight"); - static auto borderBottomField = obj->getClass()->getField("mBorderBottom"); + static auto borderLeftField = obj->getClass()->getField("mBorderLeft"); + static auto borderTopField = obj->getClass()->getField("mBorderTop"); + static auto borderRightField = obj->getClass()->getField("mBorderRight"); + static auto borderBottomField = obj->getClass()->getField("mBorderBottom"); - obj->setFieldValue(widthField, YGNodeLayoutGetWidth(root)); - obj->setFieldValue(heightField, YGNodeLayoutGetHeight(root)); - obj->setFieldValue(leftField, YGNodeLayoutGetLeft(root)); - obj->setFieldValue(topField, YGNodeLayoutGetTop(root)); + static auto hasNewLayoutField = obj->getClass()->getField("mHasNewLayout"); - obj->setFieldValue(marginLeftField, YGNodeLayoutGetMargin(root, YGEdgeLeft)); - obj->setFieldValue(marginTopField, YGNodeLayoutGetMargin(root, YGEdgeTop)); - obj->setFieldValue(marginRightField, YGNodeLayoutGetMargin(root, YGEdgeRight)); - obj->setFieldValue(marginBottomField, YGNodeLayoutGetMargin(root, YGEdgeBottom)); + obj->setFieldValue(widthField, YGNodeLayoutGetWidth(root)); + obj->setFieldValue(heightField, YGNodeLayoutGetHeight(root)); + obj->setFieldValue(leftField, YGNodeLayoutGetLeft(root)); + obj->setFieldValue(topField, YGNodeLayoutGetTop(root)); - obj->setFieldValue(paddingLeftField, YGNodeLayoutGetPadding(root, YGEdgeLeft)); - obj->setFieldValue(paddingTopField, YGNodeLayoutGetPadding(root, YGEdgeTop)); - obj->setFieldValue(paddingRightField, YGNodeLayoutGetPadding(root, YGEdgeRight)); - obj->setFieldValue(paddingBottomField, YGNodeLayoutGetPadding(root, YGEdgeBottom)); + obj->setFieldValue(marginLeftField, YGNodeLayoutGetMargin(root, YGEdgeLeft)); + obj->setFieldValue(marginTopField, YGNodeLayoutGetMargin(root, YGEdgeTop)); + obj->setFieldValue(marginRightField, YGNodeLayoutGetMargin(root, YGEdgeRight)); + obj->setFieldValue(marginBottomField, YGNodeLayoutGetMargin(root, YGEdgeBottom)); - obj->setFieldValue(borderLeftField, YGNodeLayoutGetBorder(root, YGEdgeLeft)); - obj->setFieldValue(borderTopField, YGNodeLayoutGetBorder(root, YGEdgeTop)); - obj->setFieldValue(borderRightField, YGNodeLayoutGetBorder(root, YGEdgeRight)); - obj->setFieldValue(borderBottomField, YGNodeLayoutGetBorder(root, YGEdgeBottom)); + obj->setFieldValue(paddingLeftField, YGNodeLayoutGetPadding(root, YGEdgeLeft)); + obj->setFieldValue(paddingTopField, YGNodeLayoutGetPadding(root, YGEdgeTop)); + obj->setFieldValue(paddingRightField, YGNodeLayoutGetPadding(root, YGEdgeRight)); + obj->setFieldValue(paddingBottomField, YGNodeLayoutGetPadding(root, YGEdgeBottom)); - YGTransferLayoutDirection(root, obj); + obj->setFieldValue(borderLeftField, YGNodeLayoutGetBorder(root, YGEdgeLeft)); + obj->setFieldValue(borderTopField, YGNodeLayoutGetBorder(root, YGEdgeTop)); + obj->setFieldValue(borderRightField, YGNodeLayoutGetBorder(root, YGEdgeRight)); + obj->setFieldValue(borderBottomField, YGNodeLayoutGetBorder(root, YGEdgeBottom)); - for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) { - YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i)); + obj->setFieldValue(hasNewLayoutField, true); + YGTransferLayoutDirection(root, obj); + YGNodeSetHasNewLayout(root, false); + for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) { + YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i)); + } + } else { + YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); } - } else { - YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); } } @@ -242,14 +247,6 @@ void jni_YGNodeSetHasBaselineFunc(alias_ref, hasBaselineFunc ? YGJNIBaselineFunc : NULL); } -jboolean jni_YGNodeHasNewLayout(alias_ref, jlong nativePointer) { - return (jboolean) YGNodeGetHasNewLayout(_jlong2YGNodeRef(nativePointer)); -} - -void jni_YGNodeMarkLayoutSeen(alias_ref, jlong nativePointer) { - YGNodeSetHasNewLayout(_jlong2YGNodeRef(nativePointer), false); -} - void jni_YGNodeCopyStyle(alias_ref, jlong dstNativePointer, jlong srcNativePointer) { YGNodeCopyStyle(_jlong2YGNodeRef(dstNativePointer), _jlong2YGNodeRef(srcNativePointer)); } @@ -403,10 +400,8 @@ jint JNI_OnLoad(JavaVM *vm, void *) { YGMakeNativeMethod(jni_YGNodeInsertChild), YGMakeNativeMethod(jni_YGNodeRemoveChild), YGMakeNativeMethod(jni_YGNodeCalculateLayout), - YGMakeNativeMethod(jni_YGNodeHasNewLayout), YGMakeNativeMethod(jni_YGNodeMarkDirty), YGMakeNativeMethod(jni_YGNodeIsDirty), - YGMakeNativeMethod(jni_YGNodeMarkLayoutSeen), YGMakeNativeMethod(jni_YGNodeSetHasMeasureFunc), YGMakeNativeMethod(jni_YGNodeSetHasBaselineFunc), YGMakeNativeMethod(jni_YGNodeCopyStyle),