Repository navigation
fix(composition): Stop detached infinite animations driving frames - #25055
MartinZikmund wants to merge 2 commits into
Conversation
An animation on a visual below the root of a removed subtree stayed registered with the window's target, so Compositor kept requesting a frame every vsync and rooted the detached subtree. Unbounded animations whose host loses its target are now parked weakly and resume on re-attach; animations started while detached tick once attached. Targets are re-validated only on frames after a tree change. Fixes #25054 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NL1zD4nmcDjrcPhceNXFNm
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NL1zD4nmcDjrcPhceNXFNm
|
Claude finished @MartinZikmund's task in 22s —— View job Review of #25055Verdict: ship, with minor suggestions. The approach is sound. It is a compositor-side fix with a version-gated revalidation (steady-state frames are unaffected), weak parking for detached infinite animations, and tests that fail on master and pass with the fix. I only read the diff and did not build or run any tests. Issues: 0 blocking, 1 minor (posted inline: no de-dup or pruning for the parked list). Cross-cutting notes
|
| } | ||
|
|
||
| private void AddDetachedAnimation(CompositionAnimation animation, CompositionObject host) | ||
| => (_detachedAnimations ??= new()).Add((new(animation), new(host))); |
There was a problem hiding this comment.
Minor (robustness): the parked list is only pruned inside RefreshAnimationTargets, which runs only when _visualTreeVersion changes and a frame renders. If animations are repeatedly started on never-attached hosts and the tree is quiet, dead weak entries accumulate. Also nothing prevents the same animation being parked twice (e.g. Start called repeatedly while detached), which would later hit the ContainsKey guard and leave a duplicate entry. Consider a de-dup check here (RemoveDetachedAnimation first) and an opportunistic prune.
| public partial class Compositor | ||
| { | ||
| private Dictionary<CompositionAnimation, ICompositionTarget> _runningAnimations = new(); | ||
| private Dictionary<CompositionAnimation, RunningAnimation> _runningAnimations = new(); |
| else if (!RemoveDetachedAnimation(animation)) | ||
| { | ||
| if (this.Log().IsDebugEnabled()) | ||
| { | ||
| this.Log().Debug($"Cannot unregister unknown animation"); | ||
| } | ||
| } |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-25055/wasm-skia-net9/index.html |
|
The build 237644 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
Verified at head 4a18af5: read the diff and the surrounding code in Compositor.skia.cs, Visual.cs, ContainerVisual.skia.cs, KeyFrameEvaluator.cs, and the new tests. The AnimatedVisualPlayer citation is accurate: AnimatedVisualPlayer::OnUnloaded calls UnloadContent() (controls/dev/AnimatedVisualPlayer/AnimatedVisualPlayer.cpp, lines 439-451). Infinite keyframe animations wrap their progress (KeyFrameEvaluator, WrapProgress), so a parked infinite animation resumes at the phase it would have reached. That part of the design holds. The claim that WinUI composition animations survive tree changes cannot be checked here: the composition engine is not in microsoft-ui-xaml, which has only forward declarations.
Validation: code review only. I did not build, and I did not run the runtime tests.
Findings, most severe first:
-
A finite animation started while detached finishes early (medium, behaviour bug). The PR says animations started while detached "start once attached". That is not true for finite animations.
ScalarKeyFrameAnimation.Startcreates the evaluator, whose clock starts at that moment, andAdvanceToCurrentTimestampadds the whole wall-clock gap on the first evaluation. A finite animation started on a detached visual and attached after its duration jumps straight to its final value. Attached sooner, it starts mid-way. Infinite animations are not affected, because their progress wraps. Suggest restarting the evaluator clock when a parked finite animation is first registered, and only then. Add a runtime test: start a finite opacity animation on a detached child, wait longer than its duration, attach, and assert that it starts at the beginning and plays its full duration. -
The parked list grows without bound between tree changes (medium).
_detachedAnimationsis pruned only inRefreshAnimationTargets, which runs only after a tree-version bump. Each entry holds twoWeakReferenceobjects, and eachUnregisterAnimationfor a parked animation does a linear scan. A page that starts many animations on visuals that are never attached, and makes no tree change, accumulates entries. Suggest pruning dead entries when the list has doubled since the last prune, and avoiding the linear scan on removal. -
Test gaps (low). Nothing covers (a) the finite-started-detached case above, or (b) a parked animation whose caller drops its own reference, which is the case the weak-hold design depends on. In
Given_KeyFrameAnimation_Detached.cs, the#elsebranches are unreachable under[PlatformCondition(... Skia)], and the file-level handling is redundant. -
Per-frame cost (low, question).
RefreshAnimationTargetswalks every running and parked animation on each frame in which the tree version changed. That is fine at steady state. During virtualised list churn it is O(animations) per frame. Please say whether this was measured.
|
Tick the box to add this pull request to the merge queue (same as
|
GitHub Issue: closes #25054
PR Type:
🐞 Bugfix
What changed? 🚀
A composition animation on a visual below the root of a removed subtree kept running after the subtree left the tree.
ContainerVisualstops animations only on the directly removed child, andCompositor.RegisterAnimationcaptured the window's target at start time. So an infinite animation (e.g. aProgressRing/Lottie inside a removed panel):Compositor.RenderRootVisualrequesting a new frame every vsync, forever (if (_runningAnimations.Count > 0) ... RequestNewFrame()): constant rendering at 60/120 Hz, heat, battery;_runningAnimations→ animation →AnimationFramehandler → visual → parent chain.The fix (compositor side, Skia):
_runningAnimationsremembers each animation's host. After any change to the composition tree (ContainerVisualchildren change or aVisual.CompositionTargetassignment bumps a version), the next frame re-validates where running animations tick. Steady-state frames skip this.IterationBehavior.Forever) whose host lost its target is unregistered and parked in a list that holds the animation and host weakly: no ticking, no frame requests, and the detached subtree stays collectable. When the host is attached again it resumes; keyframe animations are time-based, so it continues as if it had kept running, which matches WinUI, where composition animations survive tree changes.CompositionScopedBatch).How WinUI deals with this, for context: the composition engine is a separate compositor that only presents connected trees, and WinUI controls explicitly tear down their animations on
Unloaded(e.g.AnimatedVisualPlayer::OnUnloaded→UnloadContent(), withComplete()called on unload "so that the AnimationPlay cannot outlive the AnimatedVisualPlayer"). Uno's in-process compositor drives frames from its animation registry, so it needs to know which animations are still connected.Raised in review of #25046, which is now stacked on this PR (#25187 covers the retention in the meantime).
Tests:
Given_KeyFrameAnimation_Detached(runtime, Skia), an infinite opacity animation on a child whose parent is removed:When_Ancestor_Removed_Then_Animation_Stops_Requesting_Frames: fails on master (Compositor.IsAnimatingstays true), passes with the fix.When_Ancestor_Removed_Then_Detached_Visual_Is_Collectable: fails on master (visual kept alive), passes with the fix.When_Ancestor_Readded_Then_Animation_Resumes: passes on both (guards the resume path).Regression:
Windows_UI_Composition,Given_AnimatedVisualPlayer,Given_ProgressRing,AnimatedVisualsandGiven_Storyboardruntime tests on Skia Desktop: 165/165 pass (127/127 re-run after a final hardening for an animation instance shared across targets, TODO #24102).PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
https://claude.ai/code/session_01NL1zD4nmcDjrcPhceNXFNm