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" 12 #include "GrEffectStage.h" 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 */ name()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. */ glEffectKey(const GrEffectStage & stage,const GrGLCaps & caps)33 virtual EffectKey glEffectKey(const GrEffectStage& stage, 34 const GrGLCaps& caps) const SK_OVERRIDE { 35 GrAssert(kIllegalEffectClassID != fEffectClassID); 36 EffectKey effectKey = GLEffect::GenKey(stage, caps); 37 EffectKey textureKey = GLEffect::GenTextureKey(stage.getEffect(), caps); 38 #if GR_DEBUG 39 static const EffectKey kIllegalIDMask = (uint16_t) (~((1U << kEffectKeyBits) - 1)); 40 GrAssert(!(kIllegalIDMask & effectKey)); 41 42 static const EffectKey kIllegalTextureKeyMask = (uint16_t) (~((1U << kTextureKeyBits) - 1)); 43 GrAssert(!(kIllegalTextureKeyMask & textureKey)); 44 #endif 45 return fEffectClassID | (textureKey << kEffectKeyBits) | effectKey; 46 } 47 48 /** Returns a new instance of the appropriate *GL* implementation class 49 for the given GrEffect; caller is responsible for deleting 50 the object. */ createGLInstance(const GrEffectRef & effect)51 virtual GLEffect* createGLInstance(const GrEffectRef& effect) const SK_OVERRIDE { 52 return SkNEW_ARGS(GLEffect, (*this, effect)); 53 } 54 55 /** This class is a singleton. This function returns the single instance. 56 */ getInstance()57 static const GrBackendEffectFactory& getInstance() { 58 static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; 59 static const GrTBackendEffectFactory* gInstance; 60 if (!gInstance) { 61 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), 62 GrTBackendEffectFactory); 63 } 64 return *gInstance; 65 } 66 67 protected: GrTBackendEffectFactory()68 GrTBackendEffectFactory() { 69 fEffectClassID = GenID() << (kEffectKeyBits + kTextureKeyBits) ; 70 } 71 }; 72 73 #endif 74