fix(canvas): implement native clipping regions and viewport projection

This commit is contained in:
ldm0
2026-11-20 22:47:52 +08:00
parent cf3bc0e91c
commit aa4caba7f9
21 changed files with 1196 additions and 86 deletions
Generated
+14
View File
@@ -3525,6 +3525,9 @@ dependencies = [
"moli-websocket",
"moli-window-features",
"moli-xpath",
"num-bigint",
"num-rational",
"num-traits",
"parking_lot",
"percent-encoding",
"png",
@@ -4087,6 +4090,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
+135 -6
View File
@@ -231,7 +231,7 @@ impl PixelBuffer {
/// Copy/resample pixels, interpolating in premultiplied coordinates.
pub fn blit(&mut self, source: &Self, blit: DrawImageBlit, filter: ScaleFilter) {
self.sample_into(source, blit, filter, false, 1.0);
self.sample_into(source, blit, filter, false, 1.0, None);
}
pub fn draw_image(
@@ -244,7 +244,25 @@ impl PixelBuffer {
if alpha == 0.0 {
return;
}
self.sample_into(source, blit, filter, true, alpha);
self.sample_into(source, blit, filter, true, alpha, None);
}
/// Source-over drawing inside a validated bitmap-coordinate clip mask.
pub fn draw_image_with_clip(
&mut self,
source: &Self,
blit: DrawImageBlit,
filter: ScaleFilter,
alpha: f32,
clip: Option<&[u8]>,
) -> Option<()> {
if clip.is_some_and(|mask| Some(mask.len()) != crate::byte_len(self.width, self.height)) {
return None;
}
if alpha != 0.0 {
self.sample_into(source, blit, filter, true, alpha, clip);
}
Some(())
}
fn sample_into(
@@ -254,6 +272,7 @@ impl PixelBuffer {
filter: ScaleFilter,
composite: bool,
alpha: f32,
clip: Option<&[u8]>,
) {
let left = blit.dest_x.floor().max(0.0).min(f64::from(self.width)) as u32;
let top = blit.dest_y.floor().max(0.0).min(f64::from(self.height)) as u32;
@@ -272,6 +291,12 @@ impl PixelBuffer {
continue;
}
for x in left..right {
let coverage = clip.map_or(1.0, |mask| {
f32::from(mask[(y as usize * self.width as usize + x as usize) * 4 + 3]) / 255.0
});
if coverage == 0.0 {
continue;
}
let sx = blit.source_x
+ (f64::from(x) - blit.dest_x) * blit.source_width / blit.dest_width;
if sx < 0.0 || sx >= f64::from(source.width) {
@@ -295,7 +320,7 @@ impl PixelBuffer {
};
if composite {
for channel in &mut pixel {
*channel *= alpha;
*channel *= alpha * coverage;
}
if self.format == PixelFormat::Unorm8 {
pixel = self.format.quantize(pixel);
@@ -398,6 +423,52 @@ impl PixelBuffer {
}
}
/// Clear geometry through the current clip. Uncovered HDR samples retain
/// their exact bits; opaque contexts blend toward opaque black instead.
pub fn clear_coverage(
&mut self,
coverage: &[u8],
clip: Option<&[u8]>,
opaque: bool,
) -> Option<()> {
if crate::byte_len(self.width, self.height)? != coverage.len()
|| clip.is_some_and(|mask| mask.len() != coverage.len())
{
return None;
}
for (index, (destination, mask)) in self
.bytes
.chunks_exact_mut(self.format.bytes_per_pixel())
.zip(coverage.as_chunks::<4>().0)
.enumerate()
{
let clipping = clip.map_or(1.0, |mask| f32::from(mask[index * 4 + 3]) / 255.0);
let amount = (f32::from(mask[3]) / 255.0) * clipping;
if amount == 0.0 {
continue;
}
if amount == 1.0 {
self.format
.write(destination, [0.0, 0.0, 0.0, if opaque { 1.0 } else { 0.0 }]);
continue;
}
let mut target = self
.alpha_mode
.convert_pixel(self.format.read(destination), AlphaMode::Premultiplied);
for channel in &mut target {
*channel *= 1.0 - amount;
}
if opaque {
target[3] = 1.0;
}
self.format.write(
destination,
AlphaMode::Premultiplied.convert_pixel(target, self.alpha_mode),
);
}
Some(())
}
pub fn force_opaque(&mut self) {
for pixel in self.bytes.chunks_exact_mut(self.format.bytes_per_pixel()) {
match self.format {
@@ -423,22 +494,36 @@ impl PixelBuffer {
/// Apply an RGBA8 coverage mask without quantizing the source color or
/// unrelated destination pixels. Used with the path rasterizer's geometry.
pub fn composite_solid(&mut self, coverage: &[u8], color: [f32; 4]) -> Option<()> {
self.composite_solid_with_clip(coverage, color, None)
}
pub fn composite_solid_with_clip(
&mut self,
coverage: &[u8],
color: [f32; 4],
clip: Option<&[u8]>,
) -> Option<()> {
if clip.is_some_and(|mask| mask.len() != coverage.len()) {
return None;
}
if crate::byte_len(self.width, self.height)? != coverage.len() {
return None;
}
if color[3] == 0.0 {
return Some(());
}
for (destination, mask) in self
for (index, (destination, mask)) in self
.bytes
.chunks_exact_mut(self.format.bytes_per_pixel())
.zip(coverage.as_chunks::<4>().0)
.enumerate()
{
if mask[3] == 0 {
let clipping = clip.map_or(1.0, |mask| f32::from(mask[index * 4 + 3]) / 255.0);
if mask[3] == 0 || clipping == 0.0 {
continue;
}
let mut source = color;
source[3] *= f32::from(mask[3]) / 255.0;
source[3] *= (f32::from(mask[3]) / 255.0) * clipping;
let mut source = AlphaMode::Straight.convert_pixel(source, AlphaMode::Premultiplied);
if self.format == PixelFormat::Unorm8 {
// The 8-bit raster source uses the same channel precision as
@@ -464,6 +549,50 @@ fn source_over(destination: [f32; 4], source: [f32; 4]) -> [f32; 4] {
#[cfg(test)]
mod tests {
#[test]
fn clipping_preserves_uncovered_hdr_bits_for_solid_and_image_drawing() {
let mut target = half_image(2, &[2.0, -3.0, 0.25, 1.0, 4.0, -2.0, 0.5, 1.0]);
let original = target.bytes().to_vec();
let clip = [0, 0, 0, 255, 0, 0, 0, 0];
target
.composite_solid_with_clip(&[255; 8], [4.0, -4.0, 0.5, 0.5], Some(&clip))
.unwrap();
assert_eq!(target.pixel(0, 0), Some([3.0, -3.5, 0.375, 1.0]));
assert_eq!(&target.bytes()[8..], &original[8..]);
let source = half_image(2, &[4.0, 2.0, -1.0, 1.0, 2.0, 2.0, 2.0, 1.0]);
target
.draw_image_with_clip(
&source,
DrawImageBlit::new(0.0, 0.0, 2.0, 1.0, 0.0, 0.0, 2.0, 1.0).unwrap(),
ScaleFilter::Nearest,
1.0,
Some(&clip),
)
.unwrap();
assert_eq!(target.pixel(0, 0), Some([4.0, 2.0, -1.0, 1.0]));
assert_eq!(&target.bytes()[8..], &original[8..]);
}
#[test]
fn clipped_clear_keeps_hdr_samples_and_handles_opaque_edges() {
for opaque in [false, true] {
let mut target = half_image(2, &[2.0, -1.0, 0.5, 1.0, 4.0, -2.0, 0.5, 1.0]);
let original = target.bytes().to_vec();
target
.clear_coverage(&[255; 8], Some(&[0, 0, 0, 128, 0, 0, 0, 0]), opaque)
.unwrap();
let p = target.pixel(0, 0).unwrap();
if opaque {
assert!((p[0] - 2.0 * 127.0 / 255.0).abs() < 0.002);
assert_eq!(p[3], 1.0);
} else {
assert_eq!(p[0], 2.0);
assert!((p[3] - 127.0 / 255.0).abs() < 0.001);
}
assert_eq!(&target.bytes()[8..], &original[8..]);
}
}
use super::*;
fn half_image(width: u32, channels: &[f32]) -> PixelBuffer {
+6
View File
@@ -47,6 +47,8 @@ pub enum PaintError {
required_bytes: usize,
max_bytes: usize,
},
/// Exact clipping arithmetic would exceed its coordinate precision budget.
CanvasClipCoordinateBudgetExceeded { bits: u64, max_bits: u64 },
/// The backend returned a buffer with an unexpected length.
UnexpectedBufferLength {
/// Checked expected byte length.
@@ -113,6 +115,10 @@ impl fmt::Display for PaintError {
formatter,
"raster and nested filter surfaces conservatively account for {required_bytes} RGBA bytes, exceeding the {max_bytes}-byte transient budget"
),
Self::CanvasClipCoordinateBudgetExceeded { bits, max_bits } => write!(
formatter,
"Canvas clipping requires {bits}-bit coordinates, exceeding the {max_bits}-bit budget"
),
Self::UnexpectedBufferLength { expected, actual } => write!(
formatter,
"raster backend returned {actual} bytes, expected {expected}"
+3 -1
View File
@@ -15,4 +15,6 @@ mod validation;
pub use error::PaintError;
pub use moli_image::RgbaImage as RasterImage;
pub use raster::{MAX_TRANSIENT_RASTER_BYTES, raster_snapshot};
pub use raster::{
CanvasClip, MAX_TRANSIENT_RASTER_BYTES, raster_canvas_clip_mask, raster_snapshot,
};
+59 -2
View File
@@ -13,7 +13,7 @@ mod filters;
#[cfg(test)]
mod tests;
use crate::geometry::{device_transform, to_backend_shape};
use crate::geometry::{device_transform, to_backend_path, to_backend_shape};
use crate::scene::{paint_fragment, sanitize_unit, to_backend_blend_mode, to_backend_color};
use crate::validation::validate_fragment_stream;
use crate::{PaintError, RasterImage};
@@ -21,7 +21,7 @@ use anyrender::ImageRenderer;
use anyrender::PaintScene;
use anyrender_vello_cpu::VelloCpuImageRenderer;
use filters::{apply_software_color_filter, is_software_color_filter};
use moli_layout::{PaintCaptureSurface, PaintColor, PaintFragment, PaintSnapshot};
use moli_layout::{PaintCaptureSurface, PaintColor, PaintFragment, PaintPath, PaintSnapshot};
use peniko::kurbo::{Affine, Rect};
use peniko::{
Blob, Extend, Fill, ImageAlphaType, ImageBrush, ImageData, ImageFormat, ImageQuality,
@@ -39,6 +39,63 @@ struct RasterDimensions {
byte_len: usize,
}
/// One immutable Canvas clipping path, already frozen in bitmap coordinates.
#[derive(Clone, Debug)]
pub struct CanvasClip {
pub path: PaintPath,
pub fill_rule: moli_layout::PaintFillRule,
}
/// Rasterize the geometric intersection of Canvas clips. Each path keeps its
/// own winding rule; the backend receives a real clip stack, not a product of
/// independently rasterized masks. No live layout or DOM state is consulted.
pub fn raster_canvas_clip_mask(
width: u32,
height: u32,
clips: &[CanvasClip],
) -> Result<RasterImage, PaintError> {
let surface =
PaintCaptureSurface::for_viewport(moli_layout::PaintViewport::new(width, height, 1.0));
let dimensions = validate_dimensions(surface)?;
validate_resource_budget(dimensions, 0)?;
let mut renderer = VelloCpuImageRenderer::new(width, height);
let mut rgba = Vec::new();
renderer.render_to_vec(
|scene| {
for clip in clips {
scene.render_ctx.set_fill_rule(match clip.fill_rule {
moli_layout::PaintFillRule::NonZero => Fill::NonZero,
moli_layout::PaintFillRule::EvenOdd => Fill::EvenOdd,
});
scene.push_clip_layer(Affine::IDENTITY, &to_backend_path(&clip.path));
}
scene.fill(
Fill::NonZero,
Affine::IDENTITY,
to_backend_color(PaintColor::WHITE),
None,
&Rect::new(0.0, 0.0, f64::from(width), f64::from(height)),
);
for _ in clips {
scene.pop_layer();
}
},
&mut rgba,
);
if rgba.len() != dimensions.byte_len {
return Err(PaintError::UnexpectedBufferLength {
expected: dimensions.byte_len,
actual: rgba.len(),
});
}
unpremultiply_rgba(&mut rgba);
Ok(RasterImage {
width,
height,
rgba,
})
}
/// Rasterizes an owned snapshot to row-major, straight-alpha RGBA8 pixels with Vello CPU.
pub fn raster_snapshot(snapshot: &PaintSnapshot) -> Result<RasterImage, PaintError> {
let dimensions = validate_dimensions(snapshot.surface)?;
+3
View File
@@ -36,6 +36,9 @@ kurbo = "0.13.1"
futures-util = "0.3.31"
half = { version = "2.7.1", default-features = false }
icu_segmenter = { version = "2.1.2", default-features = false, features = ["compiled_data"] }
num-bigint = "0.4.6"
num-rational = "0.4.2"
num-traits = "0.2.19"
precomputed-hash = "0.1"
read-fonts = "0.41.0"
percent-encoding = "2.3"
@@ -95,6 +95,7 @@ pub(crate) use blob_serialization::{
CanvasBlobCompletion, CanvasBlobEncodeJob, CanvasBlobFile, CanvasBlobPromise, CanvasBlobTask,
CanvasBlobTaskEffect, canvas_to_blob_callback,
};
mod clipping;
mod constructors;
mod context2d;
mod drawing_state;
@@ -140,7 +141,6 @@ pub(crate) use context2d::{
canvas_context_line_width_getter_callback, canvas_context_line_width_setter_callback,
canvas_context_measure_text_callback, canvas_context_miter_limit_getter_callback,
canvas_context_miter_limit_setter_callback, canvas_context_move_to_callback,
canvas_context_noop_callback,
canvas_context_quadratic_curve_to_callback, canvas_context_rect_callback,
canvas_context_reset_transform_callback, canvas_context_rotate_callback,
canvas_context_scale_callback, canvas_context_set_line_dash_callback,
@@ -316,31 +316,48 @@ pub(super) fn with_canvas_image_mut<'s, F>(
) -> bool
where
F: FnOnce(&mut CanvasImage),
{
match try_with_canvas_image_mut(scope, canvas, |image| {
mutate(image);
Ok::<(), std::convert::Infallible>(())
}) {
Ok(changed) => changed,
Err(never) => match never {},
}
}
pub(super) fn try_with_canvas_image_mut<'s, E, F>(
scope: &mut v8::PinScope<'s, '_>,
canvas: v8::Local<'s, v8::Object>,
mutate: F,
) -> Result<bool, E>
where
F: FnOnce(&mut CanvasImage) -> Result<(), E>,
{
let Some((view, width, height)) = canvas_like_pixel_view(scope, canvas) else {
return false;
return Ok(false);
};
let (space, format) = canvas_pixel_settings(scope, canvas);
let alpha_mode = canvas_alpha_mode(scope, canvas);
let Some(mut image) = CanvasImage::from_view(view, width, height, format, space, alpha_mode)
else {
return false;
return Ok(false);
};
mutate(&mut image);
mutate(&mut image)?;
if get_private_value(scope, canvas, CANVAS_BITMAP_OPAQUE_SLOT)
.is_some_and(|value| value.boolean_value(scope))
{
image.pixels.force_opaque();
}
if write_bytes_to_view(scope, view, image.pixels.bytes()).is_none() {
return false;
return Ok(false);
}
if html_canvas_identity(scope, canvas).is_some()
&& let Some((bytes, _, _)) = image.into_rgba8()
{
replace_html_canvas_pixels(scope, canvas, width, height, bytes);
}
true
Ok(true)
}
pub(super) fn canvas_like_pixels_copy<'s>(
@@ -0,0 +1,198 @@
//! Immutable clipping snapshots and their shared, dimension-specific masks.
use super::{path::Canvas2dPathState, path2d::CanvasFillRule, state::canvas_clipping_state};
use std::{cell::RefCell, rc::Rc};
#[derive(Clone)]
struct ClipPath {
path: Canvas2dPathState,
rule: moli_layout::PaintFillRule,
}
#[derive(Default)]
struct ClipData {
paths: Vec<Rc<ClipPath>>,
mask: RefCell<Option<Rc<moli_paint::RasterImage>>>,
}
#[derive(Clone, Default)]
pub(super) struct CanvasClipRegion(Rc<ClipData>);
impl CanvasClipRegion {
pub(super) fn intersect(&mut self, path: Canvas2dPathState, rule: CanvasFillRule) {
let mut paths = self.0.paths.clone();
paths.push(Rc::new(ClipPath {
path,
rule: rule.paint_rule(),
}));
self.0 = Rc::new(ClipData {
paths,
mask: RefCell::default(),
});
}
pub(super) fn mask(
&self,
width: u32,
height: u32,
) -> Result<Option<Rc<moli_paint::RasterImage>>, moli_paint::PaintError> {
if self.0.paths.is_empty() || width == 0 || height == 0 {
return Ok(None);
}
if let Some(mask) = self.0.mask.borrow().as_ref()
&& mask.width == width
&& mask.height == height
{
return Ok(Some(mask.clone()));
}
let clips = self
.0
.paths
.iter()
.map(|clip| {
Ok(moli_paint::CanvasClip {
path: clip.path.clip_paint_path(width, height)?,
fill_rule: clip.rule,
})
})
.collect::<Result<Vec<_>, moli_paint::PaintError>>()?;
let mask = Rc::new(moli_paint::raster_canvas_clip_mask(width, height, &clips)?);
*self.0.mask.borrow_mut() = Some(mask.clone());
Ok(Some(mask))
}
}
pub(super) fn region<'s>(
scope: &mut v8::PinScope<'s, '_>,
context: v8::Local<'s, v8::Object>,
) -> CanvasClipRegion {
canvas_clipping_state(scope, context).borrow().clone()
}
pub(super) fn throw_paint_error(scope: &mut v8::PinScope<'_, '_>, error: &moli_paint::PaintError) {
let message = crate::util::v8_string(scope, &format!("Canvas drawing failed: {error}"))
.unwrap_or_else(|| v8::String::empty(scope));
let exception = v8::Exception::range_error(scope, message);
scope.throw_exception(exception);
}
/// All drawing consumers share the same final clip. putImageData deliberately
/// uses the raw bitmap updater because pixel uploads do not use drawing state.
pub(super) fn draw_with_clip<'s, F>(
scope: &mut v8::PinScope<'s, '_>,
context: v8::Local<'s, v8::Object>,
canvas: v8::Local<'s, v8::Object>,
draw: F,
) where
F: FnOnce(&mut super::pixels::CanvasImage, Option<&[u8]>) -> Result<(), moli_paint::PaintError>,
{
let clipping = region(scope, context);
let result = super::backing_store::try_with_canvas_image_mut(scope, canvas, |image| {
let mask = clipping.mask(image.pixels.width(), image.pixels.height())?;
draw(image, mask.as_ref().map(|mask| mask.rgba.as_slice()))
});
if let Err(error) = result {
throw_paint_error(scope, &error);
}
}
pub(super) fn clip_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
_rv: v8::ReturnValue<'_, v8::Value>,
) {
let context = moli_webapi_declare::web_api_object_target(scope, args.this())
.expect("generated clip receiver");
let Some((explicit, rule)) = super::path2d::clip_arguments(scope, &args) else {
return;
};
// Author conversions can reset the context or change either path. Read
// geometry and the current transform only after every conversion succeeds.
let context_path = super::state::canvas_path_state(scope, context);
let transform = if explicit.is_some() {
context_path.borrow().transform()
} else {
moli_layout::LayoutTransform2D::IDENTITY
};
let source = explicit
.map(|path| super::state::canvas_path_state(scope, path))
.unwrap_or(context_path);
let snapshot = source.borrow().transformed_snapshot(transform);
canvas_clipping_state(scope, context)
.borrow_mut()
.intersect(snapshot, rule);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_clipping_preserves_large_finite_rectangles_and_diagonal_sides() {
let mut path = Canvas2dPathState::default();
path.set_transform(f64::MAX, 0.0, 0.0, f64::MAX, 0.0, 0.0);
path.rect(-10.0, -10.0, 20.0, 20.0);
let mut clip = CanvasClipRegion::default();
clip.intersect(path, CanvasFillRule::Nonzero);
let mask = clip.mask(8, 8).unwrap().unwrap();
assert!(mask.rgba.as_chunks::<4>().0.iter().all(|p| p[3] == 255));
let mut path = Canvas2dPathState::default();
path.set_transform(1e200, 0.0, 0.0, 1e200, 0.0, 0.0);
path.move_to(-10.0, -10.0);
path.line_to(10.0, 10.0);
path.line_to(-10.0, 10.0);
path.close_path();
let mut clip = CanvasClipRegion::default();
clip.intersect(path, CanvasFillRule::Nonzero);
let mask = clip.mask(8, 8).unwrap().unwrap();
assert_eq!(mask.rgba[(7 * 8) * 4 + 3], 255);
assert_eq!(mask.rgba[7 * 4 + 3], 0);
let mut path = Canvas2dPathState::default();
path.move_to(-1e200, -2e200);
path.line_to(1e200, 2e200);
path.line_to(-1e200, 2e200);
path.close_path();
let mut clip = CanvasClipRegion::default();
clip.intersect(path, CanvasFillRule::Nonzero);
let mask = clip.mask(8, 8).unwrap().unwrap();
// Clamping each coordinate independently would change this slope.
assert_eq!(mask.rgba[(5 * 8 + 3) * 4 + 3], 0);
assert_eq!(mask.rgba[(7 * 8) * 4 + 3], 255);
}
#[test]
fn native_clip_curves_retain_pixel_sized_terms_beside_enormous_controls() {
let mut path = Canvas2dPathState::default();
path.move_to(-1e200, -1e200);
path.quadratic_curve_to(1e200, 1e200, 8.0, 0.0);
path.close_path();
let mut clip = CanvasClipRegion::default();
clip.intersect(path, CanvasFillRule::Nonzero);
let mask = clip.mask(8, 8).unwrap().unwrap();
// At the near-origin crossing t tends to 1/3 and x-y tends to 8/9.
// Rounded subdivision would erase the endpoint's eight-pixel offset
// and incorrectly turn these diagonal samples half-opaque.
assert!(mask.rgba[(8 + 1) * 4 + 3] < 8);
assert_eq!(mask.rgba[(8 + 7) * 4 + 3], 255);
assert_eq!(mask.rgba[(7 * 8) * 4 + 3], 0);
}
#[test]
fn native_clip_precision_is_bounded_before_allocating_unbounded_integers() {
let mut path = Canvas2dPathState::default();
path.rect(-1.0, -1.0, 2.0, 2.0);
for _ in 0..20 {
let mut next = Canvas2dPathState::default();
next.add_path(&path, [f64::MAX, 0.0, 0.0, f64::MAX, 0.0, 0.0]);
path = next;
}
let mut clip = CanvasClipRegion::default();
clip.intersect(path, CanvasFillRule::Nonzero);
assert!(matches!(
clip.mask(8, 8),
Err(moli_paint::PaintError::CanvasClipCoordinateBudgetExceeded { .. })
));
}
}
@@ -1,7 +1,7 @@
use moli_webapi::canvas_context_values::ImageSmoothingQuality;
use super::backing_store::{canvas_owner_from_context, with_canvas_image_mut};
use super::helpers::canvas_unrestricted_double_arg;
use super::backing_store::canvas_owner_from_context;
use super::state::canvas_path_state;
use super::*;
use crate::util::{get_private_value, set_private_value};
@@ -23,7 +23,7 @@ pub(super) fn reset_canvas_context_state<'s>(
) {
super::helpers::init_canvas_like_context_object(scope, context);
super::drawing_state::clear_canvas_drawing_stack(scope, context);
super::state::reset_canvas_path_state(scope, context);
super::state::reset_canvas_native_state(scope, context);
}
macro_rules! canvas_rect_declaration {
@@ -643,7 +643,7 @@ pub(crate) fn canvas_context_fill_rect_callback<'s>(
transform,
};
let color = context_fill_color(scope, context);
rasterize_canvas_fragment(scope, canvas, fragment, color);
rasterize_canvas_fragment(scope, context, canvas, fragment, color);
}
fn canvas_context_clear_rect_callback<'s>(
@@ -652,18 +652,48 @@ fn canvas_context_clear_rect_callback<'s>(
_rv: v8::ReturnValue<'_, v8::Value>,
) {
let context = moli_webapi_declare::web_api_object_target(scope, args.this())
.expect("generated clearRect binding validates the receiver");
.expect("generated clearRect receiver");
let Some(parsed) = webidl::parse_args::<CanvasContextClearRectArgs>(scope, &args) else {
return;
};
let Some(rect) = canvas_normalize_rect(parsed.x, parsed.y, parsed.width, parsed.height) else {
if ![parsed.x, parsed.y, parsed.width, parsed.height]
.into_iter()
.all(f64::is_finite)
|| parsed.width == 0.0
|| parsed.height == 0.0
{
return;
};
}
let Some(canvas) = canvas_owner_from_context(scope, context) else {
return;
};
let _ = with_canvas_image_mut(scope, canvas, |image| {
image.pixels.paint_rect(rect, [0.0; 4]);
let transform = canvas_path_state(scope, context).borrow().transform();
let mut rectangle = super::path::Canvas2dPathState::default();
rectangle.rect(parsed.x, parsed.y, parsed.width, parsed.height);
let fragment = PaintFragment::Fill {
fill_rule: moli_layout::PaintFillRule::NonZero,
shape: PaintShape::Path(rectangle.paint_path()),
brush: PaintBrush::Solid(PaintColor::WHITE),
transform,
};
let opaque = !super::settings::Canvas2dSettings::for_context(scope, context).alpha;
super::clipping::draw_with_clip(scope, context, canvas, |image, clip| {
let width = image.pixels.width();
let height = image.pixels.height();
if width == 0 || height == 0 {
return Ok(());
}
let mut snapshot = PaintSnapshot::new(
PaintViewport::new(width, height, 1.0),
PaintColor::new(0.0, 0.0, 0.0, 0.0),
);
snapshot.push_fragment(fragment);
let raster = moli_paint::raster_snapshot(&snapshot)?;
image
.pixels
.clear_coverage(&raster.rgba, clip, opaque)
.expect("validated clear and clipping dimensions");
Ok(())
});
}
@@ -1021,7 +1051,7 @@ pub(crate) fn canvas_context_fill_callback<'s>(
});
if let Some(fragment) = fragment {
let color = context_fill_color(scope, context);
rasterize_canvas_fragment(scope, canvas, fragment, color);
rasterize_canvas_fragment(scope, context, canvas, fragment, color);
}
rv.set_undefined();
}
@@ -1062,7 +1092,7 @@ pub(crate) fn canvas_context_stroke_callback<'s>(
});
if let Some(fragment) = fragment {
let color = context_style_color(scope, context, CANVAS_CONTEXT_STROKE_STYLE_SLOT);
rasterize_canvas_fragment(scope, canvas, fragment, color);
rasterize_canvas_fragment(scope, context, canvas, fragment, color);
}
rv.set_undefined();
}
@@ -1108,7 +1138,13 @@ pub(crate) fn canvas_context_stroke_rect_callback<'s>(
};
let stroke = context_stroke(scope, args.this(), path, transform);
let color = context_style_color(scope, args.this(), CANVAS_CONTEXT_STROKE_STYLE_SLOT);
rasterize_canvas_fragment(scope, canvas, PaintFragment::Stroke(stroke), color);
rasterize_canvas_fragment(
scope,
args.this(),
canvas,
PaintFragment::Stroke(stroke),
color,
);
rv.set_undefined();
}
@@ -1437,15 +1473,16 @@ fn context_line_dash<'s>(
fn rasterize_canvas_fragment<'s>(
scope: &mut v8::PinScope<'s, '_>,
context: v8::Local<'s, v8::Object>,
canvas: v8::Local<'s, v8::Object>,
fragment: PaintFragment,
color: [f32; 4],
) {
let _ = with_canvas_image_mut(scope, canvas, |image| {
super::clipping::draw_with_clip(scope, context, canvas, |image, clip| {
let width = image.pixels.width();
let height = image.pixels.height();
if width == 0 || height == 0 {
return;
return Ok(());
}
let mut snapshot = PaintSnapshot::new(
PaintViewport::new(width, height, 1.0),
@@ -1456,8 +1493,12 @@ fn rasterize_canvas_fragment<'s>(
&& raster.width == width
&& raster.height == height
{
let _ = image.pixels.composite_solid(&raster.rgba, color);
image
.pixels
.composite_solid_with_clip(&raster.rgba, color, clip)
.expect("validated raster and clipping dimensions");
}
Ok(())
});
}
@@ -1500,25 +1541,22 @@ fn draw_canvas_context_text<'s>(
let font = context_string_slot(scope, context, CANVAS_CONTEXT_FONT_SLOT)
.unwrap_or_else(|| DEFAULT_FONT.to_owned());
let color = context_fill_color(scope, context);
let _ = with_canvas_image_mut(scope, canvas, |image| {
super::clipping::draw_with_clip(scope, context, canvas, |image, clip| {
let width = image.pixels.width();
let height = image.pixels.height();
let Some(length) = moli_canvas::byte_len(width, height) else {
return;
return Ok(());
};
let mut coverage = vec![0; length];
draw_text(&mut coverage, width, height, text, x, y, &font, [255; 4]);
let _ = image.pixels.composite_solid(&coverage, color);
image
.pixels
.composite_solid_with_clip(&coverage, color, clip)
.expect("validated text and clipping dimensions");
Ok(())
});
}
pub(crate) fn canvas_context_noop_callback(
_scope: &mut v8::PinScope<'_, '_>,
_args: v8::FunctionCallbackArguments<'_>,
_rv: v8::ReturnValue<'_, v8::Value>,
) {
}
pub(crate) fn canvas_context_set_line_dash_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -1,6 +1,6 @@
use super::{CANVAS_CONTEXT_IMAGE_SMOOTHING_ENABLED_SLOT, context_bool_slot};
use crate::context_bootstrap::canvas::{
backing_store::{canvas_owner_from_context, with_canvas_image_mut},
backing_store::canvas_owner_from_context,
image_source::{CanvasImageSource, image_source_image as source_pixels},
};
use crate::context_bootstrap::image_data::ImageDataPixelFormat;
@@ -193,14 +193,25 @@ fn canvas_context_draw_image_callback<'s>(
ScaleFilter::Nearest
};
let alpha = super::context_global_alpha(scope, context) as f32;
let _ = with_canvas_image_mut(scope, canvas, |target| {
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()) {
target.pixels.draw_image(&image.pixels, blit, filter, alpha);
}
});
crate::context_bootstrap::canvas::clipping::draw_with_clip(
scope,
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())
{
target
.pixels
.draw_image_with_clip(&image.pixels, blit, filter, alpha, clip)
.expect("validated image and clipping dimensions");
}
Ok(())
},
);
}
@@ -67,6 +67,12 @@ fn copy_drawing_slots<'s>(
source: v8::Local<'s, v8::Object>,
destination: v8::Local<'s, v8::Object>,
) {
// The immutable region (including its cached raster mask) is shared by
// saved states. The current path itself is never saved or restored.
let clipping = super::state::canvas_clipping_state(scope, source)
.borrow()
.clone();
*super::state::canvas_clipping_state(scope, destination).borrow_mut() = clipping;
for slot in CANVAS_DRAWING_SLOTS {
let value = get_private_value(scope, source, slot)
.expect("native drawing attributes are initialized together");
@@ -1,26 +1,9 @@
pub(super) use moli_webapi::canvas_context_values::CANVAS_DRAWING_SLOTS;
use super::style_state::CanvasStyleColor;
use crate::native_bridge::{
element::{StyleMode, style_property_value},
node_runtime_and_handle_from_object_or_detached,
};
use crate::webidl;
pub(super) fn canvas_unrestricted_double_arg<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: &v8::FunctionCallbackArguments<'s>,
index: i32,
prefix: &'static str,
) -> Option<f64> {
webidl::argument::<webidl::UnrestrictedDouble>(
scope,
args,
index,
webidl::Context::argument(prefix, (index + 1) as usize),
)
.ok()
.map(f64::from)
}
pub(super) use moli_webapi::canvas_context_values::CANVAS_DRAWING_SLOTS;
pub(super) fn canonical_canvas_color<'s>(
scope: &mut v8::PinScope<'s, '_>,
@@ -48,7 +31,12 @@ pub(super) fn canonical_canvas_color<'s>(
let converted = color.absolute.to_color_space(color_space.stylo());
Some(CanvasStyleColor::new(
color.serialize(),
[converted.components.0, converted.components.1, converted.components.2, converted.alpha],
[
converted.components.0,
converted.components.1,
converted.components.2,
converted.alpha,
],
))
}
@@ -7,6 +7,7 @@ use moli_svg::StrokeQueryLimitExceeded;
mod containment;
mod coordinates;
mod viewport;
use coordinates::{Point, Scalar};
#[derive(Debug)]
@@ -132,6 +133,25 @@ impl Canvas2dPathState {
self.move_to_canvas(source.current.transform(affine));
}
/// Freeze a clip's geometry in bitmap coordinates without modifying either
/// the author's path or the default path's recorded transform.
pub(super) fn transformed_snapshot(&self, transform: LayoutTransform2D) -> Self {
let affine = kurbo::Affine::new(transform.coefficients);
if !affine.is_finite() {
return Self::default();
}
let mut snapshot = self.clone();
snapshot.elements = snapshot
.elements
.into_iter()
.map(|element| element.map(|point| point.transform(affine)))
.collect();
snapshot.current = snapshot.current.transform(affine);
snapshot.current_subpath_start = snapshot.current_subpath_start.transform(affine);
snapshot.transform = LayoutTransform2D::IDENTITY;
snapshot
}
pub(super) fn begin_path(&mut self) {
self.elements.clear();
self.current = Point::ZERO;
@@ -472,6 +492,14 @@ impl Canvas2dPathState {
}
}
pub(super) fn clip_paint_path(
&self,
width: u32,
height: u32,
) -> Result<PaintPath, moli_paint::PaintError> {
viewport::paint_path(self, width, height)
}
pub(super) fn is_empty(&self) -> bool {
self.elements.is_empty()
}
@@ -42,6 +42,10 @@ impl Scalar {
self.significand == 0.0
}
pub(super) fn binary_parts(self) -> (f64, i64) {
(self.significand, self.exponent)
}
#[cfg(test)]
pub(super) fn scaled(self, exponent: i64) -> Self {
if self.is_zero() {
@@ -0,0 +1,305 @@
//! Intersect native fill geometry with the bitmap before narrowing to f32.
//!
//! Paths already inside the bitmap keep the existing curve backend. Outside
//! paths use exact rational subdivision and polygon clipping: subtracting two
//! enormous endpoints must not erase a visible, pixel-sized intersection.
use moli_paint::PaintError;
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::{ToPrimitive, Zero};
use super::coordinates::{Point, Scalar};
use super::{
Canvas2dPathState, LayoutPoint, PaintPath, PaintPathElement, PathElement, path_bounds,
};
const MAX_COORDINATE_BITS: u64 = 16_384;
#[derive(Clone, PartialEq)]
struct ExactPoint {
x: BigRational,
y: BigRational,
}
fn check_point_precision(point: &ExactPoint) -> Result<(), PaintError> {
let bits = [
point.x.numer().bits(),
point.x.denom().bits(),
point.y.numer().bits(),
point.y.denom().bits(),
]
.into_iter()
.max()
.unwrap();
check_precision(bits)
}
fn check_precision(bits: u64) -> Result<(), PaintError> {
if bits > MAX_COORDINATE_BITS {
return Err(PaintError::CanvasClipCoordinateBudgetExceeded {
bits,
max_bits: MAX_COORDINATE_BITS,
});
}
Ok(())
}
fn exact(value: Scalar) -> Result<BigRational, PaintError> {
let (significand, exponent) = value.binary_parts();
if significand == 0.0 {
return Ok(BigRational::zero());
}
check_precision(exponent.unsigned_abs().saturating_add(53))?;
let value = BigRational::from_float(significand).expect("native coordinates are finite");
let scale = BigRational::from_integer(BigInt::from(1) << exponent.unsigned_abs() as usize);
Ok(if exponent >= 0 {
value * scale
} else {
value / scale
})
}
impl ExactPoint {
fn from_native(point: Point) -> Result<Self, PaintError> {
let point = Self {
x: exact(point.x)?,
y: exact(point.y)?,
};
check_point_precision(&point)?;
Ok(point)
}
fn midpoint(&self, other: &Self) -> Result<Self, PaintError> {
let point = Self {
x: (&self.x + &other.x) / BigInt::from(2),
y: (&self.y + &other.y) / BigInt::from(2),
};
check_point_precision(&point)?;
Ok(point)
}
fn paint(&self) -> LayoutPoint {
LayoutPoint::new(
self.x.to_f32().expect("clipped x is finite"),
self.y.to_f32().expect("clipped y is finite"),
)
}
}
#[derive(Clone, Copy)]
enum Axis {
X,
Y,
}
struct Boundary {
axis: Axis,
value: BigRational,
minimum: bool,
}
impl Boundary {
fn coordinate<'a>(&self, point: &'a ExactPoint) -> &'a BigRational {
match self.axis {
Axis::X => &point.x,
Axis::Y => &point.y,
}
}
fn inside(&self, point: &ExactPoint) -> bool {
if self.minimum {
self.coordinate(point) >= &self.value
} else {
self.coordinate(point) <= &self.value
}
}
fn intersection(&self, a: &ExactPoint, b: &ExactPoint) -> Result<ExactPoint, PaintError> {
let (ax, ay, bx, by) = match self.axis {
Axis::X => (&a.x, &a.y, &b.x, &b.y),
Axis::Y => (&a.y, &a.x, &b.y, &b.x),
};
let other = (ay * bx - by * ax + &self.value * (by - ay)) / (bx - ax);
let point = match self.axis {
Axis::X => ExactPoint {
x: self.value.clone(),
y: other,
},
Axis::Y => ExactPoint {
x: other,
y: self.value.clone(),
},
};
check_point_precision(&point)?;
Ok(point)
}
}
fn square(value: &BigRational) -> BigRational {
value * value
}
fn flat(points: &[ExactPoint]) -> bool {
let a = &points[0];
let b = points.last().unwrap();
let dx = &b.x - &a.x;
let dy = &b.y - &a.y;
let length_squared = square(&dx) + square(&dy);
let tolerance_squared = BigRational::new(1.into(), (1024 * 1024).into());
points[1..points.len() - 1].iter().all(|point| {
let x = &point.x - &a.x;
let y = &point.y - &a.y;
if length_squared.is_zero() {
square(&x) + square(&y) <= tolerance_squared
} else {
square(&(&dx * y - &dy * x)) <= &tolerance_squared * &length_squared
}
})
}
fn flatten(
points: Vec<ExactPoint>,
boundaries: &[Boundary; 4],
contour: &mut Vec<ExactPoint>,
) -> Result<(), PaintError> {
let mut stack = vec![points];
while let Some(points) = stack.pop() {
// A curve and its chord are homotopic outside the bitmap whenever the
// entire control hull is outside one clipping half-plane.
if boundaries
.iter()
.any(|edge| points.iter().all(|point| !edge.inside(point)))
|| flat(&points)
{
contour.push(points.last().unwrap().clone());
continue;
}
let count = points.len();
let mut triangle = points;
let mut left = vec![triangle[0].clone()];
let mut right = vec![triangle[count - 1].clone()];
for level in 1..count {
for index in 0..count - level {
triangle[index] = triangle[index].midpoint(&triangle[index + 1])?;
}
left.push(triangle[0].clone());
right.push(triangle[count - 1 - level].clone());
}
right.reverse();
stack.push(right);
stack.push(left);
}
Ok(())
}
fn finish(
contour: &mut Vec<ExactPoint>,
boundaries: &[Boundary; 4],
elements: &mut Vec<PaintPathElement>,
) -> Result<(), PaintError> {
let mut polygon = std::mem::take(contour);
for edge in boundaries {
let mut clipped = Vec::new();
if let Some(mut previous) = polygon.last() {
for point in &polygon {
let inside = edge.inside(point);
if inside != edge.inside(previous) {
clipped.push(edge.intersection(previous, point)?);
}
if inside {
clipped.push(point.clone());
}
previous = point;
}
}
polygon = clipped;
}
if let Some(first) = polygon.first() {
elements.push(PaintPathElement::MoveTo(first.paint()));
elements.extend(
polygon[1..]
.iter()
.map(|point| PaintPathElement::LineTo(point.paint())),
);
elements.push(PaintPathElement::Close);
}
Ok(())
}
pub(super) fn paint_path(
path: &Canvas2dPathState,
width: u32,
height: u32,
) -> Result<PaintPath, PaintError> {
let bounds = [-1.0, f64::from(width) + 1.0, -1.0, f64::from(height) + 1.0];
if path
.elements
.iter()
.flat_map(PathElement::points)
.all(|point| {
point.x >= Scalar::new(bounds[0])
&& point.x <= Scalar::new(bounds[1])
&& point.y >= Scalar::new(bounds[2])
&& point.y <= Scalar::new(bounds[3])
})
{
return Ok(path.paint_path());
}
let boundaries = [
Boundary {
axis: Axis::X,
value: BigRational::from_integer((-1).into()),
minimum: true,
},
Boundary {
axis: Axis::X,
value: BigRational::from_integer((u64::from(width) + 1).into()),
minimum: false,
},
Boundary {
axis: Axis::Y,
value: BigRational::from_integer((-1).into()),
minimum: true,
},
Boundary {
axis: Axis::Y,
value: BigRational::from_integer((u64::from(height) + 1).into()),
minimum: false,
},
];
let mut contour = Vec::new();
let mut elements = Vec::new();
for element in &path.elements {
match *element {
PathElement::MoveTo(point) => {
finish(&mut contour, &boundaries, &mut elements)?;
contour.push(ExactPoint::from_native(point)?);
}
PathElement::LineTo(point) => contour.push(ExactPoint::from_native(point)?),
PathElement::QuadTo(control, end) => {
let points = vec![
contour.last().expect("subpath starts with MoveTo").clone(),
ExactPoint::from_native(control)?,
ExactPoint::from_native(end)?,
];
flatten(points, &boundaries, &mut contour)?;
}
PathElement::CubicTo(a, b, end) => {
let points = vec![
contour.last().expect("subpath starts with MoveTo").clone(),
ExactPoint::from_native(a)?,
ExactPoint::from_native(b)?,
ExactPoint::from_native(end)?,
];
flatten(points, &boundaries, &mut contour)?;
}
PathElement::Close => finish(&mut contour, &boundaries, &mut elements)?,
}
}
finish(&mut contour, &boundaries, &mut elements)?;
Ok(PaintPath {
bounds: path_bounds(&elements),
elements,
})
}
@@ -56,6 +56,8 @@ macro_rules! canvas_draw_path_declaration {
fill: (),
#[webapi(method = "stroke", length = 0, callback = canvas_context_stroke_callback)]
stroke: (),
#[webapi(method = "clip", length = 0, callback = super::clipping::clip_callback)]
clip: (),
#[webapi(method = "isPointInPath", length = 2, callback = is_point_in_path_callback)]
is_point_in_path: (),
#[webapi(method = "isPointInStroke", length = 2, callback = is_point_in_stroke_callback)]
@@ -164,6 +166,42 @@ pub(super) fn fill_arguments<'s>(
Some((None, parsed.rule))
}
#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "CanvasDrawPath.clip")]
struct ClipRuleArgs {
#[webidl(default = CanvasFillRule::Nonzero, converter = "enum")]
rule: CanvasFillRule,
}
#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "CanvasDrawPath.clip")]
struct ClipPathArgs<'s> {
#[webidl(required, interface = web_api_interfaces::Path2D)]
path: v8::Local<'s, v8::Object>,
#[webidl(default = CanvasFillRule::Nonzero, converter = "enum")]
rule: CanvasFillRule,
}
pub(super) fn clip_arguments<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: &v8::FunctionCallbackArguments<'s>,
) -> Option<(Option<v8::Local<'s, v8::Object>>, CanvasFillRule)> {
if args.length() > 1 {
let parsed = webidl::parse_args::<ClipPathArgs>(scope, args)?;
let target = web_api_object_target(scope, parsed.path).expect("validated Path2D argument");
return Some((Some(target), parsed.rule));
}
if let Ok(object) = path_object(
scope,
args.get(0),
webidl::Context::argument("CanvasDrawPath.clip", 1),
) {
return Some((Some(object), CanvasFillRule::Nonzero));
}
let parsed = webidl::parse_args::<ClipRuleArgs>(scope, args)?;
Some((None, parsed.rule))
}
#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "CanvasDrawPath.stroke")]
pub(super) struct StrokeArgs<'s> {
@@ -5,10 +5,10 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use super::path::Canvas2dPathState;
use super::{clipping::CanvasClipRegion, path::Canvas2dPathState};
use crate::util::{get_private_value, set_private_value};
const PATH_STATE_SLOT: &str = "__moliCanvasPathState";
const NATIVE_STATE_SLOT: &str = "__moliCanvasNativeState";
type StateStore = Rc<RefCell<CanvasStates>>;
#[derive(Default)]
@@ -19,13 +19,16 @@ struct CanvasStates {
struct CanvasStateEntry {
_context: v8::Weak<v8::Object>,
state: Rc<RefCell<Canvas2dPathState>>,
state: Rc<RefCell<NativeCanvasState>>,
}
pub(super) fn canvas_path_state<'s>(
fn canvas_native_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
context: v8::Local<'s, v8::Object>,
) -> Rc<RefCell<Canvas2dPathState>> {
) -> Rc<RefCell<NativeCanvasState>> {
// Registered native proxies share the target's private identity. Saved
// drawing-state objects are plain internal objects and keep their own key.
let context = moli_webapi_declare::web_api_object_target(scope, context).unwrap_or(context);
let store = if let Some(store) = scope.get_slot::<StateStore>() {
store.clone()
} else {
@@ -33,7 +36,7 @@ pub(super) fn canvas_path_state<'s>(
scope.set_slot(store.clone());
store
};
if let Some(id) = get_private_value(scope, context, PATH_STATE_SLOT)
if let Some(id) = get_private_value(scope, context, NATIVE_STATE_SLOT)
.and_then(|value| v8::Local::<v8::BigInt>::try_from(value).ok())
.map(|value| value.u64_value().0)
{
@@ -63,7 +66,7 @@ pub(super) fn canvas_path_state<'s>(
}
}),
);
let state = Rc::new(RefCell::new(Canvas2dPathState::default()));
let state = Rc::new(RefCell::new(NativeCanvasState::default()));
store.borrow_mut().entries.insert(
id,
CanvasStateEntry {
@@ -72,16 +75,53 @@ pub(super) fn canvas_path_state<'s>(
},
);
let id = v8::BigInt::new_from_u64(scope, id);
set_private_value(scope, context, PATH_STATE_SLOT, id.into());
set_private_value(scope, context, NATIVE_STATE_SLOT, id.into());
state
}
pub(super) fn reset_canvas_path_state<'s>(
#[derive(Default)]
struct NativeCanvasState {
// Path2D, contexts and saved drawing-state objects share lifetime tracking,
// but allocate only the native components they actually use.
path: Option<Rc<RefCell<Canvas2dPathState>>>,
clipping: Option<Rc<RefCell<CanvasClipRegion>>>,
}
pub(super) fn canvas_path_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
context: v8::Local<'s, v8::Object>,
object: v8::Local<'s, v8::Object>,
) -> Rc<RefCell<Canvas2dPathState>> {
canvas_native_state(scope, object)
.borrow_mut()
.path
.get_or_insert_with(|| Rc::new(RefCell::new(Canvas2dPathState::default())))
.clone()
}
pub(super) fn canvas_clipping_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
) -> Rc<RefCell<CanvasClipRegion>> {
canvas_native_state(scope, object)
.borrow_mut()
.clipping
.get_or_insert_with(|| Rc::new(RefCell::new(CanvasClipRegion::default())))
.clone()
}
pub(super) fn reset_canvas_native_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
) {
if get_private_value(scope, context, PATH_STATE_SLOT).is_some() {
*canvas_path_state(scope, context).borrow_mut() = Canvas2dPathState::default();
if get_private_value(scope, object, NATIVE_STATE_SLOT).is_some() {
let state = canvas_native_state(scope, object);
let state = state.borrow();
if let Some(path) = &state.path {
*path.borrow_mut() = Canvas2dPathState::default();
}
if let Some(clipping) = &state.clipping {
*clipping.borrow_mut() = CanvasClipRegion::default();
}
}
}
@@ -89,6 +129,70 @@ pub(super) fn reset_canvas_path_state<'s>(
mod tests {
use super::*;
#[test]
fn saved_clips_survive_owner_gc_and_native_state_is_reclaimed() {
moli_v8_test_util::ensure_v8();
let mut isolate = v8::Isolate::new(Default::default());
let (context, original, saved) = {
let scope = std::pin::pin!(v8::HandleScope::new(&mut isolate));
let scope = &mut scope.init();
let context = v8::Context::new(scope, Default::default());
let scope = &mut v8::ContextScope::new(scope, context);
let owner = v8::Object::new(scope);
let snapshot = crate::util::new_null_prototype_object(scope);
let region = canvas_clipping_state(scope, owner);
let mut path = Canvas2dPathState::default();
path.rect(0.0, 0.0, 2.0, 4.0);
region
.borrow_mut()
.intersect(path, super::super::path2d::CanvasFillRule::Nonzero);
let captured = region.borrow().clone();
let retained = canvas_clipping_state(scope, snapshot);
*retained.borrow_mut() = captured;
let key = crate::util::v8str(scope, "retainedClip");
context
.global(scope)
.create_data_property(scope, key.into(), snapshot.into())
.unwrap();
(
v8::Global::new(scope, context),
Rc::downgrade(&region),
Rc::downgrade(&retained),
)
};
isolate.low_memory_notification();
assert!(original.upgrade().is_none());
assert!(saved.upgrade().is_some());
{
let scope = std::pin::pin!(v8::HandleScope::new(&mut isolate));
let scope = &mut scope.init();
let local = v8::Local::new(scope, &context);
let scope = &mut v8::ContextScope::new(scope, local);
let key = crate::util::v8str(scope, "retainedClip");
let global = local.global(scope);
let object =
v8::Local::<v8::Object>::try_from(global.get(scope, key.into()).unwrap()).unwrap();
let region = canvas_clipping_state(scope, object);
let mask = region.borrow().mask(4, 4).unwrap().unwrap();
assert_eq!(mask.rgba[3], 255);
assert_eq!(mask.rgba[3 * 4 + 3], 0);
let null = v8::null(scope);
global
.create_data_property(scope, key.into(), null.into())
.unwrap();
}
isolate.low_memory_notification();
assert!(saved.upgrade().is_none());
assert!(
isolate
.get_slot::<StateStore>()
.unwrap()
.borrow()
.entries
.is_empty()
);
}
#[test]
fn path_state_is_reclaimed_with_context_gc_and_isolate_destruction() {
moli_v8_test_util::ensure_v8();
@@ -15,7 +15,6 @@ use super::canvas::{
canvas_context_line_join_setter_callback, canvas_context_line_width_getter_callback,
canvas_context_line_width_setter_callback, canvas_context_measure_text_callback,
canvas_context_miter_limit_getter_callback, canvas_context_miter_limit_setter_callback,
canvas_context_noop_callback,
canvas_context_reset_transform_callback, canvas_context_rotate_callback,
canvas_context_scale_callback, canvas_context_set_line_dash_callback,
canvas_context_set_transform_callback,
@@ -216,9 +215,6 @@ struct CanvasRenderingContext2dTemplateDeclaration {
#[webapi(method = "beginPath", length = 0, callback = canvas_context_begin_path_callback)]
begin_path: (),
#[webapi(method = "clip", length = 0, callback = canvas_context_noop_callback)]
clip: (),
#[webapi(method = "measureText", length = 1, callback = canvas_context_measure_text_callback)]
measure_text: (),
@@ -0,0 +1,107 @@
(owner, callee, kind, label) => {
const make = () => {
const canvas = kind === 'element' ? owner.document.createElement('canvas') : new owner.OffscreenCanvas(8,8);
canvas.width = canvas.height = 8;
return canvas;
};
const canvas = make(), ctx = canvas.getContext('2d');
const iface = kind === 'element' ? 'CanvasRenderingContext2D' : 'OffscreenCanvasRenderingContext2D';
const clipMethod = callee[iface].prototype.clip;
const clip = (...args) => Reflect.apply(clipMethod, ctx, args);
const rows = [];
const check = (name, run) => {
try { rows.push({name: label + ':' + name, passed: run() === true}); }
catch (error) { rows.push({name: label + ':' + name, passed: false, error: String(error)}); }
};
const is = (x,y,color) => Array.from(ctx.getImageData(x,y,1,1).data).join() === color;
const red = '255,0,0,255', green = '0,255,0,255', blue = '0,0,255,255';
const paint = color => { ctx.fillStyle=color; ctx.fillRect(0,0,8,8); };
const fresh = () => { ctx.reset(); paint('red'); };
const rect = (x,y,w,h) => { ctx.beginPath(); ctx.rect(x,y,w,h); };
const throws = run => { try { run(); } catch(error) { return error instanceof callee.TypeError; } return false; };
check('clip method length', () => clipMethod.length === 0);
check('default clip restricts fillRect', () => { fresh(); rect(0,0,4,8); clip(); paint('lime'); return is(1,1,green) && is(6,1,red); });
check('implicit close does not modify the current subpath', () => {
fresh(); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(8,0); ctx.lineTo(8,8); clip();
ctx.lineTo(0,8); const current=ctx.isPointInPath(1,7); paint('lime');
return current && is(6,1,green) && is(1,6,red);
});
check('explicit clip snapshots Path2D without changing the default path', () => {
fresh(); ctx.beginPath(); const p=new owner.Path2D(); p.rect(0,0,4,8); clip(p); p.rect(4,0,4,8); paint('lime');
return is(1,1,green) && is(6,1,red) && !ctx.isPointInPath(1,1);
});
check('successive clips intersect', () => { fresh(); rect(0,0,6,8); clip(); rect(2,0,6,8); clip(); paint('lime'); return is(0,1,red)&&is(3,1,green)&&is(7,1,red); });
check('evenodd double paths cancel', () => { fresh(); rect(0,0,8,8); ctx.rect(0,0,8,8); clip('evenodd'); paint('lime'); return is(3,3,red); });
check('nonzero double paths retain area', () => { fresh(); rect(0,0,8,8); ctx.rect(0,0,8,8); clip('nonzero'); paint('lime'); return is(3,3,green); });
check('evenodd holes survive later nonzero clips', () => {
fresh(); rect(0,0,8,8); ctx.rect(2,2,4,4); clip('evenodd'); rect(0,0,8,8); clip(); paint('lime'); return is(0,0,green)&&is(3,3,red);
});
check('default clip uses the transform frozen when recorded', () => { fresh(); ctx.translate(2,0); rect(0,0,2,8); ctx.resetTransform(); clip(); paint('lime'); return is(2,1,green)&&is(0,1,red)&&is(5,1,red); });
check('explicit clip freezes the CTM at invocation', () => {
fresh(); const p=new owner.Path2D(); p.rect(0,0,2,8); ctx.translate(4,0); clip(p); ctx.resetTransform(); paint('lime'); return is(4,1,green)&&is(0,1,red);
});
check('save and restore retain immutable intersected regions', () => {
fresh(); ctx.save(); rect(0,0,4,8); clip(); ctx.save(); rect(0,0,8,4); clip(); paint('lime');
const nested=is(1,1,green)&&is(1,6,red)&&is(6,1,red);
ctx.restore(); paint('blue'); const restored=is(1,6,blue)&&is(6,1,red);
ctx.restore(); paint('lime'); return nested&&restored&&is(6,1,green);
});
check('restore does not restore the current path', () => {
fresh(); rect(0,0,4,8); ctx.save(); rect(4,0,4,8); ctx.restore(); return !ctx.isPointInPath(1,1)&&ctx.isPointInPath(6,1);
});
check('empty clip blocks fill stroke image text and clearing', () => {
fresh(); ctx.beginPath(); clip(); paint('lime'); ctx.strokeStyle='blue'; ctx.strokeRect(0,0,8,8);
const source=make(); const sc=source.getContext('2d'); sc.fillStyle='blue'; sc.fillRect(0,0,8,8);
ctx.drawImage(source,0,0); ctx.fillText('MMMM',0,7); ctx.clearRect(0,0,8,8);
return Array.from(ctx.getImageData(0,0,8,8).data).every((value,index)=>value===[255,0,0,255][index%4]);
});
check('putImageData ignores the drawing clip', () => {
fresh(); ctx.beginPath(); clip(); const data=new owner.ImageData(new owner.Uint8ClampedArray([0,0,255,255]),1,1); ctx.putImageData(data,3,3); paint('lime'); return is(3,3,blue)&&is(1,1,red);
});
check('reset restores an unbounded clip', () => { fresh(); ctx.beginPath(); clip(); ctx.reset(); paint('lime'); return is(6,6,green); });
check('dimension reset restores an unbounded clip', () => { fresh(); ctx.beginPath(); clip(); canvas.width=8; paint('lime'); return is(6,6,green); });
check('clearRect respects the clipping region', () => { fresh(); rect(0,0,4,8); clip(); ctx.clearRect(0,0,8,8); return is(1,1,'0,0,0,0')&&is(6,1,red); });
check('clearRect applies CTM before clipping', () => { fresh(); rect(2,0,4,8); clip(); ctx.translate(2,0); ctx.clearRect(0,0,2,8); return is(3,1,'0,0,0,0')&&is(1,1,red)&&is(5,1,red); });
check('clearRect ignores color alpha shadows and compositing', () => {
fresh(); rect(0,0,4,8); clip(); ctx.globalAlpha=0; ctx.shadowColor='blue'; ctx.shadowOffsetX=4; ctx.globalCompositeOperation='destination-out'; ctx.clearRect(0,0,8,8); return is(1,1,'0,0,0,0')&&is(6,1,red);
});
check('opaque contexts clear clipped pixels to black', () => {
const canvas=make(), c=canvas.getContext('2d',{alpha:false}); c.fillStyle='red'; c.fillRect(0,0,8,8); c.beginPath(); c.rect(0,0,4,8);
Reflect.apply(clipMethod,c,[]); c.clearRect(0,0,8,8);
return Array.from(c.getImageData(1,1,1,1).data).join()==='0,0,0,255'&&Array.from(c.getImageData(6,1,1,1).data).join()===red;
});
check('drawImage uses the same final clip', () => {
fresh(); rect(0,0,4,8); clip(); const source=make(),c=source.getContext('2d'); c.fillStyle='blue';c.fillRect(0,0,8,8);ctx.drawImage(source,0,0);return is(1,1,blue)&&is(6,1,red);
});
check('stroke uses the same final clip', () => {
fresh(); rect(0,0,4,8); clip(); ctx.beginPath();ctx.moveTo(0,4);ctx.lineTo(8,4);ctx.strokeStyle='blue';ctx.lineWidth=2;ctx.stroke();return is(1,4,blue)&&is(6,4,red);
});
check('text uses the same final clip', () => {
fresh();rect(0,0,4,8);clip();ctx.fillStyle='blue';ctx.fillText('MMMM',0,7);
let inside=false,outside=false;
for(let y=0;y<8;y++)for(let x=0;x<8;x++)if(!is(x,y,red)){if(x<4)inside=true;else outside=true;}
return inside&&!outside;
});
check('enum conversion happens before default geometry snapshot', () => {
fresh();ctx.beginPath();clip({toString(){ctx.rect(0,0,4,8);return 'nonzero';}});paint('lime');return is(1,1,green)&&is(6,1,red);
});
check('enum conversion happens before explicit geometry and CTM snapshot', () => {
fresh();const p=new owner.Path2D();clip(p,{toString(){p.rect(0,0,4,8);ctx.translate(2,0);return 'nonzero';}});ctx.resetTransform();paint('lime');return is(3,1,green)&&is(0,1,red);
});
check('invalid enum leaves the current clip unchanged', () => { fresh();rect(0,0,4,8);clip();const rejected=throws(()=>clip('bad'));paint('lime');return rejected&&is(1,1,green)&&is(6,1,red); });
check('conversion exception keeps its identity and the current clip', () => {
fresh();rect(0,0,4,8);clip();const marker={};let same=false;try{clip({toString(){throw marker;}});}catch(error){same=error===marker;}paint('lime');return same&&is(1,1,green)&&is(6,1,red);
});
check('getImageData ignores the drawing clip', () => { fresh();ctx.beginPath();clip();return is(7,7,red); });
const revoked=owner.Proxy.revocable(ctx,{});revoked.revoke();
const receivers=[null,undefined,{},Object.create(callee[iface].prototype),Object.create(ctx),new owner.Proxy(ctx,{}),revoked.proxy];
for(const [index,receiver] of receivers.entries()) {
check('clip receiver before conversion '+index,()=>{let count=0;return throws(()=>Reflect.apply(clipMethod,receiver,[{toString(){count++;return 'nonzero';}}]))&&count===0;});
check('clearRect receiver before conversion '+index,()=>{let count=0;return throws(()=>Reflect.apply(callee[iface].prototype.clearRect,receiver,[{valueOf(){count++;return 0;}},0,8,8]))&&count===0;});
}
const path=new owner.Path2D(),revokedPath=owner.Proxy.revocable(path,{});revokedPath.revoke();
for(const [index,p] of [null,undefined,{},Object.create(owner.Path2D.prototype),Object.create(path),new owner.Proxy(path,{}),revokedPath.proxy].entries()) {
check('invalid explicit path before enum conversion '+index,()=>{let count=0;return throws(()=>clip(p,{toString(){count++;return 'nonzero';}}))&&count===0;});
}
return rows;
}
@@ -1,5 +1,64 @@
use super::*;
#[test]
fn canvas_clipping_registered_native_proxies_share_identity_and_reject_author_wrappers() {
let mut vm = new_storage_page_task_executor_test_vm("https://canvas-clip-proxy.test/");
vm.eval("globalThis.clipContext=new OffscreenCanvas(8,8).getContext('2d');globalThis.clipPath=new Path2D();clipPath.rect(0,0,4,8);clipContext.fillStyle='red';clipContext.fillRect(0,0,8,8);").unwrap();
let context_ptr = &vm.current_main_world().unwrap().context as *const _;
vm.with_context_scope_by_ptr_and_checkpoint_for_test(context_ptr, |scope, _| {
let global = scope.get_current_context().global(scope);
for (source, destination) in [
("clipContext", "clipContextProxy"),
("clipPath", "clipPathProxy"),
] {
let key = crate::util::v8str(scope, source);
let target =
v8::Local::<v8::Object>::try_from(global.get(scope, key.into()).unwrap()).unwrap();
let handler = crate::util::new_null_prototype_object(scope);
let proxy = v8::Proxy::new(scope, target, handler).unwrap();
moli_webapi_declare::register_web_api_proxy(scope, proxy)?;
let key = crate::util::v8str(scope, destination);
global
.create_data_property(scope, key.into(), proxy.into())
.unwrap();
}
Ok(())
})
.unwrap();
assert_eq!(vm.eval(r#"(() => {
const clip=OffscreenCanvasRenderingContext2D.prototype.clip;
clip.call(clipContextProxy,clipPathProxy);
clipContext.fillStyle='lime';clipContext.fillRect(0,0,8,8);
const pixel=(x)=>Array.from(clipContext.getImageData(x,1,1,1).data).join();
if(pixel(1)!=='0,255,0,255'||pixel(6)!=='255,0,0,255')return false;
OffscreenCanvasRenderingContext2D.prototype.clearRect.call(clipContextProxy,0,0,8,8);
if(pixel(1)!=='0,0,0,0'||pixel(6)!=='255,0,0,255')return false;
let conversions=0,traps=0;
for(const [receiver,path] of [[new Proxy(clipContextProxy,{get(){traps++;throw Error('trap');}}),clipPathProxy],[clipContextProxy,new Proxy(clipPathProxy,{get(){traps++;throw Error('trap');}})]]) {
let rejected=false;try{clip.call(receiver,path,{toString(){conversions++;return 'nonzero';}});}catch(error){rejected=error instanceof TypeError;}
if(!rejected)return false;
}
return conversions===0&&traps===0;
})()"#).unwrap(),"true");
}
#[test]
fn canvas_clipping_snapshots_state_and_applies_to_all_drawing_consumers_across_realms() {
let mut vm = new_storage_page_task_executor_test_vm("https://canvas-clipping.test/");
vm.eval("document.body.innerHTML='<iframe id=other></iframe>'")
.unwrap();
vm.eval(&format!("const clipProbe={};const clipRealms=[globalThis,document.getElementById('other').contentWindow];\
globalThis.clipChecks=[];\
for(let owner=0;owner<2;owner++)for(let callee=0;callee<2;callee++)for(const kind of ['element','offscreen'])\
clipChecks.push(...clipProbe(clipRealms[owner],clipRealms[callee],kind,owner+'-'+callee+'-'+kind));",include_str!("canvas_clipping.js"))).unwrap();
assert_eq!(
vm.eval("JSON.stringify(clipChecks.filter(row=>!row.passed))")
.unwrap(),
"[]"
);
assert_eq!(vm.eval("clipChecks.length").unwrap(), "392");
}
#[test]
fn canvas_path_numeric_range_preserves_finite_geometry_across_realms() {
let mut vm = new_storage_page_task_executor_test_vm("https://canvas-path-range.test/");