canvas: Move current_default_path to script CanvasState (#38114)

This PR moves current path handling from canvas paint thread to script's
`CanvasState`. Because we used manual impl for arcto and some prechecks
for early fail before sending IPC we also needed to fix some bugs in
Path impl.

Testing: Existing WPT tests
work towards #38022
try run: https://github.com/sagudev/servo/actions/runs/16316090028

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
sagudev 2025-07-18 07:45:53 +02:00 committed by GitHub
parent bbed6cddcd
commit a070f24450
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 82 additions and 594 deletions

View file

@ -15,7 +15,6 @@ use fonts::{
ByteIndex, FontBaseline, FontContext, FontGroup, FontMetrics, FontRef, GlyphInfo, GlyphStore,
LAST_RESORT_GLYPH_ADVANCE, ShapingFlags, ShapingOptions,
};
use ipc_channel::ipc::IpcSender;
use log::warn;
use pixels::Snapshot;
use range::Range;
@ -34,180 +33,6 @@ use crate::backend::{
// https://github.com/servo/webrender/blob/main/webrender/src/texture_cache.rs#L1475
const MIN_WR_IMAGE_SIZE: Size2D<u64> = Size2D::new(1, 1);
/// A wrapper around a stored PathBuilder and an optional transformation that should be
/// applied to any points to ensure they are in the matching device space.
pub(crate) struct PathBuilderRef<'a> {
pub(crate) builder: &'a mut Path,
pub(crate) transform: Transform2D<f32>,
}
impl PathBuilderRef<'_> {
/// <https://html.spec.whatwg.org/multipage#dom-context-2d-lineto>
pub(crate) fn line_to(&mut self, pt: &Point2D<f32>) {
let pt = self.transform.transform_point(*pt).cast();
self.builder.line_to(pt.x, pt.y);
}
pub(crate) fn move_to(&mut self, pt: &Point2D<f32>) {
let pt = self.transform.transform_point(*pt).cast();
self.builder.move_to(pt.x, pt.y);
}
pub(crate) fn rect(&mut self, rect: &Rect<f32>) {
let (first, second, third, fourth) = (
Point2D::new(rect.origin.x, rect.origin.y),
Point2D::new(rect.origin.x + rect.size.width, rect.origin.y),
Point2D::new(
rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height,
),
Point2D::new(rect.origin.x, rect.origin.y + rect.size.height),
);
self.move_to(&first);
self.line_to(&second);
self.line_to(&third);
self.line_to(&fourth);
self.close();
self.move_to(&first);
}
/// <https://html.spec.whatwg.org/multipage#dom-context-2d-quadraticcurveto>
pub(crate) fn quadratic_curve_to(&mut self, cp: &Point2D<f32>, endpoint: &Point2D<f32>) {
let cp = self.transform.transform_point(*cp).cast();
let endpoint = self.transform.transform_point(*endpoint).cast();
self.builder
.quadratic_curve_to(cp.x, cp.y, endpoint.x, endpoint.y)
}
/// <https://html.spec.whatwg.org/multipage#dom-context-2d-beziercurveto>
pub(crate) fn bezier_curve_to(
&mut self,
cp1: &Point2D<f32>,
cp2: &Point2D<f32>,
endpoint: &Point2D<f32>,
) {
let cp1 = self.transform.transform_point(*cp1).cast();
let cp2 = self.transform.transform_point(*cp2).cast();
let endpoint = self.transform.transform_point(*endpoint).cast();
self.builder
.bezier_curve_to(cp1.x, cp1.y, cp2.x, cp2.y, endpoint.x, endpoint.y)
}
pub(crate) fn arc(
&mut self,
center: &Point2D<f32>,
radius: f32,
start_angle: f32,
end_angle: f32,
ccw: bool,
) {
let center = self.transform.transform_point(*center).cast();
let _ = self.builder.arc(
center.x,
center.y,
radius as f64,
start_angle as f64,
end_angle as f64,
ccw,
);
}
/// <https://html.spec.whatwg.org/multipage#dom-context-2d-arcto>
pub(crate) fn arc_to(&mut self, cp1: &Point2D<f32>, cp2: &Point2D<f32>, radius: f32) {
let cp0 = if let (Some(inverse), Some(point)) =
(self.transform.inverse(), self.builder.last_point())
{
inverse.transform_point(Point2D::new(point.x, point.y).cast())
} else {
*cp1
};
// 2. Ensure there is a subpath for (x1, y1) is done by one of self.line_to calls
if (cp0.x == cp1.x && cp0.y == cp1.y) || cp1 == cp2 || radius == 0.0 {
self.line_to(cp1);
return;
}
// if all three control points lie on a single straight line,
// connect the first two by a straight line
let direction = (cp2.x - cp1.x) * (cp0.y - cp1.y) + (cp2.y - cp1.y) * (cp1.x - cp0.x);
if direction == 0.0 {
self.line_to(cp1);
return;
}
// otherwise, draw the Arc
let a2 = (cp0.x - cp1.x).powi(2) + (cp0.y - cp1.y).powi(2);
let b2 = (cp1.x - cp2.x).powi(2) + (cp1.y - cp2.y).powi(2);
let d = {
let c2 = (cp0.x - cp2.x).powi(2) + (cp0.y - cp2.y).powi(2);
let cosx = (a2 + b2 - c2) / (2.0 * (a2 * b2).sqrt());
let sinx = (1.0 - cosx.powi(2)).sqrt();
radius / ((1.0 - cosx) / sinx)
};
// first tangent point
let anx = (cp1.x - cp0.x) / a2.sqrt();
let any = (cp1.y - cp0.y) / a2.sqrt();
let tp1 = Point2D::new(cp1.x - anx * d, cp1.y - any * d);
// second tangent point
let bnx = (cp1.x - cp2.x) / b2.sqrt();
let bny = (cp1.y - cp2.y) / b2.sqrt();
let tp2 = Point2D::new(cp1.x - bnx * d, cp1.y - bny * d);
// arc center and angles
let anticlockwise = direction < 0.0;
let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 };
let cy = tp1.y - anx * radius * if anticlockwise { 1.0 } else { -1.0 };
let angle_start = (tp1.y - cy).atan2(tp1.x - cx);
let angle_end = (tp2.y - cy).atan2(tp2.x - cx);
self.line_to(&self.transform.transform_point(tp1));
if [cx, cy, angle_start, angle_end]
.iter()
.all(|x| x.is_finite())
{
self.arc(
&self.transform.transform_point(Point2D::new(cx, cy)),
radius,
angle_start,
angle_end,
anticlockwise,
);
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn ellipse(
&mut self,
center: &Point2D<f32>,
radius_x: f32,
radius_y: f32,
rotation_angle: f32,
start_angle: f32,
end_angle: f32,
ccw: bool,
) {
let center = self.transform.transform_point(*center).cast();
let _ = self.builder.ellipse(
center.x,
center.y,
radius_x as f64,
radius_y as f64,
rotation_angle as f64,
start_angle as f64,
end_angle as f64,
ccw,
);
}
pub(crate) fn close(&mut self) {
self.builder.close_path();
}
}
#[derive(Default)]
struct UnshapedTextRun<'a> {
font: Option<FontRef>,
@ -287,8 +112,6 @@ pub(crate) enum Filter {
pub(crate) struct CanvasData<'a, B: Backend> {
backend: B,
drawtarget: B::DrawTarget,
/// <https://html.spec.whatwg.org/multipage/#current-default-path>
path_state: Path,
state: CanvasPaintState<'a, B>,
saved_states: Vec<CanvasPaintState<'a, B>>,
compositor_api: CrossProcessCompositorApi,
@ -312,7 +135,6 @@ impl<'a, B: Backend> CanvasData<'a, B> {
state: backend.new_paint_state(),
backend,
drawtarget: draw_target,
path_state: Path::new(),
saved_states: vec![],
compositor_api,
image_key,
@ -689,61 +511,18 @@ impl<'a, B: Backend> CanvasData<'a, B> {
}
}
pub(crate) fn begin_path(&mut self) {
// Erase any traces of previous paths that existed before this.
self.path_state = Path::new();
}
pub(crate) fn close_path(&mut self) {
self.path_builder().close();
}
pub(crate) fn fill(&mut self) {
if self.state.fill_style.is_zero_size_gradient() {
return; // Paint nothing if gradient size is zero.
}
let path = self.path_state.clone();
self.maybe_bound_shape_with_pattern(
self.state.fill_style.clone(),
&path.bounding_box(),
|self_| {
self_.drawtarget.fill(
&path,
&self_.state.fill_style,
&self_.state.draw_options.clone(),
);
},
)
}
pub(crate) fn fill_path(&mut self, path: &Path) {
if self.state.fill_style.is_zero_size_gradient() {
return; // Paint nothing if gradient size is zero.
}
self.drawtarget
.fill(path, &self.state.fill_style, &self.state.draw_options);
}
pub(crate) fn stroke(&mut self) {
if self.state.stroke_style.is_zero_size_gradient() {
return; // Paint nothing if gradient size is zero.
}
let path = self.path_state.clone();
self.maybe_bound_shape_with_pattern(
self.state.stroke_style.clone(),
self.state.fill_style.clone(),
&path.bounding_box(),
|self_| {
self_.drawtarget.stroke(
&path,
&self_.state.stroke_style,
&self_.state.stroke_opts,
&self_.state.draw_options,
);
self_
.drawtarget
.fill(path, &self_.state.fill_style, &self_.state.draw_options)
},
)
}
@ -767,98 +546,10 @@ impl<'a, B: Backend> CanvasData<'a, B> {
)
}
pub(crate) fn clip(&mut self) {
let path = &self.path_state;
self.drawtarget.push_clip(path);
}
pub(crate) fn clip_path(&mut self, path: &Path) {
self.drawtarget.push_clip(path);
}
pub(crate) fn is_point_in_path(
&mut self,
x: f64,
y: f64,
fill_rule: FillRule,
chan: IpcSender<bool>,
) {
let mut path = self.path_state.clone();
path.transform(self.get_transform().cast());
chan.send(path.is_point_in_path(x, y, fill_rule)).unwrap();
}
pub(crate) fn move_to(&mut self, point: &Point2D<f32>) {
self.path_builder().move_to(point);
}
pub(crate) fn line_to(&mut self, point: &Point2D<f32>) {
self.path_builder().line_to(point);
}
fn path_builder(&mut self) -> PathBuilderRef {
PathBuilderRef {
builder: &mut self.path_state,
transform: Transform2D::identity(),
}
}
pub(crate) fn rect(&mut self, rect: &Rect<f32>) {
self.path_builder().rect(rect);
}
pub(crate) fn quadratic_curve_to(&mut self, cp: &Point2D<f32>, endpoint: &Point2D<f32>) {
self.path_builder().quadratic_curve_to(cp, endpoint);
}
pub(crate) fn bezier_curve_to(
&mut self,
cp1: &Point2D<f32>,
cp2: &Point2D<f32>,
endpoint: &Point2D<f32>,
) {
self.path_builder().bezier_curve_to(cp1, cp2, endpoint);
}
pub(crate) fn arc(
&mut self,
center: &Point2D<f32>,
radius: f32,
start_angle: f32,
end_angle: f32,
ccw: bool,
) {
self.path_builder()
.arc(center, radius, start_angle, end_angle, ccw);
}
pub(crate) fn arc_to(&mut self, cp1: &Point2D<f32>, cp2: &Point2D<f32>, radius: f32) {
self.path_builder().arc_to(cp1, cp2, radius);
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn ellipse(
&mut self,
center: &Point2D<f32>,
radius_x: f32,
radius_y: f32,
rotation_angle: f32,
start_angle: f32,
end_angle: f32,
ccw: bool,
) {
self.path_builder().ellipse(
center,
radius_x,
radius_y,
rotation_angle,
start_angle,
end_angle,
ccw,
);
}
pub(crate) fn set_fill_style(&mut self, style: FillOrStrokeStyle) {
self.backend
.set_fill_style(style, &mut self.state, &self.drawtarget);
@ -898,12 +589,8 @@ impl<'a, B: Backend> CanvasData<'a, B> {
}
pub(crate) fn set_transform(&mut self, transform: &Transform2D<f32>) {
self.path_state.transform(self.get_transform().cast());
self.state.transform = *transform;
self.drawtarget.set_transform(transform);
if let Some(inverse) = transform.inverse() {
self.path_state.transform(inverse.cast());
}
}
pub(crate) fn set_global_alpha(&mut self, alpha: f32) {
@ -925,9 +612,6 @@ impl<'a, B: Backend> CanvasData<'a, B> {
.backend
.create_drawtarget(Size2D::new(size.width, size.height));
// Step 2. Empty the list of subpaths in context's current default path.
self.path_state = Path::new();
// Step 3. Clear the context's drawing state stack.
self.saved_states.clear();