canvas: Add OffscreenCanvas 'convertToBlob' method (#37786)

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

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

Testing: Improvements in the following tests
-
html/canvas/offscreen/manual/convert-to-blob/offscreencanvas.convert.to.blob*
-
html/canvas/offscreen/manual/wide-gamut-canvas/2d.color.space.p3.convertToBlobp3.canvas.html

Fixes: #24272

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
Andrei Volykhin 2025-07-04 09:58:12 +03:00 committed by GitHub
parent 3ba5b89ef2
commit 6ba54e4d79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 187 additions and 187 deletions

View file

@ -3,26 +3,33 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::rc::Rc;
use constellation_traits::BlobImpl;
use dom_struct::dom_struct;
use euclid::default::Size2D;
use js::rust::{HandleObject, HandleValue};
use pixels::Snapshot;
use pixels::{EncodedImageType, Snapshot};
use script_bindings::weakref::WeakRef;
use crate::canvas_context::{CanvasContext, OffscreenRenderingContext};
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasBinding::{
OffscreenCanvasMethods, OffscreenRenderingContext as RootedOffscreenRenderingContext,
ImageEncodeOptions, OffscreenCanvasMethods,
OffscreenRenderingContext as RootedOffscreenRenderingContext,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::blob::Blob;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::offscreencanvasrenderingcontext2d::OffscreenCanvasRenderingContext2D;
use crate::dom::promise::Promise;
use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::{CanGc, JSContext};
/// <https://html.spec.whatwg.org/multipage/#offscreencanvas>
@ -206,4 +213,71 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
canvas.set_natural_height(value as _, can_gc)
}
}
/// <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.
let in_realm_proof = AlreadyInRealm::assert::<crate::DomTypeHolder>();
let promise = Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), can_gc);
// Step 2. If this's context mode is 2d and the rendering context's
// output bitmap's origin-clean flag is set to false, then return a
// promise rejected with a "SecurityError" DOMException.
if !self.origin_is_clean() {
promise.reject_error(Error::Security, can_gc);
return promise;
}
// Step 3. If this's bitmap has no pixels (i.e., either its horizontal
// dimension or its vertical dimension is zero), then return a promise
// rejected with an "IndexSizeError" DOMException.
if self.Width() == 0 || self.Height() == 0 {
promise.reject_error(Error::IndexSize, can_gc);
return promise;
}
// Step 4. Let bitmap be a copy of this's bitmap.
let Some(mut snapshot) = self.get_image_data() else {
promise.reject_error(Error::InvalidState, can_gc);
return promise;
};
// Step 7. Run these steps in parallel:
// Step 7.1. Let file be a serialization of bitmap as a file, with
// options's type and quality if present.
// Step 7.2. Queue a global task on the canvas blob serialization task
// source given global to run these steps:
let trusted_this = Trusted::new(self);
let trusted_promise = TrustedPromise::new(promise.clone());
let image_type = EncodedImageType::from(options.type_.to_string());
let quality = options.quality;
self.global()
.task_manager()
.canvas_blob_task_source()
.queue(task!(convert_to_blob: move || {
let this = trusted_this.root();
let promise = trusted_promise.root();
let mut encoded: Vec<u8> = vec![];
if snapshot.encode_for_mime_type(&image_type, quality, &mut encoded).is_err() {
// Step 7.2.1. If file is null, then reject result with an
// "EncodingError" DOMException.
promise.reject_error(Error::Encoding, CanGc::note());
return;
};
// Step 7.2.2. Otherwise, resolve result with a new Blob object,
// created in global's relevant realm, representing file.
let blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
let blob = Blob::new(&this.global(), blob_impl, CanGc::note());
promise.resolve_native(&blob, CanGc::note());
}));
// Step 8. Return result.
promise
}
}