1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/layout/layout_result.h"
#include <memory>
#include <utility>
#include "third_party/blink/renderer/core/dom/flat_tree_traversal.h"
#include "third_party/blink/renderer/core/layout/box_fragment_builder.h"
#include "third_party/blink/renderer/core/layout/column_spanner_path.h"
#include "third_party/blink/renderer/core/layout/exclusions/exclusion_space.h"
#include "third_party/blink/renderer/core/layout/inline/line_box_fragment_builder.h"
#include "third_party/blink/renderer/core/layout/physical_box_fragment.h"
#include "third_party/blink/renderer/core/layout/positioned_float.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/wtf/size_assertions.h"
namespace blink {
namespace {
struct SameSizeAsLayoutResult
: public GarbageCollected<SameSizeAsLayoutResult> {
const ConstraintSpace space;
Member<void*> physical_fragment;
Member<void*> rare_data_;
union {
BfcOffset bfc_offset;
BoxStrut oof_insets_for_get_computed_style;
};
LayoutUnit intrinsic_block_size;
unsigned bitfields[1];
};
ASSERT_SIZE(LayoutResult, SameSizeAsLayoutResult);
} // namespace
// static
const LayoutResult* LayoutResult::Clone(const LayoutResult& other) {
return MakeGarbageCollected<LayoutResult>(
other, PhysicalBoxFragment::Clone(
To<PhysicalBoxFragment>(other.GetPhysicalFragment())));
}
// static
const LayoutResult* LayoutResult::CloneWithPostLayoutFragments(
const LayoutResult& other) {
return MakeGarbageCollected<LayoutResult>(
other, PhysicalBoxFragment::CloneWithPostLayoutFragments(
To<PhysicalBoxFragment>(other.GetPhysicalFragment())));
}
LayoutResult::LayoutResult(BoxFragmentBuilderPassKey passkey,
const PhysicalFragment* physical_fragment,
BoxFragmentBuilder* builder)
: LayoutResult(std::move(physical_fragment),
static_cast<FragmentBuilder*>(builder)) {
bitfields_.is_initial_block_size_indefinite =
builder->is_initial_block_size_indefinite_;
intrinsic_block_size_ = builder->intrinsic_block_size_;
if (builder->custom_layout_data_) {
EnsureRareData()->custom_layout_data =
std::move(builder->custom_layout_data_);
}
if (builder->annotation_overflow_)
EnsureRareData()->annotation_overflow = builder->annotation_overflow_;
if (builder->block_end_annotation_space_) {
EnsureRareData()->block_end_annotation_space =
builder->block_end_annotation_space_;
}
if (builder->has_block_fragmentation_) {
RareData* rare_data = EnsureRareData();
rare_data->block_size_for_fragmentation =
builder->block_size_for_fragmentation_;
bitfields_.is_block_size_for_fragmentation_clamped =
builder->is_block_size_for_fragmentation_clamped_;
bitfields_.has_forced_break = builder->has_forced_break_;
}
bitfields_.is_truncated_by_fragmentation_line =
builder->is_truncated_by_fragmentation_line;
if (builder->GetConstraintSpace().ShouldPropagateChildBreakValues() &&
!builder->layout_object_->ShouldApplyLayoutContainment()) {
bitfields_.initial_break_before = static_cast<unsigned>(
builder->initial_break_before_.value_or(EBreakBetween::kAuto));
bitfields_.final_break_after =
static_cast<unsigned>(builder->previous_break_after_);
}
if (builder->table_column_count_) {
EnsureRareData()->EnsureTableData()->table_column_count =
*builder->table_column_count_;
}
if (builder->math_italic_correction_) {
EnsureRareData()->EnsureMathData()->italic_correction =
builder->math_italic_correction_;
}
if (builder->grid_layout_data_) {
EnsureRareData()->EnsureGridData()->grid_layout_data =
std::move(builder->grid_layout_data_);
}
if (builder->flex_layout_data_) {
EnsureRareData()->EnsureFlexData()->flex_layout_data =
std::move(builder->flex_layout_data_);
}
}
LayoutResult::LayoutResult(LineBoxFragmentBuilderPassKey passkey,
const PhysicalFragment* physical_fragment,
LineBoxFragmentBuilder* builder)
: LayoutResult(std::move(physical_fragment),
static_cast<FragmentBuilder*>(builder)) {
DCHECK_EQ(builder->bfc_block_offset_.has_value(),
builder->line_box_bfc_block_offset_.has_value());
if (builder->bfc_block_offset_ != builder->line_box_bfc_block_offset_) {
EnsureRareData()->SetLineBoxBfcBlockOffset(
*builder->line_box_bfc_block_offset_);
}
// `EnsureLineData()` must be done before `EnsureLineSmallData()`.
DCHECK(!rare_data_ || !rare_data_->HasData(RareData::kLineSmallData));
if (builder->annotation_block_offset_adjustment_) {
EnsureRareData()->EnsureLineData()->annotation_block_offset_adjustment =
builder->annotation_block_offset_adjustment_;
}
if (builder->clearance_after_line_) {
EnsureRareData()->EnsureLineSmallData()->clearance_after_line =
*builder->clearance_after_line_;
}
if (builder->trim_block_end_by_) {
EnsureRareData()->EnsureLineSmallData()->trim_block_end_by =
*builder->trim_block_end_by_;
}
}
LayoutResult::LayoutResult(FragmentBuilderPassKey key,
EStatus status,
FragmentBuilder* builder)
: LayoutResult(/* physical_fragment */ nullptr, builder) {
bitfields_.status = status;
DCHECK_NE(status, kSuccess)
<< "Use the other constructor for successful layout";
}
LayoutResult::LayoutResult(const LayoutResult& other,
const ConstraintSpace& new_space,
const MarginStrut& new_end_margin_strut,
LayoutUnit bfc_line_offset,
std::optional<LayoutUnit> bfc_block_offset,
LayoutUnit block_offset_delta)
: space_(new_space),
physical_fragment_(other.physical_fragment_),
rare_data_(other.rare_data_
? MakeGarbageCollected<RareData>(*other.rare_data_)
: nullptr),
intrinsic_block_size_(other.intrinsic_block_size_),
bitfields_(other.bitfields_) {
if (!bitfields_.has_oof_insets_for_get_computed_style) {
bfc_offset_.line_offset = bfc_line_offset;
bfc_offset_.block_offset = bfc_block_offset.value_or(LayoutUnit());
bitfields_.is_bfc_block_offset_nullopt = !bfc_block_offset.has_value();
} else {
DCHECK(physical_fragment_->IsOutOfFlowPositioned());
DCHECK_EQ(bfc_line_offset, LayoutUnit());
DCHECK(bfc_block_offset && bfc_block_offset.value() == LayoutUnit());
oof_insets_for_get_computed_style_ = BoxStrut();
}
ExclusionSpace new_exclusion_space = MergeExclusionSpaces(
other, space_.GetExclusionSpace(), bfc_line_offset, block_offset_delta);
if (new_exclusion_space != space_.GetExclusionSpace()) {
bitfields_.has_rare_data_exclusion_space = true;
EnsureRareData()->exclusion_space = std::move(new_exclusion_space);
} else {
space_.GetExclusionSpace().MoveDerivedGeometry(new_exclusion_space);
}
if (new_end_margin_strut != MarginStrut() || rare_data_) {
EnsureRareData()->end_margin_strut = new_end_margin_strut;
}
}
LayoutResult::LayoutResult(const LayoutResult& other,
const PhysicalFragment* physical_fragment)
: space_(other.space_),
physical_fragment_(std::move(physical_fragment)),
rare_data_(other.rare_data_
? MakeGarbageCollected<RareData>(*other.rare_data_)
: nullptr),
intrinsic_block_size_(other.intrinsic_block_size_),
bitfields_(other.bitfields_) {
if (!bitfields_.has_oof_insets_for_get_computed_style) {
bfc_offset_ = other.bfc_offset_;
} else {
DCHECK(physical_fragment_->IsOutOfFlowPositioned());
oof_insets_for_get_computed_style_ =
other.oof_insets_for_get_computed_style_;
}
DCHECK_EQ(physical_fragment_->Size(), other.physical_fragment_->Size());
}
LayoutResult::LayoutResult(const PhysicalFragment* physical_fragment,
FragmentBuilder* builder)
: space_(builder->space_),
physical_fragment_(std::move(physical_fragment)),
rare_data_(nullptr),
bitfields_(builder->is_self_collapsing_,
builder->is_pushed_by_floats_,
builder->adjoining_object_types_,
builder->has_descendant_that_depends_on_percentage_block_size_,
builder->subtree_modified_margin_strut_) {
#if DCHECK_IS_ON()
if (bitfields_.is_self_collapsing && physical_fragment_) {
// A new formatting-context shouldn't be self-collapsing.
DCHECK(!physical_fragment_->IsFormattingContextRoot());
// Self-collapsing children must have a block-size of zero.
LogicalFragment fragment(physical_fragment_->Style().GetWritingDirection(),
*physical_fragment_);
DCHECK_EQ(LayoutUnit(), fragment.BlockSize());
}
#endif
if (builder->end_margin_strut_ != MarginStrut()) {
EnsureRareData()->end_margin_strut = builder->end_margin_strut_;
}
if (builder->annotation_overflow_ > LayoutUnit())
EnsureRareData()->annotation_overflow = builder->annotation_overflow_;
if (builder->block_end_annotation_space_) {
EnsureRareData()->block_end_annotation_space =
builder->block_end_annotation_space_;
}
if (builder->exclusion_space_ != space_.GetExclusionSpace()) {
bitfields_.has_rare_data_exclusion_space = true;
EnsureRareData()->exclusion_space = std::move(builder->exclusion_space_);
} else {
space_.GetExclusionSpace().MoveDerivedGeometry(builder->exclusion_space_);
}
if (builder->lines_until_clamp_) {
EnsureRareData()->lines_until_clamp = *builder->lines_until_clamp_;
}
if (builder->is_block_end_trimmable_line_) {
EnsureRareData()->set_is_block_end_trimmable_line();
}
if (builder->would_be_last_line_if_not_for_ellipsis_) {
EnsureRareData()->set_would_be_last_line_if_not_for_ellipsis();
}
if (builder->tallest_unbreakable_block_size_ >= LayoutUnit()) {
EnsureRareData()->tallest_unbreakable_block_size =
builder->tallest_unbreakable_block_size_;
// This field shares storage with "minimal space shortage", so both cannot
// be set at the same time.
DCHECK_EQ(builder->minimal_space_shortage_, kIndefiniteSize);
} else if (builder->minimal_space_shortage_ != kIndefiniteSize) {
EnsureRareData()->minimal_space_shortage = builder->minimal_space_shortage_;
}
// If we produced a fragment that we didn't break inside, provide the best
// early possible breakpoint that we found inside. This early breakpoint will
// be propagated to the container for further consideration. If we didn't
// produce a fragment, on the other hand, it means that we're going to
// re-layout now, and break at the early breakpoint (i.e. the status is
// kNeedsEarlierBreak).
if (builder->early_break_ &&
(!physical_fragment_ || !physical_fragment_->GetBreakToken())) {
EnsureRareData()->early_break = builder->early_break_;
}
if (builder->column_spanner_path_) {
EnsureRareData()->column_spanner_path = builder->column_spanner_path_;
bitfields_.is_empty_spanner_parent = builder->is_empty_spanner_parent_;
}
bitfields_.break_appeal = builder->break_appeal_;
bitfields_.should_force_same_fragmentation_flow =
builder->should_force_same_fragmentation_flow_;
bitfields_.has_orthogonal_fallback_size_descendant =
builder->has_orthogonal_fallback_size_descendant_;
bfc_offset_.line_offset = builder->bfc_line_offset_;
bfc_offset_.block_offset = builder->bfc_block_offset_.value_or(LayoutUnit());
bitfields_.is_bfc_block_offset_nullopt =
!builder->bfc_block_offset_.has_value();
}
ExclusionSpace LayoutResult::MergeExclusionSpaces(
const LayoutResult& other,
const ExclusionSpace& new_input_exclusion_space,
LayoutUnit bfc_line_offset,
LayoutUnit block_offset_delta) {
BfcDelta offset_delta = {bfc_line_offset - other.BfcLineOffset(),
block_offset_delta};
return ExclusionSpace::MergeExclusionSpaces(
/* old_output */ other.GetExclusionSpace(),
/* old_input */ other.space_.GetExclusionSpace(),
/* new_input */ new_input_exclusion_space, offset_delta);
}
LayoutResult::RareData* LayoutResult::EnsureRareData() {
if (!rare_data_) {
rare_data_ = MakeGarbageCollected<RareData>();
}
return rare_data_.Get();
}
void LayoutResult::CopyMutableOutOfFlowData(const LayoutResult& other) const {
if (bitfields_.has_oof_insets_for_get_computed_style) {
return;
}
GetMutableForOutOfFlow().SetOutOfFlowInsetsForGetComputedStyle(
other.OutOfFlowInsetsForGetComputedStyle());
GetMutableForOutOfFlow().SetOutOfFlowPositionedOffset(
other.OutOfFlowPositionedOffset());
}
void LayoutResult::MutableForOutOfFlow::SetAccessibilityAnchor(
Element* anchor) {
if (layout_result_->rare_data_ || anchor) {
layout_result_->EnsureRareData()->accessibility_anchor = anchor;
}
}
void LayoutResult::MutableForOutOfFlow::SetDisplayLocksAffectedByAnchors(
GCedHeapHashSet<Member<Element>>* display_locks) {
if (layout_result_->rare_data_ || display_locks) {
layout_result_->EnsureRareData()->display_locks_affected_by_anchors =
display_locks;
}
}
#if DCHECK_IS_ON()
void LayoutResult::CheckSameForSimplifiedLayout(
const LayoutResult& other,
bool check_same_block_size,
bool check_no_fragmentation) const {
To<PhysicalBoxFragment>(*physical_fragment_)
.CheckSameForSimplifiedLayout(
To<PhysicalBoxFragment>(*other.physical_fragment_),
check_same_block_size, check_no_fragmentation);
DCHECK(LinesUntilClamp() == other.LinesUntilClamp());
GetExclusionSpace().CheckSameForSimplifiedLayout(other.GetExclusionSpace());
// We ignore |BfcBlockOffset|, and |BfcLineOffset| as "simplified" layout
// will move the layout result if required.
// We ignore the |intrinsic_block_size_| as if a scrollbar gets added/removed
// this may change (even if the size of the fragment remains the same).
DCHECK(EndMarginStrut() == other.EndMarginStrut());
DCHECK(MinimalSpaceShortage() == other.MinimalSpaceShortage());
DCHECK_EQ(TableColumnCount(), other.TableColumnCount());
DCHECK_EQ(bitfields_.has_forced_break, other.bitfields_.has_forced_break);
DCHECK_EQ(bitfields_.is_self_collapsing, other.bitfields_.is_self_collapsing);
DCHECK_EQ(bitfields_.is_pushed_by_floats,
other.bitfields_.is_pushed_by_floats);
DCHECK_EQ(bitfields_.adjoining_object_types,
other.bitfields_.adjoining_object_types);
DCHECK_EQ(bitfields_.subtree_modified_margin_strut,
other.bitfields_.subtree_modified_margin_strut);
DCHECK_EQ(CustomLayoutData(), other.CustomLayoutData());
DCHECK_EQ(bitfields_.initial_break_before,
other.bitfields_.initial_break_before);
DCHECK_EQ(bitfields_.final_break_after, other.bitfields_.final_break_after);
DCHECK_EQ(
bitfields_.has_descendant_that_depends_on_percentage_block_size,
other.bitfields_.has_descendant_that_depends_on_percentage_block_size);
DCHECK_EQ(bitfields_.status, other.bitfields_.status);
}
#endif
#if DCHECK_IS_ON()
void LayoutResult::AssertSoleBoxFragment() const {
DCHECK(physical_fragment_->IsBox());
DCHECK(To<PhysicalBoxFragment>(GetPhysicalFragment()).IsFirstForNode());
DCHECK(!physical_fragment_->GetBreakToken());
}
#endif
void LayoutResult::Trace(Visitor* visitor) const {
visitor->Trace(space_);
visitor->Trace(physical_fragment_);
visitor->Trace(rare_data_);
}
void LayoutResult::RareData::Trace(Visitor* visitor) const {
visitor->Trace(early_break);
visitor->Trace(non_overflowing_scroll_ranges);
visitor->Trace(column_spanner_path);
visitor->Trace(exclusion_space);
visitor->Trace(accessibility_anchor);
visitor->Trace(display_locks_affected_by_anchors);
}
} // namespace blink
|