From c75ce8146f9366b9d1ea7cdc6fc73c87fb518430 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 20 Feb 2018 05:41:50 -0800 Subject: [PATCH] Add constructor in YGLayout Reviewed By: emilsjolander Differential Revision: D7019653 fbshipit-source-id: 5a2655626db0915fcebe7d4517e2d0b2e2484460 --- React/React.xcodeproj/project.pbxproj | 12 +++ ReactCommon/yoga/yoga/YGLayout.cpp | 73 ++++++++++++++++++ ReactCommon/yoga/yoga/YGLayout.h | 42 ++++++++++ ReactCommon/yoga/yoga/YGNode.cpp | 2 +- ReactCommon/yoga/yoga/YGNode.h | 1 + ReactCommon/yoga/yoga/YGStyle.cpp | 21 +++++ ReactCommon/yoga/yoga/Yoga-internal.h | 107 -------------------------- 7 files changed, 150 insertions(+), 108 deletions(-) create mode 100644 ReactCommon/yoga/yoga/YGLayout.cpp create mode 100644 ReactCommon/yoga/yoga/YGLayout.h diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index 539f62fc672..0f79353b3f2 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -952,6 +952,10 @@ 3DFE0D1C1DF8575800459392 /* Yoga.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 130A77081DF767AF001F9587 /* Yoga.h */; }; 3EDCA8A51D3591E700450C31 /* RCTErrorInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EDCA8A41D3591E700450C31 /* RCTErrorInfo.m */; }; 5335D5411FE81A4700883D58 /* RCTShadowView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5335D5401FE81A4700883D58 /* RCTShadowView.m */; }; + 53438962203905BB008E0CB3 /* YGLayout.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5343895E203905B6008E0CB3 /* YGLayout.cpp */; }; + 53438963203905BC008E0CB3 /* YGLayout.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5343895E203905B6008E0CB3 /* YGLayout.cpp */; }; + 53438964203905BF008E0CB3 /* YGLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 5343895F203905B6008E0CB3 /* YGLayout.h */; }; + 53438965203905C0008E0CB3 /* YGLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 5343895F203905B6008E0CB3 /* YGLayout.h */; }; 5352C5752038FF9500A3B97E /* YGStyle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5352C5722038FF8D00A3B97E /* YGStyle.cpp */; }; 5352C5762038FF9700A3B97E /* YGStyle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5352C5722038FF8D00A3B97E /* YGStyle.cpp */; }; 5352C5772038FF9A00A3B97E /* YGStyle.h in Headers */ = {isa = PBXBuildFile; fileRef = 5352C5712038FF8D00A3B97E /* YGStyle.h */; }; @@ -2148,6 +2152,8 @@ 3EDCA8A31D3591E700450C31 /* RCTErrorInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTErrorInfo.h; sourceTree = ""; }; 3EDCA8A41D3591E700450C31 /* RCTErrorInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTErrorInfo.m; sourceTree = ""; }; 5335D5401FE81A4700883D58 /* RCTShadowView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTShadowView.m; sourceTree = ""; }; + 5343895E203905B6008E0CB3 /* YGLayout.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGLayout.cpp; sourceTree = ""; }; + 5343895F203905B6008E0CB3 /* YGLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGLayout.h; sourceTree = ""; }; 5352C5712038FF8D00A3B97E /* YGStyle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGStyle.h; sourceTree = ""; }; 5352C5722038FF8D00A3B97E /* YGStyle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGStyle.cpp; sourceTree = ""; }; 53756E372004FFF700FBBD99 /* Utils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Utils.cpp; sourceTree = ""; }; @@ -2338,6 +2344,8 @@ 130A77021DF767AF001F9587 /* yoga */ = { isa = PBXGroup; children = ( + 5343895E203905B6008E0CB3 /* YGLayout.cpp */, + 5343895F203905B6008E0CB3 /* YGLayout.h */, 5352C5722038FF8D00A3B97E /* YGStyle.cpp */, 5352C5712038FF8D00A3B97E /* YGStyle.h */, 53756E372004FFF700FBBD99 /* Utils.cpp */, @@ -3284,6 +3292,7 @@ 53756E3D2004FFFE00FBBD99 /* Utils.h in Headers */, 5376C5E71FC6DDC20083513D /* YGNodePrint.h in Headers */, 3DFE0D191DF8574D00459392 /* Yoga.h in Headers */, + 53438965203905C0008E0CB3 /* YGLayout.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -3363,6 +3372,7 @@ 53756E3C2004FFFC00FBBD99 /* Utils.h in Headers */, 5376C5E61FC6DDC10083513D /* YGNodePrint.h in Headers */, 133957891DF76D3500EC27BE /* YGMacros.h in Headers */, + 53438964203905BF008E0CB3 /* YGLayout.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -4306,6 +4316,7 @@ 53EC85E21FDEC75F0051B2B5 /* YGNode.cpp in Sources */, 53D1239A1FBF1EF2001B8A10 /* YGEnums.cpp in Sources */, 53756E3B2004FFFA00FBBD99 /* Utils.cpp in Sources */, + 53438962203905BB008E0CB3 /* YGLayout.cpp in Sources */, 5376C5E41FC6DDBC0083513D /* YGNodePrint.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -4319,6 +4330,7 @@ 53EC85E31FDEC75F0051B2B5 /* YGNode.cpp in Sources */, 53D1239B1FBF1EF4001B8A10 /* YGEnums.cpp in Sources */, 53756E3E2005000300FBBD99 /* Utils.cpp in Sources */, + 53438963203905BC008E0CB3 /* YGLayout.cpp in Sources */, 5376C5E51FC6DDBD0083513D /* YGNodePrint.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/ReactCommon/yoga/yoga/YGLayout.cpp b/ReactCommon/yoga/yoga/YGLayout.cpp new file mode 100644 index 00000000000..8f00c0f3ef4 --- /dev/null +++ b/ReactCommon/yoga/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/ReactCommon/yoga/yoga/YGLayout.h b/ReactCommon/yoga/yoga/YGLayout.h new file mode 100644 index 00000000000..125a6d0b03e --- /dev/null +++ b/ReactCommon/yoga/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/ReactCommon/yoga/yoga/YGNode.cpp b/ReactCommon/yoga/yoga/YGNode.cpp index 24a00a883f7..89be5bfe3bb 100644 --- a/ReactCommon/yoga/yoga/YGNode.cpp +++ b/ReactCommon/yoga/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/ReactCommon/yoga/yoga/YGNode.h b/ReactCommon/yoga/yoga/YGNode.h index 452b8f31065..27a1ed68fac 100644 --- a/ReactCommon/yoga/yoga/YGNode.h +++ b/ReactCommon/yoga/yoga/YGNode.h @@ -7,6 +7,7 @@ #pragma once #include +#include "YGLayout.h" #include "YGStyle.h" #include "Yoga-internal.h" diff --git a/ReactCommon/yoga/yoga/YGStyle.cpp b/ReactCommon/yoga/yoga/YGStyle.cpp index 516ce061818..a73cb3ce6dd 100644 --- a/ReactCommon/yoga/yoga/YGStyle.cpp +++ b/ReactCommon/yoga/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/ReactCommon/yoga/yoga/Yoga-internal.h b/ReactCommon/yoga/yoga/Yoga-internal.h index b1bb0de3ded..43723df087d 100644 --- a/ReactCommon/yoga/yoga/Yoga-internal.h +++ b/ReactCommon/yoga/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(