Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 1 | /* |
| 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 Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 13 | #include "src/sksl/SkSLExternalFunction.h" |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 14 | |
| 15 | #include <memory> |
| 16 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 17 | class SkParticleEffect; |
| 18 | class SkParticleEffectParams; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 19 | |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 20 | namespace skresources { |
| 21 | class ResourceProvider; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 22 | } // namespace skresources |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 23 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 24 | namespace SkSL { |
| 25 | class Compiler; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 26 | } // namespace SkSL |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 27 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 28 | class SkParticleExternalValue : public SkSL::ExternalFunction { |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 29 | public: |
| 30 | SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type) |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 31 | : SkSL::ExternalFunction(name, type) |
Brian Osman | ace3f29 | 2021-01-12 13:18:52 -0500 | [diff] [blame] | 32 | , fCompiler(compiler) {} |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 33 | |
| 34 | protected: |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 35 | SkSL::Compiler& fCompiler; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | class SkParticleBinding : public SkReflected { |
| 39 | public: |
| 40 | SkParticleBinding(const char* name = "name") : fName(name) {} |
| 41 | |
| 42 | REFLECTED_ABSTRACT(SkParticleBinding, SkReflected) |
| 43 | |
| 44 | void visitFields(SkFieldVisitor* v) override; |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 45 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 46 | virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0; |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 47 | virtual void prepare(const skresources::ResourceProvider*) = 0; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 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 | |
Brian Osman | 6104ba0 | 2019-12-03 14:31:37 -0500 | [diff] [blame] | 58 | // 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 Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 66 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 67 | protected: |
| 68 | SkString fName; |
| 69 | }; |
| 70 | |
| 71 | #endif // SkParticleBinding_DEFINED |