Skip to content

Commit 5884ab7

Browse files
woehrl01facebook-github-bot
authored andcommitted
Don't transfer layout outputs to java for nodes which don't have a new layout
Summary: As suggested in #484. This is the PR which only adds the part of using a local bool field for storing the hasLayoutFlag, to be able to pass the layout only if it has really changed. Closes #492 Reviewed By: astreet Differential Revision: D4786961 Pulled By: emilsjolander fbshipit-source-id: cf3d354b93f6dcc3ef817ef73a47bd29e37d1848
1 parent 60db018 commit 5884ab7

File tree

2 files changed

+61
-65
lines changed

2 files changed

+61
-65
lines changed

java/com/facebook/yoga/YogaNode.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public static void setLogger(YogaLogger logger) {
8181
private float mBorderBottom = 0;
8282
@DoNotStrip
8383
private int mLayoutDirection = 0;
84+
@DoNotStrip
85+
private boolean mHasNewLayout = true;
8486

8587
private native long jni_YGNodeNew();
8688
public YogaNode() {
@@ -115,6 +117,7 @@ public void reset() {
115117
mHasSetMargin = false;
116118
mHasSetBorder = false;
117119
mHasSetPosition = false;
120+
mHasNewLayout = true;
118121

119122
mWidth = YogaConstants.UNDEFINED;
120123
mHeight = YogaConstants.UNDEFINED;
@@ -180,10 +183,9 @@ public void calculateLayout(float width, float height) {
180183
jni_YGNodeCalculateLayout(mNativePointer, width, height);
181184
}
182185

183-
private native boolean jni_YGNodeHasNewLayout(long nativePointer);
184186
@Override
185187
public boolean hasNewLayout() {
186-
return jni_YGNodeHasNewLayout(mNativePointer);
188+
return mHasNewLayout;
187189
}
188190

189191
private native void jni_YGNodeMarkDirty(long nativePointer);
@@ -198,18 +200,17 @@ public boolean isDirty() {
198200
return jni_YGNodeIsDirty(mNativePointer);
199201
}
200202

201-
private native void jni_YGNodeMarkLayoutSeen(long nativePointer);
202-
@Override
203-
public void markLayoutSeen() {
204-
jni_YGNodeMarkLayoutSeen(mNativePointer);
205-
}
206-
207203
private native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer);
208204
@Override
209205
public void copyStyle(YogaNode srcNode) {
210206
jni_YGNodeCopyStyle(mNativePointer, srcNode.mNativePointer);
211207
}
212208

209+
@Override
210+
public void markLayoutSeen() {
211+
mHasNewLayout = false;
212+
}
213+
213214
private native int jni_YGNodeStyleGetDirection(long nativePointer);
214215
@Override
215216
public YogaDirection getStyleDirection() {

java/jni/YGJNI.cpp

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,59 @@ static void YGTransferLayoutDirection(YGNodeRef node, alias_ref<jobject> javaNod
2424
}
2525

2626
static void YGTransferLayoutOutputsRecursive(YGNodeRef root) {
27-
if (auto obj = YGNodeJobject(root)->lockLocal()) {
28-
static auto widthField = obj->getClass()->getField<jfloat>("mWidth");
29-
static auto heightField = obj->getClass()->getField<jfloat>("mHeight");
30-
static auto leftField = obj->getClass()->getField<jfloat>("mLeft");
31-
static auto topField = obj->getClass()->getField<jfloat>("mTop");
32-
33-
static auto marginLeftField = obj->getClass()->getField<jfloat>("mMarginLeft");
34-
static auto marginTopField = obj->getClass()->getField<jfloat>("mMarginTop");
35-
static auto marginRightField = obj->getClass()->getField<jfloat>("mMarginRight");
36-
static auto marginBottomField = obj->getClass()->getField<jfloat>("mMarginBottom");
37-
38-
static auto paddingLeftField = obj->getClass()->getField<jfloat>("mPaddingLeft");
39-
static auto paddingTopField = obj->getClass()->getField<jfloat>("mPaddingTop");
40-
static auto paddingRightField = obj->getClass()->getField<jfloat>("mPaddingRight");
41-
static auto paddingBottomField = obj->getClass()->getField<jfloat>("mPaddingBottom");
42-
43-
static auto borderLeftField = obj->getClass()->getField<jfloat>("mBorderLeft");
44-
static auto borderTopField = obj->getClass()->getField<jfloat>("mBorderTop");
45-
static auto borderRightField = obj->getClass()->getField<jfloat>("mBorderRight");
46-
static auto borderBottomField = obj->getClass()->getField<jfloat>("mBorderBottom");
47-
48-
obj->setFieldValue(widthField, YGNodeLayoutGetWidth(root));
49-
obj->setFieldValue(heightField, YGNodeLayoutGetHeight(root));
50-
obj->setFieldValue(leftField, YGNodeLayoutGetLeft(root));
51-
obj->setFieldValue(topField, YGNodeLayoutGetTop(root));
52-
53-
obj->setFieldValue(marginLeftField, YGNodeLayoutGetMargin(root, YGEdgeLeft));
54-
obj->setFieldValue(marginTopField, YGNodeLayoutGetMargin(root, YGEdgeTop));
55-
obj->setFieldValue(marginRightField, YGNodeLayoutGetMargin(root, YGEdgeRight));
56-
obj->setFieldValue(marginBottomField, YGNodeLayoutGetMargin(root, YGEdgeBottom));
57-
58-
obj->setFieldValue(paddingLeftField, YGNodeLayoutGetPadding(root, YGEdgeLeft));
59-
obj->setFieldValue(paddingTopField, YGNodeLayoutGetPadding(root, YGEdgeTop));
60-
obj->setFieldValue(paddingRightField, YGNodeLayoutGetPadding(root, YGEdgeRight));
61-
obj->setFieldValue(paddingBottomField, YGNodeLayoutGetPadding(root, YGEdgeBottom));
62-
63-
obj->setFieldValue(borderLeftField, YGNodeLayoutGetBorder(root, YGEdgeLeft));
64-
obj->setFieldValue(borderTopField, YGNodeLayoutGetBorder(root, YGEdgeTop));
65-
obj->setFieldValue(borderRightField, YGNodeLayoutGetBorder(root, YGEdgeRight));
66-
obj->setFieldValue(borderBottomField, YGNodeLayoutGetBorder(root, YGEdgeBottom));
67-
68-
YGTransferLayoutDirection(root, obj);
69-
70-
for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) {
71-
YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i));
27+
if(YGNodeGetHasNewLayout(root)){
28+
if (auto obj = YGNodeJobject(root)->lockLocal()) {
29+
static auto widthField = obj->getClass()->getField<jfloat>("mWidth");
30+
static auto heightField = obj->getClass()->getField<jfloat>("mHeight");
31+
static auto leftField = obj->getClass()->getField<jfloat>("mLeft");
32+
static auto topField = obj->getClass()->getField<jfloat>("mTop");
33+
34+
static auto marginLeftField = obj->getClass()->getField<jfloat>("mMarginLeft");
35+
static auto marginTopField = obj->getClass()->getField<jfloat>("mMarginTop");
36+
static auto marginRightField = obj->getClass()->getField<jfloat>("mMarginRight");
37+
static auto marginBottomField = obj->getClass()->getField<jfloat>("mMarginBottom");
38+
39+
static auto paddingLeftField = obj->getClass()->getField<jfloat>("mPaddingLeft");
40+
static auto paddingTopField = obj->getClass()->getField<jfloat>("mPaddingTop");
41+
static auto paddingRightField = obj->getClass()->getField<jfloat>("mPaddingRight");
42+
static auto paddingBottomField = obj->getClass()->getField<jfloat>("mPaddingBottom");
43+
44+
static auto borderLeftField = obj->getClass()->getField<jfloat>("mBorderLeft");
45+
static auto borderTopField = obj->getClass()->getField<jfloat>("mBorderTop");
46+
static auto borderRightField = obj->getClass()->getField<jfloat>("mBorderRight");
47+
static auto borderBottomField = obj->getClass()->getField<jfloat>("mBorderBottom");
48+
49+
static auto hasNewLayoutField = obj->getClass()->getField<jboolean>("mHasNewLayout");
50+
51+
obj->setFieldValue(widthField, YGNodeLayoutGetWidth(root));
52+
obj->setFieldValue(heightField, YGNodeLayoutGetHeight(root));
53+
obj->setFieldValue(leftField, YGNodeLayoutGetLeft(root));
54+
obj->setFieldValue(topField, YGNodeLayoutGetTop(root));
55+
56+
obj->setFieldValue(marginLeftField, YGNodeLayoutGetMargin(root, YGEdgeLeft));
57+
obj->setFieldValue(marginTopField, YGNodeLayoutGetMargin(root, YGEdgeTop));
58+
obj->setFieldValue(marginRightField, YGNodeLayoutGetMargin(root, YGEdgeRight));
59+
obj->setFieldValue(marginBottomField, YGNodeLayoutGetMargin(root, YGEdgeBottom));
60+
61+
obj->setFieldValue(paddingLeftField, YGNodeLayoutGetPadding(root, YGEdgeLeft));
62+
obj->setFieldValue(paddingTopField, YGNodeLayoutGetPadding(root, YGEdgeTop));
63+
obj->setFieldValue(paddingRightField, YGNodeLayoutGetPadding(root, YGEdgeRight));
64+
obj->setFieldValue(paddingBottomField, YGNodeLayoutGetPadding(root, YGEdgeBottom));
65+
66+
obj->setFieldValue(borderLeftField, YGNodeLayoutGetBorder(root, YGEdgeLeft));
67+
obj->setFieldValue(borderTopField, YGNodeLayoutGetBorder(root, YGEdgeTop));
68+
obj->setFieldValue(borderRightField, YGNodeLayoutGetBorder(root, YGEdgeRight));
69+
obj->setFieldValue(borderBottomField, YGNodeLayoutGetBorder(root, YGEdgeBottom));
70+
71+
obj->setFieldValue<jboolean>(hasNewLayoutField, true);
72+
YGTransferLayoutDirection(root, obj);
73+
YGNodeSetHasNewLayout(root, false);
74+
for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) {
75+
YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i));
76+
}
77+
} else {
78+
YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n");
7279
}
73-
} else {
74-
YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n");
7580
}
7681
}
7782

