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

@ -27,6 +27,7 @@ use crate::dom::blob::Blob;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::offscreencanvasrenderingcontext2d::OffscreenCanvasRenderingContext2D;
use crate::dom::promise::Promise;
use crate::realms::{AlreadyInRealm, InRealm};
@ -214,6 +215,40 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-transfertoimagebitmap>
fn TransferToImageBitmap(&self, can_gc: CanGc) -> Fallible<DomRoot<ImageBitmap>> {
// TODO Step 1. If the value of this OffscreenCanvas object's
// [[Detached]] internal slot is set to true, then throw an
// "InvalidStateError" DOMException.
// Step 2. If this OffscreenCanvas object's context mode is set to none,
// then throw an "InvalidStateError" DOMException.
if self.context.borrow().is_none() {
return Err(Error::InvalidState);
}
// Step 3. Let image be a newly created ImageBitmap object that
// references the same underlying bitmap data as this OffscreenCanvas
// object's bitmap.
let Some(snapshot) = self.get_image_data() else {
return Err(Error::InvalidState);
};
let image_bitmap = ImageBitmap::new(&self.global(), snapshot, can_gc);
image_bitmap.set_origin_clean(self.origin_is_clean());
// Step 4. Set this OffscreenCanvas object's bitmap to reference a newly
// created bitmap of the same dimensions and color space as the previous
// bitmap, and with its pixels initialized to transparent black, or
// opaque black if the rendering context's alpha is false.
if let Some(canvas_context) = self.context() {
canvas_context.reset_bitmap();
}
// Step 5. Return image.
Ok(image_bitmap)
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-converttoblob>
fn ConvertToBlob(&self, options: &ImageEncodeOptions, can_gc: CanGc) -> Rc<Promise> {
// Step 5. Let result be a new promise object.