diff options
author | Andreas Kling <kling@serenityos.org> | 2022-02-19 20:13:47 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-21 18:35:12 +0100 |
commit | 561612f2192adf4c0cecfe7a19df5804fc1960ff (patch) | |
tree | 24a33bf7c36d5548dbdc030d0b1d05365014300e /Userland/Libraries | |
parent | 9c05639d35d51f17397751ce3c5b4e7999a93ba4 (diff) | |
download | serenity-561612f2192adf4c0cecfe7a19df5804fc1960ff.zip |
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. :^)
Diffstat (limited to 'Userland/Libraries')
14 files changed, 50 insertions, 31 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 842866bc6e..5c30e9da92 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<Layout::InitialContainingBlock>(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<float>()); diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp index 43955c9454..a5d7ebfa84 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 c9d7c18158..05b584fd0b 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 525663dc19..72a018baa3 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<CSS::LengthPercentage> 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<BlockContainer>(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<BlockContainer>(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<BlockContainer>(box), this); + BlockFormattingContext context(m_state, verify_cast<BlockContainer>(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 0a684de2af..111a146ccc 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 a1558eff0b..68fc5c4a4c 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 <kling@serenityos.org> + * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> * * 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> FormattingContext::create_independent_formatting_conte auto child_display = child_box.computed_values().display(); if (is<SVGSVGBox>(child_box)) - return make<SVGFormattingContext>(child_box, this); + return make<SVGFormattingContext>(m_state, child_box, this); if (child_display.is_flex_inside()) - return make<FlexFormattingContext>(child_box, this); + return make<FlexFormattingContext>(m_state, child_box, this); if (creates_block_formatting_context(child_box)) - return make<BlockFormattingContext>(verify_cast<BlockContainer>(child_box), this); + return make<BlockFormattingContext>(m_state, verify_cast<BlockContainer>(child_box), this); if (child_display.is_table_inside()) - return make<TableFormattingContext>(verify_cast<TableBox>(child_box), this); + return make<TableFormattingContext>(m_state, verify_cast<TableBox>(child_box), this); VERIFY(is_block_formatting_context()); if (child_box.children_are_inline()) - return make<InlineFormattingContext>(verify_cast<BlockContainer>(child_box), static_cast<BlockFormattingContext&>(*this)); + return make<InlineFormattingContext>(m_state, verify_cast<BlockContainer>(child_box), static_cast<BlockFormattingContext&>(*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 48f3216cba..13e4f25fa9 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +8,7 @@ #include <AK/OwnPtr.h> #include <LibWeb/Forward.h> +#include <LibWeb/Layout/FormattingState.h> namespace Web::Layout { @@ -46,7 +47,7 @@ public: virtual void parent_context_did_dimension_child_root_box() { } protected: - FormattingContext(Type, Box&, FormattingContext* parent = nullptr); + FormattingContext(Type, FormattingState&, Box&, FormattingContext* parent = nullptr); OwnPtr<FormattingContext> layout_inside(Box&, LayoutMode); @@ -77,6 +78,8 @@ protected: 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 0000000000..f90ffa29f9 --- /dev/null +++ b/Userland/Libraries/LibWeb/Layout/FormattingState.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> + * + * 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 4e803f7434..803b8bf596 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 b4882a1b6c..8f68362103 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 8d273aa905..ecb46df05d 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 9fca1c4edc..640b401233 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 508ba34434..0b1d28e394 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 0567cec5d5..bf9576001c 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; |