Skip to content

Commit 5bb2265

Browse files
davidaureliofacebook-github-bot
authored andcommitted
Move native methods to a single class
Summary: @public Moving all native methods in a single class provides the benefit of not having to load native bindings eagerly when just creating config objects in the startup paths, or setting Java-only values on them. Loading native bindings triggers additional class loads (`YogaConfig` / `YogaNode`), and can lead to problems in multi-dex scenarions. Reviewed By: pasqualeanatriello Differential Revision: D14560658 fbshipit-source-id: 14e31e3c3b560675b5a752a38ae75ab80a565ea1
1 parent ab3bf40 commit 5bb2265

File tree

6 files changed

+308
-321
lines changed

6 files changed

+308
-321
lines changed

java/com/facebook/yoga/YogaConfig.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,71 @@
66
*/
77
package com.facebook.yoga;
88

9-
import com.facebook.proguard.annotations.DoNotStrip;
109
import com.facebook.soloader.SoLoader;
1110

12-
@DoNotStrip
1311
public class YogaConfig {
1412

1513
public static int SPACING_TYPE = 1;
1614

17-
static {
18-
SoLoader.loadLibrary("yoga");
19-
}
20-
2115
long mNativePointer;
2216
private YogaLogger mLogger;
2317
private YogaNodeCloneFunction mYogaNodeCloneFunction;
2418

25-
private native long jni_YGConfigNew();
2619
public YogaConfig() {
27-
mNativePointer = jni_YGConfigNew();
20+
mNativePointer = YogaNative.jni_YGConfigNew();
2821
if (mNativePointer == 0) {
2922
throw new IllegalStateException("Failed to allocate native memory");
3023
}
3124
}
3225

33-
private native void jni_YGConfigFree(long nativePointer);
3426
@Override
3527
protected void finalize() throws Throwable {
3628
try {
37-
jni_YGConfigFree(mNativePointer);
29+
YogaNative.jni_YGConfigFree(mNativePointer);
3830
} finally {
3931
super.finalize();
4032
}
4133
}
4234

43-
private native void jni_YGConfigSetExperimentalFeatureEnabled(
44-
long nativePointer,
45-
int feature,
46-
boolean enabled);
4735
public void setExperimentalFeatureEnabled(YogaExperimentalFeature feature, boolean enabled) {
48-
jni_YGConfigSetExperimentalFeatureEnabled(mNativePointer, feature.intValue(), enabled);
36+
YogaNative.jni_YGConfigSetExperimentalFeatureEnabled(mNativePointer, feature.intValue(), enabled);
4937
}
5038

51-
private native void jni_YGConfigSetUseWebDefaults(long nativePointer, boolean useWebDefaults);
5239
public void setUseWebDefaults(boolean useWebDefaults) {
53-
jni_YGConfigSetUseWebDefaults(mNativePointer, useWebDefaults);
40+
YogaNative.jni_YGConfigSetUseWebDefaults(mNativePointer, useWebDefaults);
5441
}
5542

56-
private native void jni_YGConfigSetPrintTreeFlag(long nativePointer, boolean enable);
5743
public void setPrintTreeFlag(boolean enable) {
58-
jni_YGConfigSetPrintTreeFlag(mNativePointer, enable);
44+
YogaNative.jni_YGConfigSetPrintTreeFlag(mNativePointer, enable);
5945
}
6046

61-
private native void jni_YGConfigSetPointScaleFactor(long nativePointer, float pixelsInPoint);
6247
public void setPointScaleFactor(float pixelsInPoint) {
63-
jni_YGConfigSetPointScaleFactor(mNativePointer, pixelsInPoint);
48+
YogaNative.jni_YGConfigSetPointScaleFactor(mNativePointer, pixelsInPoint);
6449
}
6550

66-
private native void jni_YGConfigSetUseLegacyStretchBehaviour(long nativePointer, boolean useLegacyStretchBehaviour);
67-
6851
/**
6952
* Yoga previously had an error where containers would take the maximum space possible instead of the minimum
7053
* like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
7154
* Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
7255
*/
7356
public void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour) {
74-
jni_YGConfigSetUseLegacyStretchBehaviour(mNativePointer, useLegacyStretchBehaviour);
57+
YogaNative.jni_YGConfigSetUseLegacyStretchBehaviour(mNativePointer, useLegacyStretchBehaviour);
7558
}
7659

77-
private native void jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
78-
long nativePointer, boolean shouldDiffLayoutWithoutLegacyStretchBehaviour);
7960
/**
8061
* If this flag is set then yoga would diff the layout without legacy flag and would set a bool in
8162
* YogaNode(mDoesLegacyStretchFlagAffectsLayout) with true if the layouts were different and false
8263
* if not
8364
*/
8465
public void setShouldDiffLayoutWithoutLegacyStretchBehaviour(
8566
boolean shouldDiffLayoutWithoutLegacyStretchBehaviour) {
86-
jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
67+
YogaNative.jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
8768
mNativePointer, shouldDiffLayoutWithoutLegacyStretchBehaviour);
8869
}
8970

