Skip to content

Truly independent web browser

LibWeb: Decide the relayout of the host's style install in Rust

Aliaksandr Kalenik95a5fdbf67cd
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index db4f919f96f..9b25941625c 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -5460,7 +5460,7 @@ void Element::replace_style_record(CSS::StyleRecordID style_record_identity)
// what it moves from, and everything below that compares against the old record is read from it, before the
// element lets go of it. The engine may free the old record's payloads as soon as it is no longer held.
if (auto* layout_node = unsafe_layout_node(read))
- layout_node->set_style_record_identity(m_installed_style, held_before);
+ layout_node->set_style_record_identity(m_installed_style.record());
bool const was_display_none = !held_before || held_before.display_is_none();
// https://drafts.csswg.org/scroll-animations-1/#timeline-scoping
bool const timeline_declarations_changed = timeline_declarations_differ(held_before, m_installed_style);
diff --git a/Libraries/LibWeb/DOM/PseudoElement.cpp b/Libraries/LibWeb/DOM/PseudoElement.cpp
index 3821666eb77..8de732460c1 100644
--- a/Libraries/LibWeb/DOM/PseudoElement.cpp
+++ b/Libraries/LibWeb/DOM/PseudoElement.cpp
@@ -87,11 +87,11 @@ void SyntheticPseudoElement::replace_style_record(CSS::StyleRecordID style_recor
if (m_installed_style.record() == style_record_identity)
return;
auto& style_computer = m_originating_element->document().style_computer();
- auto held_before = exchange(m_installed_style, style_computer.install_style(read, style_record_identity));
+ m_installed_style = style_computer.install_style(read, style_record_identity);
// The pseudo-element holds the record it installed before until the engine takes the new one in, so the layout node
// reads what it moves from before the pseudo-element lets go of it.
if (auto* layout_node = unsafe_layout_node(read))
- layout_node->set_style_record_identity(m_installed_style, held_before);
+ layout_node->set_style_record_identity(m_installed_style.record());
if (auto style_node_id = m_originating_element->style_node_id(); style_node_id != 0)
CSS::StyleEngineFFI::style_engine_set_held_pseudo_style_record(style_computer.style_engine().host(), style_node_id, to_underlying(type()), style_record_identity.value());
}
diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp
index 4d0ee2c829f..0e5ebcb5fbd 100644
--- a/Libraries/LibWeb/Layout/Node.cpp
+++ b/Libraries/LibWeb/Layout/Node.cpp
@@ -137,11 +137,6 @@ StringView Node::class_name() const
VERIFY_NOT_REACHED();
}
-void Node::reset_cached_intrinsic_sizes_of_self_and_ancestors()
-{
- RustFFI::render_state_reset_cached_intrinsic_sizes_of_self_and_ancestors(document_host(), slot_id(this));
-}
-
RustFFI::DocumentHost* Node::document_host() const
{
return m_arena->host();
@@ -504,9 +499,8 @@ static bool uses_button_layout(Node const& node)
return html_element && html_element->uses_button_layout();
}
-void NodeWithStyle::set_style_record_identity(CSS::InstalledStyle const& installed, CSS::InstalledStyle const& held_before)
+void NodeWithStyle::set_style_record_identity(CSS::StyleRecordID style_record_identity)
{
- auto style_record_identity = installed.record();
// A detached or layout-derived record is independent of its DOM target's record. A
// rendering consequence replaces and re-derives it explicitly through apply_style().
if (m_has_layout_derived_style)
@@ -517,21 +511,6 @@ void NodeWithStyle::set_style_record_identity(CSS::InstalledStyle const& install
}
bool should_repin_style_record = m_style_record_pinned_for_cxx_consumers;
- // Both answers come from the records themselves, so neither side needs a ComputedValues built
- // for it. Compare the effective payloads, including animations, rather than treating every
- // animation overlay as layout-affecting. The node's own record is the one its target held
- // before where the node followed it, which the target still holds.
- auto const& new_record_view = installed.view();
- VERIFY(new_record_view.present);
- CSS::StyleEngine::StyleRecordView old_record_view {};
- if (!!m_style_record_identity) {
- old_record_view = held_before.record() == m_style_record_identity
- ? held_before.view()
- : document().style_computer().style_engine().style_record_view(held_read(), m_style_record_identity);
- }
- bool changes_layout_affecting_style = !old_record_view.present
- || !CSS::ComputedValuesFFI::rust_style_payloads_layout_inputs_equal(old_record_view.payloads, new_record_view.payloads, is_svg_box());
-
release_pinned_style_record();
m_background_images.clear();
m_mask_images.clear();
@@ -542,9 +521,8 @@ void NodeWithStyle::set_style_record_identity(CSS::InstalledStyle const& install
if (should_repin_style_record)
pin_style_record_for_cxx_consumers();
- if (changes_layout_affecting_style)
- reset_cached_intrinsic_sizes_of_self_and_ancestors();
- if (changes_layout_affecting_style && (kind() == RustFFI::NodeKind::FieldSetBox || uses_button_layout(*this)))
+ // The anonymous content box of a button or a fieldset also takes properties of this style that it does not inherit.
+ if (kind() == RustFFI::NodeKind::FieldSetBox || uses_button_layout(*this))
RustFFI::render_state_reinherit_anonymous_descendants(document_host(), slot_id(this), false);
}
diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h
index 87d1dffefa7..4d9f53610ac 100644
--- a/Libraries/LibWeb/Layout/Node.h
+++ b/Libraries/LibWeb/Layout/Node.h
@@ -171,13 +171,6 @@ public:
bool needs_compositor_background_color_frame() const { return has_compositor_animation_frame(RustFFI::CompositorAnimationFrameKind::BackgroundColor); }
void set_needs_compositor_background_color_frame(bool value) { set_needs_compositor_animation_frame(RustFFI::CompositorAnimationFrameKind::BackgroundColor, value); }
- // Any invalidation below a node must reach every ancestor's epoch: cached runs capture
- // subtree structure, and unlike intrinsic-size invalidation there is no absolutely-positioned
- // or SVG boundary — those descendants' fragments live in ancestor run trees. The arena runs
- // the same walk for every structural change; this serves content changes that never
- // restructure the tree.
- void reset_cached_intrinsic_sizes_of_self_and_ancestors();
-
// Set when a style change altered geometry-determining properties of this node itself, so
// a partial relayout must re-resolve its own size and position instead of reusing them.
void set_needs_own_geometry_update() { set_flag(RustFFI::NodeFlag::NeedsOwnGeometryUpdate, true); }
@@ -525,9 +518,9 @@ public:
bool is_scroll_container() const;
void set_computed_values(Layout::BegunRead const& read, NonnullRefPtr<CSS::ComputedValues const>);
- // Takes the record its DOM target installed, `installed`, where it followed the target's record. `held_before` is
- // the record the target held before and still holds, or none.
- void set_style_record_identity(CSS::InstalledStyle const& installed, CSS::InstalledStyle const& held_before);
+ // Takes the record its DOM target installed, where it followed the target's record. The target still holds the
+ // record it held before until the engine takes the new one in, so the render state compares the two.
+ void set_style_record_identity(CSS::StyleRecordID);
void refresh_style_from_arena(CSS::StyleRecordID, void const* payloads, bool derived, bool should_attach_resources);
// The pin lives on the node's arena row and is released with it, so
// Document::tear_down_layout_tree() must free the layout root before the document's style
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index 5cd03c02e45..d972be232a4 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -2072,26 +2072,6 @@ pub(crate) fn release_group_payload(group_index: usize, payload: *const c_void)
}
}
-/// See [`style_payloads_layout_inputs_equal`].
-///
-/// # Safety
-/// `a` and `b` must each point to a record's array of live group payloads.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_payloads_layout_inputs_equal(
- a: *const *const c_void,
- b: *const *const c_void,
- is_svg_box: bool,
-) -> bool {
- // SAFETY: The caller guarantees two arrays of live group payloads.
- unsafe {
- style_payloads_layout_inputs_equal(
- ak::slice_from_raw_parts(a, group_index::COUNT),
- ak::slice_from_raw_parts(b, group_index::COUNT),
- is_svg_box,
- )
- }
-}
-
/// Clamps a border box size by the min and max sizes of a sizing group that resolve against the given basis, as
/// resizing an element by its resizer does.
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_changes.rs b/Libraries/LibWeb/Rust/src/layout/layout_changes.rs
index 7bb315f8772..9e49b159160 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_changes.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_changes.rs
@@ -29,11 +29,6 @@ pub(crate) enum LayoutChange {
},
/// The whole layout tree is built again.
SetNeedsFullLayoutTreeUpdate,
- /// What the node's content is sized from changed: its fragment caches and intrinsic sizes, and those of its
- /// ancestors, are stale.
- ResetCachedIntrinsicSizesOfSelfAndAncestors {
- node: NodeSlotId,
- },
/// The box takes `marks`.
MarkBox {
target: super::tree_update_marks::MarkedBox,
@@ -123,7 +118,8 @@ pub(crate) enum LayoutChange {
kind: super::node_data::CompositorAnimationFrameKind,
value: bool,
},
- /// The row takes the style record `record`, whose payloads `payloads` holds.
+ /// The row takes the style record `record`, whose payloads `payloads` holds. Where the move changes what layout reads
+ /// of the row's style, the sizes the row and its ancestors cached are measured again.
SetNodeStyle {
node: NodeSlotId,
record: u64,
@@ -170,7 +166,6 @@ impl LayoutChange {
Self::SetNodeStyle { .. } => RowWrite::NamedStyles,
Self::SetNeedsLayoutUpdate { .. }
| Self::SetNeedsFullLayoutTreeUpdate
- | Self::ResetCachedIntrinsicSizesOfSelfAndAncestors { .. }
| Self::MarkBox { .. }
| Self::ApplyLayoutTreeUpdateMark { .. }
| Self::RecordPartialRelayoutEscape
@@ -236,12 +231,6 @@ impl LayoutChange {
}
}
Self::SetNeedsFullLayoutTreeUpdate => arena.set_needs_full_layout_tree_update(true),
- Self::ResetCachedIntrinsicSizesOfSelfAndAncestors { node } => {
- if arena.slot_is_live(node) {
- arena.bump_fragment_cache_epoch_of_self_and_ancestors(node);
- arena.reset_cached_intrinsic_sizes_of_self_and_ancestors(node);
- }
- }
Self::MarkBox { target, marks } => marks.apply(arena, target),
Self::ApplyLayoutTreeUpdateMark { node, mark } => arena.apply_layout_tree_update_mark(node, mark),
Self::RecordPartialRelayoutEscape => arena.record_partial_relayout_escape(),
@@ -303,11 +292,20 @@ impl LayoutChange {
arena.set_node_needs_compositor_animation_frame(node, kind, value);
}
Self::SetNodeStyle { node, record, payloads } => {
+ // A style the arena derived is replaced only to be derived again, which decides the relayout itself.
+ let previous_payloads = (arena.slot_is_live(node) && !arena.node_style_record_is_derived(node))
+ .then(|| arena.data(node).style.get());
if arena.set_node_style(node, record, payloads) {
arena.refresh_style_flags(node);
}
arena.publish_new_size_container_geometry(node);
arena.enroll_node_for_svg_paint_resources_sync(node);
+ if previous_payloads
+ .is_some_and(|previous| !arena.style_move_keeps_layout_inputs(node, previous, payloads))
+ {
+ arena.bump_fragment_cache_epoch_of_self_and_ancestors(node);
+ arena.reset_cached_intrinsic_sizes_of_self_and_ancestors(node);
+ }
}
Self::PinBoundBoxStyleRecordForDetachment {
style_node,
@@ -479,14 +477,6 @@ pub extern "C" fn render_state_set_needs_full_layout_tree_update(host: &Document
queue(host, LayoutChange::SetNeedsFullLayoutTreeUpdate);
}
-#[unsafe(no_mangle)]
-pub extern "C" fn render_state_reset_cached_intrinsic_sizes_of_self_and_ancestors(
- host: &DocumentHost,
- node: NodeSlotId,
-) {
- queue(host, LayoutChange::ResetCachedIntrinsicSizesOfSelfAndAncestors { node });
-}
-
#[unsafe(no_mangle)]
pub extern "C" fn render_state_record_partial_relayout_escape(host: &DocumentHost) {
queue(host, LayoutChange::RecordPartialRelayoutEscape);
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 60905e14823..2f50eaaebf6 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -2376,7 +2376,7 @@ impl LayoutNodeArena {
/// Whether the box `slot` keeps the layout it cached when it shows `new` in place of `previous` (see
/// [`crate::css::computed_values::style_payloads_layout_inputs_equal`]).
- fn style_move_keeps_layout_inputs(
+ pub(crate) fn style_move_keeps_layout_inputs(
&self,
slot: NodeSlotId,
previous: StylePayloadsRef,