OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrTBackendEffectFactory_DEFINED | 8 #ifndef GrTBackendEffectFactory_DEFINED |
9 #define GrTBackendEffectFactory_DEFINED | 9 #define GrTBackendEffectFactory_DEFINED |
10 | 10 |
11 #include "GrBackendEffectFactory.h" | 11 #include "GrBackendEffectFactory.h" |
12 #include "GrDrawEffect.h" | 12 #include "GrDrawEffect.h" |
13 #include "gl/GrGLProgramEffects.h" | 13 #include "gl/GrGLProgramEffects.h" |
14 | 14 |
15 /** | 15 /** |
16 * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. | 16 * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. Thi
s 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 an
d 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. |
17 */ | 36 */ |
18 template <typename EffectClass> | 37 template <typename EffectClass> |
19 class GrTBackendEffectFactory : public GrBackendEffectFactory { | 38 class GrTBackendEffectFactory : public GrBackendEffectFactory { |
20 | 39 |
21 public: | 40 public: |
22 typedef typename EffectClass::GLEffect GLEffect; | 41 typedef typename EffectClass::GLEffect GLEffect; |
23 | 42 |
24 /** Returns a human-readable name that is accessible via GrEffect or | 43 /** Returns a human-readable name for the effect. Implemented using GLEffect
::Name as described |
25 GrGLEffect and is consistent between the two of them. | 44 * in this class's comment. */ |
26 */ | |
27 virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } | 45 virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } |
28 | 46 |
29 /** Generates an effect's key. This enables caching of generated shaders. Pa
rt of the | 47 |
30 id identifies the GrEffect subclass. The remainder is based on the aspec
ts of the | 48 /** Implemented using GLEffect::GenKey as described in this class's comment.
*/ |
31 GrEffect object's configuration that affect GLSL code generation. If thi
s fails | 49 virtual void getGLEffectKey(const GrDrawEffect& drawEffect, |
32 then program generation should be aborted. Failure occurs if the effect
uses more | |
33 transforms, attributes, or textures than the key has space for. */ | |
34 virtual bool getGLEffectKey(const GrDrawEffect& drawEffect, | |
35 const GrGLCaps& caps, | 50 const GrGLCaps& caps, |
36 GrEffectKeyBuilder* b) const SK_OVERRIDE { | 51 GrEffectKeyBuilder* b) const SK_OVERRIDE { |
37 SkASSERT(kIllegalEffectClassID != fEffectClassID); | |
38 EffectKey effectKey = GLEffect::GenKey(drawEffect, caps); | 52 EffectKey effectKey = GLEffect::GenKey(drawEffect, caps); |
39 EffectKey textureKey = GrGLProgramEffects::GenTextureKey(drawEffect, cap
s); | |
40 EffectKey transformKey = GrGLProgramEffects::GenTransformKey(drawEffect)
; | |
41 EffectKey attribKey = GrGLProgramEffects::GenAttribKey(drawEffect); | |
42 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | |
43 if ((textureKey | transformKey | attribKey | fEffectClassID) & kMetaKeyI
nvalidMask) { | |
44 return false; | |
45 } | |
46 | |
47 // effectKey must be first because it is what will be returned by | |
48 // GrGLProgramDesc::EffectKeyProvider and passed to the GrGLEffect as it
s key. | |
49 b->add32(effectKey); | 53 b->add32(effectKey); |
50 b->add32(textureKey << 16 | transformKey); | |
51 b->add32(fEffectClassID << 16 | attribKey); | |
52 return true; | |
53 } | 54 } |
54 | 55 |
55 /** Returns a new instance of the appropriate *GL* implementation class | 56 /** Returns a new instance of the appropriate *GL* implementation class |
56 for the given GrEffect; caller is responsible for deleting | 57 for the given GrEffect; caller is responsible for deleting |
57 the object. */ | 58 the object. */ |
58 virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const S
K_OVERRIDE { | 59 virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const S
K_OVERRIDE { |
59 return SkNEW_ARGS(GLEffect, (*this, drawEffect)); | 60 return SkNEW_ARGS(GLEffect, (*this, drawEffect)); |
60 } | 61 } |
61 | 62 |
62 /** This class is a singleton. This function returns the single instance. | 63 /** This class is a singleton. This function returns the single instance. */ |
63 */ | |
64 static const GrBackendEffectFactory& getInstance() { | 64 static const GrBackendEffectFactory& getInstance() { |
65 static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; | 65 static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; |
66 static const GrTBackendEffectFactory* gInstance; | 66 static const GrTBackendEffectFactory* gInstance; |
67 if (!gInstance) { | 67 if (!gInstance) { |
68 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), | 68 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
69 GrTBackendEffectFactory); | 69 GrTBackendEffectFactory); |
70 } | 70 } |
71 return *gInstance; | 71 return *gInstance; |
72 } | 72 } |
73 | 73 |
74 protected: | 74 protected: |
75 GrTBackendEffectFactory() { | 75 GrTBackendEffectFactory() {} |
76 fEffectClassID = GenID(); | |
77 } | |
78 }; | 76 }; |
79 | 77 |
80 #endif | 78 #endif |
OLD | NEW |