blob: 12c88611048f10f9e76e42b3451b8e77515ec7cb [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"
17
18#include "SkXfermode.h"
19
20class GrRenderTarget;
21class GrTexture;
22
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.
39 */
40 enum {
41 kNumStages = 3,
42 kMaxTexCoords = kNumStages
43 };
44
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000045 /**
46 * Bitfield used to indicate a set of stages.
47 */
48 typedef uint32_t StageMask;
49 GR_STATIC_ASSERT(sizeof(StageMask)*8 >= GrDrawState::kNumStages);
50
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000051 GrDrawState() {
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000052 this->reset();
53 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000054
55 GrDrawState(const GrDrawState& state) {
56 *this = state;
57 }
58
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000059 /**
60 * Resets to the default state. Sampler states will not be modified.
61 */
62 void reset() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000063 // make sure any pad is zero for memcmp
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000064 // all GrDrawState members should default to something valid by the
65 // the memset except those initialized individually below. There should
66 // be no padding between the individually initialized members.
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000067 memset(this->podStart(), 0, this->memsetSize());
68
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000069 // pedantic assertion that our ptrs will
70 // be NULL (0 ptr is mem addr 0)
71 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000072 GR_STATIC_ASSERT(0 == kBoth_DrawFace);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000073 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000074
75 // memset exceptions
76 fColor = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +000077 fCoverage = 0xffffffff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000078 fFirstCoverageStage = kNumStages;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000079 fColorFilterMode = SkXfermode::kDst_Mode;
80 fSrcBlend = kOne_BlendCoeff;
81 fDstBlend = kZero_BlendCoeff;
82 fViewMatrix.reset();
83
84 // ensure values that will be memcmp'ed in == but not memset in reset()
85 // are tightly packed
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000086 GrAssert(this->memsetSize() + sizeof(fColor) + sizeof(fCoverage) +
bsalomon@google.com2401ae82012-01-17 21:03:05 +000087 sizeof(fFirstCoverageStage) + sizeof(fColorFilterMode) +
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000088 sizeof(fSrcBlend) + sizeof(fDstBlend) ==
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000089 this->podSize());
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000090
91 fEdgeAANumEdges = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000092 }
93
94 ///////////////////////////////////////////////////////////////////////////
95 /// @name Color
96 ////
97
98 /**
99 * Sets color for next draw to a premultiplied-alpha color.
100 *
101 * @param color the color to set.
102 */
103 void setColor(GrColor color) { fColor = color; }
104
105 GrColor getColor() const { return fColor; }
106
107 /**
108 * Sets the color to be used for the next draw to be
109 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
110 *
111 * @param alpha The alpha value to set as the color.
112 */
113 void setAlpha(uint8_t a) {
114 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
115 }
116
117 /**
118 * Add a color filter that can be represented by a color and a mode. Applied
119 * after color-computing texture stages.
120 */
121 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
122 fColorFilterColor = c;
123 fColorFilterMode = mode;
124 }
125
126 GrColor getColorFilterColor() const { return fColorFilterColor; }
127 SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; }
128
129 /// @}
130
131 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000132 /// @name Coverage
133 ////
134
135 /**
136 * Sets a constant fractional coverage to be applied to the draw. The
137 * initial value (after construction or reset()) is 0xff. The constant
138 * coverage is ignored when per-vertex coverage is provided.
139 */
140 void setCoverage(uint8_t coverage) {
141 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
142 }
143
144 /**
145 * Version of above that specifies 4 channel per-vertex color. The value
146 * should be premultiplied.
147 */
148 void setCoverage4(GrColor coverage) {
149 fCoverage = coverage;
150 }
151
152 GrColor getCoverage() const {
153 return fCoverage;
154 }
155
156 /// @}
157
158 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000159 /// @name Textures
160 ////
161
162 /**
163 * Sets the texture used at the next drawing call
164 *
165 * @param stage The texture stage for which the texture will be set
166 *
167 * @param texture The texture to set. Can be NULL though there is no
168 * advantage to settings a NULL texture if doing non-textured drawing
169 */
170 void setTexture(int stage, GrTexture* texture) {
171 GrAssert((unsigned)stage < kNumStages);
172 fTextures[stage] = texture;
173 }
174
175 /**
176 * Retrieves the currently set texture.
177 *
178 * @return The currently set texture. The return value will be NULL if no
179 * texture has been set, NULL was most recently passed to
180 * setTexture, or the last setTexture was destroyed.
181 */
182 const GrTexture* getTexture(int stage) const {
183 GrAssert((unsigned)stage < kNumStages);
184 return fTextures[stage];
185 }
186 GrTexture* getTexture(int stage) {
187 GrAssert((unsigned)stage < kNumStages);
188 return fTextures[stage];
189 }
190
191 /// @}
192
193 ///////////////////////////////////////////////////////////////////////////
194 /// @name Samplers
195 ////
196
197 /**
198 * Returns the current sampler for a stage.
199 */
200 const GrSamplerState& getSampler(int stage) const {
201 GrAssert((unsigned)stage < kNumStages);
202 return fSamplerStates[stage];
203 }
204
205 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000206 * Writable pointer to a stage's sampler.
207 */
208 GrSamplerState* sampler(int stage) {
209 GrAssert((unsigned)stage < kNumStages);
210 return fSamplerStates + stage;
211 }
212
213 /**
214 * Preconcats the matrix of all samplers in the mask with the same matrix.
215 */
216 void preConcatSamplerMatrices(StageMask stageMask, const GrMatrix& matrix) {
217 GrAssert(!(stageMask & kIllegalStageMaskBits));
218 for (int i = 0; i < kNumStages; ++i) {
219 if ((1 << i) & stageMask) {
220 fSamplerStates[i].preConcatMatrix(matrix);
221 }
222 }
223 }
224
225 /// @}
226
227 ///////////////////////////////////////////////////////////////////////////
228 /// @name Coverage / Color Stages
229 ////
230
231 /**
232 * A common pattern is to compute a color with the initial stages and then
233 * modulate that color by a coverage value in later stage(s) (AA, mask-
234 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
235 * computed based on the pre-coverage-modulated color. The division of
236 * stages between color-computing and coverage-computing is specified by
237 * this method. Initially this is kNumStages (all stages
238 * are color-computing).
239 */
240 void setFirstCoverageStage(int firstCoverageStage) {
241 GrAssert((unsigned)firstCoverageStage <= kNumStages);
242 fFirstCoverageStage = firstCoverageStage;
243 }
244
245 /**
246 * Gets the index of the first coverage-computing stage.
247 */
248 int getFirstCoverageStage() const {
249 return fFirstCoverageStage;
250 }
251
252 ///@}
253
254 ///////////////////////////////////////////////////////////////////////////
255 /// @name Blending
256 ////
257
258 /**
259 * Sets the blending function coeffecients.
260 *
261 * The blend function will be:
262 * D' = sat(S*srcCoef + D*dstCoef)
263 *
264 * where D is the existing destination color, S is the incoming source
265 * color, and D' is the new destination color that will be written. sat()
266 * is the saturation function.
267 *
268 * @param srcCoef coeffecient applied to the src color.
269 * @param dstCoef coeffecient applied to the dst color.
270 */
271 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
272 fSrcBlend = srcCoeff;
273 fDstBlend = dstCoeff;
274 #if GR_DEBUG
275 switch (dstCoeff) {
276 case kDC_BlendCoeff:
277 case kIDC_BlendCoeff:
278 case kDA_BlendCoeff:
279 case kIDA_BlendCoeff:
280 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
281 "coverage stages.\n");
282 break;
283 default:
284 break;
285 }
286 switch (srcCoeff) {
287 case kSC_BlendCoeff:
288 case kISC_BlendCoeff:
289 case kSA_BlendCoeff:
290 case kISA_BlendCoeff:
291 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
292 "coverage stages.\n");
293 break;
294 default:
295 break;
296 }
297 #endif
298 }
299
300 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
301 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
302
303 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
304 GrBlendCoeff* dstBlendCoeff) const {
305 *srcBlendCoeff = fSrcBlend;
306 *dstBlendCoeff = fDstBlend;
307 }
308
309 /**
310 * Sets the blending function constant referenced by the following blending
311 * coeffecients:
312 * kConstC_BlendCoeff
313 * kIConstC_BlendCoeff
314 * kConstA_BlendCoeff
315 * kIConstA_BlendCoeff
316 *
317 * @param constant the constant to set
318 */
319 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
320
321 /**
322 * Retrieves the last value set by setBlendConstant()
323 * @return the blending constant value
324 */
325 GrColor getBlendConstant() const { return fBlendConstant; }
326
327 /// @}
328
329 ///////////////////////////////////////////////////////////////////////////
330 /// @name View Matrix
331 ////
332
333 /**
334 * Sets the matrix applied to veretx positions.
335 *
336 * In the post-view-matrix space the rectangle [0,w]x[0,h]
337 * fully covers the render target. (w and h are the width and height of the
338 * the rendertarget.)
339 */
340 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
341
342 /**
343 * Gets a writable pointer to the view matrix.
344 */
345 GrMatrix* viewMatrix() { return &fViewMatrix; }
346
347 /**
348 * Multiplies the current view matrix by a matrix
349 *
350 * After this call V' = V*m where V is the old view matrix,
351 * m is the parameter to this function, and V' is the new view matrix.
352 * (We consider positions to be column vectors so position vector p is
353 * transformed by matrix X as p' = X*p.)
354 *
355 * @param m the matrix used to modify the view matrix.
356 */
357 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
358
359 /**
360 * Multiplies the current view matrix by a matrix
361 *
362 * After this call V' = m*V where V is the old view matrix,
363 * m is the parameter to this function, and V' is the new view matrix.
364 * (We consider positions to be column vectors so position vector p is
365 * transformed by matrix X as p' = X*p.)
366 *
367 * @param m the matrix used to modify the view matrix.
368 */
369 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
370
371 /**
372 * Retrieves the current view matrix
373 * @return the current view matrix.
374 */
375 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
376
377 /**
378 * Retrieves the inverse of the current view matrix.
379 *
380 * If the current view matrix is invertible, return true, and if matrix
381 * is non-null, copy the inverse into it. If the current view matrix is
382 * non-invertible, return false and ignore the matrix parameter.
383 *
384 * @param matrix if not null, will receive a copy of the current inverse.
385 */
386 bool getViewInverse(GrMatrix* matrix) const {
387 // TODO: determine whether we really need to leave matrix unmodified
388 // at call sites when inversion fails.
389 GrMatrix inverse;
390 if (fViewMatrix.invert(&inverse)) {
391 if (matrix) {
392 *matrix = inverse;
393 }
394 return true;
395 }
396 return false;
397 }
398
399 class AutoViewMatrixRestore : public ::GrNoncopyable {
400 public:
401 AutoViewMatrixRestore() : fDrawState(NULL) {}
402 AutoViewMatrixRestore(GrDrawState* ds, const GrMatrix& newMatrix) {
403 fDrawState = NULL;
404 this->set(ds, newMatrix);
405 }
406 AutoViewMatrixRestore(GrDrawState* ds) {
407 fDrawState = NULL;
408 this->set(ds);
409 }
410 ~AutoViewMatrixRestore() {
411 this->set(NULL, GrMatrix::I());
412 }
413 void set(GrDrawState* ds, const GrMatrix& newMatrix) {
414 if (NULL != fDrawState) {
415 fDrawState->setViewMatrix(fSavedMatrix);
416 }
417 if (NULL != ds) {
418 fSavedMatrix = ds->getViewMatrix();
419 ds->setViewMatrix(newMatrix);
420 }
421 fDrawState = ds;
422 }
423 void set(GrDrawState* ds) {
424 if (NULL != fDrawState) {
425 fDrawState->setViewMatrix(fSavedMatrix);
426 }
427 if (NULL != ds) {
428 fSavedMatrix = ds->getViewMatrix();
429 }
430 fDrawState = ds;
431 }
432 private:
433 GrDrawState* fDrawState;
434 GrMatrix fSavedMatrix;
tomhudson@google.com93813632011-10-27 20:21:16 +0000435 };
436
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437 /// @}
438
439 ///////////////////////////////////////////////////////////////////////////
440 /// @name Render Target
441 ////
442
443 /**
444 * Sets the rendertarget used at the next drawing call
445 *
446 * @param target The render target to set.
447 */
448 void setRenderTarget(GrRenderTarget* target) { fRenderTarget = target; }
449
450 /**
451 * Retrieves the currently set rendertarget.
452 *
453 * @return The currently set render target.
454 */
455 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
456 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
457
458 class AutoRenderTargetRestore : public ::GrNoncopyable {
459 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000460 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000461 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
462 fDrawState = NULL;
463 this->set(ds, newTarget);
464 }
465 ~AutoRenderTargetRestore() { this->set(NULL, NULL); }
466 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
467 if (NULL != fDrawState) {
468 fDrawState->setRenderTarget(fSavedTarget);
469 }
470 if (NULL != ds) {
471 fSavedTarget = ds->getRenderTarget();
472 ds->setRenderTarget(newTarget);
473 }
474 fDrawState = ds;
475 }
476 private:
477 GrDrawState* fDrawState;
478 GrRenderTarget* fSavedTarget;
479 };
480
481 /// @}
482
483 ///////////////////////////////////////////////////////////////////////////
484 /// @name Stencil
485 ////
486
487 /**
488 * Sets the stencil settings to use for the next draw.
489 * Changing the clip has the side-effect of possibly zeroing
490 * out the client settable stencil bits. So multipass algorithms
491 * using stencil should not change the clip between passes.
492 * @param settings the stencil settings to use.
493 */
494 void setStencil(const GrStencilSettings& settings) {
495 fStencilSettings = settings;
496 }
497
498 /**
499 * Shortcut to disable stencil testing and ops.
500 */
501 void disableStencil() {
502 fStencilSettings.setDisabled();
503 }
504
505 const GrStencilSettings& getStencil() const { return fStencilSettings; }
506
507 GrStencilSettings* stencil() { return &fStencilSettings; }
508
509 /// @}
510
511 ///////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000512 /// @name Color Matrix
513 ////
514
515 /**
516 * Sets the color matrix to use for the next draw.
517 * @param matrix the 5x4 matrix to apply to the incoming color
518 */
519 void setColorMatrix(const float matrix[20]) {
520 memcpy(fColorMatrix, matrix, sizeof(fColorMatrix));
521 }
522
523 const float* getColorMatrix() const { return fColorMatrix; }
524
525 /// @}
526
527 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000528 // @name Edge AA
529 // There are two ways to perform antialiasing using edge equations. One
530 // is to specify an (linear or quadratic) edge eq per-vertex. This requires
531 // splitting vertices shared by primitives.
532 //
533 // The other is via setEdgeAAData which sets a set of edges and each
534 // is tested against all the edges.
535 ////
536
537 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000538 * When specifying edges as vertex data this enum specifies what type of
539 * edges are in use. The edges are always 4 GrScalars in memory, even when
540 * the edge type requires fewer than 4.
541 */
542 enum VertexEdgeType {
543 /* 1-pixel wide line
544 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
545 kHairLine_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000546 /* Quadratic specified by u^2-v canonical coords (only 2
547 components used). Coverage based on signed distance with negative
548 being inside, positive outside.*/
549 kQuad_EdgeType,
550 /* Same as above but for hairline quadratics. Uses unsigned distance.
551 Coverage is min(0, 1-distance). */
552 kHairQuad_EdgeType,
553
554 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000555 };
556
557 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000558 * Determines the interpretation per-vertex edge data when the
559 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
560 * are not specified the value of this setting has no effect.
561 */
562 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000563 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000564 fVertexEdgeType = type;
565 }
566
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000567 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000568
569 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000570 * The absolute maximum number of edges that may be specified for
571 * a single draw call when performing edge antialiasing. This is used for
572 * the size of several static buffers, so implementations of getMaxEdges()
573 * (below) should clamp to this value.
574 */
575 enum {
tomhudson@google.com62b09682011-11-09 16:39:17 +0000576 // TODO: this should be 32 when GrTesselatedPathRenderer is used
577 // Visual Studio 2010 does not permit a member array of size 0.
578 kMaxEdges = 1
tomhudson@google.com93813632011-10-27 20:21:16 +0000579 };
580
581 class Edge {
582 public:
583 Edge() {}
584 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
585 GrPoint intersect(const Edge& other) {
586 return GrPoint::Make(
bsalomon@google.com72e49b82011-10-27 21:47:03 +0000587 SkFloatToScalar((fY * other.fZ - other.fY * fZ) /
588 (fX * other.fY - other.fX * fY)),
589 SkFloatToScalar((fX * other.fZ - other.fX * fZ) /
590 (other.fX * fY - fX * other.fY)));
tomhudson@google.com93813632011-10-27 20:21:16 +0000591 }
592 float fX, fY, fZ;
593 };
594
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595 /**
596 * Sets the edge data required for edge antialiasing.
597 *
598 * @param edges 3 * numEdges float values, representing the edge
599 * equations in Ax + By + C form
600 */
601 void setEdgeAAData(const Edge* edges, int numEdges) {
602 GrAssert(numEdges <= GrDrawState::kMaxEdges);
603 memcpy(fEdgeAAEdges, edges, numEdges * sizeof(GrDrawState::Edge));
604 fEdgeAANumEdges = numEdges;
tomhudson@google.com93813632011-10-27 20:21:16 +0000605 }
606
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000607 int getNumAAEdges() const { return fEdgeAANumEdges; }
tomhudson@google.com93813632011-10-27 20:21:16 +0000608
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000609 const Edge* getAAEdges() const { return fEdgeAAEdges; }
tomhudson@google.com62b09682011-11-09 16:39:17 +0000610
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000611 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000612
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000613 ///////////////////////////////////////////////////////////////////////////
614 /// @name State Flags
615 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000616
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000617 /**
618 * Flags that affect rendering. Controlled using enable/disableState(). All
619 * default to disabled.
620 */
621 enum StateBits {
622 /**
623 * Perform dithering. TODO: Re-evaluate whether we need this bit
624 */
625 kDither_StateBit = 0x01,
626 /**
627 * Perform HW anti-aliasing. This means either HW FSAA, if supported
628 * by the render target, or smooth-line rendering if a line primitive
629 * is drawn and line smoothing is supported by the 3D API.
630 */
631 kHWAntialias_StateBit = 0x02,
632 /**
633 * Draws will respect the clip, otherwise the clip is ignored.
634 */
635 kClip_StateBit = 0x04,
636 /**
637 * Disables writing to the color buffer. Useful when performing stencil
638 * operations.
639 */
640 kNoColorWrites_StateBit = 0x08,
641 /**
642 * Modifies the behavior of edge AA specified by setEdgeAA. If set,
643 * will test edge pairs for convexity when rasterizing. Set this if the
644 * source polygon is non-convex.
645 */
646 kEdgeAAConcave_StateBit = 0x10,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000647 /**
648 * Draws will apply the color matrix, otherwise the color matrix is
649 * ignored.
650 */
651 kColorMatrix_StateBit = 0x20,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000652
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000653 // Users of the class may add additional bits to the vector
654 kDummyStateBit,
655 kLastPublicStateBit = kDummyStateBit-1,
656 };
657
658 void resetStateFlags() {
659 fFlagBits = 0;
660 }
661
662 /**
663 * Enable render state settings.
664 *
665 * @param flags bitfield of StateBits specifing the states to enable
666 */
667 void enableState(uint32_t stateBits) {
668 fFlagBits |= stateBits;
669 }
670
671 /**
672 * Disable render state settings.
673 *
674 * @param flags bitfield of StateBits specifing the states to disable
675 */
676 void disableState(uint32_t stateBits) {
677 fFlagBits &= ~(stateBits);
678 }
679
680 bool isDitherState() const {
681 return 0 != (fFlagBits & kDither_StateBit);
682 }
683
684 bool isHWAntialiasState() const {
685 return 0 != (fFlagBits & kHWAntialias_StateBit);
686 }
687
688 bool isClipState() const {
689 return 0 != (fFlagBits & kClip_StateBit);
690 }
691
692 bool isColorWriteDisabled() const {
693 return 0 != (fFlagBits & kNoColorWrites_StateBit);
694 }
695
696 bool isConcaveEdgeAAState() const {
697 return 0 != (fFlagBits & kEdgeAAConcave_StateBit);
698 }
699
700 bool isStateFlagEnabled(uint32_t stateBit) const {
701 return 0 != (stateBit & fFlagBits);
702 }
703
704 void copyStateFlags(const GrDrawState& ds) {
705 fFlagBits = ds.fFlagBits;
706 }
707
708 /// @}
709
710 ///////////////////////////////////////////////////////////////////////////
711 /// @name Face Culling
712 ////
713
714 enum DrawFace {
715 kBoth_DrawFace,
716 kCCW_DrawFace,
717 kCW_DrawFace,
718 };
719
720 /**
721 * Controls whether clockwise, counterclockwise, or both faces are drawn.
722 * @param face the face(s) to draw.
723 */
724 void setDrawFace(DrawFace face) {
725 fDrawFace = face;
726 }
727
728 /**
729 * Gets whether the target is drawing clockwise, counterclockwise,
730 * or both faces.
731 * @return the current draw face(s).
732 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000733 DrawFace getDrawFace() const { return fDrawFace; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000734
735 /// @}
736
737 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000738
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000739 // Most stages are usually not used, so conditionals here
740 // reduce the expected number of bytes touched by 50%.
741 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000742 if (memcmp(this->podStart(), s.podStart(), this->podSize())) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000743 return false;
744 }
745
746 if (!s.fViewMatrix.cheapEqualTo(fViewMatrix)) {
747 return false;
748 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000749
750 for (int i = 0; i < kNumStages; i++) {
751 if (fTextures[i] &&
752 memcmp(&this->fSamplerStates[i], &s.fSamplerStates[i],
753 sizeof(GrSamplerState))) {
754 return false;
755 }
756 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000757 if (kColorMatrix_StateBit & s.fFlagBits) {
758 if (memcmp(fColorMatrix,
759 s.fColorMatrix,
760 sizeof(fColorMatrix))) {
761 return false;
762 }
763 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000764
765 return true;
766 }
767 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
768
769 // Most stages are usually not used, so conditionals here
770 // reduce the expected number of bytes touched by 50%.
771 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000772 memcpy(this->podStart(), s.podStart(), this->podSize());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000773
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000774 fViewMatrix = s.fViewMatrix;
775
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000776 GrAssert(0 == s.fEdgeAANumEdges);
777 fEdgeAANumEdges = 0;
778
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000779 for (int i = 0; i < kNumStages; i++) {
780 if (s.fTextures[i]) {
781 memcpy(&this->fSamplerStates[i], &s.fSamplerStates[i],
782 sizeof(GrSamplerState));
783 }
784 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000785 if (kColorMatrix_StateBit & s.fFlagBits) {
786 memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix));
787 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000788
789 return *this;
790 }
791
792private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000793
794 const void* podStart() const {
795 return reinterpret_cast<const void*>(&fPodStartMarker);
796 }
797 void* podStart() {
798 return reinterpret_cast<void*>(&fPodStartMarker);
799 }
800 size_t memsetSize() const {
801 return reinterpret_cast<size_t>(&fMemsetEndMarker) -
802 reinterpret_cast<size_t>(&fPodStartMarker) +
803 sizeof(fMemsetEndMarker);
804 }
805 size_t podSize() const {
806 // Can't use offsetof() with non-POD types, so stuck with pointer math.
807 // TODO: ignores GrTesselatedPathRenderer data structures. We don't
808 // have a compile-time flag that lets us know if it's being used, and
809 // checking at runtime seems to cost 5% performance.
810 return reinterpret_cast<size_t>(&fPodEndMarker) -
811 reinterpret_cast<size_t>(&fPodStartMarker) +
812 sizeof(fPodEndMarker);
813 }
814
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000815 static const StageMask kIllegalStageMaskBits = ~((1 << kNumStages)-1);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000816 // @{ these fields can be initialized with memset to 0
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000817 union {
818 GrColor fBlendConstant;
819 GrColor fPodStartMarker;
820 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000821 GrTexture* fTextures[kNumStages];
822 GrColor fColorFilterColor;
823 uint32_t fFlagBits;
824 DrawFace fDrawFace;
825 VertexEdgeType fVertexEdgeType;
826 GrStencilSettings fStencilSettings;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000827 union {
828 GrRenderTarget* fRenderTarget;
829 GrRenderTarget* fMemsetEndMarker;
830 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000831 // @}
832
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000833 // @{ Initialized to values other than zero, but memcmp'ed in operator==
834 // and memcpy'ed in operator=.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000835 GrColor fColor;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000836 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000837 int fFirstCoverageStage;
838 SkXfermode::Mode fColorFilterMode;
839 GrBlendCoeff fSrcBlend;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000840 union {
841 GrBlendCoeff fDstBlend;
842 GrBlendCoeff fPodEndMarker;
843 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000844 // @}
845
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000846 GrMatrix fViewMatrix;
847
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000848 // @{ Data for GrTesselatedPathRenderer
849 // TODO: currently ignored in copying & comparison for performance.
850 // Must be considered if GrTesselatedPathRenderer is being used.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000851 int fEdgeAANumEdges;
852 Edge fEdgeAAEdges[kMaxEdges];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000853 // @}
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000854
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 // This field must be last; it will not be copied or compared
856 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000857 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000858 // only compared if the color matrix enable flag is set
859 float fColorMatrix[20]; // 5 x 4 matrix
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860
tomhudson@google.com93813632011-10-27 20:21:16 +0000861};
862
863#endif