Adding ability to account for rounding in YGNodeCanUseCachedMeasurement

Reviewed By: emilsjolander

Differential Revision: D5059560

fbshipit-source-id: d729e991758a8c668a4b373105b71337961875cd
This commit is contained in:
Georgiy Kassabli 2017-05-15 06:18:24 -07:00 committed by Facebook Github Bot
parent 28c07f8698
commit baf93e46d0
2 changed files with 33 additions and 25 deletions

View File

@ -3129,6 +3129,25 @@ static inline bool YGMeasureModeNewMeasureSizeIsStricterAndStillValid(YGMeasureM
lastSize > size && (lastComputedSize <= size || YGFloatsEqual(size, lastComputedSize)); lastSize > size && (lastComputedSize <= size || YGFloatsEqual(size, lastComputedSize));
} }
static float YGRoundValueToPixelGrid(const float value,
const float pointScaleFactor,
const bool forceCeil,
const bool forceFloor) {
float fractial = fmodf(value, pointScaleFactor);
if (YGFloatsEqual(fractial, 0)) {
// Still remove fractial as fractial could be extremely small.
return value - fractial;
}
if (forceCeil) {
return value - fractial + pointScaleFactor;
} else if (forceFloor) {
return value - fractial;
} else {
return value - fractial + (fractial >= pointScaleFactor / 2.0f ? pointScaleFactor : 0);
}
}
bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
const float width, const float width,
const YGMeasureMode heightMode, const YGMeasureMode heightMode,
@ -3140,13 +3159,18 @@ bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
const float lastComputedWidth, const float lastComputedWidth,
const float lastComputedHeight, const float lastComputedHeight,
const float marginRow, const float marginRow,
const float marginColumn) { const float marginColumn,
const YGConfigRef config) {
if (lastComputedHeight < 0 || lastComputedWidth < 0) { if (lastComputedHeight < 0 || lastComputedWidth < 0) {
return false; return false;
} }
const float effectiveWidth = config != NULL ? YGRoundValueToPixelGrid(width, config->pointScaleFactor, false, false) : width;
const float effectiveHeight = config != NULL ? YGRoundValueToPixelGrid(height, config->pointScaleFactor, false, false) : height;
const float effectiveLastWidth = config != NULL ? YGRoundValueToPixelGrid(lastWidth, config->pointScaleFactor, false, false) : lastWidth;
const float effectiveLastHeight = config != NULL ? YGRoundValueToPixelGrid(lastHeight, config->pointScaleFactor, false, false) : lastHeight;
const bool hasSameWidthSpec = lastWidthMode == widthMode && YGFloatsEqual(lastWidth, width); const bool hasSameWidthSpec = lastWidthMode == widthMode && YGFloatsEqual(effectiveLastWidth, effectiveWidth);
const bool hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(lastHeight, height); const bool hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(effectiveLastHeight, effectiveHeight);
const bool widthIsCompatible = const bool widthIsCompatible =
hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode, hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode,
@ -3239,7 +3263,8 @@ bool YGLayoutNodeInternal(const YGNodeRef node,
layout->cachedLayout.computedWidth, layout->cachedLayout.computedWidth,
layout->cachedLayout.computedHeight, layout->cachedLayout.computedHeight,
marginAxisRow, marginAxisRow,
marginAxisColumn)) { marginAxisColumn,
config)) {
cachedResults = &layout->cachedLayout; cachedResults = &layout->cachedLayout;
} else { } else {
// Try to use the measurement cache. // Try to use the measurement cache.
@ -3255,7 +3280,8 @@ bool YGLayoutNodeInternal(const YGNodeRef node,
layout->cachedMeasurements[i].computedWidth, layout->cachedMeasurements[i].computedWidth,
layout->cachedMeasurements[i].computedHeight, layout->cachedMeasurements[i].computedHeight,
marginAxisRow, marginAxisRow,
marginAxisColumn)) { marginAxisColumn,
config)) {
cachedResults = &layout->cachedMeasurements[i]; cachedResults = &layout->cachedMeasurements[i];
break; break;
} }
@ -3389,25 +3415,6 @@ void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInP
} }
} }
static float YGRoundValueToPixelGrid(const float value,
const float pointScaleFactor,
const bool forceCeil,
const bool forceFloor) {
float fractial = fmodf(value, pointScaleFactor);
if (YGFloatsEqual(fractial, 0)) {
// Still remove fractial as fractial could be extremely small.
return value - fractial;
}
if (forceCeil) {
return value - fractial + pointScaleFactor;
} else if (forceFloor) {
return value - fractial;
} else {
return value - fractial + (fractial >= pointScaleFactor / 2.0f ? pointScaleFactor : 0);
}
}
static void YGRoundToPixelGrid(const YGNodeRef node, static void YGRoundToPixelGrid(const YGNodeRef node,
const float pointScaleFactor, const float pointScaleFactor,
const float absoluteLeft, const float absoluteLeft,

View File

@ -111,7 +111,8 @@ WIN_EXPORT bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
const float lastComputedWidth, const float lastComputedWidth,
const float lastComputedHeight, const float lastComputedHeight,
const float marginRow, const float marginRow,
const float marginColumn); const float marginColumn,
YGConfigRef config);
WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode); WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode);