Skip to content

fix(skia): Replace stale pixels on retained layer blit - #24546

Open
ajpinedam wants to merge 3 commits into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.skia.backdrop.retained.layer.blit
Open

ajpinedam wants to merge 3 commits into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.skia.backdrop.retained.layer.blit

Conversation

@ajpinedam

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #24495

PR Type:

🐞 Bugfix

What changed? 🚀

Setting Window.SystemBackdrop on Skia macOS (Metal) made the whole window glitch: hover states and text changes left ghosts, semi-transparent content got denser frame after frame, and the UI flickered between the last few frames. Flyouts and dialogs were unaffected. The regression appeared with damage-region rendering (6.7); 6.6 renders correctly.

Root cause. Damage-region renderers draw into a retained layer and blit it onto the swapchain surface every frame (RetainedLayer.Present). The Metal drawable comes from a pool of three recycled textures, so the swapchain surface still holds the frame presented three frames earlier. The blit used a default (source-over) paint. With an opaque frame that is harmless, but a system backdrop makes the visual tree transparent, so the retained layer carries per-pixel alpha and source-over blends the new frame over the stale one instead of replacing it.

Fix. RetainedLayer.Present blits with SKBlendMode.Src, so the swapchain ends up with exactly the retained layer's pixels regardless of what the recycled texture contained. This is the shared blit used by the macOS Metal, iOS Metal, Android OpenGL, X11 OpenGL/EGL and WASM WebGL renderers; none of them pre-fills the swapchain with a background that source-over was meant to composite against, so the change is safe for all of them and fixes the same latent issue for any transparent window on those targets. Win32 does not use RetainedLayer. EnsureSurface now accepts a null GRContext to create a raster layer, which the new test uses.

Tests and sample.

  • Runtime test Given_RetainedLayer.When_Layer_Has_Alpha_Then_Present_Replaces_Stale_Swapchain_Pixels: an alpha-carrying raster layer is presented onto an opaque red "stale" surface and the result must equal the layer.
  • Manual sample Windowing → "SystemBackdrop ghosting (damage)": applies Mica on load and animates a small square across a wide host, so the per-frame damage stays much smaller than the window and the stale drawable is left visible. Toggles cover the damage-region overlay and an opaque Rectangle fill behind the square — because the backdrop pass only transparentizes Background brushes, that plate stays opaque, and ghosting must disappear over it and nowhere else.

