blob: 6951d46e1dc107156ca967fa85fb745c4c69f874 [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.com2e3d1442012-03-26 20:33:54 +000013#include "GrRefCnt.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000014#include "GrSamplerState.h"
15#include "GrStencil.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000016#include "GrTexture.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000017#include "GrRenderTarget.h"
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000018#include "effects/GrSingleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000019
20#include "SkXfermode.h"
21
bsalomon@google.comaf84e742012-10-05 13:23:24 +000022class GrPaint;
tomhudson@google.com93813632011-10-27 20:21:16 +000023
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000024class GrDrawState : public GrRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000025public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000026 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000027
tomhudson@google.com93813632011-10-27 20:21:16 +000028 /**
29 * Number of texture stages. Each stage takes as input a color and
30 * 2D texture coordinates. The color input to the first enabled stage is the
31 * per-vertex color or the constant color (setColor/setAlpha) if there are
32 * no per-vertex colors. For subsequent stages the input color is the output
33 * color from the previous enabled stage. The output color of each stage is
34 * the input color modulated with the result of a texture lookup. Texture
35 * lookups are specified by a texture a sampler (setSamplerState). Texture
36 * coordinates for each stage come from the vertices based on a
37 * GrVertexLayout bitfield. The output fragment color is the output color of
38 * the last enabled stage. The presence or absence of texture coordinates
39 * for each stage in the vertex layout indicates whether a stage is enabled
40 * or not.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000041 *
42 * Stages 0 through GrPaint::kTotalStages-1 are reserved for setting up
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 * the draw (i.e., textures and filter masks). Stages GrPaint::kTotalStages
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000044 * through kNumStages-2 are earmarked for use by GrTextContext and
45 * GrPathRenderer-derived classes. kNumStages-1 is earmarked for clipping
46 * by GrClipMaskManager.
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
bsalomon@google.com1e269b52012-10-15 14:25:31 +000097 * encompasses more than GrPaint. Aspects of GrDrawState that have no
bsalomon@google.comaf84e742012-10-05 13:23:24 +000098 * 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.comdfdb7e52012-10-16 15:19:45 +0000197 void createTextureEffect(int stage, GrTexture* texture, const GrMatrix& matrix) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000198 GrAssert(!this->getSampler(stage).getCustomStage());
bsalomon@google.coma469c282012-10-24 18:28:34 +0000199 GrEffect* customStage = SkNEW_ARGS(GrSingleTextureEffect, (texture));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000200 this->sampler(stage)->setCustomStage(customStage, matrix)->unref();
201 }
202 void createTextureEffect(int stage, GrTexture* texture,
203 const GrMatrix& matrix,
204 const GrTextureParams& params) {
205 GrAssert(!this->getSampler(stage).getCustomStage());
bsalomon@google.coma469c282012-10-24 18:28:34 +0000206 GrEffect* customStage = SkNEW_ARGS(GrSingleTextureEffect, (texture, params));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000207 this->sampler(stage)->setCustomStage(customStage, matrix)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000208 }
209
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000210
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000211 bool stagesDisabled() {
212 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000213 if (NULL != fSamplerStates[i].getCustomStage()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000214 return false;
215 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000216 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000217 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000218 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000219
220 void disableStage(int index) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000221 fSamplerStates[index].setCustomStage(NULL);
222 }
223
robertphillips@google.com972265d2012-06-13 18:49:30 +0000224 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000225 * Release all the textures and custom stages referred to by this
226 * draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000227 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000228 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000229 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000230 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000231 }
232 }
233
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000234 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000235 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000236 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
237 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000238 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000239 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000240 }
241 }
242 private:
243 GrDrawState* fDrawState;
244 };
245
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000246 /// @}
247
248 ///////////////////////////////////////////////////////////////////////////
249 /// @name Samplers
250 ////
251
252 /**
253 * Returns the current sampler for a stage.
254 */
255 const GrSamplerState& getSampler(int stage) const {
256 GrAssert((unsigned)stage < kNumStages);
257 return fSamplerStates[stage];
258 }
259
260 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000261 * Writable pointer to a stage's sampler.
262 */
263 GrSamplerState* sampler(int stage) {
264 GrAssert((unsigned)stage < kNumStages);
265 return fSamplerStates + stage;
266 }
267
268 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000269 * Called when the source coord system is changing. preConcat gives the transformation from the
270 * old coord system to the new coord system.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000271 */
bsalomon@google.com288d9542012-10-17 12:53:54 +0000272 void preConcatSamplerMatrices(const GrMatrix& preConcat) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000273 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000274 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000275 fSamplerStates[i].preConcatCoordChange(preConcat);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000276 }
277 }
278 }
279
bsalomon@google.come3d32162012-07-20 13:37:06 +0000280 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000281 * Called when the source coord system is changing. preConcatInverse is the inverse of the
282 * transformation from the old coord system to the new coord system. Returns false if the matrix
283 * cannot be inverted.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000284 */
bsalomon@google.com288d9542012-10-17 12:53:54 +0000285 bool preConcatSamplerMatricesWithInverse(const GrMatrix& preConcatInverse) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000286 GrMatrix inv;
287 bool computed = false;
288 for (int i = 0; i < kNumStages; ++i) {
289 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000290 if (!computed && !preConcatInverse.invert(&inv)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000291 return false;
292 } else {
293 computed = true;
294 }
bsalomon@google.com288d9542012-10-17 12:53:54 +0000295 fSamplerStates[i].preConcatCoordChange(preConcatInverse);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000296 }
297 }
298 return true;
299 }
300
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000301 /// @}
302
303 ///////////////////////////////////////////////////////////////////////////
304 /// @name Coverage / Color Stages
305 ////
306
307 /**
308 * A common pattern is to compute a color with the initial stages and then
309 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000310 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
311 * computed based on the pre-coverage-modulated color. The division of
312 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000313 * this method. Initially this is kNumStages (all stages
314 * are color-computing).
315 */
316 void setFirstCoverageStage(int firstCoverageStage) {
317 GrAssert((unsigned)firstCoverageStage <= kNumStages);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000318 fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000319 }
320
321 /**
322 * Gets the index of the first coverage-computing stage.
323 */
324 int getFirstCoverageStage() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000325 return fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000326 }
327
328 ///@}
329
330 ///////////////////////////////////////////////////////////////////////////
331 /// @name Blending
332 ////
333
334 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000335 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000336 *
337 * The blend function will be:
338 * D' = sat(S*srcCoef + D*dstCoef)
339 *
340 * where D is the existing destination color, S is the incoming source
341 * color, and D' is the new destination color that will be written. sat()
342 * is the saturation function.
343 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000344 * @param srcCoef coefficient applied to the src color.
345 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000346 */
347 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
348 fSrcBlend = srcCoeff;
349 fDstBlend = dstCoeff;
350 #if GR_DEBUG
351 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000352 case kDC_GrBlendCoeff:
353 case kIDC_GrBlendCoeff:
354 case kDA_GrBlendCoeff:
355 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000356 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
357 "coverage stages.\n");
358 break;
359 default:
360 break;
361 }
362 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000363 case kSC_GrBlendCoeff:
364 case kISC_GrBlendCoeff:
365 case kSA_GrBlendCoeff:
366 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000367 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
368 "coverage stages.\n");
369 break;
370 default:
371 break;
372 }
373 #endif
374 }
375
376 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
377 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
378
379 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
380 GrBlendCoeff* dstBlendCoeff) const {
381 *srcBlendCoeff = fSrcBlend;
382 *dstBlendCoeff = fDstBlend;
383 }
384
385 /**
386 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000387 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000388 * kConstC_GrBlendCoeff
389 * kIConstC_GrBlendCoeff
390 * kConstA_GrBlendCoeff
391 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000392 *
393 * @param constant the constant to set
394 */
395 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
396
397 /**
398 * Retrieves the last value set by setBlendConstant()
399 * @return the blending constant value
400 */
401 GrColor getBlendConstant() const { return fBlendConstant; }
402
403 /// @}
404
405 ///////////////////////////////////////////////////////////////////////////
406 /// @name View Matrix
407 ////
408
409 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000410 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000411 *
412 * In the post-view-matrix space the rectangle [0,w]x[0,h]
413 * fully covers the render target. (w and h are the width and height of the
414 * the rendertarget.)
415 */
416 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
417
418 /**
419 * Gets a writable pointer to the view matrix.
420 */
421 GrMatrix* viewMatrix() { return &fViewMatrix; }
422
423 /**
424 * Multiplies the current view matrix by a matrix
425 *
426 * After this call V' = V*m where V is the old view matrix,
427 * m is the parameter to this function, and V' is the new view matrix.
428 * (We consider positions to be column vectors so position vector p is
429 * transformed by matrix X as p' = X*p.)
430 *
431 * @param m the matrix used to modify the view matrix.
432 */
433 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
434
435 /**
436 * Multiplies the current view matrix by a matrix
437 *
438 * After this call V' = m*V where V is the old view matrix,
439 * m is the parameter to this function, and V' is the new view matrix.
440 * (We consider positions to be column vectors so position vector p is
441 * transformed by matrix X as p' = X*p.)
442 *
443 * @param m the matrix used to modify the view matrix.
444 */
445 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
446
447 /**
448 * Retrieves the current view matrix
449 * @return the current view matrix.
450 */
451 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
452
453 /**
454 * Retrieves the inverse of the current view matrix.
455 *
456 * If the current view matrix is invertible, return true, and if matrix
457 * is non-null, copy the inverse into it. If the current view matrix is
458 * non-invertible, return false and ignore the matrix parameter.
459 *
460 * @param matrix if not null, will receive a copy of the current inverse.
461 */
462 bool getViewInverse(GrMatrix* matrix) const {
463 // TODO: determine whether we really need to leave matrix unmodified
464 // at call sites when inversion fails.
465 GrMatrix inverse;
466 if (fViewMatrix.invert(&inverse)) {
467 if (matrix) {
468 *matrix = inverse;
469 }
470 return true;
471 }
472 return false;
473 }
474
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000475 ////////////////////////////////////////////////////////////////////////////
476
477 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000478 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
479 * Stage matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000480 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 class AutoViewMatrixRestore : public ::GrNoncopyable {
482 public:
483 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000484
485 AutoViewMatrixRestore(GrDrawState* ds,
486 const GrMatrix& preconcatMatrix,
487 uint32_t explicitCoordStageMask = 0) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000488 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000489 this->set(ds, preconcatMatrix, explicitCoordStageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000491
492 ~AutoViewMatrixRestore() { this->restore(); }
493
bsalomon@google.coma8347462012-10-08 18:59:39 +0000494 /**
495 * Can be called prior to destructor to restore the original matrix.
496 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000497 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000498
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000499 void set(GrDrawState* drawState,
500 const GrMatrix& preconcatMatrix,
501 uint32_t explicitCoordStageMask = 0);
502
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000503 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000504
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000506 GrDrawState* fDrawState;
507 GrMatrix fViewMatrix;
508 GrSamplerState::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
509 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000510 };
511
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000512 ////////////////////////////////////////////////////////////////////////////
513
514 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000515 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
516 * destructor undoes the changes, restoring the view matrix that was set before the
517 * constructor. It is similar to passing the inverse of the current view matrix to
518 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000519 */
520 class AutoDeviceCoordDraw : ::GrNoncopyable {
521 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000522 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000523 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000524 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
525 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
526 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000527 */
528 AutoDeviceCoordDraw(GrDrawState* drawState,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000529 uint32_t explicitCoordStageMask = 0) {
530 fDrawState = NULL;
531 this->set(drawState, explicitCoordStageMask);
532 }
533
bsalomon@google.coma8347462012-10-08 18:59:39 +0000534 ~AutoDeviceCoordDraw() { this->restore(); }
535
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000536 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
537
bsalomon@google.coma8347462012-10-08 18:59:39 +0000538 /**
539 * Returns true if this object was successfully initialized on to a GrDrawState. It may
540 * return false because a non-default constructor or set() were never called or because
541 * the view matrix was not invertible.
542 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000543 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000544
bsalomon@google.coma8347462012-10-08 18:59:39 +0000545 /**
546 * Returns the matrix that was set previously set on the drawState. This is only valid
547 * if succeeded returns true.
548 */
549 const GrMatrix& getOriginalMatrix() const {
550 GrAssert(this->succeeded());
551 return fViewMatrix;
552 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000553
bsalomon@google.coma8347462012-10-08 18:59:39 +0000554 /**
555 * Can be called prior to destructor to restore the original matrix.
556 */
557 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000558
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000559 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000560 GrDrawState* fDrawState;
561 GrMatrix fViewMatrix;
562 GrSamplerState::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
563 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000564 };
565
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000566 /// @}
567
568 ///////////////////////////////////////////////////////////////////////////
569 /// @name Render Target
570 ////
571
572 /**
573 * Sets the rendertarget used at the next drawing call
574 *
575 * @param target The render target to set.
576 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000577 void setRenderTarget(GrRenderTarget* target) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000578 GrSafeAssign(fRenderTarget, target);
579 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580
581 /**
582 * Retrieves the currently set rendertarget.
583 *
584 * @return The currently set render target.
585 */
586 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
587 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
588
589 class AutoRenderTargetRestore : public ::GrNoncopyable {
590 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000591 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
593 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000594 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595 this->set(ds, newTarget);
596 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000597 ~AutoRenderTargetRestore() { this->restore(); }
598
599 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000600 if (NULL != fDrawState) {
601 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000602 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000604 GrSafeSetNull(fSavedTarget);
605 }
606
607 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
608 this->restore();
609
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000611 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000612 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000613 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000615 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000617 }
618 private:
619 GrDrawState* fDrawState;
620 GrRenderTarget* fSavedTarget;
621 };
622
623 /// @}
624
625 ///////////////////////////////////////////////////////////////////////////
626 /// @name Stencil
627 ////
628
629 /**
630 * Sets the stencil settings to use for the next draw.
631 * Changing the clip has the side-effect of possibly zeroing
632 * out the client settable stencil bits. So multipass algorithms
633 * using stencil should not change the clip between passes.
634 * @param settings the stencil settings to use.
635 */
636 void setStencil(const GrStencilSettings& settings) {
637 fStencilSettings = settings;
638 }
639
640 /**
641 * Shortcut to disable stencil testing and ops.
642 */
643 void disableStencil() {
644 fStencilSettings.setDisabled();
645 }
646
647 const GrStencilSettings& getStencil() const { return fStencilSettings; }
648
649 GrStencilSettings* stencil() { return &fStencilSettings; }
650
651 /// @}
652
653 ///////////////////////////////////////////////////////////////////////////
654 // @name Edge AA
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000655 // Edge equations can be specified to perform anti-aliasing. Because the
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000656 // edges are specified as per-vertex data, vertices that are shared by
657 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000658 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000659 ////
660
661 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000662 * When specifying edges as vertex data this enum specifies what type of
663 * edges are in use. The edges are always 4 GrScalars in memory, even when
664 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000665 *
666 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
667 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000668 */
669 enum VertexEdgeType {
670 /* 1-pixel wide line
671 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
672 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000673 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000674 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000675 being inside, positive outside. Edge specified in window space
676 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000677 kQuad_EdgeType,
678 /* Same as above but for hairline quadratics. Uses unsigned distance.
679 Coverage is min(0, 1-distance). */
680 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000681 /* Circle specified as center_x, center_y, outer_radius, inner_radius
682 all in window space (y-down). */
683 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000684
685 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000686 };
687
688 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000689 * Determines the interpretation per-vertex edge data when the
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
691 * are not specified the value of this setting has no effect.
692 */
693 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000694 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 fVertexEdgeType = type;
696 }
697
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000698 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000699
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000701
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 ///////////////////////////////////////////////////////////////////////////
703 /// @name State Flags
704 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000705
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 /**
707 * Flags that affect rendering. Controlled using enable/disableState(). All
708 * default to disabled.
709 */
710 enum StateBits {
711 /**
712 * Perform dithering. TODO: Re-evaluate whether we need this bit
713 */
714 kDither_StateBit = 0x01,
715 /**
716 * Perform HW anti-aliasing. This means either HW FSAA, if supported
717 * by the render target, or smooth-line rendering if a line primitive
718 * is drawn and line smoothing is supported by the 3D API.
719 */
720 kHWAntialias_StateBit = 0x02,
721 /**
722 * Draws will respect the clip, otherwise the clip is ignored.
723 */
724 kClip_StateBit = 0x04,
725 /**
726 * Disables writing to the color buffer. Useful when performing stencil
727 * operations.
728 */
729 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000730
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000731 // Users of the class may add additional bits to the vector
732 kDummyStateBit,
733 kLastPublicStateBit = kDummyStateBit-1,
734 };
735
736 void resetStateFlags() {
737 fFlagBits = 0;
738 }
739
740 /**
741 * Enable render state settings.
742 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000743 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000744 */
745 void enableState(uint32_t stateBits) {
746 fFlagBits |= stateBits;
747 }
748
749 /**
750 * Disable render state settings.
751 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000752 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000753 */
754 void disableState(uint32_t stateBits) {
755 fFlagBits &= ~(stateBits);
756 }
757
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000758 /**
759 * Enable or disable stateBits based on a boolean.
760 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000761 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000762 * @param enable if true enable stateBits, otherwise disable
763 */
764 void setState(uint32_t stateBits, bool enable) {
765 if (enable) {
766 this->enableState(stateBits);
767 } else {
768 this->disableState(stateBits);
769 }
770 }
771
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000772 bool isDitherState() const {
773 return 0 != (fFlagBits & kDither_StateBit);
774 }
775
776 bool isHWAntialiasState() const {
777 return 0 != (fFlagBits & kHWAntialias_StateBit);
778 }
779
780 bool isClipState() const {
781 return 0 != (fFlagBits & kClip_StateBit);
782 }
783
784 bool isColorWriteDisabled() const {
785 return 0 != (fFlagBits & kNoColorWrites_StateBit);
786 }
787
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000788 bool isStateFlagEnabled(uint32_t stateBit) const {
789 return 0 != (stateBit & fFlagBits);
790 }
791
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000792 /// @}
793
794 ///////////////////////////////////////////////////////////////////////////
795 /// @name Face Culling
796 ////
797
798 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000799 kInvalid_DrawFace = -1,
800
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000801 kBoth_DrawFace,
802 kCCW_DrawFace,
803 kCW_DrawFace,
804 };
805
806 /**
807 * Controls whether clockwise, counterclockwise, or both faces are drawn.
808 * @param face the face(s) to draw.
809 */
810 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000811 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 fDrawFace = face;
813 }
814
815 /**
816 * Gets whether the target is drawing clockwise, counterclockwise,
817 * or both faces.
818 * @return the current draw face(s).
819 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000820 DrawFace getDrawFace() const { return fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000821
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 /// @}
823
824 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000825
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000826 bool isStageEnabled(int s) const {
827 GrAssert((unsigned)s < kNumStages);
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000828 return (NULL != fSamplerStates[s].getCustomStage());
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000829 }
830
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000831 // Most stages are usually not used, so conditionals here
832 // reduce the expected number of bytes touched by 50%.
833 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000834 if (fColor != s.fColor ||
835 !s.fViewMatrix.cheapEqualTo(fViewMatrix) ||
836 fRenderTarget != s.fRenderTarget ||
837 fSrcBlend != s.fSrcBlend ||
838 fDstBlend != s.fDstBlend ||
839 fBlendConstant != s.fBlendConstant ||
840 fFlagBits != s.fFlagBits ||
841 fVertexEdgeType != s.fVertexEdgeType ||
842 fStencilSettings != s.fStencilSettings ||
843 fFirstCoverageStage != s.fFirstCoverageStage ||
844 fCoverage != s.fCoverage ||
845 fColorFilterMode != s.fColorFilterMode ||
846 fColorFilterColor != s.fColorFilterColor ||
847 fDrawFace != s.fDrawFace) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000848 return false;
849 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000850
851 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000852 bool enabled = this->isStageEnabled(i);
853 if (enabled != s.isStageEnabled(i)) {
854 return false;
855 }
856 if (enabled && this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000857 return false;
858 }
859 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000860 return true;
861 }
862 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
863
rmistry@google.comd6176b02012-08-23 18:14:13 +0000864 // Most stages are usually not used, so conditionals here
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000865 // reduce the expected number of bytes touched by 50%.
866 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000867 fColor = s.fColor;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000868 fViewMatrix = s.fViewMatrix;
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000869 SkRefCnt_SafeAssign(fRenderTarget, s.fRenderTarget);
870 fSrcBlend = s.fSrcBlend;
871 fDstBlend = s.fDstBlend;
872 fBlendConstant = s.fBlendConstant;
873 fFlagBits = s.fFlagBits;
874 fVertexEdgeType = s.fVertexEdgeType;
875 fStencilSettings = s.fStencilSettings;
876 fFirstCoverageStage = s.fFirstCoverageStage;
877 fCoverage = s.fCoverage;
878 fColorFilterMode = s.fColorFilterMode;
879 fColorFilterColor = s.fColorFilterColor;
880 fDrawFace = s.fDrawFace;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000881
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000882 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000883 if (s.isStageEnabled(i)) {
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000884 this->fSamplerStates[i] = s.fSamplerStates[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000885 }
886 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000887
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000888 return *this;
889 }
890
891private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000892
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000893 // These fields are roughly sorted by decreasing likelihood of being different in op==
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000894 GrColor fColor;
895 GrMatrix fViewMatrix;
896 GrRenderTarget* fRenderTarget;
897 GrBlendCoeff fSrcBlend;
898 GrBlendCoeff fDstBlend;
899 GrColor fBlendConstant;
900 uint32_t fFlagBits;
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000901 VertexEdgeType fVertexEdgeType;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000902 GrStencilSettings fStencilSettings;
robertphillips@google.com69ffcf02012-06-26 21:01:05 +0000903 int fFirstCoverageStage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000904 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000905 SkXfermode::Mode fColorFilterMode;
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000906 GrColor fColorFilterColor;
907 DrawFace fDrawFace;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000908
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000909 // This field must be last; it will not be copied or compared
910 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000911 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000912
reed@google.comfa35e3d2012-06-26 20:16:17 +0000913 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000914};
915
916#endif