JNI batching API using float array

Summary:
Added a new API `YGNodeSetLayoutStyleInputs` to pass layout style inputs from java to native code.
All the style inputs are passed in a float array in [key1, key2, value2, key3, value3a, value3b .....] format over JNI layer.
There are three types of style inputs
- do not need any value to be passed along with them like WidthAuto, HeightAuto
- need one value to be passed like Width, Height
- need two values to be passed like Margin, Padding (edge value and actual margin, padding value)

Reviewed By: davidaurelio

Differential Revision: D14166948

fbshipit-source-id: 4bea64d6a429959c3962c87e337914dcd99199fd
This commit is contained in:
Sidharth Guglani
2019-02-28 12:42:22 -08:00
committed by Facebook Github Bot
parent 8823cc357a
commit afadc5cf6b
4 changed files with 277 additions and 0 deletions

View File

@@ -212,4 +212,6 @@ public abstract class YogaNode {
public abstract Object getData();
public abstract void print();
public abstract void setStyleInputs(float[] styleInputs, int size);
}

View File

@@ -730,6 +730,12 @@ public class YogaNodeJNI extends YogaNode {
jni_YGNodePrint(mNativePointer);
}
private static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size);
public void setStyleInputs(float[] styleInputsArray, int size) {
jni_YGNodeSetStyleInputs(mNativePointer, styleInputsArray, size);
}
/**
* This method replaces the child at childIndex position with the newNode received by parameter.
* This is different than calling removeChildAt and addChildAt because this method ONLY replaces

View File

@@ -0,0 +1,53 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public class YogaStyleInputs {
public static final short LAYOUT_DIRECTION = 0;
public static final short FLEX_DIRECTION = 1;
public static final short FLEX = 2;
public static final short FLEX_GROW = 3;
public static final short FLEX_SHRINK = 4;
public static final short FLEX_BASIS = 5;
public static final short FLEX_BASIS_PERCENT = 6;
public static final short FLEX_BASIS_AUTO = 7;
public static final short FLEX_WRAP = 8;
public static final short WIDTH = 9;
public static final short WIDTH_PERCENT = 10;
public static final short WIDTH_AUTO = 11;
public static final short MIN_WIDTH = 12;
public static final short MIN_WIDTH_PERCENT = 13;
public static final short MAX_WIDTH = 14;
public static final short MAX_WIDTH_PERCENT = 15;
public static final short HEIGHT = 16;
public static final short HEIGHT_PERCENT = 17;
public static final short HEIGHT_AUTO = 18;
public static final short MIN_HEIGHT = 19;
public static final short MIN_HEIGHT_PERCENT = 20;
public static final short MAX_HEIGHT = 21;
public static final short MAX_HEIGHT_PERCENT = 22;
public static final short JUSTIFY_CONTENT = 23;
public static final short ALIGN_ITEMS = 24;
public static final short ALIGN_SELF = 25;
public static final short ALIGN_CONTENT = 26;
public static final short POSITION_TYPE = 27;
public static final short ASPECT_RATIO = 28;
public static final short OVERFLOW = 29;
public static final short DISPLAY = 30;
public static final short MARGIN = 31;
public static final short MARGIN_PERCENT = 32;
public static final short MARGIN_AUTO = 33;
public static final short PADDING = 34;
public static final short PADDING_PERCENT = 35;
public static final short BORDER = 36;
public static final short POSITION = 37;
public static final short POSITION_PERCENT = 38;
public static final short IS_REFERENCE_BASELINE = 39;
}

View File

@@ -15,6 +15,49 @@ using namespace facebook::jni;
using namespace std;
using facebook::yoga::detail::Log;
enum YGStyleInput {
LayoutDirection,
FlexDirection,
Flex,
FlexGrow,
FlexShrink,
FlexBasis,
FlexBasisPercent,
FlexBasisAuto,
FlexWrap,
Width,
WidthPercent,
WidthAuto,
MinWidth,
MinWidthPercent,
MaxWidth,
MaxWidthPercent,
Height,
HeightPercent,
HeightAuto,
MinHeight,
MinHeightPercent,
MaxHeight,
MaxHeightPercent,
JustifyContent,
AlignItems,
AlignSelf,
AlignContent,
PositionType,
AspectRatio,
Overflow,
Display,
Margin,
MarginPercent,
MarginAuto,
Padding,
PaddingPercent,
Border,
Position,
PositionPercent,
IsReferenceBaseline,
};
struct JYogaNode : public JavaClass<JYogaNode> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaNodeJNI;";
};
@@ -647,6 +690,178 @@ void jni_YGConfigSetLogger(
}
}
static void YGNodeSetStyleInputs(
const YGNodeRef node,
float* styleInputs,
int size) {
const auto end = styleInputs + size;
while (styleInputs < end) {
auto styleInputKey = static_cast<YGStyleInput>((int) *styleInputs++);
switch (styleInputKey) {
case LayoutDirection:
YGNodeStyleSetDirection(node, static_cast<YGDirection>(*styleInputs++));
break;
case FlexDirection:
YGNodeStyleSetFlexDirection(
node, static_cast<YGFlexDirection>(*styleInputs++));
break;
case Flex:
YGNodeStyleSetFlex(node, *styleInputs++);
break;
case FlexGrow:
YGNodeStyleSetFlexGrow(node, *styleInputs++);
break;
case FlexShrink:
YGNodeStyleSetFlexShrink(node, *styleInputs++);
break;
case FlexBasis:
YGNodeStyleSetFlexBasis(node, *styleInputs++);
break;
case FlexBasisPercent:
YGNodeStyleSetFlexBasisPercent(node, *styleInputs++);
break;
case FlexBasisAuto:
YGNodeStyleSetFlexBasisAuto(node);
break;
case FlexWrap:
YGNodeStyleSetFlexWrap(node, static_cast<YGWrap>(*styleInputs++));
break;
case Width:
YGNodeStyleSetWidth(node, *styleInputs++);
break;
case WidthPercent:
YGNodeStyleSetWidthPercent(node, *styleInputs++);
break;
case WidthAuto:
YGNodeStyleSetWidthAuto(node);
break;
case MinWidth:
YGNodeStyleSetMinWidth(node, *styleInputs++);
break;
case MinWidthPercent:
YGNodeStyleSetMinWidthPercent(node, *styleInputs++);
break;
case MaxWidth:
YGNodeStyleSetMaxWidth(node, *styleInputs++);
break;
case MaxWidthPercent:
YGNodeStyleSetMaxWidthPercent(node, *styleInputs++);
break;
case Height:
YGNodeStyleSetHeight(node, *styleInputs++);
break;
case HeightPercent:
YGNodeStyleSetHeightPercent(node, *styleInputs++);
break;
case HeightAuto:
YGNodeStyleSetHeightAuto(node);
break;
case MinHeight:
YGNodeStyleSetMinHeight(node, *styleInputs++);
break;
case MinHeightPercent:
YGNodeStyleSetMinHeightPercent(node, *styleInputs++);
break;
case MaxHeight:
YGNodeStyleSetMaxHeight(node, *styleInputs++);
break;
case MaxHeightPercent:
YGNodeStyleSetMaxHeightPercent(node, *styleInputs++);
break;
case JustifyContent:
YGNodeStyleSetJustifyContent(
node, static_cast<YGJustify>(*styleInputs++));
break;
case AlignItems:
YGNodeStyleSetAlignItems(node, static_cast<YGAlign>(*styleInputs++));
break;
case AlignSelf:
YGNodeStyleSetAlignSelf(node, static_cast<YGAlign>(*styleInputs++));
break;
case AlignContent:
YGNodeStyleSetAlignContent(node, static_cast<YGAlign>(*styleInputs++));
break;
case PositionType:
YGNodeStyleSetPositionType(
node, static_cast<YGPositionType>(*styleInputs++));
break;
case AspectRatio:
YGNodeStyleSetAspectRatio(node, *styleInputs++);
break;
case Overflow:
YGNodeStyleSetOverflow(node, static_cast<YGOverflow>(*styleInputs++));
break;
case Display:
YGNodeStyleSetDisplay(node, static_cast<YGDisplay>(*styleInputs++));
break;
case Margin: {
float edge = *styleInputs++;
float marginValue = *styleInputs++;
YGNodeStyleSetMargin(node, static_cast<YGEdge>(edge), marginValue);
break;
}
case MarginPercent: {
float edge = *styleInputs++;
float marginPercent = *styleInputs++;
YGNodeStyleSetMarginPercent(
node, static_cast<YGEdge>(edge), marginPercent);
break;
}
case MarginAuto:
YGNodeStyleSetMarginAuto(node, static_cast<YGEdge>(*styleInputs++));
break;
case Padding: {
float edge = *styleInputs++;
float paddingValue = *styleInputs++;
YGNodeStyleSetPadding(node, static_cast<YGEdge>(edge), paddingValue);
break;
}
case PaddingPercent: {
float edge = *styleInputs++;
float paddingPercent = *styleInputs++;
YGNodeStyleSetPaddingPercent(
node, static_cast<YGEdge>(edge), paddingPercent);
break;
}
case Border: {
float edge = *styleInputs++;
float borderValue = *styleInputs++;
YGNodeStyleSetBorder(node, static_cast<YGEdge>(edge), borderValue);
break;
}
case Position: {
float edge = *styleInputs++;
float positionValue = *styleInputs++;
YGNodeStyleSetPosition(node, static_cast<YGEdge>(edge), positionValue);
break;
}
case PositionPercent: {
float edge = *styleInputs++;
float positionPercent = *styleInputs++;
YGNodeStyleSetPositionPercent(
node, static_cast<YGEdge>(edge), positionPercent);
break;
}
case IsReferenceBaseline: {
YGNodeSetIsReferenceBaseline(node, *styleInputs++ == 1 ? true : false);
break;
}
default:
break;
}
}
}
void jni_YGNodeSetStyleInputs(
alias_ref<jobject> thiz,
jlong nativePointer,
alias_ref<JArrayFloat> styleInputs,
jint size) {
float result[size];
styleInputs->getRegion(0, size, result);
YGNodeSetStyleInputs(_jlong2YGNodeRef(nativePointer), result, size);
}
jint jni_YGNodeGetInstanceCount() {
return YGNodeGetInstanceCount();
}
@@ -823,6 +1038,7 @@ jint JNI_OnLoad(JavaVM* vm, void*) {
YGMakeCriticalNativeMethod(jni_YGNodeStyleSetAspectRatio),
YGMakeCriticalNativeMethod(jni_YGNodeGetInstanceCount),
YGMakeCriticalNativeMethod(jni_YGNodePrint),
YGMakeNativeMethod(jni_YGNodeSetStyleInputs),
});
registerNatives(
"com/facebook/yoga/YogaConfig",