• 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 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> attachShiftChannelsEffect     (const skjson::ArrayValue&,
83                                                            sk_sp<sksg::RenderNode>) const;
84     sk_sp<sksg::RenderNode> attachSkSLEffect              (const skjson::ArrayValue&,
85                                                            sk_sp<sksg::RenderNode>) const;
86     sk_sp<sksg::RenderNode> attachSphereEffect            (const skjson::ArrayValue&,
87                                                            sk_sp<sksg::RenderNode>) const;
88     sk_sp<sksg::RenderNode> attachThresholdEffect         (const skjson::ArrayValue&,
89                                                            sk_sp<sksg::RenderNode>) const;
90     sk_sp<sksg::RenderNode> attachTintEffect              (const skjson::ArrayValue&,
91                                                            sk_sp<sksg::RenderNode>) const;
92     sk_sp<sksg::RenderNode> attachTransformEffect         (const skjson::ArrayValue&,
93                                                            sk_sp<sksg::RenderNode>) const;
94     sk_sp<sksg::RenderNode> attachTritoneEffect           (const skjson::ArrayValue&,
95                                                            sk_sp<sksg::RenderNode>) const;
96     sk_sp<sksg::RenderNode> attachVenetianBlindsEffect    (const skjson::ArrayValue&,
97                                                            sk_sp<sksg::RenderNode>) const;
98 
99     sk_sp<sksg::RenderNode> attachDropShadowStyle(const skjson::ObjectValue&,
100                                                   sk_sp<sksg::RenderNode>) const;
101     sk_sp<sksg::RenderNode> attachInnerShadowStyle(const skjson::ObjectValue&,
102                                                    sk_sp<sksg::RenderNode>) const;
103     sk_sp<sksg::RenderNode> attachInnerGlowStyle(const skjson::ObjectValue&,
104                                                  sk_sp<sksg::RenderNode>) const;
105     sk_sp<sksg::RenderNode> attachOuterGlowStyle(const skjson::ObjectValue&,
106                                                  sk_sp<sksg::RenderNode>) const;
107 
108     EffectBuilderT findBuilder(const skjson::ObjectValue&) const;
109 
110     const AnimationBuilder* fBuilder;
111     CompositionBuilder*     fCompBuilder;
112     const SkSize            fLayerSize;
113 };
114 
115 // Syntactic sugar/helper.
116 class EffectBinder {
117 public:
EffectBinder(const skjson::ArrayValue & jprops,const AnimationBuilder & abuilder,AnimatablePropertyContainer * acontainer)118     EffectBinder(const skjson::ArrayValue& jprops,
119                  const AnimationBuilder& abuilder,
120                  AnimatablePropertyContainer* acontainer)
121         : fProps(jprops)
122         , fBuilder(abuilder)
123         , fContainer(acontainer) {}
124 
125     template <typename T>
bind(size_t prop_index,T & value)126     const EffectBinder& bind(size_t prop_index, T& value) const {
127         fContainer->bind(fBuilder, EffectBuilder::GetPropValue(fProps, prop_index), value);
128 
129         return *this;
130     }
131 
132 private:
133     const skjson::ArrayValue&    fProps;
134     const AnimationBuilder&      fBuilder;
135     AnimatablePropertyContainer* fContainer;
136 };
137 
138 /**
139  * Base class for mask-shader-related effects.
140  */
141 class MaskShaderEffectBase : public AnimatablePropertyContainer {
142 public:
node()143     const sk_sp<sksg::MaskShaderEffect>& node() const { return fMaskEffectNode; }
144 
145 protected:
146     MaskShaderEffectBase(sk_sp<sksg::RenderNode>, const SkSize&);
147 
layerSize()148     const SkSize& layerSize() const { return  fLayerSize; }
149 
150     struct MaskInfo {
151         sk_sp<SkShader> fMaskShader;
152         bool            fVisible;
153     };
154     virtual MaskInfo onMakeMask() const = 0;
155 
156 private:
157     void onSync() final;
158 
159     const sk_sp<sksg::MaskShaderEffect> fMaskEffectNode;
160     const SkSize                        fLayerSize;
161 };
162 
163 } // namespace internal
164 } // namespace skottie
165 
166 #endif // SkottieEffects_DEFINED
167