blob: c07001c3a33880cb8ca54f8296ca2b280be9abb8 [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 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000228
229 void disableStage(int index) {
230 GrSafeSetNull(fTextures[index]);
231 fSamplerStates[index].setCustomStage(NULL);
232 }
233
robertphillips@google.com972265d2012-06-13 18:49:30 +0000234 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000235 * Release all the textures and custom stages referred to by this
236 * draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000237 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000238 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000239 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000240 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000241 }
242 }
243
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000244 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000245 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000246 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
247 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000248 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000249 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000250 }
251 }
252 private:
253 GrDrawState* fDrawState;
254 };
255
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000256 /// @}
257
258 ///////////////////////////////////////////////////////////////////////////
259 /// @name Samplers
260 ////
261
262 /**
263 * Returns the current sampler for a stage.
264 */
265 const GrSamplerState& getSampler(int stage) const {
266 GrAssert((unsigned)stage < kNumStages);
267 return fSamplerStates[stage];
268 }
269
270 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000271 * Writable pointer to a stage's sampler.
272 */
273 GrSamplerState* sampler(int stage) {
274 GrAssert((unsigned)stage < kNumStages);
275 return fSamplerStates + stage;
276 }
277
278 /**
279 * Preconcats the matrix of all samplers in the mask with the same matrix.
280 */
281 void preConcatSamplerMatrices(StageMask stageMask, const GrMatrix& matrix) {
282 GrAssert(!(stageMask & kIllegalStageMaskBits));
283 for (int i = 0; i < kNumStages; ++i) {
284 if ((1 << i) & stageMask) {
285 fSamplerStates[i].preConcatMatrix(matrix);
286 }
287 }
288 }
289
290 /// @}
291
292 ///////////////////////////////////////////////////////////////////////////
293 /// @name Coverage / Color Stages
294 ////
295
296 /**
297 * A common pattern is to compute a color with the initial stages and then
298 * modulate that color by a coverage value in later stage(s) (AA, mask-
299 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
300 * computed based on the pre-coverage-modulated color. The division of
301 * stages between color-computing and coverage-computing is specified by
302 * this method. Initially this is kNumStages (all stages
303 * are color-computing).
304 */
305 void setFirstCoverageStage(int firstCoverageStage) {
306 GrAssert((unsigned)firstCoverageStage <= kNumStages);
307 fFirstCoverageStage = firstCoverageStage;
308 }
309
310 /**
311 * Gets the index of the first coverage-computing stage.
312 */
313 int getFirstCoverageStage() const {
314 return fFirstCoverageStage;
315 }
316
317 ///@}
318
319 ///////////////////////////////////////////////////////////////////////////
320 /// @name Blending
321 ////
322
323 /**
324 * Sets the blending function coeffecients.
325 *
326 * The blend function will be:
327 * D' = sat(S*srcCoef + D*dstCoef)
328 *
329 * where D is the existing destination color, S is the incoming source
330 * color, and D' is the new destination color that will be written. sat()
331 * is the saturation function.
332 *
333 * @param srcCoef coeffecient applied to the src color.
334 * @param dstCoef coeffecient applied to the dst color.
335 */
336 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
337 fSrcBlend = srcCoeff;
338 fDstBlend = dstCoeff;
339 #if GR_DEBUG
340 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000341 case kDC_GrBlendCoeff:
342 case kIDC_GrBlendCoeff:
343 case kDA_GrBlendCoeff:
344 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000345 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
346 "coverage stages.\n");
347 break;
348 default:
349 break;
350 }
351 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000352 case kSC_GrBlendCoeff:
353 case kISC_GrBlendCoeff:
354 case kSA_GrBlendCoeff:
355 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000356 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
357 "coverage stages.\n");
358 break;
359 default:
360 break;
361 }
362 #endif
363 }
364
365 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
366 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
367
368 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
369 GrBlendCoeff* dstBlendCoeff) const {
370 *srcBlendCoeff = fSrcBlend;
371 *dstBlendCoeff = fDstBlend;
372 }
373
374 /**
375 * Sets the blending function constant referenced by the following blending
376 * coeffecients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000377 * kConstC_GrBlendCoeff
378 * kIConstC_GrBlendCoeff
379 * kConstA_GrBlendCoeff
380 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000381 *
382 * @param constant the constant to set
383 */
384 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
385
386 /**
387 * Retrieves the last value set by setBlendConstant()
388 * @return the blending constant value
389 */
390 GrColor getBlendConstant() const { return fBlendConstant; }
391
392 /// @}
393
394 ///////////////////////////////////////////////////////////////////////////
395 /// @name View Matrix
396 ////
397
398 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000399 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400 *
401 * In the post-view-matrix space the rectangle [0,w]x[0,h]
402 * fully covers the render target. (w and h are the width and height of the
403 * the rendertarget.)
404 */
405 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
406
407 /**
408 * Gets a writable pointer to the view matrix.
409 */
410 GrMatrix* viewMatrix() { return &fViewMatrix; }
411
412 /**
413 * Multiplies the current view matrix by a matrix
414 *
415 * After this call V' = V*m where V is the old view matrix,
416 * m is the parameter to this function, and V' is the new view matrix.
417 * (We consider positions to be column vectors so position vector p is
418 * transformed by matrix X as p' = X*p.)
419 *
420 * @param m the matrix used to modify the view matrix.
421 */
422 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
423
424 /**
425 * Multiplies the current view matrix by a matrix
426 *
427 * After this call V' = m*V where V is the old view matrix,
428 * m is the parameter to this function, and V' is the new view matrix.
429 * (We consider positions to be column vectors so position vector p is
430 * transformed by matrix X as p' = X*p.)
431 *
432 * @param m the matrix used to modify the view matrix.
433 */
434 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
435
436 /**
437 * Retrieves the current view matrix
438 * @return the current view matrix.
439 */
440 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
441
442 /**
443 * Retrieves the inverse of the current view matrix.
444 *
445 * If the current view matrix is invertible, return true, and if matrix
446 * is non-null, copy the inverse into it. If the current view matrix is
447 * non-invertible, return false and ignore the matrix parameter.
448 *
449 * @param matrix if not null, will receive a copy of the current inverse.
450 */
451 bool getViewInverse(GrMatrix* matrix) const {
452 // TODO: determine whether we really need to leave matrix unmodified
453 // at call sites when inversion fails.
454 GrMatrix inverse;
455 if (fViewMatrix.invert(&inverse)) {
456 if (matrix) {
457 *matrix = inverse;
458 }
459 return true;
460 }
461 return false;
462 }
463
464 class AutoViewMatrixRestore : public ::GrNoncopyable {
465 public:
466 AutoViewMatrixRestore() : fDrawState(NULL) {}
467 AutoViewMatrixRestore(GrDrawState* ds, const GrMatrix& newMatrix) {
468 fDrawState = NULL;
469 this->set(ds, newMatrix);
470 }
471 AutoViewMatrixRestore(GrDrawState* ds) {
472 fDrawState = NULL;
473 this->set(ds);
474 }
475 ~AutoViewMatrixRestore() {
476 this->set(NULL, GrMatrix::I());
477 }
478 void set(GrDrawState* ds, const GrMatrix& newMatrix) {
479 if (NULL != fDrawState) {
480 fDrawState->setViewMatrix(fSavedMatrix);
481 }
482 if (NULL != ds) {
483 fSavedMatrix = ds->getViewMatrix();
484 ds->setViewMatrix(newMatrix);
485 }
486 fDrawState = ds;
487 }
488 void set(GrDrawState* ds) {
489 if (NULL != fDrawState) {
490 fDrawState->setViewMatrix(fSavedMatrix);
491 }
492 if (NULL != ds) {
493 fSavedMatrix = ds->getViewMatrix();
494 }
495 fDrawState = ds;
496 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000497 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 private:
499 GrDrawState* fDrawState;
500 GrMatrix fSavedMatrix;
tomhudson@google.com93813632011-10-27 20:21:16 +0000501 };
502
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000503 /// @}
504
505 ///////////////////////////////////////////////////////////////////////////
506 /// @name Render Target
507 ////
508
509 /**
510 * Sets the rendertarget used at the next drawing call
511 *
512 * @param target The render target to set.
513 */
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000514 void setRenderTarget(GrRenderTarget* target) {
515 GrSafeAssign(fRenderTarget, target);
516 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000517
518 /**
519 * Retrieves the currently set rendertarget.
520 *
521 * @return The currently set render target.
522 */
523 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
524 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
525
526 class AutoRenderTargetRestore : public ::GrNoncopyable {
527 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000528 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000529 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
530 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000531 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000532 this->set(ds, newTarget);
533 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000534 ~AutoRenderTargetRestore() { this->restore(); }
535
536 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000537 if (NULL != fDrawState) {
538 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000539 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000540 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000541 GrSafeSetNull(fSavedTarget);
542 }
543
544 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
545 this->restore();
546
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000548 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000549 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000550 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000552 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000553 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000554 }
555 private:
556 GrDrawState* fDrawState;
557 GrRenderTarget* fSavedTarget;
558 };
559
560 /// @}
561
562 ///////////////////////////////////////////////////////////////////////////
563 /// @name Stencil
564 ////
565
566 /**
567 * Sets the stencil settings to use for the next draw.
568 * Changing the clip has the side-effect of possibly zeroing
569 * out the client settable stencil bits. So multipass algorithms
570 * using stencil should not change the clip between passes.
571 * @param settings the stencil settings to use.
572 */
573 void setStencil(const GrStencilSettings& settings) {
574 fStencilSettings = settings;
575 }
576
577 /**
578 * Shortcut to disable stencil testing and ops.
579 */
580 void disableStencil() {
581 fStencilSettings.setDisabled();
582 }
583
584 const GrStencilSettings& getStencil() const { return fStencilSettings; }
585
586 GrStencilSettings* stencil() { return &fStencilSettings; }
587
588 /// @}
589
590 ///////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000591 /// @name Color Matrix
592 ////
593
594 /**
595 * Sets the color matrix to use for the next draw.
596 * @param matrix the 5x4 matrix to apply to the incoming color
597 */
598 void setColorMatrix(const float matrix[20]) {
599 memcpy(fColorMatrix, matrix, sizeof(fColorMatrix));
600 }
601
602 const float* getColorMatrix() const { return fColorMatrix; }
603
604 /// @}
605
606 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000607 // @name Edge AA
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000608 // Edge equations can be specified to perform antialiasing. Because the
609 // edges are specified as per-vertex data, vertices that are shared by
610 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000611 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000612 ////
613
614 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000615 * When specifying edges as vertex data this enum specifies what type of
616 * edges are in use. The edges are always 4 GrScalars in memory, even when
617 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000618 *
619 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
620 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000621 */
622 enum VertexEdgeType {
623 /* 1-pixel wide line
624 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
625 kHairLine_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000626 /* Quadratic specified by u^2-v canonical coords (only 2
627 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000628 being inside, positive outside. Edge specified in window space
629 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000630 kQuad_EdgeType,
631 /* Same as above but for hairline quadratics. Uses unsigned distance.
632 Coverage is min(0, 1-distance). */
633 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000634 /* Circle specified as center_x, center_y, outer_radius, inner_radius
635 all in window space (y-down). */
636 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000637
638 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000639 };
640
641 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000642 * Determines the interpretation per-vertex edge data when the
643 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
644 * are not specified the value of this setting has no effect.
645 */
646 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000647 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000648 fVertexEdgeType = type;
649 }
650
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000651 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000652
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000653 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000654
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000655 ///////////////////////////////////////////////////////////////////////////
656 /// @name State Flags
657 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000658
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000659 /**
660 * Flags that affect rendering. Controlled using enable/disableState(). All
661 * default to disabled.
662 */
663 enum StateBits {
664 /**
665 * Perform dithering. TODO: Re-evaluate whether we need this bit
666 */
667 kDither_StateBit = 0x01,
668 /**
669 * Perform HW anti-aliasing. This means either HW FSAA, if supported
670 * by the render target, or smooth-line rendering if a line primitive
671 * is drawn and line smoothing is supported by the 3D API.
672 */
673 kHWAntialias_StateBit = 0x02,
674 /**
675 * Draws will respect the clip, otherwise the clip is ignored.
676 */
677 kClip_StateBit = 0x04,
678 /**
679 * Disables writing to the color buffer. Useful when performing stencil
680 * operations.
681 */
682 kNoColorWrites_StateBit = 0x08,
683 /**
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000684 * Draws will apply the color matrix, otherwise the color matrix is
685 * ignored.
686 */
687 kColorMatrix_StateBit = 0x20,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000688
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000689 // Users of the class may add additional bits to the vector
690 kDummyStateBit,
691 kLastPublicStateBit = kDummyStateBit-1,
692 };
693
694 void resetStateFlags() {
695 fFlagBits = 0;
696 }
697
698 /**
699 * Enable render state settings.
700 *
701 * @param flags bitfield of StateBits specifing the states to enable
702 */
703 void enableState(uint32_t stateBits) {
704 fFlagBits |= stateBits;
705 }
706
707 /**
708 * Disable render state settings.
709 *
710 * @param flags bitfield of StateBits specifing the states to disable
711 */
712 void disableState(uint32_t stateBits) {
713 fFlagBits &= ~(stateBits);
714 }
715
716 bool isDitherState() const {
717 return 0 != (fFlagBits & kDither_StateBit);
718 }
719
720 bool isHWAntialiasState() const {
721 return 0 != (fFlagBits & kHWAntialias_StateBit);
722 }
723
724 bool isClipState() const {
725 return 0 != (fFlagBits & kClip_StateBit);
726 }
727
728 bool isColorWriteDisabled() const {
729 return 0 != (fFlagBits & kNoColorWrites_StateBit);
730 }
731
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000732 bool isStateFlagEnabled(uint32_t stateBit) const {
733 return 0 != (stateBit & fFlagBits);
734 }
735
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000736 /// @}
737
738 ///////////////////////////////////////////////////////////////////////////
739 /// @name Face Culling
740 ////
741
742 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000743 kInvalid_DrawFace = -1,
744
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000745 kBoth_DrawFace,
746 kCCW_DrawFace,
747 kCW_DrawFace,
748 };
749
750 /**
751 * Controls whether clockwise, counterclockwise, or both faces are drawn.
752 * @param face the face(s) to draw.
753 */
754 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000755 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000756 fDrawFace = face;
757 }
758
759 /**
760 * Gets whether the target is drawing clockwise, counterclockwise,
761 * or both faces.
762 * @return the current draw face(s).
763 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000764 DrawFace getDrawFace() const { return fDrawFace; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000765
766 /// @}
767
768 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000769
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000770 bool isStageEnabled(int s) const {
771 GrAssert((unsigned)s < kNumStages);
772 return (NULL != fTextures[s]) ||
773 (NULL != fSamplerStates[s].getCustomStage());
774 }
775
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000776 // Most stages are usually not used, so conditionals here
777 // reduce the expected number of bytes touched by 50%.
778 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000779 if (memcmp(this->podStart(), s.podStart(), this->podSize())) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000780 return false;
781 }
782
783 if (!s.fViewMatrix.cheapEqualTo(fViewMatrix)) {
784 return false;
785 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000786
787 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000788 if (this->isStageEnabled(i) &&
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000789 this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000790 return false;
791 }
792 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000793 if (kColorMatrix_StateBit & s.fFlagBits) {
794 if (memcmp(fColorMatrix,
795 s.fColorMatrix,
796 sizeof(fColorMatrix))) {
797 return false;
798 }
799 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000800
801 return true;
802 }
803 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
804
805 // Most stages are usually not used, so conditionals here
806 // reduce the expected number of bytes touched by 50%.
807 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000808 memcpy(this->podStart(), s.podStart(), this->podSize());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000809
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000810 fViewMatrix = s.fViewMatrix;
811
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000812 for (int i = 0; i < kNumStages; i++) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000813 SkSafeRef(fTextures[i]); // already copied by memcpy
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000814 if (s.fTextures[i]) {
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000815 this->fSamplerStates[i] = s.fSamplerStates[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000816 }
817 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000818
819 SkSafeRef(fRenderTarget); // already copied by memcpy
820
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000821 if (kColorMatrix_StateBit & s.fFlagBits) {
822 memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix));
823 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000824
825 return *this;
826 }
827
828private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000829
830 const void* podStart() const {
831 return reinterpret_cast<const void*>(&fPodStartMarker);
832 }
833 void* podStart() {
834 return reinterpret_cast<void*>(&fPodStartMarker);
835 }
836 size_t memsetSize() const {
837 return reinterpret_cast<size_t>(&fMemsetEndMarker) -
838 reinterpret_cast<size_t>(&fPodStartMarker) +
839 sizeof(fMemsetEndMarker);
840 }
841 size_t podSize() const {
842 // Can't use offsetof() with non-POD types, so stuck with pointer math.
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000843 return reinterpret_cast<size_t>(&fPodEndMarker) -
844 reinterpret_cast<size_t>(&fPodStartMarker) +
845 sizeof(fPodEndMarker);
846 }
847
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000848 static const StageMask kIllegalStageMaskBits = ~((1U << kNumStages)-1);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000849 // @{ these fields can be initialized with memset to 0
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000850 union {
851 GrColor fBlendConstant;
852 GrColor fPodStartMarker;
853 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000854 GrColor fColorFilterColor;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000855 DrawFace fDrawFace;
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000856 VertexEdgeType fVertexEdgeType;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000857 GrStencilSettings fStencilSettings;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000858 union {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000859 uint32_t fFlagBits;
860 uint32_t fMemsetEndMarker;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000861 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000862 // @}
863
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000864 // @{ Initialized to values other than zero, but memcmp'ed in operator==
865 // and memcpy'ed in operator=.
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000866 GrTexture* fTextures[kNumStages];
867 GrRenderTarget* fRenderTarget;
868
robertphillips@google.com69ffcf02012-06-26 21:01:05 +0000869 int fFirstCoverageStage;
870
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000871 GrColor fColor;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000872 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000873 SkXfermode::Mode fColorFilterMode;
874 GrBlendCoeff fSrcBlend;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000875 union {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000876 GrBlendCoeff fDstBlend;
877 GrBlendCoeff fPodEndMarker;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000878 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000879 // @}
880
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000881 GrMatrix fViewMatrix;
882
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000883 // This field must be last; it will not be copied or compared
884 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000885 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000886 // only compared if the color matrix enable flag is set
887 float fColorMatrix[20]; // 5 x 4 matrix
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000888
reed@google.comfa35e3d2012-06-26 20:16:17 +0000889 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000890};
891
892#endif