bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 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 GrTBackendEffectFactory_DEFINED |
| 9 | #define GrTBackendEffectFactory_DEFINED |
| 10 | |
| 11 | #include "GrBackendEffectFactory.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 12 | #include "GrDrawEffect.h" |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 13 | #include "gl/GrGLProgramEffects.h" |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 16 | * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. This can be used by |
| 17 | * most GrEffect subclasses to implement the GrEffect::getFactory() method: |
| 18 | * |
| 19 | * const GrBackendEffectFactory& MyEffect::getFactory() const { |
| 20 | * return GrTBackendEffectFactory<MyEffect>::getInstance(); |
| 21 | * } |
| 22 | * |
| 23 | * Using this class requires that the GrEffect subclass always produces the same GrGLEffect |
| 24 | * subclass. Additionally, It adds the following requirements to the GrEffect and GrGLEffect |
| 25 | * subclasses: |
| 26 | * |
| 27 | * 1. The GrGLEffect used by GrEffect subclass MyEffect must be named or typedef'ed to |
| 28 | * MyEffect::GLEffect. |
| 29 | * 2. MyEffect::GLEffect must have a static function: |
| 30 | * EffectKey GenKey(const GrDrawEffect, const GrGLCaps&) |
| 31 | * which generates a key that maps 1 to 1 with code variations emitted by |
| 32 | * MyEffect::GLEffect::emitCode(). |
| 33 | * 3. MyEffect must have a static function: |
| 34 | * const char* Name() |
| 35 | * which returns a human-readable name for the effect. |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 36 | */ |
| 37 | template <typename EffectClass> |
| 38 | class GrTBackendEffectFactory : public GrBackendEffectFactory { |
| 39 | |
| 40 | public: |
| 41 | typedef typename EffectClass::GLEffect GLEffect; |
| 42 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 43 | /** Returns a human-readable name for the effect. Implemented using GLEffect::Name as described |
| 44 | * in this class's comment. */ |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 45 | virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } |
| 46 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 47 | |
| 48 | /** Implemented using GLEffect::GenKey as described in this class's comment. */ |
| 49 | virtual void getGLEffectKey(const GrDrawEffect& drawEffect, |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 50 | const GrGLCaps& caps, |
| 51 | GrEffectKeyBuilder* b) const SK_OVERRIDE { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 52 | EffectKey effectKey = GLEffect::GenKey(drawEffect, caps); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 53 | b->add32(effectKey); |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** Returns a new instance of the appropriate *GL* implementation class |
| 57 | for the given GrEffect; caller is responsible for deleting |
| 58 | the object. */ |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 59 | virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 60 | return SkNEW_ARGS(GLEffect, (*this, drawEffect)); |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 61 | } |
| 62 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 63 | /** This class is a singleton. This function returns the single instance. */ |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 64 | static const GrBackendEffectFactory& getInstance() { |
| 65 | static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; |
| 66 | static const GrTBackendEffectFactory* gInstance; |
| 67 | if (!gInstance) { |
| 68 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
| 69 | GrTBackendEffectFactory); |
| 70 | } |
| 71 | return *gInstance; |
| 72 | } |
| 73 | |
| 74 | protected: |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 75 | GrTBackendEffectFactory() {} |
bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | #endif |