From 980fc70c262b463aa11dbd8ccbb817e596357b45 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 15 Oct 2026 09:19:20 +0800 Subject: [PATCH] fix(canvas): transform and sample drawImage sources natively --- moli-canvas/src/buffer.rs | 39 ++-- .../src/context_bootstrap/canvas.rs | 1 + .../canvas/context2d/draw_image.rs | 93 +++++---- .../src/context_bootstrap/canvas/drawing.rs | 56 +++--- .../context_bootstrap/canvas/image_paint.rs | 185 ++++++++++++++++++ .../src/context_bootstrap/canvas/path.rs | 12 ++ .../canvas/path/coordinates.rs | 14 +- .../src/script_vm/tests/canvas_draw_image.js | 143 ++++++++++++++ .../src/script_vm/tests/canvas_draw_image.rs | 17 ++ moli-renderer-v8/src/script_vm/tests/mod.rs | 1 + .../worker/thread/tests/canvas_compositing.rs | 16 +- 11 files changed, 485 insertions(+), 92 deletions(-) create mode 100644 moli-renderer-v8/src/context_bootstrap/canvas/image_paint.rs create mode 100644 moli-renderer-v8/src/script_vm/tests/canvas_draw_image.js create mode 100644 moli-renderer-v8/src/script_vm/tests/canvas_draw_image.rs diff --git a/moli-canvas/src/buffer.rs b/moli-canvas/src/buffer.rs index 401abcf0d6e..6cb1a3d3874 100644 --- a/moli-canvas/src/buffer.rs +++ b/moli-canvas/src/buffer.rs @@ -339,25 +339,26 @@ impl PixelBuffer { Some(()) } - /// Sample an image drawing before destination-format quantization. Shadow - /// generation applies globalAlpha and the shadow color to this alpha once. - pub fn sample_image( - &self, - blit: DrawImageBlit, - filter: ScaleFilter, - x: f64, - y: f64, - ) -> [f32; 4] { - let sx = blit.source_x + (x - blit.dest_x) * blit.source_width / blit.dest_width; - let sy = blit.source_y + (y - blit.dest_y) * blit.source_height / blit.dest_height; - if !(blit.dest_x.floor()..(blit.dest_x + blit.dest_width).ceil()).contains(&x) - || !(blit.dest_y.floor()..(blit.dest_y + blit.dest_height).ceil()).contains(&y) - || !(0.0..f64::from(self.width)).contains(&sx) - || !(0.0..f64::from(self.height)).contains(&sy) - { + /// Sample premultiplied pixels at image coordinates, where texel centers + /// are at half-integers. Filtering clamps to the full image's edges, not + /// to a drawImage source crop, without quantizing an intermediate surface. + pub fn sample_texel(&self, x: f64, y: f64, filter: ScaleFilter) -> [f32; 4] { + if self.width == 0 || self.height == 0 || x.is_nan() || y.is_nan() { return [0.0; 4]; } - self.filtered_pixel(sx, sy, filter) + match filter { + ScaleFilter::Nearest => self.filtered_pixel( + // A point exactly between texel centers selects the lower + // coordinate, matching the existing Canvas nearest tie rule. + (x.ceil() - 1.0).clamp(0.0, f64::from(self.width - 1)), + (y.ceil() - 1.0).clamp(0.0, f64::from(self.height - 1)), + filter, + ), + ScaleFilter::Bilinear => self.sample_bilinear( + (x - 0.5).clamp(0.0, f64::from(self.width - 1)), + (y - 0.5).clamp(0.0, f64::from(self.height - 1)), + ), + } } fn filtered_pixel(&self, x: f64, y: f64, filter: ScaleFilter) -> [f32; 4] { @@ -638,7 +639,7 @@ impl PixelBuffer { coverage: &[u8], clip: Option<&[u8]>, operation: CompositeOperation, - source: &impl PixelSource, + source: &(impl PixelSource + ?Sized), ) -> Option<()> { if clip.is_some_and(|mask| mask.len() != coverage.len()) { return None; @@ -689,7 +690,7 @@ impl PixelBuffer { &mut self, clip: Option<&[u8]>, operation: CompositeOperation, - source: &impl PixelSource, + source: &(impl PixelSource + ?Sized), ) -> Option<()> { if clip.is_some_and(|mask| Some(mask.len()) != crate::byte_len(self.width, self.height)) { return None; diff --git a/moli-renderer-v8/src/context_bootstrap/canvas.rs b/moli-renderer-v8/src/context_bootstrap/canvas.rs index a37551272df..e9ecf8ec8a7 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas.rs @@ -67,6 +67,7 @@ mod drawing; mod drawing_state; mod helpers; mod image_bitmap; +mod image_paint; mod objects; mod offscreen; mod path; diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/context2d/draw_image.rs b/moli-renderer-v8/src/context_bootstrap/canvas/context2d/draw_image.rs index acf1af05afc..2e5254a1d9b 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas/context2d/draw_image.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas/context2d/draw_image.rs @@ -1,11 +1,12 @@ use super::{CANVAS_CONTEXT_IMAGE_SMOOTHING_ENABLED_SLOT, context_bool_slot}; use crate::context_bootstrap::canvas::{ backing_store::canvas_owner_from_context, + image_paint::CanvasImagePaint, image_source::{CanvasImageSource, image_source_image as source_pixels}, }; use crate::context_bootstrap::image_data::ImageDataPixelFormat; use crate::{context_bootstrap::throw_dom_exception_value, web_api_interfaces, webidl}; -use moli_canvas::{DrawImageBlit, ScaleFilter}; +use moli_canvas::ScaleFilter; use moli_webapi_declare::{WebApiFunctionTemplate, web_api_object_target}; macro_rules! draw_image_declaration { @@ -113,18 +114,12 @@ impl Coordinates { coordinates.iter().all(|coordinate| coordinate.is_finite()) } - fn blit(self, width: u32, height: u32) -> Option { + fn rectangles(self, width: u32, height: u32) -> ([f64; 4], [f64; 4]) { let (width, height) = (f64::from(width), f64::from(height)); match self { - Self::Natural([dx, dy]) => { - DrawImageBlit::new(0.0, 0.0, width, height, dx, dy, width, height) - } - Self::Scaled([dx, dy, dw, dh]) => { - DrawImageBlit::new(0.0, 0.0, width, height, dx, dy, dw, dh) - } - Self::Cropped([sx, sy, sw, sh, dx, dy, dw, dh]) => { - DrawImageBlit::new(sx, sy, sw, sh, dx, dy, dw, dh) - } + Self::Natural([dx, dy]) => ([0.0, 0.0, width, height], [dx, dy, width, height]), + Self::Scaled([dx, dy, dw, dh]) => ([0.0, 0.0, width, height], [dx, dy, dw, dh]), + Self::Cropped([sx, sy, sw, sh, dx, dy, dw, dh]) => ([sx, sy, sw, sh], [dx, dy, dw, dh]), } } } @@ -192,9 +187,7 @@ fn canvas_context_draw_image_callback<'s>( if !image.origin_clean { super::super::backing_store::set_canvas_origin_clean(scope, canvas, false); } - let Some(blit) = coordinates.blit(width, height) else { - return; - }; + let (source_rectangle, destination_rectangle) = coordinates.rectangles(width, height); let filter = if context_bool_slot(scope, context, CANVAS_CONTEXT_IMAGE_SMOOTHING_ENABLED_SLOT) .unwrap_or(true) { @@ -203,6 +196,32 @@ fn canvas_context_draw_image_callback<'s>( ScaleFilter::Nearest }; let alpha = super::context_global_alpha(scope, context) as f32; + let space = + crate::context_bootstrap::canvas::settings::Canvas2dSettings::for_context(scope, context) + .color_space; + let format = if image.color_space != space { + ImageDataPixelFormat::RgbaFloat16 + } else { + ImageDataPixelFormat::from_native(image.pixels.format()) + }; + let source_alpha_mode = image.pixels.alpha_mode(); + let Some(image) = image.convert(space, format, source_alpha_mode) else { + crate::util::throw_range_error(scope, "Unable to allocate drawImage color conversion."); + return; + }; + let transform = crate::context_bootstrap::canvas::state::canvas_path_state(scope, context) + .borrow() + .transform(); + let Some(paint) = CanvasImagePaint::new( + &image.pixels, + source_rectangle, + destination_rectangle, + transform, + filter, + alpha, + ) else { + return; + }; let drawing = crate::context_bootstrap::canvas::drawing::CanvasDrawingState::for_context(scope, context); crate::context_bootstrap::canvas::clipping::draw_with_clip( @@ -210,36 +229,24 @@ fn canvas_context_draw_image_callback<'s>( context, canvas, |target, clip| { - let format = if image.color_space != target.color_space { - ImageDataPixelFormat::RgbaFloat16 - } else { - ImageDataPixelFormat::from_native(image.pixels.format()) - }; - if let Some(image) = - image.convert(target.color_space, format, target.pixels.alpha_mode()) - { - drawing.composite_shadow( - &mut target.pixels, - clip, - crate::context_bootstrap::canvas::drawing::ShadowSource::Image { - pixels: &image.pixels, - blit, - filter, - alpha, - }, - )?; - target - .pixels - .draw_image_with_operation( - &image.pixels, - blit, - filter, - alpha, - clip, - drawing.operation, - ) - .expect("validated image and clipping dimensions"); + let width = target.pixels.width(); + let height = target.pixels.height(); + if width == 0 || height == 0 { + return Ok(()); } + let coverage = paint.coverage(width, height)?; + drawing.composite_shadow( + &mut target.pixels, + clip, + crate::context_bootstrap::canvas::drawing::ShadowSource::Path { + path: paint.path(), + paint: &paint, + }, + )?; + target + .pixels + .composite_source(&coverage.rgba, clip, drawing.operation, &paint) + .expect("validated image and clipping dimensions"); Ok(()) }, ); diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/drawing.rs b/moli-renderer-v8/src/context_bootstrap/canvas/drawing.rs index a39e494faa8..dc09d38b588 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas/drawing.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas/drawing.rs @@ -1,8 +1,10 @@ //! Canvas drawing-model state and shadows, before the final bitmap clip. -use super::{context2d, style_state::CanvasPaint}; -use moli_canvas::{CompositeOperation, DrawImageBlit, PixelBuffer, PixelSource, ScaleFilter}; -use moli_layout::{PaintColor, PaintFragment, PaintSnapshot, PaintViewport}; +use super::{context2d, path::Canvas2dPathState}; +use moli_canvas::{CompositeOperation, PixelBuffer, PixelSource}; +use moli_layout::{ + LayoutTransform2D, PaintColor, PaintFillRule, PaintFragment, PaintSnapshot, PaintViewport, +}; use moli_paint::PaintError; // HTML explicitly permits an implementation limit on the Gaussian sigma. @@ -24,20 +26,18 @@ pub(super) struct CanvasDrawingState { pub(super) enum ShadowSource<'a> { Geometry { fragment: &'a PaintFragment, - paint: &'a CanvasPaint, + paint: &'a dyn PixelSource, + }, + Path { + path: &'a Canvas2dPathState, + paint: &'a dyn PixelSource, }, Text { text: &'a str, font: &'a str, x: f64, y: f64, - paint: &'a CanvasPaint, - }, - Image { - pixels: &'a PixelBuffer, - blit: DrawImageBlit, - filter: ScaleFilter, - alpha: f32, + paint: &'a dyn PixelSource, }, } @@ -266,22 +266,24 @@ impl ShadowSource<'_> { ); (coverage, *paint) } - Self::Image { - pixels, - blit, - filter, - alpha, - } => { - let mut blit = *blit; - blit.dest_x -= region.origin[0]; - blit.dest_y -= region.origin[1]; - return Ok((0..height) - .flat_map(|y| (0..width).map(move |x| (x, y))) - .map(|(x, y)| { - (pixels.sample_image(blit, *filter, f64::from(x), f64::from(y))[3] * alpha) - .clamp(0.0, 1.0) - }) - .collect()); + Self::Path { path, paint } => { + let path = path.transformed_snapshot(LayoutTransform2D::new([ + 1.0, + 0.0, + 0.0, + 1.0, + -region.origin[0], + -region.origin[1], + ])); + let raster = moli_paint::raster_canvas_clip_mask( + width, + height, + &[moli_paint::CanvasClip { + path: path.clip_paint_path(width, height)?, + fill_rule: PaintFillRule::NonZero, + }], + )?; + (raster.rgba, *paint) } }; Ok(coverage diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/image_paint.rs b/moli-renderer-v8/src/context_bootstrap/canvas/image_paint.rs new file mode 100644 index 00000000000..21595db0fac --- /dev/null +++ b/moli-renderer-v8/src/context_bootstrap/canvas/image_paint.rs @@ -0,0 +1,185 @@ +//! Native drawImage rectangle mapping, transformed coverage and image sampling. + +use super::path::{ + Canvas2dPathState, + coordinates::{NativeAffine, Point, Scalar}, +}; +use moli_canvas::{AlphaMode, PixelBuffer, PixelSource, ScaleFilter}; +use moli_layout::{LayoutTransform2D, PaintFillRule}; + +#[derive(Clone, Copy)] +struct ImageRect { + origin: Point, + width: Scalar, + height: Scalar, +} + +impl ImageRect { + fn new([x, y, width, height]: [f64; 4]) -> Option { + if width == 0.0 || height == 0.0 { + return None; + } + let mut origin = Point::new(x, y); + let mut width = Scalar::new(width); + let mut height = Scalar::new(height); + if width < Scalar::ZERO { + origin.x = origin.x + width; + width = -width; + } + if height < Scalar::ZERO { + origin.y = origin.y + height; + height = -height; + } + Some(Self { + origin, + width, + height, + }) + } + + fn transformed_corners(self, matrix: kurbo::Affine, offset: Point) -> [Point; 4] { + let origin = self.origin.transform(matrix); + let [a, b, c, d, _, _] = matrix.as_coeffs().map(Scalar::new); + let origin = Point { + x: origin.x + a * offset.x + c * offset.y, + y: origin.y + b * offset.x + d * offset.y, + }; + let across = Point { + x: a * self.width, + y: b * self.width, + }; + let down = Point { + x: c * self.height, + y: d * self.height, + }; + [ + origin, + Point { + x: origin.x + across.x, + y: origin.y + across.y, + }, + Point { + x: origin.x + across.x + down.x, + y: origin.y + across.y + down.y, + }, + Point { + x: origin.x + down.x, + y: origin.y + down.y, + }, + ] + } +} + +pub(super) struct CanvasImagePaint<'a> { + pixels: &'a PixelBuffer, + path: Canvas2dPathState, + inverse: NativeAffine, + source: ImageRect, + destination: ImageRect, + filter: ScaleFilter, + alpha: f32, +} + +impl<'a> CanvasImagePaint<'a> { + pub(super) fn new( + pixels: &'a PixelBuffer, + source: [f64; 4], + destination: [f64; 4], + transform: LayoutTransform2D, + filter: ScaleFilter, + alpha: f32, + ) -> Option { + let matrix = kurbo::Affine::new(transform.coefficients); + let inverse = NativeAffine::inverse(matrix)?; + let source = ImageRect::new(source)?; + let destination = ImageRect::new(destination)?; + let left = if source.origin.x < Scalar::ZERO { + Scalar::ZERO + } else { + source.origin.x + }; + let top = if source.origin.y < Scalar::ZERO { + Scalar::ZERO + } else { + source.origin.y + }; + let image_width = Scalar::new(f64::from(pixels.width())); + let image_height = Scalar::new(f64::from(pixels.height())); + let source_width = source.width - (left - source.origin.x); + let source_height = source.height - (top - source.origin.y); + let width = if source_width > image_width - left { + image_width - left + } else { + source_width + }; + let height = if source_height > image_height - top { + image_height - top + } else { + source_height + }; + if width <= Scalar::ZERO || height <= Scalar::ZERO { + return None; + } + // Clip source and destination proportionally before applying the CTM. + // Native scalars retain finite-input sums and products beyond f64. + let clipped = ImageRect { + origin: destination.origin, + width: width * destination.width / source.width, + height: height * destination.height / source.height, + }; + let offset = Point { + x: (left - source.origin.x) * destination.width / source.width, + y: (top - source.origin.y) * destination.height / source.height, + }; + let path = + Canvas2dPathState::from_closed_polygon(clipped.transformed_corners(matrix, offset)); + Some(Self { + pixels, + path, + inverse, + source, + destination, + filter, + alpha, + }) + } + + pub(super) fn path(&self) -> &Canvas2dPathState { + &self.path + } + + pub(super) fn coverage( + &self, + width: u32, + height: u32, + ) -> Result { + moli_paint::raster_canvas_clip_mask( + width, + height, + &[moli_paint::CanvasClip { + path: self.path.clip_paint_path(width, height)?, + fill_rule: PaintFillRule::NonZero, + }], + ) + } +} + +impl PixelSource for CanvasImagePaint<'_> { + fn alpha_mode(&self) -> AlphaMode { + AlphaMode::Premultiplied + } + + fn sample(&self, x: f64, y: f64) -> [f32; 4] { + if self.alpha == 0.0 { + return [0.0; 4]; + } + let local = self + .inverse + .map_relative(Point::new(x + 0.5, y + 0.5), self.destination.origin); + let x = self.source.origin.x + local.x * self.source.width / self.destination.width; + let y = self.source.origin.y + local.y * self.source.height / self.destination.height; + self.pixels + .sample_texel(x.lossy(), y.lossy(), self.filter) + .map(|channel| channel * self.alpha) + } +} diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/path.rs b/moli-renderer-v8/src/context_bootstrap/canvas/path.rs index 6a9d64e14f0..8aad27c0944 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas/path.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas/path.rs @@ -97,6 +97,18 @@ impl Default for Canvas2dPathState { const TWO_PI: f64 = std::f64::consts::TAU; impl Canvas2dPathState { + /// An owned bitmap-coordinate polygon, without narrowing native endpoints + /// or changing the context's current default path. + pub(super) fn from_closed_polygon(points: [Point; 4]) -> Self { + let mut state = Self::default(); + state.move_to_canvas(points[0]); + for point in points[1..].iter().copied() { + state.line_to_canvas(point); + } + state.close_path(); + state + } + pub(super) fn from_svg(raw: &str) -> Self { let mut state = Self::default(); for element in moli_svg::parse_path_data(raw).elements() { diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/path/coordinates.rs b/moli-renderer-v8/src/context_bootstrap/canvas/path/coordinates.rs index bd7392aca77..ddd327a57fb 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas/path/coordinates.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas/path/coordinates.rs @@ -343,10 +343,20 @@ impl NativeAffine { } pub(in crate::context_bootstrap::canvas) fn map(self, point: Point) -> Point { + self.map_relative(point, Point::ZERO) + } + + /// Subtract the local origin before adding pixel-sized variable terms. + /// Adding a very large translation first would discard those terms. + pub(in crate::context_bootstrap::canvas) fn map_relative( + self, + point: Point, + origin: Point, + ) -> Point { let [a, b, c, d, e, f] = self.0; Point { - x: a * point.x + c * point.y + e, - y: b * point.x + d * point.y + f, + x: a * point.x + c * point.y + (e - origin.x), + y: b * point.x + d * point.y + (f - origin.y), } } } diff --git a/moli-renderer-v8/src/script_vm/tests/canvas_draw_image.js b/moli-renderer-v8/src/script_vm/tests/canvas_draw_image.js new file mode 100644 index 00000000000..8c90554bf32 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/canvas_draw_image.js @@ -0,0 +1,143 @@ +(owner, callee, kind, label, videoFrameExposed = true) => { + const rows = [], colors = [[255,0,0,255],[0,255,0,255],[0,0,255,255],[255,255,255,255]]; + const assert = (ok, message='assertion failed') => {if (!ok) throw Error(message);}; + const check = (name, run) => { + try {run(); rows.push({name:label+': '+name,passed:true});} + catch (error) {rows.push({name:label+': '+name,passed:false,error:String(error)});} + }; + const proto = callee[kind==='element'?'CanvasRenderingContext2D':'OffscreenCanvasRenderingContext2D'].prototype; + const call = (ctx,name,...args) => Reflect.apply(proto[name],ctx,args); + const make = () => { + const canvas = kind==='element'?owner.document.createElement('canvas'):new owner.OffscreenCanvas(8,8); + canvas.width=8;canvas.height=8; + const ctx=canvas.getContext('2d');ctx.imageSmoothingEnabled=false; + return [canvas,ctx]; + }; + const source = () => { + const canvas=new owner.OffscreenCanvas(2,2); + canvas.getContext('2d').putImageData(new owner.ImageData(new owner.Uint8ClampedArray(colors.flat()),2,2),0,0); + return canvas; + }; + const pixel = (ctx,x,y) => Array.from(call(ctx,'getImageData',x,y,1,1).data); + const near = (actual,expected,tolerance=2) => assert(actual.every((v,i)=>Math.abs(v-expected[i])<=tolerance),actual+' != '+expected); + const red = ctx => {ctx.fillStyle='red';call(ctx,'fillRect',0,0,8,8);}; + const matrices = [ + ['identity',[1,0,0,1,0,0]], ['translation',[1,0,0,1,3,2]], + ['scale',[2,0,0,2,1,1]], ['reflection',[-1,0,0,1,5,1]], + ['rotation',[0,1,-1,0,5,1]], ['x shear',[1,0,1,1,1,1]], + ['y shear',[1,1,0,1,1,1]], ['anisotropic scale',[3,0,0,2,1,1]], + ]; + const palette = (matrix,args) => { + const [,ctx]=make();call(ctx,'setTransform',...matrix);call(ctx,'drawImage',source(),...args); + const [a,b,c,d,e,f]=matrix,det=a*d-b*c; + const local = (x,y) => [(d*(x-e)-c*(y-f))/det,(-b*(x-e)+a*(y-f))/det]; + let inside=0,checked=0; + for(let y=0;y<8;y++)for(let x=0;x<8;x++) { + const corners=[[x,y],[x+1,y],[x+1,y+1],[x,y+1]].map(p=>local(...p)); + if(corners.every(([x,y])=>x>=0&&x<=2&&y>=0&&y<=2)) { + const [sx,sy]=local(x+0.5,y+0.5); + const indices = value => (Number.isInteger(value)?[value-1,value]:[Math.floor(value)]).map(v=>Math.max(0,Math.min(1,v))); + const actual=pixel(ctx,x,y),candidates=indices(sy).flatMap(y=>indices(sx).map(x=>colors[y*2+x])); + // Nearest-neighbor ties on an exact texel boundary can select + // either neighbor. Geometry and all untied samples stay exact. + assert(candidates.some(expected=>actual.every((v,i)=>Math.abs(v-expected[i])<=2)),actual+' is not a nearest sample');inside++;checked++; + } else if([0,1].some(axis=>corners.every(p=>p[axis]<=0)||corners.every(p=>p[axis]>=2))) { + near(pixel(ctx,x,y),[0,0,0,0]);checked++; + } + } + assert(inside>0&&checked>inside,'matrix did not check inside and outside pixels'); + }; + for(const [name,matrix] of matrices)for(const [overload,args] of [[3,[0,0]],[5,[0,0,2,2]],[9,[0,0,2,2,0,0,2,2]]]) + check(name+' '+overload+' arguments',()=>palette(matrix,args)); + for(const [name,args] of [ + ['negative destination width',[2,0,-2,2]],['negative destination height',[0,2,2,-2]], + ['negative source width',[2,0,-2,2,0,0,2,2]],['negative source and destination',[2,2,-2,-2,2,2,-2,-2]], + ])check(name,()=>palette([1,0,0,1,0,0],args)); + check('source clipping proportionally clips transformed destination',()=>{ + const [,ctx]=make();call(ctx,'translate',1,1);call(ctx,'drawImage',source(),-1,0,3,2,0,0,6,4); + near(pixel(ctx,1,1),[0,0,0,0]);near(pixel(ctx,3,1),colors[0]);near(pixel(ctx,5,1),colors[1]); + near(pixel(ctx,3,3),colors[2]);near(pixel(ctx,5,3),colors[3]); + }); + check('tiny nonzero source width is retained when scaled up',()=>{ + const [,ctx]=make();call(ctx,'drawImage',source(),0.5,0,1e-200,2,0,0,4,4); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,3,0),colors[0]);near(pixel(ctx,0,3),colors[2]); + }); + for(const scale of [1e200,1e-200])check('finite extreme scale '+scale,()=>{ + const [,ctx]=make();call(ctx,'setTransform',scale,0,0,scale,0,0); + call(ctx,'drawImage',source(),0,0,4/scale,4/scale); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,3,0),colors[1]);near(pixel(ctx,0,3),colors[2]);near(pixel(ctx,3,3),colors[3]); + }); + check('large translation cancellation retains pixel-sized destination',()=>{ + const [,ctx]=make();call(ctx,'setTransform',1e200,0,0,1e200,-1e200,-1e200); + call(ctx,'drawImage',source(),1,1,4e-200,4e-200); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,3,0),colors[1]);near(pixel(ctx,0,3),colors[2]);near(pixel(ctx,3,3),colors[3]); + }); + check('copy composes transparent source outside transformed destination',()=>{ + const [,ctx]=make();red(ctx);ctx.globalCompositeOperation='copy';call(ctx,'translate',20,0);call(ctx,'drawImage',source(),0,0); + near(pixel(ctx,0,0),[0,0,0,0]);near(pixel(ctx,7,7),[0,0,0,0]); + }); + check('singular CTM does not compose',()=>{ + const [,ctx]=make();red(ctx);ctx.globalCompositeOperation='copy';call(ctx,'scale',0,1);call(ctx,'drawImage',source(),0,0); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,7,7),colors[0]); + }); + check('completely clipped source rectangle does not compose',()=>{ + const [,ctx]=make();red(ctx);ctx.globalCompositeOperation='copy';call(ctx,'drawImage',source(),10,10,2,2,0,0,2,2); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,7,7),colors[0]); + }); + for(const args of [[0,0,0,2],[0,0,2,0],[0,0,0,2,0,0,2,2]])check('zero rectangle '+args.join(','),()=>{ + const [,ctx]=make();red(ctx);ctx.globalCompositeOperation='copy';call(ctx,'drawImage',source(),...args); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,7,7),colors[0]); + }); + check('shadow follows transformed image with device-space offset',()=>{ + const [,ctx]=make();ctx.shadowColor='blue';ctx.shadowOffsetX=-3;call(ctx,'setTransform',2,0,0,2,4,4);call(ctx,'drawImage',source(),0,0); + near(pixel(ctx,1,4),[0,0,255,255]);near(pixel(ctx,4,4),colors[0]); + }); + check('source clip does not remove a transformed image shadow',()=>{ + const [,ctx]=make();call(ctx,'rect',0,0,2,8);call(ctx,'clip');ctx.shadowColor='blue';ctx.shadowOffsetX=-3; + call(ctx,'setTransform',2,0,0,2,4,4);call(ctx,'drawImage',source(),0,0); + near(pixel(ctx,1,4),[0,0,255,255]);near(pixel(ctx,4,4),[0,0,0,0]); + }); + check('self draw snapshots before transformed copy',()=>{ + const [canvas,ctx]=make();ctx.fillStyle='red';call(ctx,'fillRect',0,0,2,2);ctx.globalCompositeOperation='copy'; + call(ctx,'setTransform',-1,0,0,1,4,0);call(ctx,'drawImage',canvas,0,0); + near(pixel(ctx,0,0),[0,0,0,0]);near(pixel(ctx,3,0),colors[0]); + }); + check('numeric conversions precede CTM snapshot',()=>{ + const [,ctx]=make();call(ctx,'drawImage',source(),{valueOf(){call(ctx,'translate',4,3);return 0;}},0); + near(pixel(ctx,4,3),colors[0]);near(pixel(ctx,0,0),[0,0,0,0]); + }); + check('conversion exceptions do not paint',()=>{ + const [,ctx]=make();red(ctx);const sentinel={};let caught; + try{call(ctx,'drawImage',source(),{valueOf(){call(ctx,'translate',4,3);return 0;}},{valueOf(){throw sentinel;}});}catch(error){caught=error;} + assert(caught===sentinel);near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,7,7),colors[0]); + }); + check('drawing does not change the current path',()=>{ + const [,ctx]=make();call(ctx,'rect',6,6,2,2);call(ctx,'drawImage',source(),0,0);ctx.fillStyle='blue';call(ctx,'fill'); + near(pixel(ctx,6,6),[0,0,255,255]);near(pixel(ctx,0,0),colors[0]); + }); + check('source dimensions use native slots',()=>{ + const [,ctx]=make(),image=source(); + for(const key of ['width','height'])Object.defineProperty(image,key,{get(){throw Error('author '+key);}}); + call(ctx,'translate',3,2);call(ctx,'drawImage',image,0,0);near(pixel(ctx,3,2),colors[0]); + }); + check('HTMLCanvas source uses transformed native pixels',()=>{ + const [,ctx]=make(),image=owner.document?owner.document.createElement('canvas'):new owner.OffscreenCanvas(2,2); + image.width=2;image.height=2;image.getContext('2d').putImageData(new owner.ImageData(new owner.Uint8ClampedArray(colors.flat()),2,2),0,0); + call(ctx,'translate',3,2);call(ctx,'drawImage',image,0,0);near(pixel(ctx,3,2),colors[0]);near(pixel(ctx,4,3),colors[3]); + }); + check('VideoFrame exposure and transformed native pixels',()=>{ + if(!videoFrameExposed) {assert(owner.VideoFrame===undefined,'VideoFrame is not exposed in this worker kind');return;} + const [,ctx]=make(),frame=new owner.VideoFrame(new owner.Uint8Array(colors.flat()),{format:'RGBA',codedWidth:2,codedHeight:2,timestamp:1}); + try{call(ctx,'translate',3,2);call(ctx,'drawImage',frame,0,0);near(pixel(ctx,3,2),colors[0]);near(pixel(ctx,4,3),colors[3]);}finally{frame.close();} + }); + check('bilinear sampling uses texel centers and full-image crop neighbors',()=>{ + const [,ctx]=make();ctx.imageSmoothingEnabled=true;call(ctx,'drawImage',source(),0,0,1,1,0,0,4,1); + near(pixel(ctx,0,0),colors[0]);near(pixel(ctx,2,0),[223,32,0,255],3);near(pixel(ctx,3,0),[159,96,0,255],3); + }); + check('transformed source obeys globalAlpha copy and final clip',()=>{ + const [,ctx]=make();red(ctx);call(ctx,'rect',2,2,4,4);call(ctx,'clip');ctx.globalCompositeOperation='copy';ctx.globalAlpha=0.5; + call(ctx,'translate',4,2);call(ctx,'drawImage',source(),0,0); + near(pixel(ctx,5,2),[0,255,0,128]);near(pixel(ctx,3,3),[0,0,0,0]);near(pixel(ctx,0,0),colors[0]); + }); + return rows; +} diff --git a/moli-renderer-v8/src/script_vm/tests/canvas_draw_image.rs b/moli-renderer-v8/src/script_vm/tests/canvas_draw_image.rs new file mode 100644 index 00000000000..cb676e8172a --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/canvas_draw_image.rs @@ -0,0 +1,17 @@ +use super::*; + +#[test] +fn native_draw_image_transforms_crop_and_sample_across_realms() { + let mut vm = new_storage_page_task_executor_test_vm("https://canvas-draw-image.test/"); + vm.eval("document.body.innerHTML='';globalThis.drawImageRealms=[globalThis,document.querySelector('iframe').contentWindow];").unwrap(); + vm.eval(&format!( + "const probe={};globalThis.drawImageChecks=[];for(let owner=0;owner<2;owner++)for(let callee=0;callee<2;callee++)for(const kind of ['element','offscreen'])drawImageChecks.push(...probe(drawImageRealms[owner],drawImageRealms[callee],kind,owner+'-'+callee+'-'+kind));", + include_str!("canvas_draw_image.js") + )).unwrap(); + assert_eq!(vm.eval("drawImageChecks.length").unwrap(), "400"); + assert_eq!( + vm.eval("JSON.stringify(drawImageChecks.filter(row=>!row.passed))") + .unwrap(), + "[]" + ); +} diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs index ced6e72c418..7c9c666b0dc 100644 --- a/moli-renderer-v8/src/script_vm/tests/mod.rs +++ b/moli-renderer-v8/src/script_vm/tests/mod.rs @@ -2307,6 +2307,7 @@ mod dom_token_list_native_receivers; mod canvas_arguments; mod canvas_compositing; +mod canvas_draw_image; mod canvas_drawing_state; mod canvas_fill_rect; mod canvas_gradients; diff --git a/moli-renderer-v8/src/worker/thread/tests/canvas_compositing.rs b/moli-renderer-v8/src/worker/thread/tests/canvas_compositing.rs index 99d80a2492e..f0241ff54c7 100644 --- a/moli-renderer-v8/src/worker/thread/tests/canvas_compositing.rs +++ b/moli-renderer-v8/src/worker/thread/tests/canvas_compositing.rs @@ -24,6 +24,15 @@ async fn worker_canvas_shadows_preserve_unclipped_sources_and_ordered_compositin assert_canvas_fixture_worker_kinds(source, 25).await; } +#[tokio::test] +async fn worker_draw_image_transforms_crop_and_sample_across_global_kinds() { + let source = format!( + "const probe={};const rows=probe(globalThis,globalThis,'offscreen','worker',workerVideoFrameExposed);console.log(JSON.stringify({{total:rows.length,failures:rows.filter(row=>!row.passed)}}));", + include_str!("../../../script_vm/tests/canvas_draw_image.js") + ); + assert_canvas_fixture_worker_kinds(source, 50).await; +} + async fn assert_canvas_fixture_worker_kinds(source: String, expected: u64) { ensure_v8(); let storage_key = moli_storage_key::MoliStorageKey::new( @@ -46,9 +55,14 @@ async fn assert_canvas_fixture_worker_kinds(source: String, expected: u64) { scope_url: url::Url::parse("https://canvas-compositing.test/").unwrap(), }, ] { + let source = format!( + "const workerVideoFrameExposed={};\n{}", + matches!(&kind, super::super::WorkerGlobalKind::Dedicated { .. }), + source + ); let mut handle = spawn_test_worker_with_options( WorkerSpawnOptions::new( - source.clone(), + source, "https://canvas-compositing.test/worker.js".to_owned(), ) .with_global_kind(kind),