blob: fee22bfaf42cbcdc1af94a4785612c63a92858c8 [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 {
robertphillips@google.comec05eaa2012-04-27 18:59:52 +000041 kNumStages = 4,
tomhudson@google.com93813632011-10-27 20:21:16 +000042 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 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000334 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000335 *
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;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000463 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000464 this->set(ds, newTarget);
465 }
466 ~AutoRenderTargetRestore() { this->set(NULL, NULL); }
467 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
468 if (NULL != fDrawState) {
469 fDrawState->setRenderTarget(fSavedTarget);
470 }
471 if (NULL != ds) {
472 fSavedTarget = ds->getRenderTarget();
473 ds->setRenderTarget(newTarget);
474 }
475 fDrawState = ds;
476 }
477 private:
478 GrDrawState* fDrawState;
479 GrRenderTarget* fSavedTarget;
480 };
481
482 /// @}
483
484 ///////////////////////////////////////////////////////////////////////////
485 /// @name Stencil
486 ////
487
488 /**
489 * Sets the stencil settings to use for the next draw.
490 * Changing the clip has the side-effect of possibly zeroing
491 * out the client settable stencil bits. So multipass algorithms
492 * using stencil should not change the clip between passes.
493 * @param settings the stencil settings to use.
494 */
495 void setStencil(const GrStencilSettings& settings) {
496 fStencilSettings = settings;
497 }
498
499 /**
500 * Shortcut to disable stencil testing and ops.
501 */
502 void disableStencil() {
503 fStencilSettings.setDisabled();
504 }
505
506 const GrStencilSettings& getStencil() const { return fStencilSettings; }
507
508 GrStencilSettings* stencil() { return &fStencilSettings; }
509
510 /// @}
511
512 ///////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000513 /// @name Color Matrix
514 ////
515
516 /**
517 * Sets the color matrix to use for the next draw.
518 * @param matrix the 5x4 matrix to apply to the incoming color
519 */
520 void setColorMatrix(const float matrix[20]) {
521 memcpy(fColorMatrix, matrix, sizeof(fColorMatrix));
522 }
523
524 const float* getColorMatrix() const { return fColorMatrix; }
525
526 /// @}
527
528 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000529 // @name Edge AA
530 // There are two ways to perform antialiasing using edge equations. One
531 // is to specify an (linear or quadratic) edge eq per-vertex. This requires
532 // splitting vertices shared by primitives.
533 //
534 // The other is via setEdgeAAData which sets a set of edges and each
535 // is tested against all the edges.
536 ////
537
538 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000539 * When specifying edges as vertex data this enum specifies what type of
540 * edges are in use. The edges are always 4 GrScalars in memory, even when
541 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000542 *
543 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
544 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000545 */
546 enum VertexEdgeType {
547 /* 1-pixel wide line
548 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
549 kHairLine_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000550 /* Quadratic specified by u^2-v canonical coords (only 2
551 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000552 being inside, positive outside. Edge specified in window space
553 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000554 kQuad_EdgeType,
555 /* Same as above but for hairline quadratics. Uses unsigned distance.
556 Coverage is min(0, 1-distance). */
557 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000558 /* Circle specified as center_x, center_y, outer_radius, inner_radius
559 all in window space (y-down). */
560 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000561
562 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000563 };
564
565 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000566 * Determines the interpretation per-vertex edge data when the
567 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
568 * are not specified the value of this setting has no effect.
569 */
570 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000571 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000572 fVertexEdgeType = type;
573 }
574
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000575 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000576
577 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000578 * The absolute maximum number of edges that may be specified for
579 * a single draw call when performing edge antialiasing. This is used for
580 * the size of several static buffers, so implementations of getMaxEdges()
581 * (below) should clamp to this value.
582 */
583 enum {
tomhudson@google.com62b09682011-11-09 16:39:17 +0000584 // TODO: this should be 32 when GrTesselatedPathRenderer is used
585 // Visual Studio 2010 does not permit a member array of size 0.
586 kMaxEdges = 1
tomhudson@google.com93813632011-10-27 20:21:16 +0000587 };
588
589 class Edge {
590 public:
591 Edge() {}
592 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
593 GrPoint intersect(const Edge& other) {
594 return GrPoint::Make(
bsalomon@google.com72e49b82011-10-27 21:47:03 +0000595 SkFloatToScalar((fY * other.fZ - other.fY * fZ) /
596 (fX * other.fY - other.fX * fY)),
597 SkFloatToScalar((fX * other.fZ - other.fX * fZ) /
598 (other.fX * fY - fX * other.fY)));
tomhudson@google.com93813632011-10-27 20:21:16 +0000599 }
600 float fX, fY, fZ;
601 };
602
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 /**
604 * Sets the edge data required for edge antialiasing.
605 *
606 * @param edges 3 * numEdges float values, representing the edge
607 * equations in Ax + By + C form
608 */
609 void setEdgeAAData(const Edge* edges, int numEdges) {
610 GrAssert(numEdges <= GrDrawState::kMaxEdges);
611 memcpy(fEdgeAAEdges, edges, numEdges * sizeof(GrDrawState::Edge));
612 fEdgeAANumEdges = numEdges;
tomhudson@google.com93813632011-10-27 20:21:16 +0000613 }
614
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000615 int getNumAAEdges() const { return fEdgeAANumEdges; }
tomhudson@google.com93813632011-10-27 20:21:16 +0000616
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000617 const Edge* getAAEdges() const { return fEdgeAAEdges; }
tomhudson@google.com62b09682011-11-09 16:39:17 +0000618
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000619 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000620
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000621 ///////////////////////////////////////////////////////////////////////////
622 /// @name State Flags
623 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000624
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000625 /**
626 * Flags that affect rendering. Controlled using enable/disableState(). All
627 * default to disabled.
628 */
629 enum StateBits {
630 /**
631 * Perform dithering. TODO: Re-evaluate whether we need this bit
632 */
633 kDither_StateBit = 0x01,
634 /**
635 * Perform HW anti-aliasing. This means either HW FSAA, if supported
636 * by the render target, or smooth-line rendering if a line primitive
637 * is drawn and line smoothing is supported by the 3D API.
638 */
639 kHWAntialias_StateBit = 0x02,
640 /**
641 * Draws will respect the clip, otherwise the clip is ignored.
642 */
643 kClip_StateBit = 0x04,
644 /**
645 * Disables writing to the color buffer. Useful when performing stencil
646 * operations.
647 */
648 kNoColorWrites_StateBit = 0x08,
649 /**
650 * Modifies the behavior of edge AA specified by setEdgeAA. If set,
651 * will test edge pairs for convexity when rasterizing. Set this if the
652 * source polygon is non-convex.
653 */
654 kEdgeAAConcave_StateBit = 0x10,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000655 /**
656 * Draws will apply the color matrix, otherwise the color matrix is
657 * ignored.
658 */
659 kColorMatrix_StateBit = 0x20,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000660
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000661 // Users of the class may add additional bits to the vector
662 kDummyStateBit,
663 kLastPublicStateBit = kDummyStateBit-1,
664 };
665
666 void resetStateFlags() {
667 fFlagBits = 0;
668 }
669
670 /**
671 * Enable render state settings.
672 *
673 * @param flags bitfield of StateBits specifing the states to enable
674 */
675 void enableState(uint32_t stateBits) {
676 fFlagBits |= stateBits;
677 }
678
679 /**
680 * Disable render state settings.
681 *
682 * @param flags bitfield of StateBits specifing the states to disable
683 */
684 void disableState(uint32_t stateBits) {
685 fFlagBits &= ~(stateBits);
686 }
687
688 bool isDitherState() const {
689 return 0 != (fFlagBits & kDither_StateBit);
690 }
691
692 bool isHWAntialiasState() const {
693 return 0 != (fFlagBits & kHWAntialias_StateBit);
694 }
695
696 bool isClipState() const {
697 return 0 != (fFlagBits & kClip_StateBit);
698 }
699
700 bool isColorWriteDisabled() const {
701 return 0 != (fFlagBits & kNoColorWrites_StateBit);
702 }
703
704 bool isConcaveEdgeAAState() const {
705 return 0 != (fFlagBits & kEdgeAAConcave_StateBit);
706 }
707
708 bool isStateFlagEnabled(uint32_t stateBit) const {
709 return 0 != (stateBit & fFlagBits);
710 }
711
712 void copyStateFlags(const GrDrawState& ds) {
713 fFlagBits = ds.fFlagBits;
714 }
715
716 /// @}
717
718 ///////////////////////////////////////////////////////////////////////////
719 /// @name Face Culling
720 ////
721
722 enum DrawFace {
723 kBoth_DrawFace,
724 kCCW_DrawFace,
725 kCW_DrawFace,
726 };
727
728 /**
729 * Controls whether clockwise, counterclockwise, or both faces are drawn.
730 * @param face the face(s) to draw.
731 */
732 void setDrawFace(DrawFace face) {
733 fDrawFace = face;
734 }
735
736 /**
737 * Gets whether the target is drawing clockwise, counterclockwise,
738 * or both faces.
739 * @return the current draw face(s).
740 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000741 DrawFace getDrawFace() const { return fDrawFace; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742
743 /// @}
744
745 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000746
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000747 // Most stages are usually not used, so conditionals here
748 // reduce the expected number of bytes touched by 50%.
749 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000750 if (memcmp(this->podStart(), s.podStart(), this->podSize())) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000751 return false;
752 }
753
754 if (!s.fViewMatrix.cheapEqualTo(fViewMatrix)) {
755 return false;
756 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000757
758 for (int i = 0; i < kNumStages; i++) {
759 if (fTextures[i] &&
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000760 this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000761 return false;
762 }
763 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000764 if (kColorMatrix_StateBit & s.fFlagBits) {
765 if (memcmp(fColorMatrix,
766 s.fColorMatrix,
767 sizeof(fColorMatrix))) {
768 return false;
769 }
770 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000771
772 return true;
773 }
774 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
775
776 // Most stages are usually not used, so conditionals here
777 // reduce the expected number of bytes touched by 50%.
778 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000779 memcpy(this->podStart(), s.podStart(), this->podSize());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000780
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000781 fViewMatrix = s.fViewMatrix;
782
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000783 GrAssert(0 == s.fEdgeAANumEdges);
784 fEdgeAANumEdges = 0;
785
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000786 for (int i = 0; i < kNumStages; i++) {
787 if (s.fTextures[i]) {
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000788 this->fSamplerStates[i] = s.fSamplerStates[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000789 }
790 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000791 if (kColorMatrix_StateBit & s.fFlagBits) {
792 memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix));
793 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000794
795 return *this;
796 }
797
798private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000799
800 const void* podStart() const {
801 return reinterpret_cast<const void*>(&fPodStartMarker);
802 }
803 void* podStart() {
804 return reinterpret_cast<void*>(&fPodStartMarker);
805 }
806 size_t memsetSize() const {
807 return reinterpret_cast<size_t>(&fMemsetEndMarker) -
808 reinterpret_cast<size_t>(&fPodStartMarker) +
809 sizeof(fMemsetEndMarker);
810 }
811 size_t podSize() const {
812 // Can't use offsetof() with non-POD types, so stuck with pointer math.
813 // TODO: ignores GrTesselatedPathRenderer data structures. We don't
814 // have a compile-time flag that lets us know if it's being used, and
815 // checking at runtime seems to cost 5% performance.
816 return reinterpret_cast<size_t>(&fPodEndMarker) -
817 reinterpret_cast<size_t>(&fPodStartMarker) +
818 sizeof(fPodEndMarker);
819 }
820
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 static const StageMask kIllegalStageMaskBits = ~((1 << kNumStages)-1);
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000822 // @{ these fields can be initialized with memset to 0
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000823 union {
824 GrColor fBlendConstant;
825 GrColor fPodStartMarker;
826 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000827 GrTexture* fTextures[kNumStages];
828 GrColor fColorFilterColor;
829 uint32_t fFlagBits;
830 DrawFace fDrawFace;
831 VertexEdgeType fVertexEdgeType;
832 GrStencilSettings fStencilSettings;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000833 union {
834 GrRenderTarget* fRenderTarget;
835 GrRenderTarget* fMemsetEndMarker;
836 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000837 // @}
838
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000839 // @{ Initialized to values other than zero, but memcmp'ed in operator==
840 // and memcpy'ed in operator=.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000841 GrColor fColor;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000842 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000843 int fFirstCoverageStage;
844 SkXfermode::Mode fColorFilterMode;
845 GrBlendCoeff fSrcBlend;
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000846 union {
847 GrBlendCoeff fDstBlend;
848 GrBlendCoeff fPodEndMarker;
849 };
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000850 // @}
851
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000852 GrMatrix fViewMatrix;
853
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000854 // @{ Data for GrTesselatedPathRenderer
855 // TODO: currently ignored in copying & comparison for performance.
856 // Must be considered if GrTesselatedPathRenderer is being used.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000857 int fEdgeAANumEdges;
858 Edge fEdgeAAEdges[kMaxEdges];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000859 // @}
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000860
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 // This field must be last; it will not be copied or compared
862 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000863 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000864 // only compared if the color matrix enable flag is set
865 float fColorMatrix[20]; // 5 x 4 matrix
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866
tomhudson@google.com93813632011-10-27 20:21:16 +0000867};
868
869#endif