Add feature to use percentage as value unit

Summary:
Adds the feature to use percentage as a value unit.

You can use the function ```YGPx(float)``` and ```YGPercent(float)``` for convenience.

I did some benchmarks:

```
Without Percentage Feature - Release x86:

Stack with flex: median: 0.000000 ms, stddev: 0.146683 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.490101 ms
Huge nested layout: median: 23.000000 ms, stddev: 0.928291 ms

Stack with flex: median: 0.000000 ms, stddev: 0.170587 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.143384 ms
Nested flex: median: 0.000000 ms, stddev: 0.477791 ms
Huge nested layout: median: 22.000000 ms, stddev: 2.129779 ms

With Percentage Feature - Release x86:

Stack with flex: median: 0.000000 ms, stddev: 0.132951 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.489570 ms
Huge nested layout: median: 21.000000 ms, stddev: 1.390476 ms
Closes https://github.com/facebook/yoga/pull/258

Reviewed By: dshahidehpour

Differential Revision: D4361945

Pulled By: emilsjolander

fbshipit-source-id: a8f5bc63ad352eb9410d792729e56664468cd76a
This commit is contained in:
Lukas Woehrl
2017-01-02 05:20:37 -08:00
committed by Facebook Github Bot
parent 6f462a72bf
commit a85bd4ad2a
48 changed files with 4948 additions and 1209 deletions

View File

@@ -16,4 +16,8 @@ public class YogaConstants {
public static boolean isUndefined(float value) {
return Float.compare(value, UNDEFINED) == 0;
}
public static boolean isUndefined(YogaValue value) {
return value.unit == YogaUnit.UNDEFINED;
}
}

View File

@@ -323,10 +323,10 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetFlexShrink(mNativePointer, flexShrink);
}
private native float jni_YGNodeStyleGetFlexBasis(long nativePointer);
private native Object jni_YGNodeStyleGetFlexBasis(long nativePointer);
@Override
public float getFlexBasis() {
return jni_YGNodeStyleGetFlexBasis(mNativePointer);
public YogaValue getFlexBasis() {
return (YogaValue) jni_YGNodeStyleGetFlexBasis(mNativePointer);
}
private native void jni_YGNodeStyleSetFlexBasis(long nativePointer, float flexBasis);
@@ -335,13 +335,19 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetFlexBasis(mNativePointer, flexBasis);
}
private native float jni_YGNodeStyleGetMargin(long nativePointer, int edge);
private native void jni_YGNodeStyleSetFlexBasisPercent(long nativePointer, float percent);
@Override
public float getMargin(YogaEdge edge) {
public void setFlexBasisPercent(float percent) {
jni_YGNodeStyleSetFlexBasisPercent(mNativePointer, percent);
}
private native Object jni_YGNodeStyleGetMargin(long nativePointer, int edge);
@Override
public YogaValue getMargin(YogaEdge edge) {
if (!mHasSetMargin) {
return edge.intValue() < YogaEdge.START.intValue() ? 0 : YogaConstants.UNDEFINED;
return edge.intValue() < YogaEdge.START.intValue() ? YogaValue.ZERO : YogaValue.UNDEFINED;
}
return jni_YGNodeStyleGetMargin(mNativePointer, edge.intValue());
return (YogaValue) jni_YGNodeStyleGetMargin(mNativePointer, edge.intValue());
}
private native void jni_YGNodeStyleSetMargin(long nativePointer, int edge, float margin);
@@ -351,13 +357,20 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetMargin(mNativePointer, edge.intValue(), margin);
}
private native float jni_YGNodeStyleGetPadding(long nativePointer, int edge);
private native void jni_YGNodeStyleSetMarginPercent(long nativePointer, int edge, float percent);
@Override
public float getPadding(YogaEdge edge) {
public void setMarginPercent(YogaEdge edge, float percent) {
mHasSetMargin = true;
jni_YGNodeStyleSetMarginPercent(mNativePointer, edge.intValue(), percent);
}
private native Object jni_YGNodeStyleGetPadding(long nativePointer, int edge);
@Override
public YogaValue getPadding(YogaEdge edge) {
if (!mHasSetPadding) {
return edge.intValue() < YogaEdge.START.intValue() ? 0 : YogaConstants.UNDEFINED;
return edge.intValue() < YogaEdge.START.intValue() ? YogaValue.ZERO : YogaValue.UNDEFINED;
}
return jni_YGNodeStyleGetPadding(mNativePointer, edge.intValue());
return (YogaValue) jni_YGNodeStyleGetPadding(mNativePointer, edge.intValue());
}
private native void jni_YGNodeStyleSetPadding(long nativePointer, int edge, float padding);
@@ -367,6 +380,13 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetPadding(mNativePointer, edge.intValue(), padding);
}
private native void jni_YGNodeStyleSetPaddingPercent(long nativePointer, int edge, float percent);
@Override
public void setPaddingPercent(YogaEdge edge, float percent) {
mHasSetPadding = true;
jni_YGNodeStyleSetPaddingPercent(mNativePointer, edge.intValue(), percent);
}
private native float jni_YGNodeStyleGetBorder(long nativePointer, int edge);
@Override
public float getBorder(YogaEdge edge) {
@@ -383,13 +403,13 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetBorder(mNativePointer, edge.intValue(), border);
}
private native float jni_YGNodeStyleGetPosition(long nativePointer, int edge);
private native Object jni_YGNodeStyleGetPosition(long nativePointer, int edge);
@Override
public float getPosition(YogaEdge edge) {
public YogaValue getPosition(YogaEdge edge) {
if (!mHasSetPosition) {
return YogaConstants.UNDEFINED;
return YogaValue.UNDEFINED;
}
return jni_YGNodeStyleGetPosition(mNativePointer, edge.intValue());
return (YogaValue) jni_YGNodeStyleGetPosition(mNativePointer, edge.intValue());
}
private native void jni_YGNodeStyleSetPosition(long nativePointer, int edge, float position);
@@ -399,10 +419,17 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetPosition(mNativePointer, edge.intValue(), position);
}
private native float jni_YGNodeStyleGetWidth(long nativePointer);
private native void jni_YGNodeStyleSetPositionPercent(long nativePointer, int edge, float percent);
@Override
public float getWidth() {
return jni_YGNodeStyleGetWidth(mNativePointer);
public void setPositionPercent(YogaEdge edge, float percent) {
mHasSetPosition = true;
jni_YGNodeStyleSetPositionPercent(mNativePointer, edge.intValue(), percent);
}
private native Object jni_YGNodeStyleGetWidth(long nativePointer);
@Override
public YogaValue getWidth() {
return (YogaValue) jni_YGNodeStyleGetWidth(mNativePointer);
}
private native void jni_YGNodeStyleSetWidth(long nativePointer, float width);
@@ -411,10 +438,16 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetWidth(mNativePointer, width);
}
private native float jni_YGNodeStyleGetHeight(long nativePointer);
private native void jni_YGNodeStyleSetWidthPercent(long nativePointer, float percent);
@Override
public float getHeight() {
return jni_YGNodeStyleGetHeight(mNativePointer);
public void setWidthPercent(float percent) {
jni_YGNodeStyleSetWidthPercent(mNativePointer, percent);
}
private native Object jni_YGNodeStyleGetHeight(long nativePointer);
@Override
public YogaValue getHeight() {
return (YogaValue) jni_YGNodeStyleGetHeight(mNativePointer);
}
private native void jni_YGNodeStyleSetHeight(long nativePointer, float height);
@@ -423,10 +456,16 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetHeight(mNativePointer, height);
}
private native float jni_YGNodeStyleGetMinWidth(long nativePointer);
private native void jni_YGNodeStyleSetHeightPercent(long nativePointer, float percent);
@Override
public float getMinWidth() {
return jni_YGNodeStyleGetMinWidth(mNativePointer);
public void setHeightPercent(float percent) {
jni_YGNodeStyleSetHeightPercent(mNativePointer, percent);
}
private native Object jni_YGNodeStyleGetMinWidth(long nativePointer);
@Override
public YogaValue getMinWidth() {
return (YogaValue) jni_YGNodeStyleGetMinWidth(mNativePointer);
}
private native void jni_YGNodeStyleSetMinWidth(long nativePointer, float minWidth);
@@ -435,10 +474,16 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetMinWidth(mNativePointer, minWidth);
}
private native float jni_YGNodeStyleGetMinHeight(long nativePointer);
private native void jni_YGNodeStyleSetMinWidthPercent(long nativePointer, float percent);
@Override
public float getMinHeight() {
return jni_YGNodeStyleGetMinHeight(mNativePointer);
public void setMinWidthPercent(float percent) {
jni_YGNodeStyleSetMinWidthPercent(mNativePointer, percent);
}
private native Object jni_YGNodeStyleGetMinHeight(long nativePointer);
@Override
public YogaValue getMinHeight() {
return (YogaValue) jni_YGNodeStyleGetMinHeight(mNativePointer);
}
private native void jni_YGNodeStyleSetMinHeight(long nativePointer, float minHeight);
@@ -447,10 +492,16 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetMinHeight(mNativePointer, minHeight);
}
private native float jni_YGNodeStyleGetMaxWidth(long nativePointer);
private native void jni_YGNodeStyleSetMinHeightPercent(long nativePointer, float percent);
@Override
public float getMaxWidth() {
return jni_YGNodeStyleGetMaxWidth(mNativePointer);
public void setMinHeightPercent(float percent) {
jni_YGNodeStyleSetMinHeightPercent(mNativePointer, percent);
}
private native Object jni_YGNodeStyleGetMaxWidth(long nativePointer);
@Override
public YogaValue getMaxWidth() {
return (YogaValue) jni_YGNodeStyleGetMaxWidth(mNativePointer);
}
private native void jni_YGNodeStyleSetMaxWidth(long nativePointer, float maxWidth);
@@ -459,10 +510,16 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetMaxWidth(mNativePointer, maxWidth);
}
private native float jni_YGNodeStyleGetMaxHeight(long nativePointer);
private native void jni_YGNodeStyleSetMaxWidthPercent(long nativePointer, float percent);
@Override
public float getMaxHeight() {
return jni_YGNodeStyleGetMaxHeight(mNativePointer);
public void setMaxWidthPercent(float percent) {
jni_YGNodeStyleSetMaxWidthPercent(mNativePointer, percent);
}
private native Object jni_YGNodeStyleGetMaxHeight(long nativePointer);
@Override
public YogaValue getMaxHeight() {
return (YogaValue) jni_YGNodeStyleGetMaxHeight(mNativePointer);
}
private native void jni_YGNodeStyleSetMaxHeight(long nativePointer, float maxheight);
@@ -471,6 +528,12 @@ public class YogaNode implements YogaNodeAPI<YogaNode> {
jni_YGNodeStyleSetMaxHeight(mNativePointer, maxheight);
}
private native void jni_YGNodeStyleSetMaxHeightPercent(long nativePointer, float percent);
@Override
public void setMaxHeightPercent(float percent) {
jni_YGNodeStyleSetMaxHeightPercent(mNativePointer, percent);
}
private native float jni_YGNodeStyleGetAspectRatio(long nativePointer);
public float getAspectRatio() {
return jni_YGNodeStyleGetAspectRatio(mNativePointer);

View File

@@ -45,28 +45,38 @@ public interface YogaNodeAPI<YogaNodeType extends YogaNodeAPI> {
void setFlexGrow(float flexGrow);
float getFlexShrink();
void setFlexShrink(float flexShrink);
float getFlexBasis();
YogaValue getFlexBasis();
void setFlexBasis(float flexBasis);
float getMargin(YogaEdge edge);
void setFlexBasisPercent(float percent);
YogaValue getMargin(YogaEdge edge);
void setMargin(YogaEdge edge, float margin);
float getPadding(YogaEdge edge);
void setMarginPercent(YogaEdge edge, float percent);
YogaValue getPadding(YogaEdge edge);
void setPadding(YogaEdge edge, float padding);
void setPaddingPercent(YogaEdge edge, float percent);
float getBorder(YogaEdge edge);
void setBorder(YogaEdge edge, float border);
float getPosition(YogaEdge edge);
YogaValue getPosition(YogaEdge edge);
void setPosition(YogaEdge edge, float position);
float getWidth();
void setPositionPercent(YogaEdge edge, float percent);
YogaValue getWidth();
void setWidth(float width);
float getHeight();
void setWidthPercent(float percent);
YogaValue getHeight();
void setHeight(float height);
float getMaxWidth();
void setHeightPercent(float percent);
YogaValue getMaxWidth();
void setMaxWidth(float maxWidth);
float getMinWidth();
void setMaxWidthPercent(float percent);
YogaValue getMinWidth();
void setMinWidth(float minWidth);
float getMaxHeight();
void setMinWidthPercent(float percent);
YogaValue getMaxHeight();
void setMaxHeight(float maxHeight);
float getMinHeight();
void setMaxHeightPercent(float percent);
YogaValue getMinHeight();
void setMinHeight(float minHeight);
void setMinHeightPercent(float percent);
float getLayoutX();
float getLayoutY();
float getLayoutWidth();

View File

@@ -0,0 +1,38 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.yoga;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public enum YogaUnit {
UNDEFINED(0),
PIXEL(1),
PERCENT(2);
private int mIntValue;
YogaUnit(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static YogaUnit fromInt(int value) {
switch (value) {
case 0: return UNDEFINED;
case 1: return PIXEL;
case 2: return PERCENT;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
}

View File

@@ -0,0 +1,45 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.yoga;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public class YogaValue {
static final YogaValue UNDEFINED = new YogaValue(YogaConstants.UNDEFINED, YogaUnit.UNDEFINED);
static final YogaValue ZERO = new YogaValue(0, YogaUnit.PIXEL);
public final float value;
public final YogaUnit unit;
YogaValue(float value, YogaUnit unit) {
this.value = value;
this.unit = unit;
}
@DoNotStrip
YogaValue(float value, int unit) {
this(value, YogaUnit.fromInt(unit));
}
@Override
public boolean equals(Object other) {
if (other instanceof YogaValue) {
final YogaValue otherValue = (YogaValue) other;
return value == otherValue.value && unit == otherValue.unit;
}
return false;
}
@Override
public int hashCode() {
return Float.floatToIntBits(value) + unit.intValue();
}
}