Add YogaNodeProperties implementation based on ByteBuffer

Summary:
@public
Adds an implementation of `YogaNodeProperties` that accesses style and layout properties using a `ByteBuffer` rather than JNI calls.
We hope for a speed improvement.

This needs further cleanup after experimenting, e.g. to codegen the offsets.

Reviewed By: pasqualeanatriello

Differential Revision: D8911723

fbshipit-source-id: 3c24b57eb545155878896ebb5d64d4553eb6bedc
This commit is contained in:
David Aurelio
2018-07-30 09:30:51 -07:00
committed by Facebook Github Bot
parent b1821ab4cd
commit 3499e2e0ef
29 changed files with 2034 additions and 979 deletions

View File

@@ -1,10 +1,10 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
/*
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.yoga;
import static org.junit.Assert.assertEquals;
@@ -17,30 +17,39 @@ import static org.junit.Assert.fail;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class YogaNodeTest {
@Parameterized.Parameters(name = "{0}")
public static Iterable<TestParametrization.NodeFactory> nodeFactories() {
return TestParametrization.nodeFactories();
}
@Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory;
@Test
public void testInit() {
final int refCount = YogaNode.jni_YGNodeGetInstanceCount();
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
assertEquals(refCount + 1, YogaNode.jni_YGNodeGetInstanceCount());
}
@Test
public void testBaseline() {
final YogaNode root = new YogaNode();
final YogaNode root = createNode();
root.setFlexDirection(YogaFlexDirection.ROW);
root.setAlignItems(YogaAlign.BASELINE);
root.setWidth(100);
root.setHeight(100);
final YogaNode child1 = new YogaNode();
final YogaNode child1 = createNode();
child1.setWidth(40);
child1.setHeight(40);
root.addChildAt(child1, 0);
final YogaNode child2 = new YogaNode();
final YogaNode child2 = createNode();
child2.setWidth(40);
child2.setHeight(40);
child2.setBaselineFunction(new YogaBaselineFunction() {
@@ -58,7 +67,7 @@ public class YogaNodeTest {
@Test
public void testMeasure() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setMeasureFunction(new YogaMeasureFunction() {
public long measure(
YogaNode node,
@@ -76,7 +85,7 @@ public class YogaNodeTest {
@Test
public void testMeasureFloat() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setMeasureFunction(new YogaMeasureFunction() {
public long measure(
YogaNode node,
@@ -94,7 +103,7 @@ public class YogaNodeTest {
@Test
public void testMeasureFloatMin() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setMeasureFunction(new YogaMeasureFunction() {
public long measure(
YogaNode node,
@@ -112,7 +121,7 @@ public class YogaNodeTest {
@Test
public void testMeasureFloatBigNumber() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
final float bigNumber = (float) 10E5;
node.setMeasureFunction(
new YogaMeasureFunction() {
@@ -132,10 +141,10 @@ public class YogaNodeTest {
@Test
public void testCopyStyle() {
final YogaNode node0 = new YogaNode();
final YogaNode node0 = createNode();
assertTrue(YogaConstants.isUndefined(node0.getMaxHeight()));
final YogaNode node1 = new YogaNode();
final YogaNode node1 = createNode();
node1.setMaxHeight(100);
node0.copyStyle(node1);
@@ -144,7 +153,7 @@ public class YogaNodeTest {
@Test
public void testLayoutMargin() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setWidth(100);
node.setHeight(100);
node.setMargin(YogaEdge.START, 1);
@@ -161,7 +170,7 @@ public class YogaNodeTest {
@Test
public void testLayoutPadding() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setWidth(100);
node.setHeight(100);
node.setPadding(YogaEdge.START, 1);
@@ -178,7 +187,7 @@ public class YogaNodeTest {
@Test
public void testLayoutBorder() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setWidth(100);
node.setHeight(100);
node.setBorder(YogaEdge.START, 1);
@@ -197,13 +206,13 @@ public class YogaNodeTest {
public void testUseWebDefaults() {
final YogaConfig config = new YogaConfig();
config.setUseWebDefaults(true);
final YogaNode node = new YogaNode(config);
final YogaNode node = createNode(config);
assertEquals(YogaFlexDirection.ROW, node.getFlexDirection());
}
@Test
public void testPercentPaddingOnRoot() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
node.setPaddingPercent(YogaEdge.ALL, 10);
node.calculateLayout(50, 50);
@@ -215,7 +224,7 @@ public class YogaNodeTest {
@Test
public void testDefaultEdgeValues() {
final YogaNode node = new YogaNode();
final YogaNode node = createNode();
for (YogaEdge edge : YogaEdge.values()) {
assertEquals(YogaUnit.UNDEFINED, node.getMargin(edge).unit);
@@ -228,9 +237,9 @@ public class YogaNodeTest {
@Test
public void testCloneNode() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = new YogaNode(config);
YogaNode child = new YogaNode(config);
YogaNode grandChild = new YogaNode(config);
YogaNode root = createNode(config);
YogaNode child = createNode(config);
YogaNode grandChild = createNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
@@ -255,9 +264,9 @@ public class YogaNodeTest {
@Test
public void testCloneWithNewChildren() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = new YogaNode(config);
YogaNode child = new YogaNode(config);
YogaNode grandChild = new YogaNode(config);
YogaNode root = createNode(config);
YogaNode child = createNode(config);
YogaNode grandChild = createNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
@@ -274,9 +283,9 @@ public class YogaNodeTest {
@Test
public void testAddSharedChildCloneWithNewChildren() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = new YogaNode(config);
YogaNode child = new YogaNode(config);
YogaNode grandChild = new YogaNode(config);
YogaNode root = createNode(config);
YogaNode child = createNode(config);
YogaNode grandChild = createNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
@@ -306,10 +315,10 @@ public class YogaNodeTest {
return oldNode.clone();
}
});
YogaNode root = new YogaNode(config);
YogaNode root = createNode(config);
root.setWidth(100f);
root.setHeight(100f);
YogaNode child0 = new YogaNode(config);
YogaNode child0 = createNode(config);
root.addChildAt(child0, 0);
child0.setWidth(50f);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
@@ -359,21 +368,29 @@ public class YogaNodeTest {
YogaConfig config = new YogaConfig();
config.setShouldDiffLayoutWithoutLegacyStretchBehaviour(true);
config.setUseLegacyStretchBehaviour(true);
YogaNode root = new YogaNode(config);
YogaNode root = createNode(config);
root.setWidth(500);
root.setHeight(500);
YogaNode root_child0 = new YogaNode(config);
YogaNode root_child0 = createNode(config);
root_child0.setAlignItems(YogaAlign.FLEX_START);
root.addChildAt(root_child0, 0);
YogaNode root_child0_child0 = new YogaNode(config);
YogaNode root_child0_child0 = createNode(config);
root_child0_child0.setFlexGrow(1);
root_child0_child0.setFlexShrink(1);
root_child0.addChildAt(root_child0_child0, 0);
YogaNode root_child0_child0_child0 = new YogaNode(config);
YogaNode root_child0_child0_child0 = createNode(config);
root_child0_child0_child0.setFlexGrow(1);
root_child0_child0_child0.setFlexShrink(1);
root_child0_child0.addChildAt(root_child0_child0_child0, 0);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
assertFalse(root.getDoesLegacyStretchFlagAffectsLayout());
}
private YogaNode createNode() {
return mNodeFactory.create();
}
private YogaNode createNode(YogaConfig config) {
return mNodeFactory.create(config);
}
}