mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
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:
parent
70b0fb840e
commit
9bd8d4f026
46 changed files with 110 additions and 84 deletions
|
@ -153,6 +153,10 @@ impl CanvasContext for CanvasRenderingContext2D {
|
|||
self.set_canvas_bitmap_dimensions(self.size().cast())
|
||||
}
|
||||
|
||||
fn reset_bitmap(&self) {
|
||||
self.canvas_state.reset_bitmap()
|
||||
}
|
||||
|
||||
fn get_image_data(&self) -> Option<Snapshot> {
|
||||
if !self.canvas_state.is_paintable() {
|
||||
return None;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -84,6 +84,10 @@ impl CanvasContext for OffscreenCanvasRenderingContext2D {
|
|||
self.context.resize()
|
||||
}
|
||||
|
||||
fn reset_bitmap(&self) {
|
||||
self.context.reset_bitmap()
|
||||
}
|
||||
|
||||
fn get_image_data(&self) -> Option<Snapshot> {
|
||||
self.context.get_image_data()
|
||||
}
|
||||
|
|
|
@ -916,6 +916,10 @@ impl CanvasContext for WebGL2RenderingContext {
|
|||
self.base.resize();
|
||||
}
|
||||
|
||||
fn reset_bitmap(&self) {
|
||||
self.base.reset_bitmap();
|
||||
}
|
||||
|
||||
fn get_image_data(&self) -> Option<Snapshot> {
|
||||
self.base.get_image_data()
|
||||
}
|
||||
|
|
|
@ -1974,6 +1974,10 @@ impl CanvasContext for WebGLRenderingContext {
|
|||
}
|
||||
}
|
||||
|
||||
fn reset_bitmap(&self) {
|
||||
warn!("The WebGLRenderingContext 'reset_bitmap' is not implemented yet");
|
||||
}
|
||||
|
||||
// Used by HTMLCanvasElement.toDataURL
|
||||
//
|
||||
// This emits errors quite liberally, but the spec says that this operation
|
||||
|
|
|
@ -276,6 +276,10 @@ impl CanvasContext for GPUCanvasContext {
|
|||
}
|
||||
}
|
||||
|
||||
fn reset_bitmap(&self) {
|
||||
warn!("The GPUCanvasContext 'reset_bitmap' is not implemented yet");
|
||||
}
|
||||
|
||||
/// <https://gpuweb.github.io/gpuweb/#ref-for-abstract-opdef-get-a-copy-of-the-image-contents-of-a-context%E2%91%A5>
|
||||
fn get_image_data(&self) -> Option<Snapshot> {
|
||||
// 1. Return a copy of the image contents of context.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue