blob: e4a748fe854d458b0bde2a3dca3fe9199e472d38 [file] [log] [blame]
tomhudson@google.com93813632011-10-27 20:21:16 +00001/*
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#include "GrColor.h"
12#include "GrMatrix.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000013#include "GrNoncopyable.h"
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000014#include "GrRefCnt.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000015#include "GrSamplerState.h"
16#include "GrStencil.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000017#include "GrTexture.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000018#include "GrRenderTarget.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000019
20#include "SkXfermode.h"
21
tomhudson@google.com93813632011-10-27 20:21:16 +000022
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000023class GrDrawState : public GrRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000024public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000025 SK_DECLARE_INST_COUNT(GrDrawState)
26
tomhudson@google.com93813632011-10-27 20:21:16 +000027 /**
28 * Number of texture stages. Each stage takes as input a color and
29 * 2D texture coordinates. The color input to the first enabled stage is the
30 * per-vertex color or the constant color (setColor/setAlpha) if there are
31 * no per-vertex colors. For subsequent stages the input color is the output
32 * color from the previous enabled stage. The output color of each stage is
33 * the input color modulated with the result of a texture lookup. Texture
34 * lookups are specified by a texture a sampler (setSamplerState). Texture
35 * coordinates for each stage come from the vertices based on a
36 * GrVertexLayout bitfield. The output fragment color is the output color of
37 * the last enabled stage. The presence or absence of texture coordinates
38 * for each stage in the vertex layout indicates whether a stage is enabled
39 * or not.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000040 *
41 * Stages 0 through GrPaint::kTotalStages-1 are reserved for setting up
42 * the draw (i.e., textures and filter masks). Stages GrPaint::kTotalStages
43 * through kNumStages-1 are earmarked for use by GrTextContext and
44 * GrPathRenderer-derived classes.
tomhudson@google.com93813632011-10-27 20:21:16 +000045 */
46 enum {
robertphillips@google.comec05eaa2012-04-27 18:59:52 +000047 kNumStages = 4,
tomhudson@google.com93813632011-10-27 20:21:16 +000048 kMaxTexCoords = kNumStages
49 };
50
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000051 /**
52 * Bitfield used to indicate a set of stages.
53 */
54 typedef uint32_t StageMask;
55 GR_STATIC_ASSERT(sizeof(StageMask)*8 >= GrDrawState::kNumStages);
56
robertphillips@google.com9ec07532012-06-22 12:01:30 +000057 GrDrawState()
58 : fRenderTarget(NULL) {
59
60 for (int i = 0; i < kNumStages; ++i) {
61 fTextures[i] = NULL;
62 }
63
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000064 this->reset();
65 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000066
robertphillips@google.com9ec07532012-06-22 12:01:30 +000067 GrDrawState(const GrDrawState& state)
68 : fRenderTarget(NULL) {
69
70 for (int i = 0; i < kNumStages; ++i) {
71 fTextures[i] = NULL;
72 }
73
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000074 *this = state;
75 }
76
robertphillips@google.com9ec07532012-06-22 12:01:30 +000077 virtual ~GrDrawState() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000078 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000079 GrSafeSetNull(fRenderTarget);
80 }
81
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000082 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000083 * Resets to the default state.
84 * Sampler states *will* be modified: textures or CustomStage objects
85 * will be released.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000086 */
87 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000088
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000089 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000090 GrSafeSetNull(fRenderTarget);
91
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000092 // make sure any pad is zero for memcmp
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000093 // all GrDrawState members should default to something valid by the
94 // the memset except those initialized individually below. There should
95 // be no padding between the individually initialized members.
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000096 memset(this->podStart(), 0, this->memsetSize());
97
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000098 // pedantic assertion that our ptrs will
99 // be NULL (0 ptr is mem addr 0)
100 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000101 GR_STATIC_ASSERT(0 == kBoth_DrawFace);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000102 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000103
104 // memset exceptions
105 fColor = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000106 fCoverage = 0xffffffff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000107 fFirstCoverageStage = kNumStages;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000108 fColorFilterMode = SkXfermode::kDst_Mode;
bsalomon@google.com47059542012-06-06 20:51:20 +0000109 fSrcBlend = kOne_GrBlendCoeff;
110 fDstBlend = kZero_GrBlendCoeff;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000111 fViewMatrix.reset();
112
113 // ensure values that will be memcmp'ed in == but not memset in reset()
114 // are tightly packed
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000115 GrAssert(this->memsetSize() + sizeof(fColor) + sizeof(fCoverage) +
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000116 sizeof(fFirstCoverageStage) + sizeof(fColorFilterMode) +
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000117 sizeof(fSrcBlend) + sizeof(fDstBlend) + sizeof(fTextures) +
118 sizeof(fRenderTarget) == this->podSize());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000119 }
120
121 ///////////////////////////////////////////////////////////////////////////
122 /// @name Color
123 ////
124
125 /**
126 * Sets color for next draw to a premultiplied-alpha color.
127 *
128 * @param color the color to set.
129 */
130 void setColor(GrColor color) { fColor = color; }
131
132 GrColor getColor() const { return fColor; }
133
134 /**
135 * Sets the color to be used for the next draw to be
136 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
137 *
138 * @param alpha The alpha value to set as the color.
139 */
140 void setAlpha(uint8_t a) {
141 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
142 }
143
144 /**
145 * Add a color filter that can be represented by a color and a mode. Applied
146 * after color-computing texture stages.
147 */
148 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
149 fColorFilterColor = c;
150 fColorFilterMode = mode;
151 }
152
153 GrColor getColorFilterColor() const { return fColorFilterColor; }
154 SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; }
155
156 /// @}
157
158 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000159 /// @name Coverage
160 ////
161
162 /**
163 * Sets a constant fractional coverage to be applied to the draw. The
164 * initial value (after construction or reset()) is 0xff. The constant
165 * coverage is ignored when per-vertex coverage is provided.
166 */
167 void setCoverage(uint8_t coverage) {
168 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
169 }
170
171 /**
172 * Version of above that specifies 4 channel per-vertex color. The value
173 * should be premultiplied.
174 */
175 void setCoverage4(GrColor coverage) {
176 fCoverage = coverage;
177 }
178
179 GrColor getCoverage() const {
180 return fCoverage;
181 }
182
183 /// @}
184
185 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000186 /// @name Textures
187 ////
188
189 /**
190 * Sets the texture used at the next drawing call
191 *
192 * @param stage The texture stage for which the texture will be set
193 *
194 * @param texture The texture to set. Can be NULL though there is no
195 * advantage to settings a NULL texture if doing non-textured drawing
196 */
197 void setTexture(int stage, GrTexture* texture) {
198 GrAssert((unsigned)stage < kNumStages);
robertphillips@google.com1942c052012-05-03 17:58:27 +0000199
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000200 GrSafeAssign(fTextures[stage], texture);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000201 }
202
203 /**
204 * Retrieves the currently set texture.
205 *
206 * @return The currently set texture. The return value will be NULL if no
207 * texture has been set, NULL was most recently passed to
208 * setTexture, or the last setTexture was destroyed.
209 */
210 const GrTexture* getTexture(int stage) const {
211 GrAssert((unsigned)stage < kNumStages);
212 return fTextures[stage];
213 }
214 GrTexture* getTexture(int stage) {
215 GrAssert((unsigned)stage < kNumStages);
216 return fTextures[stage];
217 }
218
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000219 bool stagesDisabled() {
220 for (int i = 0; i < kNumStages; ++i) {
221 if (NULL != fTextures[i] ||
222 NULL != fSamplerStates[i].getCustomStage()) {
223 return false;
224 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000225 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000226 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000227 }
robertphillips@google.com972265d2012-06-13 18:49:30 +0000228 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000229 * Release all the textures and custom stages referred to by this
230 * draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000231 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000232 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000233 for (int i = 0; i < kNumStages; ++i) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000234 GrSafeSetNull(fTextures[i]);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000235 fSamplerStates[i].setCustomStage(NULL);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000236 }
237 }
238
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000239 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000240 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000241 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
242 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000243 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000244 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000245 }
246 }
247 private:
248 GrDrawState* fDrawState;
249 };
250
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000251 /// @}
252
253 ///////////////////////////////////////////////////////////////////////////
254 /// @name Samplers
255 ////
256
257 /**
258 * Returns the current sampler for a stage.
259 */
260 const GrSamplerState& getSampler(int stage) const {
261 GrAssert((unsigned)stage < kNumStages);
262 return fSamplerStates[stage];
263 }
264
265 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000266 * Writable pointer to a stage's sampler.
267 */
268 GrSamplerState* sampler(int stage) {
269 GrAssert((unsigned)stage < kNumStages);
270 return fSamplerStates + stage;
271 }
272
273 /**
274 * Preconcats the matrix of all samplers in the mask with the same matrix.
275 */
276 void preConcatSamplerMatrices(StageMask stageMask, const GrMatrix& matrix) {
277 GrAssert(!(stageMask & kIllegalStageMaskBits));
278 for (int i = 0; i < kNumStages; ++i) {
279 if ((1 << i) & stageMask) {
280 fSamplerStates[i].preConcatMatrix(matrix);
281 }
282 }
283 }
284
285 /// @}
286
287 ///////////////////////////////////////////////////////////////////////////
288 /// @name Coverage / Color Stages
289 ////
290
291 /**
292 * A common pattern is to compute a color with the initial stages and then
293 * modulate that color by a coverage value in later stage(s) (AA, mask-
294 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
295 * computed based on the pre-coverage-modulated color. The division of
296 * stages between color-computing and coverage-computing is specified by
297 * this method. Initially this is kNumStages (all stages
298 * are color-computing).
299 */
300 void setFirstCoverageStage(int firstCoverageStage) {
301 GrAssert((unsigned)firstCoverageStage <= kNumStages);
302 fFirstCoverageStage = firstCoverageStage;
303 }
304
305 /**
306 * Gets the index of the first coverage-computing stage.
307 */
308 int getFirstCoverageStage() const {
309 return fFirstCoverageStage;
310 }
311
312 ///@}
313
314 ///////////////////////////////////////////////////////////////////////////
315 /// @name Blending
316 ////
317
318 /**
319 * Sets the blending function coeffecients.
320 *
321 * The blend function will be:
322 * D' = sat(S*srcCoef + D*dstCoef)
323 *
324 * where D is the existing destination color, S is the incoming source
325 * color, and D' is the new destination color that will be written. sat()
326 * is the saturation function.
327 *
328 * @param srcCoef coeffecient applied to the src color.
329 * @param dstCoef coeffecient applied to the dst color.
330 */
331 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
332 fSrcBlend = srcCoeff;
333 fDstBlend = dstCoeff;
334 #if GR_DEBUG
335 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000336 case kDC_GrBlendCoeff:
337 case kIDC_GrBlendCoeff:
338 case kDA_GrBlendCoeff:
339 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000340 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
341 "coverage stages.\n");
342 break;
343 default:
344 break;
345 }
346 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000347 case kSC_GrBlendCoeff:
348 case kISC_GrBlendCoeff:
349 case kSA_GrBlendCoeff:
350 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000351 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
352 "coverage stages.\n");
353 break;
354 default:
355 break;
356 }
357 #endif
358 }
359
360 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
361 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
362
363 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
364 GrBlendCoeff* dstBlendCoeff) const {
365 *srcBlendCoeff = fSrcBlend;
366 *dstBlendCoeff = fDstBlend;
367 }
368
369 /**
370 * Sets the blending function constant referenced by the following blending
371 * coeffecients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000372 * kConstC_GrBlendCoeff
373 * kIConstC_GrBlendCoeff
374 * kConstA_GrBlendCoeff
375 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000376 *
377 * @param constant the constant to set
378 */
379 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
380
381 /**
382 * Retrieves the last value set by setBlendConstant()
383 * @return the blending constant value
384 */
385 GrColor getBlendConstant() const { return fBlendConstant; }
386
387 /// @}
388
389 ///////////////////////////////////////////////////////////////////////////
390 /// @name View Matrix
391 ////
392
393 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000394 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000395 *
396 * In the post-view-matrix space the rectangle [0,w]x[0,h]
397 * fully covers the render target. (w and h are the width and height of the
398 * the rendertarget.)
399 */
400 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
401
402 /**
403 * Gets a writable pointer to the view matrix.
404 */
405 GrMatrix* viewMatrix() { return &fViewMatrix; }
406
407 /**
408 * Multiplies the current view matrix by a matrix
409 *
410 * After this call V' = V*m where V is the old view matrix,
411 * m is the parameter to this function, and V' is the new view matrix.
412 * (We consider positions to be column vectors so position vector p is
413 * transformed by matrix X as p' = X*p.)
414 *
415 * @param m the matrix used to modify the view matrix.
416 */
417 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
418
419 /**
420 * Multiplies the current view matrix by a matrix
421 *
422 * After this call V' = m*V where V is the old view matrix,
423 * m is the parameter to this function, and V' is the new view matrix.
424 * (We consider positions to be column vectors so position vector p is
425 * transformed by matrix X as p' = X*p.)
426 *
427 * @param m the matrix used to modify the view matrix.
428 */
429 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
430
431 /**
432 * Retrieves the current view matrix
433 * @return the current view matrix.
434 */
435 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
436
437 /**
438 * Retrieves the inverse of the current view matrix.
439 *
440 * If the current view matrix is invertible, return true, and if matrix
441 * is non-null, copy the inverse into it. If the current view matrix is
442 * non-invertible, return false and ignore the matrix parameter.
443 *
444 * @param matrix if not null, will receive a copy of the current inverse.
445 */
446 bool getViewInverse(GrMatrix* matrix) const {
447 // TODO: determine whether we really need to leave matrix unmodified
448 // at call sites when inversion fails.
449 GrMatrix inverse;
450 if (fViewMatrix.invert(&inverse)) {
451 if (matrix) {
452 *matrix = inverse;
453 }
454 return true;
455 }
456 return false;
457 }
458
459 class AutoViewMatrixRestore : public ::GrNoncopyable {
460 public:
461 AutoViewMatrixRestore() : fDrawState(NULL) {}
462 AutoViewMatrixRestore(GrDrawState* ds, const GrMatrix& newMatrix) {
463 fDrawState = NULL;
464 this->set(ds, newMatrix);
465 }
466 AutoViewMatrixRestore(GrDrawState* ds) {
467 fDrawState = NULL;
468 this->set(ds);
469 }
470 ~AutoViewMatrixRestore() {
471 this->set(NULL, GrMatrix::I());
472 }
473 void set(GrDrawState* ds, const GrMatrix& newMatrix) {
474 if (NULL != fDrawState) {
475 fDrawState->setViewMatrix(fSavedMatrix);
476 }
477 if (NULL != ds) {
478 fSavedMatrix = ds->getViewMatrix();
479 ds->setViewMatrix(newMatrix);
480 }
481 fDrawState = ds;
482 }
483 void set(GrDrawState* ds) {
484 if (NULL != fDrawState) {
485 fDrawState->setViewMatrix(fSavedMatrix);
486 }
487 if (NULL != ds) {
488 fSavedMatrix = ds->getViewMatrix();
489 }
490 fDrawState = ds;
491 }
492 private:
493 GrDrawState* fDrawState;
494 GrMatrix fSavedMatrix;
tomhudson@google.com93813632011-10-27 20:21:16 +0000495 };
496
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000497 /// @}
498
499 ///////////////////////////////////////////////////////////////////////////
500 /// @name Render Target
501 ////
502
503 /**
504 * Sets the rendertarget used at the next drawing call
505 *
506 * @param target The render target to set.
507 */
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000508 void setRenderTarget(GrRenderTarget* target) {
509 GrSafeAssign(fRenderTarget, target);
510 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511
512 /**
513 * Retrieves the currently set rendertarget.
514 *
515 * @return The currently set render target.
516 */
517 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
518 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
519
520 class AutoRenderTargetRestore : public ::GrNoncopyable {
521 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000522 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
524 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000525 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000526 this->set(ds, newTarget);
527 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000528 ~AutoRenderTargetRestore() { this->restore(); }
529
530 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000531 if (NULL != fDrawState) {
532 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000533 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000534 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000535 GrSafeSetNull(fSavedTarget);
536 }
537
538 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
539 this->restore();
540
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000541 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000542 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000543 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000544 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000545 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000546 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000548 }
549 private:
550 GrDrawState* fDrawState;
551 GrRenderTarget* fSavedTarget;
552 };
553
554 /// @}
555
556 ///////////////////////////////////////////////////////////////////////////
557 /// @name Stencil
558 ////
559
560 /**
561 * Sets the stencil settings to use for the next draw.
562 * Changing the clip has the side-effect of possibly zeroing
563 * out the client settable stencil bits. So multipass algorithms
564 * using stencil should not change the clip between passes.
565 * @param settings the stencil settings to use.
566 */
567 void setStencil(const GrStencilSettings& settings) {
568 fStencilSettings = settings;
569 }
570
571 /**
572 * Shortcut to disable stencil testing and ops.
573 */
574 void disableStencil() {
575 fStencilSettings.setDisabled();
576 }
577
578 const GrStencilSettings& getStencil() const { return fStencilSettings; }
579
580 GrStencilSettings* stencil() { return &fStencilSettings; }
581
582 /// @}
583
584 ///////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000585 /// @name Color Matrix
586 ////
587
588 /**
589 * Sets the color matrix to use for the next draw.
590 * @param matrix the 5x4 matrix to apply to the incoming color
591 */
592 void setColorMatrix(const float matrix[20]) {
593 memcpy(fColorMatrix, matrix, sizeof(fColorMatrix));
594 }
595
596 const float* getColorMatrix() const { return fColorMatrix; }
597
598 /// @}
599
600 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601 // @name Edge AA
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000602 // Edge equations can be specified to perform antialiasing. Because the
603 // edges are specified as per-vertex data, vertices that are shared by
604 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000606 ////
607
608 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000609 * When specifying edges as vertex data this enum specifies what type of
610 * edges are in use. The edges are always 4 GrScalars in memory, even when
611 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000612 *
613 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
614 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000615 */
616 enum VertexEdgeType {
617 /* 1-pixel wide line
618 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
619 kHairLine_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000620 /* Quadratic specified by u^2-v canonical coords (only 2
621 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000622 being inside, positive outside. Edge specified in window space
623 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000624 kQuad_EdgeType,
625 /* Same as above but for hairline quadratics. Uses unsigned distance.
626 Coverage is min(0, 1-distance). */
627 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000628 /* Circle specified as center_x, center_y, outer_radius, inner_radius
629 all in window space (y-down). */
630 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000631
632 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000633 };
634
635 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000636 * Determines the interpretation per-vertex edge data when the
637 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
638 * are not specified the value of this setting has no effect.
639 */
640 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000641 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000642 fVertexEdgeType = type;
643 }
644
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000645 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000646
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000647 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000648
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000649 ///////////////////////////////////////////////////////////////////////////
650 /// @name State Flags
651 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000652
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000653 /**
654 * Flags that affect rendering. Controlled using enable/disableState(). All
655 * default to disabled.
656 */
657 enum StateBits {
658 /**
659 * Perform dithering. TODO: Re-evaluate whether we need this bit
660 */
661 kDither_StateBit = 0x01,
662 /**
663 * Perform HW anti-aliasing. This means either HW FSAA, if supported
664 * by the render target, or smooth-line rendering if a line primitive
665 * is drawn and line smoothing is supported by the 3D API.
666 */
667 kHWAntialias_StateBit = 0x02,
668 /**
669 * Draws will respect the clip, otherwise the clip is ignored.
670 */
671 kClip_StateBit = 0x04,
672 /**
673 * Disables writing to the color buffer. Useful when performing stencil
674 * operations.
675 */
676 kNoColorWrites_StateBit = 0x08,
677 /**
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000678 * Draws will apply the color matrix, otherwise the color matrix is
679 * ignored.
680 */
681 kColorMatrix_StateBit = 0x20,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000682
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 // Users of the class may add additional bits to the vector
684 kDummyStateBit,
685 kLastPublicStateBit = kDummyStateBit-1,
686 };
687
688 void resetStateFlags() {
689 fFlagBits = 0;
690 }
691
692 /**
693 * Enable render state settings.
694 *
695 * @param flags bitfield of StateBits specifing the states to enable
696 */
697 void enableState(uint32_t stateBits) {
698 fFlagBits |= stateBits;
699 }
700
701 /**
702 * Disable render state settings.
703 *
704 * @param flags bitfield of StateBits specifing the states to disable
705 */
706 void disableState(uint32_t stateBits) {
707 fFlagBits &= ~(stateBits);
708 }
709
710 bool isDitherState() const {
711 return 0 != (fFlagBits & kDither_StateBit);
712 }
713
714 bool isHWAntialiasState() const {
715 return 0 != (fFlagBits & kHWAntialias_StateBit);
716 }
717
718 bool isClipState() const {
719 return 0 != (fFlagBits & kClip_StateBit);
720 }
721
722 bool isColorWriteDisabled() const {
723 return 0 != (fFlagBits & kNoColorWrites_StateBit);
724 }
725
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000726 bool isStateFlagEnabled(uint32_t stateBit) const {
727 return 0 != (stateBit & fFlagBits);
728 }
729
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 /// @}
731
732 ///////////////////////////////////////////////////////////////////////////
733 /// @name Face Culling
734 ////
735
736 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000737 kInvalid_DrawFace = -1,
738
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000739 kBoth_DrawFace,
740 kCCW_DrawFace,
741 kCW_DrawFace,
742 };
743
744 /**
745 * Controls whether clockwise, counterclockwise, or both faces are drawn.
746 * @param face the face(s) to draw.
747 */
748 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000749 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000750 fDrawFace = face;
751 }
752
753 /**
754 * Gets whether the target is drawing clockwise, counterclockwise,
755 * or both faces.
756 * @return the current draw face(s).
757 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000758 DrawFace getDrawFace() const { return fDrawFace; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000759
760 /// @}
761
762 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000763
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000764 bool isStageEnabled(int s) const {
765 GrAssert((unsigned)s < kNumStages);
766 return (NULL != fTextures[s]) ||
767 (NULL != fSamplerStates[s].getCustomStage());
768 }
769
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000770 // Most stages are usually not used, so conditionals here
771 // reduce the expected number of bytes touched by 50%.
772 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000773 if (memcmp(this->podStart(), s.podStart(), this->podSize())) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000774 return false;
775 }
776
777 if (!s.fViewMatrix.cheapEqualTo(fViewMatrix)) {
778 return false;
779 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000780
781 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000782 if (this->isStageEnabled(i) &&
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000783 this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000784 return false;
785 }
786 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000787 if (kColorMatrix_StateBit & s.fFlagBits) {
788 if (memcmp(fColorMatrix,
789 s.fColorMatrix,
790 sizeof(fColorMatrix))) {
791 return false;
792 }
793 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000794
795 return true;
796 }
797 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
798
799 // Most stages are usually not used, so conditionals here
800 // reduce the expected number of bytes touched by 50%.
801 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000802 memcpy(this->podStart(), s.podStart(), this->podSize());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000803
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000804 fViewMatrix = s.fViewMatrix;
805
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000806 for (int i = 0; i < kNumStages; i++) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000807 SkSafeRef(fTextures[i]); // already copied by memcpy
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000808 if (s.fTextures[i]) {
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000809 this->fSamplerStates[i] = s.fSamplerStates[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000810 }
811 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000812
813 SkSafeRef(fRenderTarget); // already copied by memcpy
814
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000815 if (kColorMatrix_StateBit & s.fFlagBits) {
816 memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix));
817 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000818
819 return *this;
820 }
821
822private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000823
824 const void* podStart() const {
825 return reinterpret_cast<const void*>(&fPodStartMarker);
826 }
827 void* podStart() {
828 return reinterpret_cast<void*>(&fPodStartMarker);
829 }
830 size_t memsetSize() const {
831 return reinterpret_cast<size_t>(&fMemsetEndMarker) -
832 reinterpret_cast<size_t>(&fPodStartMarker) +
833 sizeof(fMemsetEndMarker);
834 }
835 size_t podSize() const {
836 // Can't use offsetof() with non-POD types, so stuck with pointer math.
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000837 return reinterpret_cast<size_t>(&fPodEndMarker) -
838 reinterpret_cast<size_t>(&fPodStartMarker) +
839 sizeof(fPodEndMarker);
840 }
841
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000842 static const StageMask kIllegalStageMaskBits = ~((1U << kNumStages)-1);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000843 // @{ these fields can be initialized with memset to 0
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000844 union {
845 GrColor fBlendConstant;
846 GrColor fPodStartMarker;
847 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000848 GrColor fColorFilterColor;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000849 DrawFace fDrawFace;
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000850 VertexEdgeType fVertexEdgeType;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000851 GrStencilSettings fStencilSettings;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000852 union {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000853 uint32_t fFlagBits;
854 uint32_t fMemsetEndMarker;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000855 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000856 // @}
857
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000858 int fFirstCoverageStage;
859
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000860 // @{ Initialized to values other than zero, but memcmp'ed in operator==
861 // and memcpy'ed in operator=.
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000862 GrTexture* fTextures[kNumStages];
863 GrRenderTarget* fRenderTarget;
864
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000865 GrColor fColor;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000866 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000867 SkXfermode::Mode fColorFilterMode;
868 GrBlendCoeff fSrcBlend;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000869 union {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000870 GrBlendCoeff fDstBlend;
871 GrBlendCoeff fPodEndMarker;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000872 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000873 // @}
874
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000875 GrMatrix fViewMatrix;
876
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000877 // This field must be last; it will not be copied or compared
878 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000879 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000880 // only compared if the color matrix enable flag is set
881 float fColorMatrix[20]; // 5 x 4 matrix
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000882
reed@google.comfa35e3d2012-06-26 20:16:17 +0000883 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000884};
885
886#endif