summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/block_painter.cc
blob: 652d4bc55eb15668acc5217480603cdc867a0188 (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
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
// Copyright 2014 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/paint/block_painter.h"

#include "base/optional.h"
#include "third_party/blink/renderer/core/display_lock/display_lock_context.h"
#include "third_party/blink/renderer/core/editing/drag_caret.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_api_shim.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_box.h"
#include "third_party/blink/renderer/core/layout/layout_inline.h"
#include "third_party/blink/renderer/core/layout/ng/layout_ng_block_flow.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/paint/line_box_list_painter.h"
#include "third_party/blink/renderer/core/paint/object_painter.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/core/paint/paint_timing_detector.h"
#include "third_party/blink/renderer/core/paint/scoped_paint_state.h"
#include "third_party/blink/renderer/core/paint/scrollable_area_painter.h"

namespace blink {

DISABLE_CFI_PERF
void BlockPainter::Paint(const PaintInfo& paint_info) {
  ScopedPaintState paint_state(layout_block_, paint_info);
  if (!ShouldPaint(paint_state))
    return;

  DCHECK(!layout_block_.ChildPaintBlockedByDisplayLock() ||
         paint_info.DescendantPaintingBlocked());

  auto paint_offset = paint_state.PaintOffset();
  auto& local_paint_info = paint_state.MutablePaintInfo();
  PaintPhase original_phase = local_paint_info.phase;

  if (original_phase == PaintPhase::kOutline) {
    local_paint_info.phase = PaintPhase::kDescendantOutlinesOnly;
  } else if (ShouldPaintSelfBlockBackground(original_phase)) {
    local_paint_info.phase = PaintPhase::kSelfBlockBackgroundOnly;
    // With CompositeAfterPaint we need to call PaintObject twice: once for the
    // background painting that does not scroll, and a second time for the
    // background painting that scrolls.
    // Without CompositeAfterPaint, this happens as the main graphics layer
    // paints the background, and then the scrolling contents graphics layer
    // paints the background.
    if (RuntimeEnabledFeatures::CompositeAfterPaintEnabled()) {
      auto paint_location = layout_block_.GetBackgroundPaintLocation();
      if (!(paint_location & kBackgroundPaintInGraphicsLayer))
        local_paint_info.SetSkipsBackground(true);
      layout_block_.PaintObject(local_paint_info, paint_offset);
      local_paint_info.SetSkipsBackground(false);

      if (paint_location & kBackgroundPaintInScrollingContents) {
        local_paint_info.SetIsPaintingScrollingBackground(true);
        layout_block_.PaintObject(local_paint_info, paint_offset);
        local_paint_info.SetIsPaintingScrollingBackground(false);
      }
    } else {
      layout_block_.PaintObject(local_paint_info, paint_offset);
    }
    if (ShouldPaintDescendantBlockBackgrounds(original_phase))
      local_paint_info.phase = PaintPhase::kDescendantBlockBackgroundsOnly;
  }

  if (original_phase == PaintPhase::kMask) {
    layout_block_.PaintObject(local_paint_info, paint_offset);
  } else if (original_phase != PaintPhase::kSelfBlockBackgroundOnly &&
             original_phase != PaintPhase::kSelfOutlineOnly &&
             // For now all scrollers with overlay overflow controls are
             // self-painting layers, so we don't need to traverse descendants
             // here.
             original_phase != PaintPhase::kOverlayOverflowControls) {
    ScopedBoxContentsPaintState contents_paint_state(paint_state,
                                                     layout_block_);
    layout_block_.PaintObject(contents_paint_state.GetPaintInfo(),
                              contents_paint_state.PaintOffset());
  }

  // Carets are painted in the foreground phase, outside of the contents
  // properties block. Note that caret painting does not seem to correspond to
  // any painting order steps within the CSS spec.
  if (original_phase == PaintPhase::kForeground &&
      layout_block_.ShouldPaintCarets()) {
    // Apply overflow clip if needed. TODO(wangxianzhu): Move PaintCarets()
    // under |contents_paint_state| in the above block and let the caret
    // painters paint in the space of scrolling contents.
    base::Optional<ScopedPaintChunkProperties> paint_chunk_properties;
    if (const auto* fragment = paint_state.FragmentToPaint()) {
      if (const auto* properties = fragment->PaintProperties()) {
        if (const auto* overflow_clip = properties->OverflowClip()) {
          paint_chunk_properties.emplace(
              paint_info.context.GetPaintController(), *overflow_clip,
              layout_block_, DisplayItem::kCaret);
        }
      }
    }

    PaintCarets(paint_info, paint_offset);
  }

  if (ShouldPaintSelfOutline(original_phase)) {
    local_paint_info.phase = PaintPhase::kSelfOutlineOnly;
    layout_block_.PaintObject(local_paint_info, paint_offset);
  }

  // We paint scrollbars after we painted the other things, so that the
  // scrollbars will sit above them.
  local_paint_info.phase = original_phase;
  if (auto* scrollable_area = layout_block_.GetScrollableArea()) {
    ScrollableAreaPainter(*scrollable_area)
        .PaintOverflowControls(local_paint_info, RoundedIntPoint(paint_offset));
  }
}

void BlockPainter::PaintChildren(const PaintInfo& paint_info) {
  if (paint_info.DescendantPaintingBlocked())
    return;

  // We may use legacy paint to paint the anonymous fieldset child. The layout
  // object for the rendered legend will be a child of that one, and has to be
  // skipped here, since it's handled by a special NG fieldset painter.
  bool may_contain_rendered_legend =
      layout_block_.IsAnonymousNGFieldsetContentWrapper();
  for (LayoutBox* child = layout_block_.FirstChildBox(); child;
       child = child->NextSiblingBox()) {
    if (may_contain_rendered_legend && child->IsRenderedLegend()) {
      may_contain_rendered_legend = false;
      continue;
    }
    PaintChild(*child, paint_info);
  }
}

void BlockPainter::PaintChild(const LayoutBox& child,
                              const PaintInfo& paint_info) {
  if (child.HasSelfPaintingLayer() || child.IsColumnSpanAll())
    return;
  if (!child.IsFloating()) {
    child.Paint(paint_info);
    return;
  }
  // Paint the float now if we're in the right phase and if this is NG. NG
  // paints floats in regular tree order (the FloatingObjects list is only used
  // by legacy layout).
  if (paint_info.phase != PaintPhase::kFloat &&
      paint_info.phase != PaintPhase::kSelectionDragImage &&
      paint_info.phase != PaintPhase::kTextClip)
    return;

  if (!layout_block_.IsLayoutNGObject())
    return;

  PaintInfo float_paint_info(paint_info);
  if (paint_info.phase == PaintPhase::kFloat)
    float_paint_info.phase = PaintPhase::kForeground;

  ObjectPainter(child).PaintAllPhasesAtomically(float_paint_info);
}

void BlockPainter::PaintChildrenAtomically(const OrderIterator& order_iterator,
                                           const PaintInfo& paint_info) {
  if (paint_info.DescendantPaintingBlocked())
    return;
  for (const LayoutBox* child = order_iterator.First(); child;
       child = order_iterator.Next()) {
    PaintAllChildPhasesAtomically(*child, paint_info);
  }
}

void BlockPainter::PaintAllChildPhasesAtomically(const LayoutBox& child,
                                                 const PaintInfo& paint_info) {
  if (paint_info.DescendantPaintingBlocked())
    return;
  if (!child.HasSelfPaintingLayer() && !child.IsFloating())
    ObjectPainter(child).PaintAllPhasesAtomically(paint_info);
}

void BlockPainter::PaintInlineBox(const InlineBox& inline_box,
                                  const PaintInfo& paint_info) {
  if (paint_info.phase != PaintPhase::kForeground &&
      paint_info.phase != PaintPhase::kForcedColorsModeBackplate &&
      paint_info.phase != PaintPhase::kSelectionDragImage)
    return;

  // Text clips are painted only for the direct inline children of the object
  // that has a text clip style on it, not block children.
  DCHECK(paint_info.phase != PaintPhase::kTextClip);

  ObjectPainter(
      *LineLayoutAPIShim::ConstLayoutObjectFrom(inline_box.GetLineLayoutItem()))
      .PaintAllPhasesAtomically(paint_info);
}

DISABLE_CFI_PERF
void BlockPainter::PaintObject(const PaintInfo& paint_info,
                               const PhysicalOffset& paint_offset) {
  const PaintPhase paint_phase = paint_info.phase;
  // This function implements some of the painting order algorithm (described
  // within the description of stacking context, here
  // https://www.w3.org/TR/css-position-3/#det-stacking-context). References are
  // made below to the step numbers described in that document.

  // If this block has been truncated, early-out here, because it will not be
  // displayed. A truncated block occurs when text-overflow: ellipsis is set on
  // a block, and there is not enough room to display all elements. The elements
  // that don't get shown are "Truncated".
  if (layout_block_.IsTruncated())
    return;

  ScopedPaintTimingDetectorBlockPaintHook
      scoped_paint_timing_detector_block_paint_hook;
  if (paint_info.phase == PaintPhase::kForeground) {
    scoped_paint_timing_detector_block_paint_hook.EmplaceIfNeeded(
        layout_block_,
        paint_info.context.GetPaintController().CurrentPaintChunkProperties());
  }
  // If we're *printing or creating a paint preview of* the foreground, paint
  // the URL.
  if (paint_phase == PaintPhase::kForeground &&
      paint_info.ShouldAddUrlMetadata()) {
    ObjectPainter(layout_block_).AddURLRectIfNeeded(paint_info, paint_offset);
  }

  // If we're painting our background (either 1. kBlockBackground - background
  // of the current object and non-self-painting descendants, or 2.
  // kSelfBlockBackgroundOnly -  Paint background of the current object only),
  // paint those now. This is steps #1, 2, and 4 of the CSS spec (see above).
  if (ShouldPaintSelfBlockBackground(paint_phase))
    layout_block_.PaintBoxDecorationBackground(paint_info, paint_offset);

  // Draw a backplate behind all text if in forced colors mode.
  if (paint_phase == PaintPhase::kForcedColorsModeBackplate &&
      layout_block_.GetFrame()->GetDocument()->InForcedColorsMode() &&
      layout_block_.ChildrenInline()) {
    LineBoxListPainter(To<LayoutBlockFlow>(layout_block_).LineBoxes())
        .PaintBackplate(layout_block_, paint_info, paint_offset);
  }

  // If we're in any phase except *just* the self (outline or background) or a
  // mask, paint children now. This is step #5, 7, 8, and 9 of the CSS spec (see
  // above).
  if (paint_phase != PaintPhase::kSelfOutlineOnly &&
      paint_phase != PaintPhase::kSelfBlockBackgroundOnly &&
      paint_phase != PaintPhase::kMask &&
      !paint_info.DescendantPaintingBlocked()) {
    // Actually paint the contents.
    if (layout_block_.IsLayoutBlockFlow()) {
      // All floating descendants will be LayoutBlockFlow objects, and will get
      // painted here. That is step #5 of the CSS spec (see above).
      PaintBlockFlowContents(paint_info, paint_offset);
    } else {
      PaintContents(paint_info, paint_offset);
    }
  }

  // If we're painting the outline, paint it now. This is step #10 of the CSS
  // spec (see above).
  if (ShouldPaintSelfOutline(paint_phase))
    ObjectPainter(layout_block_).PaintOutline(paint_info, paint_offset);

  // If we're painting a visible mask, paint it now. (This does not correspond
  // to any painting order steps within the CSS spec.)
  if (paint_phase == PaintPhase::kMask &&
      layout_block_.StyleRef().Visibility() == EVisibility::kVisible) {
    layout_block_.PaintMask(paint_info, paint_offset);
  }
}

void BlockPainter::PaintBlockFlowContents(const PaintInfo& paint_info,
                                          const PhysicalOffset& paint_offset) {
  DCHECK(layout_block_.IsLayoutBlockFlow());
  if (!layout_block_.ChildrenInline()) {
    PaintContents(paint_info, paint_offset);
  } else if (ShouldPaintDescendantOutlines(paint_info.phase)) {
    ObjectPainter(layout_block_).PaintInlineChildrenOutlines(paint_info);
  } else {
    LineBoxListPainter(To<LayoutBlockFlow>(layout_block_).LineBoxes())
        .Paint(layout_block_, paint_info, paint_offset);
  }

  // If we don't have any floats to paint, or we're in the wrong paint phase,
  // then we're done for now.
  auto* floating_objects =
      To<LayoutBlockFlow>(layout_block_).GetFloatingObjects();
  const PaintPhase paint_phase = paint_info.phase;
  if (!floating_objects || !(paint_phase == PaintPhase::kFloat ||
                             paint_phase == PaintPhase::kSelectionDragImage ||
                             paint_phase == PaintPhase::kTextClip)) {
    return;
  }

  // LayoutNG paints floats in regular tree order, and doesn't use the
  // FloatingObjects list.
  if (layout_block_.IsLayoutNGObject())
    return;

  // If we're painting floats (not selections or textclips), change
  // the paint phase to foreground.
  PaintInfo float_paint_info(paint_info);
  if (paint_info.phase == PaintPhase::kFloat)
    float_paint_info.phase = PaintPhase::kForeground;

  // Paint all floats.
  for (const auto& floating_object : floating_objects->Set()) {
    if (!floating_object->ShouldPaint())
      continue;
    const LayoutBox* floating_layout_object =
        floating_object->GetLayoutObject();
    // TODO(wangxianzhu): Should this be a DCHECK?
    if (floating_layout_object->HasSelfPaintingLayer())
      continue;
    ObjectPainter(*floating_layout_object)
        .PaintAllPhasesAtomically(float_paint_info);
  }
}

void BlockPainter::PaintCarets(const PaintInfo& paint_info,
                               const PhysicalOffset& paint_offset) {
  LocalFrame* frame = layout_block_.GetFrame();

  if (layout_block_.ShouldPaintCursorCaret())
    frame->Selection().PaintCaret(paint_info.context, paint_offset);

  if (layout_block_.ShouldPaintDragCaret()) {
    frame->GetPage()->GetDragCaret().PaintDragCaret(frame, paint_info.context,
                                                    paint_offset);
  }
}

PhysicalRect BlockPainter::OverflowRectForCullRectTesting() const {
  PhysicalRect overflow_rect;
  if (layout_block_.IsAnonymousBlock() && layout_block_.ChildrenInline() &&
      layout_block_.GetDocument().Printing()) {
    // For case <a href="..."><div>...</div></a>, when layout_block_ is the
    // anonymous container of <a>, the anonymous container's visual overflow is
    // empty, but we need to continue painting to output <a>'s PDF URL rect
    // which covers the continuations, as if we included <a>'s PDF URL rect into
    // layout_block_'s visual overflow.
    auto rects = layout_block_.OutlineRects(
        PhysicalOffset(), NGOutlineType::kIncludeBlockVisualOverflow);
    overflow_rect = UnionRect(rects);
  }
  overflow_rect.Unite(layout_block_.PhysicalVisualOverflowRect());

  bool include_layout_overflow =
      layout_block_.ScrollsOverflow() &&
      (layout_block_.UsesCompositedScrolling() ||
       RuntimeEnabledFeatures::CompositeAfterPaintEnabled());

  if (include_layout_overflow) {
    overflow_rect.Unite(layout_block_.PhysicalLayoutOverflowRect());
    overflow_rect.Move(
        -PhysicalOffset(layout_block_.PixelSnappedScrolledContentOffset()));
  }
  return overflow_rect;
}

DISABLE_CFI_PERF
bool BlockPainter::ShouldPaint(const ScopedPaintState& paint_state) const {
  // If there is no fragment to paint for this block, we still need to continue
  // the paint tree walk in case there are overflowing children that exist in
  // the current painting fragment of the painting layer. In the case we can't
  // check the overflow rect against the cull rect in the case because we don't
  // know the paint offset.
  if (!paint_state.FragmentToPaint())
    return true;

  return paint_state.LocalRectIntersectsCullRect(
      OverflowRectForCullRectTesting());
}

void BlockPainter::PaintContents(const PaintInfo& paint_info,
                                 const PhysicalOffset& paint_offset) {
  DCHECK(!layout_block_.ChildrenInline());
  PaintInfo paint_info_for_descendants = paint_info.ForDescendants();
  layout_block_.PaintChildren(paint_info_for_descendants, paint_offset);
}

}  // namespace blink