blob: b08aa15418e074d36a6eedf4868abe3afcab541a [file] [log] [blame]
Brian Osman2aa85df2019-08-30 10:59:47 -04001/*
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 SkParticleBinding_DEFINED
9#define SkParticleBinding_DEFINED
10
11#include "include/core/SkString.h"
12#include "modules/particles/include/SkReflected.h"
13#include "src/sksl/SkSLExternalValue.h"
14
15#include <memory>
16
Brian Osman9a8b8462019-09-19 10:06:36 -040017class SkParticleEffect;
18class SkParticleEffectParams;
Brian Osman2aa85df2019-08-30 10:59:47 -040019class SkRandom;
20
Brian Osman9dac0d82019-12-02 16:52:51 -050021namespace skresources {
22 class ResourceProvider;
23}
24
Brian Osman2aa85df2019-08-30 10:59:47 -040025namespace SkSL {
26 class Compiler;
27}
28
29class SkParticleExternalValue : public SkSL::ExternalValue {
30public:
31 SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type)
32 : SkSL::ExternalValue(name, type)
33 , fCompiler(compiler)
Brian Osman9a8b8462019-09-19 10:06:36 -040034 , fRandom(nullptr)
35 , fEffect(nullptr) {}
Brian Osman2aa85df2019-08-30 10:59:47 -040036
37 void setRandom(SkRandom* random) { fRandom = random; }
Brian Osman9a8b8462019-09-19 10:06:36 -040038 void setEffect(SkParticleEffect* effect) { fEffect = effect; }
Brian Osman2aa85df2019-08-30 10:59:47 -040039
40protected:
Brian Osman9a8b8462019-09-19 10:06:36 -040041 SkSL::Compiler& fCompiler;
42
43 SkRandom* fRandom;
44 SkParticleEffect* fEffect;
Brian Osman2aa85df2019-08-30 10:59:47 -040045};
46
47class SkParticleBinding : public SkReflected {
48public:
49 SkParticleBinding(const char* name = "name") : fName(name) {}
50
51 REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)
52
53 void visitFields(SkFieldVisitor* v) override;
Brian Osman9dac0d82019-12-02 16:52:51 -050054
Brian Osman2aa85df2019-08-30 10:59:47 -040055 virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;
Brian Osman9dac0d82019-12-02 16:52:51 -050056 virtual void prepare(const skresources::ResourceProvider*) = 0;
Brian Osman2aa85df2019-08-30 10:59:47 -040057
58 static void RegisterBindingTypes();
59
60 /*
61 * All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
62 * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
63 * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
64 * each kind of binding is described below.
65 */
66
Brian Osman6104ba02019-12-03 14:31:37 -050067 // void name(loop) -- Creates an effect instance. Effect will loop if 'loop' is true, otherwise
68 // it's a one-shot. The new effect inherits all properties from the calling effect or particle.
69 static sk_sp<SkParticleBinding> MakeEffect(const char* name,
70 sk_sp<SkParticleEffectParams> effect);
Brian Osman2aa85df2019-08-30 10:59:47 -040071
Brian Osman6104ba02019-12-03 14:31:37 -050072 // float4 name(xy) -- Fetches RGBA data from an image. 'xy' are normalized image coordinates.
73 static sk_sp<SkParticleBinding> MakeImage(const char* name,
74 const char* imagePath, const char* imageName);
75
76 // float4 name(t) -- Fetches position and normal from an SkPath. 't' is the normalized distance
77 // along the path. The return value contains position in .xy and normal in .zw.
78 static sk_sp<SkParticleBinding> MakePath(const char* name,
79 const char* pathPath, const char* pathName);
Brian Osman9a8b8462019-09-19 10:06:36 -040080
Brian Osman2aa85df2019-08-30 10:59:47 -040081protected:
82 SkString fName;
83};
84
85#endif // SkParticleBinding_DEFINED