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 | |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 20 | namespace skresources { |
| 21 | class ResourceProvider; |
| 22 | } |
| 23 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 24 | namespace SkSL { |
| 25 | class Compiler; |
| 26 | } |
| 27 | |
| 28 | class SkParticleExternalValue : public SkSL::ExternalValue { |
| 29 | public: |
| 30 | SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type) |
| 31 | : SkSL::ExternalValue(name, type) |
| 32 | , fCompiler(compiler) |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 33 | , fEffect(nullptr) {} |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 34 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 35 | void setEffect(SkParticleEffect* effect) { fEffect = effect; } |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 36 | |
| 37 | protected: |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 38 | SkSL::Compiler& fCompiler; |
| 39 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 40 | SkParticleEffect* fEffect; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class SkParticleBinding : public SkReflected { |
| 44 | public: |
| 45 | SkParticleBinding(const char* name = "name") : fName(name) {} |
| 46 | |
| 47 | REFLECTED_ABSTRACT(SkParticleBinding, SkReflected) |
| 48 | |
| 49 | void visitFields(SkFieldVisitor* v) override; |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 50 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 51 | virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0; |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 52 | virtual void prepare(const skresources::ResourceProvider*) = 0; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 53 | |
| 54 | static void RegisterBindingTypes(); |
| 55 | |
| 56 | /* |
| 57 | * All SkParticleBinding objects expose a particular native object to an effect's SkSL code. |
| 58 | * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL. |
| 59 | * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of |
| 60 | * each kind of binding is described below. |
| 61 | */ |
| 62 | |
Brian Osman | 6104ba0 | 2019-12-03 14:31:37 -0500 | [diff] [blame] | 63 | // void name(loop) -- Creates an effect instance. Effect will loop if 'loop' is true, otherwise |
| 64 | // it's a one-shot. The new effect inherits all properties from the calling effect or particle. |
| 65 | static sk_sp<SkParticleBinding> MakeEffect(const char* name, |
| 66 | sk_sp<SkParticleEffectParams> effect); |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 67 | |
Brian Osman | 6104ba0 | 2019-12-03 14:31:37 -0500 | [diff] [blame] | 68 | // float4 name(xy) -- Fetches RGBA data from an image. 'xy' are normalized image coordinates. |
| 69 | static sk_sp<SkParticleBinding> MakeImage(const char* name, |
| 70 | const char* imagePath, const char* imageName); |
| 71 | |
| 72 | // float4 name(t) -- Fetches position and normal from an SkPath. 't' is the normalized distance |
| 73 | // along the path. The return value contains position in .xy and normal in .zw. |
| 74 | static sk_sp<SkParticleBinding> MakePath(const char* name, |
| 75 | const char* pathPath, const char* pathName); |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 76 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 77 | protected: |
| 78 | SkString fName; |
| 79 | }; |
| 80 | |
| 81 | #endif // SkParticleBinding_DEFINED |