Change canvas/context/snapshot size from u64 -> u32 (#36827)

Replaces uses of `euclid::default::Size2D<u64>` with
`euclid::default::Size2D<u32>` for the canvas/context/snapshot.

This PR includes changes to the following components:
 - canvas
 - pixels
 - script
 - script_bindings
 - shared/canvas
 - shared/snapshot

Testing: https://github.com/hashcatHitman/servo/actions/runs/15426115391
(as of 892edc0048)

Fixes: #36706

---------

Signed-off-by: hashcatHitman <155700084+hashcatHitman@users.noreply.github.com>
This commit is contained in:
Sam K 2025-06-07 14:37:21 +00:00 committed by GitHub
parent 87de9fdf8c
commit a625420b23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 69 additions and 69 deletions

View file

@ -48,7 +48,7 @@ pub(crate) trait CanvasContext {
true
}
fn size(&self) -> Size2D<u64> {
fn size(&self) -> Size2D<u32> {
self.canvas().size()
}
@ -73,12 +73,12 @@ pub(crate) trait CanvasContext {
}
pub(crate) trait CanvasHelpers {
fn size(&self) -> Size2D<u64>;
fn size(&self) -> Size2D<u32>;
fn canvas(&self) -> Option<&HTMLCanvasElement>;
}
impl CanvasHelpers for HTMLCanvasElementOrOffscreenCanvas {
fn size(&self) -> Size2D<u64> {
fn size(&self) -> Size2D<u32> {
match self {
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => {
canvas.get_size().cast()
@ -164,7 +164,7 @@ impl CanvasContext for RenderingContext {
}
}
fn size(&self) -> Size2D<u64> {
fn size(&self) -> Size2D<u32> {
match self {
RenderingContext::Placeholder(context) => (*context.context().unwrap()).size(),
RenderingContext::Context2d(context) => context.size(),
@ -251,7 +251,7 @@ impl CanvasContext for OffscreenRenderingContext {
}
}
fn size(&self) -> Size2D<u64> {
fn size(&self) -> Size2D<u32> {
match self {
OffscreenRenderingContext::Context2d(context) => context.size(),
}

View file

@ -383,7 +383,7 @@ impl CanvasState {
}
}
pub(crate) fn get_rect(&self, canvas_size: Size2D<u64>, rect: Rect<u64>) -> Vec<u8> {
pub(crate) fn get_rect(&self, canvas_size: Size2D<u32>, rect: Rect<u32>) -> Vec<u8> {
assert!(self.origin_is_clean());
assert!(Rect::from_size(canvas_size).contains_rect(&rect));
@ -531,11 +531,11 @@ impl CanvasState {
};
// Step 4. Establish the source and destination rectangles.
let video_size = snapshot.size().to_f64();
let dw = dw.unwrap_or(video_size.width);
let dh = dh.unwrap_or(video_size.height);
let sw = sw.unwrap_or(video_size.width);
let sh = sh.unwrap_or(video_size.height);
let video_size = snapshot.size();
let dw = dw.unwrap_or(video_size.width as f64);
let dh = dh.unwrap_or(video_size.height as f64);
let sw = sw.unwrap_or(video_size.width as f64);
let sh = sh.unwrap_or(video_size.height as f64);
let (source_rect, dest_rect) =
self.adjust_source_dest_rects(video_size, sx, sy, sw, sh, dx, dy, dw, dh);
@ -577,7 +577,7 @@ impl CanvasState {
let sw = sw.unwrap_or(canvas_size.width as f64);
let sh = sh.unwrap_or(canvas_size.height as f64);
let image_size = Size2D::new(canvas_size.width as f64, canvas_size.height as f64);
let image_size = Size2D::new(canvas_size.width, canvas_size.height);
// 2. Establish the source and destination rectangles
let (source_rect, dest_rect) =
self.adjust_source_dest_rects(image_size, sx, sy, sw, sh, dx, dy, dw, dh);
@ -632,7 +632,7 @@ impl CanvasState {
let sw = sw.unwrap_or(canvas_size.width as f64);
let sh = sh.unwrap_or(canvas_size.height as f64);
let image_size = Size2D::new(canvas_size.width as f64, canvas_size.height as f64);
let image_size = Size2D::new(canvas_size.width, canvas_size.height);
// 2. Establish the source and destination rectangles
let (source_rect, dest_rect) =
self.adjust_source_dest_rects(image_size, sx, sy, sw, sh, dx, dy, dw, dh);
@ -702,12 +702,12 @@ impl CanvasState {
let snapshot = self
.fetch_image_data(url, cors_setting)
.ok_or(Error::InvalidState)?;
let image_size = snapshot.size().to_f64();
let image_size = snapshot.size();
let dw = dw.unwrap_or(image_size.width);
let dh = dh.unwrap_or(image_size.height);
let sw = sw.unwrap_or(image_size.width);
let sh = sh.unwrap_or(image_size.height);
let dw = dw.unwrap_or(image_size.width as f64);
let dh = dh.unwrap_or(image_size.height as f64);
let sw = sw.unwrap_or(image_size.width as f64);
let sh = sh.unwrap_or(image_size.height as f64);
// Establish the source and destination rectangles
let (source_rect, dest_rect) =
@ -741,7 +741,7 @@ impl CanvasState {
#[allow(clippy::too_many_arguments)]
fn adjust_source_dest_rects(
&self,
image_size: Size2D<f64>,
image_size: Size2D<u32>,
sx: f64,
sy: f64,
sw: f64,
@ -766,7 +766,7 @@ impl CanvasState {
// When the source rectangle is outside the source image,
// the source rectangle must be clipped to the source image
let source_rect_clipped = source_rect
.intersection(&image_rect)
.intersection(&image_rect.to_f64())
.unwrap_or(Rect::zero());
// Width and height ratios between the non clipped and clipped source rectangles
@ -1486,7 +1486,7 @@ impl CanvasState {
#[allow(clippy::too_many_arguments)]
pub(crate) fn get_image_data(
&self,
canvas_size: Size2D<u64>,
canvas_size: Size2D<u32>,
global: &GlobalScope,
sx: i32,
sy: i32,
@ -1506,7 +1506,7 @@ impl CanvasState {
}
let (origin, size) = adjust_size_sign(Point2D::new(sx, sy), Size2D::new(sw, sh));
let read_rect = match pixels::clip(origin, size.to_u64(), canvas_size) {
let read_rect = match pixels::clip(origin, size.to_u32(), canvas_size) {
Some(rect) => rect,
None => {
// All the pixels are outside the canvas surface.
@ -1526,7 +1526,7 @@ impl CanvasState {
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
pub(crate) fn put_image_data(
&self,
canvas_size: Size2D<u64>,
canvas_size: Size2D<u32>,
imagedata: &ImageData,
dx: i32,
dy: i32,
@ -1547,7 +1547,7 @@ impl CanvasState {
#[allow(unsafe_code, clippy::too_many_arguments)]
pub(crate) fn put_image_data_(
&self,
canvas_size: Size2D<u64>,
canvas_size: Size2D<u32>,
imagedata: &ImageData,
dx: i32,
dy: i32,
@ -1579,7 +1579,7 @@ impl CanvasState {
Point2D::new(dirty_x, dirty_y),
Size2D::new(dirty_width, dirty_height),
);
let src_rect = match pixels::clip(src_origin, src_size.to_u64(), imagedata_size.to_u64()) {
let src_rect = match pixels::clip(src_origin, src_size.to_u32(), imagedata_size.to_u32()) {
Some(rect) => rect,
None => return,
};

View file

@ -397,8 +397,8 @@ impl HTMLCanvasElement {
// u32 can't panic, since the data comes from a canvas which is always smaller than
// u32::MAX.
let canvas_data = snapshot.data();
let width = snapshot.size().width as u32;
let height = snapshot.size().height as u32;
let width = snapshot.size().width;
let height = snapshot.size().height;
match image_type {
EncodedImageType::Png => {

View file

@ -154,8 +154,8 @@ impl ImageData {
}
#[allow(unsafe_code)]
pub(crate) unsafe fn get_rect(&self, rect: Rect<u64>) -> Cow<[u8]> {
pixels::rgba8_get_rect(self.as_slice(), self.get_size().to_u64(), rect)
pub(crate) unsafe fn get_rect(&self, rect: Rect<u32>) -> Cow<[u8]> {
pixels::rgba8_get_rect(self.as_slice(), self.get_size().to_u32(), rect)
}
pub(crate) fn get_size(&self) -> Size2D<u32> {

View file

@ -72,8 +72,11 @@ impl OffscreenCanvas {
)
}
pub(crate) fn get_size(&self) -> Size2D<u64> {
Size2D::new(self.Width(), self.Height())
pub(crate) fn get_size(&self) -> Size2D<u32> {
Size2D::new(
self.Width().try_into().unwrap_or(u32::MAX),
self.Height().try_into().unwrap_or(u32::MAX),
)
}
pub(crate) fn origin_is_clean(&self) -> bool {

View file

@ -534,7 +534,7 @@ impl WebGL2RenderingContext {
let src_origin = Point2D::new(x, y);
let src_size = Size2D::new(width as u32, height as u32);
let fb_size = Size2D::new(fb_width as u32, fb_height as u32);
match pixels::clip(src_origin, src_size.to_u64(), fb_size.to_u64()) {
match pixels::clip(src_origin, src_size.to_u32(), fb_size.to_u32()) {
Some(rect) => rect.to_u32(),
None => return,
}
@ -2183,7 +2183,7 @@ impl WebGL2RenderingContextMethods<crate::DomTypeHolder> for WebGL2RenderingCont
let src_origin = Point2D::new(x, y);
let src_size = Size2D::new(width as u32, height as u32);
let fb_size = Size2D::new(fb_width as u32, fb_height as u32);
if pixels::clip(src_origin, src_size.to_u64(), fb_size.to_u64()).is_none() {
if pixels::clip(src_origin, src_size.to_u32(), fb_size.to_u32()).is_none() {
return;
}
}

View file

@ -3838,7 +3838,7 @@ impl WebGLRenderingContextMethods<crate::DomTypeHolder> for WebGLRenderingContex
let src_origin = Point2D::new(x, y);
let src_size = Size2D::new(width as u32, height as u32);
let fb_size = Size2D::new(fb_width as u32, fb_height as u32);
let src_rect = match pixels::clip(src_origin, src_size.to_u64(), fb_size.to_u64()) {
let src_rect = match pixels::clip(src_origin, src_size.to_u32(), fb_size.to_u32()) {
Some(rect) => rect,
None => return,
};

View file

@ -167,8 +167,8 @@ impl GPUCanvasContext {
// causes FAIL on webgpu:web_platform,canvas,configure:usage:*
usage: configuration.usage | GPUTextureUsageConstants::COPY_SRC,
size: GPUExtent3D::GPUExtent3DDict(GPUExtent3DDict {
width: size.width as u32,
height: size.height as u32,
width: size.width,
height: size.height,
depthOrArrayLayers: 1,
}),
viewFormats: configuration.viewFormats.clone(),

View file

@ -129,10 +129,7 @@ impl XRWebGLLayer {
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => canvas.get_size(),
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => {
let size = canvas.get_size();
Size2D::new(
size.width.try_into().unwrap_or(0),
size.height.try_into().unwrap_or(0),
)
Size2D::new(size.width, size.height)
},
};
Size2D::from_untyped(size)