Skia
2D Graphics Library
SkSGRenderEffect.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkSGRenderEffect_DEFINED
9 #define SkSGRenderEffect_DEFINED
10 
13 
15 #include "include/core/SkColor.h"
17 
18 #include <memory>
19 #include <vector>
20 
21 // TODO: merge EffectNode.h with this header
22 
23 class SkBlender;
24 class SkImageFilter;
25 class SkMaskFilter;
26 class SkShader;
27 
28 namespace sksg {
29 
33 class Shader : public Node {
34 public:
35  ~Shader() override;
36 
37  const sk_sp<SkShader>& getShader() const {
38  SkASSERT(!this->hasInval());
39  return fShader;
40  }
41 
42 protected:
43  Shader();
44 
46 
48 
49 private:
50  sk_sp<SkShader> fShader;
51 
52  using INHERITED = Node;
53 };
54 
58 class ShaderEffect final : public EffectNode {
59 public:
60  ~ShaderEffect() override;
61 
62  static sk_sp<ShaderEffect> Make(sk_sp<RenderNode> child, sk_sp<Shader> shader = nullptr);
63 
65 
66 protected:
67  void onRender(SkCanvas*, const RenderContext*) const override;
68 
70 
71 private:
73 
74  sk_sp<Shader> fShader;
75 
76  using INHERITED = EffectNode;
77 };
78 
82 class MaskShaderEffect final : public EffectNode {
83 public:
85 
87 
88 protected:
89  void onRender(SkCanvas*, const RenderContext*) const override;
90 
91 private:
93 
94  sk_sp<SkShader> fShader;
95 
96  using INHERITED = EffectNode;
97 };
98 
102 class ImageFilter : public Node {
103 public:
104  ~ImageFilter() override;
105 
107  SkASSERT(!this->hasInval());
108  return fFilter;
109  }
110 
111  SG_ATTRIBUTE(CropRect, SkImageFilters::CropRect, fCropRect)
112 
113 protected:
115 
117 
119 
120 private:
121  sk_sp<SkImageFilter> fFilter;
122  SkImageFilters::CropRect fCropRect = std::nullopt;
123 
124  using INHERITED = Node;
125 };
126 
130 class ImageFilterEffect final : public EffectNode {
131 public:
132  ~ImageFilterEffect() override;
133 
135 
136  enum class Cropping {
137  kNone, // Doesn't use a crop rect.
138  kContent, // Uses the content bounding box as a crop rect.
139  };
140 
141  SG_ATTRIBUTE(Cropping, Cropping, fCropping)
142 
143 protected:
144  void onRender(SkCanvas*, const RenderContext*) const override;
145  const RenderNode* onNodeAt(const SkPoint&) const override;
146 
148 
149 private:
151 
152  sk_sp<ImageFilter> fImageFilter;
153  Cropping fCropping = Cropping::kNone;
154 
155  using INHERITED = EffectNode;
156 };
157 
161 class ExternalImageFilter final : public ImageFilter {
162 public:
164 
167  }
168 
170 
171 private:
173 
174  sk_sp<SkImageFilter> onRevalidateFilter() override { return fImageFilter; }
175 
176  sk_sp<SkImageFilter> fImageFilter;
177 };
178 
182 class DropShadowImageFilter final : public ImageFilter {
183 public:
185 
187 
189 
190  SG_ATTRIBUTE(Offset, SkVector, fOffset)
191  SG_ATTRIBUTE(Sigma , SkVector, fSigma )
192  SG_ATTRIBUTE(Color , SkColor , fColor )
193  SG_ATTRIBUTE(Mode , Mode , fMode )
194 
195 protected:
197 
198 private:
199  explicit DropShadowImageFilter();
200 
201  SkVector fOffset = { 0, 0 },
202  fSigma = { 0, 0 };
203  SkColor fColor = SK_ColorBLACK;
205 
206  using INHERITED = ImageFilter;
207 };
208 
212 class BlurImageFilter final : public ImageFilter {
213 public:
214  ~BlurImageFilter() override;
215 
217 
218  SG_ATTRIBUTE(Sigma , SkVector , fSigma )
219  SG_ATTRIBUTE(TileMode, SkTileMode, fTileMode)
220 
221 protected:
223 
224 private:
225  explicit BlurImageFilter();
226 
227  SkVector fSigma = { 0, 0 };
228  SkTileMode fTileMode = SkTileMode::kDecal;
229 
230  using INHERITED = ImageFilter;
231 };
232 
236 class BlenderEffect final : public EffectNode {
237 public:
238  ~BlenderEffect() override;
239 
241 
242  SG_ATTRIBUTE(Blender, sk_sp<SkBlender>, fBlender)
243 
244 protected:
245  void onRender(SkCanvas*, const RenderContext*) const override;
246  const RenderNode* onNodeAt(const SkPoint&) const override;
247 
248 private:
250 
251  sk_sp<SkBlender> fBlender;
252 
253  using INHERITED = EffectNode;
254 };
255 
256 class LayerEffect final : public EffectNode {
257 public:
258  ~LayerEffect() override;
259 
262 
263  SG_ATTRIBUTE(Mode, SkBlendMode, fMode)
264 
265 private:
267 
268  void onRender(SkCanvas*, const RenderContext*) const override;
269 
270  SkBlendMode fMode;
271 
272  using INHERITED = EffectNode;
273 };
274 
275 } // namespace sksg
276 
277 #endif // SkSGRenderEffect_DEFINED
SkBlendMode
Blends are operators that take in two colors (source, destination) and return a new color.
Definition: SkBlendMode.h:38
@ kSrcOver
r = s + (1-sa)*d
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Represents fully opaque black.
Definition: SkColor.h:103
#define SG_ATTRIBUTE(attr_name, attr_type, attr_container)
Definition: SkSGNode.h:99
SkTileMode
Definition: SkTileMode.h:13
@ kDecal
Only draw within the original domain, return transparent-black everywhere else.
SkBlender represents a custom blend function in the Skia pipeline.
Definition: SkBlender.h:19
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
Base class for image filters.
Definition: SkImageFilter.h:35
SkMaskFilter is the base class for object that perform transformations on the mask before drawing it.
Definition: SkMaskFilter.h:27
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
Definition: SkRefCnt.h:119
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
Applies an SkBlender to descendant render nodes.
Definition: SkSGRenderEffect.h:236
const RenderNode * onNodeAt(const SkPoint &) const override
void onRender(SkCanvas *, const RenderContext *) const override
static sk_sp< BlenderEffect > Make(sk_sp< RenderNode > child, sk_sp< SkBlender >=nullptr)
~BlenderEffect() override
SkBlurImageFilter node.
Definition: SkSGRenderEffect.h:212
static sk_sp< BlurImageFilter > Make()
~BlurImageFilter() override
sk_sp< SkImageFilter > onRevalidateFilter() override
Concrete Paint node, wrapping an SkColor.
Definition: SkSGPaint.h:64
SkDropShadowImageFilter node.
Definition: SkSGRenderEffect.h:182
Mode
Definition: SkSGRenderEffect.h:188
sk_sp< SkImageFilter > onRevalidateFilter() override
static sk_sp< DropShadowImageFilter > Make()
Base class for nodes which apply some transformation when rendering their descendants.
Definition: SkSGEffectNode.h:21
EffectNode(sk_sp< RenderNode >, uint32_t inval_traits=0)
Wrapper for externally-managed SkImageFilters.
Definition: SkSGRenderEffect.h:161
static sk_sp< ExternalImageFilter > Make()
Definition: SkSGRenderEffect.h:165
Attaches an ImageFilter (chain) to the render DAG.
Definition: SkSGRenderEffect.h:130
static sk_sp< RenderNode > Make(sk_sp< RenderNode > child, sk_sp< ImageFilter > filter)
SkRect onRevalidate(InvalidationController *, const SkMatrix &) override
const RenderNode * onNodeAt(const SkPoint &) const override
Cropping
Definition: SkSGRenderEffect.h:136
~ImageFilterEffect() override
void onRender(SkCanvas *, const RenderContext *) const override
ImageFilter base class.
Definition: SkSGRenderEffect.h:102
~ImageFilter() override
virtual sk_sp< SkImageFilter > onRevalidateFilter()=0
const sk_sp< SkImageFilter > & getFilter() const
Definition: SkSGRenderEffect.h:106
SkRect onRevalidate(InvalidationController *, const SkMatrix &) final
Receiver for invalidation events.
Definition: SkSGInvalidationController.h:25
Definition: SkSGRenderEffect.h:256
static sk_sp< LayerEffect > Make(sk_sp< RenderNode > child, SkBlendMode mode=SkBlendMode::kSrcOver)
~LayerEffect() override
Attaches a mask shader to the render DAG.
Definition: SkSGRenderEffect.h:82
void onRender(SkCanvas *, const RenderContext *) const override
static sk_sp< MaskShaderEffect > Make(sk_sp< RenderNode >, sk_sp< SkShader >=nullptr)
Base class for all scene graph nodes.
Definition: SkSGNode.h:32
Node(uint32_t invalTraits)
bool hasInval() const
Definition: SkSGNode.h:59
Base class for nodes which can render to a canvas.
Definition: SkSGRenderNode.h:27
friend class ImageFilterEffect
Definition: SkSGRenderNode.h:123
Attaches a shader to the render DAG.
Definition: SkSGRenderEffect.h:58
SkRect onRevalidate(InvalidationController *, const SkMatrix &) override
void setShader(sk_sp< Shader >)
static sk_sp< ShaderEffect > Make(sk_sp< RenderNode > child, sk_sp< Shader > shader=nullptr)
~ShaderEffect() override
void onRender(SkCanvas *, const RenderContext *) const override
Shader base class.
Definition: SkSGRenderEffect.h:33
SkRect onRevalidate(InvalidationController *, const SkMatrix &) final
~Shader() override
virtual sk_sp< SkShader > onRevalidateShader()=0
const sk_sp< SkShader > & getShader() const
Definition: SkSGRenderEffect.h:37
Definition: Skottie.h:30
Definition: SkImageFilters.h:47
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: SkSGRenderNode.h:53