blob: ea8f41836445f1079848b442d28f8c52cc7a595a [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.com1e8f0162012-07-20 16:25:18 +000019#include "effects/GrSingleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000020
21#include "SkXfermode.h"
22
bsalomon@google.comaf84e742012-10-05 13:23:24 +000023class GrPaint;
tomhudson@google.com93813632011-10-27 20:21:16 +000024
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000025class GrDrawState : public GrRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000026public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000027 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
tomhudson@google.com93813632011-10-27 20:21:16 +000029 /**
30 * Number of texture stages. Each stage takes as input a color and
31 * 2D texture coordinates. The color input to the first enabled stage is the
32 * per-vertex color or the constant color (setColor/setAlpha) if there are
33 * no per-vertex colors. For subsequent stages the input color is the output
34 * color from the previous enabled stage. The output color of each stage is
35 * the input color modulated with the result of a texture lookup. Texture
36 * lookups are specified by a texture a sampler (setSamplerState). Texture
37 * coordinates for each stage come from the vertices based on a
38 * GrVertexLayout bitfield. The output fragment color is the output color of
39 * the last enabled stage. The presence or absence of texture coordinates
40 * for each stage in the vertex layout indicates whether a stage is enabled
41 * or not.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000042 *
43 * Stages 0 through GrPaint::kTotalStages-1 are reserved for setting up
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 * the draw (i.e., textures and filter masks). Stages GrPaint::kTotalStages
45 * through kNumStages-1 are earmarked for use by GrTextContext and
robertphillips@google.combf5cad42012-05-10 12:40:40 +000046 * GrPathRenderer-derived classes.
tomhudson@google.com93813632011-10-27 20:21:16 +000047 */
48 enum {
twiz@google.com58071162012-07-18 21:41:50 +000049 kNumStages = 5,
tomhudson@google.com93813632011-10-27 20:21:16 +000050 kMaxTexCoords = kNumStages
51 };
52
rmistry@google.comd6176b02012-08-23 18:14:13 +000053 GrDrawState()
robertphillips@google.com9ec07532012-06-22 12:01:30 +000054 : fRenderTarget(NULL) {
55
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000056 this->reset();
57 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000058
rmistry@google.comd6176b02012-08-23 18:14:13 +000059 GrDrawState(const GrDrawState& state)
robertphillips@google.com9ec07532012-06-22 12:01:30 +000060 : fRenderTarget(NULL) {
61
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000062 *this = state;
63 }
64
robertphillips@google.com9ec07532012-06-22 12:01:30 +000065 virtual ~GrDrawState() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000066 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000067 GrSafeSetNull(fRenderTarget);
68 }
69
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000070 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000071 * Resets to the default state.
72 * Sampler states *will* be modified: textures or CustomStage objects
73 * will be released.
rmistry@google.comd6176b02012-08-23 18:14:13 +000074 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000075 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000076
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000077 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000078
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000079 fColor = 0xffffffff;
bsalomon@google.com861b3a22012-09-26 17:28:25 +000080 fViewMatrix.reset();
81 GrSafeSetNull(fRenderTarget);
bsalomon@google.com47059542012-06-06 20:51:20 +000082 fSrcBlend = kOne_GrBlendCoeff;
83 fDstBlend = kZero_GrBlendCoeff;
bsalomon@google.com861b3a22012-09-26 17:28:25 +000084 fBlendConstant = 0x0;
85 fFlagBits = 0x0;
86 fVertexEdgeType = kHairLine_EdgeType;
87 fStencilSettings.setDisabled();
88 fFirstCoverageStage = kNumStages;
89 fCoverage = 0xffffffff;
90 fColorFilterMode = SkXfermode::kDst_Mode;
91 fColorFilterColor = 0x0;
92 fDrawFace = kBoth_DrawFace;
bsalomon@google.comaf84e742012-10-05 13:23:24 +000093 }
94
95 /**
96 * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
97 * encompases more than GrPaint. Aspects of GrDrawState that have no
98 * GrPaint equivalents are not modified. GrPaint has fewer stages than
99 * GrDrawState. The extra GrDrawState stages are disabled.
100 */
101 void setFromPaint(const GrPaint& paint);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000102
103 ///////////////////////////////////////////////////////////////////////////
104 /// @name Color
105 ////
106
107 /**
108 * Sets color for next draw to a premultiplied-alpha color.
109 *
110 * @param color the color to set.
111 */
112 void setColor(GrColor color) { fColor = color; }
113
114 GrColor getColor() const { return fColor; }
115
116 /**
117 * Sets the color to be used for the next draw to be
118 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
119 *
120 * @param alpha The alpha value to set as the color.
121 */
122 void setAlpha(uint8_t a) {
123 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
124 }
125
126 /**
127 * Add a color filter that can be represented by a color and a mode. Applied
128 * after color-computing texture stages.
129 */
130 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
131 fColorFilterColor = c;
132 fColorFilterMode = mode;
133 }
134
135 GrColor getColorFilterColor() const { return fColorFilterColor; }
136 SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; }
137
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000138 /**
139 * Constructor sets the color to be 'color' which is undone by the destructor.
140 */
141 class AutoColorRestore : public ::GrNoncopyable {
142 public:
143 AutoColorRestore(GrDrawState* drawState, GrColor color) {
144 fDrawState = drawState;
145 fOldColor = fDrawState->getColor();
146 fDrawState->setColor(color);
147 }
148 ~AutoColorRestore() {
149 fDrawState->setColor(fOldColor);
150 }
151 private:
152 GrDrawState* fDrawState;
153 GrColor fOldColor;
154 };
155
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000156 /// @}
157
158 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000159 /// @name Coverage
160 ////
161
162 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000164 * initial value (after construction or reset()) is 0xff. The constant
165 * coverage is ignored when per-vertex coverage is provided.
166 */
167 void setCoverage(uint8_t coverage) {
168 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
169 }
170
171 /**
172 * Version of above that specifies 4 channel per-vertex color. The value
173 * should be premultiplied.
174 */
175 void setCoverage4(GrColor coverage) {
176 fCoverage = coverage;
177 }
178
179 GrColor getCoverage() const {
180 return fCoverage;
181 }
182
183 /// @}
184
185 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000186 /// @name Textures
187 ////
188
189 /**
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000190 * Creates a GrSingleTextureEffect.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000191 */
192 void createTextureEffect(int stage, GrTexture* texture) {
193 GrAssert(!this->getSampler(stage).getCustomStage());
194 this->sampler(stage)->setCustomStage(
195 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
196 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000197 void createTextureEffect(int stage, GrTexture* texture, const GrTextureParams& params) {
198 GrAssert(!this->getSampler(stage).getCustomStage());
199 this->sampler(stage)->setCustomStage(
200 SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref();
201 }
202
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000203
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000204 bool stagesDisabled() {
205 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000206 if (NULL != fSamplerStates[i].getCustomStage()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000207 return false;
208 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000209 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000210 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000211 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000212
213 void disableStage(int index) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000214 fSamplerStates[index].setCustomStage(NULL);
215 }
216
robertphillips@google.com972265d2012-06-13 18:49:30 +0000217 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000218 * Release all the textures and custom stages referred to by this
219 * draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000220 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000221 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000222 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000223 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000224 }
225 }
226
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000227 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000228 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000229 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
230 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000231 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000232 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000233 }
234 }
235 private:
236 GrDrawState* fDrawState;
237 };
238
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000239 /// @}
240
241 ///////////////////////////////////////////////////////////////////////////
242 /// @name Samplers
243 ////
244
245 /**
246 * Returns the current sampler for a stage.
247 */
248 const GrSamplerState& getSampler(int stage) const {
249 GrAssert((unsigned)stage < kNumStages);
250 return fSamplerStates[stage];
251 }
252
253 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000254 * Writable pointer to a stage's sampler.
255 */
256 GrSamplerState* sampler(int stage) {
257 GrAssert((unsigned)stage < kNumStages);
258 return fSamplerStates + stage;
259 }
260
261 /**
bsalomon@google.come3d32162012-07-20 13:37:06 +0000262 * Preconcats the matrix of all samplers of enabled stages with a matrix.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000263 */
bsalomon@google.come3d32162012-07-20 13:37:06 +0000264 void preConcatSamplerMatrices(const GrMatrix& matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000265 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000266 if (this->isStageEnabled(i)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000267 fSamplerStates[i].preConcatMatrix(matrix);
268 }
269 }
270 }
271
bsalomon@google.come3d32162012-07-20 13:37:06 +0000272 /**
273 * Preconcats the matrix of all samplers in the mask with the inverse of a
274 * matrix. If the matrix inverse cannot be computed (and there is at least
275 * one enabled stage) then false is returned.
276 */
277 bool preConcatSamplerMatricesWithInverse(const GrMatrix& matrix) {
278 GrMatrix inv;
279 bool computed = false;
280 for (int i = 0; i < kNumStages; ++i) {
281 if (this->isStageEnabled(i)) {
282 if (!computed && !matrix.invert(&inv)) {
283 return false;
284 } else {
285 computed = true;
286 }
287 fSamplerStates[i].preConcatMatrix(inv);
288 }
289 }
290 return true;
291 }
292
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000293 /// @}
294
295 ///////////////////////////////////////////////////////////////////////////
296 /// @name Coverage / Color Stages
297 ////
298
299 /**
300 * A common pattern is to compute a color with the initial stages and then
301 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000302 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
303 * computed based on the pre-coverage-modulated color. The division of
304 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000305 * this method. Initially this is kNumStages (all stages
306 * are color-computing).
307 */
308 void setFirstCoverageStage(int firstCoverageStage) {
309 GrAssert((unsigned)firstCoverageStage <= kNumStages);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000310 fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000311 }
312
313 /**
314 * Gets the index of the first coverage-computing stage.
315 */
316 int getFirstCoverageStage() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317 return fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000318 }
319
320 ///@}
321
322 ///////////////////////////////////////////////////////////////////////////
323 /// @name Blending
324 ////
325
326 /**
327 * Sets the blending function coeffecients.
328 *
329 * The blend function will be:
330 * D' = sat(S*srcCoef + D*dstCoef)
331 *
332 * where D is the existing destination color, S is the incoming source
333 * color, and D' is the new destination color that will be written. sat()
334 * is the saturation function.
335 *
336 * @param srcCoef coeffecient applied to the src color.
337 * @param dstCoef coeffecient applied to the dst color.
338 */
339 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
340 fSrcBlend = srcCoeff;
341 fDstBlend = dstCoeff;
342 #if GR_DEBUG
343 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000344 case kDC_GrBlendCoeff:
345 case kIDC_GrBlendCoeff:
346 case kDA_GrBlendCoeff:
347 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000348 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
349 "coverage stages.\n");
350 break;
351 default:
352 break;
353 }
354 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000355 case kSC_GrBlendCoeff:
356 case kISC_GrBlendCoeff:
357 case kSA_GrBlendCoeff:
358 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000359 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
360 "coverage stages.\n");
361 break;
362 default:
363 break;
364 }
365 #endif
366 }
367
368 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
369 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
370
371 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
372 GrBlendCoeff* dstBlendCoeff) const {
373 *srcBlendCoeff = fSrcBlend;
374 *dstBlendCoeff = fDstBlend;
375 }
376
377 /**
378 * Sets the blending function constant referenced by the following blending
379 * coeffecients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000380 * kConstC_GrBlendCoeff
381 * kIConstC_GrBlendCoeff
382 * kConstA_GrBlendCoeff
383 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000384 *
385 * @param constant the constant to set
386 */
387 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
388
389 /**
390 * Retrieves the last value set by setBlendConstant()
391 * @return the blending constant value
392 */
393 GrColor getBlendConstant() const { return fBlendConstant; }
394
395 /// @}
396
397 ///////////////////////////////////////////////////////////////////////////
398 /// @name View Matrix
399 ////
400
401 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000402 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000403 *
404 * In the post-view-matrix space the rectangle [0,w]x[0,h]
405 * fully covers the render target. (w and h are the width and height of the
406 * the rendertarget.)
407 */
408 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
409
410 /**
411 * Gets a writable pointer to the view matrix.
412 */
413 GrMatrix* viewMatrix() { return &fViewMatrix; }
414
415 /**
416 * Multiplies the current view matrix by a matrix
417 *
418 * After this call V' = V*m where V is the old view matrix,
419 * m is the parameter to this function, and V' is the new view matrix.
420 * (We consider positions to be column vectors so position vector p is
421 * transformed by matrix X as p' = X*p.)
422 *
423 * @param m the matrix used to modify the view matrix.
424 */
425 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
426
427 /**
428 * Multiplies the current view matrix by a matrix
429 *
430 * After this call V' = m*V where V is the old view matrix,
431 * m is the parameter to this function, and V' is the new view matrix.
432 * (We consider positions to be column vectors so position vector p is
433 * transformed by matrix X as p' = X*p.)
434 *
435 * @param m the matrix used to modify the view matrix.
436 */
437 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
438
439 /**
440 * Retrieves the current view matrix
441 * @return the current view matrix.
442 */
443 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
444
445 /**
446 * Retrieves the inverse of the current view matrix.
447 *
448 * If the current view matrix is invertible, return true, and if matrix
449 * is non-null, copy the inverse into it. If the current view matrix is
450 * non-invertible, return false and ignore the matrix parameter.
451 *
452 * @param matrix if not null, will receive a copy of the current inverse.
453 */
454 bool getViewInverse(GrMatrix* matrix) const {
455 // TODO: determine whether we really need to leave matrix unmodified
456 // at call sites when inversion fails.
457 GrMatrix inverse;
458 if (fViewMatrix.invert(&inverse)) {
459 if (matrix) {
460 *matrix = inverse;
461 }
462 return true;
463 }
464 return false;
465 }
466
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000467 ////////////////////////////////////////////////////////////////////////////
468
469 /**
470 * TODO: Automatically handle stage matrices.
471 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000472 class AutoViewMatrixRestore : public ::GrNoncopyable {
473 public:
474 AutoViewMatrixRestore() : fDrawState(NULL) {}
475 AutoViewMatrixRestore(GrDrawState* ds, const GrMatrix& newMatrix) {
476 fDrawState = NULL;
477 this->set(ds, newMatrix);
478 }
479 AutoViewMatrixRestore(GrDrawState* ds) {
480 fDrawState = NULL;
481 this->set(ds);
482 }
483 ~AutoViewMatrixRestore() {
484 this->set(NULL, GrMatrix::I());
485 }
486 void set(GrDrawState* ds, const GrMatrix& newMatrix) {
487 if (NULL != fDrawState) {
488 fDrawState->setViewMatrix(fSavedMatrix);
489 }
490 if (NULL != ds) {
491 fSavedMatrix = ds->getViewMatrix();
492 ds->setViewMatrix(newMatrix);
493 }
494 fDrawState = ds;
495 }
496 void set(GrDrawState* ds) {
497 if (NULL != fDrawState) {
498 fDrawState->setViewMatrix(fSavedMatrix);
499 }
500 if (NULL != ds) {
501 fSavedMatrix = ds->getViewMatrix();
502 }
503 fDrawState = ds;
504 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000505 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000506 private:
507 GrDrawState* fDrawState;
508 GrMatrix fSavedMatrix;
tomhudson@google.com93813632011-10-27 20:21:16 +0000509 };
510
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000511 ////////////////////////////////////////////////////////////////////////////
512
513 /**
514 * This sets the view matrix to identity and adjusts stage matrices to
515 * compensate. The destructor undoes the changes, restoring the view matrix
516 * that was set before the constructor.
517 */
518 class AutoDeviceCoordDraw : ::GrNoncopyable {
519 public:
520 /**
521 * If a stage's texture matrix is applied to explicit per-vertex coords,
522 * rather than to positions, then we don't want to modify its matrix.
523 * The explicitCoordStageMask is used to specify such stages.
524 */
525 AutoDeviceCoordDraw(GrDrawState* drawState,
526 uint32_t explicitCoordStageMask = 0);
527 bool succeeded() const { return NULL != fDrawState; }
528 ~AutoDeviceCoordDraw();
529 private:
530 GrDrawState* fDrawState;
531 GrMatrix fViewMatrix;
532 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
533 int fRestoreMask;
534 };
535
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000536 /// @}
537
538 ///////////////////////////////////////////////////////////////////////////
539 /// @name Render Target
540 ////
541
542 /**
543 * Sets the rendertarget used at the next drawing call
544 *
545 * @param target The render target to set.
546 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000547 void setRenderTarget(GrRenderTarget* target) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000548 GrSafeAssign(fRenderTarget, target);
549 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000550
551 /**
552 * Retrieves the currently set rendertarget.
553 *
554 * @return The currently set render target.
555 */
556 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
557 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
558
559 class AutoRenderTargetRestore : public ::GrNoncopyable {
560 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000561 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000562 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
563 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000564 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000565 this->set(ds, newTarget);
566 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000567 ~AutoRenderTargetRestore() { this->restore(); }
568
569 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000570 if (NULL != fDrawState) {
571 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000572 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000573 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000574 GrSafeSetNull(fSavedTarget);
575 }
576
577 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
578 this->restore();
579
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000581 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000582 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000583 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000584 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000585 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000587 }
588 private:
589 GrDrawState* fDrawState;
590 GrRenderTarget* fSavedTarget;
591 };
592
593 /// @}
594
595 ///////////////////////////////////////////////////////////////////////////
596 /// @name Stencil
597 ////
598
599 /**
600 * Sets the stencil settings to use for the next draw.
601 * Changing the clip has the side-effect of possibly zeroing
602 * out the client settable stencil bits. So multipass algorithms
603 * using stencil should not change the clip between passes.
604 * @param settings the stencil settings to use.
605 */
606 void setStencil(const GrStencilSettings& settings) {
607 fStencilSettings = settings;
608 }
609
610 /**
611 * Shortcut to disable stencil testing and ops.
612 */
613 void disableStencil() {
614 fStencilSettings.setDisabled();
615 }
616
617 const GrStencilSettings& getStencil() const { return fStencilSettings; }
618
619 GrStencilSettings* stencil() { return &fStencilSettings; }
620
621 /// @}
622
623 ///////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000624 /// @name Color Matrix
625 ////
626
627 /**
628 * Sets the color matrix to use for the next draw.
629 * @param matrix the 5x4 matrix to apply to the incoming color
630 */
631 void setColorMatrix(const float matrix[20]) {
632 memcpy(fColorMatrix, matrix, sizeof(fColorMatrix));
633 }
634
635 const float* getColorMatrix() const { return fColorMatrix; }
636
637 /// @}
638
639 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000640 // @name Edge AA
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000641 // Edge equations can be specified to perform antialiasing. Because the
642 // edges are specified as per-vertex data, vertices that are shared by
643 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000644 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000645 ////
646
647 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000648 * When specifying edges as vertex data this enum specifies what type of
649 * edges are in use. The edges are always 4 GrScalars in memory, even when
650 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000651 *
652 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
653 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000654 */
655 enum VertexEdgeType {
656 /* 1-pixel wide line
657 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
658 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000659 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000660 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000661 being inside, positive outside. Edge specified in window space
662 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000663 kQuad_EdgeType,
664 /* Same as above but for hairline quadratics. Uses unsigned distance.
665 Coverage is min(0, 1-distance). */
666 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000667 /* Circle specified as center_x, center_y, outer_radius, inner_radius
668 all in window space (y-down). */
669 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000670
671 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000672 };
673
674 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000675 * Determines the interpretation per-vertex edge data when the
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
677 * are not specified the value of this setting has no effect.
678 */
679 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000680 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000681 fVertexEdgeType = type;
682 }
683
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000684 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000685
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000686 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000687
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 ///////////////////////////////////////////////////////////////////////////
689 /// @name State Flags
690 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000691
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000692 /**
693 * Flags that affect rendering. Controlled using enable/disableState(). All
694 * default to disabled.
695 */
696 enum StateBits {
697 /**
698 * Perform dithering. TODO: Re-evaluate whether we need this bit
699 */
700 kDither_StateBit = 0x01,
701 /**
702 * Perform HW anti-aliasing. This means either HW FSAA, if supported
703 * by the render target, or smooth-line rendering if a line primitive
704 * is drawn and line smoothing is supported by the 3D API.
705 */
706 kHWAntialias_StateBit = 0x02,
707 /**
708 * Draws will respect the clip, otherwise the clip is ignored.
709 */
710 kClip_StateBit = 0x04,
711 /**
712 * Disables writing to the color buffer. Useful when performing stencil
713 * operations.
714 */
715 kNoColorWrites_StateBit = 0x08,
716 /**
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000717 * Draws will apply the color matrix, otherwise the color matrix is
718 * ignored.
719 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000720 kColorMatrix_StateBit = 0x10,
721
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000722 // Users of the class may add additional bits to the vector
723 kDummyStateBit,
724 kLastPublicStateBit = kDummyStateBit-1,
725 };
726
727 void resetStateFlags() {
728 fFlagBits = 0;
729 }
730
731 /**
732 * Enable render state settings.
733 *
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000734 * @param stateBits bitfield of StateBits specifing the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000735 */
736 void enableState(uint32_t stateBits) {
737 fFlagBits |= stateBits;
738 }
739
740 /**
741 * Disable render state settings.
742 *
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000743 * @param stateBits bitfield of StateBits specifing the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000744 */
745 void disableState(uint32_t stateBits) {
746 fFlagBits &= ~(stateBits);
747 }
748
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000749 /**
750 * Enable or disable stateBits based on a boolean.
751 *
752 * @param stateBits bitfield of StateBits to enable or disablt
753 * @param enable if true enable stateBits, otherwise disable
754 */
755 void setState(uint32_t stateBits, bool enable) {
756 if (enable) {
757 this->enableState(stateBits);
758 } else {
759 this->disableState(stateBits);
760 }
761 }
762
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000763 bool isDitherState() const {
764 return 0 != (fFlagBits & kDither_StateBit);
765 }
766
767 bool isHWAntialiasState() const {
768 return 0 != (fFlagBits & kHWAntialias_StateBit);
769 }
770
771 bool isClipState() const {
772 return 0 != (fFlagBits & kClip_StateBit);
773 }
774
775 bool isColorWriteDisabled() const {
776 return 0 != (fFlagBits & kNoColorWrites_StateBit);
777 }
778
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000779 bool isStateFlagEnabled(uint32_t stateBit) const {
780 return 0 != (stateBit & fFlagBits);
781 }
782
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000783 /// @}
784
785 ///////////////////////////////////////////////////////////////////////////
786 /// @name Face Culling
787 ////
788
789 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000790 kInvalid_DrawFace = -1,
791
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000792 kBoth_DrawFace,
793 kCCW_DrawFace,
794 kCW_DrawFace,
795 };
796
797 /**
798 * Controls whether clockwise, counterclockwise, or both faces are drawn.
799 * @param face the face(s) to draw.
800 */
801 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000802 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803 fDrawFace = face;
804 }
805
806 /**
807 * Gets whether the target is drawing clockwise, counterclockwise,
808 * or both faces.
809 * @return the current draw face(s).
810 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000811 DrawFace getDrawFace() const { return fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000812
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 /// @}
814
815 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000816
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000817 bool isStageEnabled(int s) const {
818 GrAssert((unsigned)s < kNumStages);
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000819 return (NULL != fSamplerStates[s].getCustomStage());
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000820 }
821
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000822 // Most stages are usually not used, so conditionals here
823 // reduce the expected number of bytes touched by 50%.
824 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000825 if (fColor != s.fColor ||
826 !s.fViewMatrix.cheapEqualTo(fViewMatrix) ||
827 fRenderTarget != s.fRenderTarget ||
828 fSrcBlend != s.fSrcBlend ||
829 fDstBlend != s.fDstBlend ||
830 fBlendConstant != s.fBlendConstant ||
831 fFlagBits != s.fFlagBits ||
832 fVertexEdgeType != s.fVertexEdgeType ||
833 fStencilSettings != s.fStencilSettings ||
834 fFirstCoverageStage != s.fFirstCoverageStage ||
835 fCoverage != s.fCoverage ||
836 fColorFilterMode != s.fColorFilterMode ||
837 fColorFilterColor != s.fColorFilterColor ||
838 fDrawFace != s.fDrawFace) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000839 return false;
840 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000841
842 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000843 bool enabled = this->isStageEnabled(i);
844 if (enabled != s.isStageEnabled(i)) {
845 return false;
846 }
847 if (enabled && this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000848 return false;
849 }
850 }
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000851 if (kColorMatrix_StateBit & s.fFlagBits) {
852 if (memcmp(fColorMatrix,
853 s.fColorMatrix,
854 sizeof(fColorMatrix))) {
855 return false;
856 }
857 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000858
859 return true;
860 }
861 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
862
rmistry@google.comd6176b02012-08-23 18:14:13 +0000863 // Most stages are usually not used, so conditionals here
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000864 // reduce the expected number of bytes touched by 50%.
865 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000866 fColor = s.fColor;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000867 fViewMatrix = s.fViewMatrix;
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000868 SkRefCnt_SafeAssign(fRenderTarget, s.fRenderTarget);
869 fSrcBlend = s.fSrcBlend;
870 fDstBlend = s.fDstBlend;
871 fBlendConstant = s.fBlendConstant;
872 fFlagBits = s.fFlagBits;
873 fVertexEdgeType = s.fVertexEdgeType;
874 fStencilSettings = s.fStencilSettings;
875 fFirstCoverageStage = s.fFirstCoverageStage;
876 fCoverage = s.fCoverage;
877 fColorFilterMode = s.fColorFilterMode;
878 fColorFilterColor = s.fColorFilterColor;
879 fDrawFace = s.fDrawFace;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000880
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000881 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000882 if (s.isStageEnabled(i)) {
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000883 this->fSamplerStates[i] = s.fSamplerStates[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000884 }
885 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000886
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000887 if (kColorMatrix_StateBit & s.fFlagBits) {
888 memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix));
889 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000890
891 return *this;
892 }
893
894private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000895
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000896 // These fields are roughly sorted by decreasing liklihood of being different in op==
897 GrColor fColor;
898 GrMatrix fViewMatrix;
899 GrRenderTarget* fRenderTarget;
900 GrBlendCoeff fSrcBlend;
901 GrBlendCoeff fDstBlend;
902 GrColor fBlendConstant;
903 uint32_t fFlagBits;
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000904 VertexEdgeType fVertexEdgeType;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000905 GrStencilSettings fStencilSettings;
robertphillips@google.com69ffcf02012-06-26 21:01:05 +0000906 int fFirstCoverageStage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000907 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000908 SkXfermode::Mode fColorFilterMode;
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000909 GrColor fColorFilterColor;
910 DrawFace fDrawFace;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000911
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000912 // This field must be last; it will not be copied or compared
913 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000914 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com9b1517e2012-03-05 17:58:34 +0000915 // only compared if the color matrix enable flag is set
916 float fColorMatrix[20]; // 5 x 4 matrix
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000917
reed@google.comfa35e3d2012-06-26 20:16:17 +0000918 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000919};
920
921#endif