diff --git a/yoga/YGLayout.cpp b/yoga/YGLayout.cpp new file mode 100644 index 00000000..8f00c0f3 --- /dev/null +++ b/yoga/YGLayout.cpp @@ -0,0 +1,73 @@ +/** + * 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. + */ +#include "YGLayout.h" + +const std::array kYGDefaultDimensionValues = { + {YGUndefined, YGUndefined}}; + +YGLayout::YGLayout() + : position(), + dimensions(kYGDefaultDimensionValues), + margin(), + border(), + padding(), + direction(YGDirectionInherit), + computedFlexBasisGeneration(0), + computedFlexBasis(YGUndefined), + hadOverflow(false), + generationCount(0), + lastParentDirection((YGDirection)-1), + nextCachedMeasurementsIndex(0), + cachedMeasurements(), + measuredDimensions(kYGDefaultDimensionValues), + cachedLayout({ + .availableWidth = 0, + .availableHeight = 0, + .widthMeasureMode = (YGMeasureMode)-1, + .heightMeasureMode = (YGMeasureMode)-1, + .computedWidth = -1, + .computedHeight = -1, + }), + didUseLegacyFlag(false), + doesLegacyStretchFlagAffectsLayout(false) {} + +bool YGLayout::operator==(YGLayout layout) const { + bool isEqual = position == layout.position && + dimensions == layout.dimensions && margin == layout.margin && + border == layout.border && padding == layout.padding && + direction == layout.direction && hadOverflow == layout.hadOverflow && + lastParentDirection == layout.lastParentDirection && + nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex && + cachedLayout == layout.cachedLayout; + + for (uint32_t i = 0; i < YG_MAX_CACHED_RESULT_COUNT && isEqual; ++i) { + isEqual = isEqual && cachedMeasurements[i] == layout.cachedMeasurements[i]; + } + + if (!YGFloatIsUndefined(computedFlexBasis) || + !YGFloatIsUndefined(layout.computedFlexBasis)) { + isEqual = isEqual && (computedFlexBasis == layout.computedFlexBasis); + } + if (!YGFloatIsUndefined(measuredDimensions[0]) || + !YGFloatIsUndefined(layout.measuredDimensions[0])) { + isEqual = + isEqual && (measuredDimensions[0] == layout.measuredDimensions[0]); + } + if (!YGFloatIsUndefined(measuredDimensions[1]) || + !YGFloatIsUndefined(layout.measuredDimensions[1])) { + isEqual = + isEqual && (measuredDimensions[1] == layout.measuredDimensions[1]); + } + + return isEqual; +} + +bool YGLayout::operator!=(YGLayout layout) const { + return !(*this == layout); +} diff --git a/yoga/YGLayout.h b/yoga/YGLayout.h new file mode 100644 index 00000000..125a6d0b --- /dev/null +++ b/yoga/YGLayout.h @@ -0,0 +1,42 @@ +/** + * 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. + */ +#pragma once +#include "Yoga-internal.h" + +struct YGLayout { + std::array position; + std::array dimensions; + std::array margin; + std::array border; + std::array padding; + YGDirection direction; + + uint32_t computedFlexBasisGeneration; + float computedFlexBasis; + bool hadOverflow; + + // Instead of recomputing the entire layout every single time, we + // cache some information to break early when nothing changed + uint32_t generationCount; + YGDirection lastParentDirection; + + uint32_t nextCachedMeasurementsIndex; + std::array + cachedMeasurements; + std::array measuredDimensions; + + YGCachedMeasurement cachedLayout; + bool didUseLegacyFlag; + bool doesLegacyStretchFlagAffectsLayout; + + YGLayout(); + + bool operator==(YGLayout layout) const; + bool operator!=(YGLayout layout) const; +}; diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 24a00a88..89be5bfe 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -383,7 +383,7 @@ YGNode::YGNode() baseline_(nullptr), dirtied_(nullptr), style_(YGStyle()), - layout_(gYGNodeLayoutDefaults), + layout_(YGLayout()), lineIndex_(0), parent_(nullptr), children_(YGVector()), diff --git a/yoga/YGNode.h b/yoga/YGNode.h index 452b8f31..27a1ed68 100644 --- a/yoga/YGNode.h +++ b/yoga/YGNode.h @@ -7,6 +7,7 @@ #pragma once #include +#include "YGLayout.h" #include "YGStyle.h" #include "Yoga-internal.h" diff --git a/yoga/YGStyle.cpp b/yoga/YGStyle.cpp index 516ce061..a73cb3ce 100644 --- a/yoga/YGStyle.cpp +++ b/yoga/YGStyle.cpp @@ -8,6 +8,27 @@ */ #include "YGStyle.h" +const YGValue kYGValueUndefined = {YGUndefined, YGUnitUndefined}; + +const YGValue kYGValueAuto = {YGUndefined, YGUnitAuto}; + +const std::array kYGDefaultEdgeValuesUnit = { + {kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined, + kYGValueUndefined}}; + +const std::array kYGDefaultDimensionValuesAutoUnit = { + {kYGValueAuto, kYGValueAuto}}; + +const std::array kYGDefaultDimensionValuesUnit = { + {kYGValueUndefined, kYGValueUndefined}}; + YGStyle::YGStyle() : direction(YGDirectionInherit), flexDirection(YGFlexDirectionColumn), diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index b1bb0de3..43723df0 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -41,23 +41,6 @@ bool YGValueArrayEqual( return areEqual; } -const YGValue kYGValueUndefined = {YGUndefined, YGUnitUndefined}; -const YGValue kYGValueAuto = {YGUndefined, YGUnitAuto}; -const std::array kYGDefaultEdgeValuesUnit = { - {kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined}}; -const std::array kYGDefaultDimensionValuesAutoUnit = { - {kYGValueAuto, kYGValueAuto}}; -const std::array kYGDefaultDimensionValuesUnit = { - {kYGValueUndefined, kYGValueUndefined}}; - struct YGCachedMeasurement { float availableWidth; float availableHeight; @@ -95,68 +78,6 @@ struct YGCachedMeasurement { // layouts should not require more than 16 entries to fit within the cache. #define YG_MAX_CACHED_RESULT_COUNT 16 -struct YGLayout { - std::array position; - std::array dimensions; - std::array margin; - std::array border; - std::array padding; - YGDirection direction; - - uint32_t computedFlexBasisGeneration; - float computedFlexBasis; - bool hadOverflow; - - // Instead of recomputing the entire layout every single time, we - // cache some information to break early when nothing changed - uint32_t generationCount; - YGDirection lastParentDirection; - - uint32_t nextCachedMeasurementsIndex; - YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT]; - std::array measuredDimensions; - - YGCachedMeasurement cachedLayout; - bool didUseLegacyFlag; - bool doesLegacyStretchFlagAffectsLayout; - - bool operator==(YGLayout layout) const { - bool isEqual = position == layout.position && - dimensions == layout.dimensions && margin == layout.margin && - border == layout.border && padding == layout.padding && - direction == layout.direction && hadOverflow == layout.hadOverflow && - lastParentDirection == layout.lastParentDirection && - nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex && - cachedLayout == layout.cachedLayout; - - for (uint32_t i = 0; i < YG_MAX_CACHED_RESULT_COUNT && isEqual; ++i) { - isEqual = - isEqual && cachedMeasurements[i] == layout.cachedMeasurements[i]; - } - - if (!YGFloatIsUndefined(computedFlexBasis) || - !YGFloatIsUndefined(layout.computedFlexBasis)) { - isEqual = isEqual && (computedFlexBasis == layout.computedFlexBasis); - } - if (!YGFloatIsUndefined(measuredDimensions[0]) || - !YGFloatIsUndefined(layout.measuredDimensions[0])) { - isEqual = - isEqual && (measuredDimensions[0] == layout.measuredDimensions[0]); - } - if (!YGFloatIsUndefined(measuredDimensions[1]) || - !YGFloatIsUndefined(layout.measuredDimensions[1])) { - isEqual = - isEqual && (measuredDimensions[1] == layout.measuredDimensions[1]); - } - - return isEqual; - } - - bool operator!=(YGLayout layout) const { - return !(*this == layout); - } -}; - struct YGConfig { bool experimentalFeatures[YGExperimentalFeatureCount + 1]; bool useWebDefaults; @@ -172,34 +93,6 @@ static const float kDefaultFlexGrow = 0.0f; static const float kDefaultFlexShrink = 0.0f; static const float kWebDefaultFlexShrink = 1.0f; -static const YGLayout gYGNodeLayoutDefaults = { - .position = {}, - .dimensions = {{YGUndefined, YGUndefined}}, - .margin = {}, - .border = {}, - .padding = {}, - .direction = YGDirectionInherit, - .computedFlexBasisGeneration = 0, - .computedFlexBasis = YGUndefined, - .hadOverflow = false, - .generationCount = 0, - .lastParentDirection = (YGDirection)-1, - .nextCachedMeasurementsIndex = 0, - .cachedMeasurements = {}, - .measuredDimensions = {{YGUndefined, YGUndefined}}, - .cachedLayout = - { - .availableWidth = 0, - .availableHeight = 0, - .widthMeasureMode = (YGMeasureMode)-1, - .heightMeasureMode = (YGMeasureMode)-1, - .computedWidth = -1, - .computedHeight = -1, - }, - .didUseLegacyFlag = false, - .doesLegacyStretchFlagAffectsLayout = false, -}; - extern bool YGFloatsEqual(const float a, const float b); extern bool YGValueEqual(const YGValue a, const YGValue b); extern const YGValue* YGComputedEdgeValue(