mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Use timestamp from Animation Backend in Animated (#55206)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55206 To avoid timestamp representation bugs with different platforms passing the timestamp as a simple double, we now use `std::chrono::duration<double, std::milli>` to represent timestamps. # Changelog [General] [Added] - `AnimationTimestamp` to `UIManagerAnimationBackend` [General] [Changed] - updated `AnimationBackend` usages to use `AnimationTimestamp` Reviewed By: zeyap Differential Revision: D90505538 fbshipit-source-id: 98761ac0060612e9ed9a57c9952656cae70c30f1
This commit is contained in:
1 parent
d379cde036
commit
2a4a54ae6c
11 files changed
+28
-29
No files matched your search
@@ -123,7 +123,7 @@ class LayoutAnimationDelegateProxy : public LayoutAnimationStatusDelegate, publi
|
||||
- (void)displayLinkTick:(CADisplayLink *)sender
|
||||
{
|
||||
if (_choreographer != nullptr) {
|
||||
_choreographer->onAnimationFrame(static_cast<float>(sender.targetTimestamp * 1000));
|
||||
_choreographer->onAnimationFrame(std::chrono::duration<double>(sender.targetTimestamp));
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ void FabricUIManagerBinding::driveCxxAnimations() {
|
||||
}
|
||||
|
||||
void FabricUIManagerBinding::driveAnimationBackend(jdouble frameTimeMs) {
|
||||
animationChoreographer_->onAnimationFrame(static_cast<float>(frameTimeMs));
|
||||
animationChoreographer_->onAnimationFrame(AnimationTimestamp{frameTimeMs});
|
||||
}
|
||||
|
||||
void FabricUIManagerBinding::drainPreallocateViewsQueue() {
|
||||
|
||||
+9
-10
@@ -555,8 +555,10 @@ void NativeAnimatedNodesManager::startRenderCallbackIfNeeded(bool isAsync) {
|
||||
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
|
||||
#ifdef RN_USE_ANIMATION_BACKEND
|
||||
if (auto animationBackend = animationBackend_.lock()) {
|
||||
animationBackendCallbackId_ = animationBackend->start(
|
||||
[this](float /*f*/) { return pullAnimationMutations(); });
|
||||
animationBackendCallbackId_ =
|
||||
animationBackend->start([this](AnimationTimestamp timestamp) {
|
||||
return pullAnimationMutations(timestamp);
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -974,13 +976,14 @@ void NativeAnimatedNodesManager::insertMutations(
|
||||
|
||||
AnimationMutations NativeAnimatedNodesManager::onAnimationFrameForBackend(
|
||||
AnimatedPropsBuilder& propsBuilder,
|
||||
double timestamp) {
|
||||
AnimationTimestamp timestamp) {
|
||||
AnimationMutations mutations{};
|
||||
auto timestampMs = timestamp.count();
|
||||
// Run all active animations
|
||||
auto hasFinishedAnimations = false;
|
||||
std::set<int> finishedAnimationValueNodes;
|
||||
for (const auto& [_id, driver] : activeAnimations_) {
|
||||
driver->runAnimationStep(timestamp);
|
||||
driver->runAnimationStep(timestampMs);
|
||||
|
||||
if (driver->getIsComplete()) {
|
||||
hasFinishedAnimations = true;
|
||||
@@ -1017,7 +1020,8 @@ AnimationMutations NativeAnimatedNodesManager::onAnimationFrameForBackend(
|
||||
return mutations;
|
||||
}
|
||||
|
||||
AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
|
||||
AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations(
|
||||
AnimationTimestamp timestamp) {
|
||||
if (!ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
|
||||
return {};
|
||||
}
|
||||
@@ -1043,11 +1047,6 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
|
||||
|
||||
// Step through the animation loop
|
||||
if (isAnimationUpdateNeeded()) {
|
||||
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
g_now().time_since_epoch())
|
||||
.count();
|
||||
|
||||
auto timestamp = static_cast<double>(microseconds) / 1000.0;
|
||||
AnimatedPropsBuilder propsBuilder;
|
||||
mutations = onAnimationFrameForBackend(propsBuilder, timestamp);
|
||||
|
||||
|
||||
+2
-2
@@ -127,8 +127,8 @@ class NativeAnimatedNodesManager {
|
||||
AnimationMutations &mutations,
|
||||
AnimatedPropsBuilder &propsBuilder,
|
||||
bool hasLayoutUpdates = false);
|
||||
AnimationMutations onAnimationFrameForBackend(AnimatedPropsBuilder &propsBuilder, double timestamp);
|
||||
AnimationMutations pullAnimationMutations();
|
||||
AnimationMutations onAnimationFrameForBackend(AnimatedPropsBuilder &propsBuilder, AnimationTimestamp timestamp);
|
||||
AnimationMutations pullAnimationMutations(AnimationTimestamp timestamp);
|
||||
#endif
|
||||
|
||||
#pragma mark - Drivers
|
||||
|
||||
+3
-4
@@ -50,7 +50,7 @@ AnimationBackend::AnimationBackend(
|
||||
react_native_assert(uiManager_.expired() == false);
|
||||
}
|
||||
|
||||
void AnimationBackend::onAnimationFrame(double timestamp) {
|
||||
void AnimationBackend::onAnimationFrame(AnimationTimestamp timestamp) {
|
||||
std::vector<CallbackWithId> callbacksCopy;
|
||||
std::unordered_map<SurfaceId, SurfaceUpdates> surfaceUpdates;
|
||||
std::set<SurfaceId> asyncFlushSurfaces;
|
||||
@@ -61,7 +61,7 @@ void AnimationBackend::onAnimationFrame(double timestamp) {
|
||||
}
|
||||
|
||||
for (auto& callbackWithId : callbacksCopy) {
|
||||
auto mutations = callbackWithId.callback(static_cast<float>(timestamp));
|
||||
auto mutations = callbackWithId.callback(timestamp);
|
||||
asyncFlushSurfaces.merge(mutations.asyncFlushSurfaces);
|
||||
for (auto& mutation : mutations.batch) {
|
||||
const auto family = mutation.family;
|
||||
@@ -119,8 +119,7 @@ void AnimationBackend::stop(CallbackId callbackId) {
|
||||
}
|
||||
|
||||
void AnimationBackend::trigger() {
|
||||
onAnimationFrame(
|
||||
std::chrono::steady_clock::now().time_since_epoch().count() / 1000);
|
||||
onAnimationFrame(std::chrono::steady_clock::now().time_since_epoch());
|
||||
}
|
||||
|
||||
void AnimationBackend::commitUpdates(
|
||||
|
||||
+2
-2
@@ -37,7 +37,7 @@ struct AnimationMutations {
|
||||
std::set<SurfaceId> asyncFlushSurfaces;
|
||||
};
|
||||
|
||||
using Callback = std::function<AnimationMutations(float)>;
|
||||
using Callback = std::function<AnimationMutations(AnimationTimestamp)>;
|
||||
|
||||
struct CallbackWithId {
|
||||
CallbackId callbackId;
|
||||
@@ -58,7 +58,7 @@ class AnimationBackend : public UIManagerAnimationBackend {
|
||||
void clearRegistry(SurfaceId surfaceId) override;
|
||||
void registerJSInvoker(std::shared_ptr<CallInvoker> jsInvoker) override;
|
||||
|
||||
void onAnimationFrame(double timestamp) override;
|
||||
void onAnimationFrame(AnimationTimestamp timestamp) override;
|
||||
void trigger() override;
|
||||
CallbackId start(const Callback &callback) override;
|
||||
void stop(CallbackId callbackId) override;
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ class AnimationChoreographer {
|
||||
{
|
||||
animationBackend_ = animationBackend;
|
||||
}
|
||||
void onAnimationFrame(float timestamp) const
|
||||
void onAnimationFrame(AnimationTimestamp timestamp) const
|
||||
{
|
||||
if (auto animationBackend = animationBackend_.lock()) {
|
||||
animationBackend->onAnimationFrame(timestamp);
|
||||
|
||||
+4
-2
@@ -14,15 +14,17 @@
|
||||
namespace facebook::react {
|
||||
|
||||
struct AnimationMutations;
|
||||
|
||||
using AnimationTimestamp = std::chrono::duration<double, std::milli>;
|
||||
using CallbackId = uint64_t;
|
||||
|
||||
class UIManagerAnimationBackend {
|
||||
public:
|
||||
using Callback = std::function<AnimationMutations(float)>;
|
||||
using Callback = std::function<AnimationMutations(AnimationTimestamp)>;
|
||||
|
||||
virtual ~UIManagerAnimationBackend() = default;
|
||||
|
||||
virtual void onAnimationFrame(double timestamp) = 0;
|
||||
virtual void onAnimationFrame(AnimationTimestamp timestamp) = 0;
|
||||
virtual CallbackId start(const Callback &callback) = 0;
|
||||
virtual void stop(CallbackId callbackId) = 0;
|
||||
virtual void clearRegistry(SurfaceId surfaceId) = 0;
|
||||
|
||||
@@ -18,7 +18,7 @@ void TesterAnimationChoreographer::pause() {
|
||||
isPaused_ = true;
|
||||
}
|
||||
|
||||
void TesterAnimationChoreographer::runUITick(float timestamp) {
|
||||
void TesterAnimationChoreographer::runUITick(AnimationTimestamp timestamp) {
|
||||
if (!isPaused_) {
|
||||
onAnimationFrame(timestamp);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class TesterAnimationChoreographer : public AnimationChoreographer {
|
||||
public:
|
||||
void resume() override;
|
||||
void pause() override;
|
||||
void runUITick(float timestamp);
|
||||
void runUITick(AnimationTimestamp timestamp);
|
||||
|
||||
private:
|
||||
bool isPaused_{false};
|
||||
|
||||
@@ -265,10 +265,9 @@ void TesterAppDelegate::produceFramesForDuration(double milliseconds) {
|
||||
|
||||
void TesterAppDelegate::runUITick() {
|
||||
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
|
||||
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
StubClock::now().time_since_epoch())
|
||||
.count();
|
||||
animationChoreographer_->runUITick(static_cast<float>(microseconds) / 1000);
|
||||
auto milliseconds = std::chrono::duration_cast<AnimationTimestamp>(
|
||||
StubClock::now().time_since_epoch());
|
||||
animationChoreographer_->runUITick(milliseconds);
|
||||
} else if (onAnimationRender_) {
|
||||
onAnimationRender_();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user