blob: bf3ef74e9411ca6c088889c5db904e54116c958e [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.com08283af2012-10-26 13:01:20 +000013#include "GrEffectStage.h"
bsalomon@google.com73818dc2013-03-28 13:23:29 +000014#include "GrPaint.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000015#include "GrRefCnt.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000016#include "GrRenderTarget.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000017#include "GrStencil.h"
18#include "GrTemplates.h"
19#include "GrTexture.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000020#include "GrTypesPriv.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000021#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000022
jvanverth@google.comcc782382013-01-28 20:39:48 +000023#include "SkMatrix.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000024#include "SkXfermode.h"
25
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000026class GrDrawState : public GrRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000027public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000028 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000029
tomhudson@google.com93813632011-10-27 20:21:16 +000030 /**
bsalomon@google.com13221342012-10-26 13:41:59 +000031 * Total number of effect stages. Each stage can host a GrEffect. A stage is enabled if it has a
32 * GrEffect. The effect produces an output color in the fragment shader. It's inputs are the
33 * output from the previous enabled stage and a position. The position is either derived from
34 * the interpolated vertex positions or explicit per-vertex coords, depending upon the
jvanverth@google.com9b855c72013-03-01 18:21:22 +000035 * GrAttribBindings used to draw.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000036 *
bsalomon@google.com13221342012-10-26 13:41:59 +000037 * The stages are divided into two sets, color-computing and coverage-computing. The final color
38 * stage produces the final pixel color. The coverage-computing stages function exactly as the
39 * color-computing but the output of the final coverage stage is treated as a fractional pixel
40 * coverage rather than as input to the src/dst color blend step.
41 *
42 * The input color to the first enabled color-stage is either the constant color or interpolated
jvanverth@google.com054ae992013-04-01 20:06:51 +000043 * per-vertex colors. The input to the first coverage stage is either a constant coverage
44 * (usually full-coverage) or interpolated per-vertex coverage.
bsalomon@google.com13221342012-10-26 13:41:59 +000045 *
bsalomon@google.comcf939ae2012-12-13 19:59:23 +000046 * See the documentation of kCoverageDrawing_StateBit for information about disabling the
47 * the color / coverage distinction.
48 *
bsalomon@google.com13221342012-10-26 13:41:59 +000049 * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied from the client's
bsalomon@google.com73818dc2013-03-28 13:23:29 +000050 * GrPaint. Stage GrPaint::kTotalStages is earmarked for use by GrTextContext, GrPathRenderer-
51 * derived classes, and the rect/oval helper classes. GrPaint::kTotalStages+1 is earmarked for
52 * clipping by GrClipMaskManager. TODO: replace fixed size array of stages with variable size
53 * arrays of color and coverage stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000054 */
55 enum {
bsalomon@google.com73818dc2013-03-28 13:23:29 +000056 kNumStages = GrPaint::kTotalStages + 2,
tomhudson@google.com93813632011-10-27 20:21:16 +000057 };
58
bsalomon@google.comca432082013-01-23 19:53:46 +000059 GrDrawState() {
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000060 this->reset();
61 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000062
bsalomon@google.comca432082013-01-23 19:53:46 +000063 GrDrawState(const GrDrawState& state) {
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000064 *this = state;
65 }
66
robertphillips@google.com9ec07532012-06-22 12:01:30 +000067 virtual ~GrDrawState() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000068 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000069 }
70
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000071 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000072 * Resets to the default state.
bsalomon@google.com08283af2012-10-26 13:01:20 +000073 * GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000074 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000075 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +000076
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +000077 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +000078
bsalomon@google.comca432082013-01-23 19:53:46 +000079 fRenderTarget.reset(NULL);
80
jvanverth@google.com9b855c72013-03-01 18:21:22 +000081 this->setDefaultVertexAttribs();
82
bsalomon@google.comca432082013-01-23 19:53:46 +000083 fCommon.fColor = 0xffffffff;
84 fCommon.fViewMatrix.reset();
85 fCommon.fSrcBlend = kOne_GrBlendCoeff;
86 fCommon.fDstBlend = kZero_GrBlendCoeff;
87 fCommon.fBlendConstant = 0x0;
88 fCommon.fFlagBits = 0x0;
bsalomon@google.comca432082013-01-23 19:53:46 +000089 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 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000098 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
99 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
100 * equivalents are set to default values. GrPaint has fewer stages than GrDrawState. The extra
101 * GrDrawState stages are disabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000102 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000103 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000104
105 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000106 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +0000107 ////
108
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000109 enum {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000110 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000111 };
112
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000113 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000114 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
115 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
116 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000117 *
jvanverth@google.com054ae992013-04-01 20:06:51 +0000118 * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when
119 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000120 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000121
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000122 /**
robertphillips@google.com42903302013-04-20 12:26:07 +0000123 * Sets vertex attributes for next draw. The object driving the templatization
124 * should be a global GrVertexAttrib array that is never changed.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000125 */
robertphillips@google.com42903302013-04-20 12:26:07 +0000126 template <const GrVertexAttrib A[]> void setVertexAttribs(int count) {
127 this->setVertexAttribs(A, count);
128 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000129
robertphillips@google.com42903302013-04-20 12:26:07 +0000130 const GrVertexAttrib* getVertexAttribs() const { return fCommon.fVAPtr; }
131 int getVertexAttribCount() const { return fCommon.fVACount; }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000132
133 size_t getVertexSize() const;
134
135 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000136 * Sets default vertex attributes for next draw. The default is a single attribute:
137 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000138 */
139 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000140
jvanverth@google.com054ae992013-04-01 20:06:51 +0000141 /**
142 * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the
143 * binding does not appear in the current attribs. These bindings should appear only once in
144 * the attrib array.
145 */
146
147 int positionAttributeIndex() const {
148 return fCommon.fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
149 }
150 int localCoordAttributeIndex() const {
151 return fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
152 }
153 int colorVertexAttributeIndex() const {
154 return fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
155 }
156 int coverageVertexAttributeIndex() const {
157 return fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
158 }
159
160 bool hasLocalCoordAttribute() const {
161 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
162 }
163 bool hasColorVertexAttribute() const {
164 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
165 }
166 bool hasCoverageVertexAttribute() const {
167 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
168 }
169
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000170 bool validateVertexAttribs() const;
171
jvanverth@google.comcc782382013-01-28 20:39:48 +0000172 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000173 * Helper to save/restore vertex attribs
174 */
175 class AutoVertexAttribRestore {
176 public:
177 AutoVertexAttribRestore(GrDrawState* drawState) {
178 GrAssert(NULL != drawState);
179 fDrawState = drawState;
robertphillips@google.com42903302013-04-20 12:26:07 +0000180 fVAPtr = drawState->fCommon.fVAPtr;
181 fVACount = drawState->fCommon.fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000182 fDrawState->setDefaultVertexAttribs();
183 }
184
185 ~AutoVertexAttribRestore(){
robertphillips@google.com42903302013-04-20 12:26:07 +0000186 fDrawState->fCommon.fVAPtr = fVAPtr;
187 fDrawState->fCommon.fVACount = fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000188 }
189
190 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000191 GrDrawState* fDrawState;
192 const GrVertexAttrib* fVAPtr;
193 int fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000194 };
195
196 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000197 * Accessing positions, local coords, or colors, of a vertex within an array is a hassle
198 * involving casts and simple math. These helpers exist to keep GrDrawTarget clients' code a bit
199 * nicer looking.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000200 */
201
202 /**
203 * Gets a pointer to a GrPoint of a vertex's position or texture
204 * coordinate.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000205 * @param vertices the vertex array
jvanverth@google.comcc782382013-01-28 20:39:48 +0000206 * @param vertexIndex the index of the vertex in the array
207 * @param vertexSize the size of each vertex in the array
208 * @param offset the offset in bytes of the vertex component.
209 * Defaults to zero (corresponding to vertex position)
210 * @return pointer to the vertex component as a GrPoint
211 */
212 static GrPoint* GetVertexPoint(void* vertices,
213 int vertexIndex,
214 int vertexSize,
215 int offset = 0) {
216 intptr_t start = GrTCast<intptr_t>(vertices);
217 return GrTCast<GrPoint*>(start + offset +
218 vertexIndex * vertexSize);
219 }
220 static const GrPoint* GetVertexPoint(const void* vertices,
221 int vertexIndex,
222 int vertexSize,
223 int offset = 0) {
224 intptr_t start = GrTCast<intptr_t>(vertices);
225 return GrTCast<const GrPoint*>(start + offset +
226 vertexIndex * vertexSize);
227 }
228
229 /**
230 * Gets a pointer to a GrColor inside a vertex within a vertex array.
231 * @param vertices the vetex array
232 * @param vertexIndex the index of the vertex in the array
233 * @param vertexSize the size of each vertex in the array
234 * @param offset the offset in bytes of the vertex color
235 * @return pointer to the vertex component as a GrColor
236 */
237 static GrColor* GetVertexColor(void* vertices,
238 int vertexIndex,
239 int vertexSize,
240 int offset) {
241 intptr_t start = GrTCast<intptr_t>(vertices);
242 return GrTCast<GrColor*>(start + offset +
243 vertexIndex * vertexSize);
244 }
245 static const GrColor* GetVertexColor(const void* vertices,
246 int vertexIndex,
247 int vertexSize,
248 int offset) {
249 const intptr_t start = GrTCast<intptr_t>(vertices);
250 return GrTCast<const GrColor*>(start + offset +
251 vertexIndex * vertexSize);
252 }
253
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000254 /// @}
255
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000256 /**
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000257 * Determines whether src alpha is guaranteed to be one for all src pixels
258 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000259 bool srcAlphaWillBeOne() const;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000260
261 /**
262 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
263 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000264 bool hasSolidCoverage() const;
jvanverth@google.comcc782382013-01-28 20:39:48 +0000265
266 /// @}
267
268 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000269 /// @name Color
270 ////
271
272 /**
273 * Sets color for next draw to a premultiplied-alpha color.
274 *
275 * @param color the color to set.
276 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000277 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000278
bsalomon@google.comca432082013-01-23 19:53:46 +0000279 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000280
281 /**
282 * Sets the color to be used for the next draw to be
283 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
284 *
285 * @param alpha The alpha value to set as the color.
286 */
287 void setAlpha(uint8_t a) {
288 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
289 }
290
291 /**
292 * Add a color filter that can be represented by a color and a mode. Applied
bsalomon@google.comc7818882013-03-20 19:19:53 +0000293 * after color-computing effect stages.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000294 */
295 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000296 fCommon.fColorFilterColor = c;
297 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000298 }
299
bsalomon@google.comca432082013-01-23 19:53:46 +0000300 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
301 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000302
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000303 /**
304 * Constructor sets the color to be 'color' which is undone by the destructor.
305 */
306 class AutoColorRestore : public ::GrNoncopyable {
307 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000308 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000309
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000310 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000311 fDrawState = NULL;
312 this->set(drawState, color);
313 }
314
315 void reset() {
316 if (NULL != fDrawState) {
317 fDrawState->setColor(fOldColor);
318 fDrawState = NULL;
319 }
320 }
321
322 void set(GrDrawState* drawState, GrColor color) {
323 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000324 fDrawState = drawState;
325 fOldColor = fDrawState->getColor();
326 fDrawState->setColor(color);
327 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000328
329 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000330 private:
331 GrDrawState* fDrawState;
332 GrColor fOldColor;
333 };
334
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000335 /// @}
336
337 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000338 /// @name Coverage
339 ////
340
341 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000342 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000343 * initial value (after construction or reset()) is 0xff. The constant
344 * coverage is ignored when per-vertex coverage is provided.
345 */
346 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000347 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000348 }
349
350 /**
351 * Version of above that specifies 4 channel per-vertex color. The value
352 * should be premultiplied.
353 */
354 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000355 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000356 }
357
358 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000359 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000360 }
361
362 /// @}
363
364 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000365 /// @name Effect Stages
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000366 ////
367
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000368 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
369 fStages[stageIdx].setEffect(effect);
370 return effect;
371 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000372
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000373 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect,
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000374 int attr0, int attr1 = -1) {
375 fStages[stageIdx].setEffect(effect, attr0, attr1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000376 return effect;
377 }
378
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000379 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000380 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000381 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000382 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000383 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000384 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000385 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000386 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000387 void createTextureEffect(int stageIdx,
388 GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000389 const SkMatrix& matrix,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000390 const GrTextureParams& params) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000391 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000392 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000393 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000394 }
395
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000396 bool stagesDisabled() {
397 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000398 if (NULL != fStages[i].getEffect()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000399 return false;
400 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000401 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000402 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000403 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000404
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000405 void disableStage(int stageIdx) {
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000406 this->setEffect(stageIdx, NULL);
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000407 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000408
robertphillips@google.com972265d2012-06-13 18:49:30 +0000409 /**
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000410 * Release all the GrEffects referred to by this draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000411 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000412 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000413 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000414 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000415 }
416 }
417
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000418 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000419 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000420 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
421 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000422 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000423 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000424 }
425 }
426 private:
427 GrDrawState* fDrawState;
428 };
429
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000430 /**
bsalomon@google.com08283af2012-10-26 13:01:20 +0000431 * Returns the current stage by index.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000432 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000433 const GrEffectStage& getStage(int stageIdx) const {
434 GrAssert((unsigned)stageIdx < kNumStages);
435 return fStages[stageIdx];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000436 }
437
438 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000439 * Called when the source coord system is changing. This ensures that effects will see the
440 * correct local coordinates. oldToNew gives the transformation from the old coord system in
441 * which the geometry was specified to the new coordinate system from which it will be rendered.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000442 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000443 void localCoordChange(const SkMatrix& oldToNew) {
reed@google.com67e7cde2013-03-20 17:47:16 +0000444 for (int i = 0; i < kNumStages; ++i) {
445 if (this->isStageEnabled(i)) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000446 fStages[i].localCoordChange(oldToNew);
reed@google.com67e7cde2013-03-20 17:47:16 +0000447 }
448 }
reed@google.com67e7cde2013-03-20 17:47:16 +0000449 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000450
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000451 /**
452 * Checks whether any of the effects will read the dst pixel color.
453 */
bsalomon@google.comd09ab842013-05-15 17:30:26 +0000454 bool willEffectReadDstColor() const;
reed@google.com67e7cde2013-03-20 17:47:16 +0000455
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000456 /// @}
457
458 ///////////////////////////////////////////////////////////////////////////
459 /// @name Coverage / Color Stages
460 ////
461
462 /**
463 * A common pattern is to compute a color with the initial stages and then
464 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000465 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
466 * computed based on the pre-coverage-modulated color. The division of
467 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000468 * this method. Initially this is kNumStages (all stages
469 * are color-computing).
470 */
471 void setFirstCoverageStage(int firstCoverageStage) {
472 GrAssert((unsigned)firstCoverageStage <= kNumStages);
bsalomon@google.comca432082013-01-23 19:53:46 +0000473 fCommon.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000474 }
475
476 /**
477 * Gets the index of the first coverage-computing stage.
478 */
479 int getFirstCoverageStage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000480 return fCommon.fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 }
482
483 ///@}
484
485 ///////////////////////////////////////////////////////////////////////////
486 /// @name Blending
487 ////
488
489 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000490 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000491 *
492 * The blend function will be:
493 * D' = sat(S*srcCoef + D*dstCoef)
494 *
495 * where D is the existing destination color, S is the incoming source
496 * color, and D' is the new destination color that will be written. sat()
497 * is the saturation function.
498 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000499 * @param srcCoef coefficient applied to the src color.
500 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000501 */
502 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000503 fCommon.fSrcBlend = srcCoeff;
504 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 #if GR_DEBUG
506 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000507 case kDC_GrBlendCoeff:
508 case kIDC_GrBlendCoeff:
509 case kDA_GrBlendCoeff:
510 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
512 "coverage stages.\n");
513 break;
514 default:
515 break;
516 }
517 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000518 case kSC_GrBlendCoeff:
519 case kISC_GrBlendCoeff:
520 case kSA_GrBlendCoeff:
521 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000522 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
523 "coverage stages.\n");
524 break;
525 default:
526 break;
527 }
528 #endif
529 }
530
bsalomon@google.comca432082013-01-23 19:53:46 +0000531 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
532 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533
534 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
535 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000536 *srcBlendCoeff = fCommon.fSrcBlend;
537 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 }
539
540 /**
541 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000542 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000543 * kConstC_GrBlendCoeff
544 * kIConstC_GrBlendCoeff
545 * kConstA_GrBlendCoeff
546 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547 *
548 * @param constant the constant to set
549 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000550 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551
552 /**
553 * Retrieves the last value set by setBlendConstant()
554 * @return the blending constant value
555 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000556 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000557
bsalomon@google.com2b446732013-02-12 16:47:41 +0000558 /**
559 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
560 * coverage before the blend will give the correct final destination color. In general it
561 * will not as coverage is applied after blending.
562 */
563 bool canTweakAlphaForCoverage() const;
564
565 /**
566 * Optimizations for blending / coverage to that can be applied based on the current state.
567 */
568 enum BlendOptFlags {
569 /**
570 * No optimization
571 */
572 kNone_BlendOpt = 0,
573 /**
574 * Don't draw at all
575 */
576 kSkipDraw_BlendOptFlag = 0x1,
577 /**
578 * Emit the src color, disable HW blending (replace dst with src)
579 */
580 kDisableBlend_BlendOptFlag = 0x2,
581 /**
582 * The coverage value does not have to be computed separately from alpha, the the output
583 * color can be the modulation of the two.
584 */
585 kCoverageAsAlpha_BlendOptFlag = 0x4,
586 /**
587 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
588 * "don't cares".
589 */
590 kEmitCoverage_BlendOptFlag = 0x8,
591 /**
592 * Emit transparent black instead of the src color, no need to compute coverage.
593 */
594 kEmitTransBlack_BlendOptFlag = 0x10,
595 };
596 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
597
598 /**
599 * Determines what optimizations can be applied based on the blend. The coefficients may have
600 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
601 * params that receive the tweaked coefficients. Normally the function looks at the current
602 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
603 * determine the blend optimizations that would be used if there was partial pixel coverage.
604 *
605 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
606 * playback) must call this function and respect the flags that replace the output color.
607 */
608 BlendOptFlags getBlendOpts(bool forceCoverage = false,
609 GrBlendCoeff* srcCoeff = NULL,
610 GrBlendCoeff* dstCoeff = NULL) const;
611
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000612 /// @}
613
614 ///////////////////////////////////////////////////////////////////////////
615 /// @name View Matrix
616 ////
617
618 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000619 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000620 *
621 * In the post-view-matrix space the rectangle [0,w]x[0,h]
622 * fully covers the render target. (w and h are the width and height of the
bsalomon@google.comca432082013-01-23 19:53:46 +0000623 * the render-target.)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000624 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000625 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000626
627 /**
628 * Gets a writable pointer to the view matrix.
629 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000630 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000631
632 /**
633 * Multiplies the current view matrix by a matrix
634 *
635 * After this call V' = V*m where V is the old view matrix,
636 * m is the parameter to this function, and V' is the new view matrix.
637 * (We consider positions to be column vectors so position vector p is
638 * transformed by matrix X as p' = X*p.)
639 *
640 * @param m the matrix used to modify the view matrix.
641 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000642 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000643
644 /**
645 * Multiplies the current view matrix by a matrix
646 *
647 * After this call V' = m*V where V is the old view matrix,
648 * m is the parameter to this function, and V' is the new view matrix.
649 * (We consider positions to be column vectors so position vector p is
650 * transformed by matrix X as p' = X*p.)
651 *
652 * @param m the matrix used to modify the view matrix.
653 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000654 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000655
656 /**
657 * Retrieves the current view matrix
658 * @return the current view matrix.
659 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000660 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000661
662 /**
663 * Retrieves the inverse of the current view matrix.
664 *
665 * If the current view matrix is invertible, return true, and if matrix
666 * is non-null, copy the inverse into it. If the current view matrix is
667 * non-invertible, return false and ignore the matrix parameter.
668 *
669 * @param matrix if not null, will receive a copy of the current inverse.
670 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000671 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000672 // TODO: determine whether we really need to leave matrix unmodified
673 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000674 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000675 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 if (matrix) {
677 *matrix = inverse;
678 }
679 return true;
680 }
681 return false;
682 }
683
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000684 ////////////////////////////////////////////////////////////////////////////
685
686 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000687 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.comc196b522012-10-25 21:52:43 +0000688 * Effect matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000689 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 class AutoViewMatrixRestore : public ::GrNoncopyable {
691 public:
692 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000693
bsalomon@google.comc7818882013-03-20 19:19:53 +0000694 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000696 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000697 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000698
699 ~AutoViewMatrixRestore() { this->restore(); }
700
bsalomon@google.coma8347462012-10-08 18:59:39 +0000701 /**
702 * Can be called prior to destructor to restore the original matrix.
703 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000704 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000705
bsalomon@google.comc7818882013-03-20 19:19:53 +0000706 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000707
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000708 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000709
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000710 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000711 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000712 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000713 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000714 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000715 };
716
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000717 ////////////////////////////////////////////////////////////////////////////
718
719 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000720 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
721 * destructor undoes the changes, restoring the view matrix that was set before the
722 * constructor. It is similar to passing the inverse of the current view matrix to
723 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000724 */
725 class AutoDeviceCoordDraw : ::GrNoncopyable {
726 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000727 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000728 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000729 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
730 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
731 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000732 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000733 AutoDeviceCoordDraw(GrDrawState* drawState) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000734 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000735 this->set(drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000736 }
737
bsalomon@google.coma8347462012-10-08 18:59:39 +0000738 ~AutoDeviceCoordDraw() { this->restore(); }
739
bsalomon@google.comc7818882013-03-20 19:19:53 +0000740 bool set(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000741
bsalomon@google.coma8347462012-10-08 18:59:39 +0000742 /**
743 * Returns true if this object was successfully initialized on to a GrDrawState. It may
744 * return false because a non-default constructor or set() were never called or because
745 * the view matrix was not invertible.
746 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000747 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000748
bsalomon@google.coma8347462012-10-08 18:59:39 +0000749 /**
750 * Returns the matrix that was set previously set on the drawState. This is only valid
751 * if succeeded returns true.
752 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000753 const SkMatrix& getOriginalMatrix() const {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000754 GrAssert(this->succeeded());
755 return fViewMatrix;
756 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000757
bsalomon@google.coma8347462012-10-08 18:59:39 +0000758 /**
759 * Can be called prior to destructor to restore the original matrix.
760 */
761 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000762
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000763 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000764 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000765 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000766 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000767 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000768 };
769
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000770 /// @}
771
772 ///////////////////////////////////////////////////////////////////////////
773 /// @name Render Target
774 ////
775
776 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000777 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000778 *
779 * @param target The render target to set.
780 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000781 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000782 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000783 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000784
785 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000786 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000787 *
788 * @return The currently set render target.
789 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000790 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
791 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000792
793 class AutoRenderTargetRestore : public ::GrNoncopyable {
794 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000795 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000796 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
797 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000798 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799 this->set(ds, newTarget);
800 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000801 ~AutoRenderTargetRestore() { this->restore(); }
802
803 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000804 if (NULL != fDrawState) {
805 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000806 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000807 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000808 GrSafeSetNull(fSavedTarget);
809 }
810
811 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
812 this->restore();
813
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000814 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000815 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000817 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000819 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 }
822 private:
823 GrDrawState* fDrawState;
824 GrRenderTarget* fSavedTarget;
825 };
826
827 /// @}
828
829 ///////////////////////////////////////////////////////////////////////////
830 /// @name Stencil
831 ////
832
833 /**
834 * Sets the stencil settings to use for the next draw.
835 * Changing the clip has the side-effect of possibly zeroing
836 * out the client settable stencil bits. So multipass algorithms
837 * using stencil should not change the clip between passes.
838 * @param settings the stencil settings to use.
839 */
840 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000841 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000842 }
843
844 /**
845 * Shortcut to disable stencil testing and ops.
846 */
847 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000848 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000849 }
850
bsalomon@google.comca432082013-01-23 19:53:46 +0000851 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852
bsalomon@google.comca432082013-01-23 19:53:46 +0000853 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000854
855 /// @}
856
857 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000858 /// @name State Flags
859 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000860
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 /**
862 * Flags that affect rendering. Controlled using enable/disableState(). All
863 * default to disabled.
864 */
865 enum StateBits {
866 /**
867 * Perform dithering. TODO: Re-evaluate whether we need this bit
868 */
869 kDither_StateBit = 0x01,
870 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000871 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
872 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
873 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000874 */
875 kHWAntialias_StateBit = 0x02,
876 /**
877 * Draws will respect the clip, otherwise the clip is ignored.
878 */
879 kClip_StateBit = 0x04,
880 /**
881 * Disables writing to the color buffer. Useful when performing stencil
882 * operations.
883 */
884 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000885
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000886 /**
887 * Usually coverage is applied after color blending. The color is blended using the coeffs
888 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
889 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
890 * this case there is no distinction between coverage and color and the caller needs direct
891 * control over the blend coeffs. When set, there will be a single blend step controlled by
892 * setBlendFunc() which will use coverage*color as the src color.
893 */
894 kCoverageDrawing_StateBit = 0x10,
895
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000896 // Users of the class may add additional bits to the vector
897 kDummyStateBit,
898 kLastPublicStateBit = kDummyStateBit-1,
899 };
900
901 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000902 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000903 }
904
905 /**
906 * Enable render state settings.
907 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000908 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000909 */
910 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000911 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000912 }
913
914 /**
915 * Disable render state settings.
916 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000917 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000918 */
919 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000920 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000921 }
922
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000923 /**
924 * Enable or disable stateBits based on a boolean.
925 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000926 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000927 * @param enable if true enable stateBits, otherwise disable
928 */
929 void setState(uint32_t stateBits, bool enable) {
930 if (enable) {
931 this->enableState(stateBits);
932 } else {
933 this->disableState(stateBits);
934 }
935 }
936
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000937 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000938 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000939 }
940
941 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000942 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000943 }
944
945 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000946 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000947 }
948
949 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000950 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000951 }
952
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000953 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000954 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000955 }
956
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000957 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000958 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000959 }
960
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000961 /// @}
962
963 ///////////////////////////////////////////////////////////////////////////
964 /// @name Face Culling
965 ////
966
967 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000968 kInvalid_DrawFace = -1,
969
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000970 kBoth_DrawFace,
971 kCCW_DrawFace,
972 kCW_DrawFace,
973 };
974
975 /**
976 * Controls whether clockwise, counterclockwise, or both faces are drawn.
977 * @param face the face(s) to draw.
978 */
979 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000980 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000981 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000982 }
983
984 /**
985 * Gets whether the target is drawing clockwise, counterclockwise,
986 * or both faces.
987 * @return the current draw face(s).
988 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000989 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000990
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000991 /// @}
992
993 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000994
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000995 bool isStageEnabled(int s) const {
996 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000997 return (NULL != fStages[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000998 }
999
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001000 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001001 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001002 return false;
1003 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001004 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +00001005 bool enabled = this->isStageEnabled(i);
1006 if (enabled != s.isStageEnabled(i)) {
1007 return false;
1008 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001009 if (enabled && this->fStages[i] != s.fStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001010 return false;
1011 }
1012 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001013 return true;
1014 }
1015 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
1016
bsalomon@google.comca432082013-01-23 19:53:46 +00001017 GrDrawState& operator= (const GrDrawState& s) {
1018 this->setRenderTarget(s.fRenderTarget.get());
1019 fCommon = s.fCommon;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001020 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +00001021 if (s.isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001022 this->fStages[i] = s.fStages[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001023 }
1024 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001025 return *this;
1026 }
1027
1028private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +00001029
bsalomon@google.comca432082013-01-23 19:53:46 +00001030 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
1031 struct CommonState {
1032 // These fields are roughly sorted by decreasing likelihood of being different in op==
robertphillips@google.com42903302013-04-20 12:26:07 +00001033 GrColor fColor;
1034 SkMatrix fViewMatrix;
1035 GrBlendCoeff fSrcBlend;
1036 GrBlendCoeff fDstBlend;
1037 GrColor fBlendConstant;
1038 uint32_t fFlagBits;
1039 const GrVertexAttrib* fVAPtr;
1040 int fVACount;
1041 GrStencilSettings fStencilSettings;
1042 int fFirstCoverageStage;
1043 GrColor fCoverage;
1044 SkXfermode::Mode fColorFilterMode;
1045 GrColor fColorFilterColor;
1046 DrawFace fDrawFace;
jvanverth@google.com054ae992013-04-01 20:06:51 +00001047
1048 // This is simply a different representation of info in fVertexAttribs and thus does
1049 // not need to be compared in op==.
1050 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
1051
bsalomon@google.comca432082013-01-23 19:53:46 +00001052 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +00001053 bool result = fColor == other.fColor &&
1054 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
1055 fSrcBlend == other.fSrcBlend &&
1056 fDstBlend == other.fDstBlend &&
1057 fBlendConstant == other.fBlendConstant &&
1058 fFlagBits == other.fFlagBits &&
robertphillips@google.com42903302013-04-20 12:26:07 +00001059 fVACount == other.fVACount &&
1060 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) &&
jvanverth@google.com054ae992013-04-01 20:06:51 +00001061 fStencilSettings == other.fStencilSettings &&
1062 fFirstCoverageStage == other.fFirstCoverageStage &&
1063 fCoverage == other.fCoverage &&
1064 fColorFilterMode == other.fColorFilterMode &&
1065 fColorFilterColor == other.fColorFilterColor &&
1066 fDrawFace == other.fDrawFace;
1067 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
1068 other.fFixedFunctionVertexAttribIndices,
1069 sizeof(fFixedFunctionVertexAttribIndices)));
1070 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +00001071 }
1072 bool operator!= (const CommonState& other) const { return !(*this == other); }
1073 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001074
bsalomon@google.comca432082013-01-23 19:53:46 +00001075 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
1076 DeferredState must directly reference GrEffects, however. */
1077 struct SavedEffectStage {
1078 SavedEffectStage() : fEffect(NULL) {}
1079 const GrEffect* fEffect;
1080 GrEffectStage::SavedCoordChange fCoordChange;
1081 };
1082
1083public:
1084 /**
1085 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
1086 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
1087 * dispose mechanism returns them to the cache. This allows recycling resources through the
1088 * the cache while they are in a deferred draw queue.
1089 */
1090 class DeferredState {
1091 public:
1092 DeferredState() : fRenderTarget(NULL) {
1093 GR_DEBUGCODE(fInitialized = false;)
1094 }
1095 // TODO: Remove this when DeferredState no longer holds a ref to the RT
1096 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1097
1098 void saveFrom(const GrDrawState& drawState) {
1099 fCommon = drawState.fCommon;
1100 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1101 fRenderTarget = drawState.fRenderTarget.get();
1102 SkSafeRef(fRenderTarget);
1103 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1104 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1105 // and recycle them to the cache (if no one else is holding a ref to the resources).
1106 for (int i = 0; i < kNumStages; ++i) {
1107 fStages[i].saveFrom(drawState.fStages[i]);
1108 }
1109 GR_DEBUGCODE(fInitialized = true;)
1110 }
1111
1112 void restoreTo(GrDrawState* drawState) {
1113 GrAssert(fInitialized);
1114 drawState->fCommon = fCommon;
1115 drawState->setRenderTarget(fRenderTarget);
1116 for (int i = 0; i < kNumStages; ++i) {
1117 fStages[i].restoreTo(&drawState->fStages[i]);
1118 }
1119 }
1120
1121 bool isEqual(const GrDrawState& state) const {
1122 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
1123 return false;
1124 }
1125 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comdcd69bf2013-01-24 18:28:51 +00001126 if (!fStages[i].isEqual(state.fStages[i])) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001127 return false;
1128 }
1129 }
1130 return true;
1131 }
1132
1133 private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001134 GrRenderTarget* fRenderTarget;
1135 CommonState fCommon;
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001136 GrEffectStage::DeferredStage fStages[kNumStages];
bsalomon@google.comca432082013-01-23 19:53:46 +00001137
1138 GR_DEBUGCODE(bool fInitialized;)
1139 };
1140
1141private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001142
1143 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1144 CommonState fCommon;
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001145 GrEffectStage fStages[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001146
robertphillips@google.com42903302013-04-20 12:26:07 +00001147 /**
1148 * Sets vertex attributes for next draw.
1149 *
1150 * @param attribs the array of vertex attributes to set.
1151 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
1152 */
1153 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
1154
reed@google.comfa35e3d2012-06-26 20:16:17 +00001155 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001156};
1157
bsalomon@google.com2b446732013-02-12 16:47:41 +00001158GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1159
tomhudson@google.com93813632011-10-27 20:21:16 +00001160#endif