Skip to content

Commit cbac506

Browse files
Bartlomiej Bloniarzfacebook-github-bot
authored andcommitted
Move ShadowNodeFamily to PropsAnimatedNode (#54879)
Summary: This diff is a part of the process of getting the Animated-itest to work with Animation Backend. During testing I found that sometimes the disconnect method would cleanup `tagToShadowNodeFamily_` when there were more than one PropsAnimatedNode for a view, so we would wrongly skip an animation. By storing the family pointer on the props node we can avoid that. Differential Revision: D89042963
1 parent 566984f commit cbac506

File tree

4 files changed

+95
-76
lines changed

4 files changed

+95
-76
lines changed

packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ void NativeAnimatedNodesManager::connectAnimatedNodeToShadowNodeFamily(
244244
react_native_assert(propsNodeTag);
245245
auto node = getAnimatedNode<PropsAnimatedNode>(propsNodeTag);
246246
if (node != nullptr && family != nullptr) {
247-
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
248-
tagToShadowNodeFamily_[family->getTag()] = family;
247+
node->connectToShadowNodeFamily(family);
249248
} else {
250249
LOG(WARNING)
251250
<< "Cannot ConnectAnimatedNodeToShadowNodeFamily, animated node has to be props type";
@@ -265,10 +264,6 @@ void NativeAnimatedNodesManager::disconnectAnimatedNodeFromView(
265264
std::lock_guard<std::mutex> lock(connectedAnimatedNodesMutex_);
266265
connectedAnimatedNodes_.erase(viewTag);
267266
}
268-
{
269-
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
270-
tagToShadowNodeFamily_.erase(viewTag);
271-
}
272267
updatedNodeTags_.insert(node->tag());
273268

274269
onManagedPropsRemoved(viewTag);
@@ -907,13 +902,14 @@ void NativeAnimatedNodesManager::schedulePropsCommit(
907902
Tag viewTag,
908903
const folly::dynamic& props,
909904
bool layoutStyleUpdated,
910-
bool forceFabricCommit) noexcept {
905+
bool forceFabricCommit,
906+
ShadowNodeFamily::Weak shadowNodeFamily) noexcept {
911907
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
912-
if (layoutStyleUpdated) {
913-
mergeObjects(updateViewProps_[viewTag], props);
914-
} else {
915-
mergeObjects(updateViewPropsDirect_[viewTag], props);
916-
}
908+
auto& current = layoutStyleUpdated
909+
? updateViewPropsForBackend_[viewTag]
910+
: updateViewPropsDirectForBackend_[viewTag];
911+
current.first = std::move(shadowNodeFamily);
912+
mergeObjects(current.second, props);
917913
return;
918914
}
919915

@@ -1007,38 +1003,36 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
10071003
}
10081004
}
10091005

1010-
{
1011-
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
1012-
for (auto& [tag, props] : updateViewPropsDirect_) {
1013-
auto weakFamily = tagToShadowNodeFamily_[tag];
1014-
1015-
if (auto family = weakFamily.lock()) {
1016-
propsBuilder.storeDynamic(props);
1017-
mutations.batch.push_back(
1018-
AnimationMutation{
1019-
.tag = tag,
1020-
.family = family,
1021-
.props = propsBuilder.get(),
1022-
});
1023-
}
1024-
containsChange = true;
1006+
for (auto& [tag, update] : updateViewPropsDirectForBackend_) {
1007+
auto weakFamily = update.first;
1008+
1009+
if (auto family = weakFamily.lock()) {
1010+
propsBuilder.storeDynamic(update.second);
1011+
mutations.batch.push_back(
1012+
AnimationMutation{
1013+
.tag = tag,
1014+
.family = family,
1015+
.props = propsBuilder.get(),
1016+
});
10251017
}
1026-
for (auto& [tag, props] : updateViewProps_) {
1027-
auto weakFamily = tagToShadowNodeFamily_[tag];
1028-
1029-
if (auto family = weakFamily.lock()) {
1030-
propsBuilder.storeDynamic(props);
1031-
mutations.batch.push_back(
1032-
AnimationMutation{
1033-
.tag = tag,
1034-
.family = family,
1035-
.props = propsBuilder.get(),
1036-
.hasLayoutUpdates = true,
1037-
});
1038-
}
1039-
containsChange = true;
1018+
containsChange = true;
1019+
}
1020+
for (auto& [tag, update] : updateViewPropsForBackend_) {
1021+
auto weakFamily = update.first;
1022+
1023+
if (auto family = weakFamily.lock()) {
1024+
propsBuilder.storeDynamic(update.second);
1025+
mutations.batch.push_back(
1026+
AnimationMutation{
1027+
.tag = tag,
1028+
.family = family,
1029+
.props = propsBuilder.get(),
1030+
.hasLayoutUpdates = true,
1031+
});
10401032
}
1033+
containsChange = true;
10411034
}
1035+
10421036
if (containsChange) {
10431037
updateViewPropsDirect_.clear();
10441038
updateViewProps_.clear();
@@ -1068,36 +1062,34 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
10681062

10691063
isEventAnimationInProgress_ = false;
10701064

1071-
{
1072-
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
1073-
for (auto& [tag, props] : updateViewPropsDirect_) {
1074-
auto weakFamily = tagToShadowNodeFamily_[tag];
1075-
1076-
if (auto family = weakFamily.lock()) {
1077-
propsBuilder.storeDynamic(props);
1078-
mutations.batch.push_back(
1079-
AnimationMutation{
1080-
.tag = tag,
1081-
.family = family,
1082-
.props = propsBuilder.get(),
1083-
});
1084-
}
1065+
for (auto& [tag, update] : updateViewPropsDirectForBackend_) {
1066+
auto weakFamily = update.first;
1067+
1068+
if (auto family = weakFamily.lock()) {
1069+
propsBuilder.storeDynamic(update.second);
1070+
mutations.batch.push_back(
1071+
AnimationMutation{
1072+
.tag = tag,
1073+
.family = family,
1074+
.props = propsBuilder.get(),
1075+
});
10851076
}
1086-
for (auto& [tag, props] : updateViewProps_) {
1087-
auto weakFamily = tagToShadowNodeFamily_[tag];
1088-
1089-
if (auto family = weakFamily.lock()) {
1090-
propsBuilder.storeDynamic(props);
1091-
mutations.batch.push_back(
1092-
AnimationMutation{
1093-
.tag = tag,
1094-
.family = family,
1095-
.props = propsBuilder.get(),
1096-
.hasLayoutUpdates = true,
1097-
});
1098-
}
1077+
}
1078+
for (auto& [tag, update] : updateViewPropsForBackend_) {
1079+
auto weakFamily = update.first;
1080+
1081+
if (auto family = weakFamily.lock()) {
1082+
propsBuilder.storeDynamic(update.second);
1083+
mutations.batch.push_back(
1084+
AnimationMutation{
1085+
.tag = tag,
1086+
.family = family,
1087+
.props = propsBuilder.get(),
1088+
.hasLayoutUpdates = true,
1089+
});
10991090
}
11001091
}
1092+
11011093
updateViewProps_.clear();
11021094
updateViewPropsDirect_.clear();
11031095
}

packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ class NativeAnimatedNodesManager {
153153
Tag viewTag,
154154
const folly::dynamic &props,
155155
bool layoutStyleUpdated,
156-
bool forceFabricCommit) noexcept;
156+
bool forceFabricCommit,
157+
ShadowNodeFamily::Weak shadowNodeFamily = {}) noexcept;
157158

158159
/**
159160
* Commits all pending animated property updates to their respective views.
@@ -260,9 +261,8 @@ class NativeAnimatedNodesManager {
260261

261262
std::unordered_map<Tag, folly::dynamic> updateViewProps_{};
262263
std::unordered_map<Tag, folly::dynamic> updateViewPropsDirect_{};
263-
264-
mutable std::mutex tagToShadowNodeFamilyMutex_;
265-
std::unordered_map<Tag, std::weak_ptr<const ShadowNodeFamily>> tagToShadowNodeFamily_{};
264+
std::unordered_map<Tag, std::pair<ShadowNodeFamily::Weak, folly::dynamic>> updateViewPropsForBackend_{};
265+
std::unordered_map<Tag, std::pair<ShadowNodeFamily::Weak, folly::dynamic>> updateViewPropsDirectForBackend_{};
266266

267267
/*
268268
* Sometimes a view is not longer connected to a PropsAnimatedNode, but

packages/react-native/ReactCommon/react/renderer/animated/nodes/PropsAnimatedNode.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "PropsAnimatedNode.h"
1313

1414
#include <react/debug/react_native_assert.h>
15+
#include <react/featureflags/ReactNativeFeatureFlags.h>
1516
#include <react/renderer/animated/NativeAnimatedNodesManager.h>
1617
#include <react/renderer/animated/nodes/ColorAnimatedNode.h>
1718
#include <react/renderer/animated/nodes/ObjectAnimatedNode.h>
@@ -55,6 +56,11 @@ PropsAnimatedNode::PropsAnimatedNode(
5556
}
5657
}
5758

59+
void PropsAnimatedNode::connectToShadowNodeFamily(
60+
ShadowNodeFamily::Weak shadowNodeFamily) {
61+
shadowNodeFamily_ = std::move(shadowNodeFamily);
62+
}
63+
5864
void PropsAnimatedNode::connectToView(Tag viewTag) {
5965
react_native_assert(
6066
connectedViewTag_ == animated::undefinedAnimatedNodeIdentifier &&
@@ -67,15 +73,25 @@ void PropsAnimatedNode::disconnectFromView(Tag viewTag) {
6773
connectedViewTag_ == viewTag &&
6874
"Attempting to disconnect view that has not been connected with the given animated node.");
6975
connectedViewTag_ = animated::undefinedAnimatedNodeIdentifier;
76+
shadowNodeFamily_.reset();
7077
}
7178

7279
// restore the value to whatever the value was on the ShadowNode instead of in
7380
// the View hierarchy
7481
void PropsAnimatedNode::restoreDefaultValues() {
7582
// If node is already disconnected from View, we cannot restore default values
7683
if (connectedViewTag_ != animated::undefinedAnimatedNodeIdentifier) {
77-
manager_->schedulePropsCommit(
78-
connectedViewTag_, folly::dynamic::object(), false, false);
84+
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
85+
manager_->schedulePropsCommit(
86+
connectedViewTag_,
87+
folly::dynamic::object(),
88+
false,
89+
false,
90+
shadowNodeFamily_);
91+
} else {
92+
manager_->schedulePropsCommit(
93+
connectedViewTag_, folly::dynamic::object(), false, false);
94+
}
7995
}
8096
}
8197

@@ -147,8 +163,17 @@ void PropsAnimatedNode::update(bool forceFabricCommit) {
147163

148164
layoutStyleUpdated_ = isLayoutStyleUpdated(getConfig()["props"], *manager_);
149165

150-
manager_->schedulePropsCommit(
151-
connectedViewTag_, props_, layoutStyleUpdated_, forceFabricCommit);
166+
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
167+
manager_->schedulePropsCommit(
168+
connectedViewTag_,
169+
props_,
170+
layoutStyleUpdated_,
171+
forceFabricCommit,
172+
shadowNodeFamily_);
173+
} else {
174+
manager_->schedulePropsCommit(
175+
connectedViewTag_, props_, layoutStyleUpdated_, forceFabricCommit);
176+
}
152177
}
153178

154179
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/animated/nodes/PropsAnimatedNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace facebook::react {
2121
class PropsAnimatedNode final : public AnimatedNode {
2222
public:
2323
PropsAnimatedNode(Tag tag, const folly::dynamic &config, NativeAnimatedNodesManager &manager);
24+
void connectToShadowNodeFamily(ShadowNodeFamily::Weak shadowNodeFamily);
2425
void connectToView(Tag viewTag);
2526
void disconnectFromView(Tag viewTag);
2627
void restoreDefaultValues();
@@ -51,6 +52,7 @@ class PropsAnimatedNode final : public AnimatedNode {
5152
bool layoutStyleUpdated_{false};
5253

5354
Tag connectedViewTag_{animated::undefinedAnimatedNodeIdentifier};
55+
ShadowNodeFamily::Weak shadowNodeFamily_;
5456

5557
// Needed for PlatformColor resolver
5658
SurfaceId connectedRootTag_{animated::undefinedAnimatedNodeIdentifier};

0 commit comments

Comments
 (0)