blob: 2e4d7f86c44397598192d3f2567965dac21e3b25 [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"
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000012#include "GrBlend.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000013#include "GrColor.h"
bsalomon@google.com08283af2012-10-26 13:01:20 +000014#include "GrEffectStage.h"
bsalomon@google.com73818dc2013-03-28 13:23:29 +000015#include "GrPaint.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000016#include "GrPoint.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"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000025#include "SkTypes.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000026#include "SkXfermode.h"
27
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000028class GrDrawState : public SkRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000029public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000030 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000031
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000032 GrDrawState() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000033 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000034 this->reset();
35 }
tomhudson@google.com93813632011-10-27 20:21:16 +000036
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000037 GrDrawState(const SkMatrix& initialViewMatrix) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000038 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000039 this->reset(initialViewMatrix);
40 }
bsalomon@google.com137f1342013-05-29 21:27:53 +000041
42 /**
43 * Copies another draw state.
44 **/
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000045 GrDrawState(const GrDrawState& state) : INHERITED() {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000046 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000047 *this = state;
48 }
49
bsalomon@google.com137f1342013-05-29 21:27:53 +000050 /**
51 * Copies another draw state with a preconcat to the view matrix.
52 **/
53 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000054 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
bsalomon@google.com137f1342013-05-29 21:27:53 +000055 *this = state;
56 if (!preConcatMatrix.isIdentity()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000057 for (int i = 0; i < fColorStages.count(); ++i) {
58 fColorStages[i].localCoordChange(preConcatMatrix);
59 }
60 for (int i = 0; i < fCoverageStages.count(); ++i) {
61 fCoverageStages[i].localCoordChange(preConcatMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +000062 }
63 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000064 }
65
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 virtual ~GrDrawState() { SkASSERT(0 == fBlockEffectRemovalCnt); }
bsalomon@google.com137f1342013-05-29 21:27:53 +000067
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000068 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +000069 * Resets to the default state. GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +000070 */
bsalomon@google.com137f1342013-05-29 21:27:53 +000071 void reset() { this->onReset(NULL); }
robertphillips@google.com9ec07532012-06-22 12:01:30 +000072
bsalomon@google.com137f1342013-05-29 21:27:53 +000073 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMatrix); }
bsalomon@google.comaf84e742012-10-05 13:23:24 +000074
75 /**
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000076 * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
77 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000078 * equivalents are set to default values. Clipping will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000079 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000080 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000081
82 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +000083 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +000084 ////
85
jvanverth@google.com9b855c72013-03-01 18:21:22 +000086 enum {
jvanverth@google.com054ae992013-04-01 20:06:51 +000087 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000088 };
89
jvanverth@google.com9b855c72013-03-01 18:21:22 +000090 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000091 * The format of vertices is represented as an array of GrVertexAttribs, with each representing
92 * the type of the attribute, its offset, and semantic binding (see GrVertexAttrib in
93 * GrTypesPriv.h).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000094 *
jvanverth@google.com054ae992013-04-01 20:06:51 +000095 * The mapping of attributes with kEffect bindings to GrEffect inputs is specified when
96 * setEffect is called.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +000097 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000098
jvanverth@google.com9b855c72013-03-01 18:21:22 +000099 /**
robertphillips@google.com42903302013-04-20 12:26:07 +0000100 * Sets vertex attributes for next draw. The object driving the templatization
101 * should be a global GrVertexAttrib array that is never changed.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000102 */
robertphillips@google.com42903302013-04-20 12:26:07 +0000103 template <const GrVertexAttrib A[]> void setVertexAttribs(int count) {
104 this->setVertexAttribs(A, count);
105 }
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000106
robertphillips@google.com42903302013-04-20 12:26:07 +0000107 const GrVertexAttrib* getVertexAttribs() const { return fCommon.fVAPtr; }
108 int getVertexAttribCount() const { return fCommon.fVACount; }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000109
110 size_t getVertexSize() const;
111
112 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000113 * Sets default vertex attributes for next draw. The default is a single attribute:
114 * {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribType}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000115 */
116 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000117
jvanverth@google.com054ae992013-04-01 20:06:51 +0000118 /**
119 * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the
120 * binding does not appear in the current attribs. These bindings should appear only once in
121 * the attrib array.
122 */
123
124 int positionAttributeIndex() const {
125 return fCommon.fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
126 }
127 int localCoordAttributeIndex() const {
128 return fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
129 }
130 int colorVertexAttributeIndex() const {
131 return fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
132 }
133 int coverageVertexAttributeIndex() const {
134 return fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
135 }
136
137 bool hasLocalCoordAttribute() const {
138 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
139 }
140 bool hasColorVertexAttribute() const {
141 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
142 }
143 bool hasCoverageVertexAttribute() const {
144 return -1 != fCommon.fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
145 }
146
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000147 bool validateVertexAttribs() const;
148
jvanverth@google.comcc782382013-01-28 20:39:48 +0000149 /**
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000150 * Helper to save/restore vertex attribs
151 */
152 class AutoVertexAttribRestore {
153 public:
154 AutoVertexAttribRestore(GrDrawState* drawState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000155 SkASSERT(NULL != drawState);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000156 fDrawState = drawState;
robertphillips@google.com42903302013-04-20 12:26:07 +0000157 fVAPtr = drawState->fCommon.fVAPtr;
158 fVACount = drawState->fCommon.fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000159 fDrawState->setDefaultVertexAttribs();
160 }
161
162 ~AutoVertexAttribRestore(){
commit-bot@chromium.orgfe070ba2013-10-16 14:43:12 +0000163 fDrawState->setVertexAttribs(fVAPtr, 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 /**
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000268 * Constructor sets the color to be 'color' which is undone by the destructor.
269 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000270 class AutoColorRestore : public ::SkNoncopyable {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000271 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000272 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000273
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000274 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000275 fDrawState = NULL;
276 this->set(drawState, color);
277 }
278
279 void reset() {
280 if (NULL != fDrawState) {
281 fDrawState->setColor(fOldColor);
282 fDrawState = NULL;
283 }
284 }
285
286 void set(GrDrawState* drawState, GrColor color) {
287 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000288 fDrawState = drawState;
289 fOldColor = fDrawState->getColor();
290 fDrawState->setColor(color);
291 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000292
293 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000294 private:
295 GrDrawState* fDrawState;
296 GrColor fOldColor;
297 };
298
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000299 /// @}
300
301 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000302 /// @name Coverage
303 ////
304
305 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000306 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000307 * initial value (after construction or reset()) is 0xff. The constant
308 * coverage is ignored when per-vertex coverage is provided.
309 */
310 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000311 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000312 }
313
314 /**
315 * Version of above that specifies 4 channel per-vertex color. The value
316 * should be premultiplied.
317 */
318 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000319 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000320 }
321
322 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000323 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000324 }
325
326 /// @}
327
328 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000329 /// @name Effect Stages
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000330 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
331 /// shader. Its inputs are the output from the previous stage as well as some variables
332 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
333 /// the fragment position, local coordinates).
334 ///
335 /// The stages are divided into two sets, color-computing and coverage-computing. The final
336 /// color stage produces the final pixel color. The coverage-computing stages function exactly
337 /// as the color-computing but the output of the final coverage stage is treated as a fractional
338 /// pixel coverage rather than as input to the src/dst color blend step.
339 ///
340 /// The input color to the first color-stage is either the constant color or interpolated
341 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
342 /// (usually full-coverage) or interpolated per-vertex coverage.
343 ///
344 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
345 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000346 ////
347
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000348 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000349 SkASSERT(NULL != effect);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000350 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1));
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000351 return effect;
352 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000353
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000354 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000355 SkASSERT(NULL != effect);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000356 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000357 return effect;
358 }
359
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000360 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000361 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000362 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000363 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000364 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000365 this->addColorEffect(effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000366 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000367
368 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
369 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
370 this->addCoverageEffect(effect)->unref();
371 }
372
373 void addColorTextureEffect(GrTexture* texture,
374 const SkMatrix& matrix,
375 const GrTextureParams& params) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000376 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000377 this->addColorEffect(effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000378 }
379
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000380 void addCoverageTextureEffect(GrTexture* texture,
381 const SkMatrix& matrix,
382 const GrTextureParams& params) {
383 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
384 this->addCoverageEffect(effect)->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000385 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000386
robertphillips@google.com972265d2012-06-13 18:49:30 +0000387 /**
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000388 * When this object is destroyed it will remove any effects from the draw state that were added
389 * after its constructor.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000390 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000391 class AutoRestoreEffects : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000392 public:
bsalomon@google.com2fad5a82013-06-13 19:47:23 +0000393 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000394
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000395 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {
396 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000397 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000398
399 ~AutoRestoreEffects() { this->set(NULL); }
400
401 void set(GrDrawState* ds) {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000402 if (NULL != fDrawState) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000403 int n = fDrawState->fColorStages.count() - fColorEffectCnt;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000404 SkASSERT(n >= 0);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000405 fDrawState->fColorStages.pop_back_n(n);
406 n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000407 SkASSERT(n >= 0);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000408 fDrawState->fCoverageStages.pop_back_n(n);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000409 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000410 }
411 fDrawState = ds;
412 if (NULL != ds) {
413 fColorEffectCnt = ds->fColorStages.count();
414 fCoverageEffectCnt = ds->fCoverageStages.count();
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000415 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;)
robertphillips@google.com972265d2012-06-13 18:49:30 +0000416 }
417 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000418
robertphillips@google.com972265d2012-06-13 18:49:30 +0000419 private:
420 GrDrawState* fDrawState;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000421 int fColorEffectCnt;
422 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000423 };
424
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000425 int numColorStages() const { return fColorStages.count(); }
426 int numCoverageStages() const { return fCoverageStages.count(); }
427 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000428
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000429 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
430 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000431
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000432 /**
433 * Checks whether any of the effects will read the dst pixel color.
434 */
bsalomon@google.comd09ab842013-05-15 17:30:26 +0000435 bool willEffectReadDstColor() const;
reed@google.com67e7cde2013-03-20 17:47:16 +0000436
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437 /// @}
438
439 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000440 /// @name Blending
441 ////
442
443 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000444 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000445 *
446 * The blend function will be:
447 * D' = sat(S*srcCoef + D*dstCoef)
448 *
449 * where D is the existing destination color, S is the incoming source
450 * color, and D' is the new destination color that will be written. sat()
451 * is the saturation function.
452 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000453 * @param srcCoef coefficient applied to the src color.
454 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000455 */
456 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000457 fCommon.fSrcBlend = srcCoeff;
458 fCommon.fDstBlend = dstCoeff;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000459 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000460 if (GrBlendCoeffRefsDst(dstCoeff)) {
461 GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000462 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000463 if (GrBlendCoeffRefsSrc(srcCoeff)) {
464 GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000465 }
466 #endif
467 }
468
bsalomon@google.comca432082013-01-23 19:53:46 +0000469 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
470 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000471
472 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
473 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000474 *srcBlendCoeff = fCommon.fSrcBlend;
475 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000476 }
477
478 /**
479 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000480 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000481 * kConstC_GrBlendCoeff
482 * kIConstC_GrBlendCoeff
483 * kConstA_GrBlendCoeff
484 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 *
486 * @param constant the constant to set
487 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000488 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000489
490 /**
491 * Retrieves the last value set by setBlendConstant()
492 * @return the blending constant value
493 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000494 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000495
bsalomon@google.com2b446732013-02-12 16:47:41 +0000496 /**
497 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
498 * coverage before the blend will give the correct final destination color. In general it
499 * will not as coverage is applied after blending.
500 */
501 bool canTweakAlphaForCoverage() const;
502
503 /**
504 * Optimizations for blending / coverage to that can be applied based on the current state.
505 */
506 enum BlendOptFlags {
507 /**
508 * No optimization
509 */
510 kNone_BlendOpt = 0,
511 /**
512 * Don't draw at all
513 */
514 kSkipDraw_BlendOptFlag = 0x1,
515 /**
516 * Emit the src color, disable HW blending (replace dst with src)
517 */
518 kDisableBlend_BlendOptFlag = 0x2,
519 /**
520 * The coverage value does not have to be computed separately from alpha, the the output
521 * color can be the modulation of the two.
522 */
523 kCoverageAsAlpha_BlendOptFlag = 0x4,
524 /**
525 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
526 * "don't cares".
527 */
528 kEmitCoverage_BlendOptFlag = 0x8,
529 /**
530 * Emit transparent black instead of the src color, no need to compute coverage.
531 */
532 kEmitTransBlack_BlendOptFlag = 0x10,
533 };
534 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
535
536 /**
537 * Determines what optimizations can be applied based on the blend. The coefficients may have
538 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
539 * params that receive the tweaked coefficients. Normally the function looks at the current
540 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
541 * determine the blend optimizations that would be used if there was partial pixel coverage.
542 *
543 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
544 * playback) must call this function and respect the flags that replace the output color.
545 */
546 BlendOptFlags getBlendOpts(bool forceCoverage = false,
547 GrBlendCoeff* srcCoeff = NULL,
548 GrBlendCoeff* dstCoeff = NULL) const;
549
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000550 /// @}
551
552 ///////////////////////////////////////////////////////////////////////////
553 /// @name View Matrix
554 ////
555
556 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000557 * Sets the view matrix to identity and updates any installed effects to compensate for the
558 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000559 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000560 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000561
562 /**
563 * Retrieves the current view matrix
564 * @return the current view matrix.
565 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000566 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000567
568 /**
569 * Retrieves the inverse of the current view matrix.
570 *
571 * If the current view matrix is invertible, return true, and if matrix
572 * is non-null, copy the inverse into it. If the current view matrix is
573 * non-invertible, return false and ignore the matrix parameter.
574 *
575 * @param matrix if not null, will receive a copy of the current inverse.
576 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000577 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000578 // TODO: determine whether we really need to leave matrix unmodified
579 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000580 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000581 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000582 if (matrix) {
583 *matrix = inverse;
584 }
585 return true;
586 }
587 return false;
588 }
589
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000590 ////////////////////////////////////////////////////////////////////////////
591
592 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000593 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000594 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000595 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000596 class AutoViewMatrixRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000597 public:
598 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000599
bsalomon@google.comc7818882013-03-20 19:19:53 +0000600 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000602 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000604
605 ~AutoViewMatrixRestore() { this->restore(); }
606
bsalomon@google.coma8347462012-10-08 18:59:39 +0000607 /**
608 * Can be called prior to destructor to restore the original matrix.
609 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000610 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000611
bsalomon@google.comc7818882013-03-20 19:19:53 +0000612 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000613
bsalomon@google.com137f1342013-05-29 21:27:53 +0000614 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
615 is not invertible. */
616 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000617
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000618 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000619 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
620
621 GrDrawState* fDrawState;
622 SkMatrix fViewMatrix;
623 int fNumColorStages;
624 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000625 };
626
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000627 /// @}
628
629 ///////////////////////////////////////////////////////////////////////////
630 /// @name Render Target
631 ////
632
633 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000634 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000635 *
636 * @param target The render target to set.
637 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000638 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000639 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000640 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000641
642 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000643 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000644 *
645 * @return The currently set render target.
646 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000647 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
648 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000649
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000650 class AutoRenderTargetRestore : public ::SkNoncopyable {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000651 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000652 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000653 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
654 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000655 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000656 this->set(ds, newTarget);
657 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000658 ~AutoRenderTargetRestore() { this->restore(); }
659
660 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000661 if (NULL != fDrawState) {
662 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000663 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000664 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000665 SkSafeSetNull(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000666 }
667
668 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
669 this->restore();
670
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 if (NULL != ds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000672 SkASSERT(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000673 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000674 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000675 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000676 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000677 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 }
679 private:
680 GrDrawState* fDrawState;
681 GrRenderTarget* fSavedTarget;
682 };
683
684 /// @}
685
686 ///////////////////////////////////////////////////////////////////////////
687 /// @name Stencil
688 ////
689
690 /**
691 * Sets the stencil settings to use for the next draw.
692 * Changing the clip has the side-effect of possibly zeroing
693 * out the client settable stencil bits. So multipass algorithms
694 * using stencil should not change the clip between passes.
695 * @param settings the stencil settings to use.
696 */
697 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000698 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000699 }
700
701 /**
702 * Shortcut to disable stencil testing and ops.
703 */
704 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000705 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 }
707
bsalomon@google.comca432082013-01-23 19:53:46 +0000708 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000709
bsalomon@google.comca432082013-01-23 19:53:46 +0000710 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000711
712 /// @}
713
714 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000715 /// @name State Flags
716 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000717
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000718 /**
719 * Flags that affect rendering. Controlled using enable/disableState(). All
720 * default to disabled.
721 */
722 enum StateBits {
723 /**
724 * Perform dithering. TODO: Re-evaluate whether we need this bit
725 */
726 kDither_StateBit = 0x01,
727 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000728 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
729 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
730 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000731 */
732 kHWAntialias_StateBit = 0x02,
733 /**
734 * Draws will respect the clip, otherwise the clip is ignored.
735 */
736 kClip_StateBit = 0x04,
737 /**
738 * Disables writing to the color buffer. Useful when performing stencil
739 * operations.
740 */
741 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000742
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000743 /**
744 * Usually coverage is applied after color blending. The color is blended using the coeffs
745 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
746 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
747 * this case there is no distinction between coverage and color and the caller needs direct
748 * control over the blend coeffs. When set, there will be a single blend step controlled by
749 * setBlendFunc() which will use coverage*color as the src color.
750 */
751 kCoverageDrawing_StateBit = 0x10,
752
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000753 // Users of the class may add additional bits to the vector
754 kDummyStateBit,
755 kLastPublicStateBit = kDummyStateBit-1,
756 };
757
758 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000759 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000760 }
761
762 /**
763 * Enable render state settings.
764 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000765 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000766 */
767 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000768 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000769 }
770
771 /**
772 * Disable render state settings.
773 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000774 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000775 */
776 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000777 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000778 }
779
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000780 /**
781 * Enable or disable stateBits based on a boolean.
782 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000783 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000784 * @param enable if true enable stateBits, otherwise disable
785 */
786 void setState(uint32_t stateBits, bool enable) {
787 if (enable) {
788 this->enableState(stateBits);
789 } else {
790 this->disableState(stateBits);
791 }
792 }
793
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000794 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000795 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000796 }
797
798 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000799 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 }
801
802 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000803 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000804 }
805
806 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000807 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000808 }
809
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000810 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000811 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000812 }
813
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000814 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000815 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816 }
817
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818 /// @}
819
820 ///////////////////////////////////////////////////////////////////////////
821 /// @name Face Culling
822 ////
823
824 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000825 kInvalid_DrawFace = -1,
826
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000827 kBoth_DrawFace,
828 kCCW_DrawFace,
829 kCW_DrawFace,
830 };
831
832 /**
833 * Controls whether clockwise, counterclockwise, or both faces are drawn.
834 * @param face the face(s) to draw.
835 */
836 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000837 SkASSERT(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000838 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 }
840
841 /**
842 * Gets whether the target is drawing clockwise, counterclockwise,
843 * or both faces.
844 * @return the current draw face(s).
845 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000846 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000847
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000848 /// @}
849
850 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000851
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000852 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000853 if (fRenderTarget.get() != s.fRenderTarget.get() ||
854 fColorStages.count() != s.fColorStages.count() ||
855 fCoverageStages.count() != s.fCoverageStages.count() ||
856 fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000857 return false;
858 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000859 for (int i = 0; i < fColorStages.count(); i++) {
860 if (fColorStages[i] != s.fColorStages[i]) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000861 return false;
862 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000863 }
864 for (int i = 0; i < fCoverageStages.count(); i++) {
865 if (fCoverageStages[i] != s.fCoverageStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000866 return false;
867 }
868 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000869 return true;
870 }
871 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
872
bsalomon@google.comca432082013-01-23 19:53:46 +0000873 GrDrawState& operator= (const GrDrawState& s) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000874 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comca432082013-01-23 19:53:46 +0000875 this->setRenderTarget(s.fRenderTarget.get());
876 fCommon = s.fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000877 fColorStages = s.fColorStages;
878 fCoverageStages = s.fCoverageStages;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000879 return *this;
880 }
881
882private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000883
bsalomon@google.com137f1342013-05-29 21:27:53 +0000884 void onReset(const SkMatrix* initialViewMatrix) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000885 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000886 fColorStages.reset();
887 fCoverageStages.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000888
889 fRenderTarget.reset(NULL);
890
891 this->setDefaultVertexAttribs();
892
893 fCommon.fColor = 0xffffffff;
894 if (NULL == initialViewMatrix) {
895 fCommon.fViewMatrix.reset();
896 } else {
897 fCommon.fViewMatrix = *initialViewMatrix;
898 }
899 fCommon.fSrcBlend = kOne_GrBlendCoeff;
900 fCommon.fDstBlend = kZero_GrBlendCoeff;
901 fCommon.fBlendConstant = 0x0;
902 fCommon.fFlagBits = 0x0;
903 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000904 fCommon.fCoverage = 0xffffffff;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000905 fCommon.fDrawFace = kBoth_DrawFace;
906 }
907
bsalomon@google.comca432082013-01-23 19:53:46 +0000908 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
909 struct CommonState {
910 // These fields are roughly sorted by decreasing likelihood of being different in op==
robertphillips@google.com42903302013-04-20 12:26:07 +0000911 GrColor fColor;
912 SkMatrix fViewMatrix;
913 GrBlendCoeff fSrcBlend;
914 GrBlendCoeff fDstBlend;
915 GrColor fBlendConstant;
916 uint32_t fFlagBits;
917 const GrVertexAttrib* fVAPtr;
918 int fVACount;
919 GrStencilSettings fStencilSettings;
robertphillips@google.com42903302013-04-20 12:26:07 +0000920 GrColor fCoverage;
robertphillips@google.com42903302013-04-20 12:26:07 +0000921 DrawFace fDrawFace;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000922
923 // This is simply a different representation of info in fVertexAttribs and thus does
924 // not need to be compared in op==.
925 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
926
bsalomon@google.comca432082013-01-23 19:53:46 +0000927 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000928 bool result = fColor == other.fColor &&
929 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
930 fSrcBlend == other.fSrcBlend &&
931 fDstBlend == other.fDstBlend &&
932 fBlendConstant == other.fBlendConstant &&
933 fFlagBits == other.fFlagBits &&
robertphillips@google.com42903302013-04-20 12:26:07 +0000934 fVACount == other.fVACount &&
935 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000936 fStencilSettings == other.fStencilSettings &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000937 fCoverage == other.fCoverage &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000938 fDrawFace == other.fDrawFace;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000939 SkASSERT(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
jvanverth@google.com054ae992013-04-01 20:06:51 +0000940 other.fFixedFunctionVertexAttribIndices,
941 sizeof(fFixedFunctionVertexAttribIndices)));
942 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +0000943 }
944 bool operator!= (const CommonState& other) const { return !(*this == other); }
945 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000946
bsalomon@google.comca432082013-01-23 19:53:46 +0000947 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
948 DeferredState must directly reference GrEffects, however. */
949 struct SavedEffectStage {
950 SavedEffectStage() : fEffect(NULL) {}
951 const GrEffect* fEffect;
952 GrEffectStage::SavedCoordChange fCoordChange;
953 };
954
955public:
956 /**
957 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
958 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
959 * dispose mechanism returns them to the cache. This allows recycling resources through the
960 * the cache while they are in a deferred draw queue.
961 */
962 class DeferredState {
963 public:
964 DeferredState() : fRenderTarget(NULL) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000965 SkDEBUGCODE(fInitialized = false;)
bsalomon@google.comca432082013-01-23 19:53:46 +0000966 }
967 // TODO: Remove this when DeferredState no longer holds a ref to the RT
968 ~DeferredState() { SkSafeUnref(fRenderTarget); }
969
970 void saveFrom(const GrDrawState& drawState) {
971 fCommon = drawState.fCommon;
972 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
973 fRenderTarget = drawState.fRenderTarget.get();
974 SkSafeRef(fRenderTarget);
975 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
976 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
977 // 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 +0000978 fStages.reset(drawState.fColorStages.count() + drawState.fCoverageStages.count());
979 fColorStageCnt = drawState.fColorStages.count();
980 for (int i = 0; i < fColorStageCnt; ++i) {
981 fStages[i].saveFrom(drawState.fColorStages[i]);
982 }
983 for (int i = 0; i < drawState.fCoverageStages.count(); ++i) {
984 fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i]);
bsalomon@google.comca432082013-01-23 19:53:46 +0000985 }
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000986 SkDEBUGCODE(fInitialized = true;)
bsalomon@google.comca432082013-01-23 19:53:46 +0000987 }
988
989 void restoreTo(GrDrawState* drawState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000990 SkASSERT(fInitialized);
bsalomon@google.comca432082013-01-23 19:53:46 +0000991 drawState->fCommon = fCommon;
992 drawState->setRenderTarget(fRenderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000993 // reinflate color/cov stage arrays.
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +0000994 drawState->fColorStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000995 for (int i = 0; i < fColorStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +0000996 SkNEW_APPEND_TO_TARRAY(&drawState->fColorStages, GrEffectStage, (fStages[i]));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000997 }
998 int coverageStageCnt = fStages.count() - fColorStageCnt;
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +0000999 drawState->fCoverageStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001000 for (int i = 0; i < coverageStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001001 SkNEW_APPEND_TO_TARRAY(&drawState->fCoverageStages,
1002 GrEffectStage, (fStages[i + fColorStageCnt]));
bsalomon@google.comca432082013-01-23 19:53:46 +00001003 }
1004 }
1005
1006 bool isEqual(const GrDrawState& state) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001007 int numCoverageStages = fStages.count() - fColorStageCnt;
1008 if (fRenderTarget != state.fRenderTarget.get() ||
1009 fColorStageCnt != state.fColorStages.count() ||
1010 numCoverageStages != state.fCoverageStages.count() ||
1011 fCommon != state.fCommon) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001012 return false;
1013 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001014 bool explicitLocalCoords = state.hasLocalCoordAttribute();
1015 for (int i = 0; i < fColorStageCnt; ++i) {
1016 if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoords)) {
1017 return false;
1018 }
1019 }
1020 for (int i = 0; i < numCoverageStages; ++i) {
1021 int s = fColorStageCnt + i;
1022 if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalCoords)) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001023 return false;
1024 }
1025 }
1026 return true;
1027 }
1028
1029 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001030 typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArray;
1031
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001032 GrRenderTarget* fRenderTarget;
1033 CommonState fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001034 int fColorStageCnt;
1035 DeferredStageArray fStages;
bsalomon@google.comca432082013-01-23 19:53:46 +00001036
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +00001037 SkDEBUGCODE(bool fInitialized;)
bsalomon@google.comca432082013-01-23 19:53:46 +00001038 };
1039
1040private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001041
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001042 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1043 CommonState fCommon;
1044
1045 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
1046 EffectStageArray fColorStages;
1047 EffectStageArray fCoverageStages;
1048
1049 // Some of the auto restore objects assume that no effects are removed during their lifetime.
1050 // This is used to assert that this condition holds.
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +00001051 SkDEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001052
robertphillips@google.com42903302013-04-20 12:26:07 +00001053 /**
1054 * Sets vertex attributes for next draw.
1055 *
1056 * @param attribs the array of vertex attributes to set.
1057 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
1058 */
1059 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
1060
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +00001061 typedef SkRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001062};
1063
bsalomon@google.com2b446732013-02-12 16:47:41 +00001064GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1065
tomhudson@google.com93813632011-10-27 20:21:16 +00001066#endif