Repository navigation
Include animated props in layout reads with the shared animated backend - #60474
fairysword wants to merge 2 commits into
Conversation
ddb271c to
29a5b3e
Compare
zeyap
left a comment
There was a problem hiding this comment.
Thanks for putting up the fix. A few things would make this more convincing on the testing side. #59392's test plan is a good model:
-
Flag-on run with the same setup. The same 35 presses on the same builds with
enableAnimatedPropsInLayoutReadson, reported per platform: how many firedonPress, and whatmeasure()returned at press-in. Ideally 0 dropped, andmeasure()matching the on-screen size or position. -
A breakdown of the 19 dropped presses. How many measured 0 x 0 and how many measured the full box? If some drops measured the full size, the stale read probably isn't the cause, and the flag may not fix those.
-
An RNTester repro. A "Pressable inside a natively animated view" example (or reuse the one from #59392), with the device or simulator and OS you tested on, plus a before/after line, e.g. "on
maina press with finger drift only firesonPressIn; with the flag it firesonPressInandonPress." -
Test Plan and Changelog sections in the PR description, listing the commands you ran (
yarn fantom …, the gtests) and anything you couldn't run locally.
|
@zeyap Thanks for the detailed review. I've rewritten the Test Plan along these lines:
|
29a5b3e to
7bf826b
Compare
Experimental flag, off by default, for including the animated props of the shared animated backend in layout reads from JS (measure, measureInWindow, measureLayout, getBoundingClientRect, ...). Nothing reads it yet. Generated with `yarn featureflags --update`. Signed-off-by: jh.xiong <uabearbest@gmail.com>
With the shared animated backend, non-layout props such as transform are written to the views and to AnimatedPropsRegistry on every frame, but not committed to the shadow tree. AnimationBackendCommitHook applies the registry when React commits, so renders don't revert running animations. Layout reads from JS (measure, measureInWindow, measureLayout, getBoundingClientRect, ...) read the revision captured by LazyShadowTreeRevisionConsistencyManager though, which doesn't include these props. Pressability measures the pressable on press in, so a view moved or scaled by a native-driven animation is measured at its old position and the first move cancels the press (react#51621). Behind the enableAnimatedPropsInLayoutReads flag, the revision that JS reads is cloned with the props from the registry, with the same cloning as the commit hook (moved into cloneWithAnimatedProps). The clones are only used for reads: RSNRU is suspended while cloning, so the references held by React keep pointing to committed nodes. The tree is cloned when JS reads the shadow tree (layout reads, but also DOM traversal like parentNode), not when React commits. Behind the flag, completeSurface only drops the revision captured for the surface, and the next read captures it again with the props from the registry. Capturing it in completeSurface would call AnimatedPropsRegistry::getMap before clearRegistry, which would then clear the props that the UI thread wrote after the commit hook, although they were never committed. This way, commits that are not followed by such a read also don't clone the tree. This covers C++ Animated with the shared backend only. The platform Animated modules don't write to the registry and are unchanged. One of the Fantom tests reads the layout in render and in a layout effect of the same task, so the read after the commit has to see the new commit rather than the revision captured before it. The consistency manager tests cover that invalidating makes the next read capture and transform the revision again. Signed-off-by: jh.xiong <uabearbest@gmail.com>
7bf826b to
c6ec88c
Compare
Summary:
Pressabilitymeasures a view that a native-driven animation has already moved, but still sees the committed (stale) geometry, so the firstMOVEcancels the press (onPressInfires,onPressdoes not).AnimatedPropsRegistryand the same cloning path asAnimationBackendCommitHook: when JS reads layout (measure,measureInWindow,measureLayout,getBoundingClientRect, …), the revision it sees also has the registry values written into the node props.enableAnimatedPropsInLayoutReads(default off, experimental). Two commits: flag generation, then the fix.Problem
With
useNativeDriver: trueand the shared animated backend:transformare written straight to the mounted views and recorded inAnimatedPropsRegistry.AnimationBackendCommitHookalready writes the registry values into the new tree so React does not wipe the running animation.LazyShadowTreeRevisionConsistencyManager, which still has the last committed props.Pressabilitymixes the two worlds: itmeasure()s the responder on press-in (shadow tree), then compares that rect with native touch coordinates on every move (live view). On a moving / scaling view the measured rect is wrong, so the first finger drift looks likeLEAVE_PRESS_RECTandonPressis dropped.Reproducer: the App from #51621 (and the same pattern as RNTester / community demos linked from that issue).
Bug path
sequenceDiagram participant Anim as Shared Animation Backend participant View as Mounted View participant Reg as AnimatedPropsRegistry participant Tree as Committed Shadow Tree participant Press as Pressability participant Measure as measure / getBoundingClientRect loop Each animation frame Anim->>View: Write transform (synchronous) Anim->>Reg: Record current animated props Note over Tree: No per-frame commit end Press->>Measure: On press in Measure->>Tree: Read current revision Tree-->>Press: Stale transform / size Note over Press: Press rect is wrong Press->>Press: MOVE vs stale rect → LEAVE_PRESS_RECT Note over Press: onPressIn yes, onPress noApproach in this PR
The commit hook already knows how to take a root and produce a tree whose animated nodes carry the registry values (
cloneWithAnimatedProps). Layout reads need the same tree, without committing it and without retargeting React's runtime shadow node references at those clones.cloneWithAnimatedProps.UIManagerAnimationBackend::getRootShadowNodeWithAnimatedProps.LazyShadowTreeRevisionConsistencyManageroptionally transform the revision it exposes to JS;UIManagerwires that to the backend whenuseSharedAnimatedBackendandenableAnimatedPropsInLayoutReadsare on.completeSurface, when the flag is on, only drop the revision captured for the surface instead of capturing it again right away (invalidateCurrentRevision). Capturing it there would callAnimatedPropsRegistry::getMapbetween the commit hook andclearRegistry:getMapmoves the props that the UI thread wrote after the commit hook frompendingMapintomap, andclearRegistrythen clears them although they were never committed. If that was the last frame of an animation, the shadow tree keeps the previous value. The next read in the task captures the new commit with the props from the registry, so the tree is also cloned only when JS reads the shadow tree, not on every React commit. With the flag off,completeSurfaceis unchanged.Fix path
sequenceDiagram participant Anim as Shared Animation Backend participant View as Mounted View participant Reg as AnimatedPropsRegistry participant Cons as LazyShadowTreeRevisionConsistencyManager participant Backend as getRootShadowNodeWithAnimatedProps participant Press as Pressability participant Measure as measure / getBoundingClientRect loop Each animation frame Anim->>View: Write transform (synchronous) Anim->>Reg: Record current animated props end Press->>Measure: On press in Measure->>Cons: getCurrentRevision Cons->>Backend: Transform revision for layout reads Backend->>Reg: getMap(surfaceId) Backend->>Backend: cloneWithAnimatedProps (RSNRU off) Backend-->>Cons: Tree with current animated props Cons-->>Measure: Live geometry Measure-->>Press: Correct press rect Note over Press: MOVE stays inside rect → onPress firesReact commit path (commit hook unchanged, shared helper)
Other approaches
Community proposals for the same symptom, listed here for reviewers' reference.
Measure the mounted native view — #51835, #55025
Make
Pressability(or Fabricmeasureitself) measure the UI-thread view hierarchy instead of the shadow tree, like Paper'sNativeViewHierarchyManager. #51835 adds ameasureAsyncOnUIelement method thatPressabilitycalls; #55025 makesmeasure/measureInWindowinUIManagerBindingask the native mounting layer throughSchedulerDelegate.Record synchronous transforms at the write funnel — #59392
Whenever props are written synchronously to a view, record
transform/transformOriginin a small map onUIManager. Layout metrics (computeRelativeLayoutMetrics) consult that map before falling back to committed props.Why this PR
On current
main, OSS stable already enables C++ Native Animated and the shared animated backend. The registry is already the source of truth for “what the backend last applied.” React commits already trust that registry via the commit hook. Layout reads use the same source for the same reason, with the same clone helper.Scope note: This only applies to animation frontends that write through the shared backend into
AnimatedPropsRegistry.Commits
Add enableAnimatedPropsInLayoutReads feature flag— config +yarn featureflags --updateonly.Include animated props in layout reads with the shared animated backend— C++ wiring, docs, Fantom and C++ unit tests.Changelog:
[General][Fixed] - Fix layout reads such as
measureandgetBoundingClientRectnot seeing the props set by running native-driven animations with the shared animated backend, which made Pressable droponPresson views moved or scaled by them, behind the experimentalenableAnimatedPropsInLayoutReadsflagTest Plan:
Commands
Run on this PR, all locally; there was nothing I needed to skip. The last change, which only added the new method to the C++ API snapshots, came after the Fantom and gtest runs.
yarn fantom packages/react-native/Libraries/Animated/__tests__/AnimatedBackendLayoutReads-itest.js: 6/6. Mid-animationmeasure/getBoundingClientRectmatch the running transform; still correct after a re-render that changes layout; a read after a commit in the same task sees the new commit, while a read before it in the same task doesn't; fails when the flag is off.yarn fantom Animated: 382 passed, 3 skipped.LazyShadowTreeRevisionConsistencyManagerTest: 11/11. AfterinvalidateCurrentRevision, the next read captures the new revision and the revision is captured again; the revision transformer only runs on the read, once per lock. Built and run locally through the Fantom CMake build (targetreact_renderer_uimanager_consistency_tests).yarn jest packages/react-native/src/private/featureflags: 4/4.yarn featureflags --verify-unchanged,node scripts/clang-format.js --checkon the changed C++ files,yarn flow-check(no errors in tracked files).yarn cxx-api-validatewith doxygen 1.16.1, as on CI: all 12 snapshots match.RNTester repro
repro/pressable-stale-region-native-animationadds two commits on top of this PR, neither of which is part of it: an RNTester example and a switch for the flag. The example is under Pressable → Pressable with a native-driven animation. It is the #51621 reproducer: anAnimated.createAnimatedComponent(Pressable)whosescaleloops 0 → 1 → 0, 1 s each way, withuseNativeDriver: true. The page shows the flags and theonPressIn/onPresscounts.Each run is 20 presses, each held for 300 ms while moving 5 dp (5 pt on iOS) to the right, starting 60 dp left of the center of the box (
adb shell input swipeon Android,idb ui swipeon iOS). Presses that come while the box is too small to cover that point don't hit it and aren't counted. With the flag off, layout reads andcompleteSurfacerun the same code as onmain.RNTester has no switch for native feature flags, so the second commit adds one, read at app start: a system property on Android and a launch argument on iOS. React Native already sets the overrides for the release level (Stable in RNTester) at startup, so when the switch is on they are replaced with the same Stable overrides plus this flag through
dangerouslyForceOverride.Android. Coordinates are in px (
dp × density / 160, density fromadb shell wm density). The box is centered horizontally; take its vertical center from a screenshot.iOS. Coordinates are in pt; the values are for the iPhone 17 Pro.
Before/after: with the flag off, as on
main, about half of the presses with finger drift only fireonPressIn; with the flag on, every press firesonPressInandonPress.enableAnimatedPropsInLayoutReadsonPressInonPressThe presses aren't synced with the animation, so which half of it each press lands in, and how many presses hit the box, changes from run to run. Across the runs I did on each device the flag-off counts varied (e.g. 12 / 7 and 13 / 6 on the Venus e5, 11 / 5 to 14 / 7 on iOS), but every flag-off run dropped about half and every flag-on run dropped none.
The native builds for the table are from this PR before its last rebase onto
main, with an earlier version of the switch that also logged the flag; since then only a method name and comments changed in this PR. The JS is from the PR head. I also built both apps from the branch as pushed. iOS, run twice more, the second time with the commands above: 11 / 5 and 14 / 7 with the flag off, 12 / 12 and 15 / 15 with it on. Android on the vivo V2338A, with the commands above: 12 / 6 with the flag off, 12 / 12 with it on.Dropped presses
The example only shows the last
measure(), so the breakdown is from an earlier run of the same reproducer that loggedmeasure()on every press-in, with the same presses. The box there is 340 x 250 on Android and 382 x 250 on iOS.onPress0 x 0All 19 drops measured
0 x 0and none measured the full size. The shadow tree only gets the animated scale when eachAnimated.timingsegment ends, so at press-in it has the value from the end of the previous segment. While the box grows, that isscale: 0: the press rect is a point at the center of the box plus the default press rect offsets (20 on each side, 30 at the bottom), so a press 60 dp left of the center leaves it on the first move. While the box shrinks, that isscale: 1: the press rect is the full box, larger than the box on screen, so the move stays inside andonPressfires. That is why about half of the presses get through with the flag off; they also use a stale rect, it is just too large instead of too small.With the flag on, every press that landed fired
onPress(vivo 12/12, Venus 8/8, iOS 15/15), andmeasure()returned sizes between the two, following the scale at the time of the press:177.1 x 130.2to329.4 x 242.2on vivo,166.5 x 122.4to339.6 x 249.7on Venus, e.g.276.0 x 180.6and341.0 x 223.1on iOS.