Skip to content

Truly independent web browser

LibWeb: Lay out clock ticks again only for layout-affecting samples

Andreas Kling7e2322eecf60
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
index 769e1ad1f69..a9983802fbb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
@@ -325,6 +325,24 @@ const POST_COMPUTE_ADJUSTED_LONGHANDS: [u16; 6] = [
/// Whether an animated value of `property` moves an adjustment the host makes after it samples, for an element with the
/// adjustment `facts`: the line height only of an element that keeps one of its own.
+/// Pushes the longhands `property`, a property a keyframe declares, stands for that `keeps` keeps onto `properties`
+/// once each: the property itself where it is a longhand, or what it expands to where it is a shorthand.
+fn push_longhands(property: u16, keeps: &impl Fn(u16) -> bool, properties: &mut smallvec::SmallVec<[u16; 2]>) {
+ use crate::css::property_metadata::{
+ FIRST_LONGHAND_PROPERTY_ID, LAST_LONGHAND_PROPERTY_ID, longhands_for_shorthand, property_is_shorthand,
+ };
+ if property_is_shorthand(property) {
+ for &longhand in longhands_for_shorthand(property) {
+ push_longhands(longhand, keeps, properties);
+ }
+ } else if (FIRST_LONGHAND_PROPERTY_ID..=LAST_LONGHAND_PROPERTY_ID).contains(&property)
+ && keeps(property)
+ && !properties.contains(&property)
+ {
+ properties.push(property);
+ }
+}
+
fn post_compute_adjusts(property: u16, facts: u32) -> bool {
use element_adjustment_fact::{CHECK_INPUT_LINE_HEIGHT, FORCE_LINE_HEIGHT_NORMAL};
match property {
@@ -426,10 +444,7 @@ impl super::StyleEngine {
.iter()
.flat_map(|keyframe| effect.declarations_of(keyframe))
{
- let property = declaration.property_id;
- if keeps(property) && !properties.contains(&property) {
- properties.push(property);
- }
+ push_longhands(declaration.property_id, &keeps, &mut properties);
}
}
properties
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index ed44a4abbb3..3a0da7a8acd 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -1142,6 +1142,15 @@ pub(crate) enum SampleKind {
Transition,
}
+/// How a box that shows a style in place of the one it showed decides whether it lays out again: by the damage word of
+/// the move, an `FfiStyleInvalidationField` word whose level says so as the host's install of the move would, or, where
+/// the caller knows no damage, by whether the style moved any group that affects layout.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub(crate) enum StyleRelayout {
+ ByDamage(u32),
+ ByLayoutAffectingGroups,
+}
+
/// The style a clock tick took from a box to show a sample of its element's animations in its place:
/// the record the host installed for the box, and the pin the arena held it by. Only
/// [`LayoutNodeArena::restore_host_style`] gives it back, so the host never reads a sample.
@@ -2602,17 +2611,19 @@ impl LayoutNodeArena {
self.with_style_engine(|engine| engine.unpin_layout_style_record(sample.record));
return Err(NeedsHost);
}
- Ok(self.install_sample_beside_anonymous_boxes(row, sample))
+ Ok(self.install_sample_beside_anonymous_boxes(row, sample, StyleRelayout::ByLayoutAffectingGroups))
}
/// Shows `sample` in the box `row`, which [`Self::takes_sample_beside_anonymous_boxes`] answers for, or in one of
/// the anonymous boxes below such a box, which shows what it inherits of the sample (see
/// [`Self::anonymous_child_samples`]), in place of the record the host installed. Answers the host's style where
- /// the box held it, or nothing where it held a sample already, which `sample` replaces.
+ /// the box held it, or nothing where it held a sample already, which `sample` replaces. The box lays out again as
+ /// `relayout` decides.
pub(crate) fn install_sample_beside_anonymous_boxes(
&self,
row: NodeSlotId,
sample: DerivedStyleRecord,
+ relayout: StyleRelayout,
) -> Option<HostStyle> {
let index = row.slot_index() as usize;
let host_style = match self.style_record_pins[index].get() {
@@ -2627,7 +2638,7 @@ impl LayoutNodeArena {
}),
};
self.style_record_pins[index].set(ArenaStylePin::Sampled);
- self.show_style(row, self.node_style_node(row), sample);
+ self.show_style(row, self.node_style_node(row), sample, relayout);
host_style
}
@@ -2662,8 +2673,9 @@ impl LayoutNodeArena {
!style_payloads_equal_in_inherited_groups(self.data(row).style.get(), host_payloads)
}
- /// Gives `row` back the style the host installed for it, which a clock tick took to show a sample in its place.
- pub(crate) fn restore_host_style(&self, row: NodeSlotId, host_style: HostStyle) {
+ /// Gives `row` back the style the host installed for it, which a clock tick took to show a sample in its place. The
+ /// box lays out again as `relayout` decides.
+ pub(crate) fn restore_host_style(&self, row: NodeSlotId, host_style: HostStyle, relayout: StyleRelayout) {
let index = row.slot_index() as usize;
assert!(
self.style_record_pins[index].get() == ArenaStylePin::Sampled,
@@ -2686,12 +2698,20 @@ impl LayoutNodeArena {
record,
payloads: StylePayloadsRef::new(payloads.cast()),
},
+ relayout,
);
}
/// Shows `style` in `row`, whose pin the caller set, as the host's install of a row of a box that styles no anonymous
- /// box does.
- fn show_style(&self, row: NodeSlotId, style_node: Option<StyleNodeID>, style: DerivedStyleRecord) {
+ /// box does, laying the box out again as `relayout` decides. A move whose damage only repaints, such as one of
+ /// `color`, lays out nothing again.
+ fn show_style(
+ &self,
+ row: NodeSlotId,
+ style_node: Option<StyleNodeID>,
+ style: DerivedStyleRecord,
+ relayout: StyleRelayout,
+ ) {
let previous_payloads = self.data(row).style.get();
self.style_records[row.slot_index() as usize].set(style.record);
let shape = self.write_shape(row);
@@ -2706,6 +2726,11 @@ impl LayoutNodeArena {
return;
}
self.bump_fragment_cache_epoch_of_self_and_ancestors(row);
+ if let StyleRelayout::ByDamage(damage) = relayout
+ && (damage & crate::css::style::bridge::FfiStyleInvalidationField::LevelMask as u32) < RELAYOUT_LEVEL
+ {
+ return;
+ }
self.reset_cached_intrinsic_sizes_of_self_and_ancestors(row);
if let Some(style_node) = style_node {
self.mark_row_for_relayout_after_style_change(style_node, row);
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rs
index c150b352d38..ed8205b8afe 100644
--- a/Libraries/LibWeb/Rust/src/layout/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs
@@ -69,7 +69,7 @@ use crate::layout::layout_node_arena::IntrinsicSizeCacheKey;
use crate::layout::layout_node_arena::IntrinsicSizeCacheKind;
pub(crate) use crate::layout::layout_node_arena::MainThreadFfiEntry as ArenaMainThreadFfiEntry;
pub(crate) use crate::layout::layout_node_arena::{
- HostStyle, LayoutNodeArena, LiveRow, PublishedBoundRows, RowsVersion, SLOTS_PER_CHUNK, SampleKind,
+ HostStyle, LayoutNodeArena, LiveRow, PublishedBoundRows, RowsVersion, SLOTS_PER_CHUNK, SampleKind, StyleRelayout,
};
use crate::layout::layout_node_arena::{TableCellMeasurement, TableCellMeasurementKey};
use crate::layout::node_data::AncestorFact;
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock.rs b/Libraries/LibWeb/Rust/src/render_state/clock.rs
index 5d852ac9816..4bd3edff4cd 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock.rs
@@ -1025,7 +1025,7 @@ fn restore_samples_for_build(
restyles.extend(kept_restyles);
}
for (row, host_style) in ticked.extract_if(.., |(row, host_style)| holds(*row, host_style)) {
- arena.restore_host_style(row, host_style);
+ arena.restore_host_style(row, host_style, crate::layout::StyleRelayout::ByLayoutAffectingGroups);
arena.push_paint_damage_for_repaint(row, PaintDamage::ALL_PRODUCERS);
}
Ok(())
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/effects.rs b/Libraries/LibWeb/Rust/src/render_state/clock/effects.rs
index 8866ec298a9..053c225d44e 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/effects.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/effects.rs
@@ -21,7 +21,7 @@ use crate::css::transition::{HoverTransitions, LaneTransitions};
use crate::layout::node_data::{
GENERATED_FOR_AFTER, GENERATED_FOR_BEFORE, GENERATED_FOR_MARKER, NodeSlotId, pseudo_kind_of,
};
-use crate::layout::{LayoutNodeArena, SampleKind};
+use crate::layout::{LayoutNodeArena, SampleKind, StyleRelayout};
use crate::painting::host::FfiVisualAnimationTargetKind;
use crate::painting::record::damage::PaintDamage;
use crate::painting::visual_animation::VisualAnimation;
@@ -282,6 +282,7 @@ impl Lane {
scroll_snaps,
subtree_follows: inheriting.covers_subtree,
};
+ let effect_properties = state.engine_ref().animated_properties(node);
let shown = state
.engine_mut()
.sample_at(node, host_record, samples, reference_box, bounds)
@@ -290,6 +291,7 @@ impl Lane {
state,
(row, node, sample),
&overlay,
+ &effect_properties,
&inheriting.descendants,
SampleKind::Animation,
bounds,
@@ -375,6 +377,7 @@ impl Lane {
let elapsed = timestamp - start_time;
let ended = elapsed >= transitions.duration;
let inheriting = transitions.inheriting.clone();
+ let properties: SmallVec<[u16; 8]> = transitions.properties().iter().copied().collect();
let bounds = SampleBounds {
scroll_snaps,
subtree_follows: transitions.inheriting_covers_subtree,
@@ -382,7 +385,6 @@ impl Lane {
let row = state.arena.arena().bound_row(node);
let shown = if ended {
// The boxes show the after-change styles of the element and of what inherits from it.
- let properties: SmallVec<[u16; 8]> = transitions.properties().iter().copied().collect();
moved |= self.show_host_styles(state, row, &properties, &inheriting);
None
} else if row.is_invalid() {
@@ -401,6 +403,7 @@ impl Lane {
state,
(row, node, sample),
&overlay,
+ &properties,
&inheriting,
SampleKind::Transition,
bounds,
@@ -448,7 +451,7 @@ impl Lane {
engine.element_record_damage(node, false, shown, sample.record)
}) | engine.sampled_properties_damage(shown, sample.record, properties);
let arena = state.arena.arena();
- let host_style = arena.install_sample_beside_anonymous_boxes(row, sample);
+ let host_style = arena.install_sample_beside_anonymous_boxes(row, sample, StyleRelayout::ByDamage(damage));
// The box shows what the sample of transitions the hover started animates of what the compositor animates, in
// place of the compositor. The compositor runs the host's animations as the ticks sample them, and on once the
// lane presents no more.
@@ -467,18 +470,27 @@ impl Lane {
/// Shows `element`'s sample of the effects of the element in its box, and what `overlay`, the values it animates,
/// gives each of the `inheriting` descendants in its box, and the `::before`, `::after` and `::marker` boxes of
/// those in theirs, over the record the host installed there, all at once: where one of the boxes takes no sample,
- /// none of them shows one. A descendant with no box shows nothing.
+ /// none of them shows one. A descendant with no box shows nothing. `effect_properties` are the properties the
+ /// effects animate over their whole run, which a box that showed an earlier sample of them may show yet where
+ /// `overlay` animates them no more, as past their end.
+ #[allow(clippy::too_many_arguments)]
fn show_element(
&mut self,
state: &mut RenderState,
element: (NodeSlotId, StyleNodeID, DerivedStyleRecord),
overlay: &AnimatedOverlay,
+ effect_properties: &[u16],
inheriting: &InheritingDescendants,
kind: SampleKind,
bounds: SampleBounds,
) -> Result<(), NeedsHost> {
let (element_row, element_node, element_sample) = element;
- let animated: SmallVec<[u16; 8]> = overlay.entries().iter().map(|entry| entry.property).collect();
+ let mut animated: SmallVec<[u16; 8]> = overlay.entries().iter().map(|entry| entry.property).collect();
+ for &property in effect_properties {
+ if !animated.contains(&property) {
+ animated.push(property);
+ }
+ }
let mut samples: SmallVec<[BoxSample<'_>; 4]> =
smallvec::smallvec![(element_row, element_node, element_sample, &animated[..])];
let mut composed = Ok(());
@@ -793,7 +805,7 @@ impl Lane {
.engine_mut()
.sampled_properties_damage(shown, host_style.record(), properties);
let arena = state.arena.arena();
- arena.restore_host_style(row, host_style);
+ arena.restore_host_style(row, host_style, StyleRelayout::ByDamage(damage));
arena.note_style_visual_context_moves(row, damage);
arena.push_paint_damage_for_repaint(row, PaintDamage::ALL_PRODUCERS);
moved = true;
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-animates-a-shorthand.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-animates-a-shorthand.txt
new file mode 100644
index 00000000000..3f5545431b8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-animates-a-shorthand.txt
@@ -0,0 +1,4 @@
+the box's color moved: true
+the box grew: true
+the box's color after its end: rgb(0, 0, 0)
+the box's height after its end is the one before: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-animates-color-and-font-size-past-their-end.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-animates-color-and-font-size-past-their-end.txt
new file mode 100644
index 00000000000..0ebd688ec8d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-animates-color-and-font-size-past-their-end.txt
@@ -0,0 +1,7 @@
+the card's color moved: true
+the card's box did not move: true
+the growing box's color moved: true
+the growing box grew: true
+the growing box's color after its end: rgb(0, 0, 0)
+the growing box's height after its end is the one before: true
+last frame presented by: clock
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-animates-a-shorthand.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-animates-a-shorthand.html
new file mode 100644
index 00000000000..3cdc490daff
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-animates-a-shorthand.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ #grow {
+ font-size: 10px;
+ line-height: 1;
+ }
+</style>
+<div id="grow">Grow</div>
+<script>
+ // The render clock samples an animation whose keyframes declare shorthands, padding and border, beside color. Past the
+ // end of the animation, the box shows the height of the style the host installed again.
+ asyncTest(async done => {
+ const heightBefore = grow.getBoundingClientRect().height;
+ const result = await whileLaneAnimates(() => {
+ return grow.animate(
+ [
+ { color: "rgb(0, 0, 0)", padding: "0px", border: "0px solid" },
+ { color: "rgb(0, 0, 200)", padding: "20px", border: "2px solid" },
+ ],
+ { duration: 200 }
+ );
+ }, (frameTime, animation, startTime) => {
+ internals.injectClockTick(startTime + 100);
+ const during = {
+ color: internals.presentedColor(grow),
+ height: internals.presentedBorderBox(grow)?.height,
+ };
+ internals.injectClockTick(startTime + 500);
+ return {
+ during,
+ after: {
+ color: internals.presentedColor(grow),
+ height: internals.presentedBorderBox(grow)?.height,
+ },
+ };
+ });
+ const { during, after } = result;
+ println(`the box's color moved: ${during.color !== null && during.color !== "rgb(0, 0, 0)"}`);
+ println(`the box grew: ${during.height > heightBefore}`);
+ println(`the box's color after its end: ${after.color}`);
+ println(`the box's height after its end is the one before: ${after.height === heightBefore}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-animates-color-and-font-size-past-their-end.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-animates-color-and-font-size-past-their-end.html
new file mode 100644
index 00000000000..25371d34815
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-animates-color-and-font-size-past-their-end.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ #card {
+ color: rgb(0, 0, 0);
+ font-size: 10px;
+ transition: color 1s linear;
+ }
+ #card.red {
+ color: rgb(200, 0, 0);
+ }
+ #grow {
+ font-size: 10px;
+ line-height: 1;
+ }
+ @keyframes grow {
+ from {
+ color: rgb(0, 0, 0);
+ font-size: 10px;
+ }
+ to {
+ color: rgb(0, 0, 200);
+ font-size: 30px;
+ }
+ }
+ #grow.animated {
+ animation: grow 200ms linear;
+ }
+</style>
+<div id="card">Card<div>block child</div></div>
+<div id="grow">Grow</div>
+<script>
+ // The render clock samples a transition of color, which only repaints its box, and an animation of color and
+ // font-size, which lays its box out again, beside a task. Past the end of the animation, the box shows the
+ // color and the height of the style the host installed again.
+ asyncTest(async done => {
+ const heightBefore = grow.getBoundingClientRect().height;
+ const cardBefore = card.getBoundingClientRect();
+ const result = await whileLaneAnimates(() => {
+ card.classList.add("red");
+ grow.classList.add("animated");
+ return grow.getAnimations()[0];
+ }, (frameTime, animation, startTime) => {
+ internals.injectClockTick(startTime + 100);
+ const during = {
+ card: internals.presentedColor(card),
+ cardBox: internals.presentedBorderBox(card),
+ grow: internals.presentedColor(grow),
+ growHeight: internals.presentedBorderBox(grow)?.height,
+ };
+ internals.injectClockTick(startTime + 500);
+ return {
+ during,
+ after: {
+ card: internals.presentedColor(card),
+ grow: internals.presentedColor(grow),
+ growHeight: internals.presentedBorderBox(grow)?.height,
+ },
+ presentedBy: internals.lastFramePresentedBy(document),
+ };
+ });
+ const { during, after } = result;
+ println(`the card's color moved: ${during.card !== null && during.card !== "rgb(0, 0, 0)" && during.card !== "rgb(200, 0, 0)"}`);
+ println(`the card's box did not move: ${during.cardBox?.height === cardBefore.height && during.cardBox?.width === cardBefore.width}`);
+ println(`the growing box's color moved: ${during.grow !== null && during.grow !== "rgb(0, 0, 0)"}`);
+ println(`the growing box grew: ${during.growHeight > heightBefore}`);
+ println(`the growing box's color after its end: ${after.grow}`);
+ println(`the growing box's height after its end is the one before: ${after.growHeight === heightBefore}`);
+ println(`last frame presented by: ${result.presentedBy}`);
+ done();
+ });
+</script>