blob: 8704fe4084247b982a5d76678d799aeda3106e99 [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
406 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL) { this->set(ds); }
407
408 ~AutoRestoreEffects() { this->set(NULL); }
409
410 void set(GrDrawState* ds) {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000411 if (NULL != fDrawState) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000412 int n = fDrawState->fColorStages.count() - fColorEffectCnt;
413 GrAssert(n >= 0);
414 fDrawState->fColorStages.pop_back_n(n);
415 n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt;
416 GrAssert(n >= 0);
417 fDrawState->fCoverageStages.pop_back_n(n);
418 GR_DEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
419 }
420 fDrawState = ds;
421 if (NULL != ds) {
422 fColorEffectCnt = ds->fColorStages.count();
423 fCoverageEffectCnt = ds->fCoverageStages.count();
424 GR_DEBUGCODE(++ds->fBlockEffectRemovalCnt;)
robertphillips@google.com972265d2012-06-13 18:49:30 +0000425 }
426 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000427
robertphillips@google.com972265d2012-06-13 18:49:30 +0000428 private:
429 GrDrawState* fDrawState;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000430 int fColorEffectCnt;
431 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000432 };
433
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000434 int numColorStages() const { return fColorStages.count(); }
435 int numCoverageStages() const { return fCoverageStages.count(); }
436 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000438 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
439 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000440
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000441 /**
442 * Checks whether any of the effects will read the dst pixel color.
443 */
bsalomon@google.comd09ab842013-05-15 17:30:26 +0000444 bool willEffectReadDstColor() const;
reed@google.com67e7cde2013-03-20 17:47:16 +0000445
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000446 /// @}
447
448 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000449 /// @name Blending
450 ////
451
452 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000453 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000454 *
455 * The blend function will be:
456 * D' = sat(S*srcCoef + D*dstCoef)
457 *
458 * where D is the existing destination color, S is the incoming source
459 * color, and D' is the new destination color that will be written. sat()
460 * is the saturation function.
461 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000462 * @param srcCoef coefficient applied to the src color.
463 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000464 */
465 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000466 fCommon.fSrcBlend = srcCoeff;
467 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000468 #if GR_DEBUG
469 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000470 case kDC_GrBlendCoeff:
471 case kIDC_GrBlendCoeff:
472 case kDA_GrBlendCoeff:
473 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000474 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
475 "coverage stages.\n");
476 break;
477 default:
478 break;
479 }
480 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000481 case kSC_GrBlendCoeff:
482 case kISC_GrBlendCoeff:
483 case kSA_GrBlendCoeff:
484 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
486 "coverage stages.\n");
487 break;
488 default:
489 break;
490 }
491 #endif
492 }
493
bsalomon@google.comca432082013-01-23 19:53:46 +0000494 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
495 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000496
497 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
498 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000499 *srcBlendCoeff = fCommon.fSrcBlend;
500 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000501 }
502
503 /**
504 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000505 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000506 * kConstC_GrBlendCoeff
507 * kIConstC_GrBlendCoeff
508 * kConstA_GrBlendCoeff
509 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000510 *
511 * @param constant the constant to set
512 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000513 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000514
515 /**
516 * Retrieves the last value set by setBlendConstant()
517 * @return the blending constant value
518 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000519 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000520
bsalomon@google.com2b446732013-02-12 16:47:41 +0000521 /**
522 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
523 * coverage before the blend will give the correct final destination color. In general it
524 * will not as coverage is applied after blending.
525 */
526 bool canTweakAlphaForCoverage() const;
527
528 /**
529 * Optimizations for blending / coverage to that can be applied based on the current state.
530 */
531 enum BlendOptFlags {
532 /**
533 * No optimization
534 */
535 kNone_BlendOpt = 0,
536 /**
537 * Don't draw at all
538 */
539 kSkipDraw_BlendOptFlag = 0x1,
540 /**
541 * Emit the src color, disable HW blending (replace dst with src)
542 */
543 kDisableBlend_BlendOptFlag = 0x2,
544 /**
545 * The coverage value does not have to be computed separately from alpha, the the output
546 * color can be the modulation of the two.
547 */
548 kCoverageAsAlpha_BlendOptFlag = 0x4,
549 /**
550 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
551 * "don't cares".
552 */
553 kEmitCoverage_BlendOptFlag = 0x8,
554 /**
555 * Emit transparent black instead of the src color, no need to compute coverage.
556 */
557 kEmitTransBlack_BlendOptFlag = 0x10,
558 };
559 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
560
561 /**
562 * Determines what optimizations can be applied based on the blend. The coefficients may have
563 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
564 * params that receive the tweaked coefficients. Normally the function looks at the current
565 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
566 * determine the blend optimizations that would be used if there was partial pixel coverage.
567 *
568 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
569 * playback) must call this function and respect the flags that replace the output color.
570 */
571 BlendOptFlags getBlendOpts(bool forceCoverage = false,
572 GrBlendCoeff* srcCoeff = NULL,
573 GrBlendCoeff* dstCoeff = NULL) const;
574
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000575 /// @}
576
577 ///////////////////////////////////////////////////////////////////////////
578 /// @name View Matrix
579 ////
580
581 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000582 * Sets the view matrix to identity and updates any installed effects to compensate for the
583 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000584 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000585 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586
587 /**
588 * Retrieves the current view matrix
589 * @return the current view matrix.
590 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000591 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592
593 /**
594 * Retrieves the inverse of the current view matrix.
595 *
596 * If the current view matrix is invertible, return true, and if matrix
597 * is non-null, copy the inverse into it. If the current view matrix is
598 * non-invertible, return false and ignore the matrix parameter.
599 *
600 * @param matrix if not null, will receive a copy of the current inverse.
601 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000602 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 // TODO: determine whether we really need to leave matrix unmodified
604 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000605 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000606 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000607 if (matrix) {
608 *matrix = inverse;
609 }
610 return true;
611 }
612 return false;
613 }
614
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000615 ////////////////////////////////////////////////////////////////////////////
616
617 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000618 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000619 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000620 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000621 class AutoViewMatrixRestore : public ::GrNoncopyable {
622 public:
623 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000624
bsalomon@google.comc7818882013-03-20 19:19:53 +0000625 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000626 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000627 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000629
630 ~AutoViewMatrixRestore() { this->restore(); }
631
bsalomon@google.coma8347462012-10-08 18:59:39 +0000632 /**
633 * Can be called prior to destructor to restore the original matrix.
634 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000635 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000636
bsalomon@google.comc7818882013-03-20 19:19:53 +0000637 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000638
bsalomon@google.com137f1342013-05-29 21:27:53 +0000639 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
640 is not invertible. */
641 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000642
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000643 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000644 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
645
646 GrDrawState* fDrawState;
647 SkMatrix fViewMatrix;
648 int fNumColorStages;
649 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000650 };
651
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000652 /// @}
653
654 ///////////////////////////////////////////////////////////////////////////
655 /// @name Render Target
656 ////
657
658 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000659 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000660 *
661 * @param target The render target to set.
662 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000663 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000664 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000665 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000666
667 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000668 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000669 *
670 * @return The currently set render target.
671 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000672 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
673 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000674
675 class AutoRenderTargetRestore : public ::GrNoncopyable {
676 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000677 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
679 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000680 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000681 this->set(ds, newTarget);
682 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000683 ~AutoRenderTargetRestore() { this->restore(); }
684
685 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000686 if (NULL != fDrawState) {
687 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000688 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000689 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000690 GrSafeSetNull(fSavedTarget);
691 }
692
693 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
694 this->restore();
695
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000696 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000697 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000698 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000699 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000701 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000703 }
704 private:
705 GrDrawState* fDrawState;
706 GrRenderTarget* fSavedTarget;
707 };
708
709 /// @}
710
711 ///////////////////////////////////////////////////////////////////////////
712 /// @name Stencil
713 ////
714
715 /**
716 * Sets the stencil settings to use for the next draw.
717 * Changing the clip has the side-effect of possibly zeroing
718 * out the client settable stencil bits. So multipass algorithms
719 * using stencil should not change the clip between passes.
720 * @param settings the stencil settings to use.
721 */
722 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000723 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724 }
725
726 /**
727 * Shortcut to disable stencil testing and ops.
728 */
729 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000730 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000731 }
732
bsalomon@google.comca432082013-01-23 19:53:46 +0000733 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000734
bsalomon@google.comca432082013-01-23 19:53:46 +0000735 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000736
737 /// @}
738
739 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000740 /// @name State Flags
741 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000742
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000743 /**
744 * Flags that affect rendering. Controlled using enable/disableState(). All
745 * default to disabled.
746 */
747 enum StateBits {
748 /**
749 * Perform dithering. TODO: Re-evaluate whether we need this bit
750 */
751 kDither_StateBit = 0x01,
752 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000753 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
754 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
755 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000756 */
757 kHWAntialias_StateBit = 0x02,
758 /**
759 * Draws will respect the clip, otherwise the clip is ignored.
760 */
761 kClip_StateBit = 0x04,
762 /**
763 * Disables writing to the color buffer. Useful when performing stencil
764 * operations.
765 */
766 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000767
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000768 /**
769 * Usually coverage is applied after color blending. The color is blended using the coeffs
770 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
771 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
772 * this case there is no distinction between coverage and color and the caller needs direct
773 * control over the blend coeffs. When set, there will be a single blend step controlled by
774 * setBlendFunc() which will use coverage*color as the src color.
775 */
776 kCoverageDrawing_StateBit = 0x10,
777
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000778 // Users of the class may add additional bits to the vector
779 kDummyStateBit,
780 kLastPublicStateBit = kDummyStateBit-1,
781 };
782
783 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000784 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000785 }
786
787 /**
788 * Enable render state settings.
789 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000790 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791 */
792 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000793 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000794 }
795
796 /**
797 * Disable render state settings.
798 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000799 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 */
801 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000802 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803 }
804
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000805 /**
806 * Enable or disable stateBits based on a boolean.
807 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000808 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000809 * @param enable if true enable stateBits, otherwise disable
810 */
811 void setState(uint32_t stateBits, bool enable) {
812 if (enable) {
813 this->enableState(stateBits);
814 } else {
815 this->disableState(stateBits);
816 }
817 }
818
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000819 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000820 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 }
822
823 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000824 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000825 }
826
827 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000828 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000829 }
830
831 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000832 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000833 }
834
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000835 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000836 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000837 }
838
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000840 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000841 }
842
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000843 /// @}
844
845 ///////////////////////////////////////////////////////////////////////////
846 /// @name Face Culling
847 ////
848
849 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000850 kInvalid_DrawFace = -1,
851
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852 kBoth_DrawFace,
853 kCCW_DrawFace,
854 kCW_DrawFace,
855 };
856
857 /**
858 * Controls whether clockwise, counterclockwise, or both faces are drawn.
859 * @param face the face(s) to draw.
860 */
861 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000862 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000863 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000864 }
865
866 /**
867 * Gets whether the target is drawing clockwise, counterclockwise,
868 * or both faces.
869 * @return the current draw face(s).
870 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000871 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000872
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000873 /// @}
874
875 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000876
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000877 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000878 if (fRenderTarget.get() != s.fRenderTarget.get() ||
879 fColorStages.count() != s.fColorStages.count() ||
880 fCoverageStages.count() != s.fCoverageStages.count() ||
881 fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000882 return false;
883 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000884 for (int i = 0; i < fColorStages.count(); i++) {
885 if (fColorStages[i] != s.fColorStages[i]) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000886 return false;
887 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000888 }
889 for (int i = 0; i < fCoverageStages.count(); i++) {
890 if (fCoverageStages[i] != s.fCoverageStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000891 return false;
892 }
893 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000894 return true;
895 }
896 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
897
bsalomon@google.comca432082013-01-23 19:53:46 +0000898 GrDrawState& operator= (const GrDrawState& s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000899 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comca432082013-01-23 19:53:46 +0000900 this->setRenderTarget(s.fRenderTarget.get());
901 fCommon = s.fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000902 fColorStages = s.fColorStages;
903 fCoverageStages = s.fCoverageStages;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000904 return *this;
905 }
906
907private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000908
bsalomon@google.com137f1342013-05-29 21:27:53 +0000909 void onReset(const SkMatrix* initialViewMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000910 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
911 fColorStages.reset();
912 fCoverageStages.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000913
914 fRenderTarget.reset(NULL);
915
916 this->setDefaultVertexAttribs();
917
918 fCommon.fColor = 0xffffffff;
919 if (NULL == initialViewMatrix) {
920 fCommon.fViewMatrix.reset();
921 } else {
922 fCommon.fViewMatrix = *initialViewMatrix;
923 }
924 fCommon.fSrcBlend = kOne_GrBlendCoeff;
925 fCommon.fDstBlend = kZero_GrBlendCoeff;
926 fCommon.fBlendConstant = 0x0;
927 fCommon.fFlagBits = 0x0;
928 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000929 fCommon.fCoverage = 0xffffffff;
930 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
931 fCommon.fColorFilterColor = 0x0;
932 fCommon.fDrawFace = kBoth_DrawFace;
933 }
934
bsalomon@google.comca432082013-01-23 19:53:46 +0000935 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
936 struct CommonState {
937 // These fields are roughly sorted by decreasing likelihood of being different in op==
robertphillips@google.com42903302013-04-20 12:26:07 +0000938 GrColor fColor;
939 SkMatrix fViewMatrix;
940 GrBlendCoeff fSrcBlend;
941 GrBlendCoeff fDstBlend;
942 GrColor fBlendConstant;
943 uint32_t fFlagBits;
944 const GrVertexAttrib* fVAPtr;
945 int fVACount;
946 GrStencilSettings fStencilSettings;
robertphillips@google.com42903302013-04-20 12:26:07 +0000947 GrColor fCoverage;
948 SkXfermode::Mode fColorFilterMode;
949 GrColor fColorFilterColor;
950 DrawFace fDrawFace;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000951
952 // This is simply a different representation of info in fVertexAttribs and thus does
953 // not need to be compared in op==.
954 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
955
bsalomon@google.comca432082013-01-23 19:53:46 +0000956 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000957 bool result = fColor == other.fColor &&
958 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
959 fSrcBlend == other.fSrcBlend &&
960 fDstBlend == other.fDstBlend &&
961 fBlendConstant == other.fBlendConstant &&
962 fFlagBits == other.fFlagBits &&
robertphillips@google.com42903302013-04-20 12:26:07 +0000963 fVACount == other.fVACount &&
964 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000965 fStencilSettings == other.fStencilSettings &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000966 fCoverage == other.fCoverage &&
967 fColorFilterMode == other.fColorFilterMode &&
968 fColorFilterColor == other.fColorFilterColor &&
969 fDrawFace == other.fDrawFace;
970 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
971 other.fFixedFunctionVertexAttribIndices,
972 sizeof(fFixedFunctionVertexAttribIndices)));
973 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +0000974 }
975 bool operator!= (const CommonState& other) const { return !(*this == other); }
976 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000977
bsalomon@google.comca432082013-01-23 19:53:46 +0000978 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
979 DeferredState must directly reference GrEffects, however. */
980 struct SavedEffectStage {
981 SavedEffectStage() : fEffect(NULL) {}
982 const GrEffect* fEffect;
983 GrEffectStage::SavedCoordChange fCoordChange;
984 };
985
986public:
987 /**
988 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
989 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
990 * dispose mechanism returns them to the cache. This allows recycling resources through the
991 * the cache while they are in a deferred draw queue.
992 */
993 class DeferredState {
994 public:
995 DeferredState() : fRenderTarget(NULL) {
996 GR_DEBUGCODE(fInitialized = false;)
997 }
998 // TODO: Remove this when DeferredState no longer holds a ref to the RT
999 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1000
1001 void saveFrom(const GrDrawState& drawState) {
1002 fCommon = drawState.fCommon;
1003 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1004 fRenderTarget = drawState.fRenderTarget.get();
1005 SkSafeRef(fRenderTarget);
1006 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1007 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1008 // 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 +00001009 fStages.reset(drawState.fColorStages.count() + drawState.fCoverageStages.count());
1010 fColorStageCnt = drawState.fColorStages.count();
1011 for (int i = 0; i < fColorStageCnt; ++i) {
1012 fStages[i].saveFrom(drawState.fColorStages[i]);
1013 }
1014 for (int i = 0; i < drawState.fCoverageStages.count(); ++i) {
1015 fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i]);
bsalomon@google.comca432082013-01-23 19:53:46 +00001016 }
1017 GR_DEBUGCODE(fInitialized = true;)
1018 }
1019
1020 void restoreTo(GrDrawState* drawState) {
1021 GrAssert(fInitialized);
1022 drawState->fCommon = fCommon;
1023 drawState->setRenderTarget(fRenderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001024 // reinflate color/cov stage arrays.
1025 drawState->fColorStages.reset(fColorStageCnt);
1026 for (int i = 0; i < fColorStageCnt; ++i) {
1027 fStages[i].restoreTo(&drawState->fColorStages[i]);
1028 }
1029 int coverageStageCnt = fStages.count() - fColorStageCnt;
1030 drawState->fCoverageStages.reset(coverageStageCnt);
1031 for (int i = 0; i < coverageStageCnt; ++i) {
1032 fStages[fColorStageCnt + i].restoreTo(&drawState->fCoverageStages[i]);
bsalomon@google.comca432082013-01-23 19:53:46 +00001033 }
1034 }
1035
1036 bool isEqual(const GrDrawState& state) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001037 int numCoverageStages = fStages.count() - fColorStageCnt;
1038 if (fRenderTarget != state.fRenderTarget.get() ||
1039 fColorStageCnt != state.fColorStages.count() ||
1040 numCoverageStages != state.fCoverageStages.count() ||
1041 fCommon != state.fCommon) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001042 return false;
1043 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001044 bool explicitLocalCoords = state.hasLocalCoordAttribute();
1045 for (int i = 0; i < fColorStageCnt; ++i) {
1046 if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoords)) {
1047 return false;
1048 }
1049 }
1050 for (int i = 0; i < numCoverageStages; ++i) {
1051 int s = fColorStageCnt + i;
1052 if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalCoords)) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001053 return false;
1054 }
1055 }
1056 return true;
1057 }
1058
1059 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001060 typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArray;
1061
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001062 GrRenderTarget* fRenderTarget;
1063 CommonState fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001064 int fColorStageCnt;
1065 DeferredStageArray fStages;
bsalomon@google.comca432082013-01-23 19:53:46 +00001066
1067 GR_DEBUGCODE(bool fInitialized;)
1068 };
1069
1070private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001071
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001072 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1073 CommonState fCommon;
1074
1075 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
1076 EffectStageArray fColorStages;
1077 EffectStageArray fCoverageStages;
1078
1079 // Some of the auto restore objects assume that no effects are removed during their lifetime.
1080 // This is used to assert that this condition holds.
1081 GR_DEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001082
robertphillips@google.com42903302013-04-20 12:26:07 +00001083 /**
1084 * Sets vertex attributes for next draw.
1085 *
1086 * @param attribs the array of vertex attributes to set.
1087 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
1088 */
1089 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
1090
reed@google.comfa35e3d2012-06-26 20:16:17 +00001091 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001092};
1093
bsalomon@google.com2b446732013-02-12 16:47:41 +00001094GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1095
tomhudson@google.com93813632011-10-27 20:21:16 +00001096#endif