blob: c006e6c5dd5de69fc93a8cd9385498069c8472be [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"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000015#include "GrPoint.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000016#include "GrRefCnt.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000017#include "GrRenderTarget.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000018#include "GrStencil.h"
19#include "GrTemplates.h"
20#include "GrTexture.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000021#include "GrTypesPriv.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000022#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000023
jvanverth@google.comcc782382013-01-28 20:39:48 +000024#include "SkMatrix.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000025#include "SkXfermode.h"
26
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000027class GrDrawState : public GrRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000028public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000029 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000031 GrDrawState() {
32 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
33 this->reset();
34 }
tomhudson@google.com93813632011-10-27 20:21:16 +000035
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000036 GrDrawState(const SkMatrix& initialViewMatrix) {
37 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
38 this->reset(initialViewMatrix);
39 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000040
41 /**
42 * Copies another draw state.
43 **/
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000044 GrDrawState(const GrDrawState& state) : INHERITED() {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000045 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000046 *this = state;
47 }
48
bsalomon@google.com137f1342013-05-29 21:27:53 +000049 /**
50 * Copies another draw state with a preconcat to the view matrix.
51 **/
52 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000053 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com137f1342013-05-29 21:27:53 +000054 *this = state;
55 if (!preConcatMatrix.isIdentity()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000056 for (int i = 0; i < fColorStages.count(); ++i) {
57 fColorStages[i].localCoordChange(preConcatMatrix);
58 }
59 for (int i = 0; i < fCoverageStages.count(); ++i) {
60 fCoverageStages[i].localCoordChange(preConcatMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +000061 }
62 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000063 }
64
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000065 virtual ~GrDrawState() { GrAssert(0 == fBlockEffectRemovalCnt); }
bsalomon@google.com137f1342013-05-29 21:27:53 +000066
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000067 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +000068 * Resets to the default state. GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000069 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000070 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000071
bsalomon@google.com137f1342013-05-29 21:27:53 +000072 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000073
74 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000075 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
76 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000077 * equivalents are set to default values. Clipping will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000078 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000079 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000080
81 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000082 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000083 ////
84
jvanverth@google.com9b855c72013-03-01 18:21:22 +000085 enum {
jvanverth@google.com054ae992013-04-01 20:06:51 +000086 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000087 };
88
jvanverth@google.com9b855c72013-03-01 18:21:22 +000089 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000090 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
91 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
92 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000093 *
jvanverth@google.com054ae992013-04-01 20:06:51 +000094 * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when
95 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000096 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000097
jvanverth@google.com9b855c72013-03-01 18:21:22 +000098 /**
robertphillips@google.com42903302013-04-20 12:26:07 +000099 * Sets vertex attributes for next draw. The object driving the templatization
100 * should be a global GrVertexAttrib array that is never changed.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000101 */
robertphillips@google.com42903302013-04-20 12:26:07 +0000102 template <const GrVertexAttrib A[]> void setVertexAttribs(int count) {
103 this->setVertexAttribs(A, count);
104 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000105
robertphillips@google.com42903302013-04-20 12:26:07 +0000106 const GrVertexAttrib* getVertexAttribs() const { return fCommon.fVAPtr; }
107 int getVertexAttribCount() const { return fCommon.fVACount; }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000108
109 size_t getVertexSize() const;
110
111 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000112 * Sets default vertex attributes for next draw. The default is a single attribute:
113 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000114 */
115 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000116
jvanverth@google.com054ae992013-04-01 20:06:51 +0000117 /**
118 * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the
119 * binding does not appear in the current attribs. These bindings should appear only once in
120 * the attrib array.
121 */
122
123 int positionAttributeIndex() const {
124 return fCommon.fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
125 }
126 int localCoordAttributeIndex() const {
127 return fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
128 }
129 int colorVertexAttributeIndex() const {
130 return fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
131 }
132 int coverageVertexAttributeIndex() const {
133 return fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
134 }
135
136 bool hasLocalCoordAttribute() const {
137 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
138 }
139 bool hasColorVertexAttribute() const {
140 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
141 }
142 bool hasCoverageVertexAttribute() const {
143 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
144 }
145
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000146 bool validateVertexAttribs() const;
147
jvanverth@google.comcc782382013-01-28 20:39:48 +0000148 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000149 * Helper to save/restore vertex attribs
150 */
151 class AutoVertexAttribRestore {
152 public:
153 AutoVertexAttribRestore(GrDrawState* drawState) {
154 GrAssert(NULL != drawState);
155 fDrawState = drawState;
robertphillips@google.com42903302013-04-20 12:26:07 +0000156 fVAPtr = drawState->fCommon.fVAPtr;
157 fVACount = drawState->fCommon.fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000158 fDrawState->setDefaultVertexAttribs();
159 }
160
161 ~AutoVertexAttribRestore(){
robertphillips@google.com42903302013-04-20 12:26:07 +0000162 fDrawState->fCommon.fVAPtr = fVAPtr;
163 fDrawState->fCommon.fVACount = fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000164 }
165
166 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000167 GrDrawState* fDrawState;
168 const GrVertexAttrib* fVAPtr;
169 int fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000170 };
171
172 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000173 * Accessing positions, local coords, or colors, of a vertex within an array is a hassle
174 * involving casts and simple math. These helpers exist to keep GrDrawTarget clients' code a bit
175 * nicer looking.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000176 */
177
178 /**
179 * Gets a pointer to a GrPoint of a vertex's position or texture
180 * coordinate.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000181 * @param vertices the vertex array
jvanverth@google.comcc782382013-01-28 20:39:48 +0000182 * @param vertexIndex the index of the vertex in the array
183 * @param vertexSize the size of each vertex in the array
184 * @param offset the offset in bytes of the vertex component.
185 * Defaults to zero (corresponding to vertex position)
186 * @return pointer to the vertex component as a GrPoint
187 */
188 static GrPoint* GetVertexPoint(void* vertices,
189 int vertexIndex,
190 int vertexSize,
191 int offset = 0) {
192 intptr_t start = GrTCast<intptr_t>(vertices);
193 return GrTCast<GrPoint*>(start + offset +
194 vertexIndex * vertexSize);
195 }
196 static const GrPoint* GetVertexPoint(const void* vertices,
197 int vertexIndex,
198 int vertexSize,
199 int offset = 0) {
200 intptr_t start = GrTCast<intptr_t>(vertices);
201 return GrTCast<const GrPoint*>(start + offset +
202 vertexIndex * vertexSize);
203 }
204
205 /**
206 * Gets a pointer to a GrColor inside a vertex within a vertex array.
207 * @param vertices the vetex array
208 * @param vertexIndex the index of the vertex in the array
209 * @param vertexSize the size of each vertex in the array
210 * @param offset the offset in bytes of the vertex color
211 * @return pointer to the vertex component as a GrColor
212 */
213 static GrColor* GetVertexColor(void* vertices,
214 int vertexIndex,
215 int vertexSize,
216 int offset) {
217 intptr_t start = GrTCast<intptr_t>(vertices);
218 return GrTCast<GrColor*>(start + offset +
219 vertexIndex * vertexSize);
220 }
221 static const GrColor* GetVertexColor(const void* vertices,
222 int vertexIndex,
223 int vertexSize,
224 int offset) {
225 const intptr_t start = GrTCast<intptr_t>(vertices);
226 return GrTCast<const GrColor*>(start + offset +
227 vertexIndex * vertexSize);
228 }
229
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000230 /// @}
231
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000232 /**
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000233 * Determines whether src alpha is guaranteed to be one for all src pixels
234 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000235 bool srcAlphaWillBeOne() const;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000236
237 /**
238 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
239 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000240 bool hasSolidCoverage() const;
jvanverth@google.comcc782382013-01-28 20:39:48 +0000241
242 /// @}
243
244 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000245 /// @name Color
246 ////
247
248 /**
249 * Sets color for next draw to a premultiplied-alpha color.
250 *
251 * @param color the color to set.
252 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000253 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000254
bsalomon@google.comca432082013-01-23 19:53:46 +0000255 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000256
257 /**
258 * Sets the color to be used for the next draw to be
259 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
260 *
261 * @param alpha The alpha value to set as the color.
262 */
263 void setAlpha(uint8_t a) {
264 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
265 }
266
267 /**
268 * Add a color filter that can be represented by a color and a mode. Applied
bsalomon@google.comc7818882013-03-20 19:19:53 +0000269 * after color-computing effect stages.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000270 */
271 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000272 fCommon.fColorFilterColor = c;
273 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000274 }
275
bsalomon@google.comca432082013-01-23 19:53:46 +0000276 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
277 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000278
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000279 /**
280 * Constructor sets the color to be 'color' which is undone by the destructor.
281 */
282 class AutoColorRestore : public ::GrNoncopyable {
283 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000284 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000285
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000286 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000287 fDrawState = NULL;
288 this->set(drawState, color);
289 }
290
291 void reset() {
292 if (NULL != fDrawState) {
293 fDrawState->setColor(fOldColor);
294 fDrawState = NULL;
295 }
296 }
297
298 void set(GrDrawState* drawState, GrColor color) {
299 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000300 fDrawState = drawState;
301 fOldColor = fDrawState->getColor();
302 fDrawState->setColor(color);
303 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000304
305 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000306 private:
307 GrDrawState* fDrawState;
308 GrColor fOldColor;
309 };
310
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000311 /// @}
312
313 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000314 /// @name Coverage
315 ////
316
317 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000318 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000319 * initial value (after construction or reset()) is 0xff. The constant
320 * coverage is ignored when per-vertex coverage is provided.
321 */
322 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000323 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000324 }
325
326 /**
327 * Version of above that specifies 4 channel per-vertex color. The value
328 * should be premultiplied.
329 */
330 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000331 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000332 }
333
334 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000335 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000336 }
337
338 /// @}
339
340 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000341 /// @name Effect Stages
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000342 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
343 /// shader. Its inputs are the output from the previous stage as well as some variables
344 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
345 /// the fragment position, local coordinates).
346 ///
347 /// The stages are divided into two sets, color-computing and coverage-computing. The final
348 /// color stage produces the final pixel color. The coverage-computing stages function exactly
349 /// as the color-computing but the output of the final coverage stage is treated as a fractional
350 /// pixel coverage rather than as input to the src/dst color blend step.
351 ///
352 /// The input color to the first color-stage is either the constant color or interpolated
353 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
354 /// (usually full-coverage) or interpolated per-vertex coverage.
355 ///
356 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
357 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000358 ////
359
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000360 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
361 GrAssert(NULL != effect);
362 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1));
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000363 return effect;
364 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000365
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000366 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
367 GrAssert(NULL != effect);
368 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000369 return effect;
370 }
371
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000372 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000373 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000374 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000375 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000376 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000377 this->addColorEffect(effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000378 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000379
380 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
381 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
382 this->addCoverageEffect(effect)->unref();
383 }
384
385 void addColorTextureEffect(GrTexture* texture,
386 const SkMatrix& matrix,
387 const GrTextureParams& params) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000388 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000389 this->addColorEffect(effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000390 }
391
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000392 void addCoverageTextureEffect(GrTexture* texture,
393 const SkMatrix& matrix,
394 const GrTextureParams& params) {
395 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
396 this->addCoverageEffect(effect)->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000397 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000398
robertphillips@google.com972265d2012-06-13 18:49:30 +0000399 /**
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000400 * When this object is destroyed it will remove any effects from the draw state that were added
401 * after its constructor.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000402 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000403 class AutoRestoreEffects : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000404 public:
bsalomon@google.com2fad5a82013-06-13 19:47:23 +0000405 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000406
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000407 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {
408 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000409 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000410
411 ~AutoRestoreEffects() { this->set(NULL); }
412
413 void set(GrDrawState* ds) {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000414 if (NULL != fDrawState) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000415 int n = fDrawState->fColorStages.count() - fColorEffectCnt;
416 GrAssert(n >= 0);
417 fDrawState->fColorStages.pop_back_n(n);
418 n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt;
419 GrAssert(n >= 0);
420 fDrawState->fCoverageStages.pop_back_n(n);
421 GR_DEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
422 }
423 fDrawState = ds;
424 if (NULL != ds) {
425 fColorEffectCnt = ds->fColorStages.count();
426 fCoverageEffectCnt = ds->fCoverageStages.count();
427 GR_DEBUGCODE(++ds->fBlockEffectRemovalCnt;)
robertphillips@google.com972265d2012-06-13 18:49:30 +0000428 }
429 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000430
robertphillips@google.com972265d2012-06-13 18:49:30 +0000431 private:
432 GrDrawState* fDrawState;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000433 int fColorEffectCnt;
434 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000435 };
436
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000437 int numColorStages() const { return fColorStages.count(); }
438 int numCoverageStages() const { return fCoverageStages.count(); }
439 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000440
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000441 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
442 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000443
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000444 /**
445 * Checks whether any of the effects will read the dst pixel color.
446 */
bsalomon@google.comd09ab842013-05-15 17:30:26 +0000447 bool willEffectReadDstColor() const;
reed@google.com67e7cde2013-03-20 17:47:16 +0000448
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000449 /// @}
450
451 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000452 /// @name Blending
453 ////
454
455 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000456 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000457 *
458 * The blend function will be:
459 * D' = sat(S*srcCoef + D*dstCoef)
460 *
461 * where D is the existing destination color, S is the incoming source
462 * color, and D' is the new destination color that will be written. sat()
463 * is the saturation function.
464 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000465 * @param srcCoef coefficient applied to the src color.
466 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000467 */
468 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000469 fCommon.fSrcBlend = srcCoeff;
470 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000471 #if GR_DEBUG
472 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000473 case kDC_GrBlendCoeff:
474 case kIDC_GrBlendCoeff:
475 case kDA_GrBlendCoeff:
476 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000477 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
478 "coverage stages.\n");
479 break;
480 default:
481 break;
482 }
483 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000484 case kSC_GrBlendCoeff:
485 case kISC_GrBlendCoeff:
486 case kSA_GrBlendCoeff:
487 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000488 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
489 "coverage stages.\n");
490 break;
491 default:
492 break;
493 }
494 #endif
495 }
496
bsalomon@google.comca432082013-01-23 19:53:46 +0000497 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
498 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000499
500 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
501 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000502 *srcBlendCoeff = fCommon.fSrcBlend;
503 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000504 }
505
506 /**
507 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000508 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000509 * kConstC_GrBlendCoeff
510 * kIConstC_GrBlendCoeff
511 * kConstA_GrBlendCoeff
512 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000513 *
514 * @param constant the constant to set
515 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000516 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000517
518 /**
519 * Retrieves the last value set by setBlendConstant()
520 * @return the blending constant value
521 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000522 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523
bsalomon@google.com2b446732013-02-12 16:47:41 +0000524 /**
525 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
526 * coverage before the blend will give the correct final destination color. In general it
527 * will not as coverage is applied after blending.
528 */
529 bool canTweakAlphaForCoverage() const;
530
531 /**
532 * Optimizations for blending / coverage to that can be applied based on the current state.
533 */
534 enum BlendOptFlags {
535 /**
536 * No optimization
537 */
538 kNone_BlendOpt = 0,
539 /**
540 * Don't draw at all
541 */
542 kSkipDraw_BlendOptFlag = 0x1,
543 /**
544 * Emit the src color, disable HW blending (replace dst with src)
545 */
546 kDisableBlend_BlendOptFlag = 0x2,
547 /**
548 * The coverage value does not have to be computed separately from alpha, the the output
549 * color can be the modulation of the two.
550 */
551 kCoverageAsAlpha_BlendOptFlag = 0x4,
552 /**
553 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
554 * "don't cares".
555 */
556 kEmitCoverage_BlendOptFlag = 0x8,
557 /**
558 * Emit transparent black instead of the src color, no need to compute coverage.
559 */
560 kEmitTransBlack_BlendOptFlag = 0x10,
561 };
562 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
563
564 /**
565 * Determines what optimizations can be applied based on the blend. The coefficients may have
566 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
567 * params that receive the tweaked coefficients. Normally the function looks at the current
568 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
569 * determine the blend optimizations that would be used if there was partial pixel coverage.
570 *
571 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
572 * playback) must call this function and respect the flags that replace the output color.
573 */
574 BlendOptFlags getBlendOpts(bool forceCoverage = false,
575 GrBlendCoeff* srcCoeff = NULL,
576 GrBlendCoeff* dstCoeff = NULL) const;
577
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000578 /// @}
579
580 ///////////////////////////////////////////////////////////////////////////
581 /// @name View Matrix
582 ////
583
584 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000585 * Sets the view matrix to identity and updates any installed effects to compensate for the
586 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000587 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000588 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000589
590 /**
591 * Retrieves the current view matrix
592 * @return the current view matrix.
593 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000594 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595
596 /**
597 * Retrieves the inverse of the current view matrix.
598 *
599 * If the current view matrix is invertible, return true, and if matrix
600 * is non-null, copy the inverse into it. If the current view matrix is
601 * non-invertible, return false and ignore the matrix parameter.
602 *
603 * @param matrix if not null, will receive a copy of the current inverse.
604 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000605 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000606 // TODO: determine whether we really need to leave matrix unmodified
607 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000608 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000609 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 if (matrix) {
611 *matrix = inverse;
612 }
613 return true;
614 }
615 return false;
616 }
617
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000618 ////////////////////////////////////////////////////////////////////////////
619
620 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000621 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000622 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000623 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000624 class AutoViewMatrixRestore : public ::GrNoncopyable {
625 public:
626 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000627
bsalomon@google.comc7818882013-03-20 19:19:53 +0000628 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000629 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000630 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000631 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000632
633 ~AutoViewMatrixRestore() { this->restore(); }
634
bsalomon@google.coma8347462012-10-08 18:59:39 +0000635 /**
636 * Can be called prior to destructor to restore the original matrix.
637 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000638 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000639
bsalomon@google.comc7818882013-03-20 19:19:53 +0000640 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000641
bsalomon@google.com137f1342013-05-29 21:27:53 +0000642 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
643 is not invertible. */
644 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000645
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000646 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000647 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
648
649 GrDrawState* fDrawState;
650 SkMatrix fViewMatrix;
651 int fNumColorStages;
652 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000653 };
654
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000655 /// @}
656
657 ///////////////////////////////////////////////////////////////////////////
658 /// @name Render Target
659 ////
660
661 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000662 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000663 *
664 * @param target The render target to set.
665 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000666 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000667 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000668 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000669
670 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000671 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000672 *
673 * @return The currently set render target.
674 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000675 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
676 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000677
678 class AutoRenderTargetRestore : public ::GrNoncopyable {
679 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000680 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000681 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
682 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000683 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000684 this->set(ds, newTarget);
685 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000686 ~AutoRenderTargetRestore() { this->restore(); }
687
688 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000689 if (NULL != fDrawState) {
690 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000691 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000692 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000693 GrSafeSetNull(fSavedTarget);
694 }
695
696 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
697 this->restore();
698
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000699 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000700 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000701 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000702 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000703 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000704 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000705 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 }
707 private:
708 GrDrawState* fDrawState;
709 GrRenderTarget* fSavedTarget;
710 };
711
712 /// @}
713
714 ///////////////////////////////////////////////////////////////////////////
715 /// @name Stencil
716 ////
717
718 /**
719 * Sets the stencil settings to use for the next draw.
720 * Changing the clip has the side-effect of possibly zeroing
721 * out the client settable stencil bits. So multipass algorithms
722 * using stencil should not change the clip between passes.
723 * @param settings the stencil settings to use.
724 */
725 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000726 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000727 }
728
729 /**
730 * Shortcut to disable stencil testing and ops.
731 */
732 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000733 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000734 }
735
bsalomon@google.comca432082013-01-23 19:53:46 +0000736 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000737
bsalomon@google.comca432082013-01-23 19:53:46 +0000738 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000739
740 /// @}
741
742 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000743 /// @name State Flags
744 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000745
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000746 /**
747 * Flags that affect rendering. Controlled using enable/disableState(). All
748 * default to disabled.
749 */
750 enum StateBits {
751 /**
752 * Perform dithering. TODO: Re-evaluate whether we need this bit
753 */
754 kDither_StateBit = 0x01,
755 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000756 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
757 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
758 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000759 */
760 kHWAntialias_StateBit = 0x02,
761 /**
762 * Draws will respect the clip, otherwise the clip is ignored.
763 */
764 kClip_StateBit = 0x04,
765 /**
766 * Disables writing to the color buffer. Useful when performing stencil
767 * operations.
768 */
769 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000770
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000771 /**
772 * Usually coverage is applied after color blending. The color is blended using the coeffs
773 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
774 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
775 * this case there is no distinction between coverage and color and the caller needs direct
776 * control over the blend coeffs. When set, there will be a single blend step controlled by
777 * setBlendFunc() which will use coverage*color as the src color.
778 */
779 kCoverageDrawing_StateBit = 0x10,
780
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000781 // Users of the class may add additional bits to the vector
782 kDummyStateBit,
783 kLastPublicStateBit = kDummyStateBit-1,
784 };
785
786 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000787 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000788 }
789
790 /**
791 * Enable render state settings.
792 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000793 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000794 */
795 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000796 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000797 }
798
799 /**
800 * Disable render state settings.
801 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000802 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803 */
804 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000805 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000806 }
807
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000808 /**
809 * Enable or disable stateBits based on a boolean.
810 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000811 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000812 * @param enable if true enable stateBits, otherwise disable
813 */
814 void setState(uint32_t stateBits, bool enable) {
815 if (enable) {
816 this->enableState(stateBits);
817 } else {
818 this->disableState(stateBits);
819 }
820 }
821
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000823 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 }
825
826 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000827 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000828 }
829
830 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000831 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 }
833
834 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000835 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000836 }
837
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000838 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000839 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000840 }
841
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000842 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000843 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000844 }
845
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000846 /// @}
847
848 ///////////////////////////////////////////////////////////////////////////
849 /// @name Face Culling
850 ////
851
852 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000853 kInvalid_DrawFace = -1,
854
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 kBoth_DrawFace,
856 kCCW_DrawFace,
857 kCW_DrawFace,
858 };
859
860 /**
861 * Controls whether clockwise, counterclockwise, or both faces are drawn.
862 * @param face the face(s) to draw.
863 */
864 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000865 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000866 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000867 }
868
869 /**
870 * Gets whether the target is drawing clockwise, counterclockwise,
871 * or both faces.
872 * @return the current draw face(s).
873 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000874 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000875
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000876 /// @}
877
878 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000879
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000880 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000881 if (fRenderTarget.get() != s.fRenderTarget.get() ||
882 fColorStages.count() != s.fColorStages.count() ||
883 fCoverageStages.count() != s.fCoverageStages.count() ||
884 fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000885 return false;
886 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000887 for (int i = 0; i < fColorStages.count(); i++) {
888 if (fColorStages[i] != s.fColorStages[i]) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000889 return false;
890 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000891 }
892 for (int i = 0; i < fCoverageStages.count(); i++) {
893 if (fCoverageStages[i] != s.fCoverageStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000894 return false;
895 }
896 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000897 return true;
898 }
899 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
900
bsalomon@google.comca432082013-01-23 19:53:46 +0000901 GrDrawState& operator= (const GrDrawState& s) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000902 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comca432082013-01-23 19:53:46 +0000903 this->setRenderTarget(s.fRenderTarget.get());
904 fCommon = s.fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000905 fColorStages = s.fColorStages;
906 fCoverageStages = s.fCoverageStages;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000907 return *this;
908 }
909
910private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000911
bsalomon@google.com137f1342013-05-29 21:27:53 +0000912 void onReset(const SkMatrix* initialViewMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000913 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
914 fColorStages.reset();
915 fCoverageStages.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000916
917 fRenderTarget.reset(NULL);
918
919 this->setDefaultVertexAttribs();
920
921 fCommon.fColor = 0xffffffff;
922 if (NULL == initialViewMatrix) {
923 fCommon.fViewMatrix.reset();
924 } else {
925 fCommon.fViewMatrix = *initialViewMatrix;
926 }
927 fCommon.fSrcBlend = kOne_GrBlendCoeff;
928 fCommon.fDstBlend = kZero_GrBlendCoeff;
929 fCommon.fBlendConstant = 0x0;
930 fCommon.fFlagBits = 0x0;
931 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000932 fCommon.fCoverage = 0xffffffff;
933 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
934 fCommon.fColorFilterColor = 0x0;
935 fCommon.fDrawFace = kBoth_DrawFace;
936 }
937
bsalomon@google.comca432082013-01-23 19:53:46 +0000938 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
939 struct CommonState {
940 // These fields are roughly sorted by decreasing likelihood of being different in op==
robertphillips@google.com42903302013-04-20 12:26:07 +0000941 GrColor fColor;
942 SkMatrix fViewMatrix;
943 GrBlendCoeff fSrcBlend;
944 GrBlendCoeff fDstBlend;
945 GrColor fBlendConstant;
946 uint32_t fFlagBits;
947 const GrVertexAttrib* fVAPtr;
948 int fVACount;
949 GrStencilSettings fStencilSettings;
robertphillips@google.com42903302013-04-20 12:26:07 +0000950 GrColor fCoverage;
951 SkXfermode::Mode fColorFilterMode;
952 GrColor fColorFilterColor;
953 DrawFace fDrawFace;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000954
955 // This is simply a different representation of info in fVertexAttribs and thus does
956 // not need to be compared in op==.
957 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
958
bsalomon@google.comca432082013-01-23 19:53:46 +0000959 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000960 bool result = fColor == other.fColor &&
961 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
962 fSrcBlend == other.fSrcBlend &&
963 fDstBlend == other.fDstBlend &&
964 fBlendConstant == other.fBlendConstant &&
965 fFlagBits == other.fFlagBits &&
robertphillips@google.com42903302013-04-20 12:26:07 +0000966 fVACount == other.fVACount &&
967 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000968 fStencilSettings == other.fStencilSettings &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000969 fCoverage == other.fCoverage &&
970 fColorFilterMode == other.fColorFilterMode &&
971 fColorFilterColor == other.fColorFilterColor &&
972 fDrawFace == other.fDrawFace;
973 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
974 other.fFixedFunctionVertexAttribIndices,
975 sizeof(fFixedFunctionVertexAttribIndices)));
976 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +0000977 }
978 bool operator!= (const CommonState& other) const { return !(*this == other); }
979 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000980
bsalomon@google.comca432082013-01-23 19:53:46 +0000981 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
982 DeferredState must directly reference GrEffects, however. */
983 struct SavedEffectStage {
984 SavedEffectStage() : fEffect(NULL) {}
985 const GrEffect* fEffect;
986 GrEffectStage::SavedCoordChange fCoordChange;
987 };
988
989public:
990 /**
991 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
992 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
993 * dispose mechanism returns them to the cache. This allows recycling resources through the
994 * the cache while they are in a deferred draw queue.
995 */
996 class DeferredState {
997 public:
998 DeferredState() : fRenderTarget(NULL) {
999 GR_DEBUGCODE(fInitialized = false;)
1000 }
1001 // TODO: Remove this when DeferredState no longer holds a ref to the RT
1002 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1003
1004 void saveFrom(const GrDrawState& drawState) {
1005 fCommon = drawState.fCommon;
1006 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1007 fRenderTarget = drawState.fRenderTarget.get();
1008 SkSafeRef(fRenderTarget);
1009 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1010 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1011 // 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 +00001012 fStages.reset(drawState.fColorStages.count() + drawState.fCoverageStages.count());
1013 fColorStageCnt = drawState.fColorStages.count();
1014 for (int i = 0; i < fColorStageCnt; ++i) {
1015 fStages[i].saveFrom(drawState.fColorStages[i]);
1016 }
1017 for (int i = 0; i < drawState.fCoverageStages.count(); ++i) {
1018 fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i]);
bsalomon@google.comca432082013-01-23 19:53:46 +00001019 }
1020 GR_DEBUGCODE(fInitialized = true;)
1021 }
1022
1023 void restoreTo(GrDrawState* drawState) {
1024 GrAssert(fInitialized);
1025 drawState->fCommon = fCommon;
1026 drawState->setRenderTarget(fRenderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001027 // reinflate color/cov stage arrays.
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001028 drawState->fColorStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001029 for (int i = 0; i < fColorStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001030 SkNEW_APPEND_TO_TARRAY(&drawState->fColorStages, GrEffectStage, (fStages[i]));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001031 }
1032 int coverageStageCnt = fStages.count() - fColorStageCnt;
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001033 drawState->fCoverageStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001034 for (int i = 0; i < coverageStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001035 SkNEW_APPEND_TO_TARRAY(&drawState->fCoverageStages,
1036 GrEffectStage, (fStages[i + fColorStageCnt]));
bsalomon@google.comca432082013-01-23 19:53:46 +00001037 }
1038 }
1039
1040 bool isEqual(const GrDrawState& state) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001041 int numCoverageStages = fStages.count() - fColorStageCnt;
1042 if (fRenderTarget != state.fRenderTarget.get() ||
1043 fColorStageCnt != state.fColorStages.count() ||
1044 numCoverageStages != state.fCoverageStages.count() ||
1045 fCommon != state.fCommon) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001046 return false;
1047 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001048 bool explicitLocalCoords = state.hasLocalCoordAttribute();
1049 for (int i = 0; i < fColorStageCnt; ++i) {
1050 if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoords)) {
1051 return false;
1052 }
1053 }
1054 for (int i = 0; i < numCoverageStages; ++i) {
1055 int s = fColorStageCnt + i;
1056 if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalCoords)) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001057 return false;
1058 }
1059 }
1060 return true;
1061 }
1062
1063 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001064 typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArray;
1065
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001066 GrRenderTarget* fRenderTarget;
1067 CommonState fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001068 int fColorStageCnt;
1069 DeferredStageArray fStages;
bsalomon@google.comca432082013-01-23 19:53:46 +00001070
1071 GR_DEBUGCODE(bool fInitialized;)
1072 };
1073
1074private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001075
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001076 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1077 CommonState fCommon;
1078
1079 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
1080 EffectStageArray fColorStages;
1081 EffectStageArray fCoverageStages;
1082
1083 // Some of the auto restore objects assume that no effects are removed during their lifetime.
1084 // This is used to assert that this condition holds.
1085 GR_DEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001086
robertphillips@google.com42903302013-04-20 12:26:07 +00001087 /**
1088 * Sets vertex attributes for next draw.
1089 *
1090 * @param attribs the array of vertex attributes to set.
1091 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
1092 */
1093 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
1094
reed@google.comfa35e3d2012-06-26 20:16:17 +00001095 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001096};
1097
bsalomon@google.com2b446732013-02-12 16:47:41 +00001098GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1099
tomhudson@google.com93813632011-10-27 20:21:16 +00001100#endif