Skip to content

Truly independent web browser

LibWeb: Hand layout nodes the style their element installed

Andreas Klingcb84918afe38
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 00d711b71b3..7ee3f62e5fe 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -1802,10 +1802,9 @@ void Element::swap_in_style_record(Layout::BegunRead const& read, Optional<CSS::
// A new style can change the states and the text formatting the accessibility tree reports for the element.
if (document().page().accessibility_interested())
document().invalidate_cached_accessibility_records_after_style_change(*this);
- auto const style_record = m_installed_style.record();
// NB: The render side installed the record in a box it styles itself, which the box adopts.
if (auto* layout_node = unsafe_layout_node(read))
- layout_node->set_style_record_identity(style_record);
+ layout_node->set_style_record_identity(m_installed_style);
}
Element::CustomHighlightStyles const* Element::custom_highlight_styles() const
@@ -5267,7 +5266,7 @@ void Element::replace_installed_style(Layout::BegunRead const& read, CSS::Instal
// 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 keeps the old record until it takes the new one in.
if (auto* layout_node = unsafe_layout_node(read))
- layout_node->set_style_record_identity(m_installed_style.record());
+ layout_node->set_style_record_identity(m_installed_style);
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 6425028066c..edc6b65e134 100644
--- a/Libraries/LibWeb/DOM/PseudoElement.cpp
+++ b/Libraries/LibWeb/DOM/PseudoElement.cpp
@@ -91,7 +91,7 @@ void SyntheticPseudoElement::replace_installed_style(Layout::BegunRead const& re
// 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.record());
+ layout_node->set_style_record_identity(m_installed_style);
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(), held_before.record_pointer());
}
@@ -106,7 +106,7 @@ void SyntheticPseudoElement::swap_in_style_record(Layout::BegunRead const& read,
return;
m_installed_style = move(style);
if (auto* layout_node = unsafe_layout_node(read))
- layout_node->set_style_record_identity(style_record);
+ layout_node->set_style_record_identity(m_installed_style);
}
void SyntheticPseudoElement::set_computed_style(Layout::BegunRead const& read, CSS::InstalledStyle style)
diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp
index 97b684ce276..58d9db836f7 100644
--- a/Libraries/LibWeb/Layout/Node.cpp
+++ b/Libraries/LibWeb/Layout/Node.cpp
@@ -398,11 +398,13 @@ bool style_record_installed_by_render_state(NodeWithStyle const& node, CSS::Styl
// build would otherwise lay out a box of one kind by the style of another. The element holds the new record from now
// on, so the box pins the one it keeps until the build replaces it. A record the render side installed in the box
// already passed its own check.
-bool NodeWithStyle::keeps_style_until_rebuilt(CSS::StyleRecordID style_record_identity)
+bool NodeWithStyle::keeps_style_until_rebuilt(CSS::StyleRecordID style_record_identity, CSS::InstalledStyle const* known_style)
{
if (!m_style_record || m_has_layout_derived_style || style_record_installed_by_render_state(*this, style_record_identity))
return false;
- auto const style = document().style_computer().install_style(held_read(), style_record_identity);
+ // OPTIMIZATION: A caller that holds the style of the record hands it over, sparing a round trip to the engine for
+ // each element whose style changes, as each animated element's does in each frame.
+ auto const style = known_style ? *known_style : document().style_computer().install_style(held_read(), style_record_identity);
if (!style || RustFFI::layout_box_can_show_style(kind(), m_style_record, style.groups()))
return false;
pin_style_record_for_cxx_consumers();
@@ -574,8 +576,9 @@ static bool uses_button_layout(Node const& node)
return html_element && html_element->uses_button_layout();
}
-void NodeWithStyle::set_style_record_identity(CSS::StyleRecordID style_record_identity)
+void NodeWithStyle::set_style_record_identity(CSS::InstalledStyle const& style)
{
+ auto const style_record_identity = style.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)
@@ -585,7 +588,7 @@ void NodeWithStyle::set_style_record_identity(CSS::StyleRecordID style_record_id
return;
}
- if (keeps_style_until_rebuilt(style_record_identity))
+ if (keeps_style_until_rebuilt(style_record_identity, &style))
return;
bool should_repin_style_record = m_style_record_pinned_for_cxx_consumers;
@@ -595,9 +598,9 @@ void NodeWithStyle::set_style_record_identity(CSS::StyleRecordID style_record_id
m_list_style_image.clear();
m_style_record_identity = style_record_identity;
if (style_record_installed_by_render_state(*this, style_record_identity))
- adopt_style_record_installed_by_render_state();
+ adopt_style_record_installed_by_render_state(&style);
else
- publish_style_record_to_node_data();
+ publish_style_record_to_node_data(&style);
release_pinned_style_record();
if (should_repin_style_record)
pin_style_record_for_cxx_consumers();
@@ -636,14 +639,14 @@ static Node const* scroll_snap_container_of(NodeWithStyle const& node)
return &node;
}
-void NodeWithStyle::publish_style_record_to_node_data()
+void NodeWithStyle::publish_style_record_to_node_data(CSS::InstalledStyle const* known_style)
{
// A row the render side styled is never published with another record behind its back.
if constexpr (LIBWEB_CHECK_RENDER_STATE_INSTALLS) {
auto const installed = RustFFI::render_state_installed_style_record(document_host(), slot_id(this));
VERIFY(installed == 0 || installed == m_style_record_identity.value());
}
- auto const style = document().style_computer().install_style(held_read(), m_style_record_identity);
+ auto const style = known_style ? *known_style : document().style_computer().install_style(held_read(), m_style_record_identity);
VERIFY(style);
m_style_record = style.groups();
RustFFI::render_state_set_node_style(document_host(), slot_id(this), m_style_record_identity.value(), style.groups());
@@ -651,9 +654,9 @@ void NodeWithStyle::publish_style_record_to_node_data()
did_update_style_record();
}
-void NodeWithStyle::adopt_style_record_installed_by_render_state()
+void NodeWithStyle::adopt_style_record_installed_by_render_state(CSS::InstalledStyle const* known_style)
{
- auto const style = document().style_computer().install_style(held_read(), m_style_record_identity);
+ auto const style = known_style ? *known_style : document().style_computer().install_style(held_read(), m_style_record_identity);
VERIFY(style);
m_style_record = style.groups();
m_has_layout_derived_style = false;
diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h
index d08e547d2ce..b10ccdecfb3 100644
--- a/Libraries/LibWeb/Layout/Node.h
+++ b/Libraries/LibWeb/Layout/Node.h
@@ -527,7 +527,7 @@ public:
void set_computed_values(Layout::BegunRead const& read, NonnullRefPtr<CSS::ComputedValues const>);
// 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 set_style_record_identity(CSS::InstalledStyle const&);
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
@@ -536,11 +536,12 @@ public:
void release_pinned_style_record();
private:
- bool keeps_style_until_rebuilt(CSS::StyleRecordID);
+ // NB: These take the style of the record where the caller holds it, which spares asking the engine for it.
+ bool keeps_style_until_rebuilt(CSS::StyleRecordID, CSS::InstalledStyle const* = nullptr);
virtual bool is_node_with_style() const final { return true; }
- void publish_style_record_to_node_data();
- void adopt_style_record_installed_by_render_state();
+ void publish_style_record_to_node_data(CSS::InstalledStyle const* = nullptr);
+ void adopt_style_record_installed_by_render_state(CSS::InstalledStyle const* = nullptr);
void did_update_style_record();
void rebuild_image_observers(Vector<RefPtr<CSS::CursorStyleValue const>>);