blob: 3711c7e869398f7082643c8990ad7ca4116dc19d [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/**
16 * Implements GrBackendEffectFactory for a GrEffect subclass as a singleton.
17 */
18template <typename EffectClass>
19class GrTBackendEffectFactory : public GrBackendEffectFactory {
20
21public:
22 typedef typename EffectClass::GLEffect GLEffect;
23
24 /** Returns a human-readable name that is accessible via GrEffect or
25 GrGLEffect and is consistent between the two of them.
26 */
27 virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); }
28
29 /** Returns a value that identifies the GLSL shader code generated by
30 a GrEffect. This enables caching of generated shaders. Part of the
31 id identifies the GrEffect subclass. The remainder is based
32 on the aspects of the GrEffect object's configuration that affect
33 GLSL code generation. */
bsalomon@google.comc7818882013-03-20 19:19:53 +000034 virtual EffectKey glEffectKey(const GrDrawEffect& drawEffect,
bsalomon@google.com56315b92012-10-29 20:02:06 +000035 const GrGLCaps& caps) const SK_OVERRIDE {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(kIllegalEffectClassID != fEffectClassID);
bsalomon@google.comc7818882013-03-20 19:19:53 +000037 EffectKey effectKey = GLEffect::GenKey(drawEffect, caps);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000038 EffectKey textureKey = GrGLProgramEffects::GenTextureKey(drawEffect, caps);
39 EffectKey transformKey = GrGLProgramEffects::GenTransformKey(drawEffect);
40 EffectKey attribKey = GrGLProgramEffects::GenAttribKey(drawEffect);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000041#ifdef SK_DEBUG
bsalomon@google.com77af6802013-10-02 13:04:56 +000042 static const EffectKey kIllegalEffectKeyMask = (uint16_t) (~((1U << kEffectKeyBits) - 1));
43 SkASSERT(!(kIllegalEffectKeyMask & effectKey));
bsalomon@google.com56315b92012-10-29 20:02:06 +000044
45 static const EffectKey kIllegalTextureKeyMask = (uint16_t) (~((1U << kTextureKeyBits) - 1));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000046 SkASSERT(!(kIllegalTextureKeyMask & textureKey));
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000047
bsalomon@google.com77af6802013-10-02 13:04:56 +000048 static const EffectKey kIllegalTransformKeyMask = (uint16_t) (~((1U << kTransformKeyBits) - 1));
49 SkASSERT(!(kIllegalTransformKeyMask & transformKey));
50
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000051 static const EffectKey kIllegalAttribKeyMask = (uint16_t) (~((1U << kAttribKeyBits) - 1));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000052 SkASSERT(!(kIllegalAttribKeyMask & textureKey));
bsalomon@google.com77af6802013-10-02 13:04:56 +000053
54 static const EffectKey kIllegalClassIDMask = (uint16_t) (~((1U << kClassIDBits) - 1));
55 SkASSERT(!(kIllegalClassIDMask & fEffectClassID));
bsalomon@google.com56315b92012-10-29 20:02:06 +000056#endif
bsalomon@google.com77af6802013-10-02 13:04:56 +000057 return (fEffectClassID << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits+kAttribKeyBits)) |
58 (attribKey << (kEffectKeyBits+kTextureKeyBits+kTransformKeyBits)) |
59 (transformKey << (kEffectKeyBits+kTextureKeyBits)) |
60 (textureKey << kEffectKeyBits) |
61 (effectKey);
bsalomon@google.com56315b92012-10-29 20:02:06 +000062 }
63
64 /** Returns a new instance of the appropriate *GL* implementation class
65 for the given GrEffect; caller is responsible for deleting
66 the object. */
bsalomon@google.comc7818882013-03-20 19:19:53 +000067 virtual GLEffect* createGLInstance(const GrDrawEffect& drawEffect) const SK_OVERRIDE {
68 return SkNEW_ARGS(GLEffect, (*this, drawEffect));
bsalomon@google.com56315b92012-10-29 20:02:06 +000069 }
70
71 /** This class is a singleton. This function returns the single instance.
72 */
73 static const GrBackendEffectFactory& getInstance() {
74 static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem;
75 static const GrTBackendEffectFactory* gInstance;
76 if (!gInstance) {
77 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(),
78 GrTBackendEffectFactory);
79 }
80 return *gInstance;
81 }
82
83protected:
84 GrTBackendEffectFactory() {
bsalomon@google.com77af6802013-10-02 13:04:56 +000085 fEffectClassID = GenID();
bsalomon@google.com56315b92012-10-29 20:02:06 +000086 }
87};
88
89#endif