OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 GrXferProcessor_DEFINED | 8 #ifndef GrXferProcessor_DEFINED |
9 #define GrXferProcessor_DEFINED | 9 #define GrXferProcessor_DEFINED |
10 | 10 |
11 #include "GrColor.h" | 11 #include "GrColor.h" |
12 #include "GrProcessor.h" | 12 #include "GrProcessor.h" |
| 13 #include "GrTexture.h" |
13 #include "GrTypes.h" | 14 #include "GrTypes.h" |
14 #include "SkXfermode.h" | 15 #include "SkXfermode.h" |
15 | 16 |
16 class GrDrawTargetCaps; | 17 class GrDrawTargetCaps; |
17 class GrGLCaps; | 18 class GrGLCaps; |
18 class GrGLXferProcessor; | 19 class GrGLXferProcessor; |
19 class GrProcOptInfo; | 20 class GrProcOptInfo; |
20 | 21 |
21 /** | 22 /** |
22 * GrXferProcessor is responsible for implementing the xfer mode that blends the
src color and dst | 23 * GrXferProcessor is responsible for implementing the xfer mode that blends the
src color and dst |
23 * color. It does this by emitting fragment shader code and controlling the fixe
d-function blend | 24 * color. It does this by emitting fragment shader code and controlling the fixe
d-function blend |
24 * state. The inputs to its shader code are the final computed src color and fra
ctional pixel | 25 * state. The inputs to its shader code are the final computed src color and fra
ctional pixel |
25 * coverage. The GrXferProcessor's shader code writes the fragment shader output
color that goes | 26 * coverage. The GrXferProcessor's shader code writes the fragment shader output
color that goes |
26 * into the fixed-function blend. When dual-source blending is available, it may
also write a | 27 * into the fixed-function blend. When dual-source blending is available, it may
also write a |
27 * seconday fragment shader output color. When allowed by the backend API, the G
rXferProcessor may | 28 * seconday fragment shader output color. When allowed by the backend API, the G
rXferProcessor may |
28 * read the destination color. The GrXferProcessor is responsible for setting th
e blend coefficients | 29 * read the destination color. The GrXferProcessor is responsible for setting th
e blend coefficients |
29 * and blend constant color. | 30 * and blend constant color. |
30 * | 31 * |
31 * A GrXferProcessor is never installed directly into our draw state, but instea
d is created from a | 32 * A GrXferProcessor is never installed directly into our draw state, but instea
d is created from a |
32 * GrXPFactory once we have finalized the state of our draw. | 33 * GrXPFactory once we have finalized the state of our draw. |
33 */ | 34 */ |
34 class GrXferProcessor : public GrProcessor { | 35 class GrXferProcessor : public GrProcessor { |
35 public: | 36 public: |
36 /** | 37 /** |
37 * Sets a unique key on the GrProcessorKeyBuilder that is directly associate
d with this xfer | 38 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLProcessorKey(
...) to get the |
38 * processor's GL backend implementation. | 39 * specific subclass's key. |
39 */ | 40 */ |
40 virtual void getGLProcessorKey(const GrGLCaps& caps, | 41 void getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const
; |
41 GrProcessorKeyBuilder* b) const = 0; | |
42 | 42 |
43 /** Returns a new instance of the appropriate *GL* implementation class | 43 /** Returns a new instance of the appropriate *GL* implementation class |
44 for the given GrXferProcessor; caller is responsible for deleting | 44 for the given GrXferProcessor; caller is responsible for deleting |
45 the object. */ | 45 the object. */ |
46 virtual GrGLXferProcessor* createGLInstance() const = 0; | 46 virtual GrGLXferProcessor* createGLInstance() const = 0; |
47 | 47 |
48 /** | 48 /** |
49 * Optimizations for blending / coverage that an OptDrawState should apply t
o itself. | 49 * Optimizations for blending / coverage that an OptDrawState should apply t
o itself. |
50 */ | 50 */ |
51 enum OptFlags { | 51 enum OptFlags { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 BlendInfo() : fWriteColor(true) {} | 96 BlendInfo() : fWriteColor(true) {} |
97 | 97 |
98 GrBlendCoeff fSrcBlend; | 98 GrBlendCoeff fSrcBlend; |
99 GrBlendCoeff fDstBlend; | 99 GrBlendCoeff fDstBlend; |
100 GrColor fBlendConstant; | 100 GrColor fBlendConstant; |
101 bool fWriteColor; | 101 bool fWriteColor; |
102 }; | 102 }; |
103 | 103 |
104 virtual void getBlendInfo(BlendInfo* blendInfo) const = 0; | 104 virtual void getBlendInfo(BlendInfo* blendInfo) const = 0; |
105 | 105 |
106 /** Will this prceossor read the destination pixel value? */ | |
107 bool willReadDstColor() const { return fWillReadDstColor; } | 106 bool willReadDstColor() const { return fWillReadDstColor; } |
108 | 107 |
| 108 /** |
| 109 * Returns the texture to be used as the destination when reading the dst in
the fragment |
| 110 * shader. If the returned texture is NULL then the XP is either not reading
the dst or we have |
| 111 * extentions that support framebuffer fetching and thus don't need a copy o
f the dst texture. |
| 112 */ |
| 113 const GrTexture* getDstCopyTexture() const { return fDstCopy.getTexture(); } |
| 114 |
| 115 /** |
| 116 * Returns the offset into the DstCopyTexture to use when reading it in the
shader. This value |
| 117 * is only valid if getDstCopyTexture() != NULL. |
| 118 */ |
| 119 const SkIPoint& dstCopyTextureOffset() const { return fDstCopyTextureOffset;
} |
| 120 |
109 /** | 121 /** |
110 * Returns whether or not this xferProcossor will set a secondary output to
be used with dual | 122 * Returns whether or not this xferProcossor will set a secondary output to
be used with dual |
111 * source blending. | 123 * source blending. |
112 */ | 124 */ |
113 virtual bool hasSecondaryOutput() const { return false; } | 125 virtual bool hasSecondaryOutput() const { return false; } |
114 | 126 |
115 /** Returns true if this and other processor conservatively draw identically
. It can only return | 127 /** Returns true if this and other processor conservatively draw identically
. It can only return |
116 true when the two processor are of the same subclass (i.e. they return t
he same object from | 128 true when the two processor are of the same subclass (i.e. they return t
he same object from |
117 from getFactory()). | 129 from getFactory()). |
118 | 130 |
119 A return value of true from isEqual() should not be used to test whether
the processor would | 131 A return value of true from isEqual() should not be used to test whether
the processor would |
120 generate the same shader code. To test for identical code generation use
getGLProcessorKey*/ | 132 generate the same shader code. To test for identical code generation use
getGLProcessorKey*/ |
121 | 133 |
122 bool isEqual(const GrXferProcessor& that) const { | 134 bool isEqual(const GrXferProcessor& that) const { |
123 if (this->classID() != that.classID()) { | 135 if (this->classID() != that.classID()) { |
124 return false; | 136 return false; |
125 } | 137 } |
| 138 if (this->fWillReadDstColor != that.fWillReadDstColor) { |
| 139 return false; |
| 140 } |
| 141 if (this->fDstCopy.getTexture() != that.fDstCopy.getTexture()) { |
| 142 return false; |
| 143 } |
| 144 if (this->fDstCopyTextureOffset != that.fDstCopyTextureOffset) { |
| 145 return false; |
| 146 } |
126 return this->onIsEqual(that); | 147 return this->onIsEqual(that); |
127 } | 148 } |
128 | 149 |
129 protected: | 150 protected: |
130 GrXferProcessor() : fWillReadDstColor(false) {} | 151 GrXferProcessor(); |
131 | 152 GrXferProcessor(const GrDeviceCoordTexture* dstCopy, bool willReadDstColor); |
132 /** | |
133 * If the prceossor subclass will read the destination pixel value then it m
ust call this | |
134 * function from its constructor. Otherwise, when its generated backend-spec
ific prceossor class | |
135 * attempts to generate code that reads the destination pixel it will fail. | |
136 */ | |
137 void setWillReadDstColor() { fWillReadDstColor = true; } | |
138 | 153 |
139 private: | 154 private: |
| 155 /** |
| 156 * Sets a unique key on the GrProcessorKeyBuilder that is directly associate
d with this xfer |
| 157 * processor's GL backend implementation. |
| 158 */ |
| 159 virtual void onGetGLProcessorKey(const GrGLCaps& caps, |
| 160 GrProcessorKeyBuilder* b) const = 0; |
| 161 |
140 virtual bool onIsEqual(const GrXferProcessor&) const = 0; | 162 virtual bool onIsEqual(const GrXferProcessor&) const = 0; |
141 | 163 |
142 bool fWillReadDstColor; | 164 bool fWillReadDstColor; |
| 165 SkIPoint fDstCopyTextureOffset; |
| 166 GrTextureAccess fDstCopy; |
143 | 167 |
144 typedef GrFragmentProcessor INHERITED; | 168 typedef GrFragmentProcessor INHERITED; |
145 }; | 169 }; |
146 | 170 |
147 GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags); | 171 GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags); |
148 | 172 |
| 173 /////////////////////////////////////////////////////////////////////////////// |
| 174 |
149 /** | 175 /** |
150 * We install a GrXPFactory (XPF) early on in the pipeline before all the final
draw information is | 176 * We install a GrXPFactory (XPF) early on in the pipeline before all the final
draw information is |
151 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or
4 channel, is the | 177 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or
4 channel, is the |
152 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF a
long with all the | 178 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF a
long with all the |
153 * draw information to create a GrXferProcessor (XP) which can implement the des
ired blending for | 179 * draw information to create a GrXferProcessor (XP) which can implement the des
ired blending for |
154 * the draw. | 180 * the draw. |
155 * | 181 * |
156 * Before the XP is created, the XPF is able to answer queries about what functi
onality the XPs it | 182 * Before the XP is created, the XPF is able to answer queries about what functi
onality the XPs it |
157 * creates will have. For example, can it create an XP that supports RGB coverag
e or will the XP | 183 * creates will have. For example, can it create an XP that supports RGB coverag
e or will the XP |
158 * blend with the destination color. | 184 * blend with the destination color. |
159 */ | 185 */ |
160 class GrXPFactory : public SkRefCnt { | 186 class GrXPFactory : public SkRefCnt { |
161 public: | 187 public: |
162 virtual GrXferProcessor* createXferProcessor(const GrProcOptInfo& colorPOI, | 188 GrXferProcessor* createXferProcessor(const GrProcOptInfo& colorPOI, |
163 const GrProcOptInfo& coveragePO
I) const = 0; | 189 const GrProcOptInfo& coveragePOI, |
| 190 const GrDeviceCoordTexture* dstCopy, |
| 191 const GrDrawTargetCaps& caps) const; |
164 | 192 |
165 /** | 193 /** |
166 * This function returns true if the GrXferProcessor generated from this fac
tory will be able to | 194 * This function returns true if the GrXferProcessor generated from this fac
tory will be able to |
167 * correctly blend when using RGB coverage. The knownColor and knownColorFla
gs represent the | 195 * correctly blend when using RGB coverage. The knownColor and knownColorFla
gs represent the |
168 * final computed color from the color stages. | 196 * final computed color from the color stages. |
169 */ | 197 */ |
170 virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlag
s) const = 0; | 198 virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlag
s) const = 0; |
171 | 199 |
172 /** | 200 /** |
173 * Depending on color blend mode requested it may or may not be possible to
correctly blend with | 201 * Depending on color blend mode requested it may or may not be possible to
correctly blend with |
(...skipping 21 matching lines...) Expand all Loading... |
195 virtual void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcO
ptInfo& coveragePOI, | 223 virtual void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcO
ptInfo& coveragePOI, |
196 InvariantOutput*) const = 0; | 224 InvariantOutput*) const = 0; |
197 | 225 |
198 /** | 226 /** |
199 * Determines whether multiplying the computed per-pixel color by the pixel'
s fractional | 227 * Determines whether multiplying the computed per-pixel color by the pixel'
s fractional |
200 * coverage before the blend will give the correct final destination color.
In general it | 228 * coverage before the blend will give the correct final destination color.
In general it |
201 * will not as coverage is applied after blending. | 229 * will not as coverage is applied after blending. |
202 */ | 230 */ |
203 virtual bool canTweakAlphaForCoverage() const = 0; | 231 virtual bool canTweakAlphaForCoverage() const = 0; |
204 | 232 |
205 /** | 233 bool willNeedDstCopy(const GrDrawTargetCaps& caps) const; |
206 * Returns true if the XP generated by this factory will read dst. | |
207 */ | |
208 virtual bool willReadDst() const = 0; | |
209 | 234 |
210 bool isEqual(const GrXPFactory& that) const { | 235 bool isEqual(const GrXPFactory& that) const { |
211 if (this->classID() != that.classID()) { | 236 if (this->classID() != that.classID()) { |
212 return false; | 237 return false; |
213 } | 238 } |
214 return this->onIsEqual(that); | 239 return this->onIsEqual(that); |
215 } | 240 } |
216 | 241 |
217 /** | 242 /** |
218 * Helper for down-casting to a GrXPFactory subclass | 243 * Helper for down-casting to a GrXPFactory subclass |
219 */ | 244 */ |
220 template <typename T> const T& cast() const { return *static_cast<const T*>(
this); } | 245 template <typename T> const T& cast() const { return *static_cast<const T*>(
this); } |
221 | 246 |
222 uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return
fClassID; } | 247 uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return
fClassID; } |
223 | 248 |
224 protected: | 249 protected: |
225 GrXPFactory() : fClassID(kIllegalXPFClassID) {} | 250 GrXPFactory() : fClassID(kIllegalXPFClassID) {} |
226 | 251 |
227 template <typename XPF_SUBCLASS> void initClassID() { | 252 template <typename XPF_SUBCLASS> void initClassID() { |
228 static uint32_t kClassID = GenClassID(); | 253 static uint32_t kClassID = GenClassID(); |
229 fClassID = kClassID; | 254 fClassID = kClassID; |
230 } | 255 } |
231 | 256 |
232 uint32_t fClassID; | 257 uint32_t fClassID; |
233 | 258 |
234 private: | 259 private: |
| 260 virtual GrXferProcessor* onCreateXferProcessor(const GrProcOptInfo& colorPOI
, |
| 261 const GrProcOptInfo& coverage
POI, |
| 262 const GrDeviceCoordTexture* d
stCopy) const = 0; |
| 263 /** |
| 264 * Returns true if the XP generated by this factory will explicitly read ds
t in the fragment |
| 265 * shader. |
| 266 */ |
| 267 virtual bool willReadDstColor() const = 0; |
| 268 |
235 virtual bool onIsEqual(const GrXPFactory&) const = 0; | 269 virtual bool onIsEqual(const GrXPFactory&) const = 0; |
236 | 270 |
237 static uint32_t GenClassID() { | 271 static uint32_t GenClassID() { |
238 // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The | 272 // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The |
239 // atomic inc returns the old value not the incremented value. So we add | 273 // atomic inc returns the old value not the incremented value. So we add |
240 // 1 to the returned value. | 274 // 1 to the returned value. |
241 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1
; | 275 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1
; |
242 if (!id) { | 276 if (!id) { |
243 SkFAIL("This should never wrap as it should only be called once for
each GrXPFactory " | 277 SkFAIL("This should never wrap as it should only be called once for
each GrXPFactory " |
244 "subclass."); | 278 "subclass."); |
245 } | 279 } |
246 return id; | 280 return id; |
247 } | 281 } |
248 | 282 |
249 enum { | 283 enum { |
250 kIllegalXPFClassID = 0, | 284 kIllegalXPFClassID = 0, |
251 }; | 285 }; |
252 static int32_t gCurrXPFClassID; | 286 static int32_t gCurrXPFClassID; |
253 | 287 |
254 typedef GrProgramElement INHERITED; | 288 typedef GrProgramElement INHERITED; |
255 }; | 289 }; |
256 | 290 |
257 #endif | 291 #endif |
258 | 292 |
OLD | NEW |