canvas: Unify retrieving image data from the HTMLImageElement (#37809)

Currently, HTMLImageElement uses as an image source
(ImageBitmapSource, CanvasImageSource, TexImageSource)
in various canvas2D/WebGL operations, and there is a small
inconsistency in how we get the image data of the 'img' element:
usability checking and retrieving the image data from the image cache.

To simplify and avoid state inconsistency between the window's image
cache and the 'img' element, let's retrieve the image data (as a raster)
from the HTMLImageElement itself.

Testing: No expected changes in testing results, except the
'drawimage_svg_image_with_foreign_object_does_not_taint.html'
which is 'false' passed because drawing of the non supported vector
image
is silently skip instead of throwing the 'InvalidState' exception
anymore.

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
Andrei Volykhin 2025-07-02 11:13:30 +03:00 committed by GitHub
parent d5b6160119
commit d0579256bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 118 additions and 140 deletions

View file

@ -19,7 +19,7 @@ use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapMethods, ImageBitmapOptions, ImageBitmapSource, ImageOrientation, PremultiplyAlpha,
ResizeQuality,
};
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::Serializable;
@ -28,7 +28,6 @@ use crate::dom::bindings::transferable::Transferable;
use crate::dom::globalscope::GlobalScope;
use crate::dom::types::Promise;
use crate::script_runtime::CanGc;
use crate::test::TrustedPromise;
#[dom_struct]
pub(crate) struct ImageBitmap {
@ -359,37 +358,13 @@ impl ImageBitmap {
return p;
}
// If no ImageBitmap object can be constructed, then the promise is rejected instead.
let Some(img) = image.image_data() else {
// If no ImageBitmap object can be constructed, then the promise
// is rejected instead.
let Some(snapshot) = image.get_raster_image_data() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
// TODO: Support vector HTMLImageElement.
let Some(img) = img.as_raster_image() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let size = Size2D::new(img.metadata.width, img.metadata.height);
let format = match img.format {
PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
pixel_format => {
unimplemented!("unsupported pixel format ({:?})", pixel_format)
},
};
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
let snapshot = Snapshot::from_vec(
size.cast(),
format,
alpha_mode,
img.first_frame().bytes.to_vec(),
);
// Step 6.3. Set imageBitmap's bitmap data to a copy of image's media data,
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =