Files
react-native/ReactAndroid/src/main/java/com/facebook/react/animated/FrameBasedAnimationDriver.java
T
Harry Yu 4db7a10e25 Prevent crash in runAnimationStep on OnePlus and Oppo devices (#37487)
Summary:
We've been encountering a crash in `runAnimationStep` with "Calculated frame index should never be lower than 0" https://github.com/facebook/react-native/issues/35766 with OnePlus/Oppo devices as well, but don't have one on hand to test.

This just works around the issue: if the time is before the start time of an animation, we shouldn't do anything anyways, so we just log a message instead of throwing while in production. We still throw in debug mode though for easier debugging.

### Hypothesis of the root cause

Based on stacktrace in https://github.com/facebook/react-native/issues/35766 (which is the same one we see)

Normally, this should happen

1. Choreographer.java constructs a FrameDisplayEventReceiver
2. FrameDisplayEventReceiver.onVSync gets called, which sets the `mTimestampNanos`
3. FrameDisplayEventReceiver.run gets called, which then eventually calls our `doFrame` callback with `mTimestampNanos`. This then causes `FrameBasedAnimationDriver.runAnimationStep` to be called with the same timestamp

I suspect what's happening on OnePlus devices is that the `onVSync` call either doesn't happen or happens rarely enough that the `mTimestampNanos` when `run` is called is sometime in the past

### Fix

1. Add logging so we get the parameters to debug more if we end up getting this error
2. In production, just ignore past times instead of throwing an Error

## Changelog:

Pick one each for the category and type tags:

[ANDROID] [FIXED] - Prevent crash on OnePlus/Oppo devices in runAnimationStep

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

Test Plan: Ran our app using patched version and verified no issues showed up when using it

Reviewed By: cipolleschi

Differential Revision: D46102968

Pulled By: cortinico

fbshipit-source-id: bcb36a0c2aed0afdb8e7e68b141a3db4eb02695a
2023-07-04 14:49:04 +02:00

106 lines
3.4 KiB
Java

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.animated;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.build.ReactBuildConfig;
/**
* Implementation of {@link AnimationDriver} which provides a support for simple time-based
* animations that are pre-calculate on the JS side. For each animation frame JS provides a value
* from 0 to 1 that indicates a progress of the animation at that frame.
*/
class FrameBasedAnimationDriver extends AnimationDriver {
// 60FPS
private static final double FRAME_TIME_MILLIS = 1000d / 60d;
private long mStartFrameTimeNanos;
private double[] mFrames;
private double mToValue;
private double mFromValue;
private int mIterations;
private int mCurrentLoop;
FrameBasedAnimationDriver(ReadableMap config) {
resetConfig(config);
}
@Override
public void resetConfig(ReadableMap config) {
ReadableArray frames = config.getArray("frames");
int numberOfFrames = frames.size();
if (mFrames == null || mFrames.length != numberOfFrames) {
mFrames = new double[numberOfFrames];
}
for (int i = 0; i < numberOfFrames; i++) {
mFrames[i] = frames.getDouble(i);
}
if (config.hasKey("toValue")) {
mToValue = config.getType("toValue") == ReadableType.Number ? config.getDouble("toValue") : 0;
} else {
mToValue = 0;
}
if (config.hasKey("iterations")) {
mIterations =
config.getType("iterations") == ReadableType.Number ? config.getInt("iterations") : 1;
} else {
mIterations = 1;
}
mCurrentLoop = 1;
mHasFinished = mIterations == 0;
mStartFrameTimeNanos = -1;
}
@Override
public void runAnimationStep(long frameTimeNanos) {
if (mStartFrameTimeNanos < 0) {
mStartFrameTimeNanos = frameTimeNanos;
if (mCurrentLoop == 1) {
// initiate start value when animation runs for the first time
mFromValue = mAnimatedValue.mValue;
}
}
long timeFromStartMillis = (frameTimeNanos - mStartFrameTimeNanos) / 1000000;
int frameIndex = (int) Math.round(timeFromStartMillis / FRAME_TIME_MILLIS);
if (frameIndex < 0) {
String message =
"Calculated frame index should never be lower than 0. Called with frameTimeNanos "
+ frameTimeNanos
+ " and mStartFrameTimeNanos "
+ mStartFrameTimeNanos;
if (ReactBuildConfig.DEBUG) {
throw new IllegalStateException(message);
} else {
FLog.w(ReactConstants.TAG, message);
return;
}
} else if (mHasFinished) {
// nothing to do here
return;
}
double nextValue;
if (frameIndex >= mFrames.length - 1) {
nextValue = mToValue;
if (mIterations == -1 || mCurrentLoop < mIterations) { // looping animation, return to start
mStartFrameTimeNanos = -1;
mCurrentLoop++;
} else { // animation has completed, no more frames left
mHasFinished = true;
}
} else {
nextValue = mFromValue + mFrames[frameIndex] * (mToValue - mFromValue);
}
mAnimatedValue.mValue = nextValue;
}
}