blob: e70c38f154fda0577aa86e54299b1e9183c3aaa4 [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.comcabe00e2013-01-28 16:46:55 +0000258 this->preConcatStageMatrices(~0U, preConcat);
259 }
260 /**
261 * Version of above that applies the update matrix selectively to stages via a mask.
262 */
263 void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000264 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comcabe00e2013-01-28 16:46:55 +0000265 if (((1 << i) & stageMask) && this->isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000266 fStages[i].preConcatCoordChange(preConcat);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000267 }
268 }
269 }
270
bsalomon@google.come3d32162012-07-20 13:37:06 +0000271 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000272 * Called when the source coord system is changing. preConcatInverse is the inverse of the
273 * transformation from the old coord system to the new coord system. Returns false if the matrix
274 * cannot be inverted.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000275 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000276 bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
277 SkMatrix inv;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000278 bool computed = false;
279 for (int i = 0; i < kNumStages; ++i) {
280 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000281 if (!computed && !preConcatInverse.invert(&inv)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000282 return false;
283 } else {
284 computed = true;
285 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000286 fStages[i].preConcatCoordChange(preConcatInverse);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000287 }
288 }
289 return true;
290 }
291
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000292 /// @}
293
294 ///////////////////////////////////////////////////////////////////////////
295 /// @name Coverage / Color Stages
296 ////
297
298 /**
299 * A common pattern is to compute a color with the initial stages and then
300 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000301 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
302 * computed based on the pre-coverage-modulated color. The division of
303 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000304 * this method. Initially this is kNumStages (all stages
305 * are color-computing).
306 */
307 void setFirstCoverageStage(int firstCoverageStage) {
308 GrAssert((unsigned)firstCoverageStage <= kNumStages);
bsalomon@google.comca432082013-01-23 19:53:46 +0000309 fCommon.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000310 }
311
312 /**
313 * Gets the index of the first coverage-computing stage.
314 */
315 int getFirstCoverageStage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000316 return fCommon.fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000317 }
318
319 ///@}
320
321 ///////////////////////////////////////////////////////////////////////////
322 /// @name Blending
323 ////
324
325 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000326 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000327 *
328 * The blend function will be:
329 * D' = sat(S*srcCoef + D*dstCoef)
330 *
331 * where D is the existing destination color, S is the incoming source
332 * color, and D' is the new destination color that will be written. sat()
333 * is the saturation function.
334 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000335 * @param srcCoef coefficient applied to the src color.
336 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000337 */
338 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000339 fCommon.fSrcBlend = srcCoeff;
340 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000341 #if GR_DEBUG
342 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000343 case kDC_GrBlendCoeff:
344 case kIDC_GrBlendCoeff:
345 case kDA_GrBlendCoeff:
346 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000347 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
348 "coverage stages.\n");
349 break;
350 default:
351 break;
352 }
353 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000354 case kSC_GrBlendCoeff:
355 case kISC_GrBlendCoeff:
356 case kSA_GrBlendCoeff:
357 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000358 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
359 "coverage stages.\n");
360 break;
361 default:
362 break;
363 }
364 #endif
365 }
366
bsalomon@google.comca432082013-01-23 19:53:46 +0000367 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
368 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000369
370 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
371 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000372 *srcBlendCoeff = fCommon.fSrcBlend;
373 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000374 }
375
376 /**
377 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000378 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000379 * kConstC_GrBlendCoeff
380 * kIConstC_GrBlendCoeff
381 * kConstA_GrBlendCoeff
382 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000383 *
384 * @param constant the constant to set
385 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000386 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000387
388 /**
389 * Retrieves the last value set by setBlendConstant()
390 * @return the blending constant value
391 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000392 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000393
394 /// @}
395
396 ///////////////////////////////////////////////////////////////////////////
397 /// @name View Matrix
398 ////
399
400 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000401 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000402 *
403 * In the post-view-matrix space the rectangle [0,w]x[0,h]
404 * fully covers the render target. (w and h are the width and height of the
bsalomon@google.comca432082013-01-23 19:53:46 +0000405 * the render-target.)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000406 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000407 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000408
409 /**
410 * Gets a writable pointer to the view matrix.
411 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000412 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000413
414 /**
415 * Multiplies the current view matrix by a matrix
416 *
417 * After this call V' = V*m where V is the old view matrix,
418 * m is the parameter to this function, and V' is the new view matrix.
419 * (We consider positions to be column vectors so position vector p is
420 * transformed by matrix X as p' = X*p.)
421 *
422 * @param m the matrix used to modify the view matrix.
423 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000424 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000425
426 /**
427 * Multiplies the current view matrix by a matrix
428 *
429 * After this call V' = m*V where V is the old view matrix,
430 * m is the parameter to this function, and V' is the new view matrix.
431 * (We consider positions to be column vectors so position vector p is
432 * transformed by matrix X as p' = X*p.)
433 *
434 * @param m the matrix used to modify the view matrix.
435 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000436 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437
438 /**
439 * Retrieves the current view matrix
440 * @return the current view matrix.
441 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000442 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000443
444 /**
445 * Retrieves the inverse of the current view matrix.
446 *
447 * If the current view matrix is invertible, return true, and if matrix
448 * is non-null, copy the inverse into it. If the current view matrix is
449 * non-invertible, return false and ignore the matrix parameter.
450 *
451 * @param matrix if not null, will receive a copy of the current inverse.
452 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000453 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000454 // TODO: determine whether we really need to leave matrix unmodified
455 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000456 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000457 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000458 if (matrix) {
459 *matrix = inverse;
460 }
461 return true;
462 }
463 return false;
464 }
465
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000466 ////////////////////////////////////////////////////////////////////////////
467
468 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000469 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.comc196b522012-10-25 21:52:43 +0000470 * Effect matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000471 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000472 class AutoViewMatrixRestore : public ::GrNoncopyable {
473 public:
474 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000475
476 AutoViewMatrixRestore(GrDrawState* ds,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000477 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000478 uint32_t explicitCoordStageMask = 0) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000479 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000480 this->set(ds, preconcatMatrix, explicitCoordStageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000482
483 ~AutoViewMatrixRestore() { this->restore(); }
484
bsalomon@google.coma8347462012-10-08 18:59:39 +0000485 /**
486 * Can be called prior to destructor to restore the original matrix.
487 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000488 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000489
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000490 void set(GrDrawState* drawState,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000491 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000492 uint32_t explicitCoordStageMask = 0);
493
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000494 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000495
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000496 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000497 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000498 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000499 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000500 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000501 };
502
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000503 ////////////////////////////////////////////////////////////////////////////
504
505 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000506 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
507 * destructor undoes the changes, restoring the view matrix that was set before the
508 * constructor. It is similar to passing the inverse of the current view matrix to
509 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000510 */
511 class AutoDeviceCoordDraw : ::GrNoncopyable {
512 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000513 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000514 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000515 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
516 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
517 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000518 */
519 AutoDeviceCoordDraw(GrDrawState* drawState,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000520 uint32_t explicitCoordStageMask = 0) {
521 fDrawState = NULL;
522 this->set(drawState, explicitCoordStageMask);
523 }
524
bsalomon@google.coma8347462012-10-08 18:59:39 +0000525 ~AutoDeviceCoordDraw() { this->restore(); }
526
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000527 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
528
bsalomon@google.coma8347462012-10-08 18:59:39 +0000529 /**
530 * Returns true if this object was successfully initialized on to a GrDrawState. It may
531 * return false because a non-default constructor or set() were never called or because
532 * the view matrix was not invertible.
533 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000534 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000535
bsalomon@google.coma8347462012-10-08 18:59:39 +0000536 /**
537 * Returns the matrix that was set previously set on the drawState. This is only valid
538 * if succeeded returns true.
539 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000540 const SkMatrix& getOriginalMatrix() const {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000541 GrAssert(this->succeeded());
542 return fViewMatrix;
543 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000544
bsalomon@google.coma8347462012-10-08 18:59:39 +0000545 /**
546 * Can be called prior to destructor to restore the original matrix.
547 */
548 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000549
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000550 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000551 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000552 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000553 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000554 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000555 };
556
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000557 /// @}
558
559 ///////////////////////////////////////////////////////////////////////////
560 /// @name Render Target
561 ////
562
563 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000564 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000565 *
566 * @param target The render target to set.
567 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000568 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000569 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000570 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000571
572 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000573 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000574 *
575 * @return The currently set render target.
576 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000577 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
578 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000579
580 class AutoRenderTargetRestore : public ::GrNoncopyable {
581 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000582 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000583 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
584 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000585 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586 this->set(ds, newTarget);
587 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000588 ~AutoRenderTargetRestore() { this->restore(); }
589
590 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000591 if (NULL != fDrawState) {
592 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000593 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000594 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000595 GrSafeSetNull(fSavedTarget);
596 }
597
598 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
599 this->restore();
600
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000602 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000604 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000606 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000607 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608 }
609 private:
610 GrDrawState* fDrawState;
611 GrRenderTarget* fSavedTarget;
612 };
613
614 /// @}
615
616 ///////////////////////////////////////////////////////////////////////////
617 /// @name Stencil
618 ////
619
620 /**
621 * Sets the stencil settings to use for the next draw.
622 * Changing the clip has the side-effect of possibly zeroing
623 * out the client settable stencil bits. So multipass algorithms
624 * using stencil should not change the clip between passes.
625 * @param settings the stencil settings to use.
626 */
627 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000628 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000629 }
630
631 /**
632 * Shortcut to disable stencil testing and ops.
633 */
634 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000635 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000636 }
637
bsalomon@google.comca432082013-01-23 19:53:46 +0000638 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000639
bsalomon@google.comca432082013-01-23 19:53:46 +0000640 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000641
642 /// @}
643
644 ///////////////////////////////////////////////////////////////////////////
645 // @name Edge AA
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000646 // Edge equations can be specified to perform anti-aliasing. Because the
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000647 // edges are specified as per-vertex data, vertices that are shared by
648 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000649 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 ////
651
652 /**
tomhudson@google.com93813632011-10-27 20:21:16 +0000653 * When specifying edges as vertex data this enum specifies what type of
bsalomon@google.com81712882012-11-01 17:12:34 +0000654 * edges are in use. The edges are always 4 SkScalars in memory, even when
tomhudson@google.com93813632011-10-27 20:21:16 +0000655 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +0000656 *
657 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
658 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +0000659 */
660 enum VertexEdgeType {
661 /* 1-pixel wide line
662 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
663 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000664 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000665 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +0000666 being inside, positive outside. Edge specified in window space
667 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000668 kQuad_EdgeType,
669 /* Same as above but for hairline quadratics. Uses unsigned distance.
670 Coverage is min(0, 1-distance). */
671 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000672 /* Circle specified as center_x, center_y, outer_radius, inner_radius
673 all in window space (y-down). */
674 kCircle_EdgeType,
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000675 /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius
676 all in window space (y-down). */
677 kEllipse_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000678
679 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +0000680 };
681
682 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000683 * Determines the interpretation per-vertex edge data when the
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000684 * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
685 * are not specified the value of this setting has no effect.
686 */
687 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000688 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.comca432082013-01-23 19:53:46 +0000689 fCommon.fVertexEdgeType = type;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 }
691
bsalomon@google.comca432082013-01-23 19:53:46 +0000692 VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000693
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000694 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +0000695
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000696 ///////////////////////////////////////////////////////////////////////////
697 /// @name State Flags
698 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000699
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 /**
701 * Flags that affect rendering. Controlled using enable/disableState(). All
702 * default to disabled.
703 */
704 enum StateBits {
705 /**
706 * Perform dithering. TODO: Re-evaluate whether we need this bit
707 */
708 kDither_StateBit = 0x01,
709 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000710 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
711 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
712 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000713 */
714 kHWAntialias_StateBit = 0x02,
715 /**
716 * Draws will respect the clip, otherwise the clip is ignored.
717 */
718 kClip_StateBit = 0x04,
719 /**
720 * Disables writing to the color buffer. Useful when performing stencil
721 * operations.
722 */
723 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000724
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000725 /**
726 * Usually coverage is applied after color blending. The color is blended using the coeffs
727 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
728 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
729 * this case there is no distinction between coverage and color and the caller needs direct
730 * control over the blend coeffs. When set, there will be a single blend step controlled by
731 * setBlendFunc() which will use coverage*color as the src color.
732 */
733 kCoverageDrawing_StateBit = 0x10,
734
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000735 // Users of the class may add additional bits to the vector
736 kDummyStateBit,
737 kLastPublicStateBit = kDummyStateBit-1,
738 };
739
740 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000741 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 }
743
744 /**
745 * Enable render state settings.
746 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000747 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000748 */
749 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000750 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000751 }
752
753 /**
754 * Disable render state settings.
755 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000756 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000757 */
758 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000759 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000760 }
761
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000762 /**
763 * Enable or disable stateBits based on a boolean.
764 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000765 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000766 * @param enable if true enable stateBits, otherwise disable
767 */
768 void setState(uint32_t stateBits, bool enable) {
769 if (enable) {
770 this->enableState(stateBits);
771 } else {
772 this->disableState(stateBits);
773 }
774 }
775
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000776 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000777 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000778 }
779
780 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000781 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000782 }
783
784 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000785 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000786 }
787
788 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000789 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000790 }
791
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000792 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000793 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000794 }
795
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000796 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000797 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000798 }
799
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 /// @}
801
802 ///////////////////////////////////////////////////////////////////////////
803 /// @name Face Culling
804 ////
805
806 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000807 kInvalid_DrawFace = -1,
808
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000809 kBoth_DrawFace,
810 kCCW_DrawFace,
811 kCW_DrawFace,
812 };
813
814 /**
815 * Controls whether clockwise, counterclockwise, or both faces are drawn.
816 * @param face the face(s) to draw.
817 */
818 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000819 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000820 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 }
822
823 /**
824 * Gets whether the target is drawing clockwise, counterclockwise,
825 * or both faces.
826 * @return the current draw face(s).
827 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000828 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000829
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 /// @}
831
832 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000833
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000834 bool isStageEnabled(int s) const {
835 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000836 return (NULL != fStages[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000837 }
838
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000839 // Most stages are usually not used, so conditionals here
840 // reduce the expected number of bytes touched by 50%.
841 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000842 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000843 return false;
844 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000845
846 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000847 bool enabled = this->isStageEnabled(i);
848 if (enabled != s.isStageEnabled(i)) {
849 return false;
850 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000851 if (enabled && this->fStages[i] != s.fStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000852 return false;
853 }
854 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000855 return true;
856 }
857 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
858
bsalomon@google.comca432082013-01-23 19:53:46 +0000859 GrDrawState& operator= (const GrDrawState& s) {
860 this->setRenderTarget(s.fRenderTarget.get());
861 fCommon = s.fCommon;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000862 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000863 if (s.isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000864 this->fStages[i] = s.fStages[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000865 }
866 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000867 return *this;
868 }
869
870private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000871
bsalomon@google.comca432082013-01-23 19:53:46 +0000872 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
873 struct CommonState {
874 // These fields are roughly sorted by decreasing likelihood of being different in op==
875 GrColor fColor;
876 SkMatrix fViewMatrix;
877 GrBlendCoeff fSrcBlend;
878 GrBlendCoeff fDstBlend;
879 GrColor fBlendConstant;
880 uint32_t fFlagBits;
881 VertexEdgeType fVertexEdgeType;
882 GrStencilSettings fStencilSettings;
883 int fFirstCoverageStage;
884 GrColor fCoverage;
885 SkXfermode::Mode fColorFilterMode;
886 GrColor fColorFilterColor;
887 DrawFace fDrawFace;
888 bool operator== (const CommonState& other) const {
889 return fColor == other.fColor &&
890 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
891 fSrcBlend == other.fSrcBlend &&
892 fDstBlend == other.fDstBlend &&
893 fBlendConstant == other.fBlendConstant &&
894 fFlagBits == other.fFlagBits &&
895 fVertexEdgeType == other.fVertexEdgeType &&
896 fStencilSettings == other.fStencilSettings &&
897 fFirstCoverageStage == other.fFirstCoverageStage &&
898 fCoverage == other.fCoverage &&
899 fColorFilterMode == other.fColorFilterMode &&
900 fColorFilterColor == other.fColorFilterColor &&
901 fDrawFace == other.fDrawFace;
902 }
903 bool operator!= (const CommonState& other) const { return !(*this == other); }
904 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000905
bsalomon@google.comca432082013-01-23 19:53:46 +0000906 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
907 DeferredState must directly reference GrEffects, however. */
908 struct SavedEffectStage {
909 SavedEffectStage() : fEffect(NULL) {}
910 const GrEffect* fEffect;
911 GrEffectStage::SavedCoordChange fCoordChange;
912 };
913
914public:
915 /**
916 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
917 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
918 * dispose mechanism returns them to the cache. This allows recycling resources through the
919 * the cache while they are in a deferred draw queue.
920 */
921 class DeferredState {
922 public:
923 DeferredState() : fRenderTarget(NULL) {
924 GR_DEBUGCODE(fInitialized = false;)
925 }
926 // TODO: Remove this when DeferredState no longer holds a ref to the RT
927 ~DeferredState() { SkSafeUnref(fRenderTarget); }
928
929 void saveFrom(const GrDrawState& drawState) {
930 fCommon = drawState.fCommon;
931 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
932 fRenderTarget = drawState.fRenderTarget.get();
933 SkSafeRef(fRenderTarget);
934 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
935 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
936 // and recycle them to the cache (if no one else is holding a ref to the resources).
937 for (int i = 0; i < kNumStages; ++i) {
938 fStages[i].saveFrom(drawState.fStages[i]);
939 }
940 GR_DEBUGCODE(fInitialized = true;)
941 }
942
943 void restoreTo(GrDrawState* drawState) {
944 GrAssert(fInitialized);
945 drawState->fCommon = fCommon;
946 drawState->setRenderTarget(fRenderTarget);
947 for (int i = 0; i < kNumStages; ++i) {
948 fStages[i].restoreTo(&drawState->fStages[i]);
949 }
950 }
951
952 bool isEqual(const GrDrawState& state) const {
953 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
954 return false;
955 }
956 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comdcd69bf2013-01-24 18:28:51 +0000957 if (!fStages[i].isEqual(state.fStages[i])) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000958 return false;
959 }
960 }
961 return true;
962 }
963
964 private:
965 GrRenderTarget* fRenderTarget;
966 CommonState fCommon;
967 GrEffectStage::DeferredStage fStages[kNumStages];
968
969 GR_DEBUGCODE(bool fInitialized;)
970 };
971
972private:
973 SkAutoTUnref<GrRenderTarget> fRenderTarget;
974 CommonState fCommon;
975 GrEffectStage fStages[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000976
reed@google.comfa35e3d2012-06-26 20:16:17 +0000977 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +0000978};
979
980#endif