Skip to content

Commit 23657cc

Browse files
davidaureliofacebook-github-bot
authored andcommitted
Add YogaNodeProperties implementation with ByteBuffer based setters
Summary: @public Adds an implementation of `YogaNodeProperties` that sets style properties using a `ByteBuffer` rather than JNI calls. We hope for a speed improvement. Reviewed By: pasqualeanatriello Differential Revision: D9042225 fbshipit-source-id: c7f2b24eaeddd1190755bec85a5034079bd2f492
1 parent 0c97e75 commit 23657cc

File tree

4 files changed

+302
-9
lines changed

4 files changed

+302
-9
lines changed

ReactAndroid/src/main/java/com/facebook/yoga/YogaNode.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class YogaNode implements Cloneable {
2121
SoLoader.loadLibrary("yoga");
2222
}
2323

24+
public static final int BYTE_BUFFER = 1;
25+
public static final int HYBRID = 2;
26+
2427
/** Get native instance count. Useful for testing only. */
2528
static native int jni_YGNodeGetInstanceCount();
2629

@@ -39,12 +42,30 @@ public YogaNode(YogaConfig config) {
3942
mDelegate = new YogaNodePropertiesJNI(this, config);
4043
}
4144

42-
public YogaNode(boolean unsafeClownyUseByteBufferValueDoesNotMatter) {
43-
mDelegate = new YogaNodePropertiesByteBuffer(this);
45+
public YogaNode(int storageType) {
46+
switch (storageType) {
47+
case BYTE_BUFFER:
48+
mDelegate = new YogaNodePropertiesByteBuffer(this);
49+
break;
50+
case HYBRID:
51+
mDelegate = new YogaNodePropertiesHybrid(this);
52+
break;
53+
default:
54+
mDelegate = new YogaNodePropertiesJNI(this);
55+
}
4456
}
4557

46-
public YogaNode(boolean unsafeClownyUseByteBufferValueDoesNotMatter, YogaConfig config) {
47-
mDelegate = new YogaNodePropertiesByteBuffer(this, config);
58+
public YogaNode(int storageType, YogaConfig config) {
59+
switch (storageType) {
60+
case BYTE_BUFFER:
61+
mDelegate = new YogaNodePropertiesByteBuffer(this, config);
62+
break;
63+
case HYBRID:
64+
mDelegate = new YogaNodePropertiesHybrid(this, config);
65+
break;
66+
default:
67+
mDelegate = new YogaNodePropertiesJNI(this, config);
68+
}
4869
}
4970

5071
public long getNativePointer() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
/*
2+
* Copyright (c) 2018-present, Facebook, Inc.
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+
*/
8+
package com.facebook.yoga;
9+
10+
import com.facebook.proguard.annotations.DoNotStrip;
11+
import com.facebook.soloader.SoLoader;
12+
import java.nio.ByteBuffer;
13+
import java.nio.ByteOrder;
14+
15+
@DoNotStrip
16+
public class YogaNodePropertiesHybrid extends YogaNodePropertiesJNI {
17+
18+
static {
19+
SoLoader.loadLibrary("yoga");
20+
}
21+
22+
private ByteBuffer mStyleBuffer;
23+
24+
private static native ByteBuffer jni_getStyleBuffer(long nativePointer);
25+
26+
public YogaNodePropertiesHybrid(YogaNode node) {
27+
super(node);
28+
mStyleBuffer = jni_getStyleBuffer(getNativePointer()).order(ByteOrder.LITTLE_ENDIAN);
29+
}
30+
31+
public YogaNodePropertiesHybrid(YogaNode node, YogaConfig config) {
32+
super(node, config);
33+
mStyleBuffer = jni_getStyleBuffer(getNativePointer()).order(ByteOrder.LITTLE_ENDIAN);
34+
}
35+
36+
@Override
37+
public void setDirection(YogaDirection direction) {
38+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleDirection, direction.intValue());
39+
}
40+
41+
@Override
42+
public void setFlexDirection(YogaFlexDirection flexDirection) {
43+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleFlexDirection, flexDirection.intValue());
44+
}
45+
46+
@Override
47+
public void setJustifyContent(YogaJustify justifyContent) {
48+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleJustifyContent, justifyContent.intValue());
49+
}
50+
51+
@Override
52+
public void setAlignItems(YogaAlign alignItems) {
53+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleAlignItems, alignItems.intValue());
54+
}
55+
56+
@Override
57+
public void setAlignSelf(YogaAlign alignSelf) {
58+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleAlignSelf, alignSelf.intValue());
59+
}
60+
61+
@Override
62+
public void setAlignContent(YogaAlign alignContent) {
63+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleAlignContent, alignContent.intValue());
64+
}
65+
66+
@Override
67+
public void setPositionType(YogaPositionType positionType) {
68+
mStyleBuffer.putInt(YogaNodeMemoryLayout.stylePositionType, positionType.intValue());
69+
}
70+
71+
@Override
72+
public void setWrap(YogaWrap flexWrap) {
73+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleFlexWrap, flexWrap.intValue());
74+
}
75+
76+
@Override
77+
public void setOverflow(YogaOverflow overflow) {
78+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleOverflow, overflow.intValue());
79+
}
80+
81+
@Override
82+
public void setDisplay(YogaDisplay display) {
83+
mStyleBuffer.putInt(YogaNodeMemoryLayout.styleDisplay, display.intValue());
84+
}
85+
86+
@Override
87+
public void setFlex(float flex) {
88+
YogaNodeMemoryLayout.putOptional(mStyleBuffer, YogaNodeMemoryLayout.styleFlex, flex);
89+
}
90+
91+
@Override
92+
public void setFlexGrow(float flexGrow) {
93+
YogaNodeMemoryLayout.putOptional(mStyleBuffer, YogaNodeMemoryLayout.styleFlexGrow, flexGrow);
94+
}
95+
96+
@Override
97+
public void setFlexShrink(float flexShrink) {
98+
YogaNodeMemoryLayout.putOptional(
99+
mStyleBuffer, YogaNodeMemoryLayout.styleFlexShrink, flexShrink);
100+
}
101+
102+
@Override
103+
public void setFlexBasis(float flexBasis) {
104+
YogaNodeMemoryLayout.putPointValue(
105+
mStyleBuffer, YogaNodeMemoryLayout.styleFlexBasis, flexBasis);
106+
}
107+
108+
@Override
109+
public void setFlexBasisPercent(float percent) {
110+
YogaNodeMemoryLayout.putPercentValue(
111+
mStyleBuffer, YogaNodeMemoryLayout.styleFlexBasis, percent);
112+
}
113+
114+
@Override
115+
public void setFlexBasisAuto() {
116+
YogaNodeMemoryLayout.putAutoValue(mStyleBuffer, YogaNodeMemoryLayout.styleFlexBasis);
117+
}
118+
119+
@Override
120+
public void setMargin(YogaEdge edge, float margin) {
121+
mEdgeSetFlag |= MARGIN;
122+
YogaNodeMemoryLayout.putPointValue(
123+
mStyleBuffer, YogaNodeMemoryLayout.styleMarginOffset(edge), margin);
124+
}
125+
126+
@Override
127+
public void setMarginPercent(YogaEdge edge, float percent) {
128+
mEdgeSetFlag |= MARGIN;
129+
YogaNodeMemoryLayout.putPercentValue(
130+
mStyleBuffer, YogaNodeMemoryLayout.styleMarginOffset(edge), percent);
131+
}
132+
133+
@Override
134+
public void setMarginAuto(YogaEdge edge) {
135+
mEdgeSetFlag |= MARGIN;
136+
YogaNodeMemoryLayout.putAutoValue(mStyleBuffer, YogaNodeMemoryLayout.styleMarginOffset(edge));
137+
}
138+
139+
@Override
140+
public void setPadding(YogaEdge edge, float padding) {
141+
mEdgeSetFlag |= PADDING;
142+
YogaNodeMemoryLayout.putPointValue(
143+
mStyleBuffer, YogaNodeMemoryLayout.stylePaddingOffset(edge), padding);
144+
}
145+
146+
@Override
147+
public void setPaddingPercent(YogaEdge edge, float percent) {
148+
mEdgeSetFlag |= PADDING;
149+
YogaNodeMemoryLayout.putPercentValue(
150+
mStyleBuffer, YogaNodeMemoryLayout.stylePaddingOffset(edge), percent);
151+
}
152+
153+
@Override
154+
public void setBorder(YogaEdge edge, float border) {
155+
mEdgeSetFlag |= BORDER;
156+
YogaNodeMemoryLayout.putPointValue(
157+
mStyleBuffer, YogaNodeMemoryLayout.styleBorderOffset(edge), border);
158+
}
159+
160+
@Override
161+
public void setPosition(YogaEdge edge, float position) {
162+
mHasSetPosition = true;
163+
YogaNodeMemoryLayout.putPointValue(
164+
mStyleBuffer, YogaNodeMemoryLayout.stylePositionOffset(edge), position);
165+
}
166+
167+
@Override
168+
public void setPositionPercent(YogaEdge edge, float percent) {
169+
mHasSetPosition = true;
170+
YogaNodeMemoryLayout.putPercentValue(
171+
mStyleBuffer, YogaNodeMemoryLayout.stylePositionOffset(edge), percent);
172+
}
173+
174+
@Override
175+
public void setWidth(float width) {
176+
YogaNodeMemoryLayout.putPointValue(mStyleBuffer, YogaNodeMemoryLayout.styleWidth, width);
177+
}
178+
179+
@Override
180+
public void setWidthPercent(float percent) {
181+
YogaNodeMemoryLayout.putPercentValue(mStyleBuffer, YogaNodeMemoryLayout.styleWidth, percent);
182+
}
183+
184+
@Override
185+
public void setWidthAuto() {
186+
YogaNodeMemoryLayout.putAutoValue(mStyleBuffer, YogaNodeMemoryLayout.styleWidth);
187+
}
188+
189+
@Override
190+
public void setHeight(float height) {
191+
YogaNodeMemoryLayout.putPointValue(mStyleBuffer, YogaNodeMemoryLayout.styleHeight, height);
192+
}
193+
194+
@Override
195+
public void setHeightPercent(float percent) {
196+
YogaNodeMemoryLayout.putPercentValue(mStyleBuffer, YogaNodeMemoryLayout.styleHeight, percent);
197+
}
198+
199+
@Override
200+
public void setHeightAuto() {
201+
YogaNodeMemoryLayout.putAutoValue(mStyleBuffer, YogaNodeMemoryLayout.styleHeight);
202+
}
203+
204+
@Override
205+
public void setMinWidth(float minWidth) {
206+
YogaNodeMemoryLayout.putPointValue(mStyleBuffer, YogaNodeMemoryLayout.styleMinWidth, minWidth);
207+
}
208+
209+
@Override
210+
public void setMinWidthPercent(float percent) {
211+
YogaNodeMemoryLayout.putPercentValue(mStyleBuffer, YogaNodeMemoryLayout.styleMinWidth, percent);
212+
}
213+
214+
@Override
215+
public void setMinHeight(float minHeight) {
216+
YogaNodeMemoryLayout.putPointValue(
217+
mStyleBuffer, YogaNodeMemoryLayout.styleMinHeight, minHeight);
218+
}
219+
220+
@Override
221+
public void setMinHeightPercent(float percent) {
222+
YogaNodeMemoryLayout.putPercentValue(
223+
mStyleBuffer, YogaNodeMemoryLayout.styleMinHeight, percent);
224+
}
225+
226+
@Override
227+
public void setMaxWidth(float maxWidth) {
228+
YogaNodeMemoryLayout.putPointValue(mStyleBuffer, YogaNodeMemoryLayout.styleMaxWidth, maxWidth);
229+
}
230+
231+
@Override
232+
public void setMaxWidthPercent(float percent) {
233+
YogaNodeMemoryLayout.putPercentValue(mStyleBuffer, YogaNodeMemoryLayout.styleMaxWidth, percent);
234+
}
235+
236+
@Override
237+
public void setMaxHeight(float maxHeight) {
238+
YogaNodeMemoryLayout.putPointValue(
239+
mStyleBuffer, YogaNodeMemoryLayout.styleMaxHeight, maxHeight);
240+
}
241+
242+
@Override
243+
public void setMaxHeightPercent(float percent) {
244+
YogaNodeMemoryLayout.putPercentValue(
245+
mStyleBuffer, YogaNodeMemoryLayout.styleMaxHeight, percent);
246+
}
247+
248+
@Override
249+
public void setAspectRatio(float aspectRatio) {
250+
YogaNodeMemoryLayout.putOptional(
251+
mStyleBuffer, YogaNodeMemoryLayout.styleAspectRatio, aspectRatio);
252+
}
253+
254+
@Override
255+
public YogaNodeProperties clone(YogaNode node) {
256+
YogaNodePropertiesHybrid clone = (YogaNodePropertiesHybrid) super.clone(node);
257+
clone.mStyleBuffer =
258+
jni_getStyleBuffer(clone.getNativePointer()).order(ByteOrder.LITTLE_ENDIAN);
259+
return clone;
260+
}
261+
}

ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesJNI.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public class YogaNodePropertiesJNI implements Cloneable, YogaNodeProperties {
2020
private long mNativePointer;
2121

2222
/* Those flags needs be in sync with YGJNI.cpp */
23-
private static final int MARGIN = 1;
24-
private static final int PADDING = 2;
25-
private static final int BORDER = 4;
23+
protected static final int MARGIN = 1;
24+
protected static final int PADDING = 2;
25+
protected static final int BORDER = 4;
2626

27-
@DoNotStrip private int mEdgeSetFlag = 0;
27+
@DoNotStrip protected int mEdgeSetFlag = 0;
2828

29-
private boolean mHasSetPosition = false;
29+
protected boolean mHasSetPosition = false;
3030

3131
@DoNotStrip private float mWidth = YogaConstants.UNDEFINED;
3232
@DoNotStrip private float mHeight = YogaConstants.UNDEFINED;

ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ struct JYogaNodePropertiesByteBuffer
3333
"Lcom/facebook/yoga/YogaNodePropertiesByteBuffer";
3434
};
3535

36+
struct JYogaNodePropertiesHybrid
37+
: public JavaClass<JYogaNodePropertiesHybrid, JYogaNodePropertiesJNI> {
38+
static constexpr auto kJavaDescriptor =
39+
"Lcom/facebook/yoga/YogaNodePropertiesHybrid";
40+
};
41+
3642
struct YGConfigContext {
3743
global_ref<jobject>* logger;
3844
global_ref<jobject>* config;
@@ -858,5 +864,10 @@ jint JNI_OnLoad(JavaVM* vm, void*) {
858864
YGMakeNativeMethod(jni_getStyleBuffer),
859865
YGMakeNativeMethod(jni_getLayoutBuffer),
860866
});
867+
registerNatives(
868+
"com/facebook/yoga/YogaNodePropertiesHybrid",
869+
{
870+
YGMakeNativeMethod(jni_getStyleBuffer),
871+
});
861872
});
862873
}

0 commit comments

Comments
 (0)