Skip to content

Conversation

alexchuber
Copy link
Contributor

@alexchuber alexchuber commented Aug 12, 2025

This PR updates DumpTools to fall back to a BitmapRenderingContext from a canvas when ThinEngine isn’t available, instead of relying solely on ThinEngine. It also fixes issues that were first resolved in #13251 but regressed after #13803.

To make DumpData Native-friendly, we need to avoid ThinEngine. While it might be possible to swap ThinEngine with [insert platform compatible engine], in practice it’s much simpler— at least for Native —to extend the existing 2D Canvas polyfill with bitmaprenderer support, rather than extending its graphics infrastructure to handle rendering and reading from a separate context or canvas.

We can safely remove the engine dependency entirely if/when Webkit and Gecko respect the premultiplyAlpha flag when reading ImageBitmap. Or we can remove it now, if we're OK with breaking those (quite edge) cases. Thoughts?

More details For reference, below is a table showing observed results for whether unassociated alpha is preserved when reading pixel data, across different rendering contexts and browsers.
Platform WebGL + Canvas WebGL + OffscreenCanvas Bitmap + Canvas Bitmap + OffscreenCanvas
Blink (Chromium)
WebKit
Gecko

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 12, 2025

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 12, 2025

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 12, 2025

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 12, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 12, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 12, 2025

@alexchuber
Copy link
Contributor Author

Looking into the failed vis tests...

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 13, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 13, 2025

@alexchuber
Copy link
Contributor Author

Was a timing issue. The drawing to the shared canvas (whether via transferFromImageBitmap or draw) must be atomic with the invocation ToBlob().

(Under the hood, `canvas.toBlob` and `canvas.convertToBlob` capture the current canvas data synchronously, then perform the encoding asynchronously. We only care that the rendering + reading are synchronous. However, since the reading + encoding are coupled together in these APIs-- and because the APIs are callback-based-- we have to wrap the full draw + read + encode in the same Promise.)

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 13, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Aug 13, 2025

@alexchuber alexchuber merged commit 993698c into BabylonJS:master Aug 13, 2025
18 checks passed
deltakosh pushed a commit that referenced this pull request Aug 18, 2025
For backwards compatibility. I missed this case in #17015.
Fixes specific CreateScreenshotWithRenderTarget usage in Sandbox.
georginahalpern pushed a commit to georginahalpern/Babylon.js that referenced this pull request Aug 20, 2025
…BabylonJS#17015)

This PR updates DumpTools to fall back to a BitmapRenderingContext from
a canvas when ThinEngine isn’t available, instead of relying solely on
ThinEngine. It also fixes issues that were first resolved in BabylonJS#13251 but
regressed after BabylonJS#13803.

To make DumpData Native-friendly, we need to avoid ThinEngine. While it
might be possible to swap ThinEngine with [insert platform compatible
engine], in practice it’s much simpler— at least for Native —to extend
the existing 2D Canvas polyfill with bitmaprenderer support, rather than
extending its graphics infrastructure to handle rendering and reading
from a separate context or canvas.

We can safely remove the engine dependency entirely if/when Webkit and
Gecko respect the `premultiplyAlpha` flag when reading ImageBitmap. Or
we can remove it now, if we're OK with breaking those (quite edge)
cases. Thoughts?

<details>
<summary>More details</summary>
For reference, below is a table showing observed results for whether
unassociated alpha is preserved when reading pixel data, across
different rendering contexts and browsers.

Platform | WebGL + Canvas | WebGL + OffscreenCanvas | Bitmap + Canvas |
Bitmap + OffscreenCanvas
-- | -- | -- | -- | --
Blink (Chromium) | ✅| ❌| ✅| ✅
WebKit | ✅| ❌| ❌| ❌
Gecko | ✅| ✅| ❌| ❌

</details>
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.

3 participants