1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/gpu/GrTBackendEffectFactory.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrTBackendEffectFactory_DEFINED 1.12 +#define GrTBackendEffectFactory_DEFINED 1.13 + 1.14 +#include "GrBackendEffectFactory.h" 1.15 +#include "GrDrawEffect.h" 1.16 +#include "gl/GrGLProgramEffects.h" 1.17 + 1.18 +/** 1.19 + * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. 1.20 + */ 1.21 +template <typename EffectClass> 1.22 +class GrTBackendEffectFactory : public GrBackendEffectFactory { 1.23 + 1.24 +public: 1.25 + typedef typename EffectClass::GLEffect GLEffect; 1.26 + 1.27 + /** Returns a human-readable name that is accessible via GrEffect or 1.28 + GrGLEffect and is consistent between the two of them. 1.29 + */ 1.30 + virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } 1.31 + 1.32 + /** Returns a value that identifies the GLSL shader code generated by 1.33 + a GrEffect. This enables caching of generated shaders. Part of the 1.34 + id identifies the GrEffect subclass. The remainder is based 1.35 + on the aspects of the GrEffect object's configuration that affect 1.36 + GLSL code generation. */ 1.37 + virtual EffectKey glEffectKey(const GrDrawEffect& drawEffect, 1.38 + const GrGLCaps& caps) const SK_OVERRIDE { 1.39 + SkASSERT(kIllegalEffectClassID != fEffectClassID); 1.40 + EffectKey effectKey = GLEffect::GenKey(drawEffect, caps); 1.41 + EffectKey textureKey = GrGLProgramEffects::GenTextureKey(drawEffect, caps); 1.42 + EffectKey transformKey = GrGLProgramEffects::GenTransformKey(drawEffect); 1.43 + EffectKey attribKey = GrGLProgramEffects::GenAttribKey(drawEffect); 1.44 +#ifdef SK_DEBUG 1.45 + static const EffectKey kIllegalEffectKeyMask = (uint16_t) (~((1U << kEffectKeyBits) - 1)); 1.46 + SkASSERT(!(kIllegalEffectKeyMask & effectKey)); 1.47 + 1.48 + static const EffectKey kIllegalTextureKeyMask = (uint16_t) (~((1U << kTextureKeyBits) - 1)); 1.49 + SkASSERT(!(kIllegalTextureKeyMask & textureKey)); 1.50 + 1.51 + static const EffectKey kIllegalTransformKeyMask = (uint16_t) (~((1U << kTransformKeyBits) - 1)); 1.52 + SkASSERT(!(kIllegalTransformKeyMask & transformKey)); 1.53 + 1.54 + static const EffectKey kIllegalAttribKeyMask = (uint16_t) (~((1U << kAttribKeyBits) - 1)); 1.55 + SkASSERT(!(kIllegalAttribKeyMask & textureKey)); 1.56 + 1.57 + static const EffectKey kIllegalClassIDMask = (uint16_t) (~((1U << kClassIDBits) - 1)); 1.58 + SkASSERT(!(kIllegalClassIDMask & fEffectClassID)); 1.59 +#endif 1.60 + return (fEffectClassID << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits+kAttribKeyBits)) | 1.61 + (attribKey << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits)) | 1.62 + (transformKey << (kEffectKeyBits+kTextureKeyBits)) | 1.63 + (textureKey << kEffectKeyBits) | 1.64 + (effectKey); 1.65 + } 1.66 + 1.67 + /** Returns a new instance of the appropriate *GL* implementation class 1.68 + for the given GrEffect; caller is responsible for deleting 1.69 + the object. */ 1.70 + virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE { 1.71 + return SkNEW_ARGS(GLEffect, (*this, drawEffect)); 1.72 + } 1.73 + 1.74 + /** This class is a singleton. This function returns the single instance. 1.75 + */ 1.76 + static const GrBackendEffectFactory& getInstance() { 1.77 + static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; 1.78 + static const GrTBackendEffectFactory* gInstance; 1.79 + if (!gInstance) { 1.80 + gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), 1.81 + GrTBackendEffectFactory); 1.82 + } 1.83 + return *gInstance; 1.84 + } 1.85 + 1.86 +protected: 1.87 + GrTBackendEffectFactory() { 1.88 + fEffectClassID = GenID(); 1.89 + } 1.90 +}; 1.91 + 1.92 +#endif