Skip to content

Commit 3f39661

Browse files
Bartlomiej Bloniarzfacebook-github-bot
authored andcommitted
Init the backend closer to the uiManager (#53996)
Summary: Pull Request resolved: #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
1 parent 325c681 commit 3f39661

File tree

6 files changed

+43
-33
lines changed

6 files changed

+43
-33
lines changed

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ NativeAnimatedNodesManager::NativeAnimatedNodesManager(
8989
LOG(ERROR)
9090
<< "C++ Animated was setup without a way to update UI. Animations will not work.";
9191
}
92-
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
93-
// shouldn't be initialized here, but it's convenient for now
94-
animationBackend_ = std::make_shared<AnimationBackend>(
95-
startOnRenderCallback_,
96-
stopOnRenderCallback_,
97-
directManipulationCallback_);
98-
}
9992
}
10093

94+
NativeAnimatedNodesManager::NativeAnimatedNodesManager(
95+
std::shared_ptr<AnimationBackend> animationBackend) noexcept
96+
: animationBackend_(std::move(animationBackend)) {}
97+
10198
NativeAnimatedNodesManager::~NativeAnimatedNodesManager() noexcept {
10299
stopRenderCallbackIfNeeded();
103100
}
@@ -854,6 +851,11 @@ void NativeAnimatedNodesManager::schedulePropsCommit(
854851
const folly::dynamic& props,
855852
bool layoutStyleUpdated,
856853
bool forceFabricCommit) noexcept {
854+
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
855+
mergeObjects(updateViewProps_[viewTag], props);
856+
return;
857+
}
858+
857859
// When fabricCommitCallback_ & directManipulationCallback_ are both
858860
// available, we commit layout props via Fabric and the other using direct
859861
// manipulation. If only fabricCommitCallback_ is available, we commit all
@@ -909,6 +911,7 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
909911
auto timestamp = static_cast<double>(microseconds) / 1000.0;
910912
bool containsChange = false;
911913
{
914+
// copied from onAnimationFrame
912915
// Run all active animations
913916
auto hasFinishedAnimations = false;
914917
std::set<int> finishedAnimationValueNodes;
@@ -951,11 +954,6 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
951954
AnimationMutation{tag, props["opacity"].asDouble()});
952955
containsChange = true;
953956
}
954-
for (auto& [tag, props] : updateViewPropsDirect_) {
955-
mutations.emplace_back(
956-
AnimationMutation{tag, props["opacity"].asDouble()});
957-
containsChange = true;
958-
}
959957
}
960958

961959
if (!containsChange) {
@@ -984,10 +982,6 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
984982
mutations.emplace_back(
985983
AnimationMutation{tag, props["opacity"].asDouble()});
986984
}
987-
for (auto& [tag, props] : updateViewPropsDirect_) {
988-
mutations.emplace_back(
989-
AnimationMutation{tag, props["opacity"].asDouble()});
990-
}
991985
}
992986
} else {
993987
// There is no active animation. Stop the render callback.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class NativeAnimatedNodesManager {
6363
StartOnRenderCallback&& startOnRenderCallback = nullptr,
6464
StopOnRenderCallback&& stopOnRenderCallback = nullptr) noexcept;
6565

66+
explicit NativeAnimatedNodesManager(
67+
std::shared_ptr<AnimationBackend> animationBackend) noexcept;
68+
6669
~NativeAnimatedNodesManager() noexcept;
6770

6871
template <

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <react/featureflags/ReactNativeFeatureFlags.h>
1212
#include <react/renderer/animated/MergedValueDispatcher.h>
1313
#include <react/renderer/animated/internal/AnimatedMountingOverrideDelegate.h>
14+
#include <react/renderer/animationbackend/AnimationBackend.h>
1415
#include <react/renderer/uimanager/UIManagerBinding.h>
1516

