summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/ng_layout_result.cc
blob: 66d8cbd03a89564311a21dc454c4f9d8743176c6 (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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/ng/ng_layout_result.h"

#include <memory>
#include <utility>

#include "third_party/blink/renderer/core/layout/ng/exclusions/ng_exclusion_space.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_line_box_fragment_builder.h"
#include "third_party/blink/renderer/core/layout/ng/ng_box_fragment_builder.h"
#include "third_party/blink/renderer/core/layout/ng/ng_physical_box_fragment.h"
#include "third_party/blink/renderer/core/layout/ng/ng_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 SameSizeAsNGLayoutResult : public RefCounted<SameSizeAsNGLayoutResult> {
  const NGConstraintSpace space;
  void* physical_fragment;
  union {
    NGBfcOffset bfc_offset;
    LogicalOffset oof_positioned_offset;
    void* rare_data;
  };
  LayoutUnit intrinsic_block_size;
  unsigned bitfields[1];

#if DCHECK_IS_ON()
  bool has_valid_space;
#endif
};

ASSERT_SIZE(NGLayoutResult, SameSizeAsNGLayoutResult);

}  // namespace

// static
scoped_refptr<const NGLayoutResult>
NGLayoutResult::CloneWithPostLayoutFragments(
    const NGLayoutResult& other,
    const absl::optional<PhysicalRect> updated_layout_overflow) {
  return base::AdoptRef(new NGLayoutResult(
      other, NGPhysicalBoxFragment::CloneWithPostLayoutFragments(
                 To<NGPhysicalBoxFragment>(other.PhysicalFragment()),
                 updated_layout_overflow)));
}

NGLayoutResult::NGLayoutResult(
    NGBoxFragmentBuilderPassKey passkey,
    scoped_refptr<const NGPhysicalFragment> physical_fragment,
    NGBoxFragmentBuilder* builder)
    : NGLayoutResult(std::move(physical_fragment),
                     static_cast<NGContainerFragmentBuilder*>(builder)) {
  bitfields_.is_initial_block_size_indefinite =
      builder->is_initial_block_size_indefinite_;
  bitfields_.subtree_modified_margin_strut =
      builder->subtree_modified_margin_strut_;
  intrinsic_block_size_ = builder->intrinsic_block_size_;
  if (builder->custom_layout_data_) {
    EnsureRareData()->custom_layout_data =
        std::move(builder->custom_layout_data_);
  }
  if (builder->lines_until_clamp_)
    EnsureRareData()->lines_until_clamp = *builder->lines_until_clamp_;
  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();

    if (builder->tallest_unbreakable_block_size_ >= LayoutUnit()) {
      rare_data->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_, LayoutUnit::Max());
    } else if (builder->minimal_space_shortage_ != LayoutUnit::Max()) {
      rare_data->minimal_space_shortage = builder->minimal_space_shortage_;
    }

    rare_data->has_violating_break = builder->has_violating_break_;

    if (builder->column_spanner_)
      rare_data->column_spanner = builder->column_spanner_;

    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_);
    bitfields_.has_forced_break = builder->has_forced_break_;
  }
  if (builder->table_column_count_)
    EnsureRareData()->table_column_count_ = *builder->table_column_count_;
  if (builder->math_data_.has_value())
    EnsureRareData()->math_layout_data_ = builder->math_data_;
  if (builder->grid_data_)
    EnsureRareData()->grid_layout_data_ = std::move(builder->grid_data_);
}

NGLayoutResult::NGLayoutResult(
    NGLineBoxFragmentBuilderPassKey passkey,
    scoped_refptr<const NGPhysicalFragment> physical_fragment,
    NGLineBoxFragmentBuilder* builder)
    : NGLayoutResult(std::move(physical_fragment),
                     static_cast<NGContainerFragmentBuilder*>(builder)) {}

NGLayoutResult::NGLayoutResult(NGBoxFragmentBuilderPassKey key,
                               EStatus status,
                               NGBoxFragmentBuilder* builder)
    : NGLayoutResult(/* physical_fragment */ nullptr,
                     static_cast<NGContainerFragmentBuilder*>(builder)) {
  bitfields_.status = status;
  if (builder->lines_until_clamp_)
    EnsureRareData()->lines_until_clamp = *builder->lines_until_clamp_;
  DCHECK_NE(status, kSuccess)
      << "Use the other constructor for successful layout";
}

NGLayoutResult::NGLayoutResult(const NGLayoutResult& other,
                               const NGConstraintSpace& new_space,
                               const NGMarginStrut& new_end_margin_strut,
                               LayoutUnit bfc_line_offset,
                               absl::optional<LayoutUnit> bfc_block_offset,
                               LayoutUnit block_offset_delta)
    : space_(new_space),
      physical_fragment_(other.physical_fragment_),
      intrinsic_block_size_(other.intrinsic_block_size_),
      bitfields_(other.bitfields_) {
  if (HasRareData()) {
    rare_data_ = new RareData(*other.rare_data_);
    rare_data_->bfc_line_offset = bfc_line_offset;
    rare_data_->bfc_block_offset = bfc_block_offset;
  } else if (!bitfields_.has_oof_positioned_offset) {
    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_positioned_offset_ = LogicalOffset();
  }

  NGExclusionSpace new_exclusion_space = MergeExclusionSpaces(
      other, space_.ExclusionSpace(), bfc_line_offset, block_offset_delta);

  if (new_exclusion_space != space_.ExclusionSpace()) {
    bitfields_.has_rare_data_exclusion_space = true;
    EnsureRareData()->exclusion_space = std::move(new_exclusion_space);
  } else {
    space_.ExclusionSpace().MoveDerivedGeometry(new_exclusion_space);
  }

  if (new_end_margin_strut != NGMarginStrut() || HasRareData())
    EnsureRareData()->end_margin_strut = new_end_margin_strut;

#if DCHECK_IS_ON()
  has_valid_space_ = other.has_valid_space_;
#endif
}

