blob: 25c8cb1d2a15c101ab6ecf58f53ca60fcf411c9b [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"
jvanverth@google.comcc782382013-01-28 20:39:48 +000017#include "GrRefCnt.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000018#include "GrRenderTarget.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000019#include "GrStencil.h"
20#include "GrTemplates.h"
21#include "GrTexture.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000022#include "GrTypesPriv.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000023#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000024
jvanverth@google.comcc782382013-01-28 20:39:48 +000025#include "SkMatrix.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000026#include "SkXfermode.h"
27
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000028class GrDrawState : public GrRefCnt {
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() {
33 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
34 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) {
38 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;)
39 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() {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000046 GR_DEBUGCODE(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) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000054 GR_DEBUGCODE(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(){
robertphillips@google.com42903302013-04-20 12:26:07 +0000163 fDrawState->fCommon.fVAPtr = fVAPtr;
164 fDrawState->fCommon.fVACount = fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000165 }
166
167 private:
robertphillips@google.com42903302013-04-20 12:26:07 +0000168 GrDrawState* fDrawState;
169 const GrVertexAttrib* fVAPtr;
170 int fVACount;
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000171 };
172
173 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000174 * Accessing positions, local coords, or colors, of a vertex within an array is a hassle
175 * involving casts and simple math. These helpers exist to keep GrDrawTarget clients' code a bit
176 * nicer looking.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000177 */
178
179 /**
180 * Gets a pointer to a GrPoint of a vertex's position or texture
181 * coordinate.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000182 * @param vertices the vertex array
jvanverth@google.comcc782382013-01-28 20:39:48 +0000183 * @param vertexIndex the index of the vertex in the array
184 * @param vertexSize the size of each vertex in the array
185 * @param offset the offset in bytes of the vertex component.
186 * Defaults to zero (corresponding to vertex position)
187 * @return pointer to the vertex component as a GrPoint
188 */
189 static GrPoint* GetVertexPoint(void* vertices,
190 int vertexIndex,
191 int vertexSize,
192 int offset = 0) {
193 intptr_t start = GrTCast<intptr_t>(vertices);
194 return GrTCast<GrPoint*>(start + offset +
195 vertexIndex * vertexSize);
196 }
197 static const GrPoint* GetVertexPoint(const void* vertices,
198 int vertexIndex,
199 int vertexSize,
200 int offset = 0) {
201 intptr_t start = GrTCast<intptr_t>(vertices);
202 return GrTCast<const GrPoint*>(start + offset +
203 vertexIndex * vertexSize);
204 }
205
206 /**
207 * Gets a pointer to a GrColor inside a vertex within a vertex array.
208 * @param vertices the vetex array
209 * @param vertexIndex the index of the vertex in the array
210 * @param vertexSize the size of each vertex in the array
211 * @param offset the offset in bytes of the vertex color
212 * @return pointer to the vertex component as a GrColor
213 */
214 static GrColor* GetVertexColor(void* vertices,
215 int vertexIndex,
216 int vertexSize,
217 int offset) {
218 intptr_t start = GrTCast<intptr_t>(vertices);
219 return GrTCast<GrColor*>(start + offset +
220 vertexIndex * vertexSize);
221 }
222 static const GrColor* GetVertexColor(const void* vertices,
223 int vertexIndex,
224 int vertexSize,
225 int offset) {
226 const intptr_t start = GrTCast<intptr_t>(vertices);
227 return GrTCast<const GrColor*>(start + offset +
228 vertexIndex * vertexSize);
229 }
230
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000231 /// @}
232
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000233 /**
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000234 * Determines whether src alpha is guaranteed to be one for all src pixels
235 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000236 bool srcAlphaWillBeOne() const;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000237
238 /**
239 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
240 */
jvanverth@google.com054ae992013-04-01 20:06:51 +0000241 bool hasSolidCoverage() const;
jvanverth@google.comcc782382013-01-28 20:39:48 +0000242
243 /// @}
244
245 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000246 /// @name Color
247 ////
248
249 /**
250 * Sets color for next draw to a premultiplied-alpha color.
251 *
252 * @param color the color to set.
253 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000254 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000255
bsalomon@google.comca432082013-01-23 19:53:46 +0000256 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000257
258 /**
259 * Sets the color to be used for the next draw to be
260 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
261 *
262 * @param alpha The alpha value to set as the color.
263 */
264 void setAlpha(uint8_t a) {
265 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
266 }
267
268 /**
269 * Add a color filter that can be represented by a color and a mode. Applied
bsalomon@google.comc7818882013-03-20 19:19:53 +0000270 * after color-computing effect stages.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000271 */
272 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000273 fCommon.fColorFilterColor = c;
274 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000275 }
276
bsalomon@google.comca432082013-01-23 19:53:46 +0000277 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
278 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000279
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000280 /**
281 * Constructor sets the color to be 'color' which is undone by the destructor.
282 */
283 class AutoColorRestore : public ::GrNoncopyable {
284 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000285 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000286
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000287 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000288 fDrawState = NULL;
289 this->set(drawState, color);
290 }
291
292 void reset() {
293 if (NULL != fDrawState) {
294 fDrawState->setColor(fOldColor);
295 fDrawState = NULL;
296 }
297 }
298
299 void set(GrDrawState* drawState, GrColor color) {
300 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000301 fDrawState = drawState;
302 fOldColor = fDrawState->getColor();
303 fDrawState->setColor(color);
304 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000305
306 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000307 private:
308 GrDrawState* fDrawState;
309 GrColor fOldColor;
310 };
311
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000312 /// @}
313
314 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000315 /// @name Coverage
316 ////
317
318 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000319 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000320 * initial value (after construction or reset()) is 0xff. The constant
321 * coverage is ignored when per-vertex coverage is provided.
322 */
323 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000324 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000325 }
326
327 /**
328 * Version of above that specifies 4 channel per-vertex color. The value
329 * should be premultiplied.
330 */
331 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000332 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000333 }
334
335 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000336 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000337 }
338
339 /// @}
340
341 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000342 /// @name Effect Stages
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000343 /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment
344 /// shader. Its inputs are the output from the previous stage as well as some variables
345 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
346 /// the fragment position, local coordinates).
347 ///
348 /// The stages are divided into two sets, color-computing and coverage-computing. The final
349 /// color stage produces the final pixel color. The coverage-computing stages function exactly
350 /// as the color-computing but the output of the final coverage stage is treated as a fractional
351 /// pixel coverage rather than as input to the src/dst color blend step.
352 ///
353 /// The input color to the first color-stage is either the constant color or interpolated
354 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
355 /// (usually full-coverage) or interpolated per-vertex coverage.
356 ///
357 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
358 /// the color / coverage distinction.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000359 ////
360
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000361 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000362 SkASSERT(NULL != effect);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000363 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1));
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000364 return effect;
365 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000366
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000367 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000368 SkASSERT(NULL != effect);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000369 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000370 return effect;
371 }
372
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000373 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000374 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000375 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000376 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000377 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000378 this->addColorEffect(effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000379 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000380
381 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
382 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
383 this->addCoverageEffect(effect)->unref();
384 }
385
386 void addColorTextureEffect(GrTexture* texture,
387 const SkMatrix& matrix,
388 const GrTextureParams& params) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000389 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000390 this->addColorEffect(effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000391 }
392
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000393 void addCoverageTextureEffect(GrTexture* texture,
394 const SkMatrix& matrix,
395 const GrTextureParams& params) {
396 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
397 this->addCoverageEffect(effect)->unref();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000398 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000399
robertphillips@google.com972265d2012-06-13 18:49:30 +0000400 /**
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000401 * When this object is destroyed it will remove any effects from the draw state that were added
402 * after its constructor.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000403 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000404 class AutoRestoreEffects : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000405 public:
bsalomon@google.com2fad5a82013-06-13 19:47:23 +0000406 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000407
skia.committer@gmail.com5c493d52013-06-14 07:00:49 +0000408 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(0), fCoverageEffectCnt(0) {
409 this->set(ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000410 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000411
412 ~AutoRestoreEffects() { this->set(NULL); }
413
414 void set(GrDrawState* ds) {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000415 if (NULL != fDrawState) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000416 int n = fDrawState->fColorStages.count() - fColorEffectCnt;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000417 SkASSERT(n >= 0);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000418 fDrawState->fColorStages.pop_back_n(n);
419 n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000420 SkASSERT(n >= 0);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000421 fDrawState->fCoverageStages.pop_back_n(n);
422 GR_DEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
423 }
424 fDrawState = ds;
425 if (NULL != ds) {
426 fColorEffectCnt = ds->fColorStages.count();
427 fCoverageEffectCnt = ds->fCoverageStages.count();
428 GR_DEBUGCODE(++ds->fBlockEffectRemovalCnt;)
robertphillips@google.com972265d2012-06-13 18:49:30 +0000429 }
430 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000431
robertphillips@google.com972265d2012-06-13 18:49:30 +0000432 private:
433 GrDrawState* fDrawState;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000434 int fColorEffectCnt;
435 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000436 };
437
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000438 int numColorStages() const { return fColorStages.count(); }
439 int numCoverageStages() const { return fCoverageStages.count(); }
440 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000441
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000442 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
443 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000444
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000445 /**
446 * Checks whether any of the effects will read the dst pixel color.
447 */
bsalomon@google.comd09ab842013-05-15 17:30:26 +0000448 bool willEffectReadDstColor() const;
reed@google.com67e7cde2013-03-20 17:47:16 +0000449
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000450 /// @}
451
452 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000453 /// @name Blending
454 ////
455
456 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000457 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000458 *
459 * The blend function will be:
460 * D' = sat(S*srcCoef + D*dstCoef)
461 *
462 * where D is the existing destination color, S is the incoming source
463 * color, and D' is the new destination color that will be written. sat()
464 * is the saturation function.
465 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000466 * @param srcCoef coefficient applied to the src color.
467 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000468 */
469 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000470 fCommon.fSrcBlend = srcCoeff;
471 fCommon.fDstBlend = dstCoeff;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000472 #ifdef SK_DEBUG
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000473 if (GrBlendCoeffRefsDst(dstCoeff)) {
474 GrPrintf("Unexpected dst blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000475 }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000476 if (GrBlendCoeffRefsSrc(srcCoeff)) {
477 GrPrintf("Unexpected src blend coeff. Won't work correctly with coverage stages.\n");
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000478 }
479 #endif
480 }
481
bsalomon@google.comca432082013-01-23 19:53:46 +0000482 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
483 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000484
485 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
486 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000487 *srcBlendCoeff = fCommon.fSrcBlend;
488 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000489 }
490
491 /**
492 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000493 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000494 * kConstC_GrBlendCoeff
495 * kIConstC_GrBlendCoeff
496 * kConstA_GrBlendCoeff
497 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 *
499 * @param constant the constant to set
500 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000501 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502
503 /**
504 * Retrieves the last value set by setBlendConstant()
505 * @return the blending constant value
506 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000507 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000508
bsalomon@google.com2b446732013-02-12 16:47:41 +0000509 /**
510 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
511 * coverage before the blend will give the correct final destination color. In general it
512 * will not as coverage is applied after blending.
513 */
514 bool canTweakAlphaForCoverage() const;
515
516 /**
517 * Optimizations for blending / coverage to that can be applied based on the current state.
518 */
519 enum BlendOptFlags {
520 /**
521 * No optimization
522 */
523 kNone_BlendOpt = 0,
524 /**
525 * Don't draw at all
526 */
527 kSkipDraw_BlendOptFlag = 0x1,
528 /**
529 * Emit the src color, disable HW blending (replace dst with src)
530 */
531 kDisableBlend_BlendOptFlag = 0x2,
532 /**
533 * The coverage value does not have to be computed separately from alpha, the the output
534 * color can be the modulation of the two.
535 */
536 kCoverageAsAlpha_BlendOptFlag = 0x4,
537 /**
538 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
539 * "don't cares".
540 */
541 kEmitCoverage_BlendOptFlag = 0x8,
542 /**
543 * Emit transparent black instead of the src color, no need to compute coverage.
544 */
545 kEmitTransBlack_BlendOptFlag = 0x10,
546 };
547 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
548
549 /**
550 * Determines what optimizations can be applied based on the blend. The coefficients may have
551 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
552 * params that receive the tweaked coefficients. Normally the function looks at the current
553 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
554 * determine the blend optimizations that would be used if there was partial pixel coverage.
555 *
556 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
557 * playback) must call this function and respect the flags that replace the output color.
558 */
559 BlendOptFlags getBlendOpts(bool forceCoverage = false,
560 GrBlendCoeff* srcCoeff = NULL,
561 GrBlendCoeff* dstCoeff = NULL) const;
562
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000563 /// @}
564
565 ///////////////////////////////////////////////////////////////////////////
566 /// @name View Matrix
567 ////
568
569 /**
bsalomon@google.com137f1342013-05-29 21:27:53 +0000570 * Sets the view matrix to identity and updates any installed effects to compensate for the
571 * coord system change.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000572 */
bsalomon@google.com137f1342013-05-29 21:27:53 +0000573 bool setIdentityViewMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000574
575 /**
576 * Retrieves the current view matrix
577 * @return the current view matrix.
578 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000579 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580
581 /**
582 * Retrieves the inverse of the current view matrix.
583 *
584 * If the current view matrix is invertible, return true, and if matrix
585 * is non-null, copy the inverse into it. If the current view matrix is
586 * non-invertible, return false and ignore the matrix parameter.
587 *
588 * @param matrix if not null, will receive a copy of the current inverse.
589 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000590 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000591 // TODO: determine whether we really need to leave matrix unmodified
592 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000593 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000594 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595 if (matrix) {
596 *matrix = inverse;
597 }
598 return true;
599 }
600 return false;
601 }
602
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000603 ////////////////////////////////////////////////////////////////////////////
604
605 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000606 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000607 * Effect matrices are automatically adjusted to compensate and adjusted back in the destructor.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000608 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000609 class AutoViewMatrixRestore : public ::GrNoncopyable {
610 public:
611 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000612
bsalomon@google.comc7818882013-03-20 19:19:53 +0000613 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 fDrawState = NULL;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000615 this->set(ds, preconcatMatrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000617
618 ~AutoViewMatrixRestore() { this->restore(); }
619
bsalomon@google.coma8347462012-10-08 18:59:39 +0000620 /**
621 * Can be called prior to destructor to restore the original matrix.
622 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000623 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000624
bsalomon@google.comc7818882013-03-20 19:19:53 +0000625 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000626
bsalomon@google.com137f1342013-05-29 21:27:53 +0000627 /** Sets the draw state's matrix to identity. This can fail because the current view matrix
628 is not invertible. */
629 bool setIdentity(GrDrawState* drawState);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000630
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000631 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000632 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix);
633
634 GrDrawState* fDrawState;
635 SkMatrix fViewMatrix;
636 int fNumColorStages;
637 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000638 };
639
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000640 /// @}
641
642 ///////////////////////////////////////////////////////////////////////////
643 /// @name Render Target
644 ////
645
646 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000647 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000648 *
649 * @param target The render target to set.
650 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000651 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000652 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000653 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000654
655 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000656 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000657 *
658 * @return The currently set render target.
659 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000660 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
661 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000662
663 class AutoRenderTargetRestore : public ::GrNoncopyable {
664 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000665 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000666 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
667 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000668 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000669 this->set(ds, newTarget);
670 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000671 ~AutoRenderTargetRestore() { this->restore(); }
672
673 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000674 if (NULL != fDrawState) {
675 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000676 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000677 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000678 GrSafeSetNull(fSavedTarget);
679 }
680
681 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
682 this->restore();
683
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000684 if (NULL != ds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000685 SkASSERT(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000686 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000687 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000689 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 }
692 private:
693 GrDrawState* fDrawState;
694 GrRenderTarget* fSavedTarget;
695 };
696
697 /// @}
698
699 ///////////////////////////////////////////////////////////////////////////
700 /// @name Stencil
701 ////
702
703 /**
704 * Sets the stencil settings to use for the next draw.
705 * Changing the clip has the side-effect of possibly zeroing
706 * out the client settable stencil bits. So multipass algorithms
707 * using stencil should not change the clip between passes.
708 * @param settings the stencil settings to use.
709 */
710 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000711 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000712 }
713
714 /**
715 * Shortcut to disable stencil testing and ops.
716 */
717 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000718 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000719 }
720
bsalomon@google.comca432082013-01-23 19:53:46 +0000721 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000722
bsalomon@google.comca432082013-01-23 19:53:46 +0000723 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724
725 /// @}
726
727 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000728 /// @name State Flags
729 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000730
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000731 /**
732 * Flags that affect rendering. Controlled using enable/disableState(). All
733 * default to disabled.
734 */
735 enum StateBits {
736 /**
737 * Perform dithering. TODO: Re-evaluate whether we need this bit
738 */
739 kDither_StateBit = 0x01,
740 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000741 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
742 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
743 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000744 */
745 kHWAntialias_StateBit = 0x02,
746 /**
747 * Draws will respect the clip, otherwise the clip is ignored.
748 */
749 kClip_StateBit = 0x04,
750 /**
751 * Disables writing to the color buffer. Useful when performing stencil
752 * operations.
753 */
754 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000755
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000756 /**
757 * Usually coverage is applied after color blending. The color is blended using the coeffs
758 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
759 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
760 * this case there is no distinction between coverage and color and the caller needs direct
761 * control over the blend coeffs. When set, there will be a single blend step controlled by
762 * setBlendFunc() which will use coverage*color as the src color.
763 */
764 kCoverageDrawing_StateBit = 0x10,
765
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000766 // Users of the class may add additional bits to the vector
767 kDummyStateBit,
768 kLastPublicStateBit = kDummyStateBit-1,
769 };
770
771 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000772 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000773 }
774
775 /**
776 * Enable render state settings.
777 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000778 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000779 */
780 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000781 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000782 }
783
784 /**
785 * Disable render state settings.
786 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000787 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000788 */
789 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000790 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791 }
792
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000793 /**
794 * Enable or disable stateBits based on a boolean.
795 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000796 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000797 * @param enable if true enable stateBits, otherwise disable
798 */
799 void setState(uint32_t stateBits, bool enable) {
800 if (enable) {
801 this->enableState(stateBits);
802 } else {
803 this->disableState(stateBits);
804 }
805 }
806
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000807 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000808 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000809 }
810
811 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000812 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 }
814
815 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000816 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000817 }
818
819 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000820 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 }
822
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000823 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000824 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000825 }
826
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000827 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000828 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000829 }
830
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000831 /// @}
832
833 ///////////////////////////////////////////////////////////////////////////
834 /// @name Face Culling
835 ////
836
837 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000838 kInvalid_DrawFace = -1,
839
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840 kBoth_DrawFace,
841 kCCW_DrawFace,
842 kCW_DrawFace,
843 };
844
845 /**
846 * Controls whether clockwise, counterclockwise, or both faces are drawn.
847 * @param face the face(s) to draw.
848 */
849 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000850 SkASSERT(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +0000851 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852 }
853
854 /**
855 * Gets whether the target is drawing clockwise, counterclockwise,
856 * or both faces.
857 * @return the current draw face(s).
858 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000859 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000860
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 /// @}
862
863 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000864
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000865 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000866 if (fRenderTarget.get() != s.fRenderTarget.get() ||
867 fColorStages.count() != s.fColorStages.count() ||
868 fCoverageStages.count() != s.fCoverageStages.count() ||
869 fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000870 return false;
871 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000872 for (int i = 0; i < fColorStages.count(); i++) {
873 if (fColorStages[i] != s.fColorStages[i]) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +0000874 return false;
875 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000876 }
877 for (int i = 0; i < fCoverageStages.count(); i++) {
878 if (fCoverageStages[i] != s.fCoverageStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000879 return false;
880 }
881 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000882 return true;
883 }
884 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
885
bsalomon@google.comca432082013-01-23 19:53:46 +0000886 GrDrawState& operator= (const GrDrawState& s) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000887 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comca432082013-01-23 19:53:46 +0000888 this->setRenderTarget(s.fRenderTarget.get());
889 fCommon = s.fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000890 fColorStages = s.fColorStages;
891 fCoverageStages = s.fCoverageStages;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000892 return *this;
893 }
894
895private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +0000896
bsalomon@google.com137f1342013-05-29 21:27:53 +0000897 void onReset(const SkMatrix* initialViewMatrix) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000898 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000899 fColorStages.reset();
900 fCoverageStages.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000901
902 fRenderTarget.reset(NULL);
903
904 this->setDefaultVertexAttribs();
905
906 fCommon.fColor = 0xffffffff;
907 if (NULL == initialViewMatrix) {
908 fCommon.fViewMatrix.reset();
909 } else {
910 fCommon.fViewMatrix = *initialViewMatrix;
911 }
912 fCommon.fSrcBlend = kOne_GrBlendCoeff;
913 fCommon.fDstBlend = kZero_GrBlendCoeff;
914 fCommon.fBlendConstant = 0x0;
915 fCommon.fFlagBits = 0x0;
916 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000917 fCommon.fCoverage = 0xffffffff;
918 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
919 fCommon.fColorFilterColor = 0x0;
920 fCommon.fDrawFace = kBoth_DrawFace;
921 }
922
bsalomon@google.comca432082013-01-23 19:53:46 +0000923 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
924 struct CommonState {
925 // These fields are roughly sorted by decreasing likelihood of being different in op==
robertphillips@google.com42903302013-04-20 12:26:07 +0000926 GrColor fColor;
927 SkMatrix fViewMatrix;
928 GrBlendCoeff fSrcBlend;
929 GrBlendCoeff fDstBlend;
930 GrColor fBlendConstant;
931 uint32_t fFlagBits;
932 const GrVertexAttrib* fVAPtr;
933 int fVACount;
934 GrStencilSettings fStencilSettings;
robertphillips@google.com42903302013-04-20 12:26:07 +0000935 GrColor fCoverage;
936 SkXfermode::Mode fColorFilterMode;
937 GrColor fColorFilterColor;
938 DrawFace fDrawFace;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000939
940 // This is simply a different representation of info in fVertexAttribs and thus does
941 // not need to be compared in op==.
942 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
943
bsalomon@google.comca432082013-01-23 19:53:46 +0000944 bool operator== (const CommonState& other) const {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000945 bool result = fColor == other.fColor &&
946 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
947 fSrcBlend == other.fSrcBlend &&
948 fDstBlend == other.fDstBlend &&
949 fBlendConstant == other.fBlendConstant &&
950 fFlagBits == other.fFlagBits &&
robertphillips@google.com42903302013-04-20 12:26:07 +0000951 fVACount == other.fVACount &&
952 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVertexAttrib)) &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000953 fStencilSettings == other.fStencilSettings &&
jvanverth@google.com054ae992013-04-01 20:06:51 +0000954 fCoverage == other.fCoverage &&
955 fColorFilterMode == other.fColorFilterMode &&
956 fColorFilterColor == other.fColorFilterColor &&
957 fDrawFace == other.fDrawFace;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000958 SkASSERT(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices,
jvanverth@google.com054ae992013-04-01 20:06:51 +0000959 other.fFixedFunctionVertexAttribIndices,
960 sizeof(fFixedFunctionVertexAttribIndices)));
961 return result;
bsalomon@google.comca432082013-01-23 19:53:46 +0000962 }
963 bool operator!= (const CommonState& other) const { return !(*this == other); }
964 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000965
bsalomon@google.comca432082013-01-23 19:53:46 +0000966 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
967 DeferredState must directly reference GrEffects, however. */
968 struct SavedEffectStage {
969 SavedEffectStage() : fEffect(NULL) {}
970 const GrEffect* fEffect;
971 GrEffectStage::SavedCoordChange fCoordChange;
972 };
973
974public:
975 /**
976 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
977 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
978 * dispose mechanism returns them to the cache. This allows recycling resources through the
979 * the cache while they are in a deferred draw queue.
980 */
981 class DeferredState {
982 public:
983 DeferredState() : fRenderTarget(NULL) {
984 GR_DEBUGCODE(fInitialized = false;)
985 }
986 // TODO: Remove this when DeferredState no longer holds a ref to the RT
987 ~DeferredState() { SkSafeUnref(fRenderTarget); }
988
989 void saveFrom(const GrDrawState& drawState) {
990 fCommon = drawState.fCommon;
991 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
992 fRenderTarget = drawState.fRenderTarget.get();
993 SkSafeRef(fRenderTarget);
994 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
995 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
996 // 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 +0000997 fStages.reset(drawState.fColorStages.count() + drawState.fCoverageStages.count());
998 fColorStageCnt = drawState.fColorStages.count();
999 for (int i = 0; i < fColorStageCnt; ++i) {
1000 fStages[i].saveFrom(drawState.fColorStages[i]);
1001 }
1002 for (int i = 0; i < drawState.fCoverageStages.count(); ++i) {
1003 fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i]);
bsalomon@google.comca432082013-01-23 19:53:46 +00001004 }
1005 GR_DEBUGCODE(fInitialized = true;)
1006 }
1007
1008 void restoreTo(GrDrawState* drawState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001009 SkASSERT(fInitialized);
bsalomon@google.comca432082013-01-23 19:53:46 +00001010 drawState->fCommon = fCommon;
1011 drawState->setRenderTarget(fRenderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001012 // reinflate color/cov stage arrays.
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001013 drawState->fColorStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001014 for (int i = 0; i < fColorStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001015 SkNEW_APPEND_TO_TARRAY(&drawState->fColorStages, GrEffectStage, (fStages[i]));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001016 }
1017 int coverageStageCnt = fStages.count() - fColorStageCnt;
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001018 drawState->fCoverageStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001019 for (int i = 0; i < coverageStageCnt; ++i) {
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +00001020 SkNEW_APPEND_TO_TARRAY(&drawState->fCoverageStages,
1021 GrEffectStage, (fStages[i + fColorStageCnt]));
bsalomon@google.comca432082013-01-23 19:53:46 +00001022 }
1023 }
1024
1025 bool isEqual(const GrDrawState& state) const {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001026 int numCoverageStages = fStages.count() - fColorStageCnt;
1027 if (fRenderTarget != state.fRenderTarget.get() ||
1028 fColorStageCnt != state.fColorStages.count() ||
1029 numCoverageStages != state.fCoverageStages.count() ||
1030 fCommon != state.fCommon) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001031 return false;
1032 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001033 bool explicitLocalCoords = state.hasLocalCoordAttribute();
1034 for (int i = 0; i < fColorStageCnt; ++i) {
1035 if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoords)) {
1036 return false;
1037 }
1038 }
1039 for (int i = 0; i < numCoverageStages; ++i) {
1040 int s = fColorStageCnt + i;
1041 if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalCoords)) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001042 return false;
1043 }
1044 }
1045 return true;
1046 }
1047
1048 private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001049 typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArray;
1050
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001051 GrRenderTarget* fRenderTarget;
1052 CommonState fCommon;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001053 int fColorStageCnt;
1054 DeferredStageArray fStages;
bsalomon@google.comca432082013-01-23 19:53:46 +00001055
1056 GR_DEBUGCODE(bool fInitialized;)
1057 };
1058
1059private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001060
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001061 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1062 CommonState fCommon;
1063
1064 typedef SkSTArray<4, GrEffectStage> EffectStageArray;
1065 EffectStageArray fColorStages;
1066 EffectStageArray fCoverageStages;
1067
1068 // Some of the auto restore objects assume that no effects are removed during their lifetime.
1069 // This is used to assert that this condition holds.
1070 GR_DEBUGCODE(int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001071
robertphillips@google.com42903302013-04-20 12:26:07 +00001072 /**
1073 * Sets vertex attributes for next draw.
1074 *
1075 * @param attribs the array of vertex attributes to set.
1076 * @param count the number of attributes being set, limited to kMaxVertexAttribCnt.
1077 */
1078 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
1079
reed@google.comfa35e3d2012-06-26 20:16:17 +00001080 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001081};
1082
bsalomon@google.com2b446732013-02-12 16:47:41 +00001083GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1084
tomhudson@google.com93813632011-10-27 20:21:16 +00001085#endif