blob: 5feccb57cb11df942d728c67e91e4052aa94e661 [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 {
tomhudson@google.com93813632011-10-27 20:21:16 +000024
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000025public:
tomhudson@google.com93813632011-10-27 20:21:16 +000026 /**
27 * Number of texture stages. Each stage takes as input a color and
28 * 2D texture coordinates. The color input to the first enabled stage is the
29 * per-vertex color or the constant color (setColor/setAlpha) if there are
30 * no per-vertex colors. For subsequent stages the input color is the output
31 * color from the previous enabled stage. The output color of each stage is
32 * the input color modulated with the result of a texture lookup. Texture
33 * lookups are specified by a texture a sampler (setSamplerState). Texture
34 * coordinates for each stage come from the vertices based on a
35 * GrVertexLayout bitfield. The output fragment color is the output color of
36 * the last enabled stage. The presence or absence of texture coordinates
37 * for each stage in the vertex layout indicates whether a stage is enabled
38 * or not.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000039 *
40 * Stages 0 through GrPaint::kTotalStages-1 are reserved for setting up
41 * the draw (i.e., textures and filter masks). Stages GrPaint::kTotalStages
42 * through kNumStages-1 are earmarked for use by GrTextContext and
43 * GrPathRenderer-derived classes.
tomhudson@google.com93813632011-10-27 20:21:16 +000044 */
45 enum {
robertphillips@google.comec05eaa2012-04-27 18:59:52 +000046 kNumStages = 4,
tomhudson@google.com93813632011-10-27 20:21:16 +000047 kMaxTexCoords = kNumStages
48 };
49
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000050 /**
51 * Bitfield used to indicate a set of stages.
52 */
53 typedef uint32_t StageMask;
54 GR_STATIC_ASSERT(sizeof(StageMask)*8 >= GrDrawState::kNumStages);
55
robertphillips@google.com9ec07532012-06-22 12:01:30 +000056 GrDrawState()
57 : fRenderTarget(NULL) {
58
59 for (int i = 0; i < kNumStages; ++i) {
60 fTextures[i] = NULL;
61 }
62
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000063 this->reset();
64 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000065
robertphillips@google.com9ec07532012-06-22 12:01:30 +000066 GrDrawState(const GrDrawState& state)
67 : fRenderTarget(NULL) {
68
69 for (int i = 0; i < kNumStages; ++i) {
70 fTextures[i] = NULL;
71 }
72
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000073 *this = state;
74 }
75
robertphillips@google.com9ec07532012-06-22 12:01:30 +000076 virtual ~GrDrawState() {
77 this->releaseTextures();
78 GrSafeSetNull(fRenderTarget);
79 }
80
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000081 /**
82 * Resets to the default state. Sampler states will not be modified.
83 */
84 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000085
86 this->releaseTextures();
87 GrSafeSetNull(fRenderTarget);
88
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000089 // make sure any pad is zero for memcmp
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000090 // all GrDrawState members should default to something valid by the
91 // the memset except those initialized individually below. There should
92 // be no padding between the individually initialized members.
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000093 memset(this->podStart(), 0, this->memsetSize());
94
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000095 // pedantic assertion that our ptrs will
96 // be NULL (0 ptr is mem addr 0)
97 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000098 GR_STATIC_ASSERT(0 == kBoth_DrawFace);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000099 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000100
101 // memset exceptions
102 fColor = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000103 fCoverage = 0xffffffff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000104 fFirstCoverageStage = kNumStages;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000105 fColorFilterMode = SkXfermode::kDst_Mode;
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 fSrcBlend = kOne_GrBlendCoeff;
107 fDstBlend = kZero_GrBlendCoeff;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000108 fViewMatrix.reset();
109
110 // ensure values that will be memcmp'ed in == but not memset in reset()
111 // are tightly packed
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000112 GrAssert(this->memsetSize() + sizeof(fColor) + sizeof(fCoverage) +
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000113 sizeof(fFirstCoverageStage) + sizeof(fColorFilterMode) +
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000114 sizeof(fSrcBlend) + sizeof(fDstBlend) + sizeof(fTextures) +
115 sizeof(fRenderTarget) == this->podSize());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000116 }
117
118 ///////////////////////////////////////////////////////////////////////////
119 /// @name Color
120 ////
121
122 /**
123 * Sets color for next draw to a premultiplied-alpha color.
124 *
125 * @param color the color to set.
126 */
127 void setColor(GrColor color) { fColor = color; }
128
129 GrColor getColor() const { return fColor; }
130
131 /**
132 * Sets the color to be used for the next draw to be
133 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
134 *
135 * @param alpha The alpha value to set as the color.
136 */
137 void setAlpha(uint8_t a) {
138 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
139 }
140
141 /**
142 * Add a color filter that can be represented by a color and a mode. Applied
143 * after color-computing texture stages.
144 */
145 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
146 fColorFilterColor = c;
147 fColorFilterMode = mode;
148 }
149
150 GrColor getColorFilterColor() const { return fColorFilterColor; }
151 SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; }
152
153 /// @}
154
155 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000156 /// @name Coverage
157 ////
158
159 /**
160 * Sets a constant fractional coverage to be applied to the draw. The
161 * initial value (after construction or reset()) is 0xff. The constant
162 * coverage is ignored when per-vertex coverage is provided.
163 */
164 void setCoverage(uint8_t coverage) {
165 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
166 }
167
168 /**
169 * Version of above that specifies 4 channel per-vertex color. The value
170 * should be premultiplied.
171 */
172 void setCoverage4(GrColor coverage) {
173 fCoverage = coverage;
174 }
175
176 GrColor getCoverage() const {
177 return fCoverage;
178 }
179
180 /// @}
181
182 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000183 /// @name Textures
184 ////
185
186 /**
187 * Sets the texture used at the next drawing call
188 *
189 * @param stage The texture stage for which the texture will be set
190 *
191 * @param texture The texture to set. Can be NULL though there is no
192 * advantage to settings a NULL texture if doing non-textured drawing
193 */
194 void setTexture(int stage, GrTexture* texture) {
195 GrAssert((unsigned)stage < kNumStages);
robertphillips@google.com1942c052012-05-03 17:58:27 +0000196
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000197 GrSafeAssign(fTextures[stage], texture);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000198 }
199
200 /**
201 * Retrieves the currently set texture.
202 *
203 * @return The currently set texture. The return value will be NULL if no
204 * texture has been set, NULL was most recently passed to
205 * setTexture, or the last setTexture was destroyed.
206 */
207 const GrTexture* getTexture(int stage) const {
208 GrAssert((unsigned)stage < kNumStages);
209 return fTextures[stage];
210 }
211 GrTexture* getTexture(int stage) {
212 GrAssert((unsigned)stage < kNumStages);
213 return fTextures[stage];
214 }
215
robertphillips@google.com972265d2012-06-13 18:49:30 +0000216 /**
217 * Release all the textures referred to by this draw state
218 */
219 void releaseTextures() {
220 for (int i = 0; i < kNumStages; ++i) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000221 GrSafeSetNull(fTextures[i]);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000222 }
223 }
224
225 class AutoTextureRelease : public ::GrNoncopyable {
226 public:
227 AutoTextureRelease(GrDrawState* ds) : fDrawState(ds) {}
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000228 ~AutoTextureRelease() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000229 if (NULL != fDrawState) {
230 fDrawState->releaseTextures();
231 }
232 }
233 private:
234 GrDrawState* fDrawState;
235 };
236
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000237 /// @}
238
239 ///////////////////////////////////////////////////////////////////////////
240 /// @name Samplers
241 ////
242
243 /**
244 * Returns the current sampler for a stage.
245 */
246 const GrSamplerState& getSampler(int stage) const {
247 GrAssert((unsigned)stage < kNumStages);
248 return fSamplerStates[stage];
249 }
250
251 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000252 * Writable pointer to a stage's sampler.
253 */
254 GrSamplerState* sampler(int stage) {
255 GrAssert((unsigned)stage < kNumStages);
256 return fSamplerStates + stage;
257 }
258
259 /**
260 * Preconcats the matrix of all samplers in the mask with the same matrix.
261 */
262 void preConcatSamplerMatrices(StageMask stageMask, const GrMatrix& matrix) {
263 GrAssert(!(stageMask & kIllegalStageMaskBits));
264 for (int i = 0; i < kNumStages; ++i) {
265 if ((1 << i) & stageMask) {
266 fSamplerStates[i].preConcatMatrix(matrix);
267 }
268 }
269 }
270
271 /// @}
272
273 ///////////////////////////////////////////////////////////////////////////
274 /// @name Coverage / Color Stages
275 ////
276
277 /**
278 * A common pattern is to compute a color with the initial stages and then
279 * modulate that color by a coverage value in later stage(s) (AA, mask-
280 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
281 * computed based on the pre-coverage-modulated color. The division of
282 * stages between color-computing and coverage-computing is specified by
283 * this method. Initially this is kNumStages (all stages
284 * are color-computing).
285 */
286 void setFirstCoverageStage(int firstCoverageStage) {
287 GrAssert((unsigned)firstCoverageStage <= kNumStages);
288 fFirstCoverageStage = firstCoverageStage;
289 }
290
291 /**
292 * Gets the index of the first coverage-computing stage.
293 */
294 int getFirstCoverageStage() const {
295 return fFirstCoverageStage;
296 }
297
298 ///@}
299
300 ///////////////////////////////////////////////////////////////////////////
301 /// @name Blending
302 ////
303
304 /**
305 * Sets the blending function coeffecients.
306 *
307 * The blend function will be:
308 * D' = sat(S*srcCoef + D*dstCoef)
309 *
310 * where D is the existing destination color, S is the incoming source
311 * color, and D' is the new destination color that will be written. sat()
312 * is the saturation function.
313 *
314 * @param srcCoef coeffecient applied to the src color.
315 * @param dstCoef coeffecient applied to the dst color.
316 */
317 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
318 fSrcBlend = srcCoeff;
319 fDstBlend = dstCoeff;
320 #if GR_DEBUG
321 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000322 case kDC_GrBlendCoeff:
323 case kIDC_GrBlendCoeff:
324 case kDA_GrBlendCoeff:
325 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000326 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
327 "coverage stages.\n");
328 break;
329 default:
330 break;
331 }
332 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000333 case kSC_GrBlendCoeff:
334 case kISC_GrBlendCoeff:
335 case kSA_GrBlendCoeff:
336 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000337 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
338 "coverage stages.\n");
339 break;
340 default:
341 break;
342 }
343 #endif
344 }
345
346 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
347 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
348
349 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
350 GrBlendCoeff* dstBlendCoeff) const {
351 *srcBlendCoeff = fSrcBlend;
352 *dstBlendCoeff = fDstBlend;
353 }
354
355 /**
356 * Sets the blending function constant referenced by the following blending
357 * coeffecients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000358 * kConstC_GrBlendCoeff
359 * kIConstC_GrBlendCoeff
360 * kConstA_GrBlendCoeff
361 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000362 *
363 * @param constant the constant to set
364 */
365 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
366
367 /**
368 * Retrieves the last value set by setBlendConstant()
369 * @return the blending constant value
370 */
371 GrColor getBlendConstant() const { return fBlendConstant; }
372
373 /// @}
374
375 ///////////////////////////////////////////////////////////////////////////
376 /// @name View Matrix
377 ////
378
379 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000380 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000381 *
382 * In the post-view-matrix space the rectangle [0,w]x[0,h]
383 * fully covers the render target. (w and h are the width and height of the
384 * the rendertarget.)
385 */
386 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
387
388 /**
389 * Gets a writable pointer to the view matrix.
390 */
391 GrMatrix* viewMatrix() { return &fViewMatrix; }
392
393 /**
394 * Multiplies the current view matrix by a matrix
395 *
396 * After this call V' = V*m where V is the old view matrix,
397 * m is the parameter to this function, and V' is the new view matrix.
398 * (We consider positions to be column vectors so position vector p is
399 * transformed by matrix X as p' = X*p.)
400 *
401 * @param m the matrix used to modify the view matrix.
402 */
403 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
404
405 /**
406 * Multiplies the current view matrix by a matrix
407 *
408 * After this call V' = m*V where V is the old view matrix,
409 * m is the parameter to this function, and V' is the new view matrix.
410 * (We consider positions to be column vectors so position vector p is
411 * transformed by matrix X as p' = X*p.)
412 *
413 * @param m the matrix used to modify the view matrix.
414 */
415 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
416
417 /**
418 * Retrieves the current view matrix
419 * @return the current view matrix.
420 */
421 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
422
423 /**
424 * Retrieves the inverse of the current view matrix.
425 *
426 * If the current view matrix is invertible, return true, and if matrix
427 * is non-null, copy the inverse into it. If the current view matrix is
428 * non-invertible, return false and ignore the matrix parameter.
429 *
430 * @param matrix if not null, will receive a copy of the current inverse.
431 */
432 bool getViewInverse(GrMatrix* matrix) const {
433 // TODO: determine whether we really need to leave matrix unmodified
434 // at call sites when inversion fails.
435 GrMatrix inverse;
436 if (fViewMatrix.invert(&inverse)) {
437 if (matrix) {
438 *matrix = inverse;
439 }
440 return true;
441 }
442 return false;
443 }
444
445 class AutoViewMatrixRestore : public ::GrNoncopyable {
446 public:
447 AutoViewMatrixRestore() : fDrawState(NULL) {}
448 AutoViewMatrixRestore(GrDrawState* ds, const GrMatrix& newMatrix) {
449 fDrawState = NULL;
450 this->set(ds, newMatrix);
451 }
452 AutoViewMatrixRestore(GrDrawState* ds) {
453 fDrawState = NULL;
454 this->set(ds);
455 }
456 ~AutoViewMatrixRestore() {
457 this->set(NULL, GrMatrix::I());
458 }
459 void set(GrDrawState* ds, const GrMatrix& newMatrix) {
460 if (NULL != fDrawState) {
461 fDrawState->setViewMatrix(fSavedMatrix);
462 }
463 if (NULL != ds) {
464 fSavedMatrix = ds->getViewMatrix();
465 ds->setViewMatrix(newMatrix);
466 }
467 fDrawState = ds;
468 }
469 void set(GrDrawState* ds) {
470 if (NULL != fDrawState) {
471 fDrawState->setViewMatrix(fSavedMatrix);
472 }
473 if (NULL != ds) {
474 fSavedMatrix = ds->getViewMatrix();
475 }
476 fDrawState = ds;
477 }
478 private:
479 GrDrawState* fDrawState;
480 GrMatrix fSavedMatrix;
tomhudson@google.com93813632011-10-27 20:21:16 +0000481 };
482
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000483 /// @}
484
485 ///////////////////////////////////////////////////////////////////////////
486 /// @name Render Target
487 ////
488
489 /**
490 * Sets the rendertarget used at the next drawing call
491 *
492 * @param target The render target to set.
493 */
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000494 void setRenderTarget(GrRenderTarget* target) {
495 GrSafeAssign(fRenderTarget, target);
496 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000497
498 /**
499 * Retrieves the currently set rendertarget.
500 *
501 * @return The currently set render target.
502 */
503 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
504 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
505
506 class AutoRenderTargetRestore : public ::GrNoncopyable {
507 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000508 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000509 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
510 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000511 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000512 this->set(ds, newTarget);
513 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000514 ~AutoRenderTargetRestore() { this->restore(); }
515
516 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000517 if (NULL != fDrawState) {
518 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000519 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000520 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000521 GrSafeSetNull(fSavedTarget);
522 }
523
524 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
525 this->restore();
526
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000527 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000528 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000529 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000530 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000531 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000532 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000534 }
535 private:
536 GrDrawState* fDrawState;
537 GrRenderTarget* fSavedTarget;
538 };
539
540 /// @}
541
542 ///////////////////////////////////////////////////////////////////////////
543 /// @name Stencil
544 ////
545
546 /**
547 * Sets the stencil settings to use for the next draw.
548 * Changing the clip has the side-effect of possibly zeroing
549 * out the client settable stencil bits. So multipass algorithms
550 * using stencil should not change the clip between passes.
551 * @param settings the stencil settings to use.
552 */
553 void setStencil(const GrStencilSettings& settings) {
554 fStencilSettings = settings;
555 }
556
557 /**
558 * Shortcut to disable stencil testing and ops.
559 */
560 void disableStencil() {
561 fStencilSettings.setDisabled();
562 }
563
564 const GrStencilSettings& getStencil() const { return fStencilSettings; }
565
566 GrStencilSettings* stencil() { return &fStencilSettings; }
567
568 /// @}
569
570 ///////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000571 /// @name Color Matrix
572 ////
573
574 /**
575 * Sets the color matrix to use for the next draw.
576 * @param matrix the 5x4 matrix to apply to the incoming color
577 */
578 void setColorMatrix(const float matrix[20]) {
579 memcpy(fColorMatrix, matrix, sizeof(fColorMatrix));
580 }
581
582 const float* getColorMatrix() const { return fColorMatrix; }
583
584 /// @}
585
586 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000587 // @name Edge AA
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000588 // Edge equations can be specified to perform antialiasing. Because the
589 // edges are specified as per-vertex data, vertices that are shared by
590 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000591 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592 ////
593
594 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000595 * When specifying edges as vertex data this enum specifies what type of
596 * edges are in use. The edges are always 4 GrScalars in memory, even when
597 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000598 *
599 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
600 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000601 */
602 enum VertexEdgeType {
603 /* 1-pixel wide line
604 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
605 kHairLine_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000606 /* Quadratic specified by u^2-v canonical coords (only 2
607 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000608 being inside, positive outside. Edge specified in window space
609 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000610 kQuad_EdgeType,
611 /* Same as above but for hairline quadratics. Uses unsigned distance.
612 Coverage is min(0, 1-distance). */
613 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000614 /* Circle specified as center_x, center_y, outer_radius, inner_radius
615 all in window space (y-down). */
616 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000617
618 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000619 };
620
621 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000622 * Determines the interpretation per-vertex edge data when the
623 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
624 * are not specified the value of this setting has no effect.
625 */
626 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000627 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 fVertexEdgeType = type;
629 }
630
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000631 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000632
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000633 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000634
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000635 ///////////////////////////////////////////////////////////////////////////
636 /// @name State Flags
637 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000638
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000639 /**
640 * Flags that affect rendering. Controlled using enable/disableState(). All
641 * default to disabled.
642 */
643 enum StateBits {
644 /**
645 * Perform dithering. TODO: Re-evaluate whether we need this bit
646 */
647 kDither_StateBit = 0x01,
648 /**
649 * Perform HW anti-aliasing. This means either HW FSAA, if supported
650 * by the render target, or smooth-line rendering if a line primitive
651 * is drawn and line smoothing is supported by the 3D API.
652 */
653 kHWAntialias_StateBit = 0x02,
654 /**
655 * Draws will respect the clip, otherwise the clip is ignored.
656 */
657 kClip_StateBit = 0x04,
658 /**
659 * Disables writing to the color buffer. Useful when performing stencil
660 * operations.
661 */
662 kNoColorWrites_StateBit = 0x08,
663 /**
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000664 * Draws will apply the color matrix, otherwise the color matrix is
665 * ignored.
666 */
667 kColorMatrix_StateBit = 0x20,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000668
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000669 // Users of the class may add additional bits to the vector
670 kDummyStateBit,
671 kLastPublicStateBit = kDummyStateBit-1,
672 };
673
674 void resetStateFlags() {
675 fFlagBits = 0;
676 }
677
678 /**
679 * Enable render state settings.
680 *
681 * @param flags bitfield of StateBits specifing the states to enable
682 */
683 void enableState(uint32_t stateBits) {
684 fFlagBits |= stateBits;
685 }
686
687 /**
688 * Disable render state settings.
689 *
690 * @param flags bitfield of StateBits specifing the states to disable
691 */
692 void disableState(uint32_t stateBits) {
693 fFlagBits &= ~(stateBits);
694 }
695
696 bool isDitherState() const {
697 return 0 != (fFlagBits & kDither_StateBit);
698 }
699
700 bool isHWAntialiasState() const {
701 return 0 != (fFlagBits & kHWAntialias_StateBit);
702 }
703
704 bool isClipState() const {
705 return 0 != (fFlagBits & kClip_StateBit);
706 }
707
708 bool isColorWriteDisabled() const {
709 return 0 != (fFlagBits & kNoColorWrites_StateBit);
710 }
711
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000712 bool isStateFlagEnabled(uint32_t stateBit) const {
713 return 0 != (stateBit & fFlagBits);
714 }
715
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000716 /// @}
717
718 ///////////////////////////////////////////////////////////////////////////
719 /// @name Face Culling
720 ////
721
722 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000723 kInvalid_DrawFace = -1,
724
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000725 kBoth_DrawFace,
726 kCCW_DrawFace,
727 kCW_DrawFace,
728 };
729
730 /**
731 * Controls whether clockwise, counterclockwise, or both faces are drawn.
732 * @param face the face(s) to draw.
733 */
734 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000735 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000736 fDrawFace = face;
737 }
738
739 /**
740 * Gets whether the target is drawing clockwise, counterclockwise,
741 * or both faces.
742 * @return the current draw face(s).
743 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000744 DrawFace getDrawFace() const { return fDrawFace; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000745
746 /// @}
747
748 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000749
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000750 // Most stages are usually not used, so conditionals here
751 // reduce the expected number of bytes touched by 50%.
752 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000753 if (memcmp(this->podStart(), s.podStart(), this->podSize())) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000754 return false;
755 }
756
757 if (!s.fViewMatrix.cheapEqualTo(fViewMatrix)) {
758 return false;
759 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000760
761 for (int i = 0; i < kNumStages; i++) {
762 if (fTextures[i] &&
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000763 this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000764 return false;
765 }
766 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000767 if (kColorMatrix_StateBit & s.fFlagBits) {
768 if (memcmp(fColorMatrix,
769 s.fColorMatrix,
770 sizeof(fColorMatrix))) {
771 return false;
772 }
773 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000774
775 return true;
776 }
777 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
778
779 // Most stages are usually not used, so conditionals here
780 // reduce the expected number of bytes touched by 50%.
781 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000782 memcpy(this->podStart(), s.podStart(), this->podSize());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000783
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000784 fViewMatrix = s.fViewMatrix;
785
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000786 for (int i = 0; i < kNumStages; i++) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000787 SkSafeRef(fTextures[i]); // already copied by memcpy
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000788 if (s.fTextures[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 }
791 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000792
793 SkSafeRef(fRenderTarget); // already copied by memcpy
794
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000795 if (kColorMatrix_StateBit & s.fFlagBits) {
796 memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix));
797 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000798
799 return *this;
800 }
801
802private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000803
804 const void* podStart() const {
805 return reinterpret_cast<const void*>(&fPodStartMarker);
806 }
807 void* podStart() {
808 return reinterpret_cast<void*>(&fPodStartMarker);
809 }
810 size_t memsetSize() const {
811 return reinterpret_cast<size_t>(&fMemsetEndMarker) -
812 reinterpret_cast<size_t>(&fPodStartMarker) +
813 sizeof(fMemsetEndMarker);
814 }
815 size_t podSize() const {
816 // Can't use offsetof() with non-POD types, so stuck with pointer math.
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000817 return reinterpret_cast<size_t>(&fPodEndMarker) -
818 reinterpret_cast<size_t>(&fPodStartMarker) +
819 sizeof(fPodEndMarker);
820 }
821
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000822 static const StageMask kIllegalStageMaskBits = ~((1U << kNumStages)-1);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000823 // @{ these fields can be initialized with memset to 0
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000824 union {
825 GrColor fBlendConstant;
826 GrColor fPodStartMarker;
827 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000828 GrColor fColorFilterColor;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000829 DrawFace fDrawFace;
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000830 VertexEdgeType fVertexEdgeType;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000831 GrStencilSettings fStencilSettings;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000832 union {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000833 uint32_t fFlagBits;
834 uint32_t fMemsetEndMarker;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000835 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000836 // @}
837
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000838 int fFirstCoverageStage;
839
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000840 // @{ Initialized to values other than zero, but memcmp'ed in operator==
841 // and memcpy'ed in operator=.
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000842 GrTexture* fTextures[kNumStages];
843 GrRenderTarget* fRenderTarget;
844
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000845 GrColor fColor;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000846 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000847 SkXfermode::Mode fColorFilterMode;
848 GrBlendCoeff fSrcBlend;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000849 union {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000850 GrBlendCoeff fDstBlend;
851 GrBlendCoeff fPodEndMarker;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000852 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000853 // @}
854
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000855 GrMatrix fViewMatrix;
856
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000857 // This field must be last; it will not be copied or compared
858 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000859 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000860 // only compared if the color matrix enable flag is set
861 float fColorMatrix[20]; // 5 x 4 matrix
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000862
tomhudson@google.com93813632011-10-27 20:21:16 +0000863};
864
865#endif