From 561612f2192adf4c0cecfe7a19df5804fc1960ff Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 19 Feb 2022 20:13:47 +0100 Subject: [PATCH] LibWeb: Add Layout::FormattingState The purpose of this new object will be to keep track of various states during an ongoing layout. Until now, we've been updating layout tree nodes as we go during layout, which adds an invisible layer of implicit serialization to the whole layout system. My idea with FormattingState is that running layout will produce a result entirely contained within the FormattingState object. At the end of layout, it can then be applied to the layout tree, or simply queried for some metrics we were trying to determine. When doing subtree layouts to determine intrinsic sizes, we will eventually be able to clone the current FormattingState, and run the subtree layout in isolation, opening up opportunities for parallelism. This first patch doesn't go very far though, it merely adds the object as a skeleton class, and makes sure the root BFC has one. :^) --- Userland/Libraries/LibWeb/DOM/Document.cpp | 3 ++- .../LibWeb/Layout/BlockFormattingContext.cpp | 6 +++--- .../LibWeb/Layout/BlockFormattingContext.h | 2 +- .../LibWeb/Layout/FlexFormattingContext.cpp | 14 +++++++------- .../LibWeb/Layout/FlexFormattingContext.h | 2 +- .../Libraries/LibWeb/Layout/FormattingContext.cpp | 15 ++++++++------- .../Libraries/LibWeb/Layout/FormattingContext.h | 7 +++++-- .../Libraries/LibWeb/Layout/FormattingState.h | 14 ++++++++++++++ .../LibWeb/Layout/InlineFormattingContext.cpp | 4 ++-- .../LibWeb/Layout/InlineFormattingContext.h | 2 +- .../LibWeb/Layout/SVGFormattingContext.cpp | 4 ++-- .../LibWeb/Layout/SVGFormattingContext.h | 2 +- .../LibWeb/Layout/TableFormattingContext.cpp | 4 ++-- .../LibWeb/Layout/TableFormattingContext.h | 2 +- 14 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 Userland/Libraries/LibWeb/Layout/FormattingState.h diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 842866bc6e138e..5c30e9da923ec2 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -563,7 +563,8 @@ void Document::update_layout() m_layout_root = static_ptr_cast(tree_builder.build(*this)); } - Layout::BlockFormattingContext root_formatting_context(*m_layout_root, nullptr); + Layout::FormattingState root_formatting_state; + Layout::BlockFormattingContext root_formatting_context(root_formatting_state, *m_layout_root, nullptr); m_layout_root->build_stacking_context_tree(); m_layout_root->set_content_size(viewport_rect.size().to_type()); diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp index 43955c9454f675..a5d7ebfa845e69 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp @@ -18,8 +18,8 @@ namespace Web::Layout { -BlockFormattingContext::BlockFormattingContext(BlockContainer& root, FormattingContext* parent) - : FormattingContext(Type::Block, root, parent) +BlockFormattingContext::BlockFormattingContext(FormattingState& state, BlockContainer& root, FormattingContext* parent) + : FormattingContext(Type::Block, state, root, parent) { } @@ -382,7 +382,7 @@ void BlockFormattingContext::layout_inline_children(BlockContainer& block_contai { VERIFY(block_container.children_are_inline()); - InlineFormattingContext context(block_container, *this); + InlineFormattingContext context(m_state, block_container, *this); context.run(block_container, layout_mode); } diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h index c9d7c18158a3b1..05b584fd0b9d84 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h @@ -16,7 +16,7 @@ namespace Web::Layout { // https://www.w3.org/TR/css-display/#block-formatting-context class BlockFormattingContext : public FormattingContext { public: - explicit BlockFormattingContext(BlockContainer&, FormattingContext* parent); + explicit BlockFormattingContext(FormattingState&, BlockContainer&, FormattingContext* parent); ~BlockFormattingContext(); virtual void run(Box&, LayoutMode) override; diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp index 525663dc194eb1..72a018baa32481 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp @@ -32,8 +32,8 @@ static bool is_undefined_or_auto(Optional const& length_p return length_percentage->is_length() && length_percentage->length().is_auto(); } -FlexFormattingContext::FlexFormattingContext(Box& flex_container, FormattingContext* parent) - : FormattingContext(Type::Flex, flex_container, parent) +FlexFormattingContext::FlexFormattingContext(FormattingState& state, Box& flex_container, FormattingContext* parent) + : FormattingContext(Type::Flex, state, flex_container, parent) , m_flex_direction(flex_container.computed_values().flex_direction()) { } @@ -471,9 +471,9 @@ float FlexFormattingContext::layout_for_maximum_main_size(Box& box) if (!main_constrained && box.children_are_inline()) { auto& block_container = verify_cast(box); - BlockFormattingContext bfc(block_container, this); + BlockFormattingContext bfc(m_state, block_container, this); bfc.run(box, LayoutMode::Default); - InlineFormattingContext ifc(block_container, bfc); + InlineFormattingContext ifc(m_state, block_container, bfc); if (is_row_layout()) { ifc.run(box, LayoutMode::OnlyRequiredLineBreaks); @@ -821,9 +821,9 @@ float FlexFormattingContext::determine_hypothetical_cross_size_of_item(Box& box) if (!cross_constrained && box.children_are_inline()) { auto& block_container = verify_cast(box); - BlockFormattingContext bfc(block_container, this); + BlockFormattingContext bfc(m_state, block_container, this); bfc.run(box, LayoutMode::Default); - InlineFormattingContext ifc(block_container, bfc); + InlineFormattingContext ifc(m_state, block_container, bfc); ifc.run(box, LayoutMode::OnlyRequiredLineBreaks); return is_row_layout() ? box.content_height() : box.content_width(); @@ -831,7 +831,7 @@ float FlexFormattingContext::determine_hypothetical_cross_size_of_item(Box& box) if (is_row_layout()) return BlockFormattingContext::compute_theoretical_height(box); - BlockFormattingContext context(verify_cast(box), this); + BlockFormattingContext context(m_state, verify_cast(box), this); context.compute_width(box); return box.content_width(); } diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h index 0a684de2af1d53..111a146ccc3a38 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h @@ -13,7 +13,7 @@ namespace Web::Layout { class FlexFormattingContext final : public FormattingContext { public: - FlexFormattingContext(Box& flex_container, FormattingContext* parent); + FlexFormattingContext(FormattingState&, Box& flex_container, FormattingContext* parent); ~FlexFormattingContext(); virtual bool inhibits_floating() const override { return true; } diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp index a1558eff0b8c3c..68fc5c4a4ce52b 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -19,10 +19,11 @@ namespace Web::Layout { -FormattingContext::FormattingContext(Type type, Box& context_box, FormattingContext* parent) +FormattingContext::FormattingContext(Type type, FormattingState& state, Box& context_box, FormattingContext* parent) : m_type(type) , m_parent(parent) , m_context_box(context_box) + , m_state(state) { } @@ -82,20 +83,20 @@ OwnPtr FormattingContext::create_independent_formatting_conte auto child_display = child_box.computed_values().display(); if (is(child_box)) - return make(child_box, this); + return make(m_state, child_box, this); if (child_display.is_flex_inside()) - return make(child_box, this); + return make(m_state, child_box, this); if (creates_block_formatting_context(child_box)) - return make(verify_cast(child_box), this); + return make(m_state, verify_cast(child_box), this); if (child_display.is_table_inside()) - return make(verify_cast(child_box), this); + return make(m_state, verify_cast(child_box), this); VERIFY(is_block_formatting_context()); if (child_box.children_are_inline()) - return make(verify_cast(child_box), static_cast(*this)); + return make(m_state, verify_cast(child_box), static_cast(*this)); // The child box is a block container that doesn't create its own BFC. // It will be formatted by this BFC. diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.h b/Userland/Libraries/LibWeb/Layout/FormattingContext.h index 48f3216cba3a51..13e4f25fa96e45 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +8,7 @@ #include #include +#include namespace Web::Layout { @@ -46,7 +47,7 @@ class FormattingContext { virtual void parent_context_did_dimension_child_root_box() { } protected: - FormattingContext(Type, Box&, FormattingContext* parent = nullptr); + FormattingContext(Type, FormattingState&, Box&, FormattingContext* parent = nullptr); OwnPtr layout_inside(Box&, LayoutMode); @@ -77,6 +78,8 @@ class FormattingContext { FormattingContext* m_parent { nullptr }; Box& m_context_box; + + FormattingState& m_state; }; } diff --git a/Userland/Libraries/LibWeb/Layout/FormattingState.h b/Userland/Libraries/LibWeb/Layout/FormattingState.h new file mode 100644 index 00000000000000..f90ffa29f9c8d2 --- /dev/null +++ b/Userland/Libraries/LibWeb/Layout/FormattingState.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +namespace Web::Layout { + +struct FormattingState { +}; + +} diff --git a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp index 4e803f743422df..803b8bf5964007 100644 --- a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp @@ -17,8 +17,8 @@ namespace Web::Layout { -InlineFormattingContext::InlineFormattingContext(BlockContainer& containing_block, BlockFormattingContext& parent) - : FormattingContext(Type::Inline, containing_block, &parent) +InlineFormattingContext::InlineFormattingContext(FormattingState& state, BlockContainer& containing_block, BlockFormattingContext& parent) + : FormattingContext(Type::Inline, state, containing_block, &parent) { } diff --git a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.h b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.h index b4882a1b6c4b7f..8f683621036259 100644 --- a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.h @@ -15,7 +15,7 @@ namespace Web::Layout { class InlineFormattingContext final : public FormattingContext { public: - InlineFormattingContext(BlockContainer& containing_block, BlockFormattingContext& parent); + InlineFormattingContext(FormattingState&, BlockContainer& containing_block, BlockFormattingContext& parent); ~InlineFormattingContext(); BlockFormattingContext& parent(); diff --git a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp index 8d273aa90507f1..ecb46df05d3b06 100644 --- a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp @@ -11,8 +11,8 @@ namespace Web::Layout { -SVGFormattingContext::SVGFormattingContext(Box& box, FormattingContext* parent) - : FormattingContext(Type::SVG, box, parent) +SVGFormattingContext::SVGFormattingContext(FormattingState& state, Box& box, FormattingContext* parent) + : FormattingContext(Type::SVG, state, box, parent) { } diff --git a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h index 9fca1c4edc9a57..640b401233ae58 100644 --- a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h @@ -13,7 +13,7 @@ namespace Web::Layout { class SVGFormattingContext : public FormattingContext { public: - explicit SVGFormattingContext(Box&, FormattingContext* parent); + explicit SVGFormattingContext(FormattingState&, Box&, FormattingContext* parent); ~SVGFormattingContext(); virtual void run(Box&, LayoutMode) override; diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp index 508ba34434f715..0b1d28e394f562 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp @@ -16,8 +16,8 @@ namespace Web::Layout { -TableFormattingContext::TableFormattingContext(BlockContainer& block_container, FormattingContext* parent) - : BlockFormattingContext(block_container, parent) +TableFormattingContext::TableFormattingContext(FormattingState& state, BlockContainer& block_container, FormattingContext* parent) + : BlockFormattingContext(state, block_container, parent) { } diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h index 0567cec5d59ae9..bf9576001c1478 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h @@ -13,7 +13,7 @@ namespace Web::Layout { class TableFormattingContext final : public BlockFormattingContext { public: - explicit TableFormattingContext(BlockContainer&, FormattingContext* parent); + explicit TableFormattingContext(FormattingState&, BlockContainer&, FormattingContext* parent); ~TableFormattingContext(); virtual void run(Box&, LayoutMode) override;