BREAKING - Change measure() api to remove need for MeasureOutput allocation
Reviewed By: splhack Differential Revision: D4081037 fbshipit-source-id: 28adbcdd160cbd3f59a0fdd4b9f1200ae18678f1
This commit is contained in:
parent
bafc6ddbd1
commit
553f4371e0
|
@ -487,15 +487,12 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
|
|||
throw new RuntimeException("Measure function isn't defined!");
|
||||
}
|
||||
|
||||
MeasureOutput output = new MeasureOutput();
|
||||
mMeasureFunction.measure(
|
||||
return mMeasureFunction.measure(
|
||||
this,
|
||||
width,
|
||||
CSSMeasureMode.values()[widthMode],
|
||||
height,
|
||||
CSSMeasureMode.values()[heightMode],
|
||||
output);
|
||||
return ((long) output.width) << 32 | ((long) output.height);
|
||||
CSSMeasureMode.values()[heightMode]);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -12,13 +12,15 @@ package com.facebook.csslayout;
|
|||
public interface CSSNodeAPI<CSSNodeType extends CSSNodeAPI> {
|
||||
|
||||
interface MeasureFunction {
|
||||
void measure(
|
||||
/**
|
||||
* Return a value created by MeasureOutput.make(width, height);
|
||||
*/
|
||||
long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput);
|
||||
CSSMeasureMode heightMode);
|
||||
}
|
||||
|
||||
int getChildCount();
|
||||
|
|
|
@ -134,14 +134,11 @@ public class CSSNodeDEPRECATED implements CSSNodeAPI<CSSNodeDEPRECATED> {
|
|||
return mIsTextNode;
|
||||
}
|
||||
|
||||
MeasureOutput measure(MeasureOutput measureOutput, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) {
|
||||
long measure(float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) {
|
||||
if (!isMeasureDefined()) {
|
||||
throw new RuntimeException("Measure function isn't defined!");
|
||||
}
|
||||
measureOutput.height = CSSConstants.UNDEFINED;
|
||||
measureOutput.width = CSSConstants.UNDEFINED;
|
||||
Assertions.assertNotNull(mMeasureFunction).measure(this, width, widthMode, height, heightMode, measureOutput);
|
||||
return measureOutput;
|
||||
return Assertions.assertNotNull(mMeasureFunction).measure(this, width, widthMode, height, heightMode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -560,22 +560,23 @@ public class LayoutEngine {
|
|||
} else {
|
||||
|
||||
// Measure the text under the current constraints.
|
||||
MeasureOutput measureDim = node.measure(
|
||||
|
||||
layoutContext.measureOutput,
|
||||
long measureOutput = node.measure(
|
||||
innerWidth,
|
||||
widthMeasureMode,
|
||||
innerHeight,
|
||||
heightMeasureMode
|
||||
);
|
||||
|
||||
int outputWidth = MeasureOutput.getWidth(measureOutput);
|
||||
int outputHeight = MeasureOutput.getHeight(measureOutput);
|
||||
|
||||
node.layout.measuredDimensions[DIMENSION_WIDTH] = boundAxis(node, CSS_FLEX_DIRECTION_ROW,
|
||||
(widthMeasureMode == CSSMeasureMode.UNDEFINED || widthMeasureMode == CSSMeasureMode.AT_MOST) ?
|
||||
measureDim.width + paddingAndBorderAxisRow :
|
||||
outputWidth + paddingAndBorderAxisRow :
|
||||
availableWidth - marginAxisRow);
|
||||
node.layout.measuredDimensions[DIMENSION_HEIGHT] = boundAxis(node, CSS_FLEX_DIRECTION_COLUMN,
|
||||
(heightMeasureMode == CSSMeasureMode.UNDEFINED || heightMeasureMode == CSSMeasureMode.AT_MOST) ?
|
||||
measureDim.height + paddingAndBorderAxisColumn :
|
||||
outputHeight + paddingAndBorderAxisColumn :
|
||||
availableHeight - marginAxisColumn);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,10 +10,23 @@
|
|||
package com.facebook.csslayout;
|
||||
|
||||
/**
|
||||
* POJO to hold the output of the measure function.
|
||||
* Helpers for building measure output value.
|
||||
*/
|
||||
public class MeasureOutput {
|
||||
|
||||
public float width;
|
||||
public float height;
|
||||
public static long make(float width, float height) {
|
||||
return make((int) width, (int) height);
|
||||
}
|
||||
|
||||
public static long make(int width, int height) {
|
||||
return ((long) width) << 32 | ((long) height);
|
||||
}
|
||||
|
||||
public static int getWidth(long measureOutput) {
|
||||
return (int) (0xFFFFFFFF & (measureOutput >> 32));
|
||||
}
|
||||
|
||||
public static int getHeight(long measureOutput) {
|
||||
return (int) (0xFFFFFFFF & measureOutput);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,13 +28,12 @@ public class ARTSurfaceViewManager extends
|
|||
|
||||
private static final CSSNodeAPI.MeasureFunction MEASURE_FUNCTION = new CSSNodeAPI.MeasureFunction() {
|
||||
@Override
|
||||
public void measure(
|
||||
public long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput) {
|
||||
CSSMeasureMode heightMode) {
|
||||
throw new IllegalStateException("SurfaceView should have explicit width and height set");
|
||||
}
|
||||
};
|
||||
|
|
|
@ -52,13 +52,12 @@ public class ProgressBarShadowNode extends LayoutShadowNode implements CSSNodeAP
|
|||
}
|
||||
|
||||
@Override
|
||||
public void measure(
|
||||
public long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput) {
|
||||
CSSMeasureMode heightMode) {
|
||||
final int style = ReactProgressBarViewManager.getStyleFromString(getStyle());
|
||||
if (!mMeasured.contains(style)) {
|
||||
ProgressBar progressBar = ReactProgressBarViewManager.createProgressBar(getThemedContext(), style);
|
||||
|
@ -71,7 +70,6 @@ public class ProgressBarShadowNode extends LayoutShadowNode implements CSSNodeAP
|
|||
mMeasured.add(style);
|
||||
}
|
||||
|
||||
measureOutput.height = mHeight.get(style);
|
||||
measureOutput.width = mWidth.get(style);
|
||||
return MeasureOutput.make(mWidth.get(style), mHeight.get(style));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,13 +50,12 @@ public class ReactSliderManager extends SimpleViewManager<ReactSlider> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void measure(
|
||||
public long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput) {
|
||||
CSSMeasureMode heightMode) {
|
||||
if (!mMeasured) {
|
||||
SeekBar reactSlider = new ReactSlider(getThemedContext(), null, STYLE);
|
||||
final int spec = View.MeasureSpec.makeMeasureSpec(
|
||||
|
@ -67,8 +66,8 @@ public class ReactSliderManager extends SimpleViewManager<ReactSlider> {
|
|||
mHeight = reactSlider.getMeasuredHeight();
|
||||
mMeasured = true;
|
||||
}
|
||||
measureOutput.width = mWidth;
|
||||
measureOutput.height = mHeight;
|
||||
|
||||
return MeasureOutput.make(mWidth, mHeight);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,13 +44,12 @@ public class ReactSwitchManager extends SimpleViewManager<ReactSwitch> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void measure(
|
||||
public long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput) {
|
||||
CSSMeasureMode heightMode) {
|
||||
if (!mMeasured) {
|
||||
// Create a switch with the default config and measure it; since we don't (currently)
|
||||
// support setting custom switch text, this is fine, as all switches will measure the same
|
||||
|
@ -64,8 +63,8 @@ public class ReactSwitchManager extends SimpleViewManager<ReactSwitch> {
|
|||
mHeight = reactSwitch.getMeasuredHeight();
|
||||
mMeasured = true;
|
||||
}
|
||||
measureOutput.width = mWidth;
|
||||
measureOutput.height = mHeight;
|
||||
|
||||
return MeasureOutput.make(mWidth, mHeight);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -221,13 +221,12 @@ public class ReactTextShadowNode extends LayoutShadowNode {
|
|||
private static final CSSNodeAPI.MeasureFunction TEXT_MEASURE_FUNCTION =
|
||||
new CSSNodeAPI.MeasureFunction() {
|
||||
@Override
|
||||
public void measure(
|
||||
public long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput) {
|
||||
CSSMeasureMode heightMode) {
|
||||
// TODO(5578671): Handle text direction (see View#getTextDirectionHeuristic)
|
||||
ReactTextShadowNode reactCSSNode = (ReactTextShadowNode) node;
|
||||
TextPaint textPaint = sTextPaintInstance;
|
||||
|
@ -279,11 +278,13 @@ public class ReactTextShadowNode extends LayoutShadowNode {
|
|||
true);
|
||||
}
|
||||
|
||||
measureOutput.height = layout.getHeight();
|
||||
measureOutput.width = layout.getWidth();
|
||||
if (reactCSSNode.mNumberOfLines != UNSET &&
|
||||
reactCSSNode.mNumberOfLines < layout.getLineCount()) {
|
||||
measureOutput.height = layout.getLineBottom(reactCSSNode.mNumberOfLines - 1);
|
||||
return MeasureOutput.make(
|
||||
layout.getWidth(),
|
||||
layout.getLineBottom(reactCSSNode.mNumberOfLines - 1));
|
||||
} else {
|
||||
return MeasureOutput.make(layout.getWidth(), layout.getHeight());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -71,13 +71,12 @@ public class ReactTextInputShadowNode extends ReactTextShadowNode implements
|
|||
}
|
||||
|
||||
@Override
|
||||
public void measure(
|
||||
public long measure(
|
||||
CSSNodeAPI node,
|
||||
float width,
|
||||
CSSMeasureMode widthMode,
|
||||
float height,
|
||||
CSSMeasureMode heightMode,
|
||||
MeasureOutput measureOutput) {
|
||||
CSSMeasureMode heightMode) {
|
||||
// measure() should never be called before setThemedContext()
|
||||
EditText editText = Assertions.assertNotNull(mEditText);
|
||||
|
||||
|
@ -104,8 +103,8 @@ public class ReactTextInputShadowNode extends ReactTextShadowNode implements
|
|||
editText.measure(
|
||||
MeasureUtil.getMeasureSpec(width, widthMode),
|
||||
MeasureUtil.getMeasureSpec(height, heightMode));
|
||||
measureOutput.width = editText.getMeasuredWidth();
|
||||
measureOutput.height = editText.getMeasuredHeight();
|
||||
|
||||
return MeasureOutput.make(editText.getMeasuredWidth(), editText.getMeasuredHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue