Skip to content

Commit 197783b

Browse files
zeyapmeta-codesync[bot]
authored andcommitted
create UIManagerAnimationBackend to replace forward declare class AnimationBackend (#54058)
Summary: Pull Request resolved: #54058 ## Changelog: [Internal] [Changed] - create UIManagerAnimationBackend to replace forward declare `class AnimationBackend` for other delegates in UIManager, we usually explicitly create an interface in UIManager package with the public APIs we want to expose, we can follow that pattern here, so we avoid the forward declaration without definition situation, and decoupling UIManager with AnimationBackend also this way, in the future, animation runtimes that consume AnimationBackend can just include `UIManagerAnimationBackend` (where we expose all of public APIs of AnimationBackend) and remain somewhat decoupled too Reviewed By: sammy-SC Differential Revision: D83679693 fbshipit-source-id: e58d180b7d36a3f0e766c2914b16128346abe070
1 parent 52b2c46 commit 197783b

7 files changed

Lines changed: 51 additions & 15 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <react/renderer/animated/nodes/TrackingAnimatedNode.h>
3434
#include <react/renderer/animated/nodes/TransformAnimatedNode.h>
3535
#include <react/renderer/animated/nodes/ValueAnimatedNode.h>
36+
#include <react/renderer/animationbackend/AnimatedPropsBuilder.h>
3637
#include <react/renderer/core/EventEmitter.h>
3738

3839
namespace facebook::react {
@@ -92,7 +93,7 @@ NativeAnimatedNodesManager::NativeAnimatedNodesManager(
9293
}
9394

9495
NativeAnimatedNodesManager::NativeAnimatedNodesManager(
95-
std::shared_ptr<AnimationBackend> animationBackend) noexcept
96+
std::shared_ptr<UIManagerAnimationBackend> animationBackend) noexcept
9697
: animationBackend_(std::move(animationBackend)) {}
9798

9899
NativeAnimatedNodesManager::~NativeAnimatedNodesManager() noexcept {
@@ -514,8 +515,12 @@ NativeAnimatedNodesManager::ensureEventEmitterListener() noexcept {
514515

515516
void NativeAnimatedNodesManager::startRenderCallbackIfNeeded() {
516517
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
517-
animationBackend_->start(
518-
[this](float /*f*/) { return pullAnimationMutations(); });
518+
if (auto animationBackend =
519+
std::static_pointer_cast<AnimationBackend>(animationBackend_)) {
520+
animationBackend->start(
521+
[this](float /*f*/) { return pullAnimationMutations(); });
522+
}
523+
519524
return;
520525
}
521526
// This method can be called from either the UI thread or JavaScript thread.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <react/renderer/animated/event_drivers/EventAnimationDriver.h>
2020
#include <react/renderer/animationbackend/AnimationBackend.h>
2121
#include <react/renderer/core/ReactPrimitives.h>
22+
#include <react/renderer/uimanager/UIManagerAnimationBackend.h>
2223
#include <chrono>
2324
#include <memory>
2425
#include <mutex>
@@ -68,7 +69,7 @@ class NativeAnimatedNodesManager {
6869
StopOnRenderCallback&& stopOnRenderCallback = nullptr) noexcept;
6970

7071
explicit NativeAnimatedNodesManager(
71-
std::shared_ptr<AnimationBackend> animationBackend) noexcept;
72+
std::shared_ptr<UIManagerAnimationBackend> animationBackend) noexcept;
7273

7374
~NativeAnimatedNodesManager() noexcept;
7475

@@ -212,7 +213,7 @@ class NativeAnimatedNodesManager {
212213
const std::string& eventName,
213214
const EventPayload& payload) noexcept;
214215

215-
std::shared_ptr<AnimationBackend> animationBackend_;
216+
std::shared_ptr<UIManagerAnimationBackend> animationBackend_;
216217

217218
std::unique_ptr<AnimatedNode> animatedNode(
218219
Tag tag,

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

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

4848
private:
49-
std::shared_ptr<AnimationBackend> animationBackend_;
49+
std::shared_ptr<UIManagerAnimationBackend> animationBackend_;
5050
std::shared_ptr<NativeAnimatedNodesManager> nativeAnimatedNodesManager_;
5151

5252
std::shared_ptr<EventEmitterListenerContainer> eventEmitterListenerContainer_;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <folly/dynamic.h>
1111
#include <react/renderer/core/ReactPrimitives.h>
1212
#include <react/renderer/uimanager/UIManager.h>
13+
#include <react/renderer/uimanager/UIManagerAnimationBackend.h>
1314
#include <functional>
1415
#include <vector>
1516
#include "AnimatedProps.h"
@@ -25,7 +26,7 @@ struct AnimationMutation {
2526

2627
using AnimationMutations = std::vector<AnimationMutation>;
2728

28-
class AnimationBackend {
29+
class AnimationBackend : public UIManagerAnimationBackend {
2930
public:
3031
using Callback = std::function<AnimationMutations(float)>;
3132
using StartOnRenderCallback = std::function<void(std::function<void()>&&)>;
@@ -54,8 +55,8 @@ class AnimationBackend {
5455
void synchronouslyUpdateProps(
5556
const std::unordered_map<Tag, AnimatedProps>& updates);
5657

57-
void onAnimationFrame(double timestamp);
58+
void onAnimationFrame(double timestamp) override;
5859
void start(const Callback& callback);
59-
void stop();
60+
void stop() override;
6061
};
6162
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,12 @@ void UIManager::setNativeAnimatedDelegate(
681681
}
682682

683683
void UIManager::unstable_setAnimationBackend(
684-
std::weak_ptr<AnimationBackend> animationBackend) {
684+
std::weak_ptr<UIManagerAnimationBackend> animationBackend) {
685685
animationBackend_ = animationBackend;
686686
}
687687

688-
std::weak_ptr<AnimationBackend> UIManager::unstable_getAnimationBackend() {
688+
std::weak_ptr<UIManagerAnimationBackend>
689+
UIManager::unstable_getAnimationBackend() {
689690
return animationBackend_;
690691
}
691692

packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <react/renderer/mounting/ShadowTree.h>
2424
#include <react/renderer/mounting/ShadowTreeDelegate.h>
2525
#include <react/renderer/mounting/ShadowTreeRegistry.h>
26+
#include <react/renderer/uimanager/UIManagerAnimationBackend.h>
2627
#include <react/renderer/uimanager/UIManagerAnimationDelegate.h>
2728
#include <react/renderer/uimanager/UIManagerDelegate.h>
2829
#include <react/renderer/uimanager/UIManagerNativeAnimatedDelegate.h>
@@ -36,7 +37,6 @@ namespace facebook::react {
3637
class UIManagerBinding;
3738
class UIManagerCommitHook;
3839
class UIManagerMountHook;
39-
class AnimationBackend;
4040

4141
class UIManager final : public ShadowTreeDelegate {
4242
public:
@@ -63,9 +63,13 @@ class UIManager final : public ShadowTreeDelegate {
6363
* the pointer before being destroyed.
6464
*/
6565
void setAnimationDelegate(UIManagerAnimationDelegate* delegate);
66+
67+
/**
68+
* Sets and gets UIManager's AnimationBackend reference.
69+
*/
6670
void unstable_setAnimationBackend(
67-
std::weak_ptr<AnimationBackend> animationBackend);
68-
std::weak_ptr<AnimationBackend> unstable_getAnimationBackend();
71+
std::weak_ptr<UIManagerAnimationBackend> animationBackend);
72+
std::weak_ptr<UIManagerAnimationBackend> unstable_getAnimationBackend();
6973

7074
/**
7175
* Execute stopSurface on any UIMAnagerAnimationDelegate.
@@ -263,7 +267,7 @@ class UIManager final : public ShadowTreeDelegate {
263267
std::unique_ptr<LazyShadowTreeRevisionConsistencyManager>
264268
lazyShadowTreeRevisionConsistencyManager_;
265269

266-
std::weak_ptr<AnimationBackend> animationBackend_;
270+
std::weak_ptr<UIManagerAnimationBackend> animationBackend_;
267271
};
268272

269273
} // namespace facebook::react
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/components/view/BaseViewProps.h>
11+
#include <react/renderer/core/ShadowNodeFamily.h>
12+
13+
namespace facebook::react {
14+
15+
class UIManagerAnimationBackend {
16+
public:
17+
virtual ~UIManagerAnimationBackend() = default;
18+
19+
virtual void onAnimationFrame(double timestamp) = 0;
20+
// TODO: T240293839 Move over start() function and mutation types
21+
virtual void stop() = 0;
22+
};
23+
24+
} // namespace facebook::react

0 commit comments

Comments
 (0)