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