Skip to content

Truly independent web browser

LibWeb: Record SVG background tiles at their transformed size

sideshowbarker6cb16937962c
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
index 8b3ea9e1432..aa75de7a4f0 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
@@ -883,6 +883,9 @@ fn paint_image_layer_in_current_space<O: Observer>(
}
}
if let crate::painting::image_content::ImageContent::Vector { content_identity, .. } = &facts.content {
+ use crate::painting::record::vector_images::{
+ VectorImageRenderGeometry, VectorImageRenderRequest, vector_image_render_geometry,
+ };
if clip_rect.is_empty() {
return;
}
@@ -891,21 +894,6 @@ fn paint_image_layer_in_current_space<O: Observer>(
(dest_rect.width, dest_rect.height),
(dest_rect.width, dest_rect.height),
);
- let display_list_id = recorder.vector_image_placeholder(
- crate::painting::record::vector_images::VectorImageRenderRequest::new(
- vector_image_source,
- CssPixels::from_integer(i64::from(dest_rect.width)),
- CssPixels::from_integer(i64::from(dest_rect.height)),
- 1.0,
- *content_identity,
- recorder.vector_image_color_scheme(vector_image_source.owner()),
- recorder.vector_image_force_dark(),
- ),
- );
- let tile_size = IntSize {
- width: dest_rect.width,
- height: dest_rect.height,
- };
let tree = recorder.paint_state.visual_context_tree.as_ref().unwrap();
let mut spatial = recorder.recorder.accumulated_visual_context().spatial;
let mut is_untransformed = recorder.recorder.ambient_inline_transform().is_none();
@@ -914,6 +902,53 @@ fn paint_image_layer_in_current_space<O: Observer>(
is_untransformed = !matches!(node.data, SpatialData::Transform(_) | SpatialData::Perspective(_));
spatial = node.parent;
}
+ let destination_size = IntSize {
+ width: dest_rect.width,
+ height: dest_rect.height,
+ };
+ // The tile is rasterized before its transforms apply, so it is recorded at the scale they accumulate to,
+ // the way other vector images are, but never smaller than its destination. Otherwise, a transform that
+ // enlarges the tile enlarges the pixels of its raster. Nearest-neighbor scaling shows the pixels of a tile
+ // that has the size of its destination.
+ let scaled_tile_size = if is_untransformed || scaling_mode == ScalingMode::NearestNeighbor {
+ None
+ } else {
+ scaled_size_of_repeated_vector_tile(
+ destination_size,
+ recorder.accumulated_2d_scale_at(recorder.recorder.accumulated_visual_context().spatial),
+ )
+ };
+ // The image is rendered at one scale in both axes, in a viewport of the size of the destination, since a
+ // viewport of other proportions can change how the image is laid out.
+ let geometry = match scaled_tile_size {
+ Some(tile_size) => {
+ let scale = tile_size.width as f32 / dest_rect.width as f32;
+ vector_image_render_geometry(
+ FloatRect::new(0.0, 0.0, dest_rect.width as f32, dest_rect.height as f32),
+ FloatSize {
+ width: scale,
+ height: scale,
+ },
+ false,
+ )
+ }
+ None => VectorImageRenderGeometry {
+ css_width: CssPixels::from_integer(i64::from(dest_rect.width)),
+ css_height: CssPixels::from_integer(i64::from(dest_rect.height)),
+ raster_scale: 1.0,
+ list_size: destination_size,
+ },
+ };
+ let tile_size = scaled_tile_size.unwrap_or(destination_size);
+ let display_list_id = recorder.vector_image_placeholder(VectorImageRenderRequest::new(
+ vector_image_source,
+ geometry.css_width,
+ geometry.css_height,
+ geometry.raster_scale,
+ *content_identity,
+ recorder.vector_image_color_scheme(vector_image_source.owner()),
+ recorder.vector_image_force_dark(),
+ ));
// OPTIMIZATION: Paint a single untransformed SVG tile directly to keep filters clipped to the background.
// Rasterize other tiles to preserve placement, subpixel opacity and seamless boundaries.
if inline_operator == CompositingAndBlendingOperator::Normal && tile_count == 1.0 && is_untransformed {
@@ -925,8 +960,13 @@ fn paint_image_layer_in_current_space<O: Observer>(
let group = recorder.recorder.begin_repeated_tile();
recorder.recorder.paint_vector_image_records(
display_list_id,
- FloatRect::new(0.0, 0.0, dest_rect.width as f32, dest_rect.height as f32),
- tile_size,
+ FloatRect::new(
+ 0.0,
+ 0.0,
+ geometry.list_size.width as f32,
+ geometry.list_size.height as f32,
+ ),
+ geometry.list_size,
);
recorder.recorder.finish_repeated_tile(
group,
@@ -1119,6 +1159,40 @@ fn fraction_to_int(numerator: CssPixels, denominator: CssPixels) -> i32 {
numerator.div_as_fraction(denominator).to_int()
}
+/// The most device pixels that the tile of a repeated vector image is recorded with, where its transforms enlarge it,
+/// and the most along one side.
+const MAXIMUM_SCALED_VECTOR_TILE_AREA: f32 = 2048.0 * 2048.0;
+const MAXIMUM_SCALED_VECTOR_TILE_DIMENSION: f32 = 8192.0;
+
+/// The size to record the tile of a repeated vector image at, for the scale that its transforms accumulate to. The tile
+/// is scaled by the larger of the two axes, as far as the limits on its size allow. And a tile scaled to a fraction of
+/// a pixel would be cut off there, or left partly covered, so the scale is lowered to the nearest one that gives the
+/// tile a whole number of pixels on each side. None where that leaves the tile at the size of its destination, which it
+/// is never recorded smaller than.
+fn scaled_size_of_repeated_vector_tile(destination_size: IntSize, accumulated_scale: FloatSize) -> Option<IntSize> {
+ let width = destination_size.width as f32;
+ let height = destination_size.height as f32;
+ let scale = accumulated_scale.width.max(accumulated_scale.height);
+ if !scale.is_finite() {
+ return None;
+ }
+ let scale = scale
+ .min((MAXIMUM_SCALED_VECTOR_TILE_AREA / (width * height)).sqrt())
+ .min(MAXIMUM_SCALED_VECTOR_TILE_DIMENSION / width.max(height));
+ let mut divisor = destination_size.width;
+ let mut remainder = destination_size.height;
+ while remainder != 0 {
+ (divisor, remainder) = (remainder, divisor % remainder);
+ }
+ // A scale within 1/64 of a step below one counts as that step, since a scale read back from a matrix can fall
+ // just short of the scale that the matrix was made with.
+ let steps = (scale * divisor as f32 + 1.0 / 64.0) as i32;
+ (steps > divisor).then(|| IntSize {
+ width: destination_size.width / divisor * steps,
+ height: destination_size.height / divisor * steps,
+ })
+}
+
/// Moves a tile start by whole steps to the last position at or before the clip start. Integer math keeps this exact: a
/// fixed point quotient truncates a gap below 1/64 of a step to zero steps.
fn first_tile_start(tile_start: i32, clip_start: i32, step: i32) -> i32 {
@@ -1276,3 +1350,54 @@ fn text_clip_fragments<O: Observer>(
}
fragments
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn tile_size(width: i32, height: i32, scale_x: f32, scale_y: f32) -> Option<(i32, i32)> {
+ scaled_size_of_repeated_vector_tile(
+ IntSize { width, height },
+ FloatSize {
+ width: scale_x,
+ height: scale_y,
+ },
+ )
+ .map(|size| (size.width, size.height))
+ }
+
+ #[test]
+ fn repeated_vector_tile_is_recorded_at_the_larger_scale_of_its_transforms() {
+ assert_eq!(tile_size(24, 24, 4.0, 4.0), Some((96, 96)));
+ assert_eq!(tile_size(24, 24, 2.0, 3.0), Some((72, 72)));
+ assert_eq!(tile_size(24, 24, 2.0, 0.5), Some((48, 48)));
+ assert_eq!(tile_size(24, 12, 1.5, 1.5), Some((36, 18)));
+
+ assert_eq!(tile_size(24, 24, 1.0, 1.0), None);
+ assert_eq!(tile_size(24, 24, 0.5, 0.75), None);
+ assert_eq!(tile_size(24, 24, f32::NAN, f32::NAN), None);
+ assert_eq!(tile_size(24, 24, f32::INFINITY, 1.0), None);
+ }
+
+ #[test]
+ fn repeated_vector_tile_is_recorded_at_a_whole_number_of_pixels() {
+ assert_eq!(tile_size(24, 12, 3.7, 3.7), Some((88, 44)));
+ assert_eq!(tile_size(24, 24, 4.3, 4.3), Some((103, 103)));
+ assert_eq!(tile_size(24, 1, 1.5, 1.5), None);
+ assert_eq!(tile_size(24, 1, 2.5, 2.5), Some((48, 2)));
+ assert_eq!(tile_size(24, 24, 1.03, 1.0), None);
+ // A scale just short of a whole size gives that size.
+ assert_eq!(tile_size(24, 24, 3.9999998, 3.9999998), Some((96, 96)));
+ }
+
+ #[test]
+ fn repeated_vector_tile_stays_within_its_limits() {
+ assert_eq!(tile_size(1024, 1024, 4.0, 4.0), Some((2048, 2048)));
+ assert_eq!(tile_size(1024, 1024, 1.0, 100.0), Some((2048, 2048)));
+ assert_eq!(tile_size(4096, 16, 4.0, 4.0), Some((8192, 32)));
+ assert_eq!(tile_size(16, 4096, 4.0, 4.0), Some((32, 8192)));
+
+ assert_eq!(tile_size(4096, 4096, 4.0, 4.0), None);
+ assert_eq!(tile_size(10000, 16, 4.0, 4.0), None);
+ }
+}
diff --git a/Tests/LibWeb/Ref/expected/scaled-animated-svg-background-ref.html b/Tests/LibWeb/Ref/expected/scaled-animated-svg-background-ref.html
new file mode 100644
index 00000000000..6ba5af3c30f
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/scaled-animated-svg-background-ref.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+body { margin: 0; }
+.box { position: absolute; left: 8px; width: 96px; height: 96px; overflow: hidden; }
+.strip { width: 192px; height: 100%; background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' preserveAspectRatio='none'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>"); background-size: 96px 96px; background-repeat: repeat-x; }
+.first { top: 8px; }
+.second { top: 112px; width: 48px; height: 48px; }
+.third { left: 112px; top: 8px; }
+.fourth { left: 112px; top: 112px; }
+.first .strip { background-position: -8px 0; }
+.second .strip { width: 96px; background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>"); background-size: 48px 24px; background-position: -4px 0; }
+.fourth .strip { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='96' height='96'><circle cx='48' cy='48' r='40' fill='none' stroke='blue' stroke-width='4'/></svg>"); background-size: auto; }
+</style>
+<div class="box first"><div class="strip"></div></div>
+<div class="box second"><div class="strip"></div></div>
+<div class="box third"><div class="strip"></div></div>
+<div class="box fourth"><div class="strip"></div></div>
+<script>
+requestAnimationFrame(() => requestAnimationFrame(() => requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+})));
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/scaled-animated-svg-background.html b/Tests/LibWeb/Ref/input/scaled-animated-svg-background.html
new file mode 100644
index 00000000000..26a55e9bbd4
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/scaled-animated-svg-background.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/scaled-animated-svg-background-ref.html">
+<style>
+body { margin: 0; }
+.box { position: absolute; left: 8px; width: 24px; height: 24px; overflow: hidden; transform-origin: top left; }
+.strip { width: 48px; height: 100%; background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' preserveAspectRatio='none'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>"); background-size: 24px 24px; background-repeat: repeat-x; }
+@keyframes slide { from, to { transform: translate3d(-2px, 0, 0); } }
+.first { top: 8px; transform: scale(4); }
+.second { top: 112px; transform: scale(2); }
+.third { left: 112px; top: 8px; transform: scale(4); }
+.fourth { left: 112px; top: 112px; transform: scale(4); }
+.first .strip, .second .strip { animation: slide 1s linear infinite; }
+.second .strip { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>"); background-size: 24px 12px; }
+.fourth .strip { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>"); background-size: auto; }
+</style>
+<div class="box first"><div class="strip"></div></div>
+<div class="box second"><div class="strip"></div></div>
+<div class="box third"><div class="strip"></div></div>
+<div class="box fourth"><div class="strip"></div></div>
+<script>
+requestAnimationFrame(() => requestAnimationFrame(() => requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+})));
+</script>
+</html>