blob: a2a762feec309d53f7b697e90db42a8809aed799 [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 Osmane89d8ea2021-01-20 14:01:30 -050017class SkArenaAlloc;
Brian Osman9a8b8462019-09-19 10:06:36 -040018class SkParticleEffect;
19class SkParticleEffectParams;
Brian Osman2aa85df2019-08-30 10:59:47 -040020
Brian Osman9dac0d82019-12-02 16:52:51 -050021namespace skresources {
22 class ResourceProvider;
John Stilesa6841be2020-08-06 14:11:56 -040023} // namespace skresources
Brian Osman9dac0d82019-12-02 16:52:51 -050024
Brian Osman2aa85df2019-08-30 10:59:47 -040025namespace SkSL {
26 class Compiler;
John Stilesa6841be2020-08-06 14:11:56 -040027} // namespace SkSL
Brian Osman2aa85df2019-08-30 10:59:47 -040028
Brian Osmane89d8ea2021-01-20 14:01:30 -050029namespace skvm {
30 struct Uniforms;
31} // namespace skvm
32
33class SkParticleExternalFunction : public SkSL::ExternalFunction {
Brian Osman2aa85df2019-08-30 10:59:47 -040034public:
Brian Osmane89d8ea2021-01-20 14:01:30 -050035 SkParticleExternalFunction(const char* name,
36 SkSL::Compiler& compiler,
37 const SkSL::Type& type,
38 skvm::Uniforms* uniforms,
39 SkArenaAlloc* alloc)
40 : SkSL::ExternalFunction(name, type)
41 , fCompiler(compiler)
42 , fUniforms(uniforms)
43 , fAlloc(alloc) {}
Brian Osman2aa85df2019-08-30 10:59:47 -040044
45protected:
Brian Osmane89d8ea2021-01-20 14:01:30 -050046 SkSL::Compiler& fCompiler;
47 skvm::Uniforms* fUniforms;
48 SkArenaAlloc* fAlloc;
Brian Osman2aa85df2019-08-30 10:59:47 -040049};
50
51class SkParticleBinding : public SkReflected {
52public:
53 SkParticleBinding(const char* name = "name") : fName(name) {}
54
55 REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)
56
57 void visitFields(SkFieldVisitor* v) override;
Brian Osman9dac0d82019-12-02 16:52:51 -050058
Brian Osmane89d8ea2021-01-20 14:01:30 -050059 virtual std::unique_ptr<SkParticleExternalFunction> toFunction(SkSL::Compiler&,
60 skvm::Uniforms*,
61 SkArenaAlloc*) = 0;
Brian Osman9dac0d82019-12-02 16:52:51 -050062 virtual void prepare(const skresources::ResourceProvider*) = 0;
Brian Osman2aa85df2019-08-30 10:59:47 -040063
64 static void RegisterBindingTypes();
65
66 /*
67 * All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
68 * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
69 * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
70 * each kind of binding is described below.
71 */
72
Brian Osman6104ba02019-12-03 14:31:37 -050073 // float4 name(xy) -- Fetches RGBA data from an image. 'xy' are normalized image coordinates.
74 static sk_sp<SkParticleBinding> MakeImage(const char* name,
75 const char* imagePath, const char* imageName);
76
77 // float4 name(t) -- Fetches position and normal from an SkPath. 't' is the normalized distance
78 // along the path. The return value contains position in .xy and normal in .zw.
79 static sk_sp<SkParticleBinding> MakePath(const char* name,
80 const char* pathPath, const char* pathName);
Brian Osman9a8b8462019-09-19 10:06:36 -040081
Brian Osman2aa85df2019-08-30 10:59:47 -040082protected:
83 SkString fName;
84};
85
86#endif // SkParticleBinding_DEFINED