1617
namespace facebook::react {
@@ -64,11 +65,22 @@ NativeAnimatedNodesManagerProvider::getOrCreate(
6465
uiManager->synchronouslyUpdateViewOnUIThread(viewTag, props);
6566
};
6667

67-
nativeAnimatedNodesManager_ = std::make_shared<NativeAnimatedNodesManager>(
68-
std::move(directManipulationCallback),
69-
std::move(fabricCommitCallback),
70-
std::move(startOnRenderCallback_),
71-
std::move(stopOnRenderCallback_));
68+
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
69+
animationBackend_ = std::make_shared<AnimationBackend>(
70+
std::move(startOnRenderCallback_),
71+
std::move(stopOnRenderCallback_),
72+
std::move(directManipulationCallback));
73+
74+
nativeAnimatedNodesManager_ =
75+
std::make_shared<NativeAnimatedNodesManager>(animationBackend_);
76+
} else {
77+
nativeAnimatedNodesManager_ =
78+
std::make_shared<NativeAnimatedNodesManager>(
79+
std::move(directManipulationCallback),
80+
std::move(fabricCommitCallback),
81+
std::move(startOnRenderCallback_),
82+
std::move(stopOnRenderCallback_));
83+
}
7284

7385
addEventEmitterListener(
7486
nativeAnimatedNodesManager_->getEventEmitterListener());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class NativeAnimatedNodesManagerProvider {
4646
std::shared_ptr<EventEmitterListener> getEventEmitterListener();
4747

4848
private:
49+
std::shared_ptr<AnimationBackend> animationBackend_;
4950
std::shared_ptr<NativeAnimatedNodesManager> nativeAnimatedNodesManager_;
5051

5152
std::shared_ptr<EventEmitterListenerContainer> eventEmitterListenerContainer_;

packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
namespace facebook::react {
1111

1212
AnimationBackend::AnimationBackend(
13-
const StartOnRenderCallback& startOnRenderCallback,
14-
const StopOnRenderCallback& stopOnRenderCallback,
15-
const DirectManipulationCallback& directManipulationCallback)
16-
: startOnRenderCallback_(startOnRenderCallback),
17-
stopOnRenderCallback_(stopOnRenderCallback),
18-
directManipulationCallback_(directManipulationCallback) {}
13+
StartOnRenderCallback&& startOnRenderCallback,
14+
StopOnRenderCallback&& stopOnRenderCallback,
15+
DirectManipulationCallback&& directManipulationCallback)
16+
: startOnRenderCallback_(std::move(startOnRenderCallback)),
17+
stopOnRenderCallback_(std::move(stopOnRenderCallback)),
18+
directManipulationCallback_(std::move(directManipulationCallback)) {}
1919

2020
void AnimationBackend::onAnimationFrame(double timestamp) {
2121
for (auto& callback : callbacks) {

packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ using DirectManipulationCallback =
2929
class AnimationBackend {
3030
public:
3131
std::vector<Callback> callbacks;
32-
const StartOnRenderCallback& startOnRenderCallback_;
33-
const StopOnRenderCallback& stopOnRenderCallback_;
34-
const DirectManipulationCallback& directManipulationCallback_;
32+
const StartOnRenderCallback startOnRenderCallback_;
33+
const StopOnRenderCallback stopOnRenderCallback_;
34+
const DirectManipulationCallback directManipulationCallback_;
3535

3636
AnimationBackend(
37-
const StartOnRenderCallback& startOnRenderCallback,
38-
const StopOnRenderCallback& stopOnRenderCallback,
39-
const DirectManipulationCallback& directManipulationCallback);
37+
StartOnRenderCallback&& startOnRenderCallback,
38+
StopOnRenderCallback&& stopOnRenderCallback,
39+
DirectManipulationCallback&& directManipulationCallback);
4040
void onAnimationFrame(double timestamp);
4141
void start(const Callback& callback);
4242
void stop();

0 commit comments

Comments
 (0)