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