Skip to content

perf(wasm): Repaint only damaged regions on WebGL - #26041

Open
MartinZikmund wants to merge 3 commits into
masterfrom
dev/mazi/wasm-damage-repaint
Open

MartinZikmund wants to merge 3 commits into
masterfrom
dev/mazi/wasm-damage-repaint

Conversation

@MartinZikmund

@MartinZikmund MartinZikmund commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

GitHub Issue: closes #26040

PR Type:

✨ Feature (performance)

What changed? 🚀

WebGL was the only Skia host that repainted the whole canvas every frame. WasmGLGraphicsContext.PreservesContents was always false, so the compositor's damage-region path never ran, and the canvas carried 4x MSAA and a depth buffer that Skia never uses.

  • perf(wasm): the canvas is created single-sampled, without depth, and with preserveDrawingBuffer, so the compositor repaints only the damaged regions. A resized canvas or a lost context repaints whole. setCanvasSize now floors the size, because re-assigning an unchanged fractional size still clears the preserved buffer. This also removes a C#/TS property-name mismatch (fboId/samples vs fbo/sample).
  • fix(skia): SkiaPresentSession.ClipToDamage snaps the damage outwards to whole pixels and clips without AA. A rect becomes a scissor, and several rects become a region. The fractional AA clip blended cleared and redrawn edge pixels with the retained ones on every host that keeps frames.
  • test(skia): Given_SkiaPresentSession. It fails on the previous AA clip and passes with the fix.

Measured on SamplesApp Progress/ProgressRing, 2560x1600, Chrome:

scenario before after
weak GPU (SwiftShader) 23-25 fps 101-107 fps
weak GPU + 4x CPU throttle, interpreted 26.6 fps 38.7 fps
weak GPU + 4x CPU throttle, profiled AOT 26.7 fps 65.9 fps
hardware GPU + 6x CPU throttle ~23 fps ~23 fps (CPU-bound, neutral)

Retaining frames through an offscreen FBO and blitFramebuffer was measured too, and it was slower (18.6 fps on SwiftShader): the full-canvas copy outweighs the damage savings.

Validation: runtime tests on Skia Desktop (Given_SkiaPresentSession, plus the fail-before check), the WASM SamplesApp checked manually for resizes at 1.25 DPR with odd sizes, hover/press damage and an animating ProgressRing, and no stale or ghosted pixels.

On physical hardware (Pixel Tablet, Tensor G2 / Mali-G710, 2560x1224 canvas, Chrome 157, profiled-AOT build, RealWorld_SpotlightMenu), two runs with the order reversed:

fps draw phase
tablet, unthrottled, before / after 59.9 / 60.2 (vsync-capped) 5.5 ms / 1.2 ms
tablet + 3x CPU throttle (a cheaper phone), before 25.8, 27.5 13.8, 16.1 ms
tablet + 3x CPU throttle, after 36.8, 35.8 1.9, 2.4 ms

A bare full-screen WebGL2 canvas runs at 60 fps on the tablet with MSAA, without it, with preserveDrawingBuffer, and with FBO + blit alike (3 interleaved rounds), so the preserved buffer costs nothing there. On a Galaxy Z Fold7 every full-screen WebGL canvas, whatever its configuration, was capped at ~20 fps while driven over remote debugging, so that device could not separate the variants. It showed no regression either.

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01TXMNu2pLwNk6UenehZFfNA

MartinZikmund and others added 3 commits November 4, 2026 17:31
An antialiased, fractional damage clip blends cleared and redrawn edge
pixels with the retained ones, and a multi-rect AA path clip is costly
on the GPU. Snap outwards and use a hard rect/region clip instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2pLwNk6UenehZFfNA
WebGL was the only host that repainted the whole canvas every frame:
it reported no retained contents, and the canvas carried 4x MSAA and a
depth buffer Skia never uses. Create it single-sampled with a preserved
drawing buffer so the compositor repaints only the damaged regions.

SwiftShader (weak-GPU proxy, 2560x1600 ProgressRing): 23-25 fps ->
101-107 fps; hardware GPU under 6x CPU throttle: neutral. An FBO +
blitFramebuffer variant was slower (18.6 fps), the full copy dominates.

The canvas size is floored, since re-assigning it clears the preserved
buffer, and a resize or lost context repaints whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2pLwNk6UenehZFfNA
@MartinZikmund MartinZikmund added the platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform label Nov 4, 2026
Copilot AI balanced review requested due to automatic review settings November 4, 2026 16:33
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 30s —— View job


PR review summary

Verdict: ship, with minor follow-ups. I reviewed this by reading the diff only. I did not build it or run any tests.

Findings: 0 high, 0 medium, 2 low. Both are inline comments:

  • isContentPreserved mutates state, which makes its contract fragile. I also asked about the context-restore path.
  • preserveDrawingBuffer has not been measured on a physical low-end device.

Cross-cutting notes

  • SkiaPresentSession.ClipToDamage is a root-cause fix, not a guard. Snapping the damage outwards to whole pixels and clipping without AA removes the edge blending. The change affects every host that retains frames, not only WASM, so the Skia Desktop runtime tests are the right place to cover it.
  • Given_SkiaPresentSession runs on a raster SKBitmap, not a GPU surface. It covers the pixel-snapping logic but not the GPU scissor path. The manual WASM checks described in the PR cover the rest.
  • WasmGLGraphicsContext: the _contentsPreserved field sits after the property that uses it. This is cosmetic.
  • JsInfo.Depth may now be unused. Please check whether it can be removed along with FboId and Samples.
  • I did not see any async, event or code-style violations, and the commits follow Conventional Commits.

}

