Repository navigation
fix(viewer): read WebGL tiles back for pixel surfaces and drop the hold before the bitmap goes (#380) - #388
Merged
Conversation
…ld before the bitmap goes (#380) With the WebGL backend selected, a tile stays in the WebGL canvas after its render (ADR 0012) until the canvas surface claims it. The demo's Pages panel presents to a PngSurface, which read the bitmap without claiming the tile, so thumbnails held only the paper; the session then released the bitmap while the backend still held it, and the next render read the canvas back into it (ObjectDisposedException). The contract is now explicit: - ITileRender.ReadBack() puts a finished tile left on the device into the target bitmap (default: nothing to do); disposing the render drops the hold. - IRenderSurface.AcceptsDevicePixels (default false) marks a surface that takes a held tile without reading pixels; only the Blazor CanvasSurface does. - DocumentSession (cooperative) and TileScheduler present before disposing the render, reading back first unless the surface accepts device pixels; the background path reads back before its render ends. The bitmap is released after the render, so no hold outlives it. Tests: DeviceTileTests (a backend that keeps tiles on a fake device: pixel surfaces get them read back, accepting surfaces claim them, the next render never touches a released bitmap; session and scheduler), and a browser test that renders WebGL thumbnails through a PngSurface twice and matches both against the software rasterizer within the webgl rows (Metal and SwiftShader). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to the public rendering lifecycle and shared GPU/bitmap ownership warrant owner review before approval.
No open issues 🎉
What changed in this PR
Fixes #380 by making device-held tiles readable by pixel-based surfaces and releasing GPU holds before their bitmaps are disposed. This addresses blank WebGL thumbnails and failures on subsequent renders while preserving direct GPU presentation for the viewer.
Changes:
- Adds explicit read-back and surface-capability contracts.
- Updates session, scheduler, and WebGL render lifetimes to release resources in the correct order.
- Adds unit and browser regression coverage for thumbnails and subsequent renders.
| File | Description |
|---|---|
| tests/Broadside.Viewer.Tests/ViewerTestSupport.cs | Forwards read-back through the test wrapper. |
| tests/Broadside.Viewer.Tests/DeviceTileTests.cs | Tests device-tile presentation, read-back, and lifetime ordering. |
| tests/Broadside.Viewer.Blazor.BrowserTests/WebGLBackendTests.cs | Checks consecutive WebGL thumbnails against reference images. |
| src/Broadside.Viewer/TileScheduler.cs | Reads back when needed and retains renders through presentation. |
| src/Broadside.Viewer/PublicAPI.Unshipped.txt | Registers the new public contract members. |
| src/Broadside.Viewer/ITileBackend.cs | Adds read-back and documents device-tile ownership. |
| src/Broadside.Viewer/IRenderSurface.cs | Adds the device-pixel capability flag. |
| src/Broadside.Viewer/DocumentSession.cs | Coordinates read-back, presentation, and disposal. |
| src/Broadside.Viewer.Blazor/WebGLTileBackend.cs | Implements read-back and releases GPU holds on disposal. |
| src/Broadside.Viewer.Blazor/CanvasSurface.cs | Declares support for direct device-pixel presentation. |
| samples/Broadside.Viewer.Blazor.Sample/Renderers.cs | Forwards read-back and exposes thumbnail rendering for browser tests. |
Review effort: Balanced
This was referenced Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #380.
Root cause
With the WebGL backend (ADR 0012) a finished tile stays in the WebGL canvas, held by
GpuPresentation, untilCanvasSurfaceclaims it. The demo's Pages panel presents thumbnails to aPngSurfacethroughDocumentSession.RenderPageAsync; it read the bitmap without claiming the tile, so thumbnails held only the paper. The session then released the bitmap while_heldstill pointed at it, and the next render'sFlushread the canvas into the released bitmap (ObjectDisposedExceptionin the browser).Fix
An explicit capability and lifecycle instead of a type check:
ITileRender.ReadBack()(default: nothing) puts a tile the backend left on the device into the target bitmap; disposing the render drops the hold.IRenderSurface.AcceptsDevicePixels(defaultfalse) marks a surface that can take a held tile without reading pixels; only the BlazorCanvasSurfacereturns true, so the GPU-to-GPU path is unchanged for the viewer.DocumentSession(cooperative) andTileScheduler.Drawnow present before disposing the render and callReadBack()first unless the surface accepts device pixels; the background path reads back before its render ends. The bitmap is released after the render, so no hold outlives its bitmap.WebGLTileBackend's render implementsReadBackand releases its hold on disposal; the demo'sTimedwrapper forwardsReadBack.No allocations added; the software path's
ReadBackis the empty default.Tests
DeviceTileTests(unit): a backend that keeps finished tiles on a fake device (and throws like the oldFlushwhen it reads into a released bitmap). Pixel surfaces get the tile read back and the next render succeeds (cooperative and background); a surface that accepts device pixels claims it with no read-back; the tile scheduler does the same for both kinds of surface. All 5 fail on the old session/scheduler code.WebGLBackendTests.A_thumbnail_drawn_with_WebGL_matches_the_software_rasterizer_and_the_render_after_it_too(Playwright): renders a page twice through a cooperative session withWebGLTileBackendintoPngSurface, as the Pages panel does, and matches both against the reviewed software renders within thewebglrows. Run locally in headless Chromium on ANGLE Metal and on SwiftShader (forced locally); against an app published from the old code it fails withObjectDisposed.Gate: build 0/0,
Broadside.Core.slnf16964 total / 0 failed / 74 skipped, format and conformance check clean. Locally the whole browser suite passes in Chromium; the only failures are the non-AOT build not being published here and Firefox's sandbox on this Mac.🤖 Generated with Claude Code