Dirty nodes when dynamically setting config (#37207)

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

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

Yoga exposes public APIs for dirtying Nodes, but will itself perform dirty marking when changing bits which invalidate layout. E.g. changing the style of a Node will invalidate it along with every parent Node.

Because config setting is newly public to the C ABI, this makes a similar change so that replacing a Node's config will dirty the tree above the node if there is a layout impacting config change (I don't think children need to be invalidated since child output shouldn't change given the same owner dimensions).

One quirk of this is that configs may be changed independently of the node. So someone could attach a config to a Node, then change the live config after the fact. The config does not currently have a back pointer to the Node, so we do not invalidate in that case of live config edits. The future work to rectify this would be to make configs immutable once created.

There are also currently some experimental features here which should maybe be compared, but these should be moved to YGErrata anyway.

Reviewed By: javache

Differential Revision: D45505089

fbshipit-source-id: 72b2b84ba758679af081d92e7403750c9cc53cb5
This commit is contained in:
Nick Gerleman
2023-05-07 00:34:02 -07:00
committed by Facebook GitHub Bot
parent 54d78926ce
commit b8126cdc6c
4 changed files with 131 additions and 11 deletions

View File

@@ -68,6 +68,99 @@ TEST(YogaTest, dirty_propagation_only_if_prop_changed) {
YGNodeFreeRecursive(root);
}
TEST(YogaTest, dirty_propagation_changing_layout_config) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
const YGNodeRef root_child0_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0_child0, 25);
YGNodeStyleSetHeight(root_child0_child0, 20);
YGNodeInsertChild(root, root_child0_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_FALSE(root->isDirty());
EXPECT_FALSE(root_child0->isDirty());
EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(root_child0_child0->isDirty());
YGConfigRef newConfig = YGConfigNew();
YGConfigSetErrata(newConfig, YGErrataStretchFlexBasis);
YGNodeSetConfig(root_child0, newConfig);
EXPECT_TRUE(root->isDirty());
EXPECT_TRUE(root_child0->isDirty());
EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(root_child0_child0->isDirty());
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_FALSE(root->isDirty());
EXPECT_FALSE(root_child0->isDirty());
EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(root_child0_child0->isDirty());
YGConfigFree(newConfig);
YGNodeFreeRecursive(root);
}
TEST(YogaTest, dirty_propagation_changing_benign_config) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
const YGNodeRef root_child0_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0_child0, 25);
YGNodeStyleSetHeight(root_child0_child0, 20);
YGNodeInsertChild(root, root_child0_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_FALSE(root->isDirty());
EXPECT_FALSE(root_child0->isDirty());
EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(root_child0_child0->isDirty());
YGConfigRef newConfig = YGConfigNew();
YGConfigSetLogger(
newConfig,
[](const YGConfigRef, const YGNodeRef, YGLogLevel, const char*, va_list) {
return 0;
});
YGNodeSetConfig(root_child0, newConfig);
EXPECT_FALSE(root->isDirty());
EXPECT_FALSE(root_child0->isDirty());
EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(root_child0_child0->isDirty());
YGConfigFree(newConfig);
YGNodeFreeRecursive(root);
}
TEST(YogaTest, dirty_mark_all_children_as_dirty_when_display_changes) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);