From adad054cad3a49a78b4b8be1283dbcfee7192145 Mon Sep 17 00:00:00 2001 From: Georgiy Kassabli Date: Mon, 15 May 2017 06:18:24 -0700 Subject: [PATCH] Adding ability to account for rounding in YGNodeCanUseCachedMeasurement Summary: We want to be able to use YGConfig to account for possible rounding of width/height Reviewed By: emilsjolander Differential Revision: D5059560 fbshipit-source-id: d729e991758a8c668a4b373105b71337961875cd --- yoga/Yoga.c | 55 ++++++++++++++++++++++++++++++----------------------- yoga/Yoga.h | 3 ++- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/yoga/Yoga.c b/yoga/Yoga.c index a7dd3f78..e5fd5f79 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -3129,6 +3129,25 @@ static inline bool YGMeasureModeNewMeasureSizeIsStricterAndStillValid(YGMeasureM 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, const float width, const YGMeasureMode heightMode, @@ -3140,13 +3159,18 @@ bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, const float lastComputedWidth, const float lastComputedHeight, const float marginRow, - const float marginColumn) { + const float marginColumn, + const YGConfigRef config) { if (lastComputedHeight < 0 || lastComputedWidth < 0) { 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 hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(lastHeight, height); + const bool hasSameWidthSpec = lastWidthMode == widthMode && YGFloatsEqual(effectiveLastWidth, effectiveWidth); + const bool hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(effectiveLastHeight, effectiveHeight); const bool widthIsCompatible = hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode, @@ -3239,7 +3263,8 @@ bool YGLayoutNodeInternal(const YGNodeRef node, layout->cachedLayout.computedWidth, layout->cachedLayout.computedHeight, marginAxisRow, - marginAxisColumn)) { + marginAxisColumn, + config)) { cachedResults = &layout->cachedLayout; } else { // Try to use the measurement cache. @@ -3255,7 +3280,8 @@ bool YGLayoutNodeInternal(const YGNodeRef node, layout->cachedMeasurements[i].computedWidth, layout->cachedMeasurements[i].computedHeight, marginAxisRow, - marginAxisColumn)) { + marginAxisColumn, + config)) { cachedResults = &layout->cachedMeasurements[i]; 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, const float pointScaleFactor, const float absoluteLeft, diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 162415db..3579941c 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -111,7 +111,8 @@ WIN_EXPORT bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, const float lastComputedWidth, const float lastComputedHeight, const float marginRow, - const float marginColumn); + const float marginColumn, + YGConfigRef config); WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode);