summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/svg_paint_context.h
blob: a2b4da9c9fa7cd19f73bcfc2092d241fbb29d1ae (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
/**
 * Copyright (C) 2007 Rob Buis <buis@kde.org>
 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2009 Google, Inc.  All rights reserved.
 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
 * Copyright (C) 2012 Zoltan Herczeg <zherczeg@webkit.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_SVG_PAINT_CONTEXT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_SVG_PAINT_CONTEXT_H_

#include <memory>
#include "third_party/blink/renderer/core/layout/svg/layout_svg_resource_paint_server.h"
#include "third_party/blink/renderer/core/paint/clip_path_clipper.h"
#include "third_party/blink/renderer/core/paint/object_paint_properties.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/core/paint/svg_filter_painter.h"
#include "third_party/blink/renderer/platform/graphics/paint/scoped_paint_chunk_properties.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"

namespace blink {

class LayoutObject;
class LayoutSVGResourceFilter;
class LayoutSVGResourceMasker;
class SVGResources;

// Hooks up the correct paint property transform node.
class SVGTransformContext {
  STACK_ALLOCATED();

 public:
  SVGTransformContext(const PaintInfo& paint_info,
                      const LayoutObject& object,
                      const AffineTransform& transform) {
    DCHECK(object.IsSVGChild());

    const auto* fragment = paint_info.FragmentToPaint(object);
    if (!fragment)
      return;
    const auto* properties = fragment->PaintProperties();
    if (!properties)
      return;

    if (const auto* transform_node = properties->Transform()) {
      DCHECK(transform_node->Matrix() == transform.ToTransformationMatrix());
      transform_property_scope_.emplace(
          paint_info.context.GetPaintController(), transform_node, object,
          DisplayItem::PaintPhaseToSVGTransformType(paint_info.phase));
    }
  }

 private:
  base::Optional<ScopedPaintChunkProperties> transform_property_scope_;
};

class SVGPaintContext {
  STACK_ALLOCATED();

 public:
  SVGPaintContext(const LayoutObject& object, const PaintInfo& paint_info)
      : object_(object),
        paint_info_(paint_info),
        filter_(nullptr),
        masker_(nullptr) {}

  ~SVGPaintContext();

  PaintInfo& GetPaintInfo() {
    return filter_paint_info_ ? *filter_paint_info_ : paint_info_;
  }

  // Return true if these operations aren't necessary or if they are
  // successfully applied.
  bool ApplyClipMaskAndFilterIfNecessary();

  static void PaintResourceSubtree(GraphicsContext&, const LayoutObject*);

  // TODO(fs): This functions feels a bit misplaced (we don't want this to
  // turn into the new kitchen sink). Move it if a better location surfaces.
  static bool PaintForLayoutObject(
      const PaintInfo&,
      const ComputedStyle&,
      const LayoutObject&,
      LayoutSVGResourceMode,
      PaintFlags&,
      const AffineTransform* additional_paint_server_transform = nullptr);

 private:
  void ApplyPaintPropertyState();
  void ApplyClipIfNecessary();

  // Return true if no masking is necessary or if the mask is successfully
  // applied.
  bool ApplyMaskIfNecessary(SVGResources*);

  // Return true if no filtering is necessary or if the filter is successfully
  // applied.
  bool ApplyFilterIfNecessary(SVGResources*);

  const LayoutObject& object_;
  PaintInfo paint_info_;
  std::unique_ptr<PaintInfo> filter_paint_info_;
  LayoutSVGResourceFilter* filter_;
  LayoutSVGResourceMasker* masker_;
  base::Optional<ClipPathClipper> clip_path_clipper_;
  std::unique_ptr<SVGFilterRecordingContext> filter_recording_context_;
  base::Optional<ScopedPaintChunkProperties> scoped_paint_chunk_properties_;
#if DCHECK_IS_ON()
  bool apply_clip_mask_and_filter_if_necessary_called_ = false;
#endif
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_SVG_PAINT_CONTEXT_H_