blob: cf7066cbcc39441674ee781f523153cded6e6eac [file] [log] [blame]
Brian Osman7c979f52019-02-12 13:27:51 -05001/*
2* Copyright 2019 Google LLC
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 SkParticleEffect_DEFINED
9#define SkParticleEffect_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
Brian Osmanfe491632019-07-25 15:14:50 -040012#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTArray.h"
Brian Osmanfe491632019-07-25 15:14:50 -040014#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/utils/SkRandom.h"
Brian Osmanfe491632019-07-25 15:14:50 -040016#include "modules/particles/include/SkParticleData.h"
17#include "modules/particles/include/SkReflected.h"
18
19#include <memory>
Brian Osman7c979f52019-02-12 13:27:51 -050020
Brian Osman7c979f52019-02-12 13:27:51 -050021class SkCanvas;
Brian Osman3da607e2019-07-26 15:11:46 -040022struct SkCurve;
23struct SkColorCurve;
Brian Osman543d2e22019-02-15 14:29:38 -050024class SkParticleDrawable;
Brian Osmanfe491632019-07-25 15:14:50 -040025class SkParticleExternalValue;
26
27namespace SkSL {
28 struct ByteCode;
29 class Compiler;
30}
31
32class SkParticleBinding : public SkReflected {
33public:
34 SkParticleBinding(const char* name = "name") : fName(name) {}
35
36 REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)
37
38 void visitFields(SkFieldVisitor* v) override;
39 virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;
40
41 static void RegisterBindingTypes();
42
Brian Osman3da607e2019-07-26 15:11:46 -040043 /*
44 * All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
45 * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
46 * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
47 * each kind of binding is described below.
48 */
49
50 // Binds an SkCurve to an effect's SkSL. The curve is a one-dimensional function, described
51 // in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a single float value.
52 static sk_sp<SkParticleBinding> MakeCurve(const char* name, const SkCurve& curve);
53
54 // Binds an SkColorCurve to an effect's SkSL. The curve is a one-dimensional, function,
55 // described in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a float4 value.
56 static sk_sp<SkParticleBinding> MakeColorCurve(const char* name, const SkColorCurve& curve);
57
58 // Binds an SkPath to an effect's SkSL. The path is specified using SVG syntax. It is called
59 // in the SkSL as 'name(t)'. 't' is a normalized distance along the path. This returns a float4
60 // value, containing the position in .xy, and the normal in .zw.
61 static sk_sp<SkParticleBinding> MakePathBinding(const char* name, const char* path);
62
Brian Osmanfe491632019-07-25 15:14:50 -040063protected:
64 SkString fName;
65};
Brian Osman7c979f52019-02-12 13:27:51 -050066
Brian Osman7c979f52019-02-12 13:27:51 -050067class SkParticleEffectParams : public SkRefCnt {
68public:
Brian Osmanfe491632019-07-25 15:14:50 -040069 SkParticleEffectParams();
70
Brian Osman3da607e2019-07-26 15:11:46 -040071 int fMaxCount; // Maximum number of particles per instance of the effect
72 float fEffectDuration; // How long does the effect last after being played, in seconds?
73 float fRate; // How many particles are emitted per second?
Brian Osman7c979f52019-02-12 13:27:51 -050074
Brian Osman3da607e2019-07-26 15:11:46 -040075 // What is drawn for each particle? (Image, shape, sprite sheet, etc.)
76 // See SkParticleDrawable::Make*
Brian Osman543d2e22019-02-15 14:29:38 -050077 sk_sp<SkParticleDrawable> fDrawable;
Brian Osman7c979f52019-02-12 13:27:51 -050078
Brian Osman3da607e2019-07-26 15:11:46 -040079 // Particle behavior is driven by two SkSL functions defined in the fCode string.
80 // Both functions get a mutable Particle struct:
81 //
82 // struct Particle {
83 // float age;
84 // float lifetime;
85 // float2 pos = { 0, 0 }; // Local position, relative to the effect.
86 // float2 dir = { 0, -1 }; // Heading. Should be a normalized vector.
87 // float scale = 1; // Size, normalized relative to the drawable's native size
88 // float2 vel = { 0, 0 }; // Linear velocity, in (units / second)
89 // float spin = 0; // Angular velocity, in (radians / second)
90 // float4 color = { 1, 1, 1, 1 }; // RGBA color
91 // float frame = 0; // Normalized sprite index for multi-frame drawables
92 // };
93 //
94 // In addition, both functions have access to a global variable named 'rand'. Every read of
95 // 'rand' returns a random floating point value in [0, 1). The random generator is stored
96 // per-particle, and the state is rewound after each update, so calls to 'rand' will return
97 // consistent values from one update to the next.
98 //
99 // Finally, there are two global uniform values available. The first is 'dt', a floating point
100 // number of seconds that have elapsed since the last update. The second is 'effectAge', which
101 // is the normalized age of the effect (not particle). For looping effects, this will wrap
102 // back to zero when the effect's age exceeds its duration.
103 //
104 // 'void spawn(inout Particle p)' is called once for each particle when it is first created,
105 // to set initial values. At a minimum, this should set 'lifetime' to the number of seconds
106 // that the particle will exist. Other parameters have defaults shown above.
107 //
108 // 'void update(inout Particle p)' is called for each particle on every call to the running
109 // SkParticleEffect's update() method. It can animate any of the particle's values. Note that
110 // the 'lifetime' field has a different meaning in 'update', and should not be used or changed.
Brian Osmane59acb72019-07-25 16:58:46 -0400111 SkString fCode;
Brian Osmanfe491632019-07-25 15:14:50 -0400112
Brian Osman3da607e2019-07-26 15:11:46 -0400113 // External objects accessible by the effect's SkSL code. Each binding is a name and particular
114 // kind of object. See SkParticleBinding::Make* for details.
Brian Osmanfe491632019-07-25 15:14:50 -0400115 SkTArray<sk_sp<SkParticleBinding>> fBindings;
Brian Osman7c979f52019-02-12 13:27:51 -0500116
117 void visitFields(SkFieldVisitor* v);
Brian Osmanfe491632019-07-25 15:14:50 -0400118
119private:
120 friend class SkParticleEffect;
121
122 // Cached
Brian Osmane59acb72019-07-25 16:58:46 -0400123 std::unique_ptr<SkSL::ByteCode> fByteCode;
124 SkTArray<std::unique_ptr<SkParticleExternalValue>> fExternalValues;
Brian Osmanfe491632019-07-25 15:14:50 -0400125
126 void rebuild();
Brian Osman7c979f52019-02-12 13:27:51 -0500127};
128
129class SkParticleEffect : public SkRefCnt {
130public:
Brian Osman5c1f8eb2019-02-14 14:49:55 -0500131 SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random);
Brian Osman7c979f52019-02-12 13:27:51 -0500132
Kevin Lubick269fe892019-03-06 09:32:55 -0500133 void start(double now, bool looping = false);
Kevin Lubick269fe892019-03-06 09:32:55 -0500134 void update(double now);
Brian Osman7c979f52019-02-12 13:27:51 -0500135 void draw(SkCanvas* canvas);
136
Brian Osmanb77d5022019-03-06 11:08:48 -0500137 bool isAlive() const { return fSpawnTime >= 0; }
138 int getCount() const { return fCount; }
Brian Osman7c979f52019-02-12 13:27:51 -0500139
140private:
141 void setCapacity(int capacity);
142
Brian Osman7c979f52019-02-12 13:27:51 -0500143 sk_sp<SkParticleEffectParams> fParams;
Brian Osman7c979f52019-02-12 13:27:51 -0500144
Brian Osman5c1f8eb2019-02-14 14:49:55 -0500145 SkRandom fRandom;
146
147 bool fLooping;
148 double fSpawnTime;
149
Brian Osman7c979f52019-02-12 13:27:51 -0500150 int fCount;
151 double fLastTime;
152 float fSpawnRemainder;
153
Brian Osmanfe491632019-07-25 15:14:50 -0400154 SkParticles fParticles;
155 SkAutoTMalloc<SkRandom> fStableRandoms;
Brian Osman7c979f52019-02-12 13:27:51 -0500156
157 // Cached
158 int fCapacity;
159};
160
161#endif // SkParticleEffect_DEFINED