Skip to content

Commit

Permalink
Adjust animation batch numbers to be consistent when controlled by na…
Browse files Browse the repository at this point in the history
…tive

Summary:
D27682424 (ea1ff37) updated how animated node batches are executed in Fabric. On Paper, these batches were controlled by native module in some places (batch was executed ~every 2 frames), but some animations were switching animation batching control to JS globally there as well.

This change updates two things:
- If batching is controlled by native, it makes sure batches are calculated correctly.
- At the same time, this change switches control for animation node batching to JS, aligning it with Fabric.

Changelog: [Internal]

Reviewed By: JoshuaGross, mdvacca

Differential Revision: D27939659

fbshipit-source-id: d6251bce2a303a4a007dc10297edc0175cc4b348
  • Loading branch information
Andrei Shikov authored and facebook-github-bot committed Apr 23, 2021
1 parent de477a0 commit bda032a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
11 changes: 3 additions & 8 deletions Libraries/Animated/__tests__/Animated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
import TestRenderer from 'react-test-renderer';
import * as React from 'react';

jest.mock('../../BatchedBridge/NativeModules', () => ({
NativeAnimatedModule: {},
PlatformConstants: {
getConstants() {
return {};
},
},
}));
jest.mock('../NativeAnimatedHelper', () => {
return jest.requireActual('../NativeAnimatedHelper');
});

let Animated = require('../Animated');
describe('Animated tests', () => {
Expand Down
12 changes: 3 additions & 9 deletions Libraries/Animated/__tests__/AnimatedNative-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@

jest
.clearAllMocks()
.mock('../../BatchedBridge/NativeModules', () => ({
NativeAnimatedModule: {},
PlatformConstants: {
getConstants() {
return {};
},
},
}))
.mock('../NativeAnimatedModule')
.mock('../NativeAnimatedHelper', () => {
return jest.requireActual('../NativeAnimatedHelper');
})
.mock('../../EventEmitter/NativeEventEmitter')
// findNodeHandle is imported from ReactNative so mock that whole module.
.setMock('../../Renderer/shims/ReactNative', {findNodeHandle: () => 1});
Expand Down
16 changes: 6 additions & 10 deletions Libraries/Animated/createAnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,15 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
};

_waitForUpdate = (): void => {
if (this._isFabric()) {
NativeAnimatedHelper.API.setWaitingForIdentifier(
this._animatedComponentId,
);
}
NativeAnimatedHelper.API.setWaitingForIdentifier(
this._animatedComponentId,
);
};

_markUpdateComplete = (): void => {
if (this._isFabric()) {
NativeAnimatedHelper.API.unsetWaitingForIdentifier(
this._animatedComponentId,
);
}
NativeAnimatedHelper.API.unsetWaitingForIdentifier(
this._animatedComponentId,
);
};

// The system is best designed when setNativeProps is implemented. It is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void onHostResume() {
}

private void addOperation(UIThreadOperation operation) {
operation.setBatchNumber(mIsInBatch ? mCurrentBatchNumber : -1);
operation.setBatchNumber(getCurrentBatchNumber());
mOperations.add(operation);
}

Expand All @@ -184,7 +184,7 @@ private void addUnbatchedOperation(UIThreadOperation operation) {
}

private void addPreOperation(UIThreadOperation operation) {
operation.setBatchNumber(mIsInBatch ? mCurrentBatchNumber : -1);
operation.setBatchNumber(getCurrentBatchNumber());
mPreOperations.add(operation);
}

Expand Down Expand Up @@ -342,6 +342,13 @@ private void enqueueFrameCallback() {
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, mAnimatedFrameCallback);
}

private long getCurrentBatchNumber() {
if (mBatchingControlledByJS && !mIsInBatch) {
return -1;
}
return mCurrentBatchNumber;
}

@VisibleForTesting
public void setNodesManager(NativeAnimatedNodesManager nodesManager) {
mNodesManager.set(nodesManager);
Expand Down Expand Up @@ -426,13 +433,19 @@ private void decrementInFlightAnimationsForViewTag(final int viewTag) {

@Override
public void startOperationBatch() {
if (ANIMATED_MODULE_DEBUG) {
FLog.d(NAME, "Start JS operation batch " + mCurrentBatchNumber);
}
mBatchingControlledByJS = true;
mIsInBatch = true;
mCurrentBatchNumber++;
}

@Override
public void finishOperationBatch() {
if (ANIMATED_MODULE_DEBUG) {
FLog.d(NAME, "Finish JS operation batch " + mCurrentBatchNumber);
}
mBatchingControlledByJS = true;
mIsInBatch = false;
mCurrentBatchNumber++;
Expand Down
33 changes: 33 additions & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ jest
doLeftAndRightSwapInRTL: true,
}),
},
NativeAnimatedModule: {
startOperationBatch: jest.fn(),
finishOperationBatch: jest.fn(),
createAnimatedNode: jest.fn(),
getValue: jest.fn(),
startListeningToAnimatedNodeValue: jest.fn(),
stopListeningToAnimatedNodeValue: jest.fn(),
connectAnimatedNodes: jest.fn(),
disconnectAnimatedNodes: jest.fn(),
startAnimatingNode: jest.fn(),
stopAnimation: jest.fn(),
setAnimatedNodeValue: jest.fn(),
setAnimatedNodeOffset: jest.fn(),
flattenAnimatedNodeOffset: jest.fn(),
extractAnimatedNodeOffset: jest.fn(),
connectAnimatedNodeToView: jest.fn(),
disconnectAnimatedNodeFromView: jest.fn(),
restoreDefaultValues: jest.fn(),
dropAnimatedNode: jest.fn(),
addAnimatedEventToView: jest.fn(),
removeAnimatedEventFromView: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
},
}))
.mock('../Libraries/NativeComponent/NativeComponentRegistry', () => {
return {
Expand Down Expand Up @@ -365,4 +389,13 @@ jest
__esModule: true,
default: Component,
};
})
.mock('../Libraries/Animated/NativeAnimatedHelper.js', () => {
const NativeAnimatedHelper = jest.requireActual(
'../Libraries/Animated/NativeAnimatedHelper.js',
);
return {
...NativeAnimatedHelper,
shouldUseNativeDriver: jest.fn(false),
};
});

0 comments on commit bda032a

Please sign in to comment.