LibWeb: Implement partial layout tree updates

DOM nodes now have two additional flags:

- Needs layout tree update
- Child needs layout tree update

These work similarly to the needs-style-update flags, but instead signal
the need to rebuild the corresponding part of the layout tree.

When a specific DOM node needs a layout tree update, we try to create
a new subtree starting at that node, and then replace the subtree in the
old layout tree with the newly created subtree.

This required some refactoring in TreeBuilder so that we can skip over
entire subtrees during a tree update.

Note that no partial updates happen yet (as of this commit) since we
always invalidate the full layout tree still. That will change in the
next commit.
This commit is contained in:
Andreas Kling 2025-01-13 12:23:40 +01:00 committed by Andreas Kling
commit c01d810e5a
Notes: github-actions[bot] 2025-01-18 20:02:07 +00:00
6 changed files with 152 additions and 59 deletions

View file

@ -1009,6 +1009,7 @@ void Document::tear_down_layout_tree()
{
m_layout_root = nullptr;
m_paintable = nullptr;
m_needs_full_layout_tree_update = true;
}
Color Document::background_color() const
@ -1218,7 +1219,7 @@ void Document::update_layout()
auto* document_element = this->document_element();
auto viewport_rect = navigable->viewport_rect();
if (!m_layout_root) {
if (!m_layout_root || needs_layout_tree_update() || child_needs_layout_tree_update() || needs_full_layout_tree_update()) {
Layout::TreeBuilder tree_builder;
m_layout_root = verify_cast<Layout::Viewport>(*tree_builder.build(*this));
@ -1226,6 +1227,8 @@ void Document::update_layout()
propagate_overflow_to_viewport(*document_element, *m_layout_root);
propagate_scrollbar_width_to_viewport(*document_element, *m_layout_root);
}
set_needs_full_layout_tree_update(false);
}
// Assign each box that establishes a formatting context a list of absolutely positioned children it should take care of during layout

View file

@ -502,6 +502,9 @@ public:
bool needs_full_style_update() const { return m_needs_full_style_update; }
void set_needs_full_style_update(bool b) { m_needs_full_style_update = b; }
[[nodiscard]] bool needs_full_layout_tree_update() const { return m_needs_full_layout_tree_update; }
void set_needs_full_layout_tree_update(bool b) { m_needs_full_layout_tree_update = b; }
void set_needs_to_refresh_scroll_state(bool b);
bool has_active_favicon() const { return m_active_favicon; }
@ -936,6 +939,7 @@ private:
bool m_needs_layout { false };
bool m_needs_full_style_update { false };
bool m_needs_full_layout_tree_update { false };
bool m_needs_animated_style_update { false };

View file

@ -1326,6 +1326,29 @@ void Node::set_needs_inherited_style_update(bool value)
}
}
void Node::set_needs_layout_tree_update(bool value)
{
if (m_needs_layout_tree_update == value)
return;
m_needs_layout_tree_update = value;
// NOTE: If this is a shadow root, we need to propagate the layout tree update to the host.
if (is_shadow_root()) {
auto& shadow_root = static_cast<ShadowRoot&>(*this);
if (auto host = shadow_root.host())
host->set_needs_layout_tree_update(value);
}
if (m_needs_layout_tree_update) {
for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host()) {
if (ancestor->m_child_needs_layout_tree_update)
break;
ancestor->m_child_needs_layout_tree_update = true;
}
document().set_needs_layout();
}
}
void Node::set_needs_style_update(bool value)
{
if (m_needs_style_update == value)

View file

@ -281,6 +281,12 @@ public:
virtual bool is_child_allowed(Node const&) const { return true; }
[[nodiscard]] bool needs_layout_tree_update() const { return m_needs_layout_tree_update; }
void set_needs_layout_tree_update(bool);
[[nodiscard]] bool child_needs_layout_tree_update() const { return m_child_needs_layout_tree_update; }
void set_child_needs_layout_tree_update(bool b) { m_child_needs_layout_tree_update = b; }
bool needs_style_update() const { return m_needs_style_update; }
void set_needs_style_update(bool);
@ -789,6 +795,9 @@ protected:
GC::Ptr<Layout::Node> m_layout_node;
GC::Ptr<Painting::Paintable> m_paintable;
NodeType m_type { NodeType::INVALID };
bool m_needs_layout_tree_update { false };
bool m_child_needs_layout_tree_update { false };
bool m_needs_style_update { false };
bool m_needs_inherited_style_update { false };
bool m_child_needs_style_update { false };