Skip to content

fix(viewer): read WebGL tiles back for pixel surfaces and drop the hold before the bitmap goes (#380) - #388

Merged
michaelstonis merged 1 commit into
spec/33-broadside-v1from
380-webgl-thumbnail-readback
Oct 22, 2026
Merged

michaelstonis merged 1 commit into
spec/33-broadside-v1from
380-webgl-thumbnail-readback

Conversation

@michaelstonis

Copy link
Copy Markdown
Owner

Closes #380.

Root cause

With the WebGL backend (ADR 0012) a finished tile stays in the WebGL canvas, held by GpuPresentation, until CanvasSurface claims it. The demo's Pages panel presents thumbnails to a PngSurface through DocumentSession.RenderPageAsync; it read the bitmap without claiming the tile, so thumbnails held only the paper. The session then released the bitmap while _held still pointed at it, and the next render's Flush read the canvas into the released bitmap (ObjectDisposedException in 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 (default false) marks a surface that can take a held tile without reading pixels; only the Blazor CanvasSurface returns true, so the GPU-to-GPU path is unchanged for the viewer.
  • DocumentSession (cooperative) and TileScheduler.Draw now present before disposing the render and call ReadBack() 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 implements ReadBack and releases its hold on disposal; the demo's Timed wrapper forwards ReadBack.

No allocations added; the software path's ReadBack is the empty default.

Tests

  • DeviceTileTests (unit): a backend that keeps finished tiles on a fake device (and throws like the old Flush when 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 with WebGLTileBackend into PngSurface, as the Pages panel does, and matches both against the reviewed software renders within the webgl rows. Run locally in headless Chromium on ANGLE Metal and on SwiftShader (forced locally); against an app published from the old code it fails with ObjectDisposed.

Gate: build 0/0, Broadside.Core.slnf 16964 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

…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>
Copilot AI balanced review requested due to automatic review settings October 22, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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

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.

2 participants