blob: 326df0bf953a915491671e0fcffb3d19f5c750b7 [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
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000011#include "GrBackendEffectFactory.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrColor.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000013#include "SkMatrix.h"
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000014#include "GrRefCnt.h"
bsalomon@google.com08283af2012-10-26 13:01:20 +000015#include "GrEffectStage.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000016#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"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000019#include "effects/GrSimpleTextureEffect.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 /**
bsalomon@google.com13221342012-10-26 13:41:59 +000030 * Total number of effect stages. Each stage can host a GrEffect. A stage is enabled if it has a
31 * GrEffect. The effect produces an output color in the fragment shader. It's inputs are the
32 * output from the previous enabled stage and a position. The position is either derived from
33 * the interpolated vertex positions or explicit per-vertex coords, depending upon the
34 * GrVertexLayout used to draw.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000035 *
bsalomon@google.com13221342012-10-26 13:41:59 +000036 * The stages are divided into two sets, color-computing and coverage-computing. The final color
37 * stage produces the final pixel color. The coverage-computing stages function exactly as the
38 * color-computing but the output of the final coverage stage is treated as a fractional pixel
39 * coverage rather than as input to the src/dst color blend step.
40 *
41 * The input color to the first enabled color-stage is either the constant color or interpolated
42 * per-vertex colors, depending upon GrVertexLayout. The input to the first coverage stage is
43 * either a constant coverage (usually full-coverage), interpolated per-vertex coverage, or
44 * edge-AA computed coverage. (This latter is going away as soon as it can be rewritten as a
45 * GrEffect).
46 *
bsalomon@google.comcf939ae2012-12-13 19:59:23 +000047 * See the documentation of kCoverageDrawing_StateBit for information about disabling the
48 * the color / coverage distinction.
49 *
bsalomon@google.com13221342012-10-26 13:41:59 +000050 * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied from the client's
51 * GrPaint. Stages GrPaint::kTotalStages through kNumStages-2 are earmarked for use by
52 * GrTextContext and GrPathRenderer-derived classes. kNumStages-1 is earmarked for clipping
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000053 * by GrClipMaskManager.
tomhudson@google.com93813632011-10-27 20:21:16 +000054 */
55 enum {
twiz@google.com58071162012-07-18 21:41:50 +000056 kNumStages = 5,
tomhudson@google.com93813632011-10-27 20:21:16 +000057 kMaxTexCoords = kNumStages
58 };
59
bsalomon@google.comca432082013-01-23 19:53:46 +000060 GrDrawState() {
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000061 this->reset();
62 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000063
bsalomon@google.comca432082013-01-23 19:53:46 +000064 GrDrawState(const GrDrawState& state) {
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000065 *this = state;
66 }
67
robertphillips@google.com9ec07532012-06-22 12:01:30 +000068 virtual ~GrDrawState() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000069 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000070 }
71
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000072 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000073 * Resets to the default state.
bsalomon@google.com08283af2012-10-26 13:01:20 +000074 * GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000075 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000076 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000077
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000078 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000079
bsalomon@google.comca432082013-01-23 19:53:46 +000080 fRenderTarget.reset(NULL);
81
82 fCommon.fColor = 0xffffffff;
83 fCommon.fViewMatrix.reset();
84 fCommon.fSrcBlend = kOne_GrBlendCoeff;
85 fCommon.fDstBlend = kZero_GrBlendCoeff;
86 fCommon.fBlendConstant = 0x0;
87 fCommon.fFlagBits = 0x0;
88 fCommon.fVertexEdgeType = kHairLine_EdgeType;
89 fCommon.fStencilSettings.setDisabled();
90 fCommon.fFirstCoverageStage = kNumStages;
91 fCommon.fCoverage = 0xffffffff;
92 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
93 fCommon.fColorFilterColor = 0x0;
94 fCommon.fDrawFace = kBoth_DrawFace;
bsalomon@google.comaf84e742012-10-05 13:23:24 +000095 }
96
97 /**
98 * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
bsalomon@google.com1e269b52012-10-15 14:25:31 +000099 * encompasses more than GrPaint. Aspects of GrDrawState that have no
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000100 * GrPaint equivalents are not modified. GrPaint has fewer stages than
101 * GrDrawState. The extra GrDrawState stages are disabled.
102 */
103 void setFromPaint(const GrPaint& paint);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000104
105 ///////////////////////////////////////////////////////////////////////////
106 /// @name Color
107 ////
108
109 /**
110 * Sets color for next draw to a premultiplied-alpha color.
111 *
112 * @param color the color to set.
113 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000114 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000115
bsalomon@google.comca432082013-01-23 19:53:46 +0000116 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000117
118 /**
119 * Sets the color to be used for the next draw to be
120 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
121 *
122 * @param alpha The alpha value to set as the color.
123 */
124 void setAlpha(uint8_t a) {
125 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
126 }
127
128 /**
129 * Add a color filter that can be represented by a color and a mode. Applied
130 * after color-computing texture stages.
131 */
132 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000133 fCommon.fColorFilterColor = c;
134 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000135 }
136
bsalomon@google.comca432082013-01-23 19:53:46 +0000137 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
138 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000139
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000140 /**
141 * Constructor sets the color to be 'color' which is undone by the destructor.
142 */
143 class AutoColorRestore : public ::GrNoncopyable {
144 public:
145 AutoColorRestore(GrDrawState* drawState, GrColor color) {
146 fDrawState = drawState;
147 fOldColor = fDrawState->getColor();
148 fDrawState->setColor(color);
149 }
150 ~AutoColorRestore() {
151 fDrawState->setColor(fOldColor);
152 }
153 private:
154 GrDrawState* fDrawState;
155 GrColor fOldColor;
156 };
157
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000158 /// @}
159
160 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000161 /// @name Coverage
162 ////
163
164 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000166 * initial value (after construction or reset()) is 0xff. The constant
167 * coverage is ignored when per-vertex coverage is provided.
168 */
169 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000170 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000171 }
172
173 /**
174 * Version of above that specifies 4 channel per-vertex color. The value
175 * should be premultiplied.
176 */
177 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000178 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000179 }
180
181 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000182 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000183 }
184
185 /// @}
186
187 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000188 /// @name Effect Stages
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189 ////
190
bsalomon@google.comadc65362013-01-28 14:26:09 +0000191 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
192 fStages[stageIdx].setEffect(effect);
193 return effect;
194 }
195
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000196 /**
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000197 * Creates a GrSimpleTextureEffect.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000198 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000199 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000200 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000201 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000202 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000203 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000204 void createTextureEffect(int stageIdx,
205 GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000206 const SkMatrix& matrix,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000207 const GrTextureParams& params) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000208 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000209 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000210 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000211 }
212
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000213 bool stagesDisabled() {
214 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000215 if (NULL != fStages[i].getEffect()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000216 return false;
217 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000218 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000219 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000220 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000221
bsalomon@google.comadc65362013-01-28 14:26:09 +0000222 void disableStage(int stageIdx) { this->setEffect(stageIdx, NULL); }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000223
robertphillips@google.com972265d2012-06-13 18:49:30 +0000224 /**
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000225 * Release all the GrEffects referred to by this draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000226 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000227 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000228 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000229 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000230 }
231 }
232
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000233 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000234 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000235 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
236 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000237 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000238 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000239 }
240 }
241 private:
242 GrDrawState* fDrawState;
243 };
244
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000245 /**
bsalomon@google.com08283af2012-10-26 13:01:20 +0000246 * Returns the current stage by index.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000247 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000248 const GrEffectStage& getStage(int stageIdx) const {
249 GrAssert((unsigned)stageIdx < kNumStages);
250 return fStages[stageIdx];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000251 }
252
253 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000254 * Called when the source coord system is changing. preConcat gives the transformation from the
255 * old coord system to the new coord system.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000256 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000257 void preConcatStageMatrices(const SkMatrix& preConcat) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000258 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000259 if (this->isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000260 fStages[i].preConcatCoordChange(preConcat);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000261 }
262 }
263 }
264
bsalomon@google.come3d32162012-07-20 13:37:06 +0000265 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000266 * Called when the source coord system is changing. preConcatInverse is the inverse of the
267 * transformation from the old coord system to the new coord system. Returns false if the matrix
268 * cannot be inverted.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000269 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000270 bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
271 SkMatrix inv;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000272 bool computed = false;
273 for (int i = 0; i < kNumStages; ++i) {
274 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000275 if (!computed && !preConcatInverse.invert(&inv)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000276 return false;
277 } else {
278 computed = true;
279 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000280 fStages[i].preConcatCoordChange(preConcatInverse);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000281 }
282 }
283 return true;
284 }
285
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000286 /// @}
287
288 ///////////////////////////////////////////////////////////////////////////
289 /// @name Coverage / Color Stages
290 ////
291
292 /**
293 * A common pattern is to compute a color with the initial stages and then
294 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000295 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
296 * computed based on the pre-coverage-modulated color. The division of
297 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000298 * this method. Initially this is kNumStages (all stages
299 * are color-computing).
300 */
301 void setFirstCoverageStage(int firstCoverageStage) {
302 GrAssert((unsigned)firstCoverageStage <= kNumStages);
bsalomon@google.comca432082013-01-23 19:53:46 +0000303 fCommon.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000304 }
305
306 /**
307 * Gets the index of the first coverage-computing stage.
308 */
309 int getFirstCoverageStage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000310 return fCommon.fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000311 }
312
313 ///@}
314
315 ///////////////////////////////////////////////////////////////////////////
316 /// @name Blending
317 ////
318
319 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000320 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000321 *
322 * The blend function will be:
323 * D' = sat(S*srcCoef + D*dstCoef)
324 *
325 * where D is the existing destination color, S is the incoming source
326 * color, and D' is the new destination color that will be written. sat()
327 * is the saturation function.
328 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000329 * @param srcCoef coefficient applied to the src color.
330 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000331 */
332 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000333 fCommon.fSrcBlend = srcCoeff;
334 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000335 #if GR_DEBUG
336 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000337 case kDC_GrBlendCoeff:
338 case kIDC_GrBlendCoeff:
339 case kDA_GrBlendCoeff:
340 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000341 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
342 "coverage stages.\n");
343 break;
344 default:
345 break;
346 }
347 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000348 case kSC_GrBlendCoeff:
349 case kISC_GrBlendCoeff:
350 case kSA_GrBlendCoeff:
351 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000352 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
353 "coverage stages.\n");
354 break;
355 default:
356 break;
357 }
358 #endif
359 }
360
bsalomon@google.comca432082013-01-23 19:53:46 +0000361 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
362 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000363
364 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
365 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000366 *srcBlendCoeff = fCommon.fSrcBlend;
367 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000368 }
369
370 /**
371 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000372 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 * kConstC_GrBlendCoeff
374 * kIConstC_GrBlendCoeff
375 * kConstA_GrBlendCoeff
376 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000377 *
378 * @param constant the constant to set
379 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000380 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000381
382 /**
383 * Retrieves the last value set by setBlendConstant()
384 * @return the blending constant value
385 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000386 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000387
388 /// @}
389
390 ///////////////////////////////////////////////////////////////////////////
391 /// @name View Matrix
392 ////
393
394 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000395 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000396 *
397 * In the post-view-matrix space the rectangle [0,w]x[0,h]
398 * fully covers the render target. (w and h are the width and height of the
bsalomon@google.comca432082013-01-23 19:53:46 +0000399 * the render-target.)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000401 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000402
403 /**
404 * Gets a writable pointer to the view matrix.
405 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000406 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000407
408 /**
409 * Multiplies the current view matrix by a matrix
410 *
411 * After this call V' = V*m where V is the old view matrix,
412 * m is the parameter to this function, and V' is the new view matrix.
413 * (We consider positions to be column vectors so position vector p is
414 * transformed by matrix X as p' = X*p.)
415 *
416 * @param m the matrix used to modify the view matrix.
417 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000418 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000419
420 /**
421 * Multiplies the current view matrix by a matrix
422 *
423 * After this call V' = m*V where V is the old view matrix,
424 * m is the parameter to this function, and V' is the new view matrix.
425 * (We consider positions to be column vectors so position vector p is
426 * transformed by matrix X as p' = X*p.)
427 *
428 * @param m the matrix used to modify the view matrix.
429 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000430 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000431
432 /**
433 * Retrieves the current view matrix
434 * @return the current view matrix.
435 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000436 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437
438 /**
439 * Retrieves the inverse of the current view matrix.
440 *
441 * If the current view matrix is invertible, return true, and if matrix
442 * is non-null, copy the inverse into it. If the current view matrix is
443 * non-invertible, return false and ignore the matrix parameter.
444 *
445 * @param matrix if not null, will receive a copy of the current inverse.
446 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000447 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000448 // TODO: determine whether we really need to leave matrix unmodified
449 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000450 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000451 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000452 if (matrix) {
453 *matrix = inverse;
454 }
455 return true;
456 }
457 return false;
458 }
459
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000460 ////////////////////////////////////////////////////////////////////////////
461
462 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000463 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.comc196b522012-10-25 21:52:43 +0000464 * Effect matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000465 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 class AutoViewMatrixRestore : public ::GrNoncopyable {
467 public:
468 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000469
470 AutoViewMatrixRestore(GrDrawState* ds,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000471 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000472 uint32_t explicitCoordStageMask = 0) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000473 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000474 this->set(ds, preconcatMatrix, explicitCoordStageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000475 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000476
477 ~AutoViewMatrixRestore() { this->restore(); }
478
bsalomon@google.coma8347462012-10-08 18:59:39 +0000479 /**
480 * Can be called prior to destructor to restore the original matrix.
481 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000482 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000483
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000484 void set(GrDrawState* drawState,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000485 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000486 uint32_t explicitCoordStageMask = 0);
487
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000488 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000489
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000491 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000492 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000493 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000494 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000495 };
496
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000497 ////////////////////////////////////////////////////////////////////////////
498
499 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000500 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
501 * destructor undoes the changes, restoring the view matrix that was set before the
502 * constructor. It is similar to passing the inverse of the current view matrix to
503 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000504 */
505 class AutoDeviceCoordDraw : ::GrNoncopyable {
506 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000507 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000508 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000509 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
510 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
511 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000512 */
513 AutoDeviceCoordDraw(GrDrawState* drawState,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000514 uint32_t explicitCoordStageMask = 0) {
515 fDrawState = NULL;
516 this->set(drawState, explicitCoordStageMask);
517 }
518
bsalomon@google.coma8347462012-10-08 18:59:39 +0000519 ~AutoDeviceCoordDraw() { this->restore(); }
520
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000521 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
522
bsalomon@google.coma8347462012-10-08 18:59:39 +0000523 /**
524 * Returns true if this object was successfully initialized on to a GrDrawState. It may
525 * return false because a non-default constructor or set() were never called or because
526 * the view matrix was not invertible.
527 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000528 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000529
bsalomon@google.coma8347462012-10-08 18:59:39 +0000530 /**
531 * Returns the matrix that was set previously set on the drawState. This is only valid
532 * if succeeded returns true.
533 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000534 const SkMatrix& getOriginalMatrix() const {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000535 GrAssert(this->succeeded());
536 return fViewMatrix;
537 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000538
bsalomon@google.coma8347462012-10-08 18:59:39 +0000539 /**
540 * Can be called prior to destructor to restore the original matrix.
541 */
542 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000543
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000544 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000545 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000546 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000547 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000548 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000549 };
550
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 /// @}
552
553 ///////////////////////////////////////////////////////////////////////////
554 /// @name Render Target
555 ////
556
557 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000558 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000559 *
560 * @param target The render target to set.
561 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000562 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000563 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000564 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000565
566 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000567 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000568 *
569 * @return The currently set render target.
570 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000571 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
572 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000573
574 class AutoRenderTargetRestore : public ::GrNoncopyable {
575 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000576 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000577 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
578 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000579 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580 this->set(ds, newTarget);
581 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000582 ~AutoRenderTargetRestore() { this->restore(); }
583
584 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000585 if (NULL != fDrawState) {
586 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000587 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000589 GrSafeSetNull(fSavedTarget);
590 }
591
592 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
593 this->restore();
594
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000596 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000597 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000598 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000599 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000600 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000602 }
603 private:
604 GrDrawState* fDrawState;
605 GrRenderTarget* fSavedTarget;
606 };
607
608 /// @}
609
610 ///////////////////////////////////////////////////////////////////////////
611 /// @name Stencil
612 ////
613
614 /**
615 * Sets the stencil settings to use for the next draw.
616 * Changing the clip has the side-effect of possibly zeroing
617 * out the client settable stencil bits. So multipass algorithms
618 * using stencil should not change the clip between passes.
619 * @param settings the stencil settings to use.
620 */
621 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000622 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 }
624
625 /**
626 * Shortcut to disable stencil testing and ops.
627 */
628 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000629 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000630 }
631
bsalomon@google.comca432082013-01-23 19:53:46 +0000632 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000633
bsalomon@google.comca432082013-01-23 19:53:46 +0000634 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000635
636 /// @}
637
638 ///////////////////////////////////////////////////////////////////////////
639 // @name Edge AA
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000640 // Edge equations can be specified to perform anti-aliasing. Because the
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000641 // edges are specified as per-vertex data, vertices that are shared by
642 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000643 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000644 ////
645
646 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000647 * When specifying edges as vertex data this enum specifies what type of
bsalomon@google.com81712882012-11-01 17:12:34 +0000648 * edges are in use. The edges are always 4 SkScalars in memory, even when
tomhudson@google.com93813632011-10-27 20:21:16 +0000649 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000650 *
651 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
652 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000653 */
654 enum VertexEdgeType {
655 /* 1-pixel wide line
656 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
657 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000658 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000659 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000660 being inside, positive outside. Edge specified in window space
661 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000662 kQuad_EdgeType,
663 /* Same as above but for hairline quadratics. Uses unsigned distance.
664 Coverage is min(0, 1-distance). */
665 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000666 /* Circle specified as center_x, center_y, outer_radius, inner_radius
667 all in window space (y-down). */
668 kCircle_EdgeType,
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000669 /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius
670 all in window space (y-down). */
671 kEllipse_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000672
673 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000674 };
675
676 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000677 * Determines the interpretation per-vertex edge data when the
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
679 * are not specified the value of this setting has no effect.
680 */
681 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000682 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.comca432082013-01-23 19:53:46 +0000683 fCommon.fVertexEdgeType = type;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000684 }
685
bsalomon@google.comca432082013-01-23 19:53:46 +0000686 VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000687
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000689
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 ///////////////////////////////////////////////////////////////////////////
691 /// @name State Flags
692 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000693
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000694 /**
695 * Flags that affect rendering. Controlled using enable/disableState(). All
696 * default to disabled.
697 */
698 enum StateBits {
699 /**
700 * Perform dithering. TODO: Re-evaluate whether we need this bit
701 */
702 kDither_StateBit = 0x01,
703 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000704 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
705 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
706 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000707 */
708 kHWAntialias_StateBit = 0x02,
709 /**
710 * Draws will respect the clip, otherwise the clip is ignored.
711 */
712 kClip_StateBit = 0x04,
713 /**
714 * Disables writing to the color buffer. Useful when performing stencil
715 * operations.
716 */
717 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000718
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000719 /**
720 * Usually coverage is applied after color blending. The color is blended using the coeffs
721 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
722 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
723 * this case there is no distinction between coverage and color and the caller needs direct
724 * control over the blend coeffs. When set, there will be a single blend step controlled by
725 * setBlendFunc() which will use coverage*color as the src color.
726 */
727 kCoverageDrawing_StateBit = 0x10,
728
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() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000735 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000736 }
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) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000744 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000745 }
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) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000753 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000754 }
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 {
bsalomon@google.comca432082013-01-23 19:53:46 +0000771 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000772 }
773
774 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000775 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000776 }
777
778 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000779 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000780 }
781
782 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000783 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000784 }
785
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000786 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000787 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000788 }
789
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000790 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000791 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000792 }
793
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000794 /// @}
795
796 ///////////////////////////////////////////////////////////////////////////
797 /// @name Face Culling
798 ////
799
800 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000801 kInvalid_DrawFace = -1,
802
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803 kBoth_DrawFace,
804 kCCW_DrawFace,
805 kCW_DrawFace,
806 };
807
808 /**
809 * Controls whether clockwise, counterclockwise, or both faces are drawn.
810 * @param face the face(s) to draw.
811 */
812 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000813 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000814 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000815 }
816
817 /**
818 * Gets whether the target is drawing clockwise, counterclockwise,
819 * or both faces.
820 * @return the current draw face(s).
821 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000822 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000823
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 /// @}
825
826 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000827
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000828 bool isStageEnabled(int s) const {
829 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000830 return (NULL != fStages[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000831 }
832
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000833 // Most stages are usually not used, so conditionals here
834 // reduce the expected number of bytes touched by 50%.
835 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000836 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000837 return false;
838 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000839
840 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000841 bool enabled = this->isStageEnabled(i);
842 if (enabled != s.isStageEnabled(i)) {
843 return false;
844 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000845 if (enabled && this->fStages[i] != s.fStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000846 return false;
847 }
848 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000849 return true;
850 }
851 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
852
bsalomon@google.comca432082013-01-23 19:53:46 +0000853 GrDrawState& operator= (const GrDrawState& s) {
854 this->setRenderTarget(s.fRenderTarget.get());
855 fCommon = s.fCommon;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000856 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000857 if (s.isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000858 this->fStages[i] = s.fStages[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000859 }
860 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000861 return *this;
862 }
863
864private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000865
bsalomon@google.comca432082013-01-23 19:53:46 +0000866 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
867 struct CommonState {
868 // These fields are roughly sorted by decreasing likelihood of being different in op==
869 GrColor fColor;
870 SkMatrix fViewMatrix;
871 GrBlendCoeff fSrcBlend;
872 GrBlendCoeff fDstBlend;
873 GrColor fBlendConstant;
874 uint32_t fFlagBits;
875 VertexEdgeType fVertexEdgeType;
876 GrStencilSettings fStencilSettings;
877 int fFirstCoverageStage;
878 GrColor fCoverage;
879 SkXfermode::Mode fColorFilterMode;
880 GrColor fColorFilterColor;
881 DrawFace fDrawFace;
882 bool operator== (const CommonState& other) const {
883 return fColor == other.fColor &&
884 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
885 fSrcBlend == other.fSrcBlend &&
886 fDstBlend == other.fDstBlend &&
887 fBlendConstant == other.fBlendConstant &&
888 fFlagBits == other.fFlagBits &&
889 fVertexEdgeType == other.fVertexEdgeType &&
890 fStencilSettings == other.fStencilSettings &&
891 fFirstCoverageStage == other.fFirstCoverageStage &&
892 fCoverage == other.fCoverage &&
893 fColorFilterMode == other.fColorFilterMode &&
894 fColorFilterColor == other.fColorFilterColor &&
895 fDrawFace == other.fDrawFace;
896 }
897 bool operator!= (const CommonState& other) const { return !(*this == other); }
898 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000899
bsalomon@google.comca432082013-01-23 19:53:46 +0000900 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
901 DeferredState must directly reference GrEffects, however. */
902 struct SavedEffectStage {
903 SavedEffectStage() : fEffect(NULL) {}
904 const GrEffect* fEffect;
905 GrEffectStage::SavedCoordChange fCoordChange;
906 };
907
908public:
909 /**
910 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
911 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
912 * dispose mechanism returns them to the cache. This allows recycling resources through the
913 * the cache while they are in a deferred draw queue.
914 */
915 class DeferredState {
916 public:
917 DeferredState() : fRenderTarget(NULL) {
918 GR_DEBUGCODE(fInitialized = false;)
919 }
920 // TODO: Remove this when DeferredState no longer holds a ref to the RT
921 ~DeferredState() { SkSafeUnref(fRenderTarget); }
922
923 void saveFrom(const GrDrawState& drawState) {
924 fCommon = drawState.fCommon;
925 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
926 fRenderTarget = drawState.fRenderTarget.get();
927 SkSafeRef(fRenderTarget);
928 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
929 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
930 // and recycle them to the cache (if no one else is holding a ref to the resources).
931 for (int i = 0; i < kNumStages; ++i) {
932 fStages[i].saveFrom(drawState.fStages[i]);
933 }
934 GR_DEBUGCODE(fInitialized = true;)
935 }
936
937 void restoreTo(GrDrawState* drawState) {
938 GrAssert(fInitialized);
939 drawState->fCommon = fCommon;
940 drawState->setRenderTarget(fRenderTarget);
941 for (int i = 0; i < kNumStages; ++i) {
942 fStages[i].restoreTo(&drawState->fStages[i]);
943 }
944 }
945
946 bool isEqual(const GrDrawState& state) const {
947 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
948 return false;
949 }
950 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comdcd69bf2013-01-24 18:28:51 +0000951 if (!fStages[i].isEqual(state.fStages[i])) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000952 return false;
953 }
954 }
955 return true;
956 }
957
958 private:
959 GrRenderTarget* fRenderTarget;
960 CommonState fCommon;
961 GrEffectStage::DeferredStage fStages[kNumStages];
962
963 GR_DEBUGCODE(bool fInitialized;)
964 };
965
966private:
967 SkAutoTUnref<GrRenderTarget> fRenderTarget;
968 CommonState fCommon;
969 GrEffectStage fStages[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000970
reed@google.comfa35e3d2012-06-26 20:16:17 +0000971 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000972};
973
974#endif