Add jni bindings

Summary: Add jni bindings for csslayout. First step in many of removing LayoutEngine.java and performing all layout in native.

Reviewed By: lucasr

Differential Revision: D3648793

fbshipit-source-id: d0e696e196fa7c63109c9117a65645ca3d6c9c00
This commit is contained in:
Emil Sjolander
2016-08-04 08:19:59 -07:00
committed by Facebook Github Bot 9
parent 7af5e3d68d
commit c74eae50ac
13 changed files with 1164 additions and 33 deletions

View File

@@ -28,9 +28,9 @@ import static com.facebook.csslayout.Spacing.TOP;
* A CSS Node. It has a style object you can manipulate at {@link #style}. After calling
* {@link #calculateLayout()}, {@link #layout} will be filled with the results of the layout.
*/
public class CSSNode {
public class CSSNode implements CSSNodeAPI<CSSNode> {
private static enum LayoutState {
private enum LayoutState {
/**
* Some property of this node or its children has changes and the current values in
* {@link #layout} are not valid.
@@ -49,24 +49,14 @@ public class CSSNode {
UP_TO_DATE,
}
public static interface MeasureFunction {
/**
* Should measure the given node and put the result in the given MeasureOutput.
*
* NB: measure is NOT guaranteed to be threadsafe/re-entrant safe!
*/
public void measure(CSSNode node, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode, MeasureOutput measureOutput);
}
// VisibleForTesting
/*package*/ final CSSStyle style = new CSSStyle();
/*package*/ final CSSLayout layout = new CSSLayout();
/*package*/ final CachedCSSLayout lastLayout = new CachedCSSLayout();
final CSSStyle style = new CSSStyle();
final CSSLayout layout = new CSSLayout();
final CachedCSSLayout lastLayout = new CachedCSSLayout();
public int lineIndex = 0;
/*package*/ CSSNode nextChild;
CSSNode nextChild;
private @Nullable ArrayList<CSSNode> mChildren;
private @Nullable CSSNode mParent;
@@ -74,15 +64,23 @@ public class CSSNode {
private LayoutState mLayoutState = LayoutState.DIRTY;
private boolean mIsTextNode = false;
@Override
public void init() {
reset();
}
@Override
public int getChildCount() {
return mChildren == null ? 0 : mChildren.size();
}
@Override
public CSSNode getChildAt(int i) {
Assertions.assertNotNull(mChildren);
return mChildren.get(i);
}
@Override
public void addChildAt(CSSNode child, int i) {
if (child.mParent != null) {
throw new IllegalStateException("Child already has a parent, it must be removed first.");
@@ -97,6 +95,7 @@ public class CSSNode {
dirty();
}
@Override
public CSSNode removeChildAt(int i) {
Assertions.assertNotNull(mChildren);
CSSNode removed = mChildren.remove(i);
@@ -105,6 +104,7 @@ public class CSSNode {
return removed;
}
@Override
public @Nullable CSSNode getParent() {
return mParent;
}
@@ -112,11 +112,13 @@ public class CSSNode {
/**
* @return the index of the given child, or -1 if the child doesn't exist in this node.
*/
@Override
public int indexOf(CSSNode child) {
Assertions.assertNotNull(mChildren);
return mChildren.indexOf(child);
}
@Override
public void setMeasureFunction(MeasureFunction measureFunction) {
if (mMeasureFunction != measureFunction) {
mMeasureFunction = measureFunction;
@@ -124,19 +126,22 @@ public class CSSNode {
}
}
@Override
public boolean isMeasureDefined() {
return mMeasureFunction != null;
}
@Override
public void setIsTextNode(boolean isTextNode) {
mIsTextNode = isTextNode;
}
@Override
public boolean isTextNode() {
return mIsTextNode;
}
/*package*/ MeasureOutput measure(MeasureOutput measureOutput, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) {
MeasureOutput measure(MeasureOutput measureOutput, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) {
if (!isMeasureDefined()) {
throw new RuntimeException("Measure function isn't defined!");
}
@@ -149,6 +154,7 @@ public class CSSNode {
/**
* Performs the actual layout and saves the results in {@link #layout}
*/
@Override
public void calculateLayout(CSSLayoutContext layoutContext) {
LayoutEngine.layoutNode(layoutContext, this, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED, null);
}
@@ -156,18 +162,21 @@ public class CSSNode {
/**
* See {@link LayoutState#DIRTY}.
*/
protected boolean isDirty() {
@Override
public boolean isDirty() {
return mLayoutState == LayoutState.DIRTY;
}
/**
* See {@link LayoutState#HAS_NEW_LAYOUT}.
*/
@Override
public boolean hasNewLayout() {
return mLayoutState == LayoutState.HAS_NEW_LAYOUT;
}
protected void dirty() {
@Override
public void dirty() {
if (mLayoutState == LayoutState.DIRTY) {
return;
} else if (mLayoutState == LayoutState.HAS_NEW_LAYOUT) {
@@ -181,7 +190,7 @@ public class CSSNode {
}
}
/*package*/ void markHasNewLayout() {
void markHasNewLayout() {
mLayoutState = LayoutState.HAS_NEW_LAYOUT;
}
@@ -190,6 +199,7 @@ public class CSSNode {
* to {@link #hasNewLayout()} will return false until this node is laid out with new parameters.
* You must call this each time the layout is generated if the node has a new layout.
*/
@Override
public void markLayoutSeen() {
if (!hasNewLayout()) {
throw new IllegalStateException("Expected node to have a new layout to be seen!");
@@ -227,17 +237,20 @@ public class CSSNode {
return sb.toString();
}
protected boolean valuesEqual(float f1, float f2) {
@Override
public boolean valuesEqual(float f1, float f2) {
return FloatUtil.floatsEqual(f1, f2);
}
/**
* Get this node's direction, as defined in the style.
*/
@Override
public CSSDirection getStyleDirection() {
return style.direction;
}
@Override
public void setDirection(CSSDirection direction) {
if (style.direction != direction) {
style.direction = direction;
@@ -248,10 +261,12 @@ public class CSSNode {
/**
* Get this node's flex direction, as defined by style.
*/
@Override
public CSSFlexDirection getFlexDirection() {
return style.flexDirection;
}
@Override
public void setFlexDirection(CSSFlexDirection flexDirection) {
if (style.flexDirection != flexDirection) {
style.flexDirection = flexDirection;
@@ -262,10 +277,12 @@ public class CSSNode {
/**
* Get this node's justify content, as defined by style.
*/
@Override
public CSSJustify getJustifyContent() {
return style.justifyContent;
}
@Override
public void setJustifyContent(CSSJustify justifyContent) {
if (style.justifyContent != justifyContent) {
style.justifyContent = justifyContent;
@@ -276,10 +293,12 @@ public class CSSNode {
/**
* Get this node's align items, as defined by style.
*/
@Override
public CSSAlign getAlignItems() {
return style.alignItems;
}
@Override
public void setAlignItems(CSSAlign alignItems) {
if (style.alignItems != alignItems) {
style.alignItems = alignItems;
@@ -290,10 +309,12 @@ public class CSSNode {
/**
* Get this node's align items, as defined by style.
*/
@Override
public CSSAlign getAlignSelf() {
return style.alignSelf;
}
@Override
public void setAlignSelf(CSSAlign alignSelf) {
if (style.alignSelf != alignSelf) {
style.alignSelf = alignSelf;
@@ -304,10 +325,12 @@ public class CSSNode {
/**
* Get this node's position type, as defined by style.
*/
@Override
public CSSPositionType getPositionType() {
return style.positionType;
}
@Override
public void setPositionType(CSSPositionType positionType) {
if (style.positionType != positionType) {
style.positionType = positionType;
@@ -315,6 +338,7 @@ public class CSSNode {
}
}
@Override
public void setWrap(CSSWrap flexWrap) {
if (style.flexWrap != flexWrap) {
style.flexWrap = flexWrap;
@@ -325,10 +349,12 @@ public class CSSNode {
/**
* Get this node's flex, as defined by style.
*/
@Override
public float getFlex() {
return style.flex;
}
@Override
public void setFlex(float flex) {
if (!valuesEqual(style.flex, flex)) {
style.flex = flex;
@@ -339,10 +365,12 @@ public class CSSNode {
/**
* Get this node's margin, as defined by style + default margin.
*/
@Override
public Spacing getMargin() {
return style.margin;
}
@Override
public void setMargin(int spacingType, float margin) {
if (style.margin.set(spacingType, margin)) {
dirty();
@@ -352,10 +380,12 @@ public class CSSNode {
/**
* Get this node's padding, as defined by style + default padding.
*/
@Override
public Spacing getPadding() {
return style.padding;
}
@Override
public void setPadding(int spacingType, float padding) {
if (style.padding.set(spacingType, padding)) {
dirty();
@@ -365,10 +395,12 @@ public class CSSNode {
/**
* Get this node's border, as defined by style.
*/
@Override
public Spacing getBorder() {
return style.border;
}
@Override
public void setBorder(int spacingType, float border) {
if (style.border.set(spacingType, border)) {
dirty();
@@ -378,10 +410,12 @@ public class CSSNode {
/**
* Get this node's position, as defined by style.
*/
public Spacing getPositionValue() {
@Override
public Spacing getPositionValue() {
return style.position;
}
@Override
public void setPositionValue(int spacingType, float position) {
if (style.position.set(spacingType, position)) {
dirty();
@@ -391,10 +425,12 @@ public class CSSNode {
/**
* Get this node's position top, as defined by style.
*/
@Override
public float getPositionTop() {
return style.position.get(TOP);
}
@Override
public void setPositionTop(float positionTop) {
setPositionValue(TOP, positionTop);
}
@@ -402,10 +438,12 @@ public class CSSNode {
/**
* Get this node's position bottom, as defined by style.
*/
@Override
public float getPositionBottom() {
return style.position.get(BOTTOM);
}
@Override
public void setPositionBottom(float positionBottom) {
setPositionValue(BOTTOM, positionBottom);
}
@@ -413,10 +451,12 @@ public class CSSNode {
/**
* Get this node's position left, as defined by style.
*/
@Override
public float getPositionLeft() {
return style.position.get(LEFT);
}
@Override
public void setPositionLeft(float positionLeft) {
setPositionValue(LEFT, positionLeft);
}
@@ -424,10 +464,12 @@ public class CSSNode {
/**
* Get this node's position right, as defined by style.
*/
@Override
public float getPositionRight() {
return style.position.get(RIGHT);
}
@Override
public void setPositionRight(float positionRight) {
setPositionValue(RIGHT, positionRight);
}
@@ -435,10 +477,12 @@ public class CSSNode {
/**
* Get this node's width, as defined in the style.
*/
@Override
public float getStyleWidth() {
return style.dimensions[DIMENSION_WIDTH];
}
@Override
public void setStyleWidth(float width) {
if (!valuesEqual(style.dimensions[DIMENSION_WIDTH], width)) {
style.dimensions[DIMENSION_WIDTH] = width;
@@ -449,10 +493,12 @@ public class CSSNode {
/**
* Get this node's height, as defined in the style.
*/
@Override
public float getStyleHeight() {
return style.dimensions[DIMENSION_HEIGHT];
}
@Override
public void setStyleHeight(float height) {
if (!valuesEqual(style.dimensions[DIMENSION_HEIGHT], height)) {
style.dimensions[DIMENSION_HEIGHT] = height;
@@ -463,10 +509,12 @@ public class CSSNode {
/**
* Get this node's max width, as defined in the style
*/
@Override
public float getStyleMaxWidth() {
return style.maxWidth;
}
@Override
public void setStyleMaxWidth(float maxWidth) {
if (!valuesEqual(style.maxWidth, maxWidth)) {
style.maxWidth = maxWidth;
@@ -477,10 +525,12 @@ public class CSSNode {
/**
* Get this node's min width, as defined in the style
*/
@Override
public float getStyleMinWidth() {
return style.minWidth;
}
@Override
public void setStyleMinWidth(float minWidth) {
if (!valuesEqual(style.minWidth, minWidth)) {
style.minWidth = minWidth;
@@ -491,10 +541,12 @@ public class CSSNode {
/**
* Get this node's max height, as defined in the style
*/
@Override
public float getStyleMaxHeight() {
return style.maxHeight;
}
@Override
public void setStyleMaxHeight(float maxHeight) {
if (!valuesEqual(style.maxHeight, maxHeight)) {
style.maxHeight = maxHeight;
@@ -505,10 +557,12 @@ public class CSSNode {
/**
* Get this node's min height, as defined in the style
*/
@Override
public float getStyleMinHeight() {
return style.minHeight;
}
@Override
public void setStyleMinHeight(float minHeight) {
if (!valuesEqual(style.minHeight, minHeight)) {
style.minHeight = minHeight;
@@ -516,22 +570,27 @@ public class CSSNode {
}
}
@Override
public float getLayoutX() {
return layout.position[POSITION_LEFT];
}
@Override
public float getLayoutY() {
return layout.position[POSITION_TOP];
}
@Override
public float getLayoutWidth() {
return layout.dimensions[DIMENSION_WIDTH];
}
@Override
public float getLayoutHeight() {
return layout.dimensions[DIMENSION_HEIGHT];
}
@Override
public CSSDirection getLayoutDirection() {
return layout.direction;
}
@@ -539,6 +598,7 @@ public class CSSNode {
/**
* Set a default padding (left/top/right/bottom) for this node.
*/
@Override
public void setDefaultPadding(int spacingType, float padding) {
if (style.padding.setDefault(spacingType, padding)) {
dirty();
@@ -548,10 +608,12 @@ public class CSSNode {
/**
* Get this node's overflow property, as defined in the style
*/
@Override
public CSSOverflow getOverflow() {
return style.overflow;
}
@Override
public void setOverflow(CSSOverflow overflow) {
if (style.overflow != overflow) {
style.overflow = overflow;
@@ -563,6 +625,7 @@ public class CSSNode {
* Resets this instance to its default state. This method is meant to be used when
* recycling {@link CSSNode} instances.
*/
@Override
public void reset() {
if (mParent != null || (mChildren != null && mChildren.size() > 0)) {
throw new IllegalStateException("You should not reset an attached CSSNode");