blob: 450eb68ee563e67ef5b2b2a13adca11791ed8994 [file] [log] [blame]
bsalomon@google.com56315b92012-10-29 20:02:06 +00001/*
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"
bsalomon@google.comc7818882013-03-20 19:19:53 +000012#include "GrDrawEffect.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000013#include "gl/GrGLProgramEffects.h"
bsalomon@google.com56315b92012-10-29 20:02:06 +000014
15/**
bsalomon929f29a2014-07-17 07:55:11 -070016 * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton. This 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 and 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.
bsalomon@google.com56315b92012-10-29 20:02:06 +000036 */
37template <typename EffectClass>
38class GrTBackendEffectFactory : public GrBackendEffectFactory {
39
40public:
41 typedef typename EffectClass::GLEffect GLEffect;
42
bsalomon929f29a2014-07-17 07:55:11 -070043 /** Returns a human-readable name for the effect. Implemented using GLEffect::Name as described
44 * in this class's comment. */
bsalomon@google.com56315b92012-10-29 20:02:06 +000045 virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); }
46
bsalomon929f29a2014-07-17 07:55:11 -070047
48 /** Implemented using GLEffect::GenKey as described in this class's comment. */
49 virtual void getGLEffectKey(const GrDrawEffect& drawEffect,
bsalomon848faf02014-07-11 10:01:02 -070050 const GrGLCaps& caps,
51 GrEffectKeyBuilder* b) const SK_OVERRIDE {
bsalomon63e99f72014-07-21 08:03:14 -070052 GLEffect::GenKey(drawEffect, caps, b);
bsalomon@google.com56315b92012-10-29 20:02:06 +000053 }
54
55 /** Returns a new instance of the appropriate *GL* implementation class
56 for the given GrEffect; caller is responsible for deleting
57 the object. */
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000058 virtual GrGLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE {
bsalomon@google.comc7818882013-03-20 19:19:53 +000059 return SkNEW_ARGS(GLEffect, (*this, drawEffect));
bsalomon@google.com56315b92012-10-29 20:02:06 +000060 }
61
bsalomon929f29a2014-07-17 07:55:11 -070062 /** This class is a singleton. This function returns the single instance. */
bsalomon@google.com56315b92012-10-29 20:02:06 +000063 static const GrBackendEffectFactory& getInstance() {
64 static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem;
65 static const GrTBackendEffectFactory* gInstance;
66 if (!gInstance) {
67 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(),
68 GrTBackendEffectFactory);
69 }
70 return *gInstance;
71 }
72
73protected:
bsalomon929f29a2014-07-17 07:55:11 -070074 GrTBackendEffectFactory() {}
bsalomon@google.com56315b92012-10-29 20:02:06 +000075};
76
77#endif