Skip to content

Apply animated opacity and transform to Android views through typed setters - #60471

Open
hryhoriiK97 wants to merge 1 commit into
react:mainfrom
hryhoriiK97:typed-animated-props-android
Open

hryhoriiK97 wants to merge 1 commit into
react:mainfrom
hryhoriiK97:typed-animated-props-android

Conversation

@hryhoriiK97

@hryhoriiK97 hryhoriiK97 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

With the shared animation backend (useSharedAnimatedBackend, on in the OSS stable release level), Native Animated updates reach Android through AnimatedPropBufferEncoder.cpp, which already encodes each view's animated props into typed int and double buffers. BatchedAnimatedPropsMountItem then turns those buffers back into props, every frame, for every animated view:

  • it builds a JavaOnlyMap, and a JavaOnlyArray of single-key maps for transform;
  • SurfaceMountingManager.storeSynchronousMountPropsOverride copies them into HashMaps (toHashMap) for the synchronous mount override;
  • ViewManager.updateProperties dispatches each prop through the generic delegate, compares the new transform array with the previous one, reparses it in TransformHelper.processTransform, and runs onAfterUpdateTransaction, which also refreshes the accessibility delegate and reapplies filters and the layer type.

In a simpleperf profile of 100 Animated.Views animating opacity, translateX and scale with the native driver (Galaxy A16, release build), this Java side is 54% of the main thread's samples: updateProperties 36%, the override copies 10%, the decode about 5%. The allocations also keep the GC thread busy (7% of the process' samples).

With this change. Behind the new enableTypedAnimatedPropsAndroid feature flag (default false), a view whose animated props are only opacity and pixel transforms (translateX/translateY in px, scale, scaleX, scaleY, rotate, rotateX, rotateY, rotateZ, perspective) and that is managed by the core View manager skips all of that:

  • BatchedAnimatedPropsMountItem reads its commands into primitive arrays instead of maps;
  • SurfaceMountingManager.updateAnimatedPropsTyped calls the new package-private BaseViewManager.applyAnimatedProps, which calls setOpacity and builds the transform matrix from the primitive operations (AnimatedTransform.computeMatrix, the same math as TransformHelper, including transformOrigin), sharing the decomposition with setTransformProperty;
  • the synchronous mount override is stored typed and only turned into props when a React update for that view reads it, with the same values the props path would have stored.

Everything else takes the existing path, decided per view: percentages, skewX/skewY, matrix, colors and other props, raw props, and views of other managers, including subclasses of the View manager. The typed transform is kept in a view tag, so a size change or a later transformOrigin update recomputes it as the props path would, and the raw transform tag is cleared while it is active, so the next regular transform value (including null) always applies. Backface-dependent opacity is updated after a typed transform, as ReactViewManager.setTransformProperty does. No public API changes.

One difference from the props path: the typed path doesn't replace ViewState.currentProps with the animated subset. The View manager doesn't read them in updateState; AnimatedPropsApplier.supports documents what to check before covering another manager.

