| 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" |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. |
| 16 | */ |
| 17 | template <typename EffectClass> |
| 18 | class GrTBackendEffectFactory : public GrBackendEffectFactory { |
| 19 | |
| 20 | public: |
| 21 | typedef typename EffectClass::GLEffect GLEffect; |
| 22 | |
| 23 | /** Returns a human-readable name that is accessible via GrEffect or |
| 24 | GrGLEffect and is consistent between the two of them. |
| 25 | */ |
| 26 | virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } |
| 27 | |
| 28 | /** Returns a value that identifies the GLSL shader code generated by |
| 29 | a GrEffect. This enables caching of generated shaders. Part of the |
| 30 | id identifies the GrEffect subclass. The remainder is based |
| 31 | on the aspects of the GrEffect object's configuration that affect |
| 32 | GLSL code generation. */ |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 33 | virtual EffectKey glEffectKey(const GrDrawEffect& drawEffect, |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 34 | const GrGLCaps& caps) const SK_OVERRIDE { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 35 | SkASSERT(kIllegalEffectClassID != fEffectClassID); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 36 | EffectKey effectKey = GLEffect::GenKey(drawEffect, caps); |
| 37 | EffectKey textureKey = GLEffect::GenTextureKey(drawEffect, caps); |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 38 | EffectKey transformKey = GLEffect::GenTransformKey(drawEffect); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 39 | EffectKey attribKey = GLEffect::GenAttribKey(drawEffect); |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 40 | #ifdef SK_DEBUG |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 41 | static const EffectKey kIllegalEffectKeyMask = (uint16_t) (~((1U << kEffectKeyBits) - 1)); |
| 42 | SkASSERT(!(kIllegalEffectKeyMask & effectKey)); |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 43 | |
| 44 | static const EffectKey kIllegalTextureKeyMask = (uint16_t) (~((1U << kTextureKeyBits) - 1)); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 45 | SkASSERT(!(kIllegalTextureKeyMask & textureKey)); |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 46 | |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 47 | static const EffectKey kIllegalTransformKeyMask = (uint16_t) (~((1U << kTransformKeyBits) - 1)); |
| 48 | SkASSERT(!(kIllegalTransformKeyMask & transformKey)); |
| 49 | |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 50 | static const EffectKey kIllegalAttribKeyMask = (uint16_t) (~((1U << kAttribKeyBits) - 1)); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 51 | SkASSERT(!(kIllegalAttribKeyMask & textureKey)); |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 52 | |
| 53 | static const EffectKey kIllegalClassIDMask = (uint16_t) (~((1U << kClassIDBits) - 1)); |
| 54 | SkASSERT(!(kIllegalClassIDMask & fEffectClassID)); |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 55 | #endif |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 56 | return (fEffectClassID << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits+kAttribKeyBits)) | |
| 57 | (attribKey << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits)) | |
| 58 | (transformKey << (kEffectKeyBits+kTextureKeyBits)) | |
| 59 | (textureKey << kEffectKeyBits) | |
| 60 | (effectKey); |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** Returns a new instance of the appropriate *GL* implementation class |
| 64 | for the given GrEffect; caller is responsible for deleting |
| 65 | the object. */ |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 66 | virtual GLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE { |
| 67 | return SkNEW_ARGS(GLEffect, (*this, drawEffect)); |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** This class is a singleton. This function returns the single instance. |
| 71 | */ |
| 72 | static const GrBackendEffectFactory& getInstance() { |
| 73 | static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; |
| 74 | static const GrTBackendEffectFactory* gInstance; |
| 75 | if (!gInstance) { |
| 76 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
| 77 | GrTBackendEffectFactory); |
| 78 | } |
| 79 | return *gInstance; |
| 80 | } |
| 81 | |
| 82 | protected: |
| 83 | GrTBackendEffectFactory() { |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 84 | fEffectClassID = GenID(); |
| bsalomon@google.com | 56315b9 | 2012-10-29 20:02:06 +0000 | [diff] [blame] | 85 | } |
| 86 | }; |
| 87 | |
| 88 | #endif |