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 |
17 class GrGLEffect; | 27 class GrGLEffect; |
18 class GrGLCaps; | 28 class GrGLCaps; |
19 class GrDrawEffect; | 29 class GrDrawEffect; |
20 | 30 |
21 /** | 31 /** |
22 * Used by effects to build their keys. It incorporates each per-effect key into
a larger shader key. | 32 * Used by effects to build their keys. It incorpates each per-effect key into a
larger shader key. |
23 */ | 33 */ |
24 class GrEffectKeyBuilder { | 34 class GrEffectKeyBuilder { |
25 public: | 35 public: |
26 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun
t(0) { | 36 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun
t(0) { |
27 SkASSERT(0 == fData->count() % sizeof(uint32_t)); | 37 SkASSERT(0 == fData->count() % sizeof(uint32_t)); |
28 } | 38 } |
29 | 39 |
30 void add32(uint32_t v) { | 40 void add32(uint32_t v) { |
31 ++fCount; | 41 ++fCount; |
32 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); | 42 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); |
33 } | 43 } |
34 | 44 |
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 | |
43 size_t size() const { return sizeof(uint32_t) * fCount; } | 45 size_t size() const { return sizeof(uint32_t) * fCount; } |
44 | 46 |
45 private: | 47 private: |
46 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. | 48 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. |
47 int fCount; // number of uint32_ts added to fData by the
effect. | 49 int fCount; // number of uint32_ts added to fData by the
effect. |
48 }; | 50 }; |
49 | 51 |
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 */ | |
67 class GrBackendEffectFactory : SkNoncopyable { | 52 class GrBackendEffectFactory : SkNoncopyable { |
68 public: | 53 public: |
69 typedef uint32_t EffectKey; | 54 typedef uint32_t EffectKey; |
70 | 55 |
71 /** | 56 virtual bool getGLEffectKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKe
yBuilder*) const = 0; |
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 */ | |
82 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; | 57 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; |
83 | 58 |
84 /** | 59 bool operator ==(const GrBackendEffectFactory& b) const { |
85 * Produces a human-reable name for the effect. | 60 return fEffectClassID == b.fEffectClassID; |
86 */ | 61 } |
| 62 bool operator !=(const GrBackendEffectFactory& b) const { |
| 63 return !(*this == b); |
| 64 } |
| 65 |
87 virtual const char* name() const = 0; | 66 virtual const char* name() const = 0; |
88 | 67 |
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 | |
96 protected: | 68 protected: |
97 GrBackendEffectFactory() : fEffectClassID(GenID()) {} | |
98 virtual ~GrBackendEffectFactory() {} | |
99 | |
100 private: | |
101 enum { | 69 enum { |
102 kIllegalEffectClassID = 0, | 70 kIllegalEffectClassID = 0, |
103 }; | 71 }; |
104 | 72 |
105 static uint32_t GenID() { | 73 GrBackendEffectFactory() { |
| 74 fEffectClassID = kIllegalEffectClassID; |
| 75 } |
| 76 virtual ~GrBackendEffectFactory() {} |
| 77 |
| 78 static int32_t GenID() { |
106 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The | 79 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
107 // atomic inc returns the old value not the incremented value. So we add | 80 // atomic inc returns the old value not the incremented value. So we add |
108 // 1 to the returned value. | 81 // 1 to the returned value. |
109 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrEffectClassID))
+ 1; | 82 int32_t id = 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 } | |
114 return id; | 83 return id; |
115 } | 84 } |
116 | 85 |
117 const uint32_t fEffectClassID; | 86 int32_t fEffectClassID; |
| 87 |
| 88 private: |
118 static int32_t fCurrEffectClassID; | 89 static int32_t fCurrEffectClassID; |
119 }; | 90 }; |
120 | 91 |
121 #endif | 92 #endif |
OLD | NEW |