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" |
| 13 | #include "src/sksl/SkSLExternalValue.h" |
| 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 | class SkRandom; |
| 20 | |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 21 | namespace skresources { |
| 22 | class ResourceProvider; |
| 23 | } |
| 24 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 25 | namespace SkSL { |
| 26 | class Compiler; |
| 27 | } |
| 28 | |
| 29 | class SkParticleExternalValue : public SkSL::ExternalValue { |
| 30 | public: |
| 31 | SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type) |
| 32 | : SkSL::ExternalValue(name, type) |
| 33 | , fCompiler(compiler) |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 34 | , fRandom(nullptr) |
| 35 | , fEffect(nullptr) {} |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 36 | |
| 37 | void setRandom(SkRandom* random) { fRandom = random; } |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 38 | void setEffect(SkParticleEffect* effect) { fEffect = effect; } |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 39 | |
| 40 | protected: |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 41 | SkSL::Compiler& fCompiler; |
| 42 | |
| 43 | SkRandom* fRandom; |
| 44 | SkParticleEffect* fEffect; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class SkParticleBinding : public SkReflected { |
| 48 | public: |
| 49 | SkParticleBinding(const char* name = "name") : fName(name) {} |
| 50 | |
| 51 | REFLECTED_ABSTRACT(SkParticleBinding, SkReflected) |
| 52 | |
| 53 | void visitFields(SkFieldVisitor* v) override; |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 54 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 55 | virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0; |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 56 | virtual void prepare(const skresources::ResourceProvider*) = 0; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 57 | |
| 58 | static void RegisterBindingTypes(); |
| 59 | |
| 60 | /* |
| 61 | * All SkParticleBinding objects expose a particular native object to an effect's SkSL code. |
| 62 | * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL. |
| 63 | * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of |
| 64 | * each kind of binding is described below. |
| 65 | */ |
| 66 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 67 | // Binds an SkPath to an effect's SkSL. The path is specified using SVG syntax. It is called |
| 68 | // in the SkSL as 'name(t)'. 't' is a normalized distance along the path. This returns a float4 |
| 69 | // value, containing the position in .xy, and the normal in .zw. |
| 70 | static sk_sp<SkParticleBinding> MakePathBinding(const char* name, const char* path); |
| 71 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 72 | static sk_sp<SkParticleBinding> MakeEffectBinding(const char* name, |
| 73 | sk_sp<SkParticleEffectParams> effect); |
| 74 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 75 | protected: |
| 76 | SkString fName; |
| 77 | }; |
| 78 | |
| 79 | #endif // SkParticleBinding_DEFINED |