File: object_painter.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (204 lines) | stat: -rw-r--r-- 7,542 bytes parent folder | download | duplicates (4)
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
// Copyright 2014 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/paint/object_painter.h"

#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/layout/layout_block.h"
#include "third_party/blink/renderer/core/layout/layout_inline.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/paint/outline_painter.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/core/style/border_edge.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h"
#include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

void ObjectPainter::PaintOutline(const PaintInfo& paint_info,
                                 const PhysicalOffset& paint_offset) {
  DCHECK(ShouldPaintSelfOutline(paint_info.phase));

  const ComputedStyle& style_to_use = layout_object_.StyleRef();
  if (!style_to_use.HasOutline() ||
      style_to_use.Visibility() != EVisibility::kVisible) {
    return;
  }

  // Only paint the focus ring by hand if the theme isn't able to draw the focus
  // ring.
  if (style_to_use.OutlineStyleIsAuto() &&
      !LayoutTheme::GetTheme().ShouldDrawDefaultFocusRing(
          layout_object_.GetNode(), style_to_use)) {
    return;
  }

  LayoutObject::OutlineInfo info;
  auto outline_rects = layout_object_.OutlineRects(
      &info, paint_offset,
      style_to_use.OutlineRectsShouldIncludeBlockInkOverflow());
  if (outline_rects.empty())
    return;

  OutlinePainter::PaintOutlineRects(paint_info, layout_object_, outline_rects,
                                    info, style_to_use);
}

void ObjectPainter::PaintInlineChildrenOutlines(const PaintInfo& paint_info) {
  DCHECK(ShouldPaintDescendantOutlines(paint_info.phase));

  PaintInfo paint_info_for_descendants = paint_info.ForDescendants();
  for (LayoutObject* child = layout_object_.SlowFirstChild(); child;
       child = child->NextSibling()) {
    if (child->IsLayoutInline() &&
        !To<LayoutInline>(child)->HasSelfPaintingLayer())
      child->Paint(paint_info_for_descendants);
  }
}

void ObjectPainter::AddURLRectIfNeeded(const PaintInfo& paint_info,
                                       const PhysicalOffset& paint_offset) {
  DCHECK(paint_info.ShouldAddUrlMetadata());
  if (!layout_object_.GetNode() || !layout_object_.GetNode()->IsLink() ||
      layout_object_.StyleRef().Visibility() != EVisibility::kVisible) {
    return;
  }

  KURL url = To<Element>(layout_object_.GetNode())->HrefURL();
  if (!url.IsValid())
    return;

  auto outline_rects = layout_object_.OutlineRects(
      nullptr, paint_offset, OutlineType::kIncludeBlockInkOverflow);
  gfx::Rect bounding_rect = ToPixelSnappedRect(UnionRect(outline_rects));
  if (bounding_rect.IsEmpty()) {
    return;
  }

  if (DrawingRecorder::UseCachedDrawingIfPossible(
          paint_info.context, layout_object_,
          DisplayItem::kPrintedContentPDFURLRect))
    return;

  DrawingRecorder recorder(paint_info.context, layout_object_,
                           DisplayItem::kPrintedContentPDFURLRect,
                           bounding_rect);

  Document& document = layout_object_.GetDocument();
  String fragment_name;
  if (url.HasFragmentIdentifier() &&
      EqualIgnoringFragmentIdentifier(url, document.BaseURL())) {
    fragment_name = url.FragmentIdentifier().ToString();
    if (!document.FindAnchor(fragment_name)) {
      return;
    }
  }

  for (auto physical_rect : outline_rects) {
    gfx::Rect rect = ToPixelSnappedRect(physical_rect);
    if (fragment_name) {
      paint_info.context.SetURLFragmentForRect(fragment_name, rect);
    } else {
      paint_info.context.SetURLForRect(url, rect);
    }
  }
}

void ObjectPainter::PaintAllPhasesAtomically(const PaintInfo& paint_info) {
  // Pass kSelectionDragImage and kTextClip to the descendants so that
  // they will paint for selection and text clip respectively. We don't need
  // complete painting for these phases.
  if (paint_info.phase == PaintPhase::kSelectionDragImage ||
      paint_info.phase == PaintPhase::kTextClip) {
    layout_object_.Paint(paint_info);
    return;
  }

  if (paint_info.phase != PaintPhase::kForeground)
    return;

  PaintInfo info(paint_info);
  info.phase = PaintPhase::kBlockBackground;
  layout_object_.Paint(info);
  info.phase = PaintPhase::kForcedColorsModeBackplate;
  layout_object_.Paint(info);
  info.phase = PaintPhase::kFloat;
  layout_object_.Paint(info);
  info.phase = PaintPhase::kForeground;
  layout_object_.Paint(info);
  info.phase = PaintPhase::kOutline;
  layout_object_.Paint(info);
}

void ObjectPainter::RecordHitTestData(
    const PaintInfo& paint_info,
    const gfx::Rect& paint_rect,
    const DisplayItemClient& background_client) {
  // Hit test data are only needed for compositing. This flag is used for for
  // printing and drag images which do not need hit testing.
  if (paint_info.ShouldOmitCompositingInfo()) {
    return;
  }

  // If an object is not visible, it does not participate in painting or hit
  // testing. TODO(crbug.com/1471738): Some pointer-events values actually
  // allow hit testing with visibility:hidden.
  if (layout_object_.StyleRef().Visibility() != EVisibility::kVisible) {
    return;
  }

  paint_info.context.GetPaintController().RecordHitTestData(
      background_client, paint_rect,
      layout_object_.EffectiveAllowedTouchAction(),
      layout_object_.InsideBlockingWheelEventHandler(), GetHitTestOpaqueness());
}

cc::HitTestOpaqueness ObjectPainter::GetHitTestOpaqueness() const {
  // Effects (e.g. clip-path and mask) are not checked here even if they
  // affects hit test. They are checked during PaintArtifactCompositor update
  // based on paint properties.

  if (!layout_object_.VisibleToHitTesting() ||
      !layout_object_.GetFrame()->GetVisibleToHitTesting()) {
    return cc::HitTestOpaqueness::kTransparent;
  }
  // Border radius is not considered opaque for hit test because the hit
  // test may be inside or outside of the rounded corner.
  if (layout_object_.StyleRef().HasBorderRadius()) {
    return cc::HitTestOpaqueness::kMixed;
  }
  // SVG children are not considered opaque for hit test because SVG has
  // special hit test rules for stroke/fill/etc, and the children may
  // overflow the root.
  if (layout_object_.IsSVGChild()) {
    return cc::HitTestOpaqueness::kMixed;
  }
  return cc::HitTestOpaqueness::kOpaque;
}

bool ObjectPainter::ShouldRecordSpecialHitTestData(
    const PaintInfo& paint_info) {
  if (layout_object_.EffectiveAllowedTouchAction() != TouchAction::kAuto) {
    return true;
  }
  if (layout_object_.InsideBlockingWheelEventHandler()) {
    return true;
  }
  if (layout_object_.StyleRef().UsedPointerEvents() == EPointerEvents::kNone) {
    return true;
  }
  if (paint_info.context.GetPaintController()
          .CurrentChunkIsNonEmptyAndTransparentToHitTest()) {
    // A non-none value of pointer-events will make a transparent paint chunk
    // (due to pointer-events: none on an ancestor painted into the current
    // paint chunk) not transparent.
    return true;
  }
  return false;
}

}  // namespace blink