blob: c4f872608bb818b83d256563602d13b4e75b5d29 [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
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000030 GrDrawState() {
31 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
32 this->reset();
33 }
tomhudson@google.com93813632011-10-27 20:21:16 +000034
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000035 GrDrawState(const SkMatrix& initialViewMatrix) {
36 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
37 this->reset(initialViewMatrix);
38 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000039
40 /**
41 * Copies another draw state.
42 **/
bsalomon@google.comca432082013-01-23 19:53:46 +000043 GrDrawState(const GrDrawState& state) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000044 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000045 *this = state;
46 }
47
bsalomon@google.com137f1342013-05-29 21:27:53 +000048 /**
49 * Copies another draw state with a preconcat to the view matrix.
50 **/
51 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000052 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com137f1342013-05-29 21:27:53 +000053 *this = state;
54 if (!preConcatMatrix.isIdentity()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000055 for (int i = 0; i < fColorStages.count(); ++i) {
56 fColorStages[i].localCoordChange(preConcatMatrix);
57 }
58 for (int i = 0; i < fCoverageStages.count(); ++i) {
59 fCoverageStages[i].localCoordChange(preConcatMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +000060 }
61 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000062 }
63
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000064 virtual ~GrDrawState() { GrAssert(0 == fBlockEffectRemovalCnt); }
bsalomon@google.com137f1342013-05-29 21:27:53 +000065
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000066 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +000067 * Resets to the default state. GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000068 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000069 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000070
bsalomon@google.com137f1342013-05-29 21:27:53 +000071 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000072
73 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000074 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
75 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000076 * equivalents are set to default values. Clipping will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000077 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000078 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000079
80 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000081 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000082 ////
83
jvanverth@google.com9b855c72013-03-01 18:21:22 +000084 enum {
jvanverth@google.com054ae992013-04-01 20:06:51 +000085 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000086 };
87
jvanverth@google.com9b855c72013-03-01 18:21:22 +000088 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000089 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
90 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
91 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000092 *
jvanverth@google.com054ae992013-04-01 20:06:51 +000093 * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when
94 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000095 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000096
jvanverth@google.com9b855c72013-03-01 18:21:22 +000097 /**
robertphillips@google.com42903302013-04-20 12:26:07 +000098 * Sets vertex attributes for next draw. The object driving the templatization
99 * should be a global GrVertexAttrib array that is never changed.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000100 */
robertphillips@google.com42903302013-04-20 12:26:07 +0000101 template <const GrVertexAttrib A[]> void setVertexAttribs(int count) {
102 this->setVertexAttribs(A, count);
103 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000104
robertphillips@google.com42903302013-04-20 12:26:07 +0000105 const GrVertexAttrib* getVertexAttribs() const { return fCommon.fVAPtr; }
106 int getVertexAttribCount() const { return fCommon.fVACount; }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000107
108 size_t getVertexSize() const;
109
110 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000111 * Sets default vertex attributes for next draw. The default is a single attribute:
112 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000113 */
114 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000115
jvanverth@google.com054ae992013-04-01 20:06:51 +0000116 /**
117 * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the
118 * binding does not appear in the current attribs. These bindings should appear only once in
119 * the attrib array.
120 */
121
122 int positionAttributeIndex() const {
123 return fCommon.fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
124 }
125 int localCoordAttributeIndex() const {
126 return fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
127 }
128 int colorVertexAttributeIndex() const {
129 return fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
130 }
131 int coverageVertexAttributeIndex() const {
132 return fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
133 }
134
135 bool hasLocalCoordAttribute() const {
136 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
137 }
138 bool hasColorVertexAttribute() const {
139 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
140 }
141 bool hasCoverageVertexAttribute() const {
142 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
143 }
144
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000145 bool validateVertexAttribs() const;
146
jvanverth@google.comcc782382013-01-28 20:39:48 +0000147 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000148 * Helper to save/restore vertex attribs
149 */
150 class AutoVertexAttribRestore {
151 public:
152 AutoVertexAttribRestore(GrDrawState* drawState) {
153 GrAssert(NULL != drawState);
154 fDrawState = drawState;
robertphillips@google.com42903302013-04-20 12:26:07 +0000155 fVAPtr = drawState->fCommon.fVAPtr;
156 fVACount = drawState->fCommon.fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000157 fDrawState->setDefaultVertexAttribs();
158 }
159
160 ~AutoVertexAttribRestore(){
robertphillips@google.com42903302013-04-20 12:26:07 +0000161 fDrawState->fCommon.fVAPtr = fVAPtr;
162 fDrawState->fCommon.fVACount = fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000163 }
164
165 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000166 GrDrawState* fDrawState;
167 const GrVertexAttrib* fVAPtr;
168 int fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000169 };
170
171 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000172 * Accessing positions, local coords, or colors, of a vertex within an array is a hassle
173 * involving casts and simple math. These helpers exist to keep GrDrawTarget clients' code a bit
174 * nicer looking.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000175 */
176
177 /**
178 * Gets a pointer to a GrPoint of a vertex's position or texture
179 * coordinate.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000180 * @param vertices the vertex array
jvanverth@google.comcc782382013-01-28 20:39:48 +0000181 * @param vertexIndex the index of the vertex in the array
182 * @param vertexSize the size of each vertex in the array
183 * @param offset the offset in bytes of the vertex component.
184 * Defaults to zero (corresponding to vertex position)
185 * @return pointer to the vertex component as a GrPoint
186 */
187 static GrPoint* GetVertexPoint(void* vertices,
188 int vertexIndex,
189 int vertexSize,
190 int offset = 0) {
191 intptr_t start = GrTCast<intptr_t>(vertices);
192 return GrTCast<GrPoint*>(start + offset +
193 vertexIndex * vertexSize);
194 }
195 static const GrPoint* GetVertexPoint(const void* vertices,
196 int vertexIndex,
197 int vertexSize,
198 int offset = 0) {
199 intptr_t start = GrTCast<intptr_t>(vertices);
200 return GrTCast<const GrPoint*>(start + offset +
201 vertexIndex * vertexSize);
202 }
203
204 /**
205 * Gets a pointer to a GrColor inside a vertex within a vertex array.
206 * @param vertices the vetex array
207 * @param vertexIndex the index of the vertex in the array
208 * @param vertexSize the size of each vertex in the array
209 * @param offset the offset in bytes of the vertex color
210 * @return pointer to the vertex component as a GrColor
211 */
212 static GrColor* GetVertexColor(void* vertices,
213 int vertexIndex,
214 int vertexSize,
215 int offset) {
216 intptr_t start = GrTCast<intptr_t>(vertices);
217 return GrTCast<GrColor*>(start + offset +
218 vertexIndex * vertexSize);
219 }
220 static const GrColor* GetVertexColor(const void* vertices,
221 int vertexIndex,
222 int vertexSize,
223 int offset) {
224 const intptr_t start = GrTCast<intptr_t>(vertices);
225 return GrTCast<const GrColor*>(start + offset +
226 vertexIndex * vertexSize);
227 }
228
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000229 /// @}
230
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000231 /**
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000232 * Determines whether src alpha is guaranteed to be one for all src pixels
233 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000234 bool srcAlphaWillBeOne() const;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000235
236 /**
237 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
238 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000239 bool hasSolidCoverage() const;
jvanverth@google.comcc782382013-01-28 20:39:48 +0000240
241 /// @}
242
243 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000244 /// @name Color
245 ////
246
247 /**
248 * Sets color for next draw to a premultiplied-alpha color.
249 *
250 * @param color the color to set.
251 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000252 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000253
bsalomon@google.comca432082013-01-23 19:53:46 +0000254 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000255
256 /**
257 * Sets the color to be used for the next draw to be
258 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
259 *
260 * @param alpha The alpha value to set as the color.
261 */
262 void setAlpha(uint8_t a) {
263 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
264 }
265
266 /**
267 * Add a color filter that can be represented by a color and a mode. Applied
bsalomon@google.comc7818882013-03-20 19:19:53 +0000268 * after color-computing effect stages.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000269 */
270 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000271 fCommon.fColorFilterColor = c;
272 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000273 }
274
bsalomon@google.comca432082013-01-23 19:53:46 +0000275 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
276 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000277
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000278 /**
279 * Constructor sets the color to be 'color' which is undone by the destructor.
280 */
281 class AutoColorRestore : public ::GrNoncopyable {
282 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000283 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000284
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000285 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000286 fDrawState = NULL;
287 this->set(drawState, color);
288 }
289
290 void reset() {
291 if (NULL != fDrawState) {
292 fDrawState->setColor(fOldColor);
293 fDrawState = NULL;
294 }
295 }
296
297 void set(GrDrawState* drawState, GrColor color) {
298 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000299 fDrawState = drawState;
300 fOldColor = fDrawState->getColor();
301 fDrawState->setColor(color);
302 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000303
304 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000305 private:
306 GrDrawState* fDrawState;
307 GrColor fOldColor;
308 };
309
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000310 /// @}
311
312 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000313 /// @name Coverage
314 ////
315
316 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000318 * initial value (after construction or reset()) is 0xff. The constant
319 * coverage is ignored when per-vertex coverage is provided.
320 */
321 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000322 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000323 }
324
325 /**
326 * Version of above that specifies 4 channel per-vertex color. The value
327 * should be premultiplied.
328 */
329 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000330 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000331 }
332
333 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000334 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000335 }
336
337 /// @}
338
339 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000340 /// @name Effect Stages
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000341 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
342 /// shader. Its inputs are the output from the previous stage as well as some variables
343 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
344 /// the fragment position, local coordinates).
345 ///
346 /// The stages are divided into two sets, color-computing and coverage-computing. The final
347 /// color stage produces the final pixel color. The coverage-computing stages function exactly
348 /// as the color-computing but the output of the final coverage stage is treated as a fractional
349 /// pixel coverage rather than as input to the src/dst color blend step.
350 ///
351 /// The input color to the first color-stage is either the constant color or interpolated
352 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
353 /// (usually full-coverage) or interpolated per-vertex coverage.
354 ///
355 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
356 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000357 ////
358
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000359 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
360 GrAssert(NULL != effect);
361 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1));
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000362 return effect;
363 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000364
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000365 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
366 GrAssert(NULL != effect);
367 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000368 return effect;
369 }
370
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000371 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000372 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000373 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000374 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000375 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000376 this->addColorEffect(effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000377 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000378
379 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
380 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
381 this->addCoverageEffect(effect)->unref();
382 }
383
384 void addColorTextureEffect(GrTexture* texture,
385 const SkMatrix& matrix,
386 const GrTextureParams& params) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000387 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000388 this->addColorEffect(effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000389 }
390
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000391 void addCoverageTextureEffect(GrTexture* texture,
392 const SkMatrix& matrix,
393 const GrTextureParams& params) {
394 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
395 this->addCoverageEffect(effect)->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000396 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000397
robertphillips@google.com972265d2012-06-13 18:49:30 +0000398 /**
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000399 * When this object is destroyed it will remove any effects from the draw state that were added
400 * after its constructor.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000401 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000402 class AutoRestoreEffects : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000403 public:
bsalomon@google.com2fad5a82013-06-13 19:47:23 +0000404 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000405
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000406 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {
407 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000408 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000409
410 ~AutoRestoreEffects() { this->set(NULL); }
411
412 void set(GrDrawState* ds) {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000413 if (NULL != fDrawState) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000414 int n = fDrawState->fColorStages.count() - fColorEffectCnt;
415 GrAssert(n >= 0);
416 fDrawState->fColorStages.pop_back_n(n);
417 n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt;
418 GrAssert(n >= 0);
419 fDrawState->fCoverageStages.pop_back_n(n);
420 GR_DEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
421 }
422 fDrawState = ds;
423 if (NULL != ds) {
424 fColorEffectCnt = ds->fColorStages.count();
425 fCoverageEffectCnt = ds->fCoverageStages.count();
426 GR_DEBUGCODE(++ds->fBlockEffectRemovalCnt;)
robertphillips@google.com972265d2012-06-13 18:49:30 +0000427 }
428 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000429
robertphillips@google.com972265d2012-06-13 18:49:30 +0000430 private:
431 GrDrawState* fDrawState;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000432 int fColorEffectCnt;
433 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000434 };
435
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000436 int numColorStages() const { return fColorStages.count(); }
437 int numCoverageStages() const { return fCoverageStages.count(); }
438 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000439
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000440 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
441 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000442
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000443 /**
444 * Checks whether any of the effects will read the dst pixel color.
445 */
bsalomon@google.comd09ab842013-05-15 17:30:26 +0000446 bool willEffectReadDstColor() const;
reed@google.com67e7cde2013-03-20 17:47:16 +0000447
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000448 /// @}
449
450 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000451 /// @name Blending
452 ////
453
454 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000455 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000456 *
457 * The blend function will be:
458 * D' = sat(S*srcCoef + D*dstCoef)
459 *
460 * where D is the existing destination color, S is the incoming source
461 * color, and D' is the new destination color that will be written. sat()
462 * is the saturation function.
463 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000464 * @param srcCoef coefficient applied to the src color.
465 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 */
467 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000468 fCommon.fSrcBlend = srcCoeff;
469 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000470 #if GR_DEBUG
471 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000472 case kDC_GrBlendCoeff:
473 case kIDC_GrBlendCoeff:
474 case kDA_GrBlendCoeff:
475 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000476 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
477 "coverage stages.\n");
478 break;
479 default:
480 break;
481 }
482 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000483 case kSC_GrBlendCoeff:
484 case kISC_GrBlendCoeff:
485 case kSA_GrBlendCoeff:
486 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000487 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
488 "coverage stages.\n");
489 break;
490 default:
491 break;
492 }
493 #endif
494 }
495
bsalomon@google.comca432082013-01-23 19:53:46 +0000496 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
497 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498
499 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
500 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000501 *srcBlendCoeff = fCommon.fSrcBlend;
502 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000503 }
504
505 /**
506 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000507 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000508 * kConstC_GrBlendCoeff
509 * kIConstC_GrBlendCoeff
510 * kConstA_GrBlendCoeff
511 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000512 *
513 * @param constant the constant to set
514 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000515 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000516
517 /**
518 * Retrieves the last value set by setBlendConstant()
519 * @return the blending constant value
520 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000521 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000522
bsalomon@google.com2b446732013-02-12 16:47:41 +0000523 /**
524 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
525 * coverage before the blend will give the correct final destination color. In general it
526 * will not as coverage is applied after blending.
527 */
528 bool canTweakAlphaForCoverage() const;
529
530 /**
531 * Optimizations for blending / coverage to that can be applied based on the current state.
532 */
533 enum BlendOptFlags {
534 /**
535 * No optimization
536 */
537 kNone_BlendOpt = 0,
538 /**
539 * Don't draw at all
540 */
541 kSkipDraw_BlendOptFlag = 0x1,
542 /**
543 * Emit the src color, disable HW blending (replace dst with src)
544 */
545 kDisableBlend_BlendOptFlag = 0x2,
546 /**
547 * The coverage value does not have to be computed separately from alpha, the the output
548 * color can be the modulation of the two.
549 */
550 kCoverageAsAlpha_BlendOptFlag = 0x4,
551 /**
552 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
553 * "don't cares".
554 */
555 kEmitCoverage_BlendOptFlag = 0x8,
556 /**
557 * Emit transparent black instead of the src color, no need to compute coverage.
558 */
559 kEmitTransBlack_BlendOptFlag = 0x10,
560 };
561 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
562
563 /**
564 * Determines what optimizations can be applied based on the blend. The coefficients may have
565 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
566 * params that receive the tweaked coefficients. Normally the function looks at the current
567 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
568 * determine the blend optimizations that would be used if there was partial pixel coverage.
569 *
570 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
571 * playback) must call this function and respect the flags that replace the output color.
572 */
573 BlendOptFlags getBlendOpts(bool forceCoverage = false,
574 GrBlendCoeff* srcCoeff = NULL,
575 GrBlendCoeff* dstCoeff = NULL) const;
576
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000577 /// @}
578
579 ///////////////////////////////////////////////////////////////////////////
580 /// @name View Matrix
581 ////
582
583 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000584 * Sets the view matrix to identity and updates any installed effects to compensate for the
585 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000587 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588
589 /**
590 * Retrieves the current view matrix
591 * @return the current view matrix.
592 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000593 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000594
595 /**
596 * Retrieves the inverse of the current view matrix.
597 *
598 * If the current view matrix is invertible, return true, and if matrix
599 * is non-null, copy the inverse into it. If the current view matrix is
600 * non-invertible, return false and ignore the matrix parameter.
601 *
602 * @param matrix if not null, will receive a copy of the current inverse.
603 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000604 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 // TODO: determine whether we really need to leave matrix unmodified
606 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000607 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000608 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000609 if (matrix) {
610 *matrix = inverse;
611 }
612 return true;
613 }
614 return false;
615 }
616
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000617 ////////////////////////////////////////////////////////////////////////////
618
619 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000620 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000621 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000622 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 class AutoViewMatrixRestore : public ::GrNoncopyable {
624 public:
625 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000626
bsalomon@google.comc7818882013-03-20 19:19:53 +0000627 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000629 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000630 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000631
632 ~AutoViewMatrixRestore() { this->restore(); }
633
bsalomon@google.coma8347462012-10-08 18:59:39 +0000634 /**
635 * Can be called prior to destructor to restore the original matrix.
636 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000637 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000638
bsalomon@google.comc7818882013-03-20 19:19:53 +0000639 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000640
bsalomon@google.com137f1342013-05-29 21:27:53 +0000641 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
642 is not invertible. */
643 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000644
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000645 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000646 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
647
648 GrDrawState* fDrawState;
649 SkMatrix fViewMatrix;
650 int fNumColorStages;
651 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000652 };
653
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000654 /// @}
655
656 ///////////////////////////////////////////////////////////////////////////
657 /// @name Render Target
658 ////
659
660 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000661 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000662 *
663 * @param target The render target to set.
664 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000665 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000666 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000667 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000668
669 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000670 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 *
672 * @return The currently set render target.
673 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000674 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
675 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676
677 class AutoRenderTargetRestore : public ::GrNoncopyable {
678 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000679 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000680 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
681 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000682 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 this->set(ds, newTarget);
684 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000685 ~AutoRenderTargetRestore() { this->restore(); }
686
687 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 if (NULL != fDrawState) {
689 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000690 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000692 GrSafeSetNull(fSavedTarget);
693 }
694
695 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
696 this->restore();
697
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000698 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000699 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000701 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000703 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000704 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000705 }
706 private:
707 GrDrawState* fDrawState;
708 GrRenderTarget* fSavedTarget;
709 };
710
711 /// @}
712
713 ///////////////////////////////////////////////////////////////////////////
714 /// @name Stencil
715 ////
716
717 /**
718 * Sets the stencil settings to use for the next draw.
719 * Changing the clip has the side-effect of possibly zeroing
720 * out the client settable stencil bits. So multipass algorithms
721 * using stencil should not change the clip between passes.
722 * @param settings the stencil settings to use.
723 */
724 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000725 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000726 }
727
728 /**
729 * Shortcut to disable stencil testing and ops.
730 */
731 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000732 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000733 }
734
bsalomon@google.comca432082013-01-23 19:53:46 +0000735 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000736
bsalomon@google.comca432082013-01-23 19:53:46 +0000737 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000738
739 /// @}
740
741 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 /// @name State Flags
743 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000744
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000745 /**
746 * Flags that affect rendering. Controlled using enable/disableState(). All
747 * default to disabled.
748 */
749 enum StateBits {
750 /**
751 * Perform dithering. TODO: Re-evaluate whether we need this bit
752 */
753 kDither_StateBit = 0x01,
754 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000755 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
756 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
757 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000758 */
759 kHWAntialias_StateBit = 0x02,
760 /**
761 * Draws will respect the clip, otherwise the clip is ignored.
762 */
763 kClip_StateBit = 0x04,
764 /**
765 * Disables writing to the color buffer. Useful when performing stencil
766 * operations.
767 */
768 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000769
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000770 /**
771 * Usually coverage is applied after color blending. The color is blended using the coeffs
772 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
773 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
774 * this case there is no distinction between coverage and color and the caller needs direct
775 * control over the blend coeffs. When set, there will be a single blend step controlled by
776 * setBlendFunc() which will use coverage*color as the src color.
777 */
778 kCoverageDrawing_StateBit = 0x10,
779
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000780 // Users of the class may add additional bits to the vector
781 kDummyStateBit,
782 kLastPublicStateBit = kDummyStateBit-1,
783 };
784
785 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000786 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000787 }
788
789 /**
790 * Enable render state settings.
791 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000792 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000793 */
794 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000795 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000796 }
797
798 /**
799 * Disable render state settings.
800 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000801 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000802 */
803 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000804 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000805 }
806
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000807 /**
808 * Enable or disable stateBits based on a boolean.
809 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000810 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000811 * @param enable if true enable stateBits, otherwise disable
812 */
813 void setState(uint32_t stateBits, bool enable) {
814 if (enable) {
815 this->enableState(stateBits);
816 } else {
817 this->disableState(stateBits);
818 }
819 }
820
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000822 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000823 }
824
825 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000826 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000827 }
828
829 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000830 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000831 }
832
833 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000834 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000835 }
836
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000837 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000838 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000839 }
840
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000841 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000842 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000843 }
844
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000845 /// @}
846
847 ///////////////////////////////////////////////////////////////////////////
848 /// @name Face Culling
849 ////
850
851 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000852 kInvalid_DrawFace = -1,
853
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000854 kBoth_DrawFace,
855 kCCW_DrawFace,
856 kCW_DrawFace,
857 };
858
859 /**
860 * Controls whether clockwise, counterclockwise, or both faces are drawn.
861 * @param face the face(s) to draw.
862 */
863 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000864 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000865 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866 }
867
868 /**
869 * Gets whether the target is drawing clockwise, counterclockwise,
870 * or both faces.
871 * @return the current draw face(s).
872 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000873 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000874
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000875 /// @}
876
877 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000878
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000879 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000880 if (fRenderTarget.get() != s.fRenderTarget.get() ||
881 fColorStages.count() != s.fColorStages.count() ||
882 fCoverageStages.count() != s.fCoverageStages.count() ||
883 fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000884 return false;
885 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000886 for (int i = 0; i < fColorStages.count(); i++) {
887 if (fColorStages[i] != s.fColorStages[i]) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000888 return false;
889 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000890 }
891 for (int i = 0; i < fCoverageStages.count(); i++) {
892 if (fCoverageStages[i] != s.fCoverageStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000893 return false;
894 }
895 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000896 return true;
897 }
898 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
899
bsalomon@google.comca432082013-01-23 19:53:46 +0000900 GrDrawState& operator= (const GrDrawState& s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000901 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comca432082013-01-23 19:53:46 +0000902 this->setRenderTarget(s.fRenderTarget.get());
903 fCommon = s.fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000904 fColorStages = s.fColorStages;
905 fCoverageStages = s.fCoverageStages;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000906 return *this;
907 }
908
909private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000910
bsalomon@google.com137f1342013-05-29 21:27:53 +0000911 void onReset(const SkMatrix* initialViewMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000912 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
913 fColorStages.reset();
914 fCoverageStages.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000915
916 fRenderTarget.reset(NULL);
917
918 this->setDefaultVertexAttribs();
919
920 fCommon.fColor = 0xffffffff;
921 if (NULL == initialViewMatrix) {
922 fCommon.fViewMatrix.reset();
923 } else {
924 fCommon.fViewMatrix = *initialViewMatrix;
925 }
926 fCommon.fSrcBlend = kOne_GrBlendCoeff;
927 fCommon.fDstBlend = kZero_GrBlendCoeff;
928 fCommon.fBlendConstant = 0x0;
929 fCommon.fFlagBits = 0x0;
930 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000931 fCommon.fCoverage = 0xffffffff;
932 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
933 fCommon.fColorFilterColor = 0x0;
934 fCommon.fDrawFace = kBoth_DrawFace;
935 }
936
bsalomon@google.comca432082013-01-23 19:53:46 +0000937 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
938 struct CommonState {
939 // These fields are roughly sorted by decreasing likelihood of being different in op==
robertphillips@google.com42903302013-04-20 12:26:07 +0000940 GrColor fColor;
941 SkMatrix fViewMatrix;
942 GrBlendCoeff fSrcBlend;
943 GrBlendCoeff fDstBlend;
944 GrColor fBlendConstant;
945 uint32_t fFlagBits;
946 const GrVertexAttrib* fVAPtr;
947 int fVACount;
948 GrStencilSettings fStencilSettings;
robertphillips@google.com42903302013-04-20 12:26:07 +0000949 GrColor fCoverage;
950 SkXfermode::Mode fColorFilterMode;
951 GrColor fColorFilterColor;
952 DrawFace fDrawFace;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000953
954 // This is simply a different representation of info in fVertexAttribs and thus does
955 // not need to be compared in op==.
956 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
957
bsalomon@google.comca432082013-01-23 19:53:46 +0000958 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000959 bool result = fColor == other.fColor &&
960 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
961 fSrcBlend == other.fSrcBlend &&
962 fDstBlend == other.fDstBlend &&
963 fBlendConstant == other.fBlendConstant &&
964 fFlagBits == other.fFlagBits &&
robertphillips@google.com42903302013-04-20 12:26:07 +0000965 fVACount == other.fVACount &&
966 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000967 fStencilSettings == other.fStencilSettings &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000968 fCoverage == other.fCoverage &&
969 fColorFilterMode == other.fColorFilterMode &&
970 fColorFilterColor == other.fColorFilterColor &&
971 fDrawFace == other.fDrawFace;
972 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
973 other.fFixedFunctionVertexAttribIndices,
974 sizeof(fFixedFunctionVertexAttribIndices)));
975 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +0000976 }
977 bool operator!= (const CommonState& other) const { return !(*this == other); }
978 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000979
bsalomon@google.comca432082013-01-23 19:53:46 +0000980 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
981 DeferredState must directly reference GrEffects, however. */
982 struct SavedEffectStage {
983 SavedEffectStage() : fEffect(NULL) {}
984 const GrEffect* fEffect;
985 GrEffectStage::SavedCoordChange fCoordChange;
986 };
987
988public:
989 /**
990 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
991 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
992 * dispose mechanism returns them to the cache. This allows recycling resources through the
993 * the cache while they are in a deferred draw queue.
994 */
995 class DeferredState {
996 public:
997 DeferredState() : fRenderTarget(NULL) {
998 GR_DEBUGCODE(fInitialized = false;)
999 }
1000 // TODO: Remove this when DeferredState no longer holds a ref to the RT
1001 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1002
1003 void saveFrom(const GrDrawState& drawState) {
1004 fCommon = drawState.fCommon;
1005 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1006 fRenderTarget = drawState.fRenderTarget.get();
1007 SkSafeRef(fRenderTarget);
1008 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1009 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1010 // and recycle them to the cache (if no one else is holding a ref to the resources).
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001011 fStages.reset(drawState.fColorStages.count() + drawState.fCoverageStages.count());
1012 fColorStageCnt = drawState.fColorStages.count();
1013 for (int i = 0; i < fColorStageCnt; ++i) {
1014 fStages[i].saveFrom(drawState.fColorStages[i]);
1015 }
1016 for (int i = 0; i < drawState.fCoverageStages.count(); ++i) {
1017 fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i]);
bsalomon@google.comca432082013-01-23 19:53:46 +00001018 }
1019 GR_DEBUGCODE(fInitialized = true;)
1020 }
1021
1022 void restoreTo(GrDrawState* drawState) {
1023 GrAssert(fInitialized);
1024 drawState->fCommon = fCommon;
1025 drawState->setRenderTarget(fRenderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001026 // reinflate color/cov stage arrays.
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001027 drawState->fColorStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001028 for (int i = 0; i < fColorStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001029 SkNEW_APPEND_TO_TARRAY(&drawState->fColorStages, GrEffectStage, (fStages[i]));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001030 }
1031 int coverageStageCnt = fStages.count() - fColorStageCnt;
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001032 drawState->fCoverageStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001033 for (int i = 0; i < coverageStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001034 SkNEW_APPEND_TO_TARRAY(&drawState->fCoverageStages,
1035 GrEffectStage, (fStages[i + fColorStageCnt]));
bsalomon@google.comca432082013-01-23 19:53:46 +00001036 }
1037 }
1038
1039 bool isEqual(const GrDrawState& state) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001040 int numCoverageStages = fStages.count() - fColorStageCnt;
1041 if (fRenderTarget != state.fRenderTarget.get() ||
1042 fColorStageCnt != state.fColorStages.count() ||
1043 numCoverageStages != state.fCoverageStages.count() ||
1044 fCommon != state.fCommon) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001045 return false;
1046 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001047 bool explicitLocalCoords = state.hasLocalCoordAttribute();
1048 for (int i = 0; i < fColorStageCnt; ++i) {
1049 if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoords)) {
1050 return false;
1051 }
1052 }
1053 for (int i = 0; i < numCoverageStages; ++i) {
1054 int s = fColorStageCnt + i;
1055 if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalCoords)) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001056 return false;
1057 }
1058 }
1059 return true;
1060 }
1061
1062 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001063 typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArray;
1064
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001065 GrRenderTarget* fRenderTarget;
1066 CommonState fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001067 int fColorStageCnt;
1068 DeferredStageArray fStages;
bsalomon@google.comca432082013-01-23 19:53:46 +00001069
1070 GR_DEBUGCODE(bool fInitialized;)
1071 };
1072
1073private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001074
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001075 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1076 CommonState fCommon;
1077
1078 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
1079 EffectStageArray fColorStages;
1080 EffectStageArray fCoverageStages;
1081
1082 // Some of the auto restore objects assume that no effects are removed during their lifetime.
1083 // This is used to assert that this condition holds.
1084 GR_DEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001085
robertphillips@google.com42903302013-04-20 12:26:07 +00001086 /**
1087 * Sets vertex attributes for next draw.
1088 *
1089 * @param attribs the array of vertex attributes to set.
1090 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
1091 */
1092 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
1093
reed@google.comfa35e3d2012-06-26 20:16:17 +00001094 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001095};
1096
bsalomon@google.com2b446732013-02-12 16:47:41 +00001097GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1098
tomhudson@google.com93813632011-10-27 20:21:16 +00001099#endif