OLD | NEW |
| (Empty) |
1 /* | |
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 GrBackendProcessorFactory_DEFINED | |
9 #define GrBackendProcessorFactory_DEFINED | |
10 | |
11 #include "GrTypes.h" | |
12 #include "SkTemplates.h" | |
13 #include "SkThread.h" | |
14 #include "SkTypes.h" | |
15 #include "SkTArray.h" | |
16 | |
17 class GrGLProcessor; | |
18 class GrGLCaps; | |
19 class GrProcessor; | |
20 | |
21 /** | |
22 * Used by processors to build their keys. It incorporates each per-processor ke
y into a larger shader | |
23 * key. | |
24 */ | |
25 class GrProcessorKeyBuilder { | |
26 public: | |
27 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fC
ount(0) { | |
28 SkASSERT(0 == fData->count() % sizeof(uint32_t)); | |
29 } | |
30 | |
31 void add32(uint32_t v) { | |
32 ++fCount; | |
33 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); | |
34 } | |
35 | |
36 /** Inserts count uint32_ts into the key. The returned pointer is only valid
until the next | |
37 add*() call. */ | |
38 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { | |
39 SkASSERT(count > 0); | |
40 fCount += count; | |
41 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); | |
42 } | |
43 | |
44 size_t size() const { return sizeof(uint32_t) * fCount; } | |
45 | |
46 private: | |
47 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. | |
48 int fCount; // number of uint32_ts added to fData by the
processor. | |
49 }; | |
50 | |
51 /** | |
52 * Given a GrProcessor of a particular type, creates the corresponding graphics-
backend-specific | |
53 * processor object. It also tracks equivalence of shaders generated via a key.
The factory for an | |
54 * processor is accessed via GrProcessor::getFactory(). Each factory instance is
assigned an ID at | |
55 * construction. The ID of GrProcessor::getFactory() is used as a type identifie
r. Thus, a | |
56 * GrProcessor subclass must always return the same object from getFactory() and
that factory object | |
57 * must be unique to the GrProcessor subclass (and unique from any further deriv
ed subclasses). | |
58 * | |
59 * Rather than subclassing this class themselves, it is recommended that GrProce
ssor authors use | |
60 * the templated subclass GrTBackendProcessorFactory by writing their getFactory
() method as: | |
61 * | |
62 * const GrBackendProcessorFactory& MyProcessor::getFactory() const { | |
63 * return GrTBackendProcessorFactory<MyProcessor>::getInstance(); | |
64 * } | |
65 * | |
66 * Using GrTBackendProcessorFactory places a few constraints on the processor. S
ee that class's | |
67 * comments. | |
68 */ | |
69 class GrBackendProcessorFactory : SkNoncopyable { | |
70 public: | |
71 /** | |
72 * Produces a human-reable name for the v. | |
73 */ | |
74 virtual const char* name() const = 0; | |
75 | |
76 /** | |
77 * A unique value for every instance of this factory. It is automatically in
corporated into the | |
78 * processor's key. This allows keys generated by getGLProcessorKey() to onl
y be unique within a | |
79 * GrProcessor subclass and not necessarily across subclasses. | |
80 */ | |
81 uint32_t classID() const { return fProcessorClassID; } | |
82 | |
83 protected: | |
84 GrBackendProcessorFactory() : fProcessorClassID(GenClassID()) {} | |
85 virtual ~GrBackendProcessorFactory() {} | |
86 | |
87 private: | |
88 enum { | |
89 kIllegalProcessorClassID = 0, | |
90 }; | |
91 | |
92 static uint32_t GenClassID() { | |
93 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassI
D. The | |
94 // atomic inc returns the old value not the incremented value. So we add | |
95 // 1 to the returned value. | |
96 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrProcessorClassID
)) + 1; | |
97 if (!id) { | |
98 SkFAIL("This should never wrap as it should only be called once for
each GrProcessor " | |
99 "subclass."); | |
100 } | |
101 return id; | |
102 } | |
103 | |
104 const uint32_t fProcessorClassID; | |
105 static int32_t fCurrProcessorClassID; | |
106 }; | |
107 | |
108 class GrFragmentProcessor; | |
109 class GrGeometryProcessor; | |
110 class GrXferProcessor; | |
111 class GrGLFragmentProcessor; | |
112 class GrGLGeometryProcessor; | |
113 class GrGLXferProcessor; | |
114 | |
115 /** | |
116 * Backend processor factory cannot actually create anything, it is up to subcla
sses to implement | |
117 * a create binding which matches Gr to GL in a type safe way | |
118 */ | |
119 | |
120 class GrBackendFragmentProcessorFactory : public GrBackendProcessorFactory { | |
121 public: | |
122 /** | |
123 * Generates an processor's key. The key is based on the aspects of the GrPr
ocessor object's | |
124 * configuration that affect GLSL code generation. Two GrProcessor instances
that would cause | |
125 * this->createGLInstance()->emitCode() to produce different code must produ
ce different keys. | |
126 */ | |
127 virtual void getGLProcessorKey(const GrFragmentProcessor&, | |
128 const GrGLCaps&, | |
129 GrProcessorKeyBuilder*) const = 0; | |
130 | |
131 /** | |
132 * Creates a GrGLProcessor instance that is used both to generate code for t
he GrProcessor in a | |
133 * GLSL program and to manage updating uniforms for the program when it is u
sed. | |
134 */ | |
135 virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor&)
const = 0; | |
136 }; | |
137 | |
138 class GrBackendXferProcessorFactory : public GrBackendProcessorFactory { | |
139 public: | |
140 /** | |
141 * Creates a GrGLProcessor instance that is used both to generate code for t
he GrProcessor in a | |
142 * GLSL program and to manage updating uniforms for the program when it is u
sed. | |
143 */ | |
144 virtual GrGLXferProcessor* createGLInstance(const GrXferProcessor&) const =
0; | |
145 }; | |
146 | |
147 class GrBatchTracker; | |
148 | |
149 class GrBackendGeometryProcessorFactory : public GrBackendProcessorFactory { | |
150 public: | |
151 /** | |
152 * Generates an processor's key. The key is based on the aspects of the GrPr
ocessor object's | |
153 * configuration that affect GLSL code generation. Two GrProcessor instances
that would cause | |
154 * this->createGLInstance()->emitCode() to produce different code must produ
ce different keys. | |
155 */ | |
156 virtual void getGLProcessorKey(const GrGeometryProcessor&, | |
157 const GrBatchTracker&, | |
158 const GrGLCaps&, | |
159 GrProcessorKeyBuilder*) const = 0; | |
160 | |
161 /** | |
162 * Creates a GrGLProcessor instance that is used both to generate code for t
he GrProcessor in a | |
163 * GLSL program and to manage updating uniforms for the program when it is u
sed. | |
164 */ | |
165 virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor&, | |
166 const GrBatchTracker&) const
= 0; | |
167 }; | |
168 | |
169 #endif | |
OLD | NEW |