OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 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 GrDrawState_DEFINED | |
9 #define GrDrawState_DEFINED | |
10 | |
11 | |
12 #include "GrBlend.h" | |
13 #include "GrDrawTargetCaps.h" | |
14 #include "GrGeometryProcessor.h" | |
15 #include "GrGpuResourceRef.h" | |
16 #include "GrFragmentStage.h" | |
17 #include "GrProcOptInfo.h" | |
18 #include "GrRenderTarget.h" | |
19 #include "GrStencil.h" | |
20 #include "GrXferProcessor.h" | |
21 #include "SkMatrix.h" | |
22 #include "effects/GrCoverageSetOpXP.h" | |
23 #include "effects/GrDisableColorXP.h" | |
24 #include "effects/GrPorterDuffXferProcessor.h" | |
25 #include "effects/GrSimpleTextureEffect.h" | |
26 | |
27 class GrDrawTargetCaps; | |
28 class GrPaint; | |
29 class GrTexture; | |
30 | |
31 class GrDrawState { | |
32 public: | |
33 GrDrawState(); | |
34 /** | |
35 * Copies another draw state. | |
36 **/ | |
37 GrDrawState(const GrDrawState& state) { | |
38 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) | |
39 *this = state; | |
40 } | |
41 | |
42 virtual ~GrDrawState(); | |
43 | |
44 /** | |
45 * Initializes the GrDrawState based on a GrPaint, view matrix and render ta
rget. Note that | |
46 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that ha
ve no GrPaint | |
47 * equivalents are set to default values with the exception of vertex attrib
ute state which | |
48 * is unmodified by this function and clipping which will be enabled. | |
49 */ | |
50 void setFromPaint(const GrPaint&, GrRenderTarget*); | |
51 | |
52 /// @} | |
53 | |
54 /** | |
55 * Depending on features available in the underlying 3D API and the color bl
end mode requested | |
56 * it may or may not be possible to correctly blend with fractional pixel co
verage generated by | |
57 * the fragment shader. | |
58 * | |
59 * This function considers the current draw state and the draw target's capa
bilities to | |
60 * determine whether coverage can be handled correctly. This function assume
s that the caller | |
61 * intends to specify fractional pixel coverage via a primitive processor bu
t may not have | |
62 * specified it yet. | |
63 */ | |
64 bool canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps)
const; | |
65 | |
66 /** | |
67 * This function returns true if the render target destination pixel values
will be read for | |
68 * blending during draw. | |
69 */ | |
70 bool willBlendWithDst(const GrPrimitiveProcessor*) const; | |
71 | |
72 /////////////////////////////////////////////////////////////////////////// | |
73 /// @name Effect Stages | |
74 /// Each stage hosts a GrProcessor. The effect produces an output color or c
overage in the | |
75 /// fragment shader. Its inputs are the output from the previous stage as we
ll as some variables | |
76 /// available to it in the fragment and vertex shader (e.g. the vertex posit
ion, the dst color, | |
77 /// the fragment position, local coordinates). | |
78 /// | |
79 /// The stages are divided into two sets, color-computing and coverage-compu
ting. The final | |
80 /// color stage produces the final pixel color. The coverage-computing stage
s function exactly | |
81 /// as the color-computing but the output of the final coverage stage is tre
ated as a fractional | |
82 /// pixel coverage rather than as input to the src/dst color blend step. | |
83 /// | |
84 /// The input color to the first color-stage is either the constant color or
interpolated | |
85 /// per-vertex colors. The input to the first coverage stage is either a con
stant coverage | |
86 /// (usually full-coverage) or interpolated per-vertex coverage. | |
87 /// | |
88 /// See the documentation of kCoverageDrawing_StateBit for information about
disabling the | |
89 /// the color / coverage distinction. | |
90 //// | |
91 | |
92 int numColorStages() const { return fColorStages.count(); } | |
93 int numCoverageStages() const { return fCoverageStages.count(); } | |
94 int numFragmentStages() const { return this->numColorStages() + this->numCov
erageStages(); } | |
95 | |
96 const GrXPFactory* getXPFactory() const { | |
97 if (!fXPFactory) { | |
98 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode
)); | |
99 } | |
100 return fXPFactory.get(); | |
101 } | |
102 | |
103 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[id
x]; } | |
104 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageSta
ges[idx]; } | |
105 | |
106 /** | |
107 * Checks whether the xp will read the dst pixel color. | |
108 * TODO: remove when we have dstCpy contained inside of GrXP | |
109 */ | |
110 bool willEffectReadDstColor() const; | |
111 | |
112 /** | |
113 * The xfer processor factory. | |
114 */ | |
115 const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) { | |
116 fXPFactory.reset(SkRef(xpFactory)); | |
117 return xpFactory; | |
118 } | |
119 | |
120 void setPorterDuffXPFactory(SkXfermode::Mode mode) { | |
121 fXPFactory.reset(GrPorterDuffXPFactory::Create(mode)); | |
122 } | |
123 | |
124 void setPorterDuffXPFactory(GrBlendCoeff src, GrBlendCoeff dst) { | |
125 fXPFactory.reset(GrPorterDuffXPFactory::Create(src, dst)); | |
126 } | |
127 | |
128 void setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage =
false) { | |
129 fXPFactory.reset(GrCoverageSetOpXPFactory::Create(regionOp, invertCovera
ge)); | |
130 } | |
131 | |
132 void setDisableColorXPFactory() { | |
133 fXPFactory.reset(GrDisableColorXPFactory::Create()); | |
134 } | |
135 | |
136 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* effe
ct) { | |
137 SkASSERT(effect); | |
138 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (effect)); | |
139 fColorProcInfoValid = false; | |
140 return effect; | |
141 } | |
142 | |
143 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* e
ffect) { | |
144 SkASSERT(effect); | |
145 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (effect)); | |
146 fCoverageProcInfoValid = false; | |
147 return effect; | |
148 } | |
149 | |
150 /** | |
151 * Creates a GrSimpleTextureEffect that uses local coords as texture coordin
ates. | |
152 */ | |
153 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { | |
154 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))-
>unref(); | |
155 } | |
156 | |
157 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix)
{ | |
158 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix
))->unref(); | |
159 } | |
160 | |
161 void addColorTextureProcessor(GrTexture* texture, | |
162 const SkMatrix& matrix, | |
163 const GrTextureParams& params) { | |
164 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, p
arams))->unref(); | |
165 } | |
166 | |
167 void addCoverageTextureProcessor(GrTexture* texture, | |
168 const SkMatrix& matrix, | |
169 const GrTextureParams& params) { | |
170 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix
, params))->unref(); | |
171 } | |
172 | |
173 /** | |
174 * When this object is destroyed it will remove any color/coverage effects f
rom the draw state | |
175 * that were added after its constructor. | |
176 * | |
177 * This class has strange behavior around geometry processor. If there is a
GP on the draw state | |
178 * it will assert that the GP is not modified until after the destructor of
the ARE. If the | |
179 * draw state has a NULL GP when the ARE is constructed then it will reset i
t to null in the | |
180 * destructor. | |
181 * | |
182 * TODO: We'd prefer for the ARE to just save and restore the GP. However, t
his would add | |
183 * significant complexity to the multi-ref architecture for deferred drawing
. Once GrDrawState | |
184 * and GrOptDrawState are fully separated then GrDrawState will never be in
the deferred | |
185 * execution state and GrOptDrawState always will be (and will be immutable
and therefore | |
186 * unable to have an ARE). At this point we can restore sanity and have the
ARE save and restore | |
187 * the GP. | |
188 */ | |
189 class AutoRestoreEffects : public ::SkNoncopyable { | |
190 public: | |
191 AutoRestoreEffects() | |
192 : fDrawState(NULL) | |
193 , fColorEffectCnt(0) | |
194 , fCoverageEffectCnt(0) {} | |
195 | |
196 AutoRestoreEffects(GrDrawState* ds) | |
197 : fDrawState(NULL) | |
198 , fColorEffectCnt(0) | |
199 , fCoverageEffectCnt(0) { | |
200 this->set(ds); | |
201 } | |
202 | |
203 ~AutoRestoreEffects() { this->set(NULL); } | |
204 | |
205 void set(GrDrawState* ds); | |
206 | |
207 bool isSet() const { return SkToBool(fDrawState); } | |
208 | |
209 private: | |
210 GrDrawState* fDrawState; | |
211 int fColorEffectCnt; | |
212 int fCoverageEffectCnt; | |
213 }; | |
214 | |
215 /** | |
216 * AutoRestoreStencil | |
217 * | |
218 * This simple struct saves and restores the stencil settings | |
219 */ | |
220 class AutoRestoreStencil : public ::SkNoncopyable { | |
221 public: | |
222 AutoRestoreStencil() : fDrawState(NULL) {} | |
223 | |
224 AutoRestoreStencil(GrDrawState* ds) : fDrawState(NULL) { this->set(ds);
} | |
225 | |
226 ~AutoRestoreStencil() { this->set(NULL); } | |
227 | |
228 void set(GrDrawState* ds) { | |
229 if (fDrawState) { | |
230 fDrawState->setStencil(fStencilSettings); | |
231 } | |
232 fDrawState = ds; | |
233 if (ds) { | |
234 fStencilSettings = ds->getStencil(); | |
235 } | |
236 } | |
237 | |
238 bool isSet() const { return SkToBool(fDrawState); } | |
239 | |
240 private: | |
241 GrDrawState* fDrawState; | |
242 GrStencilSettings fStencilSettings; | |
243 }; | |
244 | |
245 /// @} | |
246 | |
247 /////////////////////////////////////////////////////////////////////////// | |
248 /// @name Blending | |
249 //// | |
250 | |
251 /** | |
252 * Determines whether multiplying the computed per-pixel color by the pixel'
s fractional | |
253 * coverage before the blend will give the correct final destination color.
In general it | |
254 * will not as coverage is applied after blending. | |
255 */ | |
256 bool canTweakAlphaForCoverage() const; | |
257 | |
258 /// @} | |
259 | |
260 | |
261 /// @} | |
262 | |
263 /////////////////////////////////////////////////////////////////////////// | |
264 /// @name Render Target | |
265 //// | |
266 | |
267 /** | |
268 * Retrieves the currently set render-target. | |
269 * | |
270 * @return The currently set render target. | |
271 */ | |
272 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } | |
273 | |
274 /** | |
275 * Sets the render-target used at the next drawing call | |
276 * | |
277 * @param target The render target to set. | |
278 */ | |
279 void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef
(target)); } | |
280 | |
281 /// @} | |
282 | |
283 /////////////////////////////////////////////////////////////////////////// | |
284 /// @name Stencil | |
285 //// | |
286 | |
287 const GrStencilSettings& getStencil() const { return fStencilSettings; } | |
288 | |
289 /** | |
290 * Sets the stencil settings to use for the next draw. | |
291 * Changing the clip has the side-effect of possibly zeroing | |
292 * out the client settable stencil bits. So multipass algorithms | |
293 * using stencil should not change the clip between passes. | |
294 * @param settings the stencil settings to use. | |
295 */ | |
296 void setStencil(const GrStencilSettings& settings) { fStencilSettings = sett
ings; } | |
297 | |
298 /** | |
299 * Shortcut to disable stencil testing and ops. | |
300 */ | |
301 void disableStencil() { fStencilSettings.setDisabled(); } | |
302 | |
303 GrStencilSettings* stencil() { return &fStencilSettings; } | |
304 | |
305 /// @} | |
306 | |
307 /////////////////////////////////////////////////////////////////////////// | |
308 /// @name State Flags | |
309 //// | |
310 | |
311 /** | |
312 * Flags that affect rendering. Controlled using enable/disableState(). All | |
313 * default to disabled. | |
314 */ | |
315 enum StateBits { | |
316 /** | |
317 * Perform dithering. TODO: Re-evaluate whether we need this bit | |
318 */ | |
319 kDither_StateBit = 0x01, | |
320 /** | |
321 * Perform HW anti-aliasing. This means either HW FSAA, if supported by
the render target, | |
322 * or smooth-line rendering if a line primitive is drawn and line smooth
ing is supported by | |
323 * the 3D API. | |
324 */ | |
325 kHWAntialias_StateBit = 0x02, | |
326 /** | |
327 * Draws will respect the clip, otherwise the clip is ignored. | |
328 */ | |
329 kClip_StateBit = 0x04, | |
330 | |
331 kLast_StateBit = kClip_StateBit, | |
332 }; | |
333 | |
334 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } | |
335 bool isDither() const { return 0 != (fFlagBits & kDither_StateBit); } | |
336 bool isHWAntialias() const { return 0 != (fFlagBits & kHWAntialias_StateBit)
; } | |
337 | |
338 /** | |
339 * Enable render state settings. | |
340 * | |
341 * @param stateBits bitfield of StateBits specifying the states to enable | |
342 */ | |
343 void enableState(uint32_t stateBits) { fFlagBits |= stateBits; } | |
344 | |
345 /** | |
346 * Disable render state settings. | |
347 * | |
348 * @param stateBits bitfield of StateBits specifying the states to disable | |
349 */ | |
350 void disableState(uint32_t stateBits) { fFlagBits &= ~(stateBits); } | |
351 | |
352 /** | |
353 * Enable or disable stateBits based on a boolean. | |
354 * | |
355 * @param stateBits bitfield of StateBits to enable or disable | |
356 * @param enable if true enable stateBits, otherwise disable | |
357 */ | |
358 void setState(uint32_t stateBits, bool enable) { | |
359 if (enable) { | |
360 this->enableState(stateBits); | |
361 } else { | |
362 this->disableState(stateBits); | |
363 } | |
364 } | |
365 | |
366 /// @} | |
367 | |
368 /////////////////////////////////////////////////////////////////////////// | |
369 /// @name Face Culling | |
370 //// | |
371 | |
372 enum DrawFace { | |
373 kInvalid_DrawFace = -1, | |
374 | |
375 kBoth_DrawFace, | |
376 kCCW_DrawFace, | |
377 kCW_DrawFace, | |
378 }; | |
379 | |
380 /** | |
381 * Gets whether the target is drawing clockwise, counterclockwise, | |
382 * or both faces. | |
383 * @return the current draw face(s). | |
384 */ | |
385 DrawFace getDrawFace() const { return fDrawFace; } | |
386 | |
387 /** | |
388 * Controls whether clockwise, counterclockwise, or both faces are drawn. | |
389 * @param face the face(s) to draw. | |
390 */ | |
391 void setDrawFace(DrawFace face) { | |
392 SkASSERT(kInvalid_DrawFace != face); | |
393 fDrawFace = face; | |
394 } | |
395 | |
396 /// @} | |
397 | |
398 /////////////////////////////////////////////////////////////////////////// | |
399 | |
400 GrDrawState& operator= (const GrDrawState& that); | |
401 | |
402 private: | |
403 const GrProcOptInfo& colorProcInfo(const GrPrimitiveProcessor* pp) const { | |
404 this->calcColorInvariantOutput(pp); | |
405 return fColorProcInfo; | |
406 } | |
407 | |
408 const GrProcOptInfo& coverageProcInfo(const GrPrimitiveProcessor* pp) const
{ | |
409 this->calcCoverageInvariantOutput(pp); | |
410 return fCoverageProcInfo; | |
411 } | |
412 | |
413 /** | |
414 * If fColorProcInfoValid is false, function calculates the invariant output
for the color | |
415 * stages and results are stored in fColorProcInfo. | |
416 */ | |
417 void calcColorInvariantOutput(const GrPrimitiveProcessor*) const; | |
418 | |
419 /** | |
420 * If fCoverageProcInfoValid is false, function calculates the invariant out
put for the coverage | |
421 * stages and results are stored in fCoverageProcInfo. | |
422 */ | |
423 void calcCoverageInvariantOutput(const GrPrimitiveProcessor*) const; | |
424 | |
425 /** | |
426 * If fColorProcInfoValid is false, function calculates the invariant output
for the color | |
427 * stages and results are stored in fColorProcInfo. | |
428 */ | |
429 void calcColorInvariantOutput(GrColor) const; | |
430 | |
431 /** | |
432 * If fCoverageProcInfoValid is false, function calculates the invariant out
put for the coverage | |
433 * stages and results are stored in fCoverageProcInfo. | |
434 */ | |
435 void calcCoverageInvariantOutput(GrColor) const; | |
436 | |
437 // Some of the auto restore objects assume that no effects are removed durin
g their lifetime. | |
438 // This is used to assert that this condition holds. | |
439 SkDEBUGCODE(int fBlockEffectRemovalCnt;) | |
440 | |
441 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray; | |
442 | |
443 SkAutoTUnref<GrRenderTarget> fRenderTarget; | |
444 uint32_t fFlagBits; | |
445 GrStencilSettings fStencilSettings; | |
446 DrawFace fDrawFace; | |
447 mutable SkAutoTUnref<const GrXPFactory> fXPFactory; | |
448 FragmentStageArray fColorStages; | |
449 FragmentStageArray fCoverageStages; | |
450 | |
451 mutable GrProcOptInfo fColorProcInfo; | |
452 mutable GrProcOptInfo fCoverageProcInfo; | |
453 mutable bool fColorProcInfoValid; | |
454 mutable bool fCoverageProcInfoValid; | |
455 mutable GrColor fColorCache; | |
456 mutable GrColor fCoverageCache; | |
457 mutable const GrPrimitiveProcessor* fColorPrimProc; | |
458 mutable const GrPrimitiveProcessor* fCoveragePrimProc; | |
459 | |
460 friend class GrOptDrawState; | |
461 }; | |
462 | |
463 #endif | |
OLD | NEW |