Skip to content

Commit

Permalink
LibWeb: Add Layout::FormattingState
Browse files Browse the repository at this point in the history
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. :^)
  • Loading branch information
awesomekling committed Feb 21, 2022
1 parent 9c05639 commit 561612f
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 31 deletions.
3 changes: 2 additions & 1 deletion Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>());

Expand Down
6 changes: 3 additions & 3 deletions Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

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

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -821,17 +821,17 @@ 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();
}
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();
}
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
15 changes: 8 additions & 7 deletions Userland/Libraries/LibWeb/Layout/FormattingContext.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <[email protected]>
* Copyright (c) 2020-2022, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -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)
{
}

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions Userland/Libraries/LibWeb/Layout/FormattingContext.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <[email protected]>
* Copyright (c) 2020-2022, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -8,6 +8,7 @@

#include <AK/OwnPtr.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Layout/FormattingState.h>

namespace Web::Layout {

Expand Down Expand Up @@ -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<FormattingContext> layout_inside(Box&, LayoutMode);

Expand Down Expand Up @@ -77,6 +78,8 @@ class FormattingContext {

FormattingContext* m_parent { nullptr };
Box& m_context_box;

FormattingState& m_state;
};

}
14 changes: 14 additions & 0 deletions Userland/Libraries/LibWeb/Layout/FormattingState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2022, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

namespace Web::Layout {

struct FormattingState {
};

}
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Layout/InlineFormattingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Layout/TableFormattingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 561612f

Please sign in to comment.