1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:07:35 +00:00

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. :^)
This commit is contained in:
Andreas Kling 2022-02-19 20:13:47 +01:00
parent 9c05639d35
commit 561612f219
14 changed files with 50 additions and 31 deletions

View file

@ -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>());

View file

@ -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);
}

View file

@ -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;

View file

@ -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();
}

View file

@ -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; }

View file

@ -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.

View file

@ -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;
};
}

View file

@ -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 {
};
}

View file

@ -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)
{
}

View file

@ -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();

View file

@ -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)
{
}

View file

@ -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;

View file

@ -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)
{
}

View file

@ -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;