Skip to content

Commit

Permalink
[layout] Refactor min/max sizes calls.
Browse files Browse the repository at this point in the history
This shuffles some code around related to the min/max sizes cache
logic. There shouldn't be any observable behaviour change.

Bug: 1272533
Change-Id: I2285a9aaa1ae508c3b6c16f85e76a1832cc603ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5037737
Reviewed-by: Alison Maher <almaher@microsoft.com>
Commit-Queue: Ian Kilpatrick <ikilpatrick@chromium.org>
Reviewed-by: Kurt Catti-Schmidt <kschmi@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1227074}
  • Loading branch information
bfgeek authored and Chromium LUCI CQ committed Nov 20, 2023
1 parent bb78d90 commit 65717c8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
50 changes: 27 additions & 23 deletions third_party/blink/renderer/core/layout/layout_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -1179,45 +1179,49 @@ class CORE_EXPORT LayoutBox : public LayoutBoxModelObject {
: PhysicalRect(PhysicalOffset(), PreviousSize());
}

// Returns cached intrinsic logical widths for this layout box.
MinMaxSizes CachedIntrinsicLogicalWidths() const {
// Returns the cached intrinsic logical widths when no children depend on the
// block constraints.
MinMaxSizesResult CachedIndefiniteIntrinsicLogicalWidths() const {
NOT_DESTROYED();
DCHECK(!IntrinsicLogicalWidthsDirty());
return intrinsic_logical_widths_;
DCHECK(!IntrinsicLogicalWidthsChildDependsOnBlockConstraints());
return {intrinsic_logical_widths_,
IntrinsicLogicalWidthsDependsOnBlockConstraints()};
}

// Returns the cached intrinsic logical widths if the initial block-size
// matches.
absl::optional<MinMaxSizesResult> CachedIntrinsicLogicalWidths(
LayoutUnit initial_block_size) const {
NOT_DESTROYED();
DCHECK(!IntrinsicLogicalWidthsDirty());
if (initial_block_size == intrinsic_logical_widths_initial_block_size_) {
return MinMaxSizesResult{
intrinsic_logical_widths_,
IntrinsicLogicalWidthsDependsOnBlockConstraints()};
}
return absl::nullopt;
}

// LayoutNG can use this function to update our cache of intrinsic logical
// widths when the layout object is managed by NG. Should not be called by
// regular code.
//
// Also clears the "dirty" flag for the intrinsic logical widths.
void SetIntrinsicLogicalWidthsFromNG(
LayoutUnit intrinsic_logical_widths_initial_block_size,
bool depends_on_block_constraints,
bool child_depends_on_block_constraints,
const MinMaxSizes& sizes) {
NOT_DESTROYED();
intrinsic_logical_widths_initial_block_size_ =
intrinsic_logical_widths_initial_block_size;
void SetIntrinsicLogicalWidths(LayoutUnit initial_block_size,
bool depends_on_block_constraints,
bool child_depends_on_block_constraints,
const MinMaxSizes& sizes) {
NOT_DESTROYED();
intrinsic_logical_widths_ = sizes;
intrinsic_logical_widths_initial_block_size_ = initial_block_size;
SetIntrinsicLogicalWidthsDependsOnBlockConstraints(
depends_on_block_constraints);
SetIntrinsicLogicalWidthsChildDependsOnBlockConstraints(
child_depends_on_block_constraints);
intrinsic_logical_widths_ = sizes;
ClearIntrinsicLogicalWidthsDirty();
}

// Returns what initial block-size was used in the intrinsic logical widths
// phase. This is used for caching purposes when %-block-size children with
// aspect-ratios are present.
//
// For non-LayoutNG code this is always LayoutUnit::Min(), and should not be
// used for caching purposes.
LayoutUnit IntrinsicLogicalWidthsInitialBlockSize() const {
NOT_DESTROYED();
return intrinsic_logical_widths_initial_block_size_;
}

// Make it public.
using LayoutObject::BackgroundIsKnownToBeObscured;

Expand Down
13 changes: 13 additions & 0 deletions third_party/blink/renderer/core/layout/min_max_sizes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ struct CORE_EXPORT MinMaxSizes {

CORE_EXPORT std::ostream& operator<<(std::ostream&, const MinMaxSizes&);

// The output of the min/max inline size calculation algorithm. Contains the
// min/max sizes, and if this calculation will change if the block constraints
// change.
struct MinMaxSizesResult {
MinMaxSizesResult() = default;
MinMaxSizesResult(MinMaxSizes sizes, bool depends_on_block_constraints)
: sizes(sizes),
depends_on_block_constraints(depends_on_block_constraints) {}

MinMaxSizes sizes;
bool depends_on_block_constraints = false;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_MIN_MAX_SIZES_H_
31 changes: 14 additions & 17 deletions third_party/blink/renderer/core/layout/ng/ng_block_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -974,21 +974,18 @@ MinMaxSizesResult BlockNode::ComputeMinMaxSizes(
bool can_use_cached_intrinsic_inline_sizes =
CanUseCachedIntrinsicInlineSizes(constraint_space, float_input, *this);

// Ensure the cache is invalid if we know we can't use our cached sizes.
if (!can_use_cached_intrinsic_inline_sizes) {
box_->SetIntrinsicLogicalWidthsDirty(kMarkOnlyThis);
}

// Use our cached sizes if we don't have a descendant which depends on our
// block constraints.
if (can_use_cached_intrinsic_inline_sizes &&
!box_->IntrinsicLogicalWidthsChildDependsOnBlockConstraints()) {
MinMaxSizes sizes = box_->CachedIntrinsicLogicalWidths();
bool depends_on_block_constraints =
box_->IntrinsicLogicalWidthsDependsOnBlockConstraints();
return MinMaxSizesResult(sizes, depends_on_block_constraints);
return box_->CachedIndefiniteIntrinsicLogicalWidths();
}

// Determine if we are dependent on the block-constraints.
bool self_depends_on_block_constraints =
DependsOnBlockConstraints() ||
UseParentPercentageResolutionBlockSizeForChildren();

const FragmentGeometry fragment_geometry = CalculateInitialFragmentGeometry(
constraint_space, *this, /* break_token */ nullptr,
/* is_intrinsic */ true);
Expand All @@ -998,15 +995,12 @@ MinMaxSizesResult BlockNode::ComputeMinMaxSizes(
// We might still be able to use the cached values if our children don't
// depend on the *input* %-block-size.
if (can_use_cached_intrinsic_inline_sizes &&
initial_block_size == box_->IntrinsicLogicalWidthsInitialBlockSize() &&
!UseParentPercentageResolutionBlockSizeForChildren()) {
DCHECK(box_->IntrinsicLogicalWidthsChildDependsOnBlockConstraints());
MinMaxSizes sizes = box_->CachedIntrinsicLogicalWidths();
return MinMaxSizesResult(sizes, self_depends_on_block_constraints);
if (auto result = box_->CachedIntrinsicLogicalWidths(initial_block_size)) {
return *result;
}
}

box_->SetIntrinsicLogicalWidthsDirty(kMarkOnlyThis);

const BoxStrut border_padding =
fragment_geometry.border + fragment_geometry.padding;

Expand All @@ -1017,8 +1011,11 @@ MinMaxSizesResult BlockNode::ComputeMinMaxSizes(
if (auto min_size = ContentMinimumInlineSize(*this, border_padding))
result.sizes.min_size = *min_size;

// Determine if we are dependent on the block-constraints.
bool depends_on_block_constraints =
self_depends_on_block_constraints && result.depends_on_block_constraints;
(DependsOnBlockConstraints() ||
UseParentPercentageResolutionBlockSizeForChildren()) &&
result.depends_on_block_constraints;

if (!Style().AspectRatio().IsAuto() &&
BlockLengthUnresolvable(constraint_space, Style().LogicalHeight())) {
Expand All @@ -1035,7 +1032,7 @@ MinMaxSizesResult BlockNode::ComputeMinMaxSizes(
Style().LogicalMaxHeight().IsPercentOrCalcOrStretch();
}

box_->SetIntrinsicLogicalWidthsFromNG(
box_->SetIntrinsicLogicalWidths(
initial_block_size, depends_on_block_constraints,
/* child_depends_on_block_constraints */
result.depends_on_block_constraints, result.sizes);
Expand Down
14 changes: 0 additions & 14 deletions third_party/blink/renderer/core/layout/ng/ng_layout_input_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ComputedStyle;
class Document;
class LayoutObject;
class LayoutBox;
struct MinMaxSizes;
struct PhysicalSize;

// The input to the min/max inline size calculation algorithm for child nodes.
Expand All @@ -36,19 +35,6 @@ struct MinMaxSizesFloatInput {
LayoutUnit float_right_inline_size;
};

// The output of the min/max inline size calculation algorithm. Contains the
// min/max sizes, and if this calculation will change if the block constraints
// change.
struct MinMaxSizesResult {
MinMaxSizesResult() = default;
MinMaxSizesResult(MinMaxSizes sizes, bool depends_on_block_constraints)
: sizes(sizes),
depends_on_block_constraints(depends_on_block_constraints) {}

MinMaxSizes sizes;
bool depends_on_block_constraints = false;
};

// Represents the input to a layout algorithm for a given node. The layout
// engine should use the style, node type to determine which type of layout
// algorithm to use to produce fragments for this node.
Expand Down

0 comments on commit 65717c8

Please sign in to comment.