156315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com/* 256315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com * Copyright 2012 Google Inc. 356315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com * 456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be 556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com * found in the LICENSE file. 656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com */ 756315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 856315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com#ifndef GrTBackendEffectFactory_DEFINED 956315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com#define GrTBackendEffectFactory_DEFINED 1056315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 1156315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com#include "GrBackendEffectFactory.h" 12c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com#include "GrDrawEffect.h" 133390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org#include "gl/GrGLProgramEffects.h" 1456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 1556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com/** 1656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. 1756315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com */ 1856315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.comtemplate <typename EffectClass> 1956315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.comclass GrTBackendEffectFactory : public GrBackendEffectFactory { 2056315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 2156315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.compublic: 2256315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com typedef typename EffectClass::GLEffect GLEffect; 2356315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 2456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com /** Returns a human-readable name that is accessible via GrEffect or 2556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com GrGLEffect and is consistent between the two of them. 2656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com */ 2756315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } 2856315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 2956315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com /** Returns a value that identifies the GLSL shader code generated by 3056315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com a GrEffect. This enables caching of generated shaders. Part of the 3156315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com id identifies the GrEffect subclass. The remainder is based 3256315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com on the aspects of the GrEffect object's configuration that affect 3356315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com GLSL code generation. */ 34c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com virtual EffectKey glEffectKey(const GrDrawEffect& drawEffect, 3556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com const GrGLCaps& caps) const SK_OVERRIDE { 36f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(kIllegalEffectClassID != fEffectClassID); 37c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com EffectKey effectKey = GLEffect::GenKey(drawEffect, caps); 383390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org EffectKey textureKey = GrGLProgramEffects::GenTextureKey(drawEffect, caps); 393390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org EffectKey transformKey = GrGLProgramEffects::GenTransformKey(drawEffect); 403390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org EffectKey attribKey = GrGLProgramEffects::GenAttribKey(drawEffect); 41515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG 4277af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com static const EffectKey kIllegalEffectKeyMask = (uint16_t) (~((1U << kEffectKeyBits) - 1)); 4377af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com SkASSERT(!(kIllegalEffectKeyMask & effectKey)); 4456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 4556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com static const EffectKey kIllegalTextureKeyMask = (uint16_t) (~((1U << kTextureKeyBits) - 1)); 46f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(!(kIllegalTextureKeyMask & textureKey)); 47ff6ea2663f76aa85ec55ddd0f00ca7906f1bc4e3commit-bot@chromium.org 4877af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com static const EffectKey kIllegalTransformKeyMask = (uint16_t) (~((1U << kTransformKeyBits) - 1)); 4977af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com SkASSERT(!(kIllegalTransformKeyMask & transformKey)); 5077af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com 51ff6ea2663f76aa85ec55ddd0f00ca7906f1bc4e3commit-bot@chromium.org static const EffectKey kIllegalAttribKeyMask = (uint16_t) (~((1U << kAttribKeyBits) - 1)); 52f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(!(kIllegalAttribKeyMask & textureKey)); 5377af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com 5477af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com static const EffectKey kIllegalClassIDMask = (uint16_t) (~((1U << kClassIDBits) - 1)); 5577af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com SkASSERT(!(kIllegalClassIDMask & fEffectClassID)); 5656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com#endif 5777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com return (fEffectClassID << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits+kAttribKeyBits)) | 5877af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com (attribKey << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits)) | 5977af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com (transformKey << (kEffectKeyBits+kTextureKeyBits)) | 6077af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com (textureKey << kEffectKeyBits) | 6177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com (effectKey); 6256315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com } 6356315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 6456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com /** Returns a new instance of the appropriate *GL* implementation class 6556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com for the given GrEffect; caller is responsible for deleting 6656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com the object. */ 67c2f7824436d05da6e8514d06a54773538aace028commit-bot@chromium.org virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE { 68c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com return SkNEW_ARGS(GLEffect, (*this, drawEffect)); 6956315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com } 7056315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 7156315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com /** This class is a singleton. This function returns the single instance. 7256315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com */ 7356315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com static const GrBackendEffectFactory& getInstance() { 7456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; 7556315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com static const GrTBackendEffectFactory* gInstance; 7656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com if (!gInstance) { 7756315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), 7856315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com GrTBackendEffectFactory); 7956315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com } 8056315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com return *gInstance; 8156315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com } 8256315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 8356315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.comprotected: 8456315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com GrTBackendEffectFactory() { 8577af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com fEffectClassID = GenID(); 8656315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com } 8756315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com}; 8856315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com 8956315b979b4a10e3f722f63d6e042e217712c6c0bsalomon@google.com#endif 90