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

@@ -18,12 +18,10 @@ import javax.annotation.Nullable;
public class YogaNode implements Cloneable {
static {
SoLoader.loadLibrary("yoga");
SoLoader.loadLibrary("yoga");
}
/**
* Get native instance count. Useful for testing only.
*/
/** Get native instance count. Useful for testing only. */
static native int jni_YGNodeGetInstanceCount();
private YogaNodeProperties mDelegate;
@@ -41,6 +39,14 @@ public class YogaNode implements Cloneable {
mDelegate = new YogaNodePropertiesJNI(this, config);
}
public YogaNode(boolean unsafeClownyUseByteBufferValueDoesNotMatter) {
mDelegate = new YogaNodePropertiesByteBuffer(this);
}
public YogaNode(boolean unsafeClownyUseByteBufferValueDoesNotMatter, YogaConfig config) {
mDelegate = new YogaNodePropertiesByteBuffer(this, config);
}
public long getNativePointer() {
return mDelegate.getNativePointer();
}
@@ -69,6 +75,7 @@ public class YogaNode implements Cloneable {
}
private native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);
public void addChildAt(YogaNode child, int i) {
if (child.mOwner != null) {
throw new IllegalStateException("Child already has a parent, it must be removed first.");
@@ -144,6 +151,7 @@ public class YogaNode implements Cloneable {
}
private native void jni_YGNodeRemoveChild(long nativePointer, long childPointer);
public YogaNode removeChildAt(int i) {
if (mChildren == null) {
throw new IllegalStateException(
@@ -156,12 +164,10 @@ public class YogaNode implements Cloneable {
}
/**
* @returns the {@link YogaNode} that owns this {@link YogaNode}.
* The owner is used to identify the YogaTree that a {@link YogaNode} belongs
* to.
* This method will return the parent of the {@link YogaNode} when the
* {@link YogaNode} only belongs to one YogaTree or null when the
* {@link YogaNode} is shared between two or more YogaTrees.
* @returns the {@link YogaNode} that owns this {@link YogaNode}. The owner is used to identify
* the YogaTree that a {@link YogaNode} belongs to. This method will return the parent of the
* {@link YogaNode} when the {@link YogaNode} only belongs to one YogaTree or null when the
* {@link YogaNode} is shared between two or more YogaTrees.
*/
@Nullable
public YogaNode getOwner() {
@@ -179,10 +185,11 @@ public class YogaNode implements Cloneable {
return mChildren == null ? -1 : mChildren.indexOf(child);
}
private native void jni_YGNodeCalculateLayout(long nativePointer, float width, float height);
private native boolean jni_YGNodeCalculateLayout(long nativePointer, float width, float height);
public void calculateLayout(float width, float height) {
jni_YGNodeCalculateLayout(getNativePointer(), width, height);
mDelegate.onAfterCalculateLayout();
boolean hasNewLayout = jni_YGNodeCalculateLayout(getNativePointer(), width, height);
mDelegate.onAfterCalculateLayout(hasNewLayout);
}
public boolean hasNewLayout() {
@@ -190,6 +197,7 @@ public class YogaNode implements Cloneable {
}
private native void jni_YGNodeMarkDirty(long nativePointer);
public void dirty() {
jni_YGNodeMarkDirty(getNativePointer());
}
@@ -205,6 +213,7 @@ public class YogaNode implements Cloneable {
}
private native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer);
public void copyStyle(YogaNode srcNode) {
jni_YGNodeCopyStyle(getNativePointer(), srcNode.getNativePointer());
}
@@ -498,6 +507,7 @@ public class YogaNode implements Cloneable {
}
private native void jni_YGNodeSetHasMeasureFunc(long nativePointer, boolean hasMeasureFunc);
public void setMeasureFunction(YogaMeasureFunction measureFunction) {
mMeasureFunction = measureFunction;
jni_YGNodeSetHasMeasureFunc(getNativePointer(), measureFunction != null);
@@ -523,6 +533,7 @@ public class YogaNode implements Cloneable {
}
private native void jni_YGNodeSetHasBaselineFunc(long nativePointer, boolean hasMeasureFunc);
public void setBaselineFunction(YogaBaselineFunction baselineFunction) {
mBaselineFunction = baselineFunction;
jni_YGNodeSetHasBaselineFunc(getNativePointer(), baselineFunction != null);
@@ -548,8 +559,8 @@ public class YogaNode implements Cloneable {
private native void jni_YGNodePrint(long nativePointer);
/**
* Use the set logger (defaults to adb log) to print out the styles, children, and computed
* layout of the tree rooted at this node.
* Use the set logger (defaults to adb log) to print out the styles, children, and computed layout
* of the tree rooted at this node.
*/
public void print() {
jni_YGNodePrint(getNativePointer());