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 SkottieEffects_DEFINED 9 #define SkottieEffects_DEFINED 10 11 #include "modules/skottie/src/Composition.h" 12 #include "modules/skottie/src/SkottiePriv.h" 13 #include "modules/skottie/src/animator/Animator.h" 14 15 class SkMaskFilter; 16 17 namespace sksg { 18 class MaskShaderEffect; 19 } // namespace sksg 20 21 namespace skottie { 22 namespace internal { 23 24 class EffectBuilder final : public SkNoncopyable { 25 public: 26 EffectBuilder(const AnimationBuilder*, const SkSize&, CompositionBuilder*); 27 28 sk_sp<sksg::RenderNode> attachEffects(const skjson::ArrayValue&, 29 sk_sp<sksg::RenderNode>) const; 30 31 sk_sp<sksg::RenderNode> attachStyles(const skjson::ArrayValue&, 32 sk_sp<sksg::RenderNode>) const; 33 34 static const skjson::Value& GetPropValue(const skjson::ArrayValue& jprops, size_t prop_index); 35 getLayerBuilder(int layer_index)36 LayerBuilder* getLayerBuilder(int layer_index) const { 37 return fCompBuilder->layerBuilder(layer_index); 38 } 39 40 private: 41 using EffectBuilderT = sk_sp<sksg::RenderNode>(EffectBuilder::*)(const skjson::ArrayValue&, 42 sk_sp<sksg::RenderNode>) const; 43 44 sk_sp<sksg::RenderNode> attachBlackAndWhiteEffect (const skjson::ArrayValue&, 45 sk_sp<sksg::RenderNode>) const; 46 sk_sp<sksg::RenderNode> attachBrightnessContrastEffect(const skjson::ArrayValue&, 47 sk_sp<sksg::RenderNode>) const; 48 sk_sp<sksg::RenderNode> attachBulgeEffect (const skjson::ArrayValue&, 49 sk_sp<sksg::RenderNode>) const; 50 sk_sp<sksg::RenderNode> attachCornerPinEffect (const skjson::ArrayValue&, 51 sk_sp<sksg::RenderNode>) const; 52 sk_sp<sksg::RenderNode> attachCCTonerEffect (const skjson::ArrayValue&, 53 sk_sp<sksg::RenderNode>) const; 54 sk_sp<sksg::RenderNode> attachDirectionalBlurEffect (const skjson::ArrayValue&, 55 sk_sp<sksg::RenderNode>) const; 56 sk_sp<sksg::RenderNode> attachDisplacementMapEffect (const skjson::ArrayValue&, 57 sk_sp<sksg::RenderNode>) const; 58 sk_sp<sksg::RenderNode> attachDropShadowEffect (const skjson::ArrayValue&, 59 sk_sp<sksg::RenderNode>) const; 60 sk_sp<sksg::RenderNode> attachFillEffect (const skjson::ArrayValue&, 61 sk_sp<sksg::RenderNode>) const; 62 sk_sp<sksg::RenderNode> attachFractalNoiseEffect (const skjson::ArrayValue&, 63 sk_sp<sksg::RenderNode>) const; 64 sk_sp<sksg::RenderNode> attachGaussianBlurEffect (const skjson::ArrayValue&, 65 sk_sp<sksg::RenderNode>) const; 66 sk_sp<sksg::RenderNode> attachGradientEffect (const skjson::ArrayValue&, 67 sk_sp<sksg::RenderNode>) const; 68 sk_sp<sksg::RenderNode> attachHueSaturationEffect (const skjson::ArrayValue&, 69 sk_sp<sksg::RenderNode>) const; 70 sk_sp<sksg::RenderNode> attachInvertEffect (const skjson::ArrayValue&, 71 sk_sp<sksg::RenderNode>) const; 72 sk_sp<sksg::RenderNode> attachEasyLevelsEffect (const skjson::ArrayValue&, 73 sk_sp<sksg::RenderNode>) const; 74 sk_sp<sksg::RenderNode> attachLinearWipeEffect (const skjson::ArrayValue&, 75 sk_sp<sksg::RenderNode>) const; 76 sk_sp<sksg::RenderNode> attachMotionTileEffect (const skjson::ArrayValue&, 77 sk_sp<sksg::RenderNode>) const; 78 sk_sp<sksg::RenderNode> attachProLevelsEffect (const skjson::ArrayValue&, 79 sk_sp<sksg::RenderNode>) const; 80 sk_sp<sksg::RenderNode> attachRadialWipeEffect (const skjson::ArrayValue&, 81 sk_sp<sksg::RenderNode>) const; 82 sk_sp<sksg::RenderNode> attachSharpenEffect (const skjson::ArrayValue&, 83 sk_sp<sksg::RenderNode>) const; 84 sk_sp<sksg::RenderNode> attachShiftChannelsEffect (const skjson::ArrayValue&, 85 sk_sp<sksg::RenderNode>) const; 86 sk_sp<sksg::RenderNode> attachSkSLColorFilter (const skjson::ArrayValue&, 87 sk_sp<sksg::RenderNode>) const; 88 sk_sp<sksg::RenderNode> attachSkSLShader (const skjson::ArrayValue&, 89 sk_sp<sksg::RenderNode>) const; 90 sk_sp<sksg::RenderNode> attachSphereEffect (const skjson::ArrayValue&, 91 sk_sp<sksg::RenderNode>) const; 92 sk_sp<sksg::RenderNode> attachThresholdEffect (const skjson::ArrayValue&, 93 sk_sp<sksg::RenderNode>) const; 94 sk_sp<sksg::RenderNode> attachTintEffect (const skjson::ArrayValue&, 95 sk_sp<sksg::RenderNode>) const; 96 sk_sp<sksg::RenderNode> attachTransformEffect (const skjson::ArrayValue&, 97 sk_sp<sksg::RenderNode>) const; 98 sk_sp<sksg::RenderNode> attachTritoneEffect (const skjson::ArrayValue&, 99 sk_sp<sksg::RenderNode>) const; 100 sk_sp<sksg::RenderNode> attachVenetianBlindsEffect (const skjson::ArrayValue&, 101 sk_sp<sksg::RenderNode>) const; 102 103 sk_sp<sksg::RenderNode> attachDropShadowStyle(const skjson::ObjectValue&, 104 sk_sp<sksg::RenderNode>) const; 105 sk_sp<sksg::RenderNode> attachInnerShadowStyle(const skjson::ObjectValue&, 106 sk_sp<sksg::RenderNode>) const; 107 sk_sp<sksg::RenderNode> attachInnerGlowStyle(const skjson::ObjectValue&, 108 sk_sp<sksg::RenderNode>) const; 109 sk_sp<sksg::RenderNode> attachOuterGlowStyle(const skjson::ObjectValue&, 110 sk_sp<sksg::RenderNode>) const; 111 112 EffectBuilderT findBuilder(const skjson::ObjectValue&) const; 113 114 const AnimationBuilder* fBuilder; 115 CompositionBuilder* fCompBuilder; 116 const SkSize fLayerSize; 117 }; 118 119 // Syntactic sugar/helper. 120 class EffectBinder { 121 public: EffectBinder(const skjson::ArrayValue & jprops,const AnimationBuilder & abuilder,AnimatablePropertyContainer * acontainer)122 EffectBinder(const skjson::ArrayValue& jprops, 123 const AnimationBuilder& abuilder, 124 AnimatablePropertyContainer* acontainer) 125 : fProps(jprops) 126 , fBuilder(abuilder) 127 , fContainer(acontainer) {} 128 129 template <typename T> bind(size_t prop_index,T & value)130 const EffectBinder& bind(size_t prop_index, T& value) const { 131 fContainer->bind(fBuilder, EffectBuilder::GetPropValue(fProps, prop_index), value); 132 133 return *this; 134 } 135 136 private: 137 const skjson::ArrayValue& fProps; 138 const AnimationBuilder& fBuilder; 139 AnimatablePropertyContainer* fContainer; 140 }; 141 142 /** 143 * Base class for mask-shader-related effects. 144 */ 145 class MaskShaderEffectBase : public AnimatablePropertyContainer { 146 public: node()147 const sk_sp<sksg::MaskShaderEffect>& node() const { return fMaskEffectNode; } 148 149 protected: 150 MaskShaderEffectBase(sk_sp<sksg::RenderNode>, const SkSize&); 151 layerSize()152 const SkSize& layerSize() const { return fLayerSize; } 153 154 struct MaskInfo { 155 sk_sp<SkShader> fMaskShader; 156 bool fVisible; 157 }; 158 virtual MaskInfo onMakeMask() const = 0; 159 160 private: 161 void onSync() final; 162 163 const sk_sp<sksg::MaskShaderEffect> fMaskEffectNode; 164 const SkSize fLayerSize; 165 }; 166 167 } // namespace internal 168 } // namespace skottie 169 170 #endif // SkottieEffects_DEFINED 171