blob: 0beebf742eae489b42c497b853b42f6a31fc054b [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 /**
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 ///////////////////////////////////////////////////////////////////////////
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 /**
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000123 * Sets vertex attributes for next draw.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000124 *
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000125 * @param attribs the array of vertex attributes to set.
jvanverth@google.com054ae992013-04-01 20:06:51 +0000126 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000127 */
128 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000129
jvanverth@google.com054ae992013-04-01 20:06:51 +0000130 const GrVertexAttrib* getVertexAttribs() const { return fCommon.fVertexAttribs.begin(); }
131 int getVertexAttribCount() const { return fCommon.fVertexAttribs.count(); }
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;
180 fVertexAttribs = drawState->fCommon.fVertexAttribs;
181 fDrawState->setDefaultVertexAttribs();
182 }
183
184 ~AutoVertexAttribRestore(){
185 fDrawState->fCommon.fVertexAttribs = fVertexAttribs;
186 }
187
188 private:
189 GrDrawState* fDrawState;
190 GrVertexAttribArray<kMaxVertexAttribCnt> fVertexAttribs;
191 };
192
193 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000194 * Accessing positions, local coords, or colors, of a vertex within an array is a hassle
195 * involving casts and simple math. These helpers exist to keep GrDrawTarget clients' code a bit
196 * nicer looking.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000197 */
198
199 /**
200 * Gets a pointer to a GrPoint of a vertex's position or texture
201 * coordinate.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000202 * @param vertices the vertex array
jvanverth@google.comcc782382013-01-28 20:39:48 +0000203 * @param vertexIndex the index of the vertex in the array
204 * @param vertexSize the size of each vertex in the array
205 * @param offset the offset in bytes of the vertex component.
206 * Defaults to zero (corresponding to vertex position)
207 * @return pointer to the vertex component as a GrPoint
208 */
209 static GrPoint* GetVertexPoint(void* vertices,
210 int vertexIndex,
211 int vertexSize,
212 int offset = 0) {
213 intptr_t start = GrTCast<intptr_t>(vertices);
214 return GrTCast<GrPoint*>(start + offset +
215 vertexIndex * vertexSize);
216 }
217 static const GrPoint* GetVertexPoint(const void* vertices,
218 int vertexIndex,
219 int vertexSize,
220 int offset = 0) {
221 intptr_t start = GrTCast<intptr_t>(vertices);
222 return GrTCast<const GrPoint*>(start + offset +
223 vertexIndex * vertexSize);
224 }
225
226 /**
227 * Gets a pointer to a GrColor inside a vertex within a vertex array.
228 * @param vertices the vetex array
229 * @param vertexIndex the index of the vertex in the array
230 * @param vertexSize the size of each vertex in the array
231 * @param offset the offset in bytes of the vertex color
232 * @return pointer to the vertex component as a GrColor
233 */
234 static GrColor* GetVertexColor(void* vertices,
235 int vertexIndex,
236 int vertexSize,
237 int offset) {
238 intptr_t start = GrTCast<intptr_t>(vertices);
239 return GrTCast<GrColor*>(start + offset +
240 vertexIndex * vertexSize);
241 }
242 static const GrColor* GetVertexColor(const void* vertices,
243 int vertexIndex,
244 int vertexSize,
245 int offset) {
246 const intptr_t start = GrTCast<intptr_t>(vertices);
247 return GrTCast<const GrColor*>(start + offset +
248 vertexIndex * vertexSize);
249 }
250
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000251 /// @}
252
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000253 /**
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000254 * Determines whether src alpha is guaranteed to be one for all src pixels
255 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000256 bool srcAlphaWillBeOne() const;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000257
258 /**
259 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
260 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000261 bool hasSolidCoverage() const;
jvanverth@google.comcc782382013-01-28 20:39:48 +0000262
263 /// @}
264
265 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000266 /// @name Color
267 ////
268
269 /**
270 * Sets color for next draw to a premultiplied-alpha color.
271 *
272 * @param color the color to set.
273 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000274 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000275
bsalomon@google.comca432082013-01-23 19:53:46 +0000276 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000277
278 /**
279 * Sets the color to be used for the next draw to be
280 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
281 *
282 * @param alpha The alpha value to set as the color.
283 */
284 void setAlpha(uint8_t a) {
285 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
286 }
287
288 /**
289 * Add a color filter that can be represented by a color and a mode. Applied
bsalomon@google.comc7818882013-03-20 19:19:53 +0000290 * after color-computing effect stages.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000291 */
292 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000293 fCommon.fColorFilterColor = c;
294 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000295 }
296
bsalomon@google.comca432082013-01-23 19:53:46 +0000297 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
298 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000299
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000300 /**
301 * Constructor sets the color to be 'color' which is undone by the destructor.
302 */
303 class AutoColorRestore : public ::GrNoncopyable {
304 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000305 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000306
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000307 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000308 fDrawState = NULL;
309 this->set(drawState, color);
310 }
311
312 void reset() {
313 if (NULL != fDrawState) {
314 fDrawState->setColor(fOldColor);
315 fDrawState = NULL;
316 }
317 }
318
319 void set(GrDrawState* drawState, GrColor color) {
320 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000321 fDrawState = drawState;
322 fOldColor = fDrawState->getColor();
323 fDrawState->setColor(color);
324 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000325
326 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000327 private:
328 GrDrawState* fDrawState;
329 GrColor fOldColor;
330 };
331
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000332 /// @}
333
334 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000335 /// @name Coverage
336 ////
337
338 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000340 * initial value (after construction or reset()) is 0xff. The constant
341 * coverage is ignored when per-vertex coverage is provided.
342 */
343 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000344 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000345 }
346
347 /**
348 * Version of above that specifies 4 channel per-vertex color. The value
349 * should be premultiplied.
350 */
351 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000352 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000353 }
354
355 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000356 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000357 }
358
359 /// @}
360
361 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000362 /// @name Effect Stages
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000363 ////
364
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000365 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
366 fStages[stageIdx].setEffect(effect);
367 return effect;
368 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000369
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000370 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect,
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000371 int attr0, int attr1 = -1) {
372 fStages[stageIdx].setEffect(effect, attr0, attr1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000373 return effect;
374 }
375
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000376 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000377 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000378 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000379 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000380 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000381 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000382 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000383 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000384 void createTextureEffect(int stageIdx,
385 GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000386 const SkMatrix& matrix,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000387 const GrTextureParams& params) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000388 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000389 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000390 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000391 }
392
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000393 bool stagesDisabled() {
394 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000395 if (NULL != fStages[i].getEffect()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000396 return false;
397 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000398 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000399 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000400 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000401
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000402 void disableStage(int stageIdx) {
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000403 this->setEffect(stageIdx, NULL);
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000404 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000405
robertphillips@google.com972265d2012-06-13 18:49:30 +0000406 /**
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000407 * Release all the GrEffects referred to by this draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000408 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000409 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000410 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000411 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000412 }
413 }
414
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000415 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000416 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000417 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
418 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000419 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000420 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000421 }
422 }
423 private:
424 GrDrawState* fDrawState;
425 };
426
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000427 /**
bsalomon@google.com08283af2012-10-26 13:01:20 +0000428 * Returns the current stage by index.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000429 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000430 const GrEffectStage& getStage(int stageIdx) const {
431 GrAssert((unsigned)stageIdx < kNumStages);
432 return fStages[stageIdx];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000433 }
434
435 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000436 * Called when the source coord system is changing. This ensures that effects will see the
437 * correct local coordinates. oldToNew gives the transformation from the old coord system in
438 * which the geometry was specified to the new coordinate system from which it will be rendered.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000439 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000440 void localCoordChange(const SkMatrix& oldToNew) {
reed@google.com67e7cde2013-03-20 17:47:16 +0000441 for (int i = 0; i < kNumStages; ++i) {
442 if (this->isStageEnabled(i)) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000443 fStages[i].localCoordChange(oldToNew);
reed@google.com67e7cde2013-03-20 17:47:16 +0000444 }
445 }
reed@google.com67e7cde2013-03-20 17:47:16 +0000446 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000447
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000448 /**
449 * Checks whether any of the effects will read the dst pixel color.
450 */
451 bool willEffectReadDst() const {
452 for (int s = 0; s < kNumStages; ++s) {
453 if (this->isStageEnabled(s) && (*this->getStage(s).getEffect())->willReadDst()) {
454 return true;
455 }
456 }
457 return false;
458 }
reed@google.com67e7cde2013-03-20 17:47:16 +0000459
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000460 /// @}
461
462 ///////////////////////////////////////////////////////////////////////////
463 /// @name Coverage / Color Stages
464 ////
465
466 /**
467 * A common pattern is to compute a color with the initial stages and then
468 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000469 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
470 * computed based on the pre-coverage-modulated color. The division of
471 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000472 * this method. Initially this is kNumStages (all stages
473 * are color-computing).
474 */
475 void setFirstCoverageStage(int firstCoverageStage) {
476 GrAssert((unsigned)firstCoverageStage <= kNumStages);
bsalomon@google.comca432082013-01-23 19:53:46 +0000477 fCommon.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000478 }
479
480 /**
481 * Gets the index of the first coverage-computing stage.
482 */
483 int getFirstCoverageStage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000484 return fCommon.fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 }
486
487 ///@}
488
489 ///////////////////////////////////////////////////////////////////////////
490 /// @name Blending
491 ////
492
493 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000494 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000495 *
496 * The blend function will be:
497 * D' = sat(S*srcCoef + D*dstCoef)
498 *
499 * where D is the existing destination color, S is the incoming source
500 * color, and D' is the new destination color that will be written. sat()
501 * is the saturation function.
502 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000503 * @param srcCoef coefficient applied to the src color.
504 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 */
506 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000507 fCommon.fSrcBlend = srcCoeff;
508 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000509 #if GR_DEBUG
510 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000511 case kDC_GrBlendCoeff:
512 case kIDC_GrBlendCoeff:
513 case kDA_GrBlendCoeff:
514 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000515 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
516 "coverage stages.\n");
517 break;
518 default:
519 break;
520 }
521 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000522 case kSC_GrBlendCoeff:
523 case kISC_GrBlendCoeff:
524 case kSA_GrBlendCoeff:
525 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000526 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
527 "coverage stages.\n");
528 break;
529 default:
530 break;
531 }
532 #endif
533 }
534
bsalomon@google.comca432082013-01-23 19:53:46 +0000535 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
536 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000537
538 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
539 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000540 *srcBlendCoeff = fCommon.fSrcBlend;
541 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000542 }
543
544 /**
545 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000546 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000547 * kConstC_GrBlendCoeff
548 * kIConstC_GrBlendCoeff
549 * kConstA_GrBlendCoeff
550 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 *
552 * @param constant the constant to set
553 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000554 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000555
556 /**
557 * Retrieves the last value set by setBlendConstant()
558 * @return the blending constant value
559 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000560 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000561
bsalomon@google.com2b446732013-02-12 16:47:41 +0000562 /**
563 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
564 * coverage before the blend will give the correct final destination color. In general it
565 * will not as coverage is applied after blending.
566 */
567 bool canTweakAlphaForCoverage() const;
568
569 /**
570 * Optimizations for blending / coverage to that can be applied based on the current state.
571 */
572 enum BlendOptFlags {
573 /**
574 * No optimization
575 */
576 kNone_BlendOpt = 0,
577 /**
578 * Don't draw at all
579 */
580 kSkipDraw_BlendOptFlag = 0x1,
581 /**
582 * Emit the src color, disable HW blending (replace dst with src)
583 */
584 kDisableBlend_BlendOptFlag = 0x2,
585 /**
586 * The coverage value does not have to be computed separately from alpha, the the output
587 * color can be the modulation of the two.
588 */
589 kCoverageAsAlpha_BlendOptFlag = 0x4,
590 /**
591 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
592 * "don't cares".
593 */
594 kEmitCoverage_BlendOptFlag = 0x8,
595 /**
596 * Emit transparent black instead of the src color, no need to compute coverage.
597 */
598 kEmitTransBlack_BlendOptFlag = 0x10,
599 };
600 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
601
602 /**
603 * Determines what optimizations can be applied based on the blend. The coefficients may have
604 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
605 * params that receive the tweaked coefficients. Normally the function looks at the current
606 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
607 * determine the blend optimizations that would be used if there was partial pixel coverage.
608 *
609 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
610 * playback) must call this function and respect the flags that replace the output color.
611 */
612 BlendOptFlags getBlendOpts(bool forceCoverage = false,
613 GrBlendCoeff* srcCoeff = NULL,
614 GrBlendCoeff* dstCoeff = NULL) const;
615
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616 /// @}
617
618 ///////////////////////////////////////////////////////////////////////////
619 /// @name View Matrix
620 ////
621
622 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000623 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000624 *
625 * In the post-view-matrix space the rectangle [0,w]x[0,h]
626 * fully covers the render target. (w and h are the width and height of the
bsalomon@google.comca432082013-01-23 19:53:46 +0000627 * the render-target.)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000629 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000630
631 /**
632 * Gets a writable pointer to the view matrix.
633 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000634 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000635
636 /**
637 * Multiplies the current view matrix by a matrix
638 *
639 * After this call V' = V*m where V is the old view matrix,
640 * m is the parameter to this function, and V' is the new view matrix.
641 * (We consider positions to be column vectors so position vector p is
642 * transformed by matrix X as p' = X*p.)
643 *
644 * @param m the matrix used to modify the view matrix.
645 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000646 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000647
648 /**
649 * Multiplies the current view matrix by a matrix
650 *
651 * After this call V' = m*V where V is the old view matrix,
652 * m is the parameter to this function, and V' is the new view matrix.
653 * (We consider positions to be column vectors so position vector p is
654 * transformed by matrix X as p' = X*p.)
655 *
656 * @param m the matrix used to modify the view matrix.
657 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000658 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000659
660 /**
661 * Retrieves the current view matrix
662 * @return the current view matrix.
663 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000664 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000665
666 /**
667 * Retrieves the inverse of the current view matrix.
668 *
669 * If the current view matrix is invertible, return true, and if matrix
670 * is non-null, copy the inverse into it. If the current view matrix is
671 * non-invertible, return false and ignore the matrix parameter.
672 *
673 * @param matrix if not null, will receive a copy of the current inverse.
674 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000675 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 // TODO: determine whether we really need to leave matrix unmodified
677 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000678 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000679 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000680 if (matrix) {
681 *matrix = inverse;
682 }
683 return true;
684 }
685 return false;
686 }
687
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000688 ////////////////////////////////////////////////////////////////////////////
689
690 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000691 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.comc196b522012-10-25 21:52:43 +0000692 * Effect matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000693 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000694 class AutoViewMatrixRestore : public ::GrNoncopyable {
695 public:
696 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000697
bsalomon@google.comc7818882013-03-20 19:19:53 +0000698 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000699 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000700 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000701 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000702
703 ~AutoViewMatrixRestore() { this->restore(); }
704
bsalomon@google.coma8347462012-10-08 18:59:39 +0000705 /**
706 * Can be called prior to destructor to restore the original matrix.
707 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000708 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000709
bsalomon@google.comc7818882013-03-20 19:19:53 +0000710 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000711
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000712 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000713
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000714 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000715 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000716 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000717 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000718 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000719 };
720
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000721 ////////////////////////////////////////////////////////////////////////////
722
723 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000724 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
725 * destructor undoes the changes, restoring the view matrix that was set before the
726 * constructor. It is similar to passing the inverse of the current view matrix to
727 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000728 */
729 class AutoDeviceCoordDraw : ::GrNoncopyable {
730 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000731 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000732 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000733 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
734 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
735 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000736 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000737 AutoDeviceCoordDraw(GrDrawState* drawState) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000738 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000739 this->set(drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000740 }
741
bsalomon@google.coma8347462012-10-08 18:59:39 +0000742 ~AutoDeviceCoordDraw() { this->restore(); }
743
bsalomon@google.comc7818882013-03-20 19:19:53 +0000744 bool set(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000745
bsalomon@google.coma8347462012-10-08 18:59:39 +0000746 /**
747 * Returns true if this object was successfully initialized on to a GrDrawState. It may
748 * return false because a non-default constructor or set() were never called or because
749 * the view matrix was not invertible.
750 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000751 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000752
bsalomon@google.coma8347462012-10-08 18:59:39 +0000753 /**
754 * Returns the matrix that was set previously set on the drawState. This is only valid
755 * if succeeded returns true.
756 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000757 const SkMatrix& getOriginalMatrix() const {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000758 GrAssert(this->succeeded());
759 return fViewMatrix;
760 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000761
bsalomon@google.coma8347462012-10-08 18:59:39 +0000762 /**
763 * Can be called prior to destructor to restore the original matrix.
764 */
765 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000766
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000767 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000768 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000769 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000770 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000771 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000772 };
773
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000774 /// @}
775
776 ///////////////////////////////////////////////////////////////////////////
777 /// @name Render Target
778 ////
779
780 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000781 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000782 *
783 * @param target The render target to set.
784 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000785 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000786 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000787 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000788
789 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000790 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791 *
792 * @return The currently set render target.
793 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000794 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
795 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000796
797 class AutoRenderTargetRestore : public ::GrNoncopyable {
798 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000799 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
801 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000802 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803 this->set(ds, newTarget);
804 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000805 ~AutoRenderTargetRestore() { this->restore(); }
806
807 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000808 if (NULL != fDrawState) {
809 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000810 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000811 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000812 GrSafeSetNull(fSavedTarget);
813 }
814
815 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
816 this->restore();
817
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000819 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000821 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000823 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000825 }
826 private:
827 GrDrawState* fDrawState;
828 GrRenderTarget* fSavedTarget;
829 };
830
831 /// @}
832
833 ///////////////////////////////////////////////////////////////////////////
834 /// @name Stencil
835 ////
836
837 /**
838 * Sets the stencil settings to use for the next draw.
839 * Changing the clip has the side-effect of possibly zeroing
840 * out the client settable stencil bits. So multipass algorithms
841 * using stencil should not change the clip between passes.
842 * @param settings the stencil settings to use.
843 */
844 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000845 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000846 }
847
848 /**
849 * Shortcut to disable stencil testing and ops.
850 */
851 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000852 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000853 }
854
bsalomon@google.comca432082013-01-23 19:53:46 +0000855 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000856
bsalomon@google.comca432082013-01-23 19:53:46 +0000857 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000858
859 /// @}
860
861 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000862 /// @name State Flags
863 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000864
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000865 /**
866 * Flags that affect rendering. Controlled using enable/disableState(). All
867 * default to disabled.
868 */
869 enum StateBits {
870 /**
871 * Perform dithering. TODO: Re-evaluate whether we need this bit
872 */
873 kDither_StateBit = 0x01,
874 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000875 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
876 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
877 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 */
879 kHWAntialias_StateBit = 0x02,
880 /**
881 * Draws will respect the clip, otherwise the clip is ignored.
882 */
883 kClip_StateBit = 0x04,
884 /**
885 * Disables writing to the color buffer. Useful when performing stencil
886 * operations.
887 */
888 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000889
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000890 /**
891 * Usually coverage is applied after color blending. The color is blended using the coeffs
892 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
893 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
894 * this case there is no distinction between coverage and color and the caller needs direct
895 * control over the blend coeffs. When set, there will be a single blend step controlled by
896 * setBlendFunc() which will use coverage*color as the src color.
897 */
898 kCoverageDrawing_StateBit = 0x10,
899
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000900 // Users of the class may add additional bits to the vector
901 kDummyStateBit,
902 kLastPublicStateBit = kDummyStateBit-1,
903 };
904
905 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000906 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000907 }
908
909 /**
910 * Enable render state settings.
911 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000912 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000913 */
914 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000915 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000916 }
917
918 /**
919 * Disable render state settings.
920 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000921 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000922 */
923 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000924 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000925 }
926
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000927 /**
928 * Enable or disable stateBits based on a boolean.
929 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000930 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000931 * @param enable if true enable stateBits, otherwise disable
932 */
933 void setState(uint32_t stateBits, bool enable) {
934 if (enable) {
935 this->enableState(stateBits);
936 } else {
937 this->disableState(stateBits);
938 }
939 }
940
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000941 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000942 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000943 }
944
945 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000946 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000947 }
948
949 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000950 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000951 }
952
953 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000954 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000955 }
956
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000957 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000958 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000959 }
960
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000961 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000962 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000963 }
964
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000965 /// @}
966
967 ///////////////////////////////////////////////////////////////////////////
968 /// @name Face Culling
969 ////
970
971 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000972 kInvalid_DrawFace = -1,
973
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000974 kBoth_DrawFace,
975 kCCW_DrawFace,
976 kCW_DrawFace,
977 };
978
979 /**
980 * Controls whether clockwise, counterclockwise, or both faces are drawn.
981 * @param face the face(s) to draw.
982 */
983 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000984 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000985 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000986 }
987
988 /**
989 * Gets whether the target is drawing clockwise, counterclockwise,
990 * or both faces.
991 * @return the current draw face(s).
992 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000993 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000994
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000995 /// @}
996
997 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000998
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000999 bool isStageEnabled(int s) const {
1000 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com08283af2012-10-26 13:01:20 +00001001 return (NULL != fStages[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001002 }
1003
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001004 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001005 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001006 return false;
1007 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001008 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +00001009 bool enabled = this->isStageEnabled(i);
1010 if (enabled != s.isStageEnabled(i)) {
1011 return false;
1012 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001013 if (enabled && this->fStages[i] != s.fStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001014 return false;
1015 }
1016 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001017 return true;
1018 }
1019 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
1020
bsalomon@google.comca432082013-01-23 19:53:46 +00001021 GrDrawState& operator= (const GrDrawState& s) {
1022 this->setRenderTarget(s.fRenderTarget.get());
1023 fCommon = s.fCommon;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001024 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +00001025 if (s.isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001026 this->fStages[i] = s.fStages[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001027 }
1028 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001029 return *this;
1030 }
1031
1032private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +00001033
bsalomon@google.comca432082013-01-23 19:53:46 +00001034 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
1035 struct CommonState {
1036 // These fields are roughly sorted by decreasing likelihood of being different in op==
jvanverth@google.com054ae992013-04-01 20:06:51 +00001037 GrColor fColor;
1038 SkMatrix fViewMatrix;
1039 GrBlendCoeff fSrcBlend;
1040 GrBlendCoeff fDstBlend;
1041 GrColor fBlendConstant;
1042 uint32_t fFlagBits;
1043 GrVertexAttribArray<kMaxVertexAttribCnt> fVertexAttribs;
1044 GrStencilSettings fStencilSettings;
1045 int fFirstCoverageStage;
1046 GrColor fCoverage;
1047 SkXfermode::Mode fColorFilterMode;
1048 GrColor fColorFilterColor;
1049 DrawFace fDrawFace;
1050
1051 // This is simply a different representation of info in fVertexAttribs and thus does
1052 // not need to be compared in op==.
1053 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
1054
bsalomon@google.comca432082013-01-23 19:53:46 +00001055 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +00001056 bool result = fColor == other.fColor &&
1057 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
1058 fSrcBlend == other.fSrcBlend &&
1059 fDstBlend == other.fDstBlend &&
1060 fBlendConstant == other.fBlendConstant &&
1061 fFlagBits == other.fFlagBits &&
1062 fVertexAttribs == other.fVertexAttribs &&
1063 fStencilSettings == other.fStencilSettings &&
1064 fFirstCoverageStage == other.fFirstCoverageStage &&
1065 fCoverage == other.fCoverage &&
1066 fColorFilterMode == other.fColorFilterMode &&
1067 fColorFilterColor == other.fColorFilterColor &&
1068 fDrawFace == other.fDrawFace;
1069 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
1070 other.fFixedFunctionVertexAttribIndices,
1071 sizeof(fFixedFunctionVertexAttribIndices)));
1072 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +00001073 }
1074 bool operator!= (const CommonState& other) const { return !(*this == other); }
1075 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001076
bsalomon@google.comca432082013-01-23 19:53:46 +00001077 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
1078 DeferredState must directly reference GrEffects, however. */
1079 struct SavedEffectStage {
1080 SavedEffectStage() : fEffect(NULL) {}
1081 const GrEffect* fEffect;
1082 GrEffectStage::SavedCoordChange fCoordChange;
1083 };
1084
1085public:
1086 /**
1087 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
1088 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
1089 * dispose mechanism returns them to the cache. This allows recycling resources through the
1090 * the cache while they are in a deferred draw queue.
1091 */
1092 class DeferredState {
1093 public:
1094 DeferredState() : fRenderTarget(NULL) {
1095 GR_DEBUGCODE(fInitialized = false;)
1096 }
1097 // TODO: Remove this when DeferredState no longer holds a ref to the RT
1098 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1099
1100 void saveFrom(const GrDrawState& drawState) {
1101 fCommon = drawState.fCommon;
1102 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1103 fRenderTarget = drawState.fRenderTarget.get();
1104 SkSafeRef(fRenderTarget);
1105 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1106 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1107 // and recycle them to the cache (if no one else is holding a ref to the resources).
1108 for (int i = 0; i < kNumStages; ++i) {
1109 fStages[i].saveFrom(drawState.fStages[i]);
1110 }
1111 GR_DEBUGCODE(fInitialized = true;)
1112 }
1113
1114 void restoreTo(GrDrawState* drawState) {
1115 GrAssert(fInitialized);
1116 drawState->fCommon = fCommon;
1117 drawState->setRenderTarget(fRenderTarget);
1118 for (int i = 0; i < kNumStages; ++i) {
1119 fStages[i].restoreTo(&drawState->fStages[i]);
1120 }
1121 }
1122
1123 bool isEqual(const GrDrawState& state) const {
1124 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
1125 return false;
1126 }
1127 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comdcd69bf2013-01-24 18:28:51 +00001128 if (!fStages[i].isEqual(state.fStages[i])) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001129 return false;
1130 }
1131 }
1132 return true;
1133 }
1134
1135 private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001136 GrRenderTarget* fRenderTarget;
1137 CommonState fCommon;
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001138 GrEffectStage::DeferredStage fStages[kNumStages];
bsalomon@google.comca432082013-01-23 19:53:46 +00001139
1140 GR_DEBUGCODE(bool fInitialized;)
1141 };
1142
1143private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001144
1145 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1146 CommonState fCommon;
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001147 GrEffectStage fStages[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001148
reed@google.comfa35e3d2012-06-26 20:16:17 +00001149 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001150};
1151
bsalomon@google.com2b446732013-02-12 16:47:41 +00001152GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1153
tomhudson@google.com93813632011-10-27 20:21:16 +00001154#endif