canvas: Add OffscreenCanvas 'transferToImageBitmap' method (#37880)

Follow the HTML speficication and add missing 'transferToImageBitmap'
method to OffscreenCanvas interface.

https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-transfertoimagebitmap

Testing: Improvements in the following tests
- html/canvas/offscreen/compositing/2d.composite.grid*
- html/canvas/offscreen/fill-and-stroke-styles/2d.gradient*
- html/canvas/offscreen/manual/the-offscreen-canvas/offscreencanvas*
- html/canvas/offscreen/reset/2d.reset*
- html/canvas/offscreen/text/2d.text*

Fixes (partially): #34111

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
Andrei Volykhin 2025-07-04 19:25:36 +03:00 committed by GitHub
parent 70b0fb840e
commit 9bd8d4f026
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 110 additions and 84 deletions

View file

@ -39,6 +39,11 @@ pub(crate) trait CanvasContext {
fn resize(&self);
// Resets the backing bitmap (to transparent or opaque black) without the
// context state reset.
// Used by OffscreenCanvas.transferToImageBitmap.
fn reset_bitmap(&self);
/// Returns none if area of canvas is zero.
///
/// In case of other errors it returns cleared snapshot
@ -145,6 +150,21 @@ impl CanvasContext for RenderingContext {
}
}
fn reset_bitmap(&self) {
match self {
RenderingContext::Placeholder(offscreen_canvas) => {
if let Some(context) = offscreen_canvas.context() {
context.reset_bitmap()
}
},
RenderingContext::Context2d(context) => context.reset_bitmap(),
RenderingContext::WebGL(context) => context.reset_bitmap(),
RenderingContext::WebGL2(context) => context.reset_bitmap(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.reset_bitmap(),
}
}
fn get_image_data(&self) -> Option<Snapshot> {
match self {
RenderingContext::Placeholder(offscreen_canvas) => {
@ -257,6 +277,12 @@ impl CanvasContext for OffscreenRenderingContext {
}
}
fn reset_bitmap(&self) {
match self {
OffscreenRenderingContext::Context2d(context) => context.reset_bitmap(),
}
}
fn get_image_data(&self) -> Option<Snapshot> {
match self {
OffscreenRenderingContext::Context2d(context) => context.get_image_data(),