Files
zed/crates/gpui_platform
Anthony Eid aff99ecc32 gpui: Animate SVG transformations without re-rendering views (#66896)
## Summary

Ref #21146, #23479, #51597

One common problem that I've been working towards fixing by adding a
retained mode is Zed's high battery usage, caused by invalidating clean
entities because a single ancestor is dirty. While looking closer into
this problem I realized that a ton of the invalidations we run into are
due to animations, which can be fixed separately from retained mode.

My solution is adding a `TimedTransformation` struct and a new animation
API that uses it.

```rust
// A spinner: one full turn every two seconds.
svg()
    .path("icons/load_circle.svg")
    .with_timed_transformation(TimedTransformation::rotation(
        Duration::from_secs(2),
        Transformation::default(),
    ))

// A pulsing ring: grows to 1.5x while fading out, each value with its own easing.
svg()
    .path("icons/ring.svg")
    .with_timed_transformation(
        TimedTransformation::new(Duration::from_millis(1750))
            .from(Transformation::alpha(0.6))
            .to(Transformation::scale(size(1.5, 1.5)).with_alpha(0.))
            .with_scale_easing(Easing::EaseOutQuint),
    )
```

Because most of our animations are an SVG sprite with simple matrix
operations applied to it, such as rotation, alpha changes and size
changes, that change based on elapsed time, we can avoid invalidating
the view by extracting the before and after transformations and deriving
a function that takes elapsed time and computes what the animated sprite
should look like.

The extracted function lives in a table on the scene, and each animated
sprite points at its entry. This lets GPUI advance animations right
before presenting a frame, without dirtying the view or recomputing the
scene from scratch. This should save a lot of CPU usage and battery life
for end users of GPUI apps that use animations.

`Icon` and `Vector` in `ui` now use this for `with_rotate_animation`, so
Zed's existing spinners get it with no call site changes. With reduced
motion on, the SVG is drawn at its starting transformation and nothing
animates.

### Results

Main-thread CPU instructions for one animated frame, from a headless
benchmark on macOS with the Metal renderer (not included in this PR):

| | 200 animated SVGs | 60 rows of text + 5 spinners |
|---|---|---|
| Re-render with `with_animation` | 6.07 M | 7.34 M |
| `with_timed_transformation` | 162 K | 100 K |
| Improvement | 37x | 73x |

The `svg_animation` example (`cargo run --release -p gpui --example
svg_animation`, 200 animated SVGs in a window, Apple M4 Max), as
reported by `top`:

| | CPU | Energy impact |
|---|---|---|
| Re-render with `with_animation` | 21% | 21.5 |
| `with_timed_transformation` | 6% | 7.5 |
| `with_timed_transformation`, capped at 15 fps | 1.9% | 3.0 |

That is about 3.5x less CPU and 3x less energy impact uncapped, and
about 11x less CPU and 7x less energy impact with the cap.

### Follow ups

- Allow animating more primitives, such as glyphs, instead of only SVG
sprites
- Lower the fixed cost of presenting a frame, which is most of what
remains after this change
- Retained layers, so a whole subtree can be moved or faded without
re-rendering

Release Notes:

- Improved battery life by animating spinners without re-rendering the
views that contain them
- [GPUI] Added `TimedTransformation` and
`Svg::with_timed_transformation` to animate SVGs without re-rendering a
view
2026-11-13 16:33:49 +00:00
..