Skip to content

Truly independent web browser

LibWeb: Record again what read a scale that a transform changed

sideshowbarker350ad13c01cf
diff --git a/Libraries/LibWeb/Rust/src/painting/record/assemble.rs b/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
index 2ede490a583..bb0cfef58d1 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
@@ -62,6 +62,8 @@ pub(crate) struct ProducerOutcome {
pub live: bool,
// Recorded again when the window gains or loses focus.
pub reads_window_focus: bool,
+ // Recorded again when a transform changes a scale that the spaces of the tree accumulate.
+ pub reads_accumulated_scale: bool,
pub blocking_wheel_event_regions: u32,
}
@@ -71,6 +73,7 @@ impl ProducerOutcome {
blocking_wheel_event_regions: self.blocking_wheel_event_regions,
live: self.live,
reads_window_focus: self.reads_window_focus,
+ reads_accumulated_scale: self.reads_accumulated_scale,
}
}
}
@@ -174,11 +177,12 @@ enum SegmentKey {
}
/// Whether the published recording can be returned as it is: nothing changed since, and nothing in it is recorded
-/// every frame or read a window focus that changed.
+/// every frame or read a window focus or an accumulated scale that changed.
pub(crate) fn frame_is_unchanged(
published_root: &RecordedScope,
anything_changed: bool,
window_focus_changed: bool,
+ accumulated_scales_changed: bool,
coverage: Coverage,
) -> bool {
let facts = published_root.facts;
@@ -187,29 +191,43 @@ pub(crate) fn frame_is_unchanged(
Coverage::WholeDocument => !published_root.leaves_out_what_lies_outside_the_window,
Coverage::AnotherWindow => false,
};
- !anything_changed && !facts.live && !(window_focus_changed && facts.reads_window_focus) && covers_the_same
+ !anything_changed
+ && !facts.live
+ && !(window_focus_changed && facts.reads_window_focus)
+ && !(accumulated_scales_changed && facts.reads_accumulated_scale)
+ && covers_the_same
}
pub(crate) struct Assembler<'a, H: AssemblyHost> {
host: &'a mut H,
// The published recording was made in the other window focus state.
window_focus_changed: bool,
+ // A transform changed a scale that the spaces of the tree accumulate since the published recording.
+ accumulated_scales_changed: bool,
coverage: Coverage,
}
impl<'a, H: AssemblyHost> Assembler<'a, H> {
- pub(crate) fn new(host: &'a mut H, window_focus_changed: bool, coverage: Coverage) -> Self {
+ pub(crate) fn new(
+ host: &'a mut H,
+ window_focus_changed: bool,
+ accumulated_scales_changed: bool,
+ coverage: Coverage,
+ ) -> Self {
Self {
host,
window_focus_changed,
+ accumulated_scales_changed,
coverage,
}
}
- // Reusing a part would keep output that is recorded every frame, or that read the window focus before it
- // changed.
+ // Reusing a part would keep output that is recorded every frame, or that read the window focus or an
+ // accumulated scale before it changed.
fn records_again(&self, facts: OutputFacts) -> bool {
- facts.live || (self.window_focus_changed && facts.reads_window_focus)
+ facts.live
+ || (self.window_focus_changed && facts.reads_window_focus)
+ || (self.accumulated_scales_changed && facts.reads_accumulated_scale)
}
/// Records the frame's root scope. `published_root` is the root the published recording walked, when that
@@ -683,6 +701,7 @@ mod tests {
version: u8,
live: bool,
reads_window_focus: bool,
+ reads_accumulated_scale: bool,
blocking: u32,
}
@@ -692,6 +711,7 @@ mod tests {
version: 1,
live: false,
reads_window_focus: false,
+ reads_accumulated_scale: false,
blocking: 0,
}
}
@@ -867,6 +887,7 @@ mod tests {
ProducerOutcome {
live: content.live,
reads_window_focus: content.reads_window_focus,
+ reads_accumulated_scale: content.reads_accumulated_scale,
blocking_wheel_event_regions: content.blocking,
}
}
@@ -928,7 +949,11 @@ mod tests {
}
// Records the canvas prologue and assembles the viewport context from the host's source frame.
- fn record_frame_with_focus_change(host: &mut TestHost, window_focus_changed: bool) -> Frame {
+ fn record_frame_with_frame_changes(
+ host: &mut TestHost,
+ window_focus_changed: bool,
+ accumulated_scales_changed: bool,
+ ) -> Frame {
host.output = new_output();
host.recorded.clear();
host.events.clear();
@@ -950,8 +975,8 @@ mod tests {
(Some(window), Some(Some(published_window))) if window == published_window => Coverage::PublishedWindow,
(Some(_), _) => Coverage::AnotherWindow,
};
- let root =
- Assembler::new(host, window_focus_changed, coverage).assemble_root(context(row(0)), published.as_ref());
+ let root = Assembler::new(host, window_focus_changed, accumulated_scales_changed, coverage)
+ .assemble_root(context(row(0)), published.as_ref());
let mut chunks = vec![Arc::new(prologue)];
root.for_each_chunk(&mut |chunk| chunks.push(Arc::clone(chunk.latest())));
Frame {
@@ -962,7 +987,7 @@ mod tests {
}
fn record_frame(host: &mut TestHost) -> Frame {
- record_frame_with_focus_change(host, false)
+ record_frame_with_frame_changes(host, false, false)
}
// Publishes `frame`: the next frame reuses from it, and asks what changed since.
@@ -1033,8 +1058,20 @@ mod tests {
);
// A chunk for the prologue, one for each of the six runs and one for the run of the walked card.
assert_eq!(frame.display_list.chunks.len(), 1 + 6 + 1);
- assert!(frame_is_unchanged(&frame.root, false, false, Coverage::WholeDocument));
- assert!(!frame_is_unchanged(&frame.root, true, false, Coverage::WholeDocument));
+ assert!(frame_is_unchanged(
+ &frame.root,
+ false,
+ false,
+ false,
+ Coverage::WholeDocument
+ ));
+ assert!(!frame_is_unchanged(
+ &frame.root,
+ true,
+ false,
+ false,
+ Coverage::WholeDocument
+ ));
}
#[test]
@@ -1176,7 +1213,13 @@ mod tests {
.unwrap()
.reads_window_focus = true;
let first = record_frame(&mut host);
- assert!(!frame_is_unchanged(&first.root, false, false, Coverage::WholeDocument));
+ assert!(!frame_is_unchanged(
+ &first.root,
+ false,
+ false,
+ false,
+ Coverage::WholeDocument
+ ));
publish(&mut host, first);
let quiet = record_frame(&mut host);
@@ -1184,11 +1227,45 @@ mod tests {
assert_matches_from_scratch(&mut host, &quiet);
publish(&mut host, quiet);
- let refocused = record_frame_with_focus_change(&mut host, true);
+ let refocused = record_frame_with_frame_changes(&mut host, true, false);
assert_eq!(host.recorded_rows(), vec![1, 2, 5, 6]);
assert_matches_from_scratch(&mut host, &refocused);
}
+ #[test]
+ fn output_that_read_an_accumulated_scale_is_not_copied_once_one_changed() {
+ let mut host = cards_host();
+ host.contents
+ .get_mut(&(row(5), ProducerKind::DrawForeground))
+ .unwrap()
+ .reads_accumulated_scale = true;
+ let first = record_frame(&mut host);
+ assert!(frame_is_unchanged(
+ &first.root,
+ false,
+ false,
+ false,
+ Coverage::WholeDocument
+ ));
+ assert!(!frame_is_unchanged(
+ &first.root,
+ false,
+ false,
+ true,
+ Coverage::WholeDocument
+ ));
+ publish(&mut host, first);
+
+ let quiet = record_frame(&mut host);
+ assert_eq!(host.recorded_rows(), Vec::<u32>::new());
+ assert_matches_from_scratch(&mut host, &quiet);
+ publish(&mut host, quiet);
+
+ let rescaled = record_frame_with_frame_changes(&mut host, false, true);
+ assert_eq!(host.recorded_rows(), vec![5, 6]);
+ assert_matches_from_scratch(&mut host, &rescaled);
+ }
+
#[test]
fn a_stacking_context_that_stops_painting_records_nothing() {
let mut host = cards_host();
@@ -1258,9 +1335,27 @@ mod tests {
let third = record_frame(&mut host);
assert!(host.recorded.is_empty());
assert_eq!(host.bounds_asked, 1);
- assert!(frame_is_unchanged(&third.root, false, false, Coverage::PublishedWindow));
- assert!(!frame_is_unchanged(&third.root, false, false, Coverage::AnotherWindow));
- assert!(!frame_is_unchanged(&third.root, false, false, Coverage::WholeDocument));
+ assert!(frame_is_unchanged(
+ &third.root,
+ false,
+ false,
+ false,
+ Coverage::PublishedWindow
+ ));
+ assert!(!frame_is_unchanged(
+ &third.root,
+ false,
+ false,
+ false,
+ Coverage::AnotherWindow
+ ));
+ assert!(!frame_is_unchanged(
+ &third.root,
+ false,
+ false,
+ false,
+ Coverage::WholeDocument
+ ));
assert_matches_from_scratch(&mut host, &third);
}
@@ -1276,7 +1371,13 @@ mod tests {
assert_eq!(host.rows_recorded().into_iter().collect::<Vec<_>>(), vec![3, 4, 5, 6]);
assert!(host.left_out_scopes().is_empty());
assert!(!second.root.leaves_out_what_lies_outside_the_window);
- assert!(frame_is_unchanged(&second.root, false, false, Coverage::WholeDocument));
+ assert!(frame_is_unchanged(
+ &second.root,
+ false,
+ false,
+ false,
+ Coverage::WholeDocument
+ ));
assert_matches_from_scratch(&mut host, &second);
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index f2ef6a8a84b..7e6da2feff9 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -74,6 +74,9 @@ pub struct RecordingOutput {
pub is_identical_to_published_recording: bool,
// Whether the window was focused, which the output of the producers that read it reflects.
pub(crate) window_is_focused: bool,
+ // A digest of the scale that each space of the tree accumulated, which the output of the producers that read one
+ // reflects. None where no producer read one.
+ pub(crate) accumulated_scales: Option<u64>,
pub(crate) capture_log: Option<trace::CaptureLog>,
/// The SVG images the recording draws, which the host rendered, and the ids of their records.
pub(crate) vector_image_ids_by_request: Vec<(vector_images::VectorImageRenderRequest, u64)>,
@@ -138,6 +141,8 @@ pub struct PaintRecorder<'a, O: Observer> {
live_producer: bool,
// Set while a producer records output that depends on whether the window is focused.
producer_reads_window_focus: std::cell::Cell<bool>,
+ // Set while a producer records output that depends on the scale that a space of the tree accumulates.
+ producer_reads_accumulated_scale: std::cell::Cell<bool>,
// Rows of the layout subtrees whose root moved: every producer of theirs records again.
pub(crate) blocking_wheel_event_region_count: u32,
pub(crate) records_per_box_wheel_hit_test_targets: bool,
@@ -730,6 +735,8 @@ impl<O: Observer> PaintRecorder<'_, O> {
self.svg_resource_walk.is_some_and(|walk| walk.draws_clip_path_geometry)
}
+ /// The scale that the transforms of `spatial` and the spaces above it accumulate to. The producer that asks records
+ /// again when a transform changes such a scale.
pub(crate) fn accumulated_2d_scale_at(&self, spatial: SpatialNodeIndex) -> libgfx_rust::FloatSize {
if self.svg_resource_walk.is_some() {
let transform = self
@@ -741,6 +748,7 @@ impl<O: Observer> PaintRecorder<'_, O> {
height: transform.y_scale(),
};
}
+ self.producer_reads_accumulated_scale.set(true);
self.paint_state.unscrolled_world_transforms().accumulated_2d_scale(
spatial,
crate::painting::visual_context::IncludeVisualViewportTransform::No,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs b/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
index 454881bbc40..a15d0c649c1 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
@@ -47,6 +47,9 @@ pub(crate) struct OutputFacts {
// Recorded again when the window gains or loses focus. A scope reads the window focus when
// any child does.
pub reads_window_focus: bool,
+ // Recorded again when a transform changes a scale that the spaces of the tree accumulate. A scope reads the
+ // accumulated scale when any child does.
+ pub reads_accumulated_scale: bool,
}
impl OutputFacts {
@@ -54,6 +57,7 @@ impl OutputFacts {
self.blocking_wheel_event_regions += other.blocking_wheel_event_regions;
self.live |= other.live;
self.reads_window_focus |= other.reads_window_focus;
+ self.reads_accumulated_scale |= other.reads_accumulated_scale;
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/producers.rs b/Libraries/LibWeb/Rust/src/painting/record/producers.rs
index f48b3d9ec3d..f0bb076f061 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/producers.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/producers.rs
@@ -187,6 +187,7 @@ impl<O: Observer> AssemblyHost for PaintRecorder<'_, O> {
}
self.live_producer = false;
self.producer_reads_window_focus.set(false);
+ self.producer_reads_accumulated_scale.set(false);
self.recorder
.begin_record_origin(owner, super::trace::producer_name(kind));
let start = self.recorder.byte_size();
@@ -232,6 +233,7 @@ impl<O: Observer> AssemblyHost for PaintRecorder<'_, O> {
ProducerOutcome {
live: self.live_producer,
reads_window_focus: self.producer_reads_window_focus.get(),
+ reads_accumulated_scale: self.producer_reads_accumulated_scale.get(),
blocking_wheel_event_regions: self.blocking_wheel_event_region_count - blocking_wheel_event_regions_before,
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
index 47ae6372cc1..e9204b97864 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
@@ -85,6 +85,12 @@ fn record_display_list_impl<O: Observer>(
let window_focus_changed = source_recording
.as_ref()
.is_some_and(|frame| frame.window_is_focused != inputs.window_is_focused);
+ // Only output that read an accumulated scale depends on one, so only a published recording that holds such
+ // output is asked which scales it read.
+ let accumulated_scales_changed = source_recording
+ .as_ref()
+ .and_then(|frame| frame.accumulated_scales)
+ .is_some_and(|published_scales| accumulated_scales_of(paint_state) != Some(published_scales));
let force_dark_settings = inputs.force_dark_enabled.then_some(inputs.force_dark_settings);
// A recording of a frame without a visual context tree holds all of it.
let window = window.filter(|_| paint_state.visual_context_tree.is_some());
@@ -125,6 +131,7 @@ fn record_display_list_impl<O: Observer>(
run_draws_vector_image_placeholders: false,
live_producer: false,
producer_reads_window_focus: std::cell::Cell::new(false),
+ producer_reads_accumulated_scale: std::cell::Cell::new(false),
line_root_positions: None,
blocking_wheel_event_region_count: 0,
records_per_box_wheel_hit_test_targets: frame_inputs.records_per_box_wheel_hit_test_targets(),
@@ -160,8 +167,13 @@ fn record_display_list_impl<O: Observer>(
let anything_changed = source
.paint_change_stamps(viewport)
.row_or_anything_below_it_changed_since(frame.recorded_as_of);
- frame_is_unchanged(root, anything_changed, window_focus_changed, coverage)
- && !has_inspector_overlays
+ frame_is_unchanged(
+ root,
+ anything_changed,
+ window_focus_changed,
+ accumulated_scales_changed,
+ coverage,
+ ) && !has_inspector_overlays
&& frame.prologue_id == prologue_id
})
.map(|(frame, root)| {
@@ -188,8 +200,13 @@ fn record_display_list_impl<O: Observer>(
(chunks, display_list, None)
}
None => {
- let recorded_root = Assembler::new(&mut recorder, window_focus_changed, coverage)
- .assemble_root(root_scope, published_root.as_ref());
+ let recorded_root = Assembler::new(
+ &mut recorder,
+ window_focus_changed,
+ accumulated_scales_changed,
+ coverage,
+ )
+ .assemble_root(root_scope, published_root.as_ref());
let mut chunks: Vec<Arc<RecordedChunk>> = prologue.into_iter().collect();
recorded_root.for_each_chunk(&mut |chunk| chunks.push(Arc::clone(chunk)));
if has_inspector_overlays {
@@ -213,6 +230,10 @@ fn record_display_list_impl<O: Observer>(
.recorder
.notes_record_origins()
.then(|| Arc::new(record_notes_of_tape(&chunks, &mut recorder.record_notes_of_chunks)));
+ let accumulated_scales = recorded_root
+ .as_ref()
+ .filter(|root| root.facts.reads_accumulated_scale)
+ .and_then(|_| accumulated_scales_of(paint_state));
let output = RecordingOutput {
recorded_structural_epoch: structural_epoch,
recorded_as_of: source.frame().paint_generation(),
@@ -230,6 +251,7 @@ fn record_display_list_impl<O: Observer>(
wheel_event_listener_state_generation: inputs.wheel_event_listener_state_generation,
is_identical_to_published_recording: false,
window_is_focused: inputs.window_is_focused,
+ accumulated_scales,
capture_log: recorder.observer.finish(),
vector_image_ids_by_request: Vec::new(),
resources: Default::default(),
@@ -370,6 +392,23 @@ fn without_vector_image_ids(bytes: &[u8]) -> Vec<u8> {
bytes
}
+/// A digest of the scale that each space of the tree accumulates, or none without a tree.
+fn accumulated_scales_of(paint_state: &crate::painting::published_frame::PublishedPaintState) -> Option<u64> {
+ use crate::painting::display_list::commands::SpatialNodeIndex;
+ use crate::painting::visual_context::IncludeVisualViewportTransform;
+ use std::hash::Hasher;
+ let tree = paint_state.visual_context_tree.as_deref()?;
+ let world_transforms = paint_state.unscrolled_world_transforms();
+ let mut digest = crate::fast_hash::fast_hasher();
+ for index in 0..tree.spatial_nodes.len() {
+ let scale =
+ world_transforms.accumulated_2d_scale(SpatialNodeIndex(index as u32), IncludeVisualViewportTransform::No);
+ digest.write_u32(scale.width.to_bits());
+ digest.write_u32(scale.height.to_bits());
+ }
+ Some(digest.finish())
+}
+
fn reused_output_is_verified() -> bool {
static VERIFIED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*VERIFIED.get_or_init(|| cfg!(debug_assertions) || std::env::var_os("LIBWEB_VERIFY_PAINT_REUSE").is_some())
diff --git a/Tests/LibWeb/Ref/expected/svg-image-under-a-scale-that-changes-twice-ref.html b/Tests/LibWeb/Ref/expected/svg-image-under-a-scale-that-changes-twice-ref.html
new file mode 100644
index 00000000000..83882d292ee
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg-image-under-a-scale-that-changes-twice-ref.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<style>
+body { margin: 0; }
+#box { position: absolute; left: 8px; top: 8px; width: 48px; height: 24px; transform-origin: top left; }
+img, #background { position: absolute; top: 0; width: 24px; height: 24px; }
+#background { left: 24px; background: 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'/></svg>") no-repeat; }
+#tiles { position: absolute; left: 0; top: 24px; width: 48px; height: 24px; background: 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'/></svg>") 0 0 / 24px 24px; }
+#other { position: absolute; left: 300px; top: 8px; width: 20px; height: 20px; background: green; }
+#box { transform: scale(3); }
+#other { background: blue; }
+</style>
+<div id="box"><img src="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'/></svg>"><div id="background"></div><div id="tiles"></div></div>
+<div id="other"></div>
diff --git a/Tests/LibWeb/Ref/input/svg-image-under-a-scale-that-changes-twice.html b/Tests/LibWeb/Ref/input/svg-image-under-a-scale-that-changes-twice.html
new file mode 100644
index 00000000000..b9a2e83ca0c
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg-image-under-a-scale-that-changes-twice.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/svg-image-under-a-scale-that-changes-twice-ref.html">
+<style>
+body { margin: 0; }
+#box { position: absolute; left: 8px; top: 8px; width: 48px; height: 24px; transform-origin: top left; }
+img, #background { position: absolute; top: 0; width: 24px; height: 24px; }
+#background { left: 24px; background: 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'/></svg>") no-repeat; }
+#tiles { position: absolute; left: 0; top: 24px; width: 48px; height: 24px; background: 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'/></svg>") 0 0 / 24px 24px; }
+#other { position: absolute; left: 300px; top: 8px; width: 20px; height: 20px; background: green; }
+#box { transform: scale(2); }
+</style>
+<div id="box"><img src="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'/></svg>"><div id="background"></div><div id="tiles"></div></div>
+<div id="other"></div>
+<script>
+const frame = () => new Promise(resolve => requestAnimationFrame(resolve));
+(async () => {
+ await frame();
+ await frame();
+ box.style.transform = "scale(4)";
+ await frame();
+ await frame();
+ // Paint something else, so that the next change of scale meets a recording of the image that can be reused.
+ other.style.background = "blue";
+ await frame();
+ await frame();
+ box.style.transform = "scale(3)";
+ await frame();
+ await frame();
+ document.documentElement.classList.remove("reftest-wait");
+})();
+</script>
+</html>