diff --git a/java/com/facebook/yoga/YogaNode.java b/java/com/facebook/yoga/YogaNode.java index 42a5bd8d..1c75d1d6 100644 --- a/java/com/facebook/yoga/YogaNode.java +++ b/java/com/facebook/yoga/YogaNode.java @@ -10,11 +10,11 @@ import javax.annotation.Nullable; public abstract class YogaNode { public static YogaNode create() { - return new YogaNodeJNIBatching(); + return new YogaNodeJNIBase(); } public static YogaNode create(YogaConfig config) { - return new YogaNodeJNIBatching(config); + return new YogaNodeJNIBase(config); } public abstract void reset(); diff --git a/java/com/facebook/yoga/YogaNodeJNI.java b/java/com/facebook/yoga/YogaNodeJNI.java deleted file mode 100644 index d2a9357b..00000000 --- a/java/com/facebook/yoga/YogaNodeJNI.java +++ /dev/null @@ -1,186 +0,0 @@ -/** - * 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 YogaNodeJNI extends YogaNodeJNIBase { - - @DoNotStrip - private float mWidth = YogaConstants.UNDEFINED; - @DoNotStrip - private float mHeight = YogaConstants.UNDEFINED; - @DoNotStrip - private float mTop = YogaConstants.UNDEFINED; - @DoNotStrip - private float mLeft = YogaConstants.UNDEFINED; - @DoNotStrip - private float mMarginLeft = 0; - @DoNotStrip - private float mMarginTop = 0; - @DoNotStrip - private float mMarginRight = 0; - @DoNotStrip - private float mMarginBottom = 0; - @DoNotStrip - private float mPaddingLeft = 0; - @DoNotStrip - private float mPaddingTop = 0; - @DoNotStrip - private float mPaddingRight = 0; - @DoNotStrip - private float mPaddingBottom = 0; - @DoNotStrip - private float mBorderLeft = 0; - @DoNotStrip - private float mBorderTop = 0; - @DoNotStrip - private float mBorderRight = 0; - @DoNotStrip - private float mBorderBottom = 0; - @DoNotStrip - private int mLayoutDirection = 0; - @DoNotStrip - private boolean mHasNewLayout = true; - @DoNotStrip - private boolean mDoesLegacyStretchFlagAffectsLayout = false; - - public YogaNodeJNI() { - super(); - } - - public YogaNodeJNI(YogaConfig config) { - super(config); - } - - @Override - public void reset() { - super.reset(); - mHasNewLayout = true; - - mWidth = YogaConstants.UNDEFINED; - mHeight = YogaConstants.UNDEFINED; - mTop = YogaConstants.UNDEFINED; - mLeft = YogaConstants.UNDEFINED; - mMarginLeft = 0; - mMarginTop = 0; - mMarginRight = 0; - mMarginBottom = 0; - mPaddingLeft = 0; - mPaddingTop = 0; - mPaddingRight = 0; - mPaddingBottom = 0; - mBorderLeft = 0; - mBorderTop = 0; - mBorderRight = 0; - mBorderBottom = 0; - mLayoutDirection = 0; - - mDoesLegacyStretchFlagAffectsLayout = false; - } - - @Override - public boolean hasNewLayout() { - return mHasNewLayout; - } - - @Override - public void markLayoutSeen() { - mHasNewLayout = false; - } - - @Override - public float getLayoutX() { - return mLeft; - } - - @Override - public float getLayoutY() { - return mTop; - } - - @Override - public float getLayoutWidth() { - return mWidth; - } - - @Override - public float getLayoutHeight() { - return mHeight; - } - - @Override - public boolean getDoesLegacyStretchFlagAffectsLayout() { - return mDoesLegacyStretchFlagAffectsLayout; - } - - @Override - public float getLayoutMargin(YogaEdge edge) { - switch (edge) { - case LEFT: - return mMarginLeft; - case TOP: - return mMarginTop; - case RIGHT: - return mMarginRight; - case BOTTOM: - return mMarginBottom; - case START: - return getLayoutDirection() == YogaDirection.RTL ? mMarginRight : mMarginLeft; - case END: - return getLayoutDirection() == YogaDirection.RTL ? mMarginLeft : mMarginRight; - default: - throw new IllegalArgumentException("Cannot get layout margins of multi-edge shorthands"); - } - } - - @Override - public float getLayoutPadding(YogaEdge edge) { - switch (edge) { - case LEFT: - return mPaddingLeft; - case TOP: - return mPaddingTop; - case RIGHT: - return mPaddingRight; - case BOTTOM: - return mPaddingBottom; - case START: - return getLayoutDirection() == YogaDirection.RTL ? mPaddingRight : mPaddingLeft; - case END: - return getLayoutDirection() == YogaDirection.RTL ? mPaddingLeft : mPaddingRight; - default: - throw new IllegalArgumentException("Cannot get layout paddings of multi-edge shorthands"); - } - } - - @Override - public float getLayoutBorder(YogaEdge edge) { - switch (edge) { - case LEFT: - return mBorderLeft; - case TOP: - return mBorderTop; - case RIGHT: - return mBorderRight; - case BOTTOM: - return mBorderBottom; - case START: - return getLayoutDirection() == YogaDirection.RTL ? mBorderRight : mBorderLeft; - case END: - return getLayoutDirection() == YogaDirection.RTL ? mBorderLeft : mBorderRight; - default: - throw new IllegalArgumentException("Cannot get layout border of multi-edge shorthands"); - } - } - - @Override - public YogaDirection getLayoutDirection() { - return YogaDirection.fromInt(mLayoutDirection); - } -} diff --git a/java/com/facebook/yoga/YogaNodeJNIBase.java b/java/com/facebook/yoga/YogaNodeJNIBase.java index 0d9500ce..3fc4c0ad 100644 --- a/java/com/facebook/yoga/YogaNodeJNIBase.java +++ b/java/com/facebook/yoga/YogaNodeJNIBase.java @@ -12,7 +12,24 @@ import java.util.List; import javax.annotation.Nullable; @DoNotStrip -public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { +public class YogaNodeJNIBase extends YogaNode implements Cloneable { + + /* Those flags needs be in sync with YGJNI.cpp */ + private static final byte MARGIN = 1; + private static final byte PADDING = 2; + private static final byte BORDER = 4; + private static final byte DOES_LEGACY_STRETCH_BEHAVIOUR = 8; + private static final byte HAS_NEW_LAYOUT = 16; + + private static final byte LAYOUT_EDGE_SET_FLAG_INDEX = 0; + private static final byte LAYOUT_WIDTH_INDEX = 1; + private static final byte LAYOUT_HEIGHT_INDEX = 2; + private static final byte LAYOUT_LEFT_INDEX = 3; + private static final byte LAYOUT_TOP_INDEX = 4; + private static final byte LAYOUT_DIRECTION_INDEX = 5; + private static final byte LAYOUT_MARGIN_START_INDEX = 6; + private static final byte LAYOUT_PADDING_START_INDEX = 10; + private static final byte LAYOUT_BORDER_START_INDEX = 14; @Nullable private YogaNodeJNIBase mOwner; @Nullable private List mChildren; @@ -21,6 +38,14 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { private long mNativePointer; @Nullable private Object mData; + @DoNotStrip + private @Nullable float[] arr = null; + + @DoNotStrip + private int mLayoutDirection = 0; + + private boolean mHasNewLayout = true; + public YogaNodeJNIBase() { mNativePointer = YogaNative.jni_YGNodeNew(); if (mNativePointer == 0) { @@ -56,6 +81,9 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { mMeasureFunction = null; mBaselineFunction = null; mData = null; + arr = null; + mHasNewLayout = true; + mLayoutDirection = 0; YogaNative.jni_YGNodeReset(mNativePointer); } @@ -443,8 +471,6 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { YogaNative.jni_YGNodeStyleSetAspectRatio(mNativePointer, aspectRatio); } - public abstract boolean getDoesLegacyStretchFlagAffectsLayout(); - public void setMeasureFunction(YogaMeasureFunction measureFunction) { mMeasureFunction = measureFunction; YogaNative.jni_YGNodeSetHasMeasureFunc(mNativePointer, measureFunction != null); @@ -530,4 +556,124 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { private static YogaValue valueFromLong(long raw) { return new YogaValue(Float.intBitsToFloat((int) raw), (int) (raw >> 32)); } + + @Override + public float getLayoutX() { + return arr != null ? arr[LAYOUT_LEFT_INDEX] : 0; + } + + @Override + public float getLayoutY() { + return arr != null ? arr[LAYOUT_TOP_INDEX] : 0; + } + + @Override + public float getLayoutWidth() { + return arr != null ? arr[LAYOUT_WIDTH_INDEX] : 0; + } + + @Override + public float getLayoutHeight() { + return arr != null ? arr[LAYOUT_HEIGHT_INDEX] : 0; + } + + public boolean getDoesLegacyStretchFlagAffectsLayout() { + return arr != null && (((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & DOES_LEGACY_STRETCH_BEHAVIOUR) == DOES_LEGACY_STRETCH_BEHAVIOUR); + } + + @Override + public float getLayoutMargin(YogaEdge edge) { + if (arr != null && ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & MARGIN) == MARGIN) { + switch (edge) { + case LEFT: + return arr[LAYOUT_MARGIN_START_INDEX]; + case TOP: + return arr[LAYOUT_MARGIN_START_INDEX + 1]; + case RIGHT: + return arr[LAYOUT_MARGIN_START_INDEX + 2]; + case BOTTOM: + return arr[LAYOUT_MARGIN_START_INDEX + 3]; + case START: + return getLayoutDirection() == YogaDirection.RTL ? arr[LAYOUT_MARGIN_START_INDEX + 2] : arr[LAYOUT_MARGIN_START_INDEX]; + case END: + return getLayoutDirection() == YogaDirection.RTL ? arr[LAYOUT_MARGIN_START_INDEX] : arr[LAYOUT_MARGIN_START_INDEX + 2]; + default: + throw new IllegalArgumentException("Cannot get layout margins of multi-edge shorthands"); + } + } else { + return 0; + } + } + + @Override + public float getLayoutPadding(YogaEdge edge) { + if (arr != null && ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & PADDING) == PADDING) { + int paddingStartIndex = LAYOUT_PADDING_START_INDEX - ((((int)arr[LAYOUT_EDGE_SET_FLAG_INDEX] & MARGIN) == MARGIN) ? 0 : 4); + switch (edge) { + case LEFT: + return arr[paddingStartIndex]; + case TOP: + return arr[paddingStartIndex + 1]; + case RIGHT: + return arr[paddingStartIndex + 2]; + case BOTTOM: + return arr[paddingStartIndex + 3]; + case START: + return getLayoutDirection() == YogaDirection.RTL ? arr[paddingStartIndex + 2] : arr[paddingStartIndex]; + case END: + return getLayoutDirection() == YogaDirection.RTL ? arr[paddingStartIndex] : arr[paddingStartIndex + 2]; + default: + throw new IllegalArgumentException("Cannot get layout paddings of multi-edge shorthands"); + } + } else { + return 0; + } + } + + @Override + public float getLayoutBorder(YogaEdge edge) { + if (arr != null && ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & BORDER) == BORDER) { + int borderStartIndex = LAYOUT_BORDER_START_INDEX - ((((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & MARGIN) == MARGIN) ? 0 : 4) - ((((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & PADDING) == PADDING) ? 0 : 4); + switch (edge) { + case LEFT: + return arr[borderStartIndex]; + case TOP: + return arr[borderStartIndex + 1]; + case RIGHT: + return arr[borderStartIndex + 2]; + case BOTTOM: + return arr[borderStartIndex + 3]; + case START: + return getLayoutDirection() == YogaDirection.RTL ? arr[borderStartIndex + 2] : arr[borderStartIndex]; + case END: + return getLayoutDirection() == YogaDirection.RTL ? arr[borderStartIndex] : arr[borderStartIndex + 2]; + default: + throw new IllegalArgumentException("Cannot get layout border of multi-edge shorthands"); + } + } else { + return 0; + } + } + + @Override + public YogaDirection getLayoutDirection() { + return YogaDirection.fromInt(arr != null ? (int) arr[LAYOUT_DIRECTION_INDEX] : mLayoutDirection); + } + + @Override + public boolean hasNewLayout() { + if (arr != null) { + return (((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX]) & HAS_NEW_LAYOUT) == HAS_NEW_LAYOUT; + } else { + return mHasNewLayout; + } + } + + @Override + public void markLayoutSeen() { + if (arr != null) { + arr[LAYOUT_EDGE_SET_FLAG_INDEX] = ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX]) & ~(HAS_NEW_LAYOUT); + } + mHasNewLayout = false; + } } diff --git a/java/com/facebook/yoga/YogaNodeJNIBatching.java b/java/com/facebook/yoga/YogaNodeJNIBatching.java deleted file mode 100644 index 7760fa80..00000000 --- a/java/com/facebook/yoga/YogaNodeJNIBatching.java +++ /dev/null @@ -1,177 +0,0 @@ -/** - * 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 javax.annotation.Nullable; - -import com.facebook.proguard.annotations.DoNotStrip; - -@DoNotStrip -public class YogaNodeJNIBatching extends YogaNodeJNIBase { - - /* Those flags needs be in sync with YGJNI.cpp */ - private static final byte MARGIN = 1; - private static final byte PADDING = 2; - private static final byte BORDER = 4; - private static final byte DOES_LEGACY_STRETCH_BEHAVIOUR = 8; - private static final byte HAS_NEW_LAYOUT = 16; - - private static final byte LAYOUT_EDGE_SET_FLAG_INDEX = 0; - private static final byte LAYOUT_WIDTH_INDEX = 1; - private static final byte LAYOUT_HEIGHT_INDEX = 2; - private static final byte LAYOUT_LEFT_INDEX = 3; - private static final byte LAYOUT_TOP_INDEX = 4; - private static final byte LAYOUT_DIRECTION_INDEX = 5; - private static final byte LAYOUT_MARGIN_START_INDEX = 6; - private static final byte LAYOUT_PADDING_START_INDEX = 10; - private static final byte LAYOUT_BORDER_START_INDEX = 14; - - @DoNotStrip - private @Nullable float[] arr = null; - - @DoNotStrip - private int mLayoutDirection = 0; - - private boolean mHasNewLayout = true; - - public YogaNodeJNIBatching() { - super(); - } - - public YogaNodeJNIBatching(YogaConfig config) { - super(config); - } - - @Override - public void reset() { - super.reset(); - arr = null; - mHasNewLayout = true; - mLayoutDirection = 0; - } - - @Override - public float getLayoutX() { - return arr != null ? arr[LAYOUT_LEFT_INDEX] : 0; - } - - @Override - public float getLayoutY() { - return arr != null ? arr[LAYOUT_TOP_INDEX] : 0; - } - - @Override - public float getLayoutWidth() { - return arr != null ? arr[LAYOUT_WIDTH_INDEX] : 0; - } - - @Override - public float getLayoutHeight() { - return arr != null ? arr[LAYOUT_HEIGHT_INDEX] : 0; - } - - @Override - public boolean getDoesLegacyStretchFlagAffectsLayout() { - return arr != null && (((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & DOES_LEGACY_STRETCH_BEHAVIOUR) == DOES_LEGACY_STRETCH_BEHAVIOUR); - } - - @Override - public float getLayoutMargin(YogaEdge edge) { - if (arr != null && ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & MARGIN) == MARGIN) { - switch (edge) { - case LEFT: - return arr[LAYOUT_MARGIN_START_INDEX]; - case TOP: - return arr[LAYOUT_MARGIN_START_INDEX + 1]; - case RIGHT: - return arr[LAYOUT_MARGIN_START_INDEX + 2]; - case BOTTOM: - return arr[LAYOUT_MARGIN_START_INDEX + 3]; - case START: - return getLayoutDirection() == YogaDirection.RTL ? arr[LAYOUT_MARGIN_START_INDEX + 2] : arr[LAYOUT_MARGIN_START_INDEX]; - case END: - return getLayoutDirection() == YogaDirection.RTL ? arr[LAYOUT_MARGIN_START_INDEX] : arr[LAYOUT_MARGIN_START_INDEX + 2]; - default: - throw new IllegalArgumentException("Cannot get layout margins of multi-edge shorthands"); - } - } else { - return 0; - } - } - - @Override - public float getLayoutPadding(YogaEdge edge) { - if (arr != null && ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & PADDING) == PADDING) { - int paddingStartIndex = LAYOUT_PADDING_START_INDEX - ((((int)arr[LAYOUT_EDGE_SET_FLAG_INDEX] & MARGIN) == MARGIN) ? 0 : 4); - switch (edge) { - case LEFT: - return arr[paddingStartIndex]; - case TOP: - return arr[paddingStartIndex + 1]; - case RIGHT: - return arr[paddingStartIndex + 2]; - case BOTTOM: - return arr[paddingStartIndex + 3]; - case START: - return getLayoutDirection() == YogaDirection.RTL ? arr[paddingStartIndex + 2] : arr[paddingStartIndex]; - case END: - return getLayoutDirection() == YogaDirection.RTL ? arr[paddingStartIndex] : arr[paddingStartIndex + 2]; - default: - throw new IllegalArgumentException("Cannot get layout paddings of multi-edge shorthands"); - } - } else { - return 0; - } - } - - @Override - public float getLayoutBorder(YogaEdge edge) { - if (arr != null && ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & BORDER) == BORDER) { - int borderStartIndex = LAYOUT_BORDER_START_INDEX - ((((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & MARGIN) == MARGIN) ? 0 : 4) - ((((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX] & PADDING) == PADDING) ? 0 : 4); - switch (edge) { - case LEFT: - return arr[borderStartIndex]; - case TOP: - return arr[borderStartIndex + 1]; - case RIGHT: - return arr[borderStartIndex + 2]; - case BOTTOM: - return arr[borderStartIndex + 3]; - case START: - return getLayoutDirection() == YogaDirection.RTL ? arr[borderStartIndex + 2] : arr[borderStartIndex]; - case END: - return getLayoutDirection() == YogaDirection.RTL ? arr[borderStartIndex] : arr[borderStartIndex + 2]; - default: - throw new IllegalArgumentException("Cannot get layout border of multi-edge shorthands"); - } - } else { - return 0; - } - } - - @Override - public YogaDirection getLayoutDirection() { - return YogaDirection.fromInt(arr != null ? (int) arr[LAYOUT_DIRECTION_INDEX] : mLayoutDirection); - } - - @Override - public boolean hasNewLayout() { - if (arr != null) { - return (((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX]) & HAS_NEW_LAYOUT) == HAS_NEW_LAYOUT; - } else { - return mHasNewLayout; - } - } - - @Override - public void markLayoutSeen() { - if (arr != null) { - arr[LAYOUT_EDGE_SET_FLAG_INDEX] = ((int) arr[LAYOUT_EDGE_SET_FLAG_INDEX]) & ~(HAS_NEW_LAYOUT); - } - mHasNewLayout = false; - } -}