blob: fe0843587dc834746d7b2a641e56976f5b7e6986 [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"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050013#include "src/sksl/SkSLExternalFunction.h"
Brian Osman2aa85df2019-08-30 10:59:47 -040014
15#include <memory>
16
Brian Osman9a8b8462019-09-19 10:06:36 -040017class SkParticleEffect;
18class SkParticleEffectParams;
Brian Osman2aa85df2019-08-30 10:59:47 -040019
Brian Osman9dac0d82019-12-02 16:52:51 -050020namespace skresources {
21 class ResourceProvider;
John Stilesa6841be2020-08-06 14:11:56 -040022} // namespace skresources
Brian Osman9dac0d82019-12-02 16:52:51 -050023
Brian Osman2aa85df2019-08-30 10:59:47 -040024namespace SkSL {
25 class Compiler;
John Stilesa6841be2020-08-06 14:11:56 -040026} // namespace SkSL
Brian Osman2aa85df2019-08-30 10:59:47 -040027
Brian Osmanbe0b3b72021-01-06 14:27:35 -050028class SkParticleExternalValue : public SkSL::ExternalFunction {
Brian Osman2aa85df2019-08-30 10:59:47 -040029public:
30 SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type)
Brian Osmanbe0b3b72021-01-06 14:27:35 -050031 : SkSL::ExternalFunction(name, type)
Brian Osmanace3f292021-01-12 13:18:52 -050032 , fCompiler(compiler) {}
Brian Osman2aa85df2019-08-30 10:59:47 -040033
34protected:
Brian Osman9a8b8462019-09-19 10:06:36 -040035 SkSL::Compiler& fCompiler;
Brian Osman2aa85df2019-08-30 10:59:47 -040036};
37
38class SkParticleBinding : public SkReflected {
39public:
40 SkParticleBinding(const char* name = "name") : fName(name) {}
41
42 REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)
43
44 void visitFields(SkFieldVisitor* v) override;
Brian Osman9dac0d82019-12-02 16:52:51 -050045
Brian Osman2aa85df2019-08-30 10:59:47 -040046 virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;
Brian Osman9dac0d82019-12-02 16:52:51 -050047 virtual void prepare(const skresources::ResourceProvider*) = 0;
Brian Osman2aa85df2019-08-30 10:59:47 -040048
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
Brian Osman6104ba02019-12-03 14:31:37 -050058 // float4 name(xy) -- Fetches RGBA data from an image. 'xy' are normalized image coordinates.
59 static sk_sp<SkParticleBinding> MakeImage(const char* name,
60 const char* imagePath, const char* imageName);
61
62 // float4 name(t) -- Fetches position and normal from an SkPath. 't' is the normalized distance
63 // along the path. The return value contains position in .xy and normal in .zw.
64 static sk_sp<SkParticleBinding> MakePath(const char* name,
65 const char* pathPath, const char* pathName);
Brian Osman9a8b8462019-09-19 10:06:36 -040066
Brian Osman2aa85df2019-08-30 10:59:47 -040067protected:
68 SkString fName;
69};
70
71#endif // SkParticleBinding_DEFINED