90-
private native void jni_YGConfigSetLogger(long nativePointer, Object logger);
9171
public void setLogger(YogaLogger logger) {
9272
mLogger = logger;
93-
jni_YGConfigSetLogger(mNativePointer, logger);
73+
YogaNative.jni_YGConfigSetLogger(mNativePointer, logger);
9474
}
9575

9676
public YogaLogger getLogger() {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
6+
*/
7+
package com.facebook.yoga;
8+
9+
import com.facebook.proguard.annotations.DoNotStrip;
10+
import com.facebook.soloader.SoLoader;
11+
12+
@DoNotStrip
13+
public class YogaNative {
14+
static {
15+
SoLoader.loadLibrary("yoga");
16+
}
17+
18+
// YGConfig related
19+
static native long jni_YGConfigNew();
20+
static native void jni_YGConfigFree(long nativePointer);
21+
static native void jni_YGConfigSetExperimentalFeatureEnabled(long nativePointer, int feature, boolean enabled);
22+
static native void jni_YGConfigSetUseWebDefaults(long nativePointer, boolean useWebDefaults);
23+
static native void jni_YGConfigSetPrintTreeFlag(long nativePointer, boolean enable);
24+
static native void jni_YGConfigSetPointScaleFactor(long nativePointer, float pixelsInPoint);
25+
static native void jni_YGConfigSetUseLegacyStretchBehaviour(long nativePointer, boolean useLegacyStretchBehaviour);
26+
static native void jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(long nativePointer, boolean shouldDiffLayoutWithoutLegacyStretchBehaviour);
27+
static native void jni_YGConfigSetLogger(long nativePointer, Object logger);
28+
29+
30+
// YGNode related
31+
static native int jni_YGNodeGetInstanceCount();
32+
static native long jni_YGNodeNew();
33+
static native long jni_YGNodeNewWithConfig(long configPointer);
34+
static native void jni_YGNodeFree(long nativePointer);
35+
static native void jni_YGNodeReset(long nativePointer);
36+
static native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);
37+
static native void jni_YGNodeSetIsReferenceBaseline(long nativePointer, boolean isReferenceBaseline);
38+
static native boolean jni_YGNodeIsReferenceBaseline(long nativePointer);
39+
static native void jni_YGNodeClearChildren(long nativePointer);
40+
static native void jni_YGNodeRemoveChild(long nativePointer, long childPointer);
41+
static native void jni_YGNodeCalculateLayout(long nativePointer, float width, float height, long[] nativePointers, YogaNodeJNIBase[] nodes);
42+
static native void jni_YGNodeMarkDirty(long nativePointer);
43+
static native void jni_YGNodeMarkDirtyAndPropogateToDescendants(long nativePointer);
44+
static native boolean jni_YGNodeIsDirty(long nativePointer);
45+
static native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer);
46+
static native int jni_YGNodeStyleGetDirection(long nativePointer);
47+
static native void jni_YGNodeStyleSetDirection(long nativePointer, int direction);
48+
static native int jni_YGNodeStyleGetFlexDirection(long nativePointer);
49+
static native void jni_YGNodeStyleSetFlexDirection(long nativePointer, int flexDirection);
50+
static native int jni_YGNodeStyleGetJustifyContent(long nativePointer);
51+
static native void jni_YGNodeStyleSetJustifyContent(long nativePointer, int justifyContent);
52+
static native int jni_YGNodeStyleGetAlignItems(long nativePointer);
53+
static native void jni_YGNodeStyleSetAlignItems(long nativePointer, int alignItems);
54+
static native int jni_YGNodeStyleGetAlignSelf(long nativePointer);
55+
static native void jni_YGNodeStyleSetAlignSelf(long nativePointer, int alignSelf);
56+
static native int jni_YGNodeStyleGetAlignContent(long nativePointer);
57+
static native void jni_YGNodeStyleSetAlignContent(long nativePointer, int alignContent);
58+
static native int jni_YGNodeStyleGetPositionType(long nativePointer);
59+
static native void jni_YGNodeStyleSetPositionType(long nativePointer, int positionType);
60+
static native int jni_YGNodeStyleGetFlexWrap(long nativePointer);
61+
static native void jni_YGNodeStyleSetFlexWrap(long nativePointer, int wrapType);
62+
static native int jni_YGNodeStyleGetOverflow(long nativePointer);
63+
static native void jni_YGNodeStyleSetOverflow(long nativePointer, int overflow);
64+
static native int jni_YGNodeStyleGetDisplay(long nativePointer);
65+
static native void jni_YGNodeStyleSetDisplay(long nativePointer, int display);
66+
static native float jni_YGNodeStyleGetFlex(long nativePointer);
67+
static native void jni_YGNodeStyleSetFlex(long nativePointer, float flex);
68+
static native float jni_YGNodeStyleGetFlexGrow(long nativePointer);
69+
static native void jni_YGNodeStyleSetFlexGrow(long nativePointer, float flexGrow);
70+
static native float jni_YGNodeStyleGetFlexShrink(long nativePointer);
71+
static native void jni_YGNodeStyleSetFlexShrink(long nativePointer, float flexShrink);
72+
static native Object jni_YGNodeStyleGetFlexBasis(long nativePointer);
73+
static native void jni_YGNodeStyleSetFlexBasis(long nativePointer, float flexBasis);
74+
static native void jni_YGNodeStyleSetFlexBasisPercent(long nativePointer, float percent);
75+
static native void jni_YGNodeStyleSetFlexBasisAuto(long nativePointer);
76+
static native Object jni_YGNodeStyleGetMargin(long nativePointer, int edge);
77+
static native void jni_YGNodeStyleSetMargin(long nativePointer, int edge, float margin);
78+
static native void jni_YGNodeStyleSetMarginPercent(long nativePointer, int edge, float percent);
79+
static native void jni_YGNodeStyleSetMarginAuto(long nativePointer, int edge);
80+
static native Object jni_YGNodeStyleGetPadding(long nativePointer, int edge);
81+
static native void jni_YGNodeStyleSetPadding(long nativePointer, int edge, float padding);
82+
static native void jni_YGNodeStyleSetPaddingPercent(long nativePointer, int edge, float percent);
83+
static native float jni_YGNodeStyleGetBorder(long nativePointer, int edge);
84+
static native void jni_YGNodeStyleSetBorder(long nativePointer, int edge, float border);
85+
static native Object jni_YGNodeStyleGetPosition(long nativePointer, int edge);
86+
static native void jni_YGNodeStyleSetPosition(long nativePointer, int edge, float position);
87+
static native void jni_YGNodeStyleSetPositionPercent(long nativePointer, int edge, float percent);
88+
static native Object jni_YGNodeStyleGetWidth(long nativePointer);
89+
static native void jni_YGNodeStyleSetWidth(long nativePointer, float width);
90+
static native void jni_YGNodeStyleSetWidthPercent(long nativePointer, float percent);
91+
static native void jni_YGNodeStyleSetWidthAuto(long nativePointer);
92+
static native Object jni_YGNodeStyleGetHeight(long nativePointer);
93+
static native void jni_YGNodeStyleSetHeight(long nativePointer, float height);
94+
static native void jni_YGNodeStyleSetHeightPercent(long nativePointer, float percent);
95+
static native void jni_YGNodeStyleSetHeightAuto(long nativePointer);
96+
static native Object jni_YGNodeStyleGetMinWidth(long nativePointer);
97+
static native void jni_YGNodeStyleSetMinWidth(long nativePointer, float minWidth);
98+
static native void jni_YGNodeStyleSetMinWidthPercent(long nativePointer, float percent);
99+
static native Object jni_YGNodeStyleGetMinHeight(long nativePointer);
100+
static native void jni_YGNodeStyleSetMinHeight(long nativePointer, float minHeight);
101+
static native void jni_YGNodeStyleSetMinHeightPercent(long nativePointer, float percent);
102+
static native Object jni_YGNodeStyleGetMaxWidth(long nativePointer);
103+
static native void jni_YGNodeStyleSetMaxWidth(long nativePointer, float maxWidth);
104+
static native void jni_YGNodeStyleSetMaxWidthPercent(long nativePointer, float percent);
105+
static native Object jni_YGNodeStyleGetMaxHeight(long nativePointer);
106+
static native void jni_YGNodeStyleSetMaxHeight(long nativePointer, float maxheight);
107+
static native void jni_YGNodeStyleSetMaxHeightPercent(long nativePointer, float percent);
108+
static native float jni_YGNodeStyleGetAspectRatio(long nativePointer);
109+
static native void jni_YGNodeStyleSetAspectRatio(long nativePointer, float aspectRatio);
110+
static native void jni_YGNodeSetHasMeasureFunc(long nativePointer, boolean hasMeasureFunc);
111+
static native void jni_YGNodeSetHasBaselineFunc(long nativePointer, boolean hasMeasureFunc);
112+
static native void jni_YGNodePrint(long nativePointer);
113+
static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size);
114+
}

0 commit comments

Comments
 (0)