Validation.

  • Runtime test on Skia macOS desktop: fails with the previous Present (pixel (0,0) expected #00000000, actual #ffff0000), passes with the fix. The 8 existing Given_Visual_Damage tests keep passing alongside it (9/9).
  • Manual A/B on Skia macOS desktop (SamplesApp, Metal renderer), swapping only Uno.UI.dll. On the new sample, the pre-fix build smears the moving square into a solid bar across its whole travel path (~1400px of red instead of the square's 129px at 2x) and renders every text block doubled; with the fix each frame shows a single crisp square. On the existing "SystemBackdrop (Mica)" sample, the pre-fix build stacks the "None", "Mica (BaseAlt)" and "DesktopAcrylicBackdrop" labels on top of each other after switching materials and ghosts button hover states; with the fix each state renders cleanly and the material shows through.

PR Checklist ✅

🤖 Generated with Claude Code

ajpinedam and others added 3 commits September 17, 2026 11:23
Copilot AI lite review requested due to automatic review settings September 17, 2026 15:24
@github-actions github-actions Bot added the area/automation Categorizes an issue or PR as relevant to project automation label Sep 17, 2026

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.

🟢 Approval recommended

The fix is narrowly scoped, covered by a regression test, and the reviewed platform integrations are consistent.

Pull request overview

Fixes retained-layer ghosting when transparent system backdrops meet recycled swapchain surfaces.

Changes:

  • Uses source replacement blending for retained-layer presentation.
  • Adds a Skia runtime regression test.
  • Adds a manual SamplesApp reproduction page.
File summaries
File Description
RetainedLayer.cs Replaces stale swapchain pixels during blits.
Given_RetainedLayer.cs Verifies alpha pixels replace stale content.
SystemBackdropDamageTests.xaml.cs Adds manual backdrop/damage test logic.
SystemBackdropDamageTests.xaml Defines the test sample UI.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 234307 found UI Test snapshots differences: skia-linux-screenshots: 57, skia-windows-screenshots: 2310, wasm: 11

Details
  • skia-linux-screenshots: 57 changed over 2348

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • ContextRequested.png-dark
    • ContextRequested.png
    • DisplayInformation.png-dark
    • Focus_FocusVisual_Properties.png-dark
    • ElementLevelTheme.png-dark
    • ElementLevelTheme.png
    • Examples.png-dark
    • Examples.png
    • ExpanderColorValidationPage.png-dark
    • ExpanderColorValidationPage.png
    • ImageIconPage.png-dark
    • DropDownButtonPage.png-dark
    • DropDownButtonPage.png
  • skia-windows-screenshots: 2310 changed over 2410

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_ScreenReader.png-dark
    • AndroidWindowInsets.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowClosing.png-dark
    • AppWindowPresenters.png-dark
    • AppXamlDefinedResources.png
    • AppXamlDefinedResources.png-dark
    • Attributed_text_Supserscript.png
    • Attributed_text_FontSize_Changing.png
    • AutoBorderStretchwithtopmargin.png-dark
    • AutoBorderStretchwithrightmargin.png-dark
    • AutomationProperties_Name.png
    • AutomationProperties_AutomationId.png-dark
    • AutoSuggestBox_Icons.png-dark
    • AutoSuggestBoxChosenSuggestion.png-dark
    • AutoSuggestBox_SoftKeboard.png
    • AutoSuggestBox_Icons.png
    • Basics Pivot Test.png-dark
    • AutoSuggestBox_Keyboard.png-dark
    • Basics Pivot Test.png
  • wasm: 11 changed over 1076

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • UITests.Shared.Microsoft_UI_Xaml_Controls.ExpanderTests.WinUIExpanderPage
    • UITests.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_NavigationProperties
    • SamplesApp.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_EnableDevTools
    • SamplesApp.Windows_UI_Xaml_Controls.ListView.ListViewSelectedItems
    • SamplesApp.Wasm.Windows_UI_Xaml_Controls.ListView.ListView_IsSelected
    • UITests.Windows_UI_Xaml_Media_Animation.ColorAnimation_Background
    • UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Minimal
    • UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Ogg_Extension
    • UITests.Uno_Web.Http.CookieManagerTests
    • UITests.Windows_UI_Xaml_Controls.CalendarView.CalendarView_Theming
    • Uno.UI.Samples.Content.UITests.WebView.WebView_AnchorNavigation

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 234307 has failed on Uno.UI - CI.

@MartinZikmund MartinZikmund left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TL;DR: master does not need this fix. Its macOS present path no longer blends (see 1), so #24495 is already gone there. This PR is only needed on servicing/6.8 and release/stable/6.7.

@ajpinedam thanks for tracking this down - the root cause (source-over blit onto a recycled drawable once the layer carries alpha) and the SKBlendMode.Src fix look right to me, and the test is a nice tight repro. A few things before it can land:

1. Base branch - this can't go to master as-is. RetainedLayer (and damage-region rendering) no longer exists on master: it was removed by the drawing-backend merge (1804d9b). On master, macOS now presents via a native MTLBlitCommandEncoder copyFromTexture in uno_window_present_texture, which is a straight copy with no blending, so the bug can't occur there. That's also why the PR shows as conflicting. I'd retarget this to servicing/6.8 and backport to release/stable/6.7 (the issue was reported on 6.7.22), where src/Uno.UI/Helpers/RetainedLayer.skia.cs still has the source-over Present.

2. Win32 OpenGL has the same blit and isn't covered. The description says Win32 doesn't use RetainedLayer. That's true, but on 6.7/6.8 it has its own inline copy of the same blit in src/Uno.UI.Runtime.Skia.Win32/Rendering/Win32WindowWrapper.Rendering.OpenGl.cs (IRenderer.CopyPixels, ~L231):

layer.Draw(swapchain.Canvas, 0, 0, null);
swapchain.Canvas.Flush();
// then SwapBuffers

OpenGL is the default Win32 renderer on those branches (UseOpenGLOnWin32 ?? true in Win32WindowWrapper), and the back buffer after SwapBuffers holds an older frame (or undefined contents), so Mica/Acrylic on Win32 should be exposed to the same ghosting. This comes from reading the code; I haven't reproduced it. This matters more once #24190 is backported to 6.7/6.8 (queued), because it makes the island root transparent under a rendered backdrop. Suggest applying the same SKBlendMode.Src there, or routing it through RetainedLayer.Present, in this PR.

3. Sample wording will go stale with #24190. The sample and description say "the backdrop pass only transparentizes Background brushes". #24190 removes that framework traversal entirely (app backgrounds are left alone, as in WinUI; only the island root goes transparent). The sample still works because the page sets Background="Transparent" itself, but the explanatory text and the "Opaque plate" note (currently "the backdrop pass leaves it opaque") should be reworded, e.g. "an opaque fill, so the layer has no alpha there".

Relation to #24190: it does not fix #24495. It never touches the frame blit, and under a rendered backdrop it still makes the root transparent, so both PRs are needed on the servicing branches.

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verified: PR diff (head adbe0e0, base 06690db), local git history for the dirty-rectangle and Src-blit commits, upstream master contents via gh api, and the PR's statusCheckRollup and reviews. Not run: build, tests, or a macOS/Win32 reproduction.

Findings, by severity:

  1. [High] Target branch. This PR targets master, but master no longer has the code it changes. src/Uno.UI/Helpers/RetainedLayer.cs is absent from upstream master (contents API returns no match in src/Uno.UI/Helpers). It was removed by the drawing-backend merge 1804d9b, which is an ancestor of master. On master, macOS presents via uno_window_present_texture in src/Uno.UI.Runtime.MacOS/UnoNativeMac/UnoNativeMac/UNOMetalViewDelegate.m, which uses a straight MTLBlitCommandEncoder copy with no blending, so the bug described cannot occur there. The PR's merge base 06690db is 3328 commits behind master, and the PR shows as conflicting. The fix is only relevant to the servicing branches (servicing/6.8, release/stable/6.7) where the source-over RetainedLayer.Present still exists. Please retarget and backport, or explain why master needs the change. The Given_RetainedLayer test and the sample would move with it.

  2. [High] Win32 OpenGL is the same blit and is not covered. The PR description says Win32 does not use RetainedLayer, which is true of the class but not of the pattern. At PR head src/Uno.UI.Runtime.Skia.Win32/Rendering/Win32WindowWrapper.Rendering.OpenGl.cs:231 still does layer.Draw(swapchain.Canvas, 0, 0, null), where _layer (line 33) is the full-frame layer that is blitted onto the GL back buffer, and GL is the default Win32 renderer on the servicing branches. The same stale-back-buffer argument applies. Please either apply SKBlendMode.Src there too, or explain why it is not affected. I have not reproduced this on Win32 (code review only).

  3. [Medium] Prior attempt in history. Local master history contains ac14f7380f ("fix: Blit retained layer with Src to avoid stale swapchain", Ramez Gerges), which made the same SKBlendMode.Src change in RetainedLayer.skia.cs and in the Win32 GL blit. It was reverted by 1e24715329 (ancestor of master). The revert's stated reason was that the overlay artifact it targeted was an RDP-specific artifact, and that the Src blit was speculative. The PR's new macOS A/B evidence is different and may well supersede that reasoning, but the PR should reference the earlier change and say why this one differs. Note that the earlier change also covered Win32, which this PR omits (see finding 2).

  4. [Medium] Validation. The PR's runtime test (Given_RetainedLayer.When_Layer_Has_Alpha_Then_Present_Replaces_Stale_Swapchain_Pixels) is a sound unit-level repro: an alpha raster layer presented onto an opaque red surface must equal the layer. It fails under SrcOver and passes under Src, by inspection. It exercises RetainedLayer in isolation on a raster surface, not the Metal/GL path. The end-to-end evidence (pre-fix vs post-fix macOS Metal A/B, screenshots of ghosting) is author-reported and I did not verify it. Please keep the author's A/B evidence labeled as manual/runtime on macOS, and note that the new EnsureSurface(GRContext? ...) null path exists for the test. The core fix is semantically correct as far as I can tell: the layer is the full composed frame, so replacing the swapchain pixels with it is right, and Src is identical to SrcOver for opaque frames.

  5. [Medium] CI on head adbe0e0 is red. Uno.UI - CI (build 234307) failed, as did Tests - Android Skia Runtime Tests 4, Tests - iOS Skia Runtime Tests 2, and Tests - tvOS Skia Runtime Tests 2. The screenshot comparison reported 57 changed on Linux, 2310 on Windows, and 11 on WASM. These are most likely caused by the stale base (3328 commits behind), but I did not read the job logs, so this is not confirmed. The PR checklist leaves the screenshot-compare item unchecked. Please rebase and re-run CI on a current base before asking for review. The Claude Code Review check is SKIPPED, and Mergify is NEUTRAL.

  6. [Low] Sample wording will go stale. The sample and PR text say "the backdrop pass only transparentizes Background brushes". MartinZikmund's review notes that #24190 removes that traversal, so the wording and the "Opaque plate" note in SystemBackdropDamageTests.xaml / .xaml.cs should be reworded to not depend on it.

  7. [Low] Code nit. _presentPaint in RetainedLayer.cs:13 is a process-wide static SKPaint that is never disposed and is shared across all RetainedLayer instances, which are used from different renderers. An instance field, as in the earlier _blitPaint attempt, would be simpler to reason about.

WinUI parity: not applicable. This is an Uno-internal Skia renderer blit, so I did not check microsoft-ui-xaml. Platform suffixes: RetainedLayer.cs has no suffix, but it is only referenced from Skia renderers (Android, AppleUIKit, MacOS, WebAssembly, X11) and is using SkiaSharp, which is consistent with the existing file.

Not validated: no build, no unit or runtime tests, no macOS or Win32 run, no CI job logs read, no check of the PR's CI failure causes, and no check against current master's RetainedLayer replacement beyond the presence of the macOS blit.

@MartinZikmund

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/7.0

@mergify

mergify Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

backport release/stable/7.0

🟠 Waiting for conditions to match

Details
  • merged [📌 backport requirement]

@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

7.0 PRs only. For issues, use the Release field. area/automation Categorizes an issue or PR as relevant to project automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On MacOS, when a SystemBackdrop is set, the whole Window UI glitches

6 participants