blob: 184bb07f8d9643418cb5b4d4777faac9d457e5f8 [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
17struct SkCurve;
18struct SkColorCurve;
Brian Osman9a8b8462019-09-19 10:06:36 -040019class SkParticleEffect;
20class SkParticleEffectParams;
Brian Osman2aa85df2019-08-30 10:59:47 -040021class SkRandom;
22
23namespace SkSL {
24 class Compiler;
25}
26
27class SkParticleExternalValue : public SkSL::ExternalValue {
28public:
29 SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type)
30 : SkSL::ExternalValue(name, type)
31 , fCompiler(compiler)
Brian Osman9a8b8462019-09-19 10:06:36 -040032 , fRandom(nullptr)
33 , fEffect(nullptr) {}
Brian Osman2aa85df2019-08-30 10:59:47 -040034
35 void setRandom(SkRandom* random) { fRandom = random; }
Brian Osman9a8b8462019-09-19 10:06:36 -040036 void setEffect(SkParticleEffect* effect) { fEffect = effect; }
Brian Osman2aa85df2019-08-30 10:59:47 -040037
38protected:
Brian Osman9a8b8462019-09-19 10:06:36 -040039 SkSL::Compiler& fCompiler;
40
41 SkRandom* fRandom;
42 SkParticleEffect* fEffect;
Brian Osman2aa85df2019-08-30 10:59:47 -040043};
44
45class SkParticleBinding : public SkReflected {
46public:
47 SkParticleBinding(const char* name = "name") : fName(name) {}
48
49 REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)
50
51 void visitFields(SkFieldVisitor* v) override;
52 virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;
53
54 static void RegisterBindingTypes();
55
56 /*
57 * All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
58 * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
59 * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
60 * each kind of binding is described below.
61 */
62
63 // Binds an SkCurve to an effect's SkSL. The curve is a one-dimensional function, described
64 // in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a single float value.
65 static sk_sp<SkParticleBinding> MakeCurve(const char* name, const SkCurve& curve);
66
67 // Binds an SkColorCurve to an effect's SkSL. The curve is a one-dimensional, function,
68 // described in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a float4 value.
69 static sk_sp<SkParticleBinding> MakeColorCurve(const char* name, const SkColorCurve& curve);
70
71 // Binds an SkPath to an effect's SkSL. The path is specified using SVG syntax. It is called
72 // in the SkSL as 'name(t)'. 't' is a normalized distance along the path. This returns a float4
73 // value, containing the position in .xy, and the normal in .zw.
74 static sk_sp<SkParticleBinding> MakePathBinding(const char* name, const char* path);
75
Brian Osman9a8b8462019-09-19 10:06:36 -040076 static sk_sp<SkParticleBinding> MakeEffectBinding(const char* name,
77 sk_sp<SkParticleEffectParams> effect);
78
Brian Osman2aa85df2019-08-30 10:59:47 -040079protected:
80 SkString fName;
81};
82
83#endif // SkParticleBinding_DEFINED