From 071b63963957aaa906f4a4edd74aee8ecbc58592 Mon Sep 17 00:00:00 2001 From: Joe Vilches Date: Fri, 2 Feb 2024 15:44:23 -0800 Subject: [PATCH] Move NodeToString.cpp to benchmark and remove interal usages Summary: X-link: https://github.com/facebook/react-native/pull/42710 X-link: https://github.com/facebook/yoga/pull/1568 This is no longer going to be built with normal yoga so we are going to switch to a public API Reviewed By: NickGerleman Differential Revision: D53141235 fbshipit-source-id: 259270a4cd91ef0dab91cefba9c41953b6340d78 --- .../cpp/yoga/algorithm/CalculateLayout.cpp | 9 - lib/yoga/src/main/cpp/yoga/config/Config.cpp | 8 - lib/yoga/src/main/cpp/yoga/config/Config.h | 4 - .../src/main/cpp/yoga/debug/NodeToString.cpp | 210 ------------------ .../src/main/cpp/yoga/debug/NodeToString.h | 29 --- 5 files changed, 260 deletions(-) delete mode 100644 lib/yoga/src/main/cpp/yoga/debug/NodeToString.cpp delete mode 100644 lib/yoga/src/main/cpp/yoga/debug/NodeToString.h diff --git a/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp b/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp index e1a280401f6..9b214cf587f 100644 --- a/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp +++ b/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -2366,14 +2365,6 @@ void calculateLayout( node->setPosition( node->getLayout().direction(), ownerWidth, ownerHeight, ownerWidth); roundLayoutResultsToPixelGrid(node, 0.0f, 0.0f); - -#ifdef DEBUG - if (node->getConfig()->shouldPrintTree()) { - yoga::print( - node, - PrintOptions::Layout | PrintOptions::Children | PrintOptions::Style); - } -#endif } Event::publish(node, {&markerData}); diff --git a/lib/yoga/src/main/cpp/yoga/config/Config.cpp b/lib/yoga/src/main/cpp/yoga/config/Config.cpp index b717d974d15..607e5774706 100644 --- a/lib/yoga/src/main/cpp/yoga/config/Config.cpp +++ b/lib/yoga/src/main/cpp/yoga/config/Config.cpp @@ -32,14 +32,6 @@ bool Config::useWebDefaults() const { return useWebDefaults_; } -void Config::setShouldPrintTree(bool printTree) { - printTree_ = printTree; -} - -bool Config::shouldPrintTree() const { - return printTree_; -} - void Config::setExperimentalFeatureEnabled( ExperimentalFeature feature, bool enabled) { diff --git a/lib/yoga/src/main/cpp/yoga/config/Config.h b/lib/yoga/src/main/cpp/yoga/config/Config.h index 1bdc0469cff..e902bd0d1b9 100644 --- a/lib/yoga/src/main/cpp/yoga/config/Config.h +++ b/lib/yoga/src/main/cpp/yoga/config/Config.h @@ -37,9 +37,6 @@ class YG_EXPORT Config : public ::YGConfig { void setUseWebDefaults(bool useWebDefaults); bool useWebDefaults() const; - void setShouldPrintTree(bool printTree); - bool shouldPrintTree() const; - void setExperimentalFeatureEnabled(ExperimentalFeature feature, bool enabled); bool isExperimentalFeatureEnabled(ExperimentalFeature feature) const; ExperimentalFeatureSet getEnabledExperiments() const; @@ -74,7 +71,6 @@ class YG_EXPORT Config : public ::YGConfig { YGLogger logger_; bool useWebDefaults_ : 1 = false; - bool printTree_ : 1 = false; ExperimentalFeatureSet experimentalFeatures_{}; Errata errata_ = Errata::None; diff --git a/lib/yoga/src/main/cpp/yoga/debug/NodeToString.cpp b/lib/yoga/src/main/cpp/yoga/debug/NodeToString.cpp deleted file mode 100644 index 39264feab7b..00000000000 --- a/lib/yoga/src/main/cpp/yoga/debug/NodeToString.cpp +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#ifdef DEBUG - -#include - -#include -#include -#include - -namespace facebook::yoga { - -static void indent(std::string& base, uint32_t level) { - for (uint32_t i = 0; i < level; ++i) { - base.append(" "); - } -} - -static void appendFormattedString(std::string& str, const char* fmt, ...) { - va_list args; - va_start(args, fmt); - va_list argsCopy; - va_copy(argsCopy, args); - std::vector buf(1 + static_cast(vsnprintf(NULL, 0, fmt, args))); - va_end(args); - vsnprintf(buf.data(), buf.size(), fmt, argsCopy); - va_end(argsCopy); - std::string result = std::string(buf.begin(), buf.end() - 1); - str.append(result); -} - -static void appendFloatOptionalIfDefined( - std::string& base, - const std::string key, - const FloatOptional num) { - if (num.isDefined()) { - appendFormattedString(base, "%s: %g; ", key.c_str(), num.unwrap()); - } -} - -static void appendNumberIfNotUndefined( - std::string& base, - const std::string key, - const Style::Length& number) { - if (number.unit() != Unit::Undefined) { - if (number.unit() == Unit::Auto) { - base.append(key + ": auto; "); - } else { - std::string unit = number.unit() == Unit::Point ? "px" : "%%"; - appendFormattedString( - base, - "%s: %g%s; ", - key.c_str(), - number.value().unwrap(), - unit.c_str()); - } - } -} - -static void appendNumberIfNotAuto( - std::string& base, - const std::string& key, - const Style::Length& number) { - if (number.unit() != Unit::Auto) { - appendNumberIfNotUndefined(base, key, number); - } -} - -static void appendNumberIfNotZero( - std::string& base, - const std::string& str, - const Style::Length& number) { - if (number.unit() == Unit::Auto) { - base.append(str + ": auto; "); - } else if (!yoga::inexactEquals(number.value().unwrap(), 0)) { - appendNumberIfNotUndefined(base, str, number); - } -} - -template -static void -appendEdges(std::string& base, const std::string& key, const Style& style) { - for (auto edge : ordinals()) { - std::string str = key + "-" + toString(edge); - appendNumberIfNotZero(base, str, (style.*Field)(edge)); - } -} - -void nodeToString( - std::string& str, - const yoga::Node* node, - PrintOptions options, - uint32_t level) { - indent(str, level); - appendFormattedString(str, "
getLayout().dimension(Dimension::Width)); - appendFormattedString( - str, "height: %g; ", node->getLayout().dimension(Dimension::Height)); - appendFormattedString( - str, "top: %g; ", node->getLayout().position(PhysicalEdge::Top)); - appendFormattedString( - str, "left: %g;", node->getLayout().position(PhysicalEdge::Left)); - appendFormattedString(str, "\" "); - } - - if ((options & PrintOptions::Style) == PrintOptions::Style) { - appendFormattedString(str, "style=\""); - const auto& style = node->style(); - if (style.flexDirection() != yoga::Style{}.flexDirection()) { - appendFormattedString( - str, "flex-direction: %s; ", toString(style.flexDirection())); - } - if (style.justifyContent() != yoga::Style{}.justifyContent()) { - appendFormattedString( - str, "justify-content: %s; ", toString(style.justifyContent())); - } - if (style.alignItems() != yoga::Style{}.alignItems()) { - appendFormattedString( - str, "align-items: %s; ", toString(style.alignItems())); - } - if (style.alignContent() != yoga::Style{}.alignContent()) { - appendFormattedString( - str, "align-content: %s; ", toString(style.alignContent())); - } - if (style.alignSelf() != yoga::Style{}.alignSelf()) { - appendFormattedString( - str, "align-self: %s; ", toString(style.alignSelf())); - } - appendFloatOptionalIfDefined(str, "flex-grow", style.flexGrow()); - appendFloatOptionalIfDefined(str, "flex-shrink", style.flexShrink()); - appendNumberIfNotAuto(str, "flex-basis", style.flexBasis()); - appendFloatOptionalIfDefined(str, "flex", style.flex()); - - if (style.flexWrap() != yoga::Style{}.flexWrap()) { - appendFormattedString(str, "flex-wrap: %s; ", toString(style.flexWrap())); - } - - if (style.overflow() != yoga::Style{}.overflow()) { - appendFormattedString(str, "overflow: %s; ", toString(style.overflow())); - } - - if (style.display() != yoga::Style{}.display()) { - appendFormattedString(str, "display: %s; ", toString(style.display())); - } - appendEdges<&Style::margin>(str, "margin", style); - appendEdges<&Style::padding>(str, "padding", style); - appendEdges<&Style::border>(str, "border", style); - - if (style.gap(Gutter::All).isDefined()) { - appendNumberIfNotUndefined(str, "gap", style.gap(Gutter::All)); - } else { - appendNumberIfNotUndefined(str, "column-gap", style.gap(Gutter::Column)); - appendNumberIfNotUndefined(str, "row-gap", style.gap(Gutter::Row)); - } - - appendNumberIfNotAuto(str, "width", style.dimension(Dimension::Width)); - appendNumberIfNotAuto(str, "height", style.dimension(Dimension::Height)); - appendNumberIfNotAuto( - str, "max-width", style.maxDimension(Dimension::Width)); - appendNumberIfNotAuto( - str, "max-height", style.maxDimension(Dimension::Height)); - appendNumberIfNotAuto( - str, "min-width", style.minDimension(Dimension::Width)); - appendNumberIfNotAuto( - str, "min-height", style.minDimension(Dimension::Height)); - - if (style.positionType() != yoga::Style{}.positionType()) { - appendFormattedString( - str, "position: %s; ", toString(style.positionType())); - } - - appendEdges<&Style::position>(str, "position", style); - appendFormattedString(str, "\" "); - - if (node->hasMeasureFunc()) { - appendFormattedString(str, "has-custom-measure=\"true\""); - } - } - appendFormattedString(str, ">"); - - const size_t childCount = node->getChildCount(); - if ((options & PrintOptions::Children) == PrintOptions::Children && - childCount > 0) { - for (size_t i = 0; i < childCount; i++) { - appendFormattedString(str, "\n"); - nodeToString(str, node->getChild(i), options, level + 1); - } - appendFormattedString(str, "\n"); - indent(str, level); - } - appendFormattedString(str, "
"); -} - -void print(const yoga::Node* node, PrintOptions options) { - std::string str; - yoga::nodeToString(str, node, options, 0); - yoga::log(node, LogLevel::Debug, str.c_str()); -} - -} // namespace facebook::yoga -#endif diff --git a/lib/yoga/src/main/cpp/yoga/debug/NodeToString.h b/lib/yoga/src/main/cpp/yoga/debug/NodeToString.h deleted file mode 100644 index 268d5ac927d..00000000000 --- a/lib/yoga/src/main/cpp/yoga/debug/NodeToString.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#ifdef DEBUG - -#pragma once - -#include - -#include -#include - -namespace facebook::yoga { - -void nodeToString( - std::string& str, - const yoga::Node* node, - PrintOptions options, - uint32_t level); - -void print(const yoga::Node* node, PrintOptions options); - -} // namespace facebook::yoga - -#endif