blob: 330ae666425aa8b13e7a9972e8b2a00414e80b6b [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.
bsalomon@google.com6f261be2012-10-24 19:07:10 +000072 * Sampler states *will* be modified: textures or GrEffect objects will be released.
rmistry@google.comd6176b02012-08-23 18:14:13 +000073 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000074 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000075
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000076 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000077
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000078 fColor = 0xffffffff;
bsalomon@google.com861b3a22012-09-26 17:28:25 +000079 fViewMatrix.reset();
80 GrSafeSetNull(fRenderTarget);
bsalomon@google.com47059542012-06-06 20:51:20 +000081 fSrcBlend = kOne_GrBlendCoeff;
82 fDstBlend = kZero_GrBlendCoeff;
bsalomon@google.com861b3a22012-09-26 17:28:25 +000083 fBlendConstant = 0x0;
84 fFlagBits = 0x0;
85 fVertexEdgeType = kHairLine_EdgeType;
86 fStencilSettings.setDisabled();
87 fFirstCoverageStage = kNumStages;
88 fCoverage = 0xffffffff;
89 fColorFilterMode = SkXfermode::kDst_Mode;
90 fColorFilterColor = 0x0;
91 fDrawFace = kBoth_DrawFace;
bsalomon@google.comaf84e742012-10-05 13:23:24 +000092 }
93
94 /**
95 * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
bsalomon@google.com1e269b52012-10-15 14:25:31 +000096 * encompasses more than GrPaint. Aspects of GrDrawState that have no
bsalomon@google.comaf84e742012-10-05 13:23:24 +000097 * GrPaint equivalents are not modified. GrPaint has fewer stages than
98 * GrDrawState. The extra GrDrawState stages are disabled.
99 */
100 void setFromPaint(const GrPaint& paint);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000101
102 ///////////////////////////////////////////////////////////////////////////
103 /// @name Color
104 ////
105
106 /**
107 * Sets color for next draw to a premultiplied-alpha color.
108 *
109 * @param color the color to set.
110 */
111 void setColor(GrColor color) { fColor = color; }
112
113 GrColor getColor() const { return fColor; }
114
115 /**
116 * Sets the color to be used for the next draw to be
117 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
118 *
119 * @param alpha The alpha value to set as the color.
120 */
121 void setAlpha(uint8_t a) {
122 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
123 }
124
125 /**
126 * Add a color filter that can be represented by a color and a mode. Applied
127 * after color-computing texture stages.
128 */
129 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
130 fColorFilterColor = c;
131 fColorFilterMode = mode;
132 }
133
134 GrColor getColorFilterColor() const { return fColorFilterColor; }
135 SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; }
136
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000137 /**
138 * Constructor sets the color to be 'color' which is undone by the destructor.
139 */
140 class AutoColorRestore : public ::GrNoncopyable {
141 public:
142 AutoColorRestore(GrDrawState* drawState, GrColor color) {
143 fDrawState = drawState;
144 fOldColor = fDrawState->getColor();
145 fDrawState->setColor(color);
146 }
147 ~AutoColorRestore() {
148 fDrawState->setColor(fOldColor);
149 }
150 private:
151 GrDrawState* fDrawState;
152 GrColor fOldColor;
153 };
154
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000155 /// @}
156
157 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000158 /// @name Coverage
159 ////
160
161 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000162 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000163 * initial value (after construction or reset()) is 0xff. The constant
164 * coverage is ignored when per-vertex coverage is provided.
165 */
166 void setCoverage(uint8_t coverage) {
167 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
168 }
169
170 /**
171 * Version of above that specifies 4 channel per-vertex color. The value
172 * should be premultiplied.
173 */
174 void setCoverage4(GrColor coverage) {
175 fCoverage = coverage;
176 }
177
178 GrColor getCoverage() const {
179 return fCoverage;
180 }
181
182 /// @}
183
184 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000185 /// @name Textures
186 ////
187
188 /**
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000189 * Creates a GrSingleTextureEffect.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000190 */
191 void createTextureEffect(int stage, GrTexture* texture) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000192 GrAssert(!this->getSampler(stage).getEffect());
193 this->sampler(stage)->setEffect(
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000194 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
195 }
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000196 void createTextureEffect(int stage, GrTexture* texture, const GrMatrix& matrix) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000197 GrAssert(!this->getSampler(stage).getEffect());
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000198 GrEffect* effect = SkNEW_ARGS(GrSingleTextureEffect, (texture));
199 this->sampler(stage)->setEffect(effect, matrix)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000200 }
201 void createTextureEffect(int stage, GrTexture* texture,
202 const GrMatrix& matrix,
203 const GrTextureParams& params) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000204 GrAssert(!this->getSampler(stage).getEffect());
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000205 GrEffect* effect = SkNEW_ARGS(GrSingleTextureEffect, (texture, params));
206 this->sampler(stage)->setEffect(effect, matrix)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000207 }
208
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000209
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000210 bool stagesDisabled() {
211 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000212 if (NULL != fSamplerStates[i].getEffect()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000213 return false;
214 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000215 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000216 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000217 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000218
219 void disableStage(int index) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000220 fSamplerStates[index].setEffect(NULL);
tomhudson@google.com676e6602012-07-10 17:21:48 +0000221 }
222
robertphillips@google.com972265d2012-06-13 18:49:30 +0000223 /**
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000224 * Release all the GrEffects referred to by this draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000225 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000226 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000227 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000228 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000229 }
230 }
231
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000232 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000233 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000234 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
235 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000236 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000237 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000238 }
239 }
240 private:
241 GrDrawState* fDrawState;
242 };
243
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000244 /// @}
245
246 ///////////////////////////////////////////////////////////////////////////
247 /// @name Samplers
248 ////
249
250 /**
251 * Returns the current sampler for a stage.
252 */
253 const GrSamplerState& getSampler(int stage) const {
254 GrAssert((unsigned)stage < kNumStages);
255 return fSamplerStates[stage];
256 }
257
258 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000259 * Writable pointer to a stage's sampler.
260 */
261 GrSamplerState* sampler(int stage) {
262 GrAssert((unsigned)stage < kNumStages);
263 return fSamplerStates + stage;
264 }
265
266 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000267 * Called when the source coord system is changing. preConcat gives the transformation from the
268 * old coord system to the new coord system.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000269 */
bsalomon@google.com288d9542012-10-17 12:53:54 +0000270 void preConcatSamplerMatrices(const GrMatrix& preConcat) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000271 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000272 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000273 fSamplerStates[i].preConcatCoordChange(preConcat);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000274 }
275 }
276 }
277
bsalomon@google.come3d32162012-07-20 13:37:06 +0000278 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000279 * Called when the source coord system is changing. preConcatInverse is the inverse of the
280 * transformation from the old coord system to the new coord system. Returns false if the matrix
281 * cannot be inverted.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000282 */
bsalomon@google.com288d9542012-10-17 12:53:54 +0000283 bool preConcatSamplerMatricesWithInverse(const GrMatrix& preConcatInverse) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000284 GrMatrix inv;
285 bool computed = false;
286 for (int i = 0; i < kNumStages; ++i) {
287 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000288 if (!computed && !preConcatInverse.invert(&inv)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000289 return false;
290 } else {
291 computed = true;
292 }
bsalomon@google.com288d9542012-10-17 12:53:54 +0000293 fSamplerStates[i].preConcatCoordChange(preConcatInverse);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000294 }
295 }
296 return true;
297 }
298
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000299 /// @}
300
301 ///////////////////////////////////////////////////////////////////////////
302 /// @name Coverage / Color Stages
303 ////
304
305 /**
306 * A common pattern is to compute a color with the initial stages and then
307 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
309 * computed based on the pre-coverage-modulated color. The division of
310 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000311 * this method. Initially this is kNumStages (all stages
312 * are color-computing).
313 */
314 void setFirstCoverageStage(int firstCoverageStage) {
315 GrAssert((unsigned)firstCoverageStage <= kNumStages);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000316 fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000317 }
318
319 /**
320 * Gets the index of the first coverage-computing stage.
321 */
322 int getFirstCoverageStage() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000323 return fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000324 }
325
326 ///@}
327
328 ///////////////////////////////////////////////////////////////////////////
329 /// @name Blending
330 ////
331
332 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000333 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000334 *
335 * The blend function will be:
336 * D' = sat(S*srcCoef + D*dstCoef)
337 *
338 * where D is the existing destination color, S is the incoming source
339 * color, and D' is the new destination color that will be written. sat()
340 * is the saturation function.
341 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000342 * @param srcCoef coefficient applied to the src color.
343 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000344 */
345 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
346 fSrcBlend = srcCoeff;
347 fDstBlend = dstCoeff;
348 #if GR_DEBUG
349 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000350 case kDC_GrBlendCoeff:
351 case kIDC_GrBlendCoeff:
352 case kDA_GrBlendCoeff:
353 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000354 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
355 "coverage stages.\n");
356 break;
357 default:
358 break;
359 }
360 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000361 case kSC_GrBlendCoeff:
362 case kISC_GrBlendCoeff:
363 case kSA_GrBlendCoeff:
364 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000365 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
366 "coverage stages.\n");
367 break;
368 default:
369 break;
370 }
371 #endif
372 }
373
374 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
375 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
376
377 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
378 GrBlendCoeff* dstBlendCoeff) const {
379 *srcBlendCoeff = fSrcBlend;
380 *dstBlendCoeff = fDstBlend;
381 }
382
383 /**
384 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000385 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000386 * kConstC_GrBlendCoeff
387 * kIConstC_GrBlendCoeff
388 * kConstA_GrBlendCoeff
389 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000390 *
391 * @param constant the constant to set
392 */
393 void setBlendConstant(GrColor constant) { fBlendConstant = constant; }
394
395 /**
396 * Retrieves the last value set by setBlendConstant()
397 * @return the blending constant value
398 */
399 GrColor getBlendConstant() const { return fBlendConstant; }
400
401 /// @}
402
403 ///////////////////////////////////////////////////////////////////////////
404 /// @name View Matrix
405 ////
406
407 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000408 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000409 *
410 * In the post-view-matrix space the rectangle [0,w]x[0,h]
411 * fully covers the render target. (w and h are the width and height of the
412 * the rendertarget.)
413 */
414 void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; }
415
416 /**
417 * Gets a writable pointer to the view matrix.
418 */
419 GrMatrix* viewMatrix() { return &fViewMatrix; }
420
421 /**
422 * Multiplies the current view matrix by a matrix
423 *
424 * After this call V' = V*m where V is the old view matrix,
425 * m is the parameter to this function, and V' is the new view matrix.
426 * (We consider positions to be column vectors so position vector p is
427 * transformed by matrix X as p' = X*p.)
428 *
429 * @param m the matrix used to modify the view matrix.
430 */
431 void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); }
432
433 /**
434 * Multiplies the current view matrix by a matrix
435 *
436 * After this call V' = m*V where V is the old view matrix,
437 * m is the parameter to this function, and V' is the new view matrix.
438 * (We consider positions to be column vectors so position vector p is
439 * transformed by matrix X as p' = X*p.)
440 *
441 * @param m the matrix used to modify the view matrix.
442 */
443 void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); }
444
445 /**
446 * Retrieves the current view matrix
447 * @return the current view matrix.
448 */
449 const GrMatrix& getViewMatrix() const { return fViewMatrix; }
450
451 /**
452 * Retrieves the inverse of the current view matrix.
453 *
454 * If the current view matrix is invertible, return true, and if matrix
455 * is non-null, copy the inverse into it. If the current view matrix is
456 * non-invertible, return false and ignore the matrix parameter.
457 *
458 * @param matrix if not null, will receive a copy of the current inverse.
459 */
460 bool getViewInverse(GrMatrix* matrix) const {
461 // TODO: determine whether we really need to leave matrix unmodified
462 // at call sites when inversion fails.
463 GrMatrix inverse;
464 if (fViewMatrix.invert(&inverse)) {
465 if (matrix) {
466 *matrix = inverse;
467 }
468 return true;
469 }
470 return false;
471 }
472
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000473 ////////////////////////////////////////////////////////////////////////////
474
475 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000476 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
477 * Stage matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000478 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000479 class AutoViewMatrixRestore : public ::GrNoncopyable {
480 public:
481 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000482
483 AutoViewMatrixRestore(GrDrawState* ds,
484 const GrMatrix& preconcatMatrix,
485 uint32_t explicitCoordStageMask = 0) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000486 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000487 this->set(ds, preconcatMatrix, explicitCoordStageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000488 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000489
490 ~AutoViewMatrixRestore() { this->restore(); }
491
bsalomon@google.coma8347462012-10-08 18:59:39 +0000492 /**
493 * Can be called prior to destructor to restore the original matrix.
494 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000495 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000496
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000497 void set(GrDrawState* drawState,
498 const GrMatrix& preconcatMatrix,
499 uint32_t explicitCoordStageMask = 0);
500
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000501 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000502
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000503 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000504 GrDrawState* fDrawState;
505 GrMatrix fViewMatrix;
506 GrSamplerState::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
507 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000508 };
509
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000510 ////////////////////////////////////////////////////////////////////////////
511
512 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000513 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
514 * destructor undoes the changes, restoring the view matrix that was set before the
515 * constructor. It is similar to passing the inverse of the current view matrix to
516 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000517 */
518 class AutoDeviceCoordDraw : ::GrNoncopyable {
519 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000520 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000521 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000522 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
523 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
524 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000525 */
526 AutoDeviceCoordDraw(GrDrawState* drawState,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000527 uint32_t explicitCoordStageMask = 0) {
528 fDrawState = NULL;
529 this->set(drawState, explicitCoordStageMask);
530 }
531
bsalomon@google.coma8347462012-10-08 18:59:39 +0000532 ~AutoDeviceCoordDraw() { this->restore(); }
533
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000534 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
535
bsalomon@google.coma8347462012-10-08 18:59:39 +0000536 /**
537 * Returns true if this object was successfully initialized on to a GrDrawState. It may
538 * return false because a non-default constructor or set() were never called or because
539 * the view matrix was not invertible.
540 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000541 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000542
bsalomon@google.coma8347462012-10-08 18:59:39 +0000543 /**
544 * Returns the matrix that was set previously set on the drawState. This is only valid
545 * if succeeded returns true.
546 */
547 const GrMatrix& getOriginalMatrix() const {
548 GrAssert(this->succeeded());
549 return fViewMatrix;
550 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000551
bsalomon@google.coma8347462012-10-08 18:59:39 +0000552 /**
553 * Can be called prior to destructor to restore the original matrix.
554 */
555 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000556
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000557 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000558 GrDrawState* fDrawState;
559 GrMatrix fViewMatrix;
560 GrSamplerState::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
561 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000562 };
563
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000564 /// @}
565
566 ///////////////////////////////////////////////////////////////////////////
567 /// @name Render Target
568 ////
569
570 /**
571 * Sets the rendertarget used at the next drawing call
572 *
573 * @param target The render target to set.
574 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000575 void setRenderTarget(GrRenderTarget* target) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000576 GrSafeAssign(fRenderTarget, target);
577 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000578
579 /**
580 * Retrieves the currently set rendertarget.
581 *
582 * @return The currently set render target.
583 */
584 const GrRenderTarget* getRenderTarget() const { return fRenderTarget; }
585 GrRenderTarget* getRenderTarget() { return fRenderTarget; }
586
587 class AutoRenderTargetRestore : public ::GrNoncopyable {
588 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000589 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000590 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
591 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000592 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000593 this->set(ds, newTarget);
594 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000595 ~AutoRenderTargetRestore() { this->restore(); }
596
597 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000598 if (NULL != fDrawState) {
599 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000600 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000602 GrSafeSetNull(fSavedTarget);
603 }
604
605 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
606 this->restore();
607
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000609 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000611 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000612 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000613 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000615 }
616 private:
617 GrDrawState* fDrawState;
618 GrRenderTarget* fSavedTarget;
619 };
620
621 /// @}
622
623 ///////////////////////////////////////////////////////////////////////////
624 /// @name Stencil
625 ////
626
627 /**
628 * Sets the stencil settings to use for the next draw.
629 * Changing the clip has the side-effect of possibly zeroing
630 * out the client settable stencil bits. So multipass algorithms
631 * using stencil should not change the clip between passes.
632 * @param settings the stencil settings to use.
633 */
634 void setStencil(const GrStencilSettings& settings) {
635 fStencilSettings = settings;
636 }
637
638 /**
639 * Shortcut to disable stencil testing and ops.
640 */
641 void disableStencil() {
642 fStencilSettings.setDisabled();
643 }
644
645 const GrStencilSettings& getStencil() const { return fStencilSettings; }
646
647 GrStencilSettings* stencil() { return &fStencilSettings; }
648
649 /// @}
650
651 ///////////////////////////////////////////////////////////////////////////
652 // @name Edge AA
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000653 // Edge equations can be specified to perform anti-aliasing. Because the
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000654 // edges are specified as per-vertex data, vertices that are shared by
655 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000656 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000657 ////
658
659 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000660 * When specifying edges as vertex data this enum specifies what type of
661 * edges are in use. The edges are always 4 GrScalars in memory, even when
662 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000663 *
664 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
665 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000666 */
667 enum VertexEdgeType {
668 /* 1-pixel wide line
669 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
670 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000671 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000672 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000673 being inside, positive outside. Edge specified in window space
674 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000675 kQuad_EdgeType,
676 /* Same as above but for hairline quadratics. Uses unsigned distance.
677 Coverage is min(0, 1-distance). */
678 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000679 /* Circle specified as center_x, center_y, outer_radius, inner_radius
680 all in window space (y-down). */
681 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000682
683 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000684 };
685
686 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000687 * Determines the interpretation per-vertex edge data when the
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
689 * are not specified the value of this setting has no effect.
690 */
691 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000692 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000693 fVertexEdgeType = type;
694 }
695
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000696 VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000697
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000698 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000699
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 ///////////////////////////////////////////////////////////////////////////
701 /// @name State Flags
702 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000703
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000704 /**
705 * Flags that affect rendering. Controlled using enable/disableState(). All
706 * default to disabled.
707 */
708 enum StateBits {
709 /**
710 * Perform dithering. TODO: Re-evaluate whether we need this bit
711 */
712 kDither_StateBit = 0x01,
713 /**
714 * Perform HW anti-aliasing. This means either HW FSAA, if supported
715 * by the render target, or smooth-line rendering if a line primitive
716 * is drawn and line smoothing is supported by the 3D API.
717 */
718 kHWAntialias_StateBit = 0x02,
719 /**
720 * Draws will respect the clip, otherwise the clip is ignored.
721 */
722 kClip_StateBit = 0x04,
723 /**
724 * Disables writing to the color buffer. Useful when performing stencil
725 * operations.
726 */
727 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000728
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000729 // Users of the class may add additional bits to the vector
730 kDummyStateBit,
731 kLastPublicStateBit = kDummyStateBit-1,
732 };
733
734 void resetStateFlags() {
735 fFlagBits = 0;
736 }
737
738 /**
739 * Enable render state settings.
740 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000741 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 */
743 void enableState(uint32_t stateBits) {
744 fFlagBits |= stateBits;
745 }
746
747 /**
748 * Disable render state settings.
749 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000750 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000751 */
752 void disableState(uint32_t stateBits) {
753 fFlagBits &= ~(stateBits);
754 }
755
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000756 /**
757 * Enable or disable stateBits based on a boolean.
758 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000759 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000760 * @param enable if true enable stateBits, otherwise disable
761 */
762 void setState(uint32_t stateBits, bool enable) {
763 if (enable) {
764 this->enableState(stateBits);
765 } else {
766 this->disableState(stateBits);
767 }
768 }
769
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000770 bool isDitherState() const {
771 return 0 != (fFlagBits & kDither_StateBit);
772 }
773
774 bool isHWAntialiasState() const {
775 return 0 != (fFlagBits & kHWAntialias_StateBit);
776 }
777
778 bool isClipState() const {
779 return 0 != (fFlagBits & kClip_StateBit);
780 }
781
782 bool isColorWriteDisabled() const {
783 return 0 != (fFlagBits & kNoColorWrites_StateBit);
784 }
785
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000786 bool isStateFlagEnabled(uint32_t stateBit) const {
787 return 0 != (stateBit & fFlagBits);
788 }
789
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000790 /// @}
791
792 ///////////////////////////////////////////////////////////////////////////
793 /// @name Face Culling
794 ////
795
796 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000797 kInvalid_DrawFace = -1,
798
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799 kBoth_DrawFace,
800 kCCW_DrawFace,
801 kCW_DrawFace,
802 };
803
804 /**
805 * Controls whether clockwise, counterclockwise, or both faces are drawn.
806 * @param face the face(s) to draw.
807 */
808 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000809 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000810 fDrawFace = face;
811 }
812
813 /**
814 * Gets whether the target is drawing clockwise, counterclockwise,
815 * or both faces.
816 * @return the current draw face(s).
817 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000818 DrawFace getDrawFace() const { return fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000819
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 /// @}
821
822 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000823
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000824 bool isStageEnabled(int s) const {
825 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000826 return (NULL != fSamplerStates[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000827 }
828
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000829 // Most stages are usually not used, so conditionals here
830 // reduce the expected number of bytes touched by 50%.
831 bool operator ==(const GrDrawState& s) const {
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000832 if (fColor != s.fColor ||
833 !s.fViewMatrix.cheapEqualTo(fViewMatrix) ||
834 fRenderTarget != s.fRenderTarget ||
835 fSrcBlend != s.fSrcBlend ||
836 fDstBlend != s.fDstBlend ||
837 fBlendConstant != s.fBlendConstant ||
838 fFlagBits != s.fFlagBits ||
839 fVertexEdgeType != s.fVertexEdgeType ||
840 fStencilSettings != s.fStencilSettings ||
841 fFirstCoverageStage != s.fFirstCoverageStage ||
842 fCoverage != s.fCoverage ||
843 fColorFilterMode != s.fColorFilterMode ||
844 fColorFilterColor != s.fColorFilterColor ||
845 fDrawFace != s.fDrawFace) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000846 return false;
847 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000848
849 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000850 bool enabled = this->isStageEnabled(i);
851 if (enabled != s.isStageEnabled(i)) {
852 return false;
853 }
854 if (enabled && this->fSamplerStates[i] != s.fSamplerStates[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000855 return false;
856 }
857 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000858 return true;
859 }
860 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
861
rmistry@google.comd6176b02012-08-23 18:14:13 +0000862 // Most stages are usually not used, so conditionals here
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000863 // reduce the expected number of bytes touched by 50%.
864 GrDrawState& operator =(const GrDrawState& s) {
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000865 fColor = s.fColor;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000866 fViewMatrix = s.fViewMatrix;
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000867 SkRefCnt_SafeAssign(fRenderTarget, s.fRenderTarget);
868 fSrcBlend = s.fSrcBlend;
869 fDstBlend = s.fDstBlend;
870 fBlendConstant = s.fBlendConstant;
871 fFlagBits = s.fFlagBits;
872 fVertexEdgeType = s.fVertexEdgeType;
873 fStencilSettings = s.fStencilSettings;
874 fFirstCoverageStage = s.fFirstCoverageStage;
875 fCoverage = s.fCoverage;
876 fColorFilterMode = s.fColorFilterMode;
877 fColorFilterColor = s.fColorFilterColor;
878 fDrawFace = s.fDrawFace;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000879
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000880 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000881 if (s.isStageEnabled(i)) {
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000882 this->fSamplerStates[i] = s.fSamplerStates[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000883 }
884 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000885
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000886 return *this;
887 }
888
889private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000890
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000891 // These fields are roughly sorted by decreasing likelihood of being different in op==
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000892 GrColor fColor;
893 GrMatrix fViewMatrix;
894 GrRenderTarget* fRenderTarget;
895 GrBlendCoeff fSrcBlend;
896 GrBlendCoeff fDstBlend;
897 GrColor fBlendConstant;
898 uint32_t fFlagBits;
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000899 VertexEdgeType fVertexEdgeType;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000900 GrStencilSettings fStencilSettings;
robertphillips@google.com69ffcf02012-06-26 21:01:05 +0000901 int fFirstCoverageStage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000902 GrColor fCoverage;
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000903 SkXfermode::Mode fColorFilterMode;
bsalomon@google.com861b3a22012-09-26 17:28:25 +0000904 GrColor fColorFilterColor;
905 DrawFace fDrawFace;
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000906
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000907 // This field must be last; it will not be copied or compared
908 // if the corresponding fTexture[] is NULL.
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000909 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000910
reed@google.comfa35e3d2012-06-26 20:16:17 +0000911 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000912};
913
914#endif