• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "SkSGEffectNode.h"
12 
13 #include "SkBlendMode.h"
14 #include "SkBlurImageFilter.h"
15 #include "SkColor.h"
16 
17 #include <memory>
18 #include <vector>
19 
20 // TODO: merge EffectNode.h with this header
21 
22 class SkImageFilter;
23 
24 namespace sksg {
25 
26 /**
27  * ImageFilter base class.
28  */
29 class ImageFilter : public Node {
30 public:
31     ~ImageFilter() override;
32 
getFilter()33     const sk_sp<SkImageFilter>& getFilter() const {
34         SkASSERT(!this->hasInval());
35         return fFilter;
36     }
37 
38 protected:
39     explicit ImageFilter(sk_sp<ImageFilter> input = 0);
40 
41     using InputsT = std::vector<sk_sp<ImageFilter>>;
42     explicit ImageFilter(std::unique_ptr<InputsT> inputs);
43 
44     SkRect onRevalidate(InvalidationController*, const SkMatrix&) final;
45 
46     virtual sk_sp<SkImageFilter> onRevalidateFilter() = 0;
47 
48     sk_sp<SkImageFilter> refInput(size_t) const;
49 
50 private:
51     const std::unique_ptr<InputsT> fInputs;
52 
53     sk_sp<SkImageFilter>           fFilter;
54 
55     using INHERITED = Node;
56 };
57 
58 /**
59  * Attaches an ImageFilter (chain) to the render DAG.
60  */
61 class ImageFilterEffect final : public EffectNode {
62 public:
63     ~ImageFilterEffect() override;
64 
65     static sk_sp<RenderNode> Make(sk_sp<RenderNode> child, sk_sp<ImageFilter> filter);
66 
67 protected:
68     void onRender(SkCanvas*, const RenderContext*) const override;
69     const RenderNode* onNodeAt(const SkPoint&)     const override;
70 
71     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
72 
73 private:
74     ImageFilterEffect(sk_sp<RenderNode> child, sk_sp<ImageFilter> filter);
75 
76     sk_sp<ImageFilter> fImageFilter;
77 
78     using INHERITED = EffectNode;
79 };
80 
81 /**
82  * SkDropShadowImageFilter node.
83  */
84 class DropShadowImageFilter final : public ImageFilter {
85 public:
86     ~DropShadowImageFilter() override;
87 
88     static sk_sp<DropShadowImageFilter> Make(sk_sp<ImageFilter> input = nullptr);
89 
90     enum class Mode { kShadowAndForeground, kShadowOnly };
91 
92     SG_ATTRIBUTE(Offset, SkVector, fOffset)
93     SG_ATTRIBUTE(Sigma , SkVector, fSigma )
94     SG_ATTRIBUTE(Color , SkColor , fColor )
95     SG_ATTRIBUTE(Mode  , Mode    , fMode  )
96 
97 protected:
98     sk_sp<SkImageFilter> onRevalidateFilter() override;
99 
100 private:
101     explicit DropShadowImageFilter(sk_sp<ImageFilter> input);
102 
103     SkVector             fOffset = { 0, 0 },
104                          fSigma  = { 0, 0 };
105     SkColor              fColor  = SK_ColorBLACK;
106     Mode                 fMode   = Mode::kShadowAndForeground;
107 
108     using INHERITED = ImageFilter;
109 };
110 
111 /**
112  * SkBlurImageFilter node.
113  */
114 class BlurImageFilter final : public ImageFilter {
115 public:
116     ~BlurImageFilter() override;
117 
118     static sk_sp<BlurImageFilter> Make(sk_sp<ImageFilter> input = nullptr);
119 
120     SG_ATTRIBUTE(Sigma   , SkVector                   , fSigma   )
121     SG_ATTRIBUTE(TileMode, SkBlurImageFilter::TileMode, fTileMode)
122 
123 protected:
124     sk_sp<SkImageFilter> onRevalidateFilter() override;
125 
126 private:
127     explicit BlurImageFilter(sk_sp<ImageFilter> input);
128 
129     SkVector                    fSigma    = { 0, 0 };
130     SkBlurImageFilter::TileMode fTileMode = SkBlurImageFilter::kClamp_TileMode;
131 
132     using INHERITED = ImageFilter;
133 };
134 
135 /**
136  * Applies a SkBlendMode to descendant render nodes.
137  */
138 class BlendModeEffect final : public EffectNode {
139 public:
140     ~BlendModeEffect() override;
141 
142     static sk_sp<BlendModeEffect> Make(sk_sp<RenderNode> child,
143                                        SkBlendMode = SkBlendMode::kSrcOver);
144 
145     SG_ATTRIBUTE(Mode, SkBlendMode, fMode)
146 
147 protected:
148     void onRender(SkCanvas*, const RenderContext*) const override;
149     const RenderNode* onNodeAt(const SkPoint&)     const override;
150 
151 private:
152     BlendModeEffect(sk_sp<RenderNode>, SkBlendMode);
153 
154     SkBlendMode fMode;
155 
156     using INHERITED = EffectNode;
157 };
158 
159 } // namespace sksg
160 
161 #endif // SkSGRenderEffect_DEFINED
162