Skip to content

perf: draw transparent frames once, into a canvas that keeps alpha - #175

Open
remloyal wants to merge 2 commits into
DEEIX-AI:devfrom
remloyal:feat/transparent-damage
Open

remloyal wants to merge 2 commits into
DEEIX-AI:devfrom
remloyal:feat/transparent-damage

Conversation

@remloyal

@remloyal remloyal commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Fixes #174

A frame that keeps alpha was drawn twice, on black and on white, with the opacity recovered from the difference: twice the paint, a whole unblend pass, and a white buffer as large as the frame kept between frames. Every change also repainted the whole target, so the cost followed the window size rather than the change: at 1920x1080 and 2x, updating one label took 31 ms, against 0.19 ms on an opaque target.

Canvas pixels now carry alpha, and blending composites premultiplied pixels, so such a frame is drawn once. Whether a canvas keeps alpha is chosen per primitive, so opaque frames keep their exact pixels: the 28 deterministic images of cpui-snapshots are byte-identical before and after. Transparent windows and offscreen targets now use the normal damage planning, so only what changed is redrawn, and an idle frame costs nothing.

1920x1080 at 2x before after opaque
one label changes 31.1 ms 0.19 ms 0.19 ms
whole frame 32.0 ms 15.4 ms 14.2 ms
no change 0.11 ms 0.11 ms 0.11 ms

The white buffer, about 33 MB at that size and scale, is gone. Renderer::draw_transparent keeps its signature and draws once too.

Checks

  • scripts/check.sh passes
  • Public items have documentation with an example
  • CHANGELOG.md is updated for a change users will notice
  • Guides are updated in both docs/ and docs/zh-CN/, if they are affected
  • cargo run --release -p cpui-bench stays within budget, if per-frame code changed
  • Platforms this was run on: Windows
  • I have read and agree to the Contributor License Agreement

New tests cover premultiplied blending, tinting a capture that keeps alpha, a scaled and faded capture matching on both canvases, and partial transparent frames matching full ones through rotating buffers, scrolling and theme changes. cargo check passes for the minimum Rust (1.89), aarch64-linux-android and wasm32-unknown-unknown.

Canvases kept no alpha, so a frame that keeps alpha was drawn twice, on black
and on white, with the opacity recovered from the difference: twice the paint, a
whole unblend pass, and a white buffer as large as the frame kept between
frames. Frames were drawn whole, too: every change repainted everything.

Canvas pixels now carry alpha, and blending composites premultiplied pixels, so
a transparent frame is drawn once. The two passes, their unblend and the white
buffer are gone, and transparent windows and offscreen targets use the normal
damage planning: a frame redraws only what changed, and an idle frame costs
nothing. Whether a canvas keeps alpha is chosen per primitive, so opaque frames
keep their exact pixels.

The Windows layered presenter now reports its bitmap as one frame old, which
lets transparent windows redraw only what changed. Offscreen's builders keep
their target transparent, which with_buffers, with_threads and with_hinting
dropped.
The new unit test only asserted that repeated blending does not lower the
opacity, which a destination alpha that never grows passes, and it blended onto
a clear canvas only, where the destination's alpha is skipped. The transparent
partial test compared two targets without checking that either keeps alpha, so
it passed with transparency disabled.

Each now fails when the behavior it names is broken: the unit test pins the
opacity three quarter-opaque layers reach, and how the destination's alpha and
color add up; the partial test requires clear pixels in the frame it compares.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redraw only what changed on transparent targets, and draw them once

1 participant