NGLayoutResult::NGLayoutResult(
    const NGLayoutResult& other,
    scoped_refptr<const NGPhysicalFragment> physical_fragment)
    : space_(other.space_),
      physical_fragment_(std::move(physical_fragment)),
      intrinsic_block_size_(other.intrinsic_block_size_),
      bitfields_(other.bitfields_) {
  if (HasRareData()) {
    rare_data_ = new RareData(*other.rare_data_);
  } else if (!bitfields_.has_oof_positioned_offset) {
    bfc_offset_ = other.bfc_offset_;
  } else {
    DCHECK(physical_fragment_->IsOutOfFlowPositioned());
    oof_positioned_offset_ = other.oof_positioned_offset_;
  }

  DCHECK_EQ(physical_fragment_->Size(), other.physical_fragment_->Size());

#if DCHECK_IS_ON()
  has_valid_space_ = other.has_valid_space_;
#endif
}

NGLayoutResult::NGLayoutResult(
    scoped_refptr<const NGPhysicalFragment> physical_fragment,
    NGContainerFragmentBuilder* builder)
    : space_(builder->space_ ? NGConstraintSpace(*builder->space_)
                             : NGConstraintSpace()),
      physical_fragment_(std::move(physical_fragment)),
      bitfields_(
          /* is_self_collapsing */ builder->is_self_collapsing_,
          /* is_pushed_by_floats */ builder->is_pushed_by_floats_,
          /* adjoining_object_types */ builder->adjoining_object_types_,
          /* has_descendant_that_depends_on_percentage_block_size */
          builder->has_descendant_that_depends_on_percentage_block_size_) {
#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.
    NGFragment fragment(physical_fragment_->Style().GetWritingDirection(),
                        *physical_fragment_);
    DCHECK_EQ(LayoutUnit(), fragment.BlockSize());
  }
#endif

  if (builder->end_margin_strut_ != NGMarginStrut())
    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_.ExclusionSpace()) {
    bitfields_.has_rare_data_exclusion_space = true;
    EnsureRareData()->exclusion_space = std::move(builder->exclusion_space_);
  } else {
    space_.ExclusionSpace().MoveDerivedGeometry(builder->exclusion_space_);
  }

  // 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_->BreakToken())) {
    auto* rare_data = EnsureRareData();
    rare_data->early_break = builder->early_break_;
    rare_data->early_break_appeal = builder->break_appeal_;
  }

  if (HasRareData()) {
    rare_data_->bfc_line_offset = builder->bfc_line_offset_;
    rare_data_->bfc_block_offset = builder->bfc_block_offset_;
  } else {
    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();
  }

#if DCHECK_IS_ON()
  has_valid_space_ = builder->space_;
#endif
}

NGLayoutResult::~NGLayoutResult() {
  if (HasRareData())
    delete rare_data_;
}

NGExclusionSpace NGLayoutResult::MergeExclusionSpaces(
    const NGLayoutResult& other,
    const NGExclusionSpace& new_input_exclusion_space,
    LayoutUnit bfc_line_offset,
    LayoutUnit block_offset_delta) {
  NGBfcDelta offset_delta = {bfc_line_offset - other.BfcLineOffset(),
                             block_offset_delta};

  return NGExclusionSpace::MergeExclusionSpaces(
      /* old_output */ other.ExclusionSpace(),
      /* old_input */ other.space_.ExclusionSpace(),
      /* new_input */ new_input_exclusion_space, offset_delta);
}

NGLayoutResult::RareData* NGLayoutResult::EnsureRareData() {
  if (!HasRareData()) {
    absl::optional<LayoutUnit> bfc_block_offset;
    if (!bitfields_.is_bfc_block_offset_nullopt)
      bfc_block_offset = bfc_offset_.block_offset;
    rare_data_ = new RareData(bfc_offset_.line_offset, bfc_block_offset);
    bitfields_.has_rare_data = true;
  }

  return rare_data_;
}

#if DCHECK_IS_ON()
void NGLayoutResult::CheckSameForSimplifiedLayout(
    const NGLayoutResult& other,
    bool check_same_block_size) const {
  To<NGPhysicalBoxFragment>(*physical_fragment_)
      .CheckSameForSimplifiedLayout(
          To<NGPhysicalBoxFragment>(*other.physical_fragment_),
          check_same_block_size);

  DCHECK(LinesUntilClamp() == other.LinesUntilClamp());
  ExclusionSpace().CheckSameForSimplifiedLayout(other.ExclusionSpace());

  // 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_EQ(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 NGLayoutResult::AssertSoleBoxFragment() const {
  DCHECK(physical_fragment_->IsBox());
  DCHECK(To<NGPhysicalBoxFragment>(PhysicalFragment()).IsFirstForNode());
  DCHECK(!physical_fragment_->BreakToken());
}
#endif

}  // namespace blink