Repository navigation
Apply animated opacity and transform to Android views through typed setters - #60471
hryhoriiK97 wants to merge 1 commit into
Conversation
…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>
Visual regressionNo visual differences at android: no capture |
|
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. |
|
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? |
Summary:
With the shared animation backend (
useSharedAnimatedBackend, on in the OSS stable release level), Native Animated updates reach Android throughAnimatedPropBufferEncoder.cpp, which already encodes each view's animated props into typed int and double buffers.BatchedAnimatedPropsMountItemthen turns those buffers back into props, every frame, for every animated view:JavaOnlyMap, and aJavaOnlyArrayof single-key maps fortransform;SurfaceMountingManager.storeSynchronousMountPropsOverridecopies them intoHashMaps (toHashMap) for the synchronous mount override;ViewManager.updatePropertiesdispatches each prop through the generic delegate, compares the new transform array with the previous one, reparses it inTransformHelper.processTransform, and runsonAfterUpdateTransaction, which also refreshes the accessibility delegate and reapplies filters and the layer type.In a simpleperf profile of 100
Animated.Views animating opacity,translateXandscalewith the native driver (Galaxy A16, release build), this Java side is 54% of the main thread's samples:updateProperties36%, 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
enableTypedAnimatedPropsAndroidfeature flag (defaultfalse), a view whose animated props are only opacity and pixel transforms (translateX/translateYin px,scale,scaleX,scaleY,rotate,rotateX,rotateY,rotateZ,perspective) and that is managed by the coreViewmanager skips all of that:BatchedAnimatedPropsMountItemreads its commands into primitive arrays instead of maps;SurfaceMountingManager.updateAnimatedPropsTypedcalls the new package-privateBaseViewManager.applyAnimatedProps, which callssetOpacityand builds the transform matrix from the primitive operations (AnimatedTransform.computeMatrix, the same math asTransformHelper, includingtransformOrigin), sharing the decomposition withsetTransformProperty;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 theViewmanager. The typed transform is kept in a view tag, so a size change or a latertransformOriginupdate recomputes it as the props path would, and the rawtransformtag is cleared while it is active, so the next regulartransformvalue (includingnull) always applies. Backface-dependent opacity is updated after a typed transform, asReactViewManager.setTransformPropertydoes. No public API changes.One difference from the props path: the typed path doesn't replace
ViewState.currentPropswith the animated subset. TheViewmanager doesn't read them inupdateState;AnimatedPropsApplier.supportsdocuments what to check before covering another manager.Follow-ups. I kept this PR to the core
Viewmanager, where the native driver is used most. Happy to follow up with:Image,Text,ScrollView), each after checking its own transform and opacity handling.NativeAnimatedNodesManager::updateNodesandcollectViewUpdates, which build afolly::dynamicper 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.ReadableMapand callsMountingManager.updatePropsSynchronouslythrough reflection, becausesynchronouslyUpdateViewOnUIThreadseeds 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 featureflagsregenerated the accessors;yarn featureflags --verify-unchangedpasses.yarn format-kotlinandyarn format-javaapplied,yarn flow-checkpasses.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 aSynchronousMountItem.AnimatedTransformTest:computeMatrixmatchesTransformHelper.processTransformfor the same operations within 1e-12, with and withouttransformOrigin, 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 withtransformOrigin, a latertransformOrigin-only update, a size change, hidden backface, a regular and anulltransform 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, anullupdate 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
scrcpyon 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.Full-quality MP4
Measurement. RNTester release build (
assembleRelease, arm64, AOT-compiled withcmd package compile -m speed) on a Galaxy A16 (Helio G99, Android 16). The Playground rendered 100Animated.Views animatingopacity,translateXandscalein a loop withuseNativeDriver: true(code below). Both arms are the same binary; for the measurement only,RNTesterApplicationoverrode the release-level flags from a system property (same values asReactNativeFeatureFlagsOverrides_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/*/statover 23 seconds, 6 seconds after launch.Animated.Views, opacity +translateX+scale, native driver, CPU per 23 s (median of 4)HeapTaskDaemon)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 gfxinfowas 22–24 ms in both arms, and in the earlier runs both arms occasionally reached 36 ms.Playground code
🤖 Generated with Claude Code