/** False when the drawing buffer lost the previous frame: resizing the canvas or losing the context clears it. */
public static isContentPreserved(instance: WebGlBrowserRenderer): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isContentPreserved reads like a query but mutates lastWidth/lastHeight, so a second call in the same frame returns true even after a resize. It works today because AcquireRenderTarget calls it once per frame, but the contract is fragile. Consider renaming it (e.g. consumeContentPreserved) or documenting the side effect in the doc comment.

Also, after a context loss and restore, the result is true again on the next frame. Skia's GrDirectContext may then still hold stale GL state. Please confirm this path is handled elsewhere, e.g. by a context-restored handler that recreates the backend.

// The preserved drawing buffer keeps the previous frame, so only the damaged regions are repainted. No MSAA:
// Skia's coverage AA does not use it, and resolving it costs a full-canvas pass every frame. No depth either:
// Skia only needs the stencil.
this.glCtx = EmscriptenWebGL.createContext(this.canvas, { antialias: 0, depth: 0, stencil: 8, preserveDrawingBuffer: 1 });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preserveDrawingBuffer: 1 forces a copy or blocks the swap-chain flip on some tiled mobile GPUs and compositors, and it can cost more than the damage savings there. The PR notes this is not yet measured on a physical low-end device. Please validate on one (e.g. a mid-range Android) before merge, or consider a fallback to full repaint if it regresses.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical buffer-preservation checks miss resize round-trips and same-size WebGL context restoration, allowing partial or skipped repaints over a cleared drawing buffer.

2 open issues
What changed in this PR

This PR addresses #26040 by enabling damage-region repainting for the WebAssembly WebGL host and correcting damage clipping on Skia hosts that retain frames.

Changes:

  • Preserves the WebGL drawing buffer and disables unused MSAA and depth buffers.
  • Adds preservation checks and avoids redundant canvas resets from fractional dimensions.
  • Uses pixel-aligned, non-antialiased damage clips and adds regression tests.
File Description
src/​Uno.UI.RuntimeTests/​Microsoft/​UI/​Composition/​Given_SkiaPresentSession.cs Tests fractional damage edges and disjoint damage regions.
src/​Uno.UI.Runtime.WebAssembly.Browser/​ts/​Runtime/​WebGlBrowserRenderer.ts Configures buffer preservation and checks canvas/context state.
src/​Uno.UI.Runtime.WebAssembly.Browser/​ts/​Runtime/​BrowserRenderer.ts Floors canvas dimensions to avoid redundant buffer clears.
src/​Uno.UI.Runtime.WebAssembly.Browser/​Rendering/​WebGlBrowserRenderer.cs Exposes preservation checks and simplifies framebuffer metadata.
src/​Uno.UI.Runtime.WebAssembly.Browser/​Rendering/​WasmGraphicsContext.cs Reports frame preservation to the compositor.
src/​Uno.UI.Composition.Skia/​Composition/​Uno/​Drawing/​SkiaPresentSession.cs Snaps damage outward and applies hard rectangular or region clips.

Review effort: Balanced

Comment on lines +62 to +64
const unchanged = canvas.width === instance.lastWidth && canvas.height === instance.lastHeight;
instance.lastWidth = canvas.width;
instance.lastHeight = canvas.height;
Comment on lines +66 to +67
const gl = instance.anyGL.getContext(instance.glCtx)?.GLctx;
return unchanged && !!gl && !gl.isContextLost();
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 243642 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedIconPage.png
    • Accessibility_ScreenReader.png
    • Accessibility_ScreenReader.png-dark
    • AppBarButtonWithIconTest.png-dark
    • AppBarButtonTest.png
    • AppBarToggleButtonTest.png-dark
    • AnimatedVisualsTestbedPage.png
    • AppBar_KeyBoard.png
    • ApplicationViewMode.png-dark
    • ApplicationViewMode.png
    • Application_DispatcherShutdownMode.png
    • Application_DispatcherShutdownMode.png-dark
    • AppWindowClosing.png
    • AppWindowFeatures.png-dark
    • AppXamlDefinedResources.png
    • AppWindowPresenters.png-dark
    • AppWindowTitleBar Properties.png-dark
    • AppWindowTitleBar Properties.png
    • Attributed_text_Simple.png
  • skia-windows-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ApplicationViewSizing.png
    • AppWindowClosing.png
    • AppWindowTitleBar Properties.png
    • Attributed_text_Simple.png
    • Attributed_text_Supserscript.png
    • AutoBorderStretchwithleftmargin.png-dark
    • AutomationProperties_AutomationId.png
    • AutomationProperties_Relations.png
    • AutoSuggestBoxChosenSuggestion.png
    • AutoSuggestBox_BitmapIcon.png-dark
    • AutoSuggestBox_SoftKeboard.png-dark
    • AutoSuggestBox_SoftKeboard.png
    • Basics.png-dark
    • Basics.png
    • BezierSegment.png-dark
    • BezierSegment.png
    • BitmapIcon_Monochromatic.png-dark
    • BitmapIcon_Monochromatic.png
    • BorderCenteredwithmargins.png-dark
    • BorderCenteredwithmargins.png

@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

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

Labels

platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WASM] Repaint only damaged regions on WebGL

4 participants