OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrBackendEffectFactory_DEFINED | 8 #ifndef GrBackendEffectFactory_DEFINED |
9 #define GrBackendEffectFactory_DEFINED | 9 #define GrBackendEffectFactory_DEFINED |
10 | 10 |
11 #include "GrTypes.h" | 11 #include "GrTypes.h" |
12 #include "SkTemplates.h" | 12 #include "SkTemplates.h" |
13 #include "SkThread.h" | 13 #include "SkThread.h" |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 #include "SkTArray.h" | 15 #include "SkTArray.h" |
16 | 16 |
17 /** Given a GrEffect of a particular type, creates the corresponding graphics-ba
ckend-specific | |
18 effect object. Also tracks equivalence of shaders generated via a key. Each
factory instance | |
19 is assigned a generation ID at construction. The ID of the return of GrEffec
t::getFactory() | |
20 is used as a type identifier. Thus a GrEffect subclass must return a singlet
on from | |
21 getFactory(). GrEffect subclasses should use the derived class GrTBackendEff
ectFactory that is | |
22 templated on the GrEffect subclass as their factory object. It requires that
the GrEffect | |
23 subclass has a nested class (or typedef) GLEffect which is its GL implementa
tion and a subclass | |
24 of GrGLEffect. | |
25 */ | |
26 | |
27 class GrGLEffect; | 17 class GrGLEffect; |
28 class GrGLCaps; | 18 class GrGLCaps; |
29 class GrDrawEffect; | 19 class GrDrawEffect; |
30 | 20 |
31 /** | 21 /** |
32 * Used by effects to build their keys. It incorpates each per-effect key into a
larger shader key. | 22 * Used by effects to build their keys. It incorporates each per-effect key into
a larger shader key. |
33 */ | 23 */ |
34 class GrEffectKeyBuilder { | 24 class GrEffectKeyBuilder { |
35 public: | 25 public: |
36 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun
t(0) { | 26 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun
t(0) { |
37 SkASSERT(0 == fData->count() % sizeof(uint32_t)); | 27 SkASSERT(0 == fData->count() % sizeof(uint32_t)); |
38 } | 28 } |
39 | 29 |
40 void add32(uint32_t v) { | 30 void add32(uint32_t v) { |
41 ++fCount; | 31 ++fCount; |
42 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); | 32 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); |
43 } | 33 } |
44 | 34 |
| 35 /** Inserts count uint32_ts into the key. The returned pointer is only valid
until the next |
| 36 add*() call. */ |
| 37 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { |
| 38 SkASSERT(count > 0); |
| 39 fCount += count; |
| 40 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); |
| 41 } |
| 42 |
45 size_t size() const { return sizeof(uint32_t) * fCount; } | 43 size_t size() const { return sizeof(uint32_t) * fCount; } |
46 | 44 |
47 private: | 45 private: |
48 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. | 46 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. |
49 int fCount; // number of uint32_ts added to fData by the
effect. | 47 int fCount; // number of uint32_ts added to fData by the
effect. |
50 }; | 48 }; |
51 | 49 |
| 50 /** |
| 51 * Given a GrEffect of a particular type, creates the corresponding graphics-bac
kend-specific |
| 52 * effect object. It also tracks equivalence of shaders generated via a key. The
factory for an |
| 53 * effect is accessed via GrEffect::getFactory(). Each factory instance is assig
ned an ID at |
| 54 * construction. The ID of GrEffect::getFactory() is used as a type identifier.
Thus, a GrEffect |
| 55 * subclass must always return the same object from getFactory() and that factor
y object must be |
| 56 * unique to the GrEffect subclass (and unique from any further derived subclass
es). |
| 57 * |
| 58 * Rather than subclassing this class themselves, it is recommended that GrEffec
t authors use |
| 59 * the templated subclass GrTBackendEffectFactory by writing their getFactory()
method as: |
| 60 * |
| 61 * const GrBackendEffectFactory& MyEffect::getFactory() const { |
| 62 * return GrTBackendEffectFactory<MyEffect>::getInstance(); |
| 63 * } |
| 64 * |
| 65 * Using GrTBackendEffectFactory places a few constraints on the effect. See tha
t class's comments. |
| 66 */ |
52 class GrBackendEffectFactory : SkNoncopyable { | 67 class GrBackendEffectFactory : SkNoncopyable { |
53 public: | 68 public: |
54 typedef uint32_t EffectKey; | 69 typedef uint32_t EffectKey; |
55 | 70 |
56 virtual bool getGLEffectKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKe
yBuilder*) const = 0; | 71 /** |
| 72 * Generates an effect's key. The key is based on the aspects of the GrEffec
t object's |
| 73 * configuration that affect GLSL code generation. Two GrEffect instances th
at would cause |
| 74 * this->createGLInstance()->emitCode() to produce different code must produ
ce different keys. |
| 75 */ |
| 76 virtual void getGLEffectKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKe
yBuilder*) const = 0; |
| 77 |
| 78 /** |
| 79 * Creates a GrGLEffect instance that is used both to generate code for the
GrEffect in a GLSL |
| 80 * program and to manage updating uniforms for the program when it is used. |
| 81 */ |
57 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; | 82 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; |
58 | 83 |
59 bool operator ==(const GrBackendEffectFactory& b) const { | 84 /** |
60 return fEffectClassID == b.fEffectClassID; | 85 * Produces a human-reable name for the effect. |
61 } | 86 */ |
62 bool operator !=(const GrBackendEffectFactory& b) const { | |
63 return !(*this == b); | |
64 } | |
65 | |
66 virtual const char* name() const = 0; | 87 virtual const char* name() const = 0; |
67 | 88 |
| 89 /** |
| 90 * A unique value for every instance of this factory. It is automatically in
corporated into the |
| 91 * effect's key. This allows keys generated by getGLEffectKey() to only be u
nique within a |
| 92 * GrEffect subclass and not necessarily across subclasses. |
| 93 */ |
| 94 uint32_t effectClassID() const { return fEffectClassID; } |
| 95 |
68 protected: | 96 protected: |
| 97 GrBackendEffectFactory() : fEffectClassID(GenID()) {} |
| 98 virtual ~GrBackendEffectFactory() {} |
| 99 |
| 100 private: |
69 enum { | 101 enum { |
70 kIllegalEffectClassID = 0, | 102 kIllegalEffectClassID = 0, |
71 }; | 103 }; |
72 | 104 |
73 GrBackendEffectFactory() { | 105 static uint32_t GenID() { |
74 fEffectClassID = kIllegalEffectClassID; | |
75 } | |
76 virtual ~GrBackendEffectFactory() {} | |
77 | |
78 static int32_t GenID() { | |
79 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The | 106 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
80 // atomic inc returns the old value not the incremented value. So we add | 107 // atomic inc returns the old value not the incremented value. So we add |
81 // 1 to the returned value. | 108 // 1 to the returned value. |
82 int32_t id = sk_atomic_inc(&fCurrEffectClassID) + 1; | 109 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrEffectClassID))
+ 1; |
| 110 if (!id) { |
| 111 SkFAIL("This should never wrap as it should only be called once for
each GrEffect " |
| 112 "subclass."); |
| 113 } |
83 return id; | 114 return id; |
84 } | 115 } |
85 | 116 |
86 int32_t fEffectClassID; | 117 const uint32_t fEffectClassID; |
87 | |
88 private: | |
89 static int32_t fCurrEffectClassID; | 118 static int32_t fCurrEffectClassID; |
90 }; | 119 }; |
91 | 120 |
92 #endif | 121 #endif |
OLD | NEW |