@@ -242,14 +247,6 @@ void jni_YGNodeSetHasBaselineFunc(alias_ref<jobject>,
242247
hasBaselineFunc ? YGJNIBaselineFunc : NULL);
243248
}
244249

245-
jboolean jni_YGNodeHasNewLayout(alias_ref<jobject>, jlong nativePointer) {
246-
return (jboolean) YGNodeGetHasNewLayout(_jlong2YGNodeRef(nativePointer));
247-
}
248-
249-
void jni_YGNodeMarkLayoutSeen(alias_ref<jobject>, jlong nativePointer) {
250-
YGNodeSetHasNewLayout(_jlong2YGNodeRef(nativePointer), false);
251-
}
252-
253250
void jni_YGNodeCopyStyle(alias_ref<jobject>, jlong dstNativePointer, jlong srcNativePointer) {
254251
YGNodeCopyStyle(_jlong2YGNodeRef(dstNativePointer), _jlong2YGNodeRef(srcNativePointer));
255252
}
@@ -403,10 +400,8 @@ jint JNI_OnLoad(JavaVM *vm, void *) {
403400
YGMakeNativeMethod(jni_YGNodeInsertChild),
404401
YGMakeNativeMethod(jni_YGNodeRemoveChild),
405402
YGMakeNativeMethod(jni_YGNodeCalculateLayout),
406-
YGMakeNativeMethod(jni_YGNodeHasNewLayout),
407403
YGMakeNativeMethod(jni_YGNodeMarkDirty),
408404
YGMakeNativeMethod(jni_YGNodeIsDirty),
409-
YGMakeNativeMethod(jni_YGNodeMarkLayoutSeen),
410405
YGMakeNativeMethod(jni_YGNodeSetHasMeasureFunc),
411406
YGMakeNativeMethod(jni_YGNodeSetHasBaselineFunc),
412407
YGMakeNativeMethod(jni_YGNodeCopyStyle),

0 commit comments

Comments
 (0)