Skip to content

Commit

Permalink
[FlexNG] Expand row cross size for fragmentation
Browse files Browse the repository at this point in the history
Previously, for flex rows, when items expanded as a result of
fragmentation, we would only adjust the subsequent rows' offsets based
on the expansion amount. However, we also want to expand the row
block-size to allow items in that row to stretch to the newly expanded
size.

Because flex items in a row can break before, we couldn't implement
this in the same way as grid (i.e. by storing the results and
offsets and then adding them to the builder once all expansion is
handled). Otherwise, we could end up with a child break before added
to the builder before the previous items in the row had been added.

Instead, we need to abort layout and re-run with the updated row
block-sizes (using the newly added layout result status of
kNeedsRelayoutWithRowCrossSizeChanges and the newly added
RelayoutWithNewRowSizes() method).

Since we will re-layout as we go, we can no longer wait to detect
the row expansion until items have completed layout, but we need to
find the max row expansion in each fragmentainer as we go (and as
items cross the current row block-size). As such, we now calculate
row expansion without making use of NGFlexItem's
total_remaining_block_size (as we do for columns).

Because more than one row can expand per fragmentainer, we can avoid
aborting layout more than once by adjusting subsequent row offsets
as expansion is discovered and by keeping track of the expansion
of all rows in a given fragmentainer (there should only be at most two
row expansions at a time, though).

While testing, I ran into a few additional issues:

1. I had noticed a bug with how row expansion was handled for items
that broke before. We would previously adjust the offset of all items
in the row rather than just the offset of the item itself. This is now
fixed by not updating |line_output.item_offset_adjustment| in this
case.

2. When checking if the |broke_before_row| is associated with the
current row, we can't just check if we are at an item at index 0
since a previous item may have broken as a result of overflow.
Check instead if the current item is at index 0 and that it broke
before.

3. When adding |item_offset_adjustment| of one row to the next,
check if the last item has a break token or if the line has seen
all children so that we do so even if the last item overflows. (But
if the last item overflowed, we only want to adjust
|item_offset_adjustment| if there aren't any additional break tokens
to process, in which case the next row would have already started
making progress).

Note: We will stop row expansion if the container's consumed block-size
hits the max to avoid infinite expansion. This keeps the following test
from timing out:
external/wpt/css/css-break/flexbox/flexbox-fragmentation-layout-001-crash.html

multi-line-row-flex-fragmentation-016.html was previously incorrect
since item stretching as a result of row expansion was not yet
implemented. The test is now updated to account for the new
behavior.

Bug: 660611
Change-Id: If4fca22d10a43052a2f0218333d3a74752762b79
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3561235
Reviewed-by: Morten Stenshorne <mstensho@chromium.org>
Commit-Queue: Alison Maher <almaher@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#988612}
  • Loading branch information
