Init the backend closer to the uiManager (#53996)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53996

## Summary

This diff makes changes to the `NativeAnimatedNodesManager` and `AnimationBackend` to initialize the backend closer to the `UIManager`.

## Changelog:

[GENERAL] [CHANGED] - initialize the backend in NativeAnimatedNodesManagerProvider

Reviewed By: sammy-SC

Differential Revision: D81138133

fbshipit-source-id: 5be0c8d5f5e3593bcd8111bbd1d0e90502a53a0d
This commit is contained in:
Bartlomiej Bloniarz
2025-10-01 04:15:11 -07:00
committed by Facebook GitHub Bot
parent 325c681cb1
commit 3f396616d2
6 changed files with 43 additions and 33 deletions
@@ -89,15 +89,12 @@ NativeAnimatedNodesManager::NativeAnimatedNodesManager(
LOG(ERROR)
<< "C++ Animated was setup without a way to update UI. Animations will not work.";
}
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
// shouldn't be initialized here, but it's convenient for now
animationBackend_ = std::make_shared<AnimationBackend>(
startOnRenderCallback_,
stopOnRenderCallback_,
directManipulationCallback_);
}
}
NativeAnimatedNodesManager::NativeAnimatedNodesManager(
std::shared_ptr<AnimationBackend> animationBackend) noexcept
: animationBackend_(std::move(animationBackend)) {}
NativeAnimatedNodesManager::~NativeAnimatedNodesManager() noexcept {
stopRenderCallbackIfNeeded();
}
@@ -854,6 +851,11 @@ void NativeAnimatedNodesManager::schedulePropsCommit(
const folly::dynamic& props,
bool layoutStyleUpdated,
bool forceFabricCommit) noexcept {
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
mergeObjects(updateViewProps_[viewTag], props);
return;
}
// When fabricCommitCallback_ & directManipulationCallback_ are both
// available, we commit layout props via Fabric and the other using direct
// manipulation. If only fabricCommitCallback_ is available, we commit all
@@ -909,6 +911,7 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
auto timestamp = static_cast<double>(microseconds) / 1000.0;
bool containsChange = false;
{
// copied from onAnimationFrame
// Run all active animations
auto hasFinishedAnimations = false;
std::set<int> finishedAnimationValueNodes;
@@ -951,11 +954,6 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
AnimationMutation{tag, props["opacity"].asDouble()});
containsChange = true;
}
for (auto& [tag, props] : updateViewPropsDirect_) {
mutations.emplace_back(
AnimationMutation{tag, props["opacity"].asDouble()});
containsChange = true;
}
}
if (!containsChange) {
@@ -984,10 +982,6 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
mutations.emplace_back(
AnimationMutation{tag, props["opacity"].asDouble()});
}
for (auto& [tag, props] : updateViewPropsDirect_) {
mutations.emplace_back(
AnimationMutation{tag, props["opacity"].asDouble()});
}
}
} else {
// There is no active animation. Stop the render callback.
@@ -63,6 +63,9 @@ class NativeAnimatedNodesManager {
StartOnRenderCallback&& startOnRenderCallback = nullptr,
StopOnRenderCallback&& stopOnRenderCallback = nullptr) noexcept;
explicit NativeAnimatedNodesManager(
std::shared_ptr<AnimationBackend> animationBackend) noexcept;
~NativeAnimatedNodesManager() noexcept;
template <
@@ -11,6 +11,7 @@
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/animated/MergedValueDispatcher.h>
#include <react/renderer/animated/internal/AnimatedMountingOverrideDelegate.h>
#include <react/renderer/animationbackend/AnimationBackend.h>
#include <react/renderer/uimanager/UIManagerBinding.h>
namespace facebook::react {
@@ -64,11 +65,22 @@ NativeAnimatedNodesManagerProvider::getOrCreate(
uiManager->synchronouslyUpdateViewOnUIThread(viewTag, props);
};
nativeAnimatedNodesManager_ = std::make_shared<NativeAnimatedNodesManager>(
std::move(directManipulationCallback),
std::move(fabricCommitCallback),
std::move(startOnRenderCallback_),
std::move(stopOnRenderCallback_));
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
animationBackend_ = std::make_shared<AnimationBackend>(
std::move(startOnRenderCallback_),
std::move(stopOnRenderCallback_),
std::move(directManipulationCallback));
nativeAnimatedNodesManager_ =
std::make_shared<NativeAnimatedNodesManager>(animationBackend_);
} else {
nativeAnimatedNodesManager_ =
std::make_shared<NativeAnimatedNodesManager>(
std::move(directManipulationCallback),
std::move(fabricCommitCallback),
std::move(startOnRenderCallback_),
std::move(stopOnRenderCallback_));
}
addEventEmitterListener(
nativeAnimatedNodesManager_->getEventEmitterListener());
@@ -46,6 +46,7 @@ class NativeAnimatedNodesManagerProvider {
std::shared_ptr<EventEmitterListener> getEventEmitterListener();
private:
std::shared_ptr<AnimationBackend> animationBackend_;
std::shared_ptr<NativeAnimatedNodesManager> nativeAnimatedNodesManager_;
std::shared_ptr<EventEmitterListenerContainer> eventEmitterListenerContainer_;
@@ -10,12 +10,12 @@
namespace facebook::react {
AnimationBackend::AnimationBackend(
const StartOnRenderCallback& startOnRenderCallback,
const StopOnRenderCallback& stopOnRenderCallback,
const DirectManipulationCallback& directManipulationCallback)
: startOnRenderCallback_(startOnRenderCallback),
stopOnRenderCallback_(stopOnRenderCallback),
directManipulationCallback_(directManipulationCallback) {}
StartOnRenderCallback&& startOnRenderCallback,
StopOnRenderCallback&& stopOnRenderCallback,
DirectManipulationCallback&& directManipulationCallback)
: startOnRenderCallback_(std::move(startOnRenderCallback)),
stopOnRenderCallback_(std::move(stopOnRenderCallback)),
directManipulationCallback_(std::move(directManipulationCallback)) {}
void AnimationBackend::onAnimationFrame(double timestamp) {
for (auto& callback : callbacks) {
@@ -29,14 +29,14 @@ using DirectManipulationCallback =
class AnimationBackend {
public:
std::vector<Callback> callbacks;
const StartOnRenderCallback& startOnRenderCallback_;
const StopOnRenderCallback& stopOnRenderCallback_;
const DirectManipulationCallback& directManipulationCallback_;
const StartOnRenderCallback startOnRenderCallback_;
const StopOnRenderCallback stopOnRenderCallback_;
const DirectManipulationCallback directManipulationCallback_;
AnimationBackend(
const StartOnRenderCallback& startOnRenderCallback,
const StopOnRenderCallback& stopOnRenderCallback,
const DirectManipulationCallback& directManipulationCallback);
StartOnRenderCallback&& startOnRenderCallback,
StopOnRenderCallback&& stopOnRenderCallback,
DirectManipulationCallback&& directManipulationCallback);
void onAnimationFrame(double timestamp);
void start(const Callback& callback);
void stop();