Commit 6196fe44 authored by Ian Kilpatrick's avatar Ian Kilpatrick Committed by Commit Bot

[LayoutNG] Optimize length calculation calls.

I noticed inside the stack-sampled metrics, we were burning a bunch of
time hopping through these calls.

Bug: 635619
Change-Id: Ic7788a666aafdf0630fd441e2762e7b7d4302879
Reviewed-on: https://chromium-review.googlesource.com/c/1489303Reviewed-by: default avatarChristian Biesinger <cbiesinger@chromium.org>
Reviewed-by: default avatarKoji Ishii <kojii@chromium.org>
Reviewed-by: default avatarMorten Stenshorne <mstensho@chromium.org>
Commit-Queue: Ian Kilpatrick <ikilpatrick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635800}
parent e230f2ca
......@@ -105,16 +105,18 @@ NGStaticPosition LayoutBoxUtils::ComputeStaticPositionFromLegacy(
if (parent_style->IsLeftToRightDirection()) {
if (!logical_left.IsAuto()) {
static_inline =
ValueForLength(logical_left, containing_block_logical_width);
MinimumValueForLength(logical_left, containing_block_logical_width);
}
} else {
if (!logical_right.IsAuto()) {
static_inline =
ValueForLength(logical_right, containing_block_logical_width);
MinimumValueForLength(logical_right, containing_block_logical_width);
}
}
if (!logical_top.IsAuto())
static_block = ValueForLength(logical_top, containing_block_logical_height);
if (!logical_top.IsAuto()) {
static_block =
MinimumValueForLength(logical_top, containing_block_logical_height);
}
// Legacy static position is relative to padding box. Convert to border box.
// Also flip offsets as necessary to make them relative to to the left/top
......
......@@ -85,6 +85,13 @@ LayoutUnit ResolveHeight(const NGConstraintSpace& space,
computed_height, type, LengthResolvePhase::kLayout);
}
inline LayoutUnit ResolveMargin(const NGConstraintSpace& space,
const Length& length) {
DCHECK(!length.IsAuto());
return MinimumValueForLength(
length, space.PercentageResolutionInlineSizeForParentWritingMode());
}
// Available size can is maximum length Element can have without overflowing
// container bounds. The position of Element's edges will determine
// how much space there is available.
......@@ -153,16 +160,16 @@ void ComputeAbsoluteHorizontal(const NGConstraintSpace& space,
base::Optional<LayoutUnit> margin_left;
if (!style.MarginLeft().IsAuto())
margin_left = ResolveMarginPaddingLength(space, style.MarginLeft());
margin_left = ResolveMargin(space, style.MarginLeft());
base::Optional<LayoutUnit> margin_right;
if (!style.MarginRight().IsAuto())
margin_right = ResolveMarginPaddingLength(space, style.MarginRight());
margin_right = ResolveMargin(space, style.MarginRight());
base::Optional<LayoutUnit> left;
if (!style.Left().IsAuto())
left = ValueForLength(style.Left(), percentage_width);
left = MinimumValueForLength(style.Left(), percentage_width);
base::Optional<LayoutUnit> right;
if (!style.Right().IsAuto())
right = ValueForLength(style.Right(), percentage_width);
right = MinimumValueForLength(style.Right(), percentage_width);
base::Optional<LayoutUnit> width = incoming_width;
NGPhysicalSize container_size =
ToNGPhysicalSize(space.AvailableSize(), space.GetWritingMode());
......@@ -323,16 +330,16 @@ void ComputeAbsoluteVertical(const NGConstraintSpace& space,
base::Optional<LayoutUnit> margin_top;
if (!style.MarginTop().IsAuto())
margin_top = ResolveMarginPaddingLength(space, style.MarginTop());
margin_top = ResolveMargin(space, style.MarginTop());
base::Optional<LayoutUnit> margin_bottom;
if (!style.MarginBottom().IsAuto())
margin_bottom = ResolveMarginPaddingLength(space, style.MarginBottom());
margin_bottom = ResolveMargin(space, style.MarginBottom());
base::Optional<LayoutUnit> top;
if (!style.Top().IsAuto())
top = ValueForLength(style.Top(), percentage_height);
top = MinimumValueForLength(style.Top(), percentage_height);
base::Optional<LayoutUnit> bottom;
if (!style.Bottom().IsAuto())
bottom = ValueForLength(style.Bottom(), percentage_height);
bottom = MinimumValueForLength(style.Bottom(), percentage_height);
base::Optional<LayoutUnit> height = incoming_height;
NGPhysicalSize container_size =
......
......@@ -15,6 +15,7 @@
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/geometry/length.h"
#include "third_party/blink/renderer/platform/geometry/length_functions.h"
namespace blink {
......@@ -119,7 +120,8 @@ LayoutUnit ResolveInlineLength(const NGConstraintSpace& constraint_space,
case Length::kCalculated: {
LayoutUnit percentage_resolution_size =
constraint_space.PercentageResolutionInlineSize();
LayoutUnit value = ValueForLength(length, percentage_resolution_size);
LayoutUnit value =
MinimumValueForLength(length, percentage_resolution_size);
if (style.BoxSizing() == EBoxSizing::kContentBox) {
value += border_padding.InlineSum();
} else {
......@@ -235,7 +237,7 @@ LayoutUnit ResolveBlockLength(
? *opt_percentage_resolution_block_size_for_min_max
: constraint_space.PercentageResolutionBlockSize();
LayoutUnit value =
ValueForLength(length, percentage_resolution_block_size);
MinimumValueForLength(length, percentage_resolution_block_size);
// Percentage-sized children of table cells, in the table "layout" phase,
// pretend they have box-sizing: border-box.
......@@ -277,35 +279,6 @@ LayoutUnit ResolveBlockLength(
}
}
LayoutUnit ResolveMarginPaddingLength(LayoutUnit percentage_resolution_size,
const Length& length) {
DCHECK_GE(percentage_resolution_size, LayoutUnit());
// Margins and padding always get computed relative to the inline size:
// https://www.w3.org/TR/CSS2/box.html#value-def-margin-width
// https://www.w3.org/TR/CSS2/box.html#value-def-padding-width
switch (length.GetType()) {
case Length::kAuto:
return LayoutUnit();
case Length::kPercent:
case Length::kFixed:
case Length::kCalculated:
return ValueForLength(length, percentage_resolution_size);
case Length::kMinContent:
case Length::kMaxContent:
case Length::kFillAvailable:
case Length::kFitContent:
case Length::kExtendToZoom:
case Length::kDeviceWidth:
case Length::kDeviceHeight:
case Length::kMaxSizeNone:
FALLTHROUGH;
default:
NOTREACHED();
return LayoutUnit();
}
}
MinMaxSize ComputeMinAndMaxContentContribution(
WritingMode parent_writing_mode,
const ComputedStyle& style,
......@@ -782,16 +755,11 @@ NGPhysicalBoxStrut ComputePhysicalMargins(
if (!style.HasMargin())
return NGPhysicalBoxStrut();
NGPhysicalBoxStrut physical_dim;
physical_dim.left = ResolveMarginPaddingLength(percentage_resolution_size,
style.MarginLeft());
physical_dim.right = ResolveMarginPaddingLength(percentage_resolution_size,
style.MarginRight());
physical_dim.top =
ResolveMarginPaddingLength(percentage_resolution_size, style.MarginTop());
physical_dim.bottom = ResolveMarginPaddingLength(percentage_resolution_size,
style.MarginBottom());
return physical_dim;
return {
MinimumValueForLength(style.MarginTop(), percentage_resolution_size),
MinimumValueForLength(style.MarginRight(), percentage_resolution_size),
MinimumValueForLength(style.MarginBottom(), percentage_resolution_size),
MinimumValueForLength(style.MarginLeft(), percentage_resolution_size)};
}
NGBoxStrut ComputeMarginsFor(const NGConstraintSpace& constraint_space,
......@@ -891,15 +859,11 @@ NGBoxStrut ComputePadding(const NGConstraintSpace& constraint_space,
LayoutUnit percentage_resolution_size =
constraint_space.PercentageResolutionInlineSizeForParentWritingMode();
NGBoxStrut padding;
padding.inline_start = ResolveMarginPaddingLength(percentage_resolution_size,
style.PaddingStart());
padding.inline_end = ResolveMarginPaddingLength(percentage_resolution_size,
style.PaddingEnd());
padding.block_start = ResolveMarginPaddingLength(percentage_resolution_size,
style.PaddingBefore());
padding.block_end = ResolveMarginPaddingLength(percentage_resolution_size,
style.PaddingAfter());
NGBoxStrut padding = {
MinimumValueForLength(style.PaddingStart(), percentage_resolution_size),
MinimumValueForLength(style.PaddingEnd(), percentage_resolution_size),
MinimumValueForLength(style.PaddingBefore(), percentage_resolution_size),
MinimumValueForLength(style.PaddingAfter(), percentage_resolution_size)};
if (style.Display() == EDisplay::kTableCell) {
// Compatibility hack to mach legacy layout. Legacy layout floors padding on
......
......@@ -92,18 +92,6 @@ CORE_EXPORT LayoutUnit ResolveBlockLength(
const LayoutUnit* opt_percentage_resolution_block_size_for_min_max =
nullptr);
// Convert margin/border/padding length to a layout unit using the
// given constraint space.
LayoutUnit ResolveMarginPaddingLength(LayoutUnit percentage_resolution_size,
const Length&);
inline LayoutUnit ResolveMarginPaddingLength(
const NGConstraintSpace& constraint_space,
const Length& length) {
LayoutUnit percentage_resolution_size =
constraint_space.PercentageResolutionInlineSizeForParentWritingMode();
return ResolveMarginPaddingLength(percentage_resolution_size, length);
}
// For the given style and min/max content sizes, computes the min and max
// content contribution (https://drafts.csswg.org/css-sizing/#contributions).
// This is similar to ComputeInlineSizeForFragment except that it does not
......
......@@ -24,13 +24,13 @@ NGPhysicalOffset ComputeRelativeOffset(const ComputedStyle& child_style,
base::Optional<LayoutUnit> left, right, top, bottom;
if (!child_style.Left().IsAuto())
left = ValueForLength(child_style.Left(), container_size.width);
left = MinimumValueForLength(child_style.Left(), container_size.width);
if (!child_style.Right().IsAuto())
right = ValueForLength(child_style.Right(), container_size.width);
right = MinimumValueForLength(child_style.Right(), container_size.width);
if (!child_style.Top().IsAuto())
top = ValueForLength(child_style.Top(), container_size.height);
top = MinimumValueForLength(child_style.Top(), container_size.height);
if (!child_style.Bottom().IsAuto())
bottom = ValueForLength(child_style.Bottom(), container_size.height);
bottom = MinimumValueForLength(child_style.Bottom(), container_size.height);
// Common case optimization
if (!left && !right && !top && !bottom)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment