Remove layoutContext Drilling (#1376)

Summary:
X-link: https://github.com/facebook/react-native/pull/39401

Pull Request resolved: https://github.com/facebook/yoga/pull/1376

kill_with_fire_flamethrower

Reviewed By: rshest

Differential Revision: D49179244

fbshipit-source-id: 9a827e1bd29205254fee5725449191726d6bcf5a
This commit is contained in:
Nick Gerleman
2023-09-12 19:08:55 -07:00
committed by Facebook GitHub Bot
parent b1e0140aaa
commit 0a90b16ac6
17 changed files with 83 additions and 370 deletions

View File

@@ -24,7 +24,6 @@ struct TypedEventTestData {};
template <>
struct TypedEventTestData<Event::LayoutPassEnd> {
void* layoutContext;
LayoutData layoutData;
};
@@ -329,7 +328,7 @@ void EventTest::listen(
case Event::LayoutPassEnd: {
auto& eventData = data.get<Event::LayoutPassEnd>();
events.push_back(createArgs<Event::LayoutPassEnd>(
node, data, {eventData.layoutContext, *eventData.layoutData}));
node, data, {*eventData.layoutData}));
break;
}

View File

@@ -33,7 +33,7 @@ TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);
yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
auto clone = config->cloneNode(&node, &owner, 0);
ASSERT_EQ(clone, &clonedNode);
}
@@ -44,22 +44,12 @@ TEST_F(
config->setCloneNodeCallback(doNotClone);
yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
auto clone = config->cloneNode(&node, &owner, 0);
ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}
TEST_F(ConfigCloningTest, can_clone_with_context) {
config->setCloneNodeCallback(
[](YGNodeConstRef, YGNodeConstRef, size_t, void* context) {
return (YGNodeRef) context;
});
yoga::Node node{}, owner{}, clone{};
ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone);
}
void ConfigCloningTest::SetUp() {
config = {static_cast<yoga::Config*>(YGConfigNew()), YGConfigFree};
}

View File

@@ -38,38 +38,7 @@ TEST(Node, measure_with_measure_fn) {
});
ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
(YGSize{23, 12}));
}
TEST(Node, measure_with_context_measure_fn) {
auto n = Node{};
n.setMeasureFunc([](YGNodeConstRef,
float,
YGMeasureMode,
float,
YGMeasureMode,
void* ctx) { return *(YGSize*) ctx; });
auto result = YGSize{123.4f, -56.7f};
ASSERT_EQ(
n.measure(0, YGMeasureModeUndefined, 0, YGMeasureModeUndefined, &result),
result);
}
TEST(Node, switching_measure_fn_types) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNodeConstRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * static_cast<float>(wm), h / static_cast<float>(hm)};
});
ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost),
(YGSize{23, 12}));
}
@@ -84,17 +53,6 @@ TEST(Node, hasMeasureFunc_after_unset) {
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(Node, hasMeasureFunc_after_unset_context) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(Node, hasBaselineFunc_initial) {
auto n = Node{};
ASSERT_FALSE(n.hasBaselineFunc());
@@ -110,17 +68,7 @@ TEST(Node, baseline_with_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float w, float h) { return w + h; });
ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f);
}
TEST(Node, baseline_with_context_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float w, float h, void* ctx) {
return w + h + *(float*) ctx;
});
auto ctx = -10.0f;
ASSERT_EQ(n.baseline(1.25f, 2.5f, &ctx), -6.25f);
ASSERT_EQ(n.baseline(1.25f, 2.5f), 3.75f);
}
TEST(Node, hasBaselineFunc_after_unset) {
@@ -130,18 +78,3 @@ TEST(Node, hasBaselineFunc_after_unset) {
n.setBaselineFunc(nullptr);
ASSERT_FALSE(n.hasBaselineFunc());
}
TEST(Node, hasBaselineFunc_after_unset_context) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(Node, switching_baseline_fn_types) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float) { return 1.0f; });
ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f);
}

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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<Event::NodeBaselineStart>(node);
const float baseline = node->baseline(
node->getLayout().measuredDimensions[YGDimensionWidth],
node->getLayout().measuredDimensions[YGDimensionHeight],
layoutContext);
node->getLayout().measuredDimensions[YGDimensionHeight]);
Event::publish<Event::NodeBaselineEnd>(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];
}

View File

@@ -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);

View File

@@ -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<size_t>(reason)] +=
@@ -625,8 +614,7 @@ static void measureNodeWithMeasureFunc(
Event::publish<Event::MeasureCallbackEnd>(
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<Event::NodeLayout>(node, {layoutType, layoutContext});
Event::publish<Event::NodeLayout>(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<Event::LayoutPassStart>(node, {layoutContext});
const YGDirection ownerDirection) {
Event::publish<Event::LayoutPassStart>(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<Event::LayoutPassEnd>(node, {layoutContext, &markerData});
Event::publish<Event::LayoutPassEnd>(node, {&markerData});
}
} // namespace facebook::yoga

View File

@@ -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

View File

@@ -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);

View File

@@ -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 @@ public:
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<YGExperimentalFeature> experimentalFeatures_{};

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -103,20 +103,13 @@ struct Event::TypedData<Event::NodeDeallocation> {
YGConfigConstRef config;
};
template <>
struct Event::TypedData<Event::LayoutPassStart> {
void* layoutContext;
};
template <>
struct Event::TypedData<Event::LayoutPassEnd> {
void* layoutContext;
LayoutData* layoutData;
};
template <>
struct Event::TypedData<Event::MeasureCallbackEnd> {
void* layoutContext;
float width;
YGMeasureMode widthMeasureMode;
float height;
@@ -129,7 +122,6 @@ struct Event::TypedData<Event::MeasureCallbackEnd> {
template <>
struct Event::TypedData<Event::NodeLayout> {
LayoutType layoutType;
void* layoutContext;
};
} // namespace facebook::yoga

View File

@@ -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<Node*>(this), width, height, layoutContext)
: baseline_.noContext(const_cast<Node*>(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;

View File

@@ -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 @@ private:
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 @@ public:
// Getters
void* getContext() const { return context_; }
void print(void*);
void print();
bool getHasNewLayout() const { return flags_.hasNewLayout; }
@@ -120,19 +92,17 @@ public:
return static_cast<YGNodeType>(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 @@ public:
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 @@ public:
}
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 @@ public:
bool removeChild(Node* child);
void removeChild(size_t index);
void cloneChildrenIfNeeded(void*);
void cloneChildrenIfNeeded();
void markDirtyAndPropagate();
float resolveFlexGrow() const;
float resolveFlexShrink() const;