Repository navigation
Description
Current behavior 🐛
On the Skia renderer, starting a single KeyFrameAnimation instance on more than one live CompositionObject throws:
System.ArgumentException: An item with the same key has already been added.
Key: Microsoft.UI.Composition.ScalarKeyFrameAnimation
WinUI allows an animation instance to be reused across targets, so this is a parity gap.
The cause is in Compositor.RegisterAnimation (src/Uno.UI.Composition/Composition/Compositor.skia.cs), which tracks running animations in a dictionary keyed by the animation instance rather than by (animation, target):
private Dictionary<CompositionAnimation, ICompositionTarget> _runningAnimations = new();
...
_runningAnimations.Add(animation, target); // throws on the second targetKeyframe animations are exactly the case that trips this, because they are compositor-tracked and are not cloned per target:
| Type | IsTrackedByCompositor |
CloneAnimation() |
Affected |
|---|---|---|---|
KeyFrameAnimation |
true (KeyFrameAnimation.cs:37) |
inherits base → returns this (CompositionAnimation.cs:106) |
✅ yes |
ExpressionAnimation |
false (ExpressionAnimation.cs:24) |
overrides → snapshots per target (ExpressionAnimation.cs:61) |
❌ no |
There is a second, latent ordering bug behind it. CompositionAnimation.Stop() carries no target — CompositionObject.StopAnimation calls it without one — so it unwinds the most recently started registration in LIFO order:
// Stop() carries no target — CompositionObject.StopAnimation calls it without one — and
// Start/Stop are balanced one-to-one per target, so unwind the most recent registration.
var index = _startedObjects.Count - 1;
Compositor.UnregisterAnimation(this, _startedObjects[index]);If the RegisterAnimation throw were fixed on its own, stopping one target could unregister a different, still-running target. Both need to be addressed together.
Expected behavior 🎯
One KeyFrameAnimation instance can be started on several CompositionObjects concurrently, matching WinUI. Stopping it on one target unregisters that target and leaves the others ticking.
How to reproduce it (as minimally and precisely as possible) 🔬
Skia head, with both visuals live in the tree:
var compositor = ElementCompositionPreview.GetElementVisual(someLoadedElement).Compositor;
var v1 = ElementCompositionPreview.GetElementVisual(border1);
var v2 = ElementCompositionPreview.GetElementVisual(border2);
var animation = compositor.CreateScalarKeyFrameAnimation();
animation.InsertKeyFrame(0f, 0f);
animation.InsertKeyFrame(1f, 1f);
animation.Duration = TimeSpan.FromSeconds(1);
v1.StartAnimation("Opacity", animation);
v2.StartAnimation("Opacity", animation); // 💥 ArgumentExceptionObserved probe output:
PROBE_RESULT: secondStartThrew=True type=ArgumentException
msg=An item with the same key has already been added. Key: Microsoft.UI.Composition.ScalarKeyFrameAnimation
Workaround 🛠️
Create a separate animation instance per target instead of sharing one. For animations built in a loop, move the compositor.Create*KeyFrameAnimation() call inside the loop.
Renderer 🎨
- Skia
- Native
Affected platforms 📱💻🖥️
All platforms 🌍 — the affected code is Compositor.skia.cs, so every Skia-rendered target is impacted.
Uno.Sdk version (and other relevant versions) 📦
Reproduced against the current 7.0 development branch (feature/breakingchanges).
IDE version 🧑💻
N/A — framework-level issue, not IDE-specific.
Anything else we need to know? 💬
This is pre-existing, not a regression. It was found while reviewing #24056 (comment). Compositor.skia.cs is byte-identical between that PR and its base branch (git diff origin/feature/breakingchanges...HEAD -- src/Uno.UI.Composition/Composition/Compositor.skia.cs returns no changes), so the behavior is the same before and after. It was flagged rather than fixed there because a proper fix is wider than that PR should carry.
A marker was left in the code at a4088ac35fa:
// TODO Uno: sharing one keyframe animation across several live targets still throws in
// Compositor.RegisterAnimation, which keys running animations by instance rather than by target.Suggested fix direction:
- Key
_runningAnimationsby(animation, target)rather than by the animation instance alone. - Thread the stopping object through — e.g.
Stop(CompositionObject owner)called fromCompositionObject.StopAnimation— so the LIFO unwind inCompositionAnimation.Stop()can be replaced with a targeted removal.
Both are needed; fixing only (1) exposes the ordering bug in (2).
Activity