Address CodeRabbit review: cost pattern filters at resvg's scale-only tile transform

resvg renders a pattern's root under Transform::from_scale(get_scale()) of
the path transform times patternTransform, so a skewed patternTransform
with slim full-affine bounds drew a much larger filter region than the
estimate charged (1.06e9 vs about 1.9e11 reads).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-29 17:10:41 +01:00
1 parent 3d657fb347
commit cd01064dc2
2 files changed
+25 -2

No files matched your search

+5 -2
View File
@@ -669,11 +669,14 @@ fn filter_cost(root: &usvg::Group, base: tiny_skia::Transform, rec: usize, visit
let world = base.pre_concat(node.abs_transform());
let mut sub = |root: &usvg::Group, ts: tiny_skia::Transform| cost = cost.saturating_add(filter_cost(root, ts, rec + 1, visits));
match node {
// resvg draws a pattern's tile at the path's scale times the pattern's own.
// resvg draws a pattern's tile at the path's scale times the pattern's own, and
// only that scale (`get_scale`, no skew or rotation): a skewed patternTransform
// whose full-affine bounds are slim still renders a large tile.
usvg::Node::Path(p) => {
for paint in [p.fill().map(|f| f.paint()), p.stroke().map(|s| s.paint())].into_iter().flatten() {
if let usvg::Paint::Pattern(pt) = paint {
sub(pt.root(), world.pre_concat(pt.transform()));
let (sx, sy) = world.pre_concat(pt.transform()).get_scale();
sub(pt.root(), tiny_skia::Transform::from_scale(sx, sy));
}
}
}
+20
View File
@@ -26,6 +26,26 @@ fn ordinary_filters_cost_nothing_on_any_canvas() {
assert!(check_filter_cost(&tree, 1.0).is_ok());
}
/// resvg draws a pattern's tile under the scale part of its transform only (`get_scale`): a
/// skewed patternTransform (determinant 1) whose full-affine bounds of a slim filter region are
/// 180 × 181 px still draws that region at 2400 × 180 px, with a morphology window as large,
/// about 2 × 10^11 reads. Never rendered here: only the estimate is checked.
#[test]
fn a_skewed_pattern_filter_counts_at_resvgs_tile_scale() {
let svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"100\" height=\"100\">\
<filter id=\"f\"><feMorphology radius=\"1000\"/></filter>\
<pattern id=\"p\" width=\"20\" height=\"20\" patternUnits=\"userSpaceOnUse\" patternTransform=\"matrix(0 -0.01 100 100 0 0)\">\
<rect width=\"20\" height=\"1.5\" filter=\"url(#f)\"/></pattern>\
<rect width=\"100\" height=\"100\" fill=\"url(#p)\"/></svg>";
let tree = parse(svg.as_bytes()).unwrap().tree;
// The tile resvg would allocate: 20 × 20 units at get_scale ≈ (100, 100).
let (sx, sy) = tiny_skia::Transform::from_row(0.0, -0.01, 100.0, 100.0, 0.0, 0.0).get_scale();
assert!((sx * 20.0).round() >= 2000.0 && (sy * 20.0).round() >= 1999.0, "{sx} {sy}");
let cost = filter_cost(tree.root(), tiny_skia::Transform::identity(), 0, &mut { MAX_FILTER_VISITS });
assert!(cost > 50 * MAX_FILTER_COST, "charged only {cost} reads");
assert!(check_filter_cost(&tree, 1.0).is_err());
}
#[test]
fn quadratics_become_cubics_and_closing_merges_the_start_knot() {
let mut pb = tiny_skia::PathBuilder::new();