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