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