alisonmaher authored and Chromium LUCI CQ committed Apr 4, 2022
1 parent f3b5358 commit 9142fd2
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CORE_EXPORT NGFlexItemIterator {
struct Entry;
Entry NextItem(bool broke_before_row);

bool HasMoreBreakTokens() const { return break_token_; }

// Move the iterator to the next line, unless we are already at the start of a
// line.
void NextLine();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class CORE_EXPORT NGFlexLayoutAlgorithm
NGBoxFragmentBuilder,
NGBlockBreakToken> {
public:
explicit NGFlexLayoutAlgorithm(const NGLayoutAlgorithmParams& params,
DevtoolsFlexInfo* devtools = nullptr);
explicit NGFlexLayoutAlgorithm(
const NGLayoutAlgorithmParams& params,
DevtoolsFlexInfo* devtools = nullptr,
const HashMap<wtf_size_t, LayoutUnit>* cross_size_adjustments = nullptr);

MinMaxSizesResult ComputeMinMaxSizes(const MinMaxSizesFloatInput&) override;
const NGLayoutResult* Layout() override;
Expand Down Expand Up @@ -159,6 +161,17 @@ class CORE_EXPORT NGFlexLayoutAlgorithm
// Add an early break for the column at the provided |index|.
void AddColumnEarlyBreak(NGEarlyBreak* breakpoint, wtf_size_t index);

// Add the amount an item expanded by to the item offset adjustment of the
// flex line at the index directly after |flex_line_idx|, if there is one.
void AdjustOffsetForNextLine(HeapVector<NGFlexLine>* flex_line_outputs,
wtf_size_t flex_line_idx,
LayoutUnit item_expansion) const;

// If a flex item expands past the row cross-size as a result of
// fragmentation, we will abort and re-run layout with the appropriate row
// cross-size adjustments.
const NGLayoutResult* RelayoutWithNewRowSizes();

#if DCHECK_IS_ON()
void CheckFlexLines(HeapVector<NGFlexLine>& flex_line_outputs) const;
#endif
Expand Down Expand Up @@ -198,6 +211,15 @@ class CORE_EXPORT NGFlexLayoutAlgorithm
// return to an early break within multiple flex columns. This stores the
// early breaks per column to be used when aborting layout.
HeapVector<Member<NGEarlyBreak>> column_early_breaks_;

// If an item expands past the row block-end, we will re-run layout with the
// new cross size. Keep track of each such flex line index mapped to how much
// it should expand by in the next layout pass.
HashMap<wtf_size_t, LayoutUnit> row_cross_size_updates_;

// If set, that means that we are re-running layout with updated row
// cross-sizes. See |row_cross_size_updates_| for what this maps to.
const HashMap<wtf_size_t, LayoutUnit>* cross_size_adjustments_ = nullptr;
};

} // namespace blink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct NGFlexItem {
// This will originally be set to the total block size of the item before
// fragmentation. It will then be reduced while performing fragmentation. If
// it becomes negative, that means that the item expanded as a result of
// fragmentation.
// fragmentation. This is only used for column flex containers.
LayoutUnit total_remaining_block_size;
FlexOffset offset;
bool has_descendant_that_depends_on_percentage_block_size = false;
Expand All @@ -38,6 +38,10 @@ struct NGFlexLine {
public:
explicit NGFlexLine(wtf_size_t num_items) : line_items(num_items) {}

LayoutUnit LineCrossEnd() const {
return line_cross_size + cross_axis_offset + item_offset_adjustment;
}

void Trace(Visitor* visitor) const { visitor->Trace(line_items); }

LayoutUnit line_cross_size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class CORE_EXPORT NGLayoutAlgorithm : public NGLayoutAlgorithmOperations {
// There are cases where we may need more than one early break per fragment.
// For example, there may be an early break within multiple flex columns. This
// can be used to pass additional early breaks to the next layout pass.
const HeapVector<Member<NGEarlyBreak>>* additional_early_breaks_;
const HeapVector<Member<NGEarlyBreak>>* additional_early_breaks_ = nullptr;
};

} // namespace blink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CORE_EXPORT NGLayoutResult final
kNeedsRelayoutWithNoForcedTruncateAtLineClamp = 4,
kDisableFragmentation = 5,
kNeedsRelayoutWithNoChildScrollbarChanges = 6,
kNeedsRelayoutWithRowCrossSizeChanges = 7,
// When adding new values, make sure the bit size of |Bitfields::status| is
// large enough to store.
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragment
crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragmentation-042.tentative.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragmentation-044.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragmentation-045.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragmentation-052.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragmentation-053.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/multi-line-row-flex-fragmentation-054.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/single-line-column-flex-fragmentation-012.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/single-line-column-flex-fragmentation-014.html [ Failure ]
crbug.com/660611 external/wpt/css/css-break/flexbox/single-line-column-flex-fragmentation-015.html [ Failure ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@
}
#flex > div {
background: green;
width: 10px;
width: 20px;
}
</style>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; background: red;">
<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
<div style="position: absolute; top: 40px; left: 20px; width: 20px; height: 60px; background: green;"></div>
<div id="flex">
<div style="position: relative;">
<div style="width: 10px;">
<div style="contain: size; width: 10px; height: 80px;"></div>
<div style="contain: size; width: 10px; height: 30px;"></div>
<div style="position: absolute; width: 10px; height: 10px; background: green;"></div>
</div>
<div>
<div style="width: 10px;">
<div style="contain: size; width: 10px; height: 70px;"></div>
<div style="contain: size; width: 10px; height: 40px;"></div>
</div>
<div style="margin-top: 10px; width: 20px; position: relative;">
<div style="margin-top: 10px;">
<div style="contain: size; width: 20px; height: 80px;"></div>
<div style="contain: size; width: 20px; height: 40px;"></div>
<div style="position: absolute; top: -60px; width: 20px; height: 60px; background: green;"></div>
</div>
<div style="height: 100px; width: 20px;"></div>
<div style="height: 60px; width: 20px;"></div>
<div style="height: 100px;"></div>
<div style="height: 60px;"></div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<title>
Multi-line row flex fragmentation: Row expansion and stretching.
</title>
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
<style>
#flex {
display: flex;
flex-wrap: wrap;
}
#flex > div {
background: green;
width: 10px;
}
</style>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
<div style="position: absolute; top: 50px; width: 10px; height: 50px; background: green;"></div>
<div style="position: absolute; top: 50px; left: 40px; width: 20px; height: 50px; background: green;"></div>
<div style="position: absolute; left: 70px; width: 10px; height: 50px; background: green;"></div>
<div style="position: absolute; top: 70px; left: 60px; width: 10px; height: 30px; background: green;"></div>
<div id="flex">
<div style="width: 20px; height: 50px;"></div>
<div style="margin-top: 40px; height: 60px; contain: size;"></div>
<div></div>
<div style="width: 20px; height: 90px;"></div>
<div style="margin-top: 40px; height: 70px; contain: size;"></div>
<div style="margin-top: 100px; height: 50px; contain: size;"></div>
<div style="height: 100px; break-before: column;"></div>
<div></div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<title>
Multi-line row flex fragmentation: Row expansion and stretching with item overflow.
</title>
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
<style>
#flex {
display: flex;
flex-wrap: wrap;
}
#flex > div {
background: green;
width: 50%;
}
</style>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
<div id="flex">
<div>
<div style="height: 30px;"></div>
<div style="height: 50px; break-before: column;"></div> <!-- The break-before forces the grid-item into the next column, and grows the row. -->
</div>
<div></div>
<div>
<div style="height: 30px;"></div>
<div style="height: 10px; break-before: column;"></div> <!-- The break-before forces the grid-item into the next column, and grows the row. -->
</div>
<div></div>
<div></div>
<div style="height: 100px;">
<div style="height: 200px;"></div>
</div>
<div style="height: 190px;"></div>
<div></div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<title>
Multi-line row flex fragmentation: Row expansion and stretching.
</title>
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
<style>
#flex {
display: flex;
flex-wrap: wrap;
}
#flex > div {
background: green;
width: 10px;
}
</style>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
<div style="position: absolute; top: 40px; left: 20px; width: 20px; height: 60px; background: green;"></div>
<div id="flex">
<div>
<div style="contain: size; width: 10px; height: 80px;"></div>
<div style="contain: size; width: 10px; height: 30px;"></div>
</div>
<div>
<div style="contain: size; width: 10px; height: 70px;"></div>
<div style="contain: size; width: 10px; height: 40px;"></div>
</div>
<div style="width: 20px; height: 100px; break-inside: avoid;"></div>
<div style="height: 200px; width: 20px;"></div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<title>
Multi-line row flex fragmentation: Item overflow and stretching.
</title>
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
<style>
#flex {
display: flex;
flex-wrap: wrap;
}
#flex > div {
background: green;
width: 50%;
}
</style>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
<div id="flex">
<div style="height: 100px;">
<div style="height: 200px;"></div>
</div>
<div></div>
<div style="height: 400px;"></div>
<div></div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<title>
Single-line row flex fragmentation: Row expansion, stretching and column balancing.
</title>
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
<style>
#flex {
display: flex;
}
#flex > div {
background: green;
width: 25px;
}
</style>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div style="width: 100px; height: 100px; background: red;">
<div style="width: 100px; columns: 2; column-gap: 0;">
<div id="flex">
<div></div>
<div>
<div style="contain: size; width: 10px; height: 70px;"></div>
<div style="contain: size; width: 10px; height: 100px;"></div>
</div>
</div>
</div>
</div>

0 comments on commit 9142fd2

Please sign in to comment.