Follow-ups. I kept this PR to the core View manager, where the native driver is used most. Happy to follow up with:

  1. Other view managers (Image, Text, ScrollView), each after checking its own transform and opacity handling.
  2. The C++ side: after this change, NativeAnimatedNodesManager::updateNodes and collectViewUpdates, which build a folly::dynamic per view per frame, are the largest remaining part of the frame (39% of the main thread's samples in the profile above). That part is shared with iOS.
  3. A typed synchronous update entry point for libraries. Reanimated, without its animation backend flag, encodes its own typed buffers, decodes them into a ReadableMap and calls MountingManager.updatePropsSynchronously through reflection, because synchronouslyUpdateViewOnUIThread seeds the synchronous mount override (software-mansion/react-native-reanimated#9681). The typed path here could back a public API for that.

Changelog:

[Internal]

Test Plan:

  • yarn featureflags regenerated the accessors; yarn featureflags --verify-unchanged passes. yarn format-kotlin and yarn format-java applied, yarn flow-check passes.

  • Unit tests.

    • BatchedAnimatedPropsMountItemTest: typed routing; every fallback (percent translation, matrix, a color next to opacity, raw props) in one batch; a percent translation after pixel operations; a view the typed path declines; NaN opacity and an empty transform; a later transform replacing an earlier one; with the flag on, a failing typed apply isn't applied again through props, and a declined view is still queued as a SynchronousMountItem.
    • AnimatedTransformTest: computeMatrix matches TransformHelper.processTransform for the same operations within 1e-12, with and without transformOrigin, and the stored override equals what the props path stores.
    • BaseViewManagerAnimatedPropsTest (Robolectric, typed path compared with the props path on the same view): opacity and transform with transformOrigin, a later transformOrigin-only update, a size change, hidden backface, a regular and a null transform replacing the typed one, and recycling.
    • SurfaceMountingManagerTypedAnimatedPropsTest: the typed override wins over a stale React update, later values win in either order with the props path, a null update clears it, deleting the view drops it, and views of other managers are declined.
    • ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.fabric.*" --tests "com.facebook.react.uimanager.*" --tests "com.facebook.react.views.view.*": 475 passed.
  • Same behavior. The video shows the Playground below with the flag off (left) and on (right): the same release APK, launched once per arm and recorded with scrcpy on an OPPO CPH2269 (Android 11). The two recordings are aligned to the same point of the 1 s animation loop. To check this beyond eyeballing, I tracked the average brightness of the animated area in every frame, which follows the animated opacity: after aligning the recordings, the two arms correlate at 0.985 over 5 seconds, with the same range (190.5–218.7 with the flag off, 190.6–218.7 with it on). On the Galaxy A16, comparing two frames 250 ms apart changes about 30% of the pixels in both arms.

    Flag off and flag on side by side

    Full-quality MP4

  • Measurement. RNTester release build (assembleRelease, arm64, AOT-compiled with cmd package compile -m speed) on a Galaxy A16 (Helio G99, Android 16). The Playground rendered 100 Animated.Views animating opacity, translateX and scale in a loop with useNativeDriver: true (code below). Both arms are the same binary; for the measurement only, RNTesterApplication overrode the release-level flags from a system property (same values as ReactNativeFeatureFlagsOverrides_RNOSS_Stable_Android, plus this flag). Runs alternated between the arms, 4 per arm; each run sampled per-thread CPU time from /proc/<pid>/task/*/stat over 23 seconds, 6 seconds after launch.

100 Animated.Views, opacity + translateX + scale, native driver, CPU per 23 s (median of 4) before this PR change
main thread 10,645 ms 8,740 ms −17.9%
GC (HeapTaskDaemon) 1,005 ms 245 ms −76%
RenderThread 8,040 ms 7,465 ms −7.1%

The main-thread ranges don't overlap (before 10,360–10,930 ms, this PR 8,190–8,920 ms); an earlier build of this change gave the same result over 6 runs per arm (−18.3%). Frame times didn't change measurably: the 90th percentile from dumpsys gfxinfo was 22–24 ms in both arms, and in the earlier runs both arms occasionally reached 36 ms.

Playground code
function Playground() {
  const progress = useRef(new Animated.Value(0)).current;
  useEffect(() => {
    const loop = Animated.loop(
      Animated.timing(progress, {
        toValue: 1,
        duration: 1000,
        easing: Easing.inOut(Easing.quad),
        useNativeDriver: true,
      }),
    );
    loop.start();
    return () => loop.stop();
  }, [progress]);
  return (
    <View style={styles.container}>
      {Array.from({length: 100}, (_, i) => (
        <Animated.View
          key={i}
          style={[
            styles.box,
            {
              opacity: progress.interpolate({inputRange: [0, 1], outputRange: [0.3, 1]}),
              transform: [
                {translateX: progress.interpolate({inputRange: [0, 1], outputRange: [0, (i % 5) * 4]})},
                {scale: progress.interpolate({inputRange: [0, 1], outputRange: [0.8, 1.1]})},
              ],
            },
          ]}
        />
      ))}
    </View>
  );
}
  • iOS is not affected: the change is in the Android mounting layer only. Only one Android device was measured.

🤖 Generated with Claude Code

…etters

The shared animation backend already encodes animated props into typed int
and double buffers in C++. On Android, BatchedAnimatedPropsMountItem decoded
them back into a JavaOnlyMap per view per frame, then applied it through the
generic ViewManager.updateProperties path, which reparses the transform array,
dispatches every prop through the generic delegate, reapplies filters and the
layer type in onAfterUpdateTransaction, and copies the props into HashMaps to
store the synchronous mount override.

Behind the new enableTypedAnimatedPropsAndroid flag, views managed by the core
View manager whose animated props are only opacity and pixel transforms now
skip all of that: their values go straight from the buffers to typed setters,
the transform matrix is built from primitive operations, and the synchronous
mount override is stored typed until a React update needs it as props.
Anything else falls back to the existing path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 2, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Dec 2, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 2, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Visual regression

No visual differences at e45cb835341d.

android: no capture

@zeyap

zeyap commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

The improvement is real, but it seems this overlaps heavily with RFC #1013 (Binary Props react-native-community/discussions-and-proposals#1013), and that RFC explicitly lists "Introduce a fast path for animated props" like Reanimated as a rejected alternative because it isn't general and grows the number of update flows. The general win from that RFC will probably make this PR obsolete, unless we intentionally want this as a stopgap before the RFC is implemented.

@zeyap zeyap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per last comment

@bartlomiejbloniarz

Copy link
Copy Markdown
Contributor

I am currently preparing the implementation of that RFC (props 3.0), so this might get superseeded soon. But that effort is combined with the pull mode, so it might take some additional time rolling out. I think we could land this, but I wouldn't put it behind a feature flag, because then it might never roll out before props 3.0 and the effort is wasted. @zeyap wdyt?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants