Skip to content

Truly independent web browser

LibWeb: Record a clock tick’s frame where its panes are scrolled to

sideshowbarkera62e2b1ead00
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 183c9b8445b..118e1baebc0 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -72,6 +72,7 @@
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/NetworkPartitionKey.h>
#include <LibWeb/Geolocation/Geolocation.h>
+#include <LibWeb/Geometry/DOMPointReadOnly.h>
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/HTML/AnimatedBitmapDecodedImageData.h>
#include <LibWeb/HTML/AutoplaySettings.h>
@@ -3075,6 +3076,14 @@ GC::Ptr<Geometry::DOMRect> Internals::presented_border_box(DOM::Element& element
return Geometry::DOMRect::create(rect.x().to_double(), rect.y().to_double(), rect.width().to_double(), rect.height().to_double());
}
+GC::Ptr<Geometry::DOMPointReadOnly> Internals::presented_scroll_offset(DOM::Element& scroller)
+{
+ CSSPixelPoint offset;
+ if (!scroller.style_node_id() || !Layout::RustFFI::document_host_presented_scroll_offset(scroller.document().layout_node_arena().host(), scroller.style_node_id().value(), &offset))
+ return nullptr;
+ return Geometry::DOMPointReadOnly::create(offset.x().to_double(), offset.y().to_double());
+}
+
Optional<Vector<String>> Internals::presented_compositor_animations(DOM::Element& element)
{
u32 properties = 0;
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index ababdb1c966..f7cf6a7e6db 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -324,6 +324,7 @@ public:
Utf16String presented_selection();
Utf16String painted_selection();
GC::Ptr<Geometry::DOMRect> presented_border_box(DOM::Element&);
+ GC::Ptr<Geometry::DOMPointReadOnly> presented_scroll_offset(DOM::Element&);
Optional<Vector<String>> presented_compositor_animations(DOM::Element&);
Optional<double> presented_opacity(DOM::Element&);
Optional<String> presented_color(DOM::Element&, Optional<Utf16String> const& pseudo_element);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 4cec5cffb99..874f01b8796 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -404,6 +404,9 @@ interface Internals {
DOMString paintedSelection();
// The border box of the element in the last frame a clock tick presented.
DOMRect? presentedBorderBox(Element element);
+ // The scroll position in CSS pixels the last frame a clock tick presented was recorded with, or null if the frame
+ // had no scroll node for the element.
+ DOMPointReadOnly? presentedScrollOffset(Element scroller);
// The properties compositor animations drove on the box of the element in the last frame a clock tick presented, or
// null where the frame showed no border box of it.
sequence<DOMString>? presentedCompositorAnimations(Element element);
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_passes.rs b/Libraries/LibWeb/Rust/src/painting/paint_passes.rs
index e094e392c74..4da0a83a723 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_passes.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_passes.rs
@@ -78,11 +78,13 @@ pub(crate) struct RenderingPreparation {
/// resource, a visual context tree built anew, or a compositor animation of a box the round built again.
pub(crate) struct VisualContextsNeedHost(pub(crate) &'static str);
-/// The visual context tree a clock tick's frame takes to the compositor, with the scroll offsets of its nodes where its
-/// structure is not the one the compositor has.
+/// The visual context tree a clock tick's frame takes to the compositor, and the scroll offsets of its nodes where the
+/// tick updated it, which the frame was recorded with.
pub(crate) struct ClockTickVisualContexts {
pub(crate) tree: Arc<VisualContextTree>,
- pub(crate) restructured_scroll_offsets: Option<Vec<FloatPoint>>,
+ pub(crate) scroll_offsets: Option<Vec<FloatPoint>>,
+ /// Whether the tree's structure is not the one the compositor has.
+ pub(crate) restructured: bool,
}
/// Brings the paint state of `arena` up to date with what a clock tick's round laid out, for the tick's frame, as the
@@ -133,7 +135,8 @@ pub(crate) fn prepare_for_clock_tick(
state.published_compositor_animations = tree.visual_animations().to_vec();
return Ok(Some(ClockTickVisualContexts {
tree: state.tree.clone().expect("the tree is there"),
- restructured_scroll_offsets: None,
+ scroll_offsets: None,
+ restructured: false,
}));
}
(inputs, tree.shared_visual_animations())
@@ -156,11 +159,16 @@ pub(crate) fn prepare_for_clock_tick(
if let Some(inputs) = state.last_compositor_scroll_inputs {
refresh_compositor_scroll_table(arena, state, inputs);
}
+ // NB: The update rebuilds the scroll state with every offset at zero, whether or not it changed the tree's
+ // structure, and the frame places its window around the viewport by those offsets: They're refreshed from
+ // layout here, so a pane scrolled down keeps what it shows in the window. The host takes the same offsets.
+ let scroll_offsets = scroll_state_snapshot(&arena.paintable_rows(), state, inputs.device_pixels_per_css_pixel);
let tree = state.tree.as_mut().expect("an incremental update keeps the tree");
if !outcome.structural_epoch_changed {
return Ok(Some(ClockTickVisualContexts {
tree: tree.clone(),
- restructured_scroll_offsets: None,
+ scroll_offsets: Some(scroll_offsets),
+ restructured: false,
}));
}
// The update dropped the animations, which name nodes of the old structure: each goes on driving the nodes of its
@@ -176,12 +184,10 @@ pub(crate) fn prepare_for_clock_tick(
)?;
Arc::make_mut(tree).set_visual_animations(animations);
}
- let tree = tree.clone();
- // The host refreshes its own copy of the scroll state still.
- let scroll_offsets = scroll_state_snapshot(&arena.paintable_rows(), state, inputs.device_pixels_per_css_pixel);
Ok(Some(ClockTickVisualContexts {
- tree,
- restructured_scroll_offsets: Some(scroll_offsets),
+ tree: tree.clone(),
+ scroll_offsets: Some(scroll_offsets),
+ restructured: true,
}))
})
}
diff --git a/Libraries/LibWeb/Rust/src/painting/presentation.rs b/Libraries/LibWeb/Rust/src/painting/presentation.rs
index dd45db6b2af..60775f81d1b 100644
--- a/Libraries/LibWeb/Rust/src/painting/presentation.rs
+++ b/Libraries/LibWeb/Rust/src/painting/presentation.rs
@@ -508,16 +508,16 @@ impl Presentation {
pub(crate) fn take_visual_context_tree(&mut self, visual_contexts: ClockTickVisualContexts) {
let ClockTickVisualContexts {
tree,
- restructured_scroll_offsets,
+ scroll_offsets,
+ restructured,
} = visual_contexts;
- // A tree of another structure takes a new display list recorded against it, and the scroll offsets of its own
- // nodes. A frame that records nothing seals no tree, and takes the render state's: the compositor's display list
- // was cut from a tree of its structure.
- let tree_is_restructured = restructured_scroll_offsets.is_some();
- if let Some(scroll_offsets) = restructured_scroll_offsets {
+ // A tree of another structure takes a new display list recorded against it. And a tree a tick updated takes the
+ // scroll offsets of its own nodes, which the frame was recorded with. A frame that records nothing seals no tree,
+ // and takes the render state's: the compositor's display list was cut from a tree of its structure.
+ if let Some(scroll_offsets) = scroll_offsets {
self.scroll_state = ScrollStateSnapshot::from_device_offsets(scroll_offsets);
}
- if !tree_is_restructured && let Some(epoch) = self.seal.visual_context_tree_structural_epoch {
+ if !restructured && let Some(epoch) = self.seal.visual_context_tree_structural_epoch {
assert_eq!(
tree.structural_epoch, epoch,
"a tree of the same structure keeps its structural epoch"
@@ -539,7 +539,8 @@ impl Presentation {
if let Some(tree) = visual_context_tree {
self.take_visual_context_tree(ClockTickVisualContexts {
tree,
- restructured_scroll_offsets: None,
+ scroll_offsets: None,
+ restructured: false,
});
}
let presenter = self.presenter.clone();
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock.rs b/Libraries/LibWeb/Rust/src/render_state/clock.rs
index 2b7df3bf4bf..b45b2a41059 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock.rs
@@ -38,7 +38,7 @@ use crate::css::style::tree::StyleNodeID;
use crate::css::transition::HoverTransitions;
use crate::layout::node_data::{GENERATED_FOR_AFTER, GENERATED_FOR_BEFORE, GENERATED_FOR_MARKER, NodeSlotId};
use crate::layout::tree_update_marks::{FfiLayoutTreeUpdateMark, layout_tree_update_reuse_reason};
-use crate::layout::used_values::FfiCssPixelRect;
+use crate::layout::used_values::{FfiCssPixelPoint, FfiCssPixelRect};
use crate::layout::{ClockRound, ClockRoundDeclined, HostStyle, LayoutNodeArena};
use crate::paint_stage::Presenting;
use crate::painting::paint_passes::{ClockTickVisualContexts, VisualContextsNeedHost, prepare_for_clock_tick};
@@ -204,6 +204,8 @@ pub(crate) struct Lane {
shown: TickShownRecords,
/// The border boxes of the plan's elements, and of those the hover restyled, in the last frame a tick presented.
presented_border_boxes: Vec<(StyleNodeID, CssPixelRect)>,
+ /// The scroll positions in CSS pixels the last frame a tick presented was recorded with. For a test.
+ presented_scroll_offsets: Vec<(StyleNodeID, CssPixelPoint)>,
/// The color each box the ticks showed samples in showed in that frame, as `0xAARRGGBB`. For a test.
presented_colors: Vec<(StyleNodeID, u8, u32)>,
/// How many compositor animations of the frame drive each box of `presented_border_boxes`, and the opacity its
@@ -619,6 +621,7 @@ pub struct ClockTicks {
#[derive(Default)]
struct PresentedBoxes {
border_boxes: Vec<(StyleNodeID, CssPixelRect)>,
+ scroll_offsets: Vec<(StyleNodeID, CssPixelPoint)>,
colors: Vec<(StyleNodeID, u8, u32)>,
compositor_animations: Vec<(StyleNodeID, u32, f32)>,
}
@@ -869,6 +872,15 @@ impl ClockTicks {
.find_map(|&(presented, rect)| (presented == element).then_some(rect))
}
+ /// The scroll position of `element` the last frame a tick presented was recorded with, in CSS pixels.
+ pub(super) fn presented_scroll_offset(&self, element: StyleNodeID) -> Option<CssPixelPoint> {
+ let presented = self.presented_boxes.lock().expect("presented boxes");
+ presented
+ .scroll_offsets
+ .iter()
+ .find_map(|&(presented, offset)| (presented == element).then_some(offset))
+ }
+
/// The properties compositor animations drove on the box of `element` in the last frame a tick presented, one bit
/// each in the order of `FfiVisualAnimationTargetKind`, where the frame showed its border box.
pub(super) fn presented_compositor_animation_properties(&self, element: StyleNodeID) -> Option<u32> {
@@ -1235,6 +1247,7 @@ impl Lane {
restyles_built_again: Vec::new(),
shown: TickShownRecords::default(),
presented_border_boxes: Vec::new(),
+ presented_scroll_offsets: Vec::new(),
presented_colors: Vec::new(),
presented_compositor_animations: Vec::new(),
scroll_offsets: Vec::new(),
@@ -1667,6 +1680,17 @@ impl Lane {
.then(|| (element, absolute_border_box_rect(&rows, row)))
}),
);
+ self.presented_scroll_offsets.clear();
+ let scroll_state = &arena.paint_state().visual_context.scroll_state;
+ self.presented_scroll_offsets
+ .extend(scroll_state.states().iter().enumerate().filter_map(|(slot, scroll)| {
+ if scroll.is_sticky {
+ return None;
+ }
+ let element = arena.dom_node_style_node(scroll.paintable)?;
+ let offset = scroll_state.own_offset(slot);
+ Some((element, CssPixelPoint::new(-offset.x, -offset.y)))
+ }));
self.presented_compositor_animations.clear();
if let Some(tree) = arena.paint_state().visual_context.tree.as_deref() {
self.presented_compositor_animations
@@ -2156,6 +2180,24 @@ pub extern "C" fn document_host_presented_opacity(host: &DocumentHost, element:
true
}
+/// Writes the scroll position of `element` the last frame a tick presented was recorded with to `offset`, in CSS
+/// pixels, and answers whether that frame had a scroll node for the element. For a test.
+#[unsafe(no_mangle)]
+pub extern "C" fn document_host_presented_scroll_offset(
+ host: &DocumentHost,
+ element: u32,
+ offset: &mut FfiCssPixelPoint,
+) -> bool {
+ super::wait_for_render_side();
+ let Some(presented) =
+ StyleNodeID::from_raw(element).and_then(|element| host.clock_ticks().presented_scroll_offset(element))
+ else {
+ return false;
+ };
+ *offset = presented.into();
+ true
+}
+
/// Writes the border box of `element` in the last frame a tick of a lane of `host`'s document presented to `rect`, and
/// answers whether a tick presented one. For a test.
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs b/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs
index 5a6ef3e8d5e..69a4327654c 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs
@@ -617,6 +617,7 @@ impl LaneSlot {
self.presented = true;
*self.ticks.presented_boxes.lock().expect("presented boxes") = super::PresentedBoxes {
border_boxes: lane.presented_border_boxes.clone(),
+ scroll_offsets: lane.presented_scroll_offsets.clone(),
colors: lane.presented_colors.clone(),
compositor_animations: lane.presented_compositor_animations.clone(),
};
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-records-a-scrolled-pane-where-it-is-scrolled.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-records-a-scrolled-pane-where-it-is-scrolled.txt
new file mode 100644
index 00000000000..e8f88ea2d78
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-records-a-scrolled-pane-where-it-is-scrolled.txt
@@ -0,0 +1,4 @@
+last frame presented by: clock
+the presented height is the transition's at the tick: true
+presented scroll offset: 5000
+the presented scroll offset matches scrollTop: true
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-records-a-scrolled-pane-where-it-is-scrolled.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-records-a-scrolled-pane-where-it-is-scrolled.html
new file mode 100644
index 00000000000..d8c49e54ec7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-records-a-scrolled-pane-where-it-is-scrolled.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ body { margin: 0; }
+ #pane {
+ position: absolute;
+ width: 200px;
+ height: 100px;
+ overflow: auto;
+ }
+ .spacer { height: 5000px; }
+ #marker { height: 100px; background: green; }
+ #box {
+ margin-left: 220px;
+ width: 100px;
+ height: 100px;
+ background: blue;
+ transition: height 10000ms linear;
+ }
+</style>
+<div id="pane"><div class="spacer"></div><div id="marker"></div><div class="spacer"></div></div>
+<div id="box"></div>
+<script>
+ // The pane is scrolled far beyond the window around the viewport that a frame records. A height transition beside
+ // it changes layout but not the visual context tree's structure, and the tick's frame must still be recorded with
+ // the pane where it's scrolled to.
+ asyncTest(async done => {
+ await twoFramesLanded();
+ pane.scrollTop = 5000;
+ await twoFramesLanded();
+ const result = await whileLaneAnimates(() => {
+ box.style.height = "200px";
+ return box.getAnimations().find(animation => animation.transitionProperty === "height");
+ }, (frameTime, animation, startTime) => {
+ const tickTime = frameTime + 1000;
+ internals.injectClockTick(tickTime);
+ const presentedBy = internals.lastFramePresentedBy(document);
+ const presented = internals.presentedBorderBox(box);
+ const offset = internals.presentedScrollOffset(pane);
+ const expectedHeight = 100 + (tickTime - startTime) / 100;
+ return {
+ presentedBy,
+ heightWasSampled: presented !== null && Math.abs(presented.height - expectedHeight) < 0.02,
+ scrollOffset: offset?.y ?? null,
+ };
+ });
+ println(`last frame presented by: ${result.presentedBy}`);
+ println(`the presented height is the transition's at the tick: ${result.heightWasSampled}`);
+ println(`presented scroll offset: ${result.scrollOffset}`);
+ println(`the presented scroll offset matches scrollTop: ${result.scrollOffset === pane.scrollTop}`);
+ done();
+ });
+</script>