2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrTBackendEffectFactory_DEFINED
9 #define GrTBackendEffectFactory_DEFINED
11 #include "GrBackendEffectFactory.h"
12 #include "GrDrawEffect.h"
13 #include "gl/GrGLProgramEffects.h"
16 * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. This can be used by
17 * most GrEffect subclasses to implement the GrEffect::getFactory() method:
19 * const GrBackendEffectFactory& MyEffect::getFactory() const {
20 * return GrTBackendEffectFactory<MyEffect>::getInstance();
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 and GrGLEffect
27 * 1. The GrGLEffect used by GrEffect subclass MyEffect must be named or typedef'ed to
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:
35 * which returns a human-readable name for the effect.
37 template <typename EffectClass>
38 class GrTBackendEffectFactory : public GrBackendEffectFactory {
41 typedef typename EffectClass::GLEffect GLEffect;
43 /** Returns a human-readable name for the effect. Implemented using GLEffect::Name as described
44 * in this class's comment. */
45 virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); }
48 /** Implemented using GLEffect::GenKey as described in this class's comment. */
49 virtual void getGLEffectKey(const GrDrawEffect& drawEffect,
51 GrEffectKeyBuilder* b) const SK_OVERRIDE {
52 GLEffect::GenKey(drawEffect, caps, b);
55 /** Returns a new instance of the appropriate *GL* implementation class
56 for the given GrEffect; caller is responsible for deleting
58 virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE {
59 return SkNEW_ARGS(GLEffect, (*this, drawEffect));
62 /** This class is a singleton. This function returns the single instance. */
63 static const GrBackendEffectFactory& getInstance() {
64 static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem;
65 static const GrTBackendEffectFactory* gInstance;
67 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(),
68 GrTBackendEffectFactory);
74 GrTBackendEffectFactory() {}