From 84e408cf89eb537439dc99db95d33df3c2a9394a Mon Sep 17 00:00:00 2001 From: Bartlomiej Bloniarz Date: Fri, 19 Dec 2025 06:25:52 -0800 Subject: [PATCH] Use dynamic for updates in Animated with Animation Backend (#54872) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54872 For the testing purposes, during the development of Animation Backend, we changed the way props are modified from animated, to use the AnimatedProps. As these would currently not benefit c++ Animated, this diff reverts that to use `folly::dynamic` again. # Changelog [General] [Changed] - C++ animated now passes the calculated `folly::dynamic` directly to the backend, without repacking it into `AnimatedProp` Reviewed By: zeyap Differential Revision: D88743857 fbshipit-source-id: a9f715730e78f4278e95a2df0aa3b6d2628f1651 --- .../animated/NativeAnimatedNodesManager.cpp | 69 +++++++++++-------- .../AnimatedPropsRegistry.cpp | 12 ++++ .../animationbackend/AnimatedPropsRegistry.h | 1 + .../animationbackend/AnimationBackend.cpp | 36 ++++------ .../animationbackend/AnimationBackend.h | 5 +- .../AnimationBackendCommitHook.cpp | 14 ++-- 6 files changed, 83 insertions(+), 54 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp index a17966d285d..e462c9ab8fa 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp @@ -1008,9 +1008,21 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() { } for (auto& [tag, props] : updateViewPropsDirect_) { - propsBuilder.storeDynamic(props); - mutations.push_back( - AnimationMutation{tag, nullptr, propsBuilder.get()}); + auto familyIt = tagToShadowNodeFamily_.find(tag); + if (familyIt == tagToShadowNodeFamily_.end()) { + continue; + } + + auto weakFamily = familyIt->second; + if (auto family = weakFamily.lock()) { + propsBuilder.storeDynamic(props); + mutations.batch.push_back( + AnimationMutation{ + .tag = tag, + .family = family, + .props = propsBuilder.get(), + }); + } containsChange = true; } { @@ -1020,22 +1032,12 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() { if (familyIt == tagToShadowNodeFamily_.end()) { continue; } - if (auto family = familyIt->second.lock()) { - // C++ Animated produces props in the form of a folly::dynamic, so - // it wouldn't make sense to unpack it here. However, for the - // purposes of testing, we want to be able to use the statically - // typed AnimationMutation. At a later stage we will instead just - // pass the dynamic directly to propsBuilder and the new API could - // be used by 3rd party libraries or in the fututre by Animated. - if (props.find("width") != props.items().end()) { - propsBuilder.setWidth( - yoga::Style::SizeLength::points(props["width"].asDouble())); - } - if (props.find("height") != props.items().end()) { - propsBuilder.setHeight( - yoga::Style::SizeLength::points(props["height"].asDouble())); - } - mutations.push_back( + + auto weakFamily = familyIt->second; + if (auto family = weakFamily.lock()) { + propsBuilder.storeDynamic(props); + mutations.hasLayoutUpdates = true; + mutations.batch.push_back( AnimationMutation{ .tag = tag, .family = family, @@ -1075,13 +1077,21 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() { isEventAnimationInProgress_ = false; for (auto& [tag, props] : updateViewPropsDirect_) { - propsBuilder.storeDynamic(props); - mutations.push_back( - AnimationMutation{ - .tag = tag, - .family = nullptr, - .props = propsBuilder.get(), - }); + auto familyIt = tagToShadowNodeFamily_.find(tag); + if (familyIt == tagToShadowNodeFamily_.end()) { + continue; + } + + auto weakFamily = familyIt->second; + if (auto family = weakFamily.lock()) { + propsBuilder.storeDynamic(props); + mutations.batch.push_back( + AnimationMutation{ + .tag = tag, + .family = family, + .props = propsBuilder.get(), + }); + } } { std::lock_guard lock(tagToShadowNodeFamilyMutex_); @@ -1090,9 +1100,12 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() { if (familyIt == tagToShadowNodeFamily_.end()) { continue; } - if (auto family = familyIt->second.lock()) { + + auto weakFamily = familyIt->second; + if (auto family = weakFamily.lock()) { propsBuilder.storeDynamic(props); - mutations.push_back( + mutations.hasLayoutUpdates = true; + mutations.batch.push_back( AnimationMutation{ .tag = tag, .family = family, diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp index bfddaeaf427..6195af1a822 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp @@ -35,6 +35,18 @@ void AnimatedPropsRegistry::update( auto& snapshot = it->second; auto& viewProps = snapshot->props; + if (animatedProps.rawProps) { + const auto& newRawProps = *animatedProps.rawProps; + auto& currentRawProps = snapshot->rawProps; + + if (currentRawProps) { + auto newRawPropsDynamic = newRawProps.toDynamic(); + currentRawProps->merge_patch(newRawPropsDynamic); + } else { + currentRawProps = + std::make_unique(newRawProps.toDynamic()); + } + } for (const auto& animatedProp : animatedProps.props) { snapshot->propNames.insert(animatedProp->propName); cloneProp(viewProps, *animatedProp); diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h index 3a982ef2245..1d1d2d1288d 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h @@ -19,6 +19,7 @@ namespace facebook::react { struct PropsSnapshot { BaseViewProps props; std::unordered_set propNames; + std::unique_ptr rawProps; }; struct SurfaceContext { diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index ddf079c3800..43e4d4af50d 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp @@ -6,6 +6,7 @@ */ #include "AnimationBackend.h" +#include #include #include #include @@ -60,17 +61,6 @@ static inline Props::Shared cloneProps( return newProps; } -static inline bool mutationHasLayoutUpdates( - facebook::react::AnimationMutation& mutation) { - for (auto& animatedProp : mutation.props.props) { - // TODO: there should also be a check for the dynamic part - if (layoutProps.contains(animatedProp->propName)) { - return true; - } - } - return false; -} - AnimationBackend::AnimationBackend( StartOnRenderCallback&& startOnRenderCallback, StopOnRenderCallback&& stopOnRenderCallback, @@ -89,17 +79,19 @@ void AnimationBackend::onAnimationFrame(double timestamp) { std::unordered_map synchronousUpdates; std::unordered_map surfaceUpdates; - bool hasAnyLayoutUpdates = false; for (auto& callback : callbacks) { auto muatations = callback(static_cast(timestamp)); - for (auto& mutation : muatations) { - hasAnyLayoutUpdates |= mutationHasLayoutUpdates(mutation); - const auto family = mutation.family; - if (family != nullptr) { + if (muatations.hasLayoutUpdates) { + for (auto& mutation : muatations.batch) { + const auto family = mutation.family; + react_native_assert(family != nullptr); + auto& [families, updates] = surfaceUpdates[family->getSurfaceId()]; families.insert(family.get()); updates[mutation.tag] = std::move(mutation.props); - } else { + } + } else { + for (auto& mutation : muatations.batch) { synchronousUpdates[mutation.tag] = std::move(mutation.props); } } @@ -107,9 +99,11 @@ void AnimationBackend::onAnimationFrame(double timestamp) { animatedPropsRegistry_->update(surfaceUpdates); - if (hasAnyLayoutUpdates) { + if (!surfaceUpdates.empty()) { commitUpdates(surfaceUpdates); - } else { + } + + if (!synchronousUpdates.empty()) { synchronouslyUpdateProps(synchronousUpdates); } } @@ -175,8 +169,8 @@ void AnimationBackend::commitUpdates( void AnimationBackend::synchronouslyUpdateProps( const std::unordered_map& updates) { for (auto& [tag, animatedProps] : updates) { - // TODO: We shouldn't repack it into dynamic, but for that a rewrite of - // directManipulationCallback_ is needed + // TODO: We shouldn't repack it into dynamic, but for that a rewrite + // of directManipulationCallback_ is needed auto dyn = animationbackend::packAnimatedProps(animatedProps); directManipulationCallback_(tag, std::move(dyn)); } diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h index d8f7e3a326d..c39583cafee 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h @@ -38,7 +38,10 @@ struct AnimationMutation { AnimatedProps props; }; -using AnimationMutations = std::vector; +struct AnimationMutations { + std::vector batch; + bool hasLayoutUpdates{false}; +}; class AnimationBackend : public UIManagerAnimationBackend { public: diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp index 5cb990fb68a..6d359ea8c1c 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp @@ -43,13 +43,19 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit( if (surfaceFamilies.contains(&shadowNode.getFamily()) && updates.contains(shadowNode.getTag())) { auto& snapshot = updates.at(shadowNode.getTag()); - if (!snapshot->propNames.empty()) { + if (!snapshot->propNames.empty() || snapshot->rawProps) { PropsParserContext propsParserContext{ shadowNode.getSurfaceId(), *shadowNode.getContextContainer()}; - - newProps = shadowNode.getComponentDescriptor().cloneProps( - propsParserContext, shadowNode.getProps(), {}); + if (snapshot->rawProps) { + newProps = shadowNode.getComponentDescriptor().cloneProps( + propsParserContext, + shadowNode.getProps(), + RawProps(*snapshot->rawProps)); + } else { + newProps = shadowNode.getComponentDescriptor().cloneProps( + propsParserContext, shadowNode.getProps(), {}); + } viewProps = std::const_pointer_cast( std::static_pointer_cast(newProps)); }