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