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

@ -226,6 +226,42 @@ pub fn clip(
.filter(|rect| !rect.is_empty())
}
#[derive(PartialEq)]
pub enum EncodedImageType {
Png,
Jpeg,
Webp,
}
impl From<String> for EncodedImageType {
// From: https://html.spec.whatwg.org/multipage/#serialising-bitmaps-to-a-file
// User agents must support PNG ("image/png"). User agents may support other
// types. If the user agent does not support the requested type, then it
// must create the file using the PNG format.
// Anything different than image/jpeg or image/webp is thus treated as PNG.
fn from(mime_type: String) -> Self {
let mime = mime_type.to_lowercase();
if mime == "image/jpeg" {
Self::Jpeg
} else if mime == "image/webp" {
Self::Webp
} else {
Self::Png
}
}
}
impl EncodedImageType {
pub fn as_mime_type(&self) -> String {
match self {
Self::Png => "image/png",
Self::Jpeg => "image/jpeg",
Self::Webp => "image/webp",
}
.to_owned()
}
}
/// Whether this response passed any CORS checks, and is thus safe to read from
/// in cross-origin environments.
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]