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 "modules/sksg/include/SkSGEffectNode.h" 12 13 #include "include/core/SkBlendMode.h" 14 #include "include/core/SkColor.h" 15 #include "include/core/SkMaskFilter.h" 16 #include "include/effects/SkImageFilters.h" 17 18 #include <memory> 19 #include <vector> 20 21 // TODO: merge EffectNode.h with this header 22 23 class SkImageFilter; 24 class SkMaskFilter; 25 class SkShader; 26 27 namespace sksg { 28 29 /** 30 * Mask filter base class. 31 */ 32 class MaskFilter : public Node { 33 public: 34 ~MaskFilter() override; 35 Make(sk_sp<SkMaskFilter> mf)36 static sk_sp<MaskFilter> Make(sk_sp<SkMaskFilter> mf) { 37 return sk_sp<MaskFilter>(new MaskFilter(std::move(mf))); 38 } 39 getMaskFilter()40 const sk_sp<SkMaskFilter>& getMaskFilter() const { 41 SkASSERT(!this->hasInval()); 42 return fMaskFilter; 43 } 44 45 void setMaskFilter(sk_sp<SkMaskFilter>); 46 47 protected: 48 explicit MaskFilter(sk_sp<SkMaskFilter> = nullptr); 49 50 SkRect onRevalidate(InvalidationController*, const SkMatrix&) final; 51 52 virtual sk_sp<SkMaskFilter> onRevalidateMask(); 53 54 private: 55 sk_sp<SkMaskFilter> fMaskFilter; 56 57 using INHERITED = Node; 58 }; 59 60 /** 61 * Attaches a mask filter to the render DAG. 62 */ 63 class MaskFilterEffect final : public EffectNode { 64 public: 65 ~MaskFilterEffect() override; 66 67 static sk_sp<MaskFilterEffect> Make(sk_sp<RenderNode>, sk_sp<MaskFilter>); 68 69 protected: 70 void onRender(SkCanvas*, const RenderContext*) const override; 71 72 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; 73 74 private: 75 MaskFilterEffect(sk_sp<RenderNode>, sk_sp<MaskFilter>); 76 77 sk_sp<MaskFilter> fMaskFilter; 78 79 using INHERITED = EffectNode; 80 }; 81 82 /** 83 * Shader base class. 84 */ 85 class Shader : public Node { 86 public: 87 ~Shader() override; 88 getShader()89 const sk_sp<SkShader>& getShader() const { 90 SkASSERT(!this->hasInval()); 91 return fShader; 92 } 93 94 protected: 95 Shader(); 96 97 SkRect onRevalidate(InvalidationController*, const SkMatrix&) final; 98 99 virtual sk_sp<SkShader> onRevalidateShader() = 0; 100 101 private: 102 sk_sp<SkShader> fShader; 103 104 using INHERITED = Node; 105 }; 106 107 /** 108 * Attaches a shader to the render DAG. 109 */ 110 class ShaderEffect final : public EffectNode { 111 public: 112 ~ShaderEffect() override; 113 114 static sk_sp<ShaderEffect> Make(sk_sp<RenderNode> child, sk_sp<Shader> shader = 0); 115 116 void setShader(sk_sp<Shader>); 117 118 protected: 119 void onRender(SkCanvas*, const RenderContext*) const override; 120 121 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; 122 123 private: 124 ShaderEffect(sk_sp<RenderNode> child, sk_sp<Shader> shader); 125 126 sk_sp<Shader> fShader; 127 128 using INHERITED = EffectNode; 129 }; 130 131 /** 132 * ImageFilter base class. 133 */ 134 class ImageFilter : public Node { 135 public: 136 ~ImageFilter() override; 137 getFilter()138 const sk_sp<SkImageFilter>& getFilter() const { 139 SkASSERT(!this->hasInval()); 140 return fFilter; 141 } 142 143 protected: 144 explicit ImageFilter(sk_sp<ImageFilter> input = 0); 145 146 using InputsT = std::vector<sk_sp<ImageFilter>>; 147 explicit ImageFilter(std::unique_ptr<InputsT> inputs); 148 149 SkRect onRevalidate(InvalidationController*, const SkMatrix&) final; 150 151 virtual sk_sp<SkImageFilter> onRevalidateFilter() = 0; 152 153 sk_sp<SkImageFilter> refInput(size_t) const; 154 155 private: 156 const std::unique_ptr<InputsT> fInputs; 157 158 sk_sp<SkImageFilter> fFilter; 159 160 using INHERITED = Node; 161 }; 162 163 /** 164 * Attaches an ImageFilter (chain) to the render DAG. 165 */ 166 class ImageFilterEffect final : public EffectNode { 167 public: 168 ~ImageFilterEffect() override; 169 170 static sk_sp<RenderNode> Make(sk_sp<RenderNode> child, sk_sp<ImageFilter> filter); 171 172 protected: 173 void onRender(SkCanvas*, const RenderContext*) const override; 174 const RenderNode* onNodeAt(const SkPoint&) const override; 175 176 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; 177 178 private: 179 ImageFilterEffect(sk_sp<RenderNode> child, sk_sp<ImageFilter> filter); 180 181 sk_sp<ImageFilter> fImageFilter; 182 183 using INHERITED = EffectNode; 184 }; 185 186 /** 187 * SkDropShadowImageFilter node. 188 */ 189 class DropShadowImageFilter final : public ImageFilter { 190 public: 191 ~DropShadowImageFilter() override; 192 193 static sk_sp<DropShadowImageFilter> Make(sk_sp<ImageFilter> input = nullptr); 194 195 enum class Mode { kShadowAndForeground, kShadowOnly }; 196 197 SG_ATTRIBUTE(Offset, SkVector, fOffset) 198 SG_ATTRIBUTE(Sigma , SkVector, fSigma ) 199 SG_ATTRIBUTE(Color , SkColor , fColor ) 200 SG_ATTRIBUTE(Mode , Mode , fMode ) 201 202 protected: 203 sk_sp<SkImageFilter> onRevalidateFilter() override; 204 205 private: 206 explicit DropShadowImageFilter(sk_sp<ImageFilter> input); 207 208 SkVector fOffset = { 0, 0 }, 209 fSigma = { 0, 0 }; 210 SkColor fColor = SK_ColorBLACK; 211 Mode fMode = Mode::kShadowAndForeground; 212 213 using INHERITED = ImageFilter; 214 }; 215 216 /** 217 * SkBlurImageFilter node. 218 */ 219 class BlurImageFilter final : public ImageFilter { 220 public: 221 ~BlurImageFilter() override; 222 223 static sk_sp<BlurImageFilter> Make(sk_sp<ImageFilter> input = nullptr); 224 225 SG_ATTRIBUTE(Sigma , SkVector , fSigma ) 226 SG_ATTRIBUTE(TileMode, SkTileMode, fTileMode) 227 228 protected: 229 sk_sp<SkImageFilter> onRevalidateFilter() override; 230 231 private: 232 explicit BlurImageFilter(sk_sp<ImageFilter> input); 233 234 SkVector fSigma = { 0, 0 }; 235 SkTileMode fTileMode = SkTileMode::kClamp; 236 237 using INHERITED = ImageFilter; 238 }; 239 240 /** 241 * Applies a SkBlendMode to descendant render nodes. 242 */ 243 class BlendModeEffect final : public EffectNode { 244 public: 245 ~BlendModeEffect() override; 246 247 static sk_sp<BlendModeEffect> Make(sk_sp<RenderNode> child, 248 SkBlendMode = SkBlendMode::kSrcOver); 249 250 SG_ATTRIBUTE(Mode, SkBlendMode, fMode) 251 252 protected: 253 void onRender(SkCanvas*, const RenderContext*) const override; 254 const RenderNode* onNodeAt(const SkPoint&) const override; 255 256 private: 257 BlendModeEffect(sk_sp<RenderNode>, SkBlendMode); 258 259 SkBlendMode fMode; 260 261 using INHERITED = EffectNode; 262 }; 263 264 } // namespace sksg 265 266 #endif // SkSGRenderEffect_DEFINED 267