diff --git a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp index e9b3ffd7ba5..f51aa4b2d2b 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp @@ -515,7 +515,10 @@ void NativeAnimatedNodesManager::handleAnimatedEvent( // frames. if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) { if (auto animationBackend = animationBackend_.lock()) { - animationBackend->trigger(); + animationBackend->pushAnimationMutations( + [this](AnimationTimestamp timestamp) -> AnimationMutations { + return pullAnimationMutations(timestamp); + }); } } else { onRender(); diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index 9fb2e5ecf94..2d73065a10c 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp @@ -51,31 +51,27 @@ AnimationBackend::AnimationBackend( react_native_assert(uiManager_.expired() == false); } -void AnimationBackend::onAnimationFrame(AnimationTimestamp timestamp) { - std::vector callbacksCopy; - std::unordered_map surfaceUpdates; - std::set asyncFlushSurfaces; +void AnimationBackend::unpackMutations( + AnimationMutations& mutations, + std::unordered_map& surfaceUpdates, + std::set& asyncFlushSurfaces) { + for (auto& mutation : mutations.batch) { + const auto family = mutation.family; + react_native_assert(family != nullptr); - { - std::lock_guard lock(mutex_); - callbacksCopy = callbacks; + auto& [families, updates, hasLayoutUpdates] = + surfaceUpdates[family->getSurfaceId()]; + hasLayoutUpdates |= mutation.hasLayoutUpdates; + families.insert(family); + updates[mutation.tag] = std::move(mutation.props); } - for (auto& callbackWithId : callbacksCopy) { - auto mutations = callbackWithId.callback(timestamp); - asyncFlushSurfaces.merge(mutations.asyncFlushSurfaces); - for (auto& mutation : mutations.batch) { - const auto family = mutation.family; - react_native_assert(family != nullptr); - - auto& [families, updates, hasLayoutUpdates] = - surfaceUpdates[family->getSurfaceId()]; - hasLayoutUpdates |= mutation.hasLayoutUpdates; - families.insert(family); - updates[mutation.tag] = std::move(mutation.props); - } - } + asyncFlushSurfaces.merge(mutations.asyncFlushSurfaces); +} +void AnimationBackend::applySurfaceUpdates( + std::unordered_map& surfaceUpdates, + const std::set& asyncFlushSurfaces) { animatedPropsRegistry_->update(surfaceUpdates); for (auto& [surfaceId, updates] : surfaceUpdates) { @@ -89,6 +85,30 @@ void AnimationBackend::onAnimationFrame(AnimationTimestamp timestamp) { requestAsyncFlushForSurfaces(asyncFlushSurfaces); } +void AnimationBackend::applyMutations(AnimationMutations mutations) { + std::unordered_map surfaceUpdates; + std::set asyncFlushSurfaces; + unpackMutations(mutations, surfaceUpdates, asyncFlushSurfaces); + applySurfaceUpdates(surfaceUpdates, asyncFlushSurfaces); +} + +void AnimationBackend::onAnimationFrame(AnimationTimestamp timestamp) { + std::vector callbacksCopy; + + { + std::lock_guard lock(mutex_); + callbacksCopy = callbacks; + } + + std::unordered_map surfaceUpdates; + std::set asyncFlushSurfaces; + for (auto& callbackWithId : callbacksCopy) { + auto mutations = callbackWithId.callback(timestamp); + unpackMutations(mutations, surfaceUpdates, asyncFlushSurfaces); + } + applySurfaceUpdates(surfaceUpdates, asyncFlushSurfaces); +} + CallbackId AnimationBackend::start(const Callback& callback) { std::lock_guard lock(mutex_); @@ -123,6 +143,12 @@ void AnimationBackend::trigger() { onAnimationFrame(std::chrono::steady_clock::now().time_since_epoch()); } +void AnimationBackend::pushAnimationMutations(const Callback& callback) { + auto timestamp = animationChoreographer_->now(); + auto mutations = callback(timestamp); + applyMutations(std::move(mutations)); +} + void AnimationBackend::commitUpdates( SurfaceId surfaceId, SurfaceUpdates& surfaceUpdates) { diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h index deef1fb532e..91463154286 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.h @@ -60,10 +60,19 @@ class AnimationBackend : public UIManagerAnimationBackend { void onAnimationFrame(AnimationTimestamp timestamp) override; void trigger() override; + void pushAnimationMutations(const Callback &callback) override; CallbackId start(const Callback &callback) override; void stop(CallbackId callbackId) override; private: + void unpackMutations( + AnimationMutations &mutations, + std::unordered_map &surfaceUpdates, + std::set &asyncFlushSurfaces); + void applySurfaceUpdates( + std::unordered_map &surfaceUpdates, + const std::set &asyncFlushSurfaces); + void applyMutations(AnimationMutations mutations); std::vector callbacks; std::shared_ptr animatedPropsRegistry_; std::shared_ptr animationChoreographer_; diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationChoreographer.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationChoreographer.h index e6194b19949..94be54e3a21 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationChoreographer.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationChoreographer.h @@ -8,6 +8,7 @@ #pragma once #include +#include namespace facebook::react { @@ -21,6 +22,10 @@ class AnimationChoreographer { virtual void resume() = 0; virtual void pause() = 0; + virtual AnimationTimestamp now() const + { + return HighResTimeStamp::now().toChronoSteadyClockTimePoint().time_since_epoch(); + } void setAnimationBackend(std::weak_ptr animationBackend) { animationBackend_ = animationBackend; diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerAnimationBackend.h b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerAnimationBackend.h index 221e4686056..46ecea5e4f0 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerAnimationBackend.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerAnimationBackend.h @@ -31,6 +31,7 @@ class UIManagerAnimationBackend { virtual void stop(CallbackId callbackId) = 0; virtual void clearRegistry(SurfaceId surfaceId) = 0; virtual void trigger() = 0; + virtual void pushAnimationMutations(const Callback &callback) = 0; virtual void registerJSInvoker(std::shared_ptr jsInvoker) = 0; }; diff --git a/private/react-native-fantom/tester/src/TesterAnimationChoreographer.h b/private/react-native-fantom/tester/src/TesterAnimationChoreographer.h index 859d3f75c12..c45a84867bc 100644 --- a/private/react-native-fantom/tester/src/TesterAnimationChoreographer.h +++ b/private/react-native-fantom/tester/src/TesterAnimationChoreographer.h @@ -7,6 +7,8 @@ #pragma once +#include + #include #include #include @@ -19,8 +21,22 @@ class TesterAnimationChoreographer : public AnimationChoreographer { void pause() override; void runUITick(AnimationTimestamp timestamp); + AnimationTimestamp now() const override + { + if (clockProvider_) { + return clockProvider_(); + } + return AnimationChoreographer::now(); + } + + void setClockProvider(std::function clockProvider) + { + clockProvider_ = std::move(clockProvider); + } + private: bool isPaused_{false}; + std::function clockProvider_; }; } // namespace facebook::react