From 8cfdd4d437bed18be150f29ddad9155e8d3a1ec3 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 11 Sep 2023 23:25:34 -0700 Subject: [PATCH] Remove layoutContext Threading Summary: X-link: https://github.com/facebook/yoga/pull/1376 kill_with_fire_flamethrower Differential Revision: D49179244 fbshipit-source-id: 7d23f4abc16ed9ae073686df79aafd80e8442b55 --- .../ReactCommon/yoga/yoga/Yoga-internal.h | 7 -- .../ReactCommon/yoga/yoga/Yoga.cpp | 16 +--- .../yoga/yoga/algorithm/Baseline.cpp | 7 +- .../yoga/yoga/algorithm/Baseline.h | 2 +- .../yoga/yoga/algorithm/CalculateLayout.cpp | 80 +++++-------------- .../yoga/yoga/algorithm/CalculateLayout.h | 3 +- .../ReactCommon/yoga/yoga/config/Config.cpp | 40 ++-------- .../ReactCommon/yoga/yoga/config/Config.h | 35 +------- .../yoga/yoga/debug/AssertFatal.cpp | 6 +- .../ReactCommon/yoga/yoga/debug/Log.cpp | 18 ++--- .../ReactCommon/yoga/yoga/debug/Log.h | 4 +- .../ReactCommon/yoga/yoga/event/event.h | 8 -- .../ReactCommon/yoga/yoga/node/Node.cpp | 61 +++++--------- .../ReactCommon/yoga/yoga/node/Node.h | 78 ++++-------------- 14 files changed, 78 insertions(+), 287 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h b/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h index 5ad0dd69371d9b..7c1caaa3785e2b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h @@ -15,13 +15,6 @@ YG_EXTERN_C_BEGIN -YG_EXPORT void YGNodeCalculateLayoutWithContext( - YGNodeRef node, - float availableWidth, - float availableHeight, - YGDirection ownerDirection, - void* layoutContext); - // Deallocates a Yoga Node. Unlike YGNodeFree, does not remove the node from // its parent or children. YG_EXPORT void YGNodeDeallocate(YGNodeRef node); diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index 5c5f6a8cca17b9..c8eff1ece3e7e5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -57,7 +57,7 @@ void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc) { } YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeConstRef node) { - return resolveRef(node)->getDirtied(); + return resolveRef(node)->getDirtiedFunc(); } void YGNodeSetDirtiedFunc(YGNodeRef node, YGDirtiedFunc dirtiedFunc) { @@ -805,7 +805,7 @@ void YGNodePrint(const YGNodeConstRef nodeRef, const YGPrintOptions options) { const auto node = resolveRef(nodeRef); std::string str; yoga::nodeToString(str, node, options, 0); - yoga::log(node, YGLogLevelDebug, nullptr, str.c_str()); + yoga::log(node, YGLogLevelDebug, str.c_str()); } #endif @@ -927,16 +927,6 @@ void YGNodeCalculateLayout( const float ownerWidth, const float ownerHeight, const YGDirection ownerDirection) { - YGNodeCalculateLayoutWithContext( - node, ownerWidth, ownerHeight, ownerDirection, nullptr); -} - -void YGNodeCalculateLayoutWithContext( - const YGNodeRef node, - const float ownerWidth, - const float ownerHeight, - const YGDirection ownerDirection, - void* layoutContext) { yoga::calculateLayout( - resolveRef(node), ownerWidth, ownerHeight, ownerDirection, layoutContext); + resolveRef(node), ownerWidth, ownerHeight, ownerDirection); } diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.cpp index 1cbfbd188342c1..a3fd42c5770cbc 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.cpp @@ -14,15 +14,14 @@ namespace facebook::yoga { -float calculateBaseline(const yoga::Node* node, void* layoutContext) { +float calculateBaseline(const yoga::Node* node) { if (node->hasBaselineFunc()) { Event::publish(node); const float baseline = node->baseline( node->getLayout().measuredDimensions[YGDimensionWidth], - node->getLayout().measuredDimensions[YGDimensionHeight], - layoutContext); + node->getLayout().measuredDimensions[YGDimensionHeight]); Event::publish(node); @@ -58,7 +57,7 @@ float calculateBaseline(const yoga::Node* node, void* layoutContext) { return node->getLayout().measuredDimensions[YGDimensionHeight]; } - const float baseline = calculateBaseline(baselineChild, layoutContext); + const float baseline = calculateBaseline(baselineChild); return baseline + baselineChild->getLayout().position[YGEdgeTop]; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.h index 71fb3d520d39cf..c95ffd0f040b05 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/Baseline.h @@ -13,7 +13,7 @@ namespace facebook::yoga { // Calculate baseline represented as an offset from the top edge of the node. -float calculateBaseline(const yoga::Node* node, void* layoutContext); +float calculateBaseline(const yoga::Node* node); // Whether any of the children of this node participate in baseline alignment bool isBaselineLayout(const yoga::Node* node); diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 78766ede9380ba..c91b30391badf2 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -46,7 +46,6 @@ bool calculateLayoutInternal( const LayoutPassReason reason, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount); @@ -134,7 +133,6 @@ static void computeFlexBasisForChild( const YGDirection direction, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount) { const YGFlexDirection mainAxis = @@ -309,7 +307,6 @@ static void computeFlexBasisForChild( LayoutPassReason::kMeasureChild, config, layoutMarkerData, - layoutContext, depth, generationCount); @@ -329,7 +326,6 @@ static void layoutAbsoluteChild( const YGDirection direction, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount) { const YGFlexDirection mainAxis = @@ -437,7 +433,6 @@ static void layoutAbsoluteChild( LayoutPassReason::kAbsMeasureChild, config, layoutMarkerData, - layoutContext, depth, generationCount); childWidth = child->getLayout().measuredDimensions[YGDimensionWidth] + @@ -459,7 +454,6 @@ static void layoutAbsoluteChild( LayoutPassReason::kAbsLayout, config, layoutMarkerData, - layoutContext, depth, generationCount); @@ -564,7 +558,6 @@ static void measureNodeWithMeasureFunc( const float ownerWidth, const float ownerHeight, LayoutData& layoutMarkerData, - void* const layoutContext, const LayoutPassReason reason) { yoga::assertFatalWithNode( node, @@ -613,11 +606,7 @@ static void measureNodeWithMeasureFunc( // Measure the text under the current constraints. const YGSize measuredSize = node->measure( - innerWidth, - widthMeasureMode, - innerHeight, - heightMeasureMode, - layoutContext); + innerWidth, widthMeasureMode, innerHeight, heightMeasureMode); layoutMarkerData.measureCallbacks += 1; layoutMarkerData.measureCallbackReasonsCount[static_cast(reason)] += @@ -625,8 +614,7 @@ static void measureNodeWithMeasureFunc( Event::publish( node, - {layoutContext, - innerWidth, + {innerWidth, widthMeasureMode, innerHeight, heightMeasureMode, @@ -739,17 +727,15 @@ static bool measureNodeWithFixedSize( return false; } -static void zeroOutLayoutRecursively( - yoga::Node* const node, - void* layoutContext) { +static void zeroOutLayoutRecursively(yoga::Node* const node) { node->getLayout() = {}; node->setLayoutDimension(0, YGDimensionWidth); node->setLayoutDimension(0, YGDimensionHeight); node->setHasNewLayout(true); - node->cloneChildrenIfNeeded(layoutContext); + node->cloneChildrenIfNeeded(); for (const auto child : node->getChildren()) { - zeroOutLayoutRecursively(child, layoutContext); + zeroOutLayoutRecursively(child); } } @@ -795,7 +781,6 @@ static float computeFlexBasisForChildren( const yoga::Config* const config, bool performLayout, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount) { float totalOuterFlexBasis = 0.0f; @@ -826,7 +811,7 @@ static float computeFlexBasisForChildren( for (auto child : children) { child->resolveDimension(); if (child->getStyle().display() == YGDisplayNone) { - zeroOutLayoutRecursively(child, layoutContext); + zeroOutLayoutRecursively(child); child->setHasNewLayout(true); child->setDirty(false); continue; @@ -861,7 +846,6 @@ static float computeFlexBasisForChildren( direction, config, layoutMarkerData, - layoutContext, depth, generationCount); } @@ -894,7 +878,6 @@ static float distributeFreeSpaceSecondPass( const bool performLayout, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount) { float childFlexBasis = 0; @@ -1059,7 +1042,6 @@ static float distributeFreeSpaceSecondPass( : LayoutPassReason::kFlexMeasure, config, layoutMarkerData, - layoutContext, depth, generationCount); node->setLayoutHadOverflow( @@ -1192,7 +1174,6 @@ static void resolveFlexibleLength( const bool performLayout, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount) { const float originalFreeSpace = flexLine.layout.remainingFreeSpace; @@ -1220,7 +1201,6 @@ static void resolveFlexibleLength( performLayout, config, layoutMarkerData, - layoutContext, depth, generationCount); @@ -1240,8 +1220,7 @@ static void YGJustifyMainAxis( const float availableInnerMainDim, const float availableInnerCrossDim, const float availableInnerWidth, - const bool performLayout, - void* const layoutContext) { + const bool performLayout) { const auto& style = node->getStyle(); const float leadingPaddingAndBorderMain = node->getLeadingPaddingAndBorder(mainAxis, ownerWidth).unwrap(); @@ -1400,7 +1379,7 @@ static void YGJustifyMainAxis( if (isNodeBaselineLayout) { // If the child is baseline aligned then the cross dimension is // calculated by adding maxAscent and maxDescent from the baseline. - const float ascent = calculateBaseline(child, layoutContext) + + const float ascent = calculateBaseline(child) + child ->getLeadingMargin( YGFlexDirectionColumn, availableInnerWidth) @@ -1519,7 +1498,6 @@ static void calculateLayoutImpl( const bool performLayout, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, const uint32_t depth, const uint32_t generationCount, const LayoutPassReason reason) { @@ -1597,7 +1575,6 @@ static void calculateLayoutImpl( ownerWidth, ownerHeight, layoutMarkerData, - layoutContext, reason); return; } @@ -1631,7 +1608,7 @@ static void calculateLayoutImpl( // At this point we know we're going to perform work. Ensure that each child // has a mutable copy. - node->cloneChildrenIfNeeded(layoutContext); + node->cloneChildrenIfNeeded(); // Reset layout flags, as they could have changed. node->setLayoutHadOverflow(false); @@ -1699,7 +1676,6 @@ static void calculateLayoutImpl( config, performLayout, layoutMarkerData, - layoutContext, depth, generationCount); @@ -1838,7 +1814,6 @@ static void calculateLayoutImpl( performLayout, config, layoutMarkerData, - layoutContext, depth, generationCount); } @@ -1867,8 +1842,7 @@ static void calculateLayoutImpl( availableInnerMainDim, availableInnerCrossDim, availableInnerWidth, - performLayout, - layoutContext); + performLayout); float containerCrossAxis = availableInnerCrossDim; if (measureModeCrossDim == YGMeasureModeUndefined || @@ -2015,7 +1989,6 @@ static void calculateLayoutImpl( LayoutPassReason::kStretch, config, layoutMarkerData, - layoutContext, depth, generationCount); } @@ -2127,7 +2100,7 @@ static void calculateLayoutImpl( .unwrap()); } if (resolveChildAlignment(node, child) == YGAlignBaseline) { - const float ascent = calculateBaseline(child, layoutContext) + + const float ascent = calculateBaseline(child) + child ->getLeadingMargin( YGFlexDirectionColumn, availableInnerWidth) @@ -2233,7 +2206,6 @@ static void calculateLayoutImpl( LayoutPassReason::kMultilineStretch, config, layoutMarkerData, - layoutContext, depth, generationCount); } @@ -2243,7 +2215,7 @@ static void calculateLayoutImpl( case YGAlignBaseline: { child->setLayoutPosition( currentLead + maxAscentForCurrentLine - - calculateBaseline(child, layoutContext) + + calculateBaseline(child) + child ->getLeadingPosition( YGFlexDirectionColumn, availableInnerCrossDim) @@ -2384,7 +2356,6 @@ static void calculateLayoutImpl( direction, config, layoutMarkerData, - layoutContext, depth, generationCount); } @@ -2465,7 +2436,6 @@ bool calculateLayoutInternal( const LayoutPassReason reason, const yoga::Config* const config, LayoutData& layoutMarkerData, - void* const layoutContext, uint32_t depth, const uint32_t generationCount) { LayoutResults* layout = &node->getLayout(); @@ -2577,15 +2547,13 @@ bool calculateLayoutInternal( yoga::log( node, YGLogLevelVerbose, - nullptr, "%s%d.{[skipped] ", spacerWithLength(depth), depth); - node->print(layoutContext); + node->print(); yoga::log( node, YGLogLevelVerbose, - nullptr, "wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n", measureModeName(widthMeasureMode, performLayout), measureModeName(heightMeasureMode, performLayout), @@ -2600,16 +2568,14 @@ bool calculateLayoutInternal( yoga::log( node, YGLogLevelVerbose, - nullptr, "%s%d.{%s", spacerWithLength(depth), depth, needToVisitNode ? "*" : ""); - node->print(layoutContext); + node->print(); yoga::log( node, YGLogLevelVerbose, - nullptr, "wm: %s, hm: %s, aw: %f ah: %f %s\n", measureModeName(widthMeasureMode, performLayout), measureModeName(heightMeasureMode, performLayout), @@ -2630,7 +2596,6 @@ bool calculateLayoutInternal( performLayout, config, layoutMarkerData, - layoutContext, depth, generationCount, reason); @@ -2639,16 +2604,14 @@ bool calculateLayoutInternal( yoga::log( node, YGLogLevelVerbose, - nullptr, "%s%d.}%s", spacerWithLength(depth), depth, needToVisitNode ? "*" : ""); - node->print(layoutContext); + node->print(); yoga::log( node, YGLogLevelVerbose, - nullptr, "wm: %s, hm: %s, d: (%f, %f) %s\n", measureModeName(widthMeasureMode, performLayout), measureModeName(heightMeasureMode, performLayout), @@ -2668,8 +2631,7 @@ bool calculateLayoutInternal( if (layout->nextCachedMeasurementsIndex == LayoutResults::MaxCachedMeasurements) { if (gPrintChanges) { - yoga::log( - node, YGLogLevelVerbose, nullptr, "Out of cache entries!\n"); + yoga::log(node, YGLogLevelVerbose, "Out of cache entries!\n"); } layout->nextCachedMeasurementsIndex = 0; } @@ -2719,7 +2681,7 @@ bool calculateLayoutInternal( layoutType = cachedResults != nullptr ? LayoutType::kCachedMeasure : LayoutType::kMeasure; } - Event::publish(node, {layoutType, layoutContext}); + Event::publish(node, {layoutType}); return (needToVisitNode || cachedResults == nullptr); } @@ -2728,9 +2690,8 @@ void calculateLayout( yoga::Node* const node, const float ownerWidth, const float ownerHeight, - const YGDirection ownerDirection, - void* layoutContext) { - Event::publish(node, {layoutContext}); + const YGDirection ownerDirection) { + Event::publish(node); LayoutData markerData = {}; // Increment the generation count. This will force the recursive routine to @@ -2791,7 +2752,6 @@ void calculateLayout( LayoutPassReason::kInitial, node->getConfig(), markerData, - layoutContext, 0, // tree root gCurrentGenerationCount.load(std::memory_order_relaxed))) { node->setPosition( @@ -2808,7 +2768,7 @@ void calculateLayout( #endif } - Event::publish(node, {layoutContext, &markerData}); + Event::publish(node, {&markerData}); } } // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h index e12dcc0dda4a40..b93dd5da37ef09 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.h @@ -16,7 +16,6 @@ void calculateLayout( yoga::Node* const node, const float ownerWidth, const float ownerHeight, - const YGDirection ownerDirection, - void* layoutContext); + const YGDirection ownerDirection); } // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index f3b28ffae01977..e86c76017bb488 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp @@ -91,56 +91,28 @@ void* Config::getContext() const { } void Config::setLogger(YGLogger logger) { - logger_.noContext = logger; - flags_.loggerUsesContext = false; -} - -void Config::setLogger(LogWithContextFn logger) { - logger_.withContext = logger; - flags_.loggerUsesContext = true; -} - -void Config::setLogger(std::nullptr_t) { - setLogger(YGLogger{nullptr}); + logger_ = logger; } void Config::log( const yoga::Node* node, YGLogLevel logLevel, - void* logContext, const char* format, va_list args) const { - if (flags_.loggerUsesContext) { - logger_.withContext(this, node, logLevel, logContext, format, args); - } else { - logger_.noContext(this, node, logLevel, format, args); - } + logger_(this, node, logLevel, format, args); } void Config::setCloneNodeCallback(YGCloneNodeFunc cloneNode) { - cloneNodeCallback_.noContext = cloneNode; - flags_.cloneNodeUsesContext = false; -} - -void Config::setCloneNodeCallback(CloneWithContextFn cloneNode) { - cloneNodeCallback_.withContext = cloneNode; - flags_.cloneNodeUsesContext = true; -} - -void Config::setCloneNodeCallback(std::nullptr_t) { - setCloneNodeCallback(YGCloneNodeFunc{nullptr}); + cloneNodeCallback_ = cloneNode; } YGNodeRef Config::cloneNode( YGNodeConstRef node, YGNodeConstRef owner, - size_t childIndex, - void* cloneContext) const { + size_t childIndex) const { YGNodeRef clone = nullptr; - if (cloneNodeCallback_.noContext != nullptr) { - clone = flags_.cloneNodeUsesContext - ? cloneNodeCallback_.withContext(node, owner, childIndex, cloneContext) - : cloneNodeCallback_.noContext(node, owner, childIndex); + if (cloneNodeCallback_ != nullptr) { + clone = cloneNodeCallback_(node, owner, childIndex); } if (clone == nullptr) { clone = YGNodeClone(node); diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index dfea29c588d681..67a0299f712d2f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -24,29 +24,12 @@ bool configUpdateInvalidatesLayout( const Config& oldConfig, const Config& newConfig); -// Internal variants of log functions, currently used only by JNI bindings. -// TODO: Reconcile this with the public API -using LogWithContextFn = int (*)( - YGConfigConstRef config, - YGNodeConstRef node, - YGLogLevel level, - void* context, - const char* format, - va_list args); -using CloneWithContextFn = YGNodeRef (*)( - YGNodeConstRef node, - YGNodeConstRef owner, - size_t childIndex, - void* cloneContext); - #pragma pack(push) #pragma pack(1) // Packed structure of <32-bit options to miminize size per node. struct ConfigFlags { bool useWebDefaults : 1; bool printTree : 1; - bool cloneNodeUsesContext : 1; - bool loggerUsesContext : 1; }; #pragma pack(pop) @@ -79,35 +62,23 @@ class YG_EXPORT Config : public ::YGConfig { void* getContext() const; void setLogger(YGLogger logger); - void setLogger(LogWithContextFn logger); - void setLogger(std::nullptr_t); void log( const yoga::Node* node, YGLogLevel logLevel, - void* logContext, const char* format, va_list args) const; void setCloneNodeCallback(YGCloneNodeFunc cloneNode); - void setCloneNodeCallback(CloneWithContextFn cloneNode); - void setCloneNodeCallback(std::nullptr_t); YGNodeRef cloneNode( YGNodeConstRef node, YGNodeConstRef owner, - size_t childIndex, - void* cloneContext) const; + size_t childIndex) const; static const Config& getDefault(); private: - union { - CloneWithContextFn withContext; - YGCloneNodeFunc noContext; - } cloneNodeCallback_; - union { - LogWithContextFn withContext; - YGLogger noContext; - } logger_; + YGCloneNodeFunc cloneNodeCallback_; + YGLogger logger_; ConfigFlags flags_{}; EnumBitset experimentalFeatures_{}; diff --git a/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp b/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp index bc85235a8555c2..d83c8cc56e857d 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp @@ -22,7 +22,7 @@ namespace facebook::yoga { void assertFatal(const bool condition, const char* message) { if (!condition) { - yoga::log(YGLogLevelFatal, nullptr, "%s\n", message); + yoga::log(YGLogLevelFatal, "%s\n", message); fatalWithMessage(message); } } @@ -32,7 +32,7 @@ void assertFatalWithNode( const bool condition, const char* message) { if (!condition) { - yoga::log(node, YGLogLevelFatal, nullptr, "%s\n", message); + yoga::log(node, YGLogLevelFatal, "%s\n", message); fatalWithMessage(message); } } @@ -42,7 +42,7 @@ void assertFatalWithConfig( const bool condition, const char* message) { if (!condition) { - yoga::log(config, YGLogLevelFatal, nullptr, "%s\n", message); + yoga::log(config, YGLogLevelFatal, "%s\n", message); fatalWithMessage(message); } } diff --git a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp index 675c1ebd4c21e6..c16547ae345675 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp @@ -19,51 +19,43 @@ void vlog( const yoga::Config* config, const yoga::Node* node, YGLogLevel level, - void* context, const char* format, va_list args) { if (config == nullptr) { getDefaultLogger()(nullptr, node, level, format, args); } else { - config->log(node, level, context, format, args); + config->log(node, level, format, args); } } } // namespace -void log(YGLogLevel level, void* context, const char* format, ...) noexcept { +void log(YGLogLevel level, const char* format, ...) noexcept { va_list args; va_start(args, format); - vlog(nullptr, nullptr, level, context, format, args); + vlog(nullptr, nullptr, level, format, args); va_end(args); } void log( const yoga::Node* node, YGLogLevel level, - void* context, const char* format, ...) noexcept { va_list args; va_start(args, format); vlog( - node == nullptr ? nullptr : node->getConfig(), - node, - level, - context, - format, - args); + node == nullptr ? nullptr : node->getConfig(), node, level, format, args); va_end(args); } void log( const yoga::Config* config, YGLogLevel level, - void* context, const char* format, ...) noexcept { va_list args; va_start(args, format); - vlog(config, nullptr, level, context, format, args); + vlog(config, nullptr, level, format, args); va_end(args); } diff --git a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h index 54f10c4b044d8d..a595eba82403d3 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h +++ b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h @@ -15,19 +15,17 @@ namespace facebook::yoga { -void log(YGLogLevel level, void*, const char* format, ...) noexcept; +void log(YGLogLevel level, const char* format, ...) noexcept; void log( const yoga::Node* node, YGLogLevel level, - void*, const char* message, ...) noexcept; void log( const yoga::Config* config, YGLogLevel level, - void*, const char* format, ...) noexcept; diff --git a/packages/react-native/ReactCommon/yoga/yoga/event/event.h b/packages/react-native/ReactCommon/yoga/yoga/event/event.h index 333595403223ca..2969f26d02195d 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/event/event.h +++ b/packages/react-native/ReactCommon/yoga/yoga/event/event.h @@ -103,20 +103,13 @@ struct Event::TypedData { YGConfigConstRef config; }; -template <> -struct Event::TypedData { - void* layoutContext; -}; - template <> struct Event::TypedData { - void* layoutContext; LayoutData* layoutData; }; template <> struct Event::TypedData { - void* layoutContext; float width; YGMeasureMode widthMeasureMode; float height; @@ -129,7 +122,6 @@ struct Event::TypedData { template <> struct Event::TypedData { LayoutType layoutType; - void* layoutContext; }; } // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp index ac6a5b88f720dc..da3438d2f3fab1 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp @@ -32,10 +32,10 @@ Node::Node(const yoga::Config* config) : config_{config} { Node::Node(Node&& node) { context_ = node.context_; flags_ = node.flags_; - measure_ = node.measure_; - baseline_ = node.baseline_; - print_ = node.print_; - dirtied_ = node.dirtied_; + measureFunc_ = node.measureFunc_; + baselineFunc_ = node.baselineFunc_; + printFunc_ = node.printFunc_; + dirtiedFunc_ = node.dirtiedFunc_; style_ = node.style_; layout_ = node.layout_; lineIndex_ = node.lineIndex_; @@ -48,13 +48,9 @@ Node::Node(Node&& node) { } } -void Node::print(void* printContext) { - if (print_.noContext != nullptr) { - if (flags_.printUsesContext) { - print_.withContext(this, printContext); - } else { - print_.noContext(this); - } +void Node::print() { + if (printFunc_ != nullptr) { + printFunc_(this); } } @@ -217,25 +213,18 @@ YGSize Node::measure( float width, YGMeasureMode widthMode, float height, - YGMeasureMode heightMode, - void* layoutContext) { - return flags_.measureUsesContext - ? measure_.withContext( - this, width, widthMode, height, heightMode, layoutContext) - : measure_.noContext(this, width, widthMode, height, heightMode); + YGMeasureMode heightMode) { + return measureFunc_(this, width, widthMode, height, heightMode); } -float Node::baseline(float width, float height, void* layoutContext) const { - return flags_.baselineUsesContext - ? baseline_.withContext( - const_cast(this), width, height, layoutContext) - : baseline_.noContext(const_cast(this), width, height); +float Node::baseline(float width, float height) const { + return baselineFunc_(this, width, height); } // Setters -void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) { - if (measureFunc.noContext == nullptr) { +void Node::setMeasureFunc(YGMeasureFunc measureFunc) { + if (measureFunc == nullptr) { // TODO: t18095186 Move nodeType to opt-in function and mark appropriate // places in Litho setNodeType(YGNodeTypeDefault); @@ -250,21 +239,7 @@ void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) { setNodeType(YGNodeTypeText); } - measure_ = measureFunc; -} - -void Node::setMeasureFunc(YGMeasureFunc measureFunc) { - flags_.measureUsesContext = false; - decltype(Node::measure_) m; - m.noContext = measureFunc; - setMeasureFunc(m); -} - -void Node::setMeasureFunc(MeasureWithContextFn measureFunc) { - flags_.measureUsesContext = true; - decltype(Node::measure_) m; - m.withContext = measureFunc; - setMeasureFunc(m); + measureFunc_ = measureFunc; } void Node::replaceChild(Node* child, size_t index) { @@ -299,8 +274,8 @@ void Node::setDirty(bool isDirty) { return; } flags_.isDirty = isDirty; - if (isDirty && dirtied_) { - dirtied_(this); + if (isDirty && dirtiedFunc_) { + dirtiedFunc_(this); } } @@ -485,11 +460,11 @@ void Node::clearChildren() { // Other Methods -void Node::cloneChildrenIfNeeded(void* cloneContext) { +void Node::cloneChildrenIfNeeded() { size_t i = 0; for (Node*& child : children_) { if (child->getOwner() != this) { - child = resolveRef(config_->cloneNode(child, this, i, cloneContext)); + child = resolveRef(config_->cloneNode(child, this, i)); child->setOwner(this); } i += 1; diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index e7491791226ac3..37f18a5a708cc2 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -30,42 +30,17 @@ struct NodeFlags { bool isReferenceBaseline : 1; bool isDirty : 1; uint32_t nodeType : 1; - bool measureUsesContext : 1; - bool baselineUsesContext : 1; - bool printUsesContext : 1; }; #pragma pack(pop) class YG_EXPORT Node : public ::YGNode { -public: - // Internal variants of callbacks, currently used only by JNI bindings. - // TODO: Reconcile this with the public API - using MeasureWithContextFn = YGSize (*)( - YGNodeConstRef, - float, - YGMeasureMode, - float, - YGMeasureMode, - void*); - using BaselineWithContextFn = float (*)(YGNodeConstRef, float, float, void*); - using PrintWithContextFn = void (*)(YGNodeConstRef, void*); - private: void* context_ = nullptr; NodeFlags flags_ = {}; - union { - YGMeasureFunc noContext; - MeasureWithContextFn withContext; - } measure_ = {nullptr}; - union { - YGBaselineFunc noContext; - BaselineWithContextFn withContext; - } baseline_ = {nullptr}; - union { - YGPrintFunc noContext; - PrintWithContextFn withContext; - } print_ = {nullptr}; - YGDirtiedFunc dirtied_ = nullptr; + YGMeasureFunc measureFunc_ = {nullptr}; + YGBaselineFunc baselineFunc_ = {nullptr}; + YGPrintFunc printFunc_ = {nullptr}; + YGDirtiedFunc dirtiedFunc_ = nullptr; Style style_ = {}; LayoutResults layout_ = {}; size_t lineIndex_ = 0; @@ -79,9 +54,6 @@ class YG_EXPORT Node : public ::YGNode { const YGFlexDirection axis, const float axisSize) const; - void setMeasureFunc(decltype(measure_)); - void setBaselineFunc(decltype(baseline_)); - void useWebDefaults() { style_.flexDirection() = YGFlexDirectionRow; style_.alignContent() = YGAlignStretch; @@ -112,7 +84,7 @@ class YG_EXPORT Node : public ::YGNode { // Getters void* getContext() const { return context_; } - void print(void*); + void print(); bool getHasNewLayout() const { return flags_.hasNewLayout; } @@ -120,19 +92,17 @@ class YG_EXPORT Node : public ::YGNode { return static_cast(flags_.nodeType); } - bool hasMeasureFunc() const noexcept { return measure_.noContext != nullptr; } + bool hasMeasureFunc() const noexcept { return measureFunc_ != nullptr; } - YGSize measure(float, YGMeasureMode, float, YGMeasureMode, void*); + YGSize measure(float, YGMeasureMode, float, YGMeasureMode); - bool hasBaselineFunc() const noexcept { - return baseline_.noContext != nullptr; - } + bool hasBaselineFunc() const noexcept { return baselineFunc_ != nullptr; } - float baseline(float width, float height, void* layoutContext) const; + float baseline(float width, float height) const; bool hasErrata(YGErrata errata) const { return config_->hasErrata(errata); } - YGDirtiedFunc getDirtied() const { return dirtied_; } + YGDirtiedFunc getDirtiedFunc() const { return dirtiedFunc_; } // For Performance reasons passing as reference. Style& getStyle() { return style_; } @@ -232,15 +202,7 @@ class YG_EXPORT Node : public ::YGNode { void setContext(void* context) { context_ = context; } - void setPrintFunc(YGPrintFunc printFunc) { - print_.noContext = printFunc; - flags_.printUsesContext = false; - } - void setPrintFunc(PrintWithContextFn printFunc) { - print_.withContext = printFunc; - flags_.printUsesContext = true; - } - void setPrintFunc(std::nullptr_t) { setPrintFunc(YGPrintFunc{nullptr}); } + void setPrintFunc(YGPrintFunc printFunc) { printFunc_ = printFunc; } void setHasNewLayout(bool hasNewLayout) { flags_.hasNewLayout = hasNewLayout; @@ -251,24 +213,12 @@ class YG_EXPORT Node : public ::YGNode { } void setMeasureFunc(YGMeasureFunc measureFunc); - void setMeasureFunc(MeasureWithContextFn); - void setMeasureFunc(std::nullptr_t) { - return setMeasureFunc(YGMeasureFunc{nullptr}); - } void setBaselineFunc(YGBaselineFunc baseLineFunc) { - flags_.baselineUsesContext = false; - baseline_.noContext = baseLineFunc; - } - void setBaselineFunc(BaselineWithContextFn baseLineFunc) { - flags_.baselineUsesContext = true; - baseline_.withContext = baseLineFunc; - } - void setBaselineFunc(std::nullptr_t) { - return setBaselineFunc(YGBaselineFunc{nullptr}); + baselineFunc_ = baseLineFunc; } - void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtied_ = dirtiedFunc; } + void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtiedFunc_ = dirtiedFunc; } void setStyle(const Style& style) { style_ = style; } @@ -325,7 +275,7 @@ class YG_EXPORT Node : public ::YGNode { bool removeChild(Node* child); void removeChild(size_t index); - void cloneChildrenIfNeeded(void*); + void cloneChildrenIfNeeded(); void markDirtyAndPropagate(); float resolveFlexGrow() const; float resolveFlexShrink() const;