blob: 220e290a838e53c483fa1801fd33d69d393718b5 [file] [log] [blame]
tomhudson@google.com93813632011-10-27 20:21:16 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrDrawState_DEFINED
9#define GrDrawState_DEFINED
10
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000011#include "GrBackendEffectFactory.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrColor.h"
bsalomon@google.com08283af2012-10-26 13:01:20 +000013#include "GrEffectStage.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000014#include "GrRefCnt.h"
robertphillips@google.com9ec07532012-06-22 12:01:30 +000015#include "GrRenderTarget.h"
jvanverth@google.comcc782382013-01-28 20:39:48 +000016#include "GrStencil.h"
17#include "GrTemplates.h"
18#include "GrTexture.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000019#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000020
jvanverth@google.comcc782382013-01-28 20:39:48 +000021#include "SkMatrix.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000022#include "SkXfermode.h"
23
bsalomon@google.comaf84e742012-10-05 13:23:24 +000024class GrPaint;
tomhudson@google.com93813632011-10-27 20:21:16 +000025
jvanverth@google.com9b855c72013-03-01 18:21:22 +000026/**
27 * Types used to describe format of vertices in arrays
28 */
29enum GrVertexAttribType {
30 kFloat_GrVertexAttribType = 0,
31 kVec2f_GrVertexAttribType,
32 kVec3f_GrVertexAttribType,
33 kVec4f_GrVertexAttribType,
34 kVec4ub_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors
35
36 kLast_GrVertexAttribType = kVec4ub_GrVertexAttribType
37};
38static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
39
40struct GrVertexAttrib {
skia.committer@gmail.comf140f182013-03-02 07:01:56 +000041 inline void set(GrVertexAttribType type, size_t offset) {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +000042 fType = type; fOffset = offset;
43 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000044 bool operator==(const GrVertexAttrib& other) const {
45 return fType == other.fType && fOffset == other.fOffset;
46 };
47 bool operator!=(const GrVertexAttrib& other) const { return !(*this == other); }
48
49 GrVertexAttribType fType;
50 size_t fOffset;
51};
52
53template <int N>
54class GrVertexAttribArray : public SkSTArray<N, GrVertexAttrib, true> {};
55
56/**
57 * Type used to describe how attributes bind to program usage
58 */
59typedef int GrAttribBindings;
60
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000061class GrDrawState : public GrRefCnt {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000062public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000063 SK_DECLARE_INST_COUNT(GrDrawState)
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
tomhudson@google.com93813632011-10-27 20:21:16 +000065 /**
bsalomon@google.com13221342012-10-26 13:41:59 +000066 * Total number of effect stages. Each stage can host a GrEffect. A stage is enabled if it has a
67 * GrEffect. The effect produces an output color in the fragment shader. It's inputs are the
68 * output from the previous enabled stage and a position. The position is either derived from
69 * the interpolated vertex positions or explicit per-vertex coords, depending upon the
jvanverth@google.com9b855c72013-03-01 18:21:22 +000070 * GrAttribBindings used to draw.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000071 *
bsalomon@google.com13221342012-10-26 13:41:59 +000072 * The stages are divided into two sets, color-computing and coverage-computing. The final color
73 * stage produces the final pixel color. The coverage-computing stages function exactly as the
74 * color-computing but the output of the final coverage stage is treated as a fractional pixel
75 * coverage rather than as input to the src/dst color blend step.
76 *
77 * The input color to the first enabled color-stage is either the constant color or interpolated
jvanverth@google.com9b855c72013-03-01 18:21:22 +000078 * per-vertex colors, depending upon GrAttribBindings. The input to the first coverage stage is
bsalomon@google.com13221342012-10-26 13:41:59 +000079 * either a constant coverage (usually full-coverage), interpolated per-vertex coverage, or
80 * edge-AA computed coverage. (This latter is going away as soon as it can be rewritten as a
81 * GrEffect).
82 *
bsalomon@google.comcf939ae2012-12-13 19:59:23 +000083 * See the documentation of kCoverageDrawing_StateBit for information about disabling the
84 * the color / coverage distinction.
85 *
bsalomon@google.com13221342012-10-26 13:41:59 +000086 * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied from the client's
87 * GrPaint. Stages GrPaint::kTotalStages through kNumStages-2 are earmarked for use by
88 * GrTextContext and GrPathRenderer-derived classes. kNumStages-1 is earmarked for clipping
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000089 * by GrClipMaskManager.
tomhudson@google.com93813632011-10-27 20:21:16 +000090 */
91 enum {
twiz@google.com58071162012-07-18 21:41:50 +000092 kNumStages = 5,
tomhudson@google.com93813632011-10-27 20:21:16 +000093 };
94
bsalomon@google.comca432082013-01-23 19:53:46 +000095 GrDrawState() {
reed@google.com75847192013-01-28 20:53:22 +000096#if GR_DEBUG
jvanverth@google.com9b855c72013-03-01 18:21:22 +000097 VertexAttributesUnitTest();
reed@google.com75847192013-01-28 20:53:22 +000098#endif
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000099 this->reset();
100 }
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000101
bsalomon@google.comca432082013-01-23 19:53:46 +0000102 GrDrawState(const GrDrawState& state) {
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000103 *this = state;
104 }
105
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000106 virtual ~GrDrawState() {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000107 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000108 }
109
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000110 /**
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000111 * Resets to the default state.
bsalomon@google.com08283af2012-10-26 13:01:20 +0000112 * GrEffects will be removed from all stages.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113 */
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +0000114 void reset() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000115
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000116 this->disableStages();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000117
bsalomon@google.comca432082013-01-23 19:53:46 +0000118 fRenderTarget.reset(NULL);
119
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000120 this->setDefaultVertexAttribs();
121
bsalomon@google.comca432082013-01-23 19:53:46 +0000122 fCommon.fColor = 0xffffffff;
123 fCommon.fViewMatrix.reset();
124 fCommon.fSrcBlend = kOne_GrBlendCoeff;
125 fCommon.fDstBlend = kZero_GrBlendCoeff;
126 fCommon.fBlendConstant = 0x0;
127 fCommon.fFlagBits = 0x0;
128 fCommon.fVertexEdgeType = kHairLine_EdgeType;
129 fCommon.fStencilSettings.setDisabled();
130 fCommon.fFirstCoverageStage = kNumStages;
131 fCommon.fCoverage = 0xffffffff;
132 fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
133 fCommon.fColorFilterColor = 0x0;
134 fCommon.fDrawFace = kBoth_DrawFace;
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000135 }
136
137 /**
138 * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000139 * encompasses more than GrPaint. Aspects of GrDrawState that have no
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000140 * GrPaint equivalents are not modified. GrPaint has fewer stages than
141 * GrDrawState. The extra GrDrawState stages are disabled.
142 */
143 void setFromPaint(const GrPaint& paint);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000144
145 ///////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000146 /// @name Vertex Attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +0000147 ////
148
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000149 enum {
150 kVertexAttribCnt = 6,
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000151 };
152
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000153 /**
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000154 * The format of vertices is represented as an array of vertex attribute
155 * pair, with each pair representing the type of the attribute and the
156 * offset in the vertex structure (see GrVertexAttrib, above).
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000157 *
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000158 * This will only set up the vertex geometry. To bind the attributes in
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000159 * the shaders, attribute indices and attribute bindings need to be set
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000160 * as well.
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000161 */
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000162
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000163 /**
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000164 * Sets vertex attributes for next draw.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000165 *
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000166 * @param attribs the array of vertex attributes to set.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000167 * @param count the number of attributes being set.
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000168 * limited to a count of kVertexAttribCnt.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000169 */
170 void setVertexAttribs(const GrVertexAttrib attribs[], int count);
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000171
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000172 const GrVertexAttrib* getVertexAttribs() const { return fVertexAttribs.begin(); }
173 int getVertexAttribCount() const { return fVertexAttribs.count(); }
174
175 size_t getVertexSize() const;
176
177 /**
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000178 * Sets default vertex attributes for next draw.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000179 *
180 * This will also set default vertex attribute indices and bindings
181 */
182 void setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000183
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000184 bool validateVertexAttribs() const;
185
jvanverth@google.comcc782382013-01-28 20:39:48 +0000186 ////////////////////////////////////////////////////////////////////////////
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000187 // Helpers for picking apart vertex attributes
jvanverth@google.comcc782382013-01-28 20:39:48 +0000188
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000189 // helper array to let us check the expected so we know what bound attrib indices
190 // we care about
191 static const size_t kVertexAttribSizes[kGrVertexAttribTypeCount];
jvanverth@google.comcc782382013-01-28 20:39:48 +0000192
193 /**
194 * Accessing positions, texture coords, or colors, of a vertex within an
195 * array is a hassle involving casts and simple math. These helpers exist
196 * to keep GrDrawTarget clients' code a bit nicer looking.
197 */
198
199 /**
200 * Gets a pointer to a GrPoint of a vertex's position or texture
201 * coordinate.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000202 * @param vertices the vertex array
jvanverth@google.comcc782382013-01-28 20:39:48 +0000203 * @param vertexIndex the index of the vertex in the array
204 * @param vertexSize the size of each vertex in the array
205 * @param offset the offset in bytes of the vertex component.
206 * Defaults to zero (corresponding to vertex position)
207 * @return pointer to the vertex component as a GrPoint
208 */
209 static GrPoint* GetVertexPoint(void* vertices,
210 int vertexIndex,
211 int vertexSize,
212 int offset = 0) {
213 intptr_t start = GrTCast<intptr_t>(vertices);
214 return GrTCast<GrPoint*>(start + offset +
215 vertexIndex * vertexSize);
216 }
217 static const GrPoint* GetVertexPoint(const void* vertices,
218 int vertexIndex,
219 int vertexSize,
220 int offset = 0) {
221 intptr_t start = GrTCast<intptr_t>(vertices);
222 return GrTCast<const GrPoint*>(start + offset +
223 vertexIndex * vertexSize);
224 }
225
226 /**
227 * Gets a pointer to a GrColor inside a vertex within a vertex array.
228 * @param vertices the vetex array
229 * @param vertexIndex the index of the vertex in the array
230 * @param vertexSize the size of each vertex in the array
231 * @param offset the offset in bytes of the vertex color
232 * @return pointer to the vertex component as a GrColor
233 */
234 static GrColor* GetVertexColor(void* vertices,
235 int vertexIndex,
236 int vertexSize,
237 int offset) {
238 intptr_t start = GrTCast<intptr_t>(vertices);
239 return GrTCast<GrColor*>(start + offset +
240 vertexIndex * vertexSize);
241 }
242 static const GrColor* GetVertexColor(const void* vertices,
243 int vertexIndex,
244 int vertexSize,
245 int offset) {
246 const intptr_t start = GrTCast<intptr_t>(vertices);
247 return GrTCast<const GrColor*>(start + offset +
248 vertexIndex * vertexSize);
249 }
250
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000251 /// @}
252
253 ///////////////////////////////////////////////////////////////////////////
254 /// @name Attribute Bindings
255 ////
256
257 /**
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000258 * The vertex data used by the current program is represented as a bitfield
259 * of flags. Programs always use positions and may also use texture
260 * coordinates, per-vertex colors, per-vertex coverage and edge data. Each
261 * stage can use the explicit texture coordinates as its input texture
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000262 * coordinates or it may use the positions as texture coordinates.
263 */
264
265 /**
266 * Generates a bit indicating that a texture stage uses texture coordinates
267 *
268 * @param stageIdx the stage that will use texture coordinates.
269 *
270 * @return the bit to add to a GrAttribBindings bitfield.
271 */
272 static int ExplicitTexCoordAttribBindingsBit(int stageIdx) {
273 GrAssert(stageIdx < kNumStages);
274 return (1 << stageIdx);
275 }
276
277 static bool StageBindsExplicitTexCoords(GrAttribBindings bindings, int stageIdx);
278
279 /**
280 * Additional Bits that can be specified in GrAttribBindings.
281 */
282 enum AttribBindingsBits {
283 /* program uses colors (GrColor) */
284 kColor_AttribBindingsBit = 1 << (kNumStages + 0),
285 /* program uses coverage (GrColor)
286 */
287 kCoverage_AttribBindingsBit = 1 << (kNumStages + 1),
288 /* program uses edge data. Distance to the edge is used to
289 * compute a coverage. See GrDrawState::setVertexEdgeType().
290 */
291 kEdge_AttribBindingsBit = 1 << (kNumStages + 2),
292 // for below assert
293 kDummyAttribBindingsBit,
294 kHighAttribBindingsBit = kDummyAttribBindingsBit - 1
295 };
296 // make sure we haven't exceeded the number of bits in GrAttribBindings.
297 GR_STATIC_ASSERT(kHighAttribBindingsBit < ((uint64_t)1 << 8*sizeof(GrAttribBindings)));
298
299 enum AttribBindings {
300 kDefault_AttribBindings = 0
301 };
302
303 /**
304 * Sets attribute bindings for next draw.
305 *
306 * @param bindings the attribute bindings to set.
307 */
308 void setAttribBindings(GrAttribBindings bindings) { fCommon.fAttribBindings = bindings; }
309
310 GrAttribBindings getAttribBindings() const { return fCommon.fAttribBindings; }
311
312 ////////////////////////////////////////////////////////////////////////////
313 // Helpers for picking apart attribute bindings
314
315 /**
316 * Helper function to determine if program uses explicit texture
317 * coordinates.
318 *
319 * @param bindings attribute bindings to query
320 *
321 * @return true if program uses texture coordinates,
322 * false otherwise.
323 */
324 static bool AttributesBindExplicitTexCoords(GrAttribBindings bindings);
325
326 /**
327 * Determines whether src alpha is guaranteed to be one for all src pixels
328 */
329 bool srcAlphaWillBeOne(GrAttribBindings) const;
330
331 /**
332 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
333 */
334 bool hasSolidCoverage(GrAttribBindings) const;
335
336 static void VertexAttributesUnitTest();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000337
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000338 /// @}
339
340 ///////////////////////////////////////////////////////////////////////////
341 /// @name Vertex Attribute Indices
342 ////
343
344 /**
345 * Vertex attribute indices map the data set in the vertex attribute array
346 * to the bindings specified in the attribute bindings. Each binding type
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000347 * has an associated index in the attribute array. This index is used to
348 * look up the vertex attribute data from the array, and potentially as the
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000349 * attribute index if we're binding attributes in GL.
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000350 *
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000351 * Indices which do not have active attribute bindings will be ignored.
352 */
353
354 enum AttribIndex {
355 kPosition_AttribIndex = 0,
356 kColor_AttribIndex,
357 kCoverage_AttribIndex,
358 kEdge_AttribIndex,
359 kTexCoord_AttribIndex,
360
361 kLast_AttribIndex = kTexCoord_AttribIndex
362 };
363 static const int kAttribIndexCount = kLast_AttribIndex + 1;
364
365 // these are used when vertex color and coverage isn't set
366 enum {
367 kColorOverrideAttribIndexValue = GrDrawState::kVertexAttribCnt,
368 kCoverageOverrideAttribIndexValue = GrDrawState::kVertexAttribCnt+1,
369 };
370
371 ////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000372 // Helpers to set attribute indices. These should match the index in the
373 // current attribute index array.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000374
375 /**
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000376 * Sets index for next draw. This is used to look up the offset
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000377 * from the current vertex attribute array and to bind the attributes.
378 *
379 * @param index the attribute index we're setting
380 * @param value the value of the index
381 */
382 void setAttribIndex(AttribIndex index, int value) { fAttribIndices[index] = value; }
383
384 int getAttribIndex(AttribIndex index) const { return fAttribIndices[index]; }
jvanverth@google.comcc782382013-01-28 20:39:48 +0000385
386 /// @}
387
388 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000389 /// @name Color
390 ////
391
392 /**
393 * Sets color for next draw to a premultiplied-alpha color.
394 *
395 * @param color the color to set.
396 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000397 void setColor(GrColor color) { fCommon.fColor = color; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000398
bsalomon@google.comca432082013-01-23 19:53:46 +0000399 GrColor getColor() const { return fCommon.fColor; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400
401 /**
402 * Sets the color to be used for the next draw to be
403 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
404 *
405 * @param alpha The alpha value to set as the color.
406 */
407 void setAlpha(uint8_t a) {
408 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
409 }
410
411 /**
412 * Add a color filter that can be represented by a color and a mode. Applied
413 * after color-computing texture stages.
414 */
415 void setColorFilter(GrColor c, SkXfermode::Mode mode) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000416 fCommon.fColorFilterColor = c;
417 fCommon.fColorFilterMode = mode;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000418 }
419
bsalomon@google.comca432082013-01-23 19:53:46 +0000420 GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
421 SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000422
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000423 /**
424 * Constructor sets the color to be 'color' which is undone by the destructor.
425 */
426 class AutoColorRestore : public ::GrNoncopyable {
427 public:
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000428 AutoColorRestore() : fDrawState(NULL), fOldColor(0) {}
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000429
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000430 AutoColorRestore(GrDrawState* drawState, GrColor color) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000431 fDrawState = NULL;
432 this->set(drawState, color);
433 }
434
435 void reset() {
436 if (NULL != fDrawState) {
437 fDrawState->setColor(fOldColor);
438 fDrawState = NULL;
439 }
440 }
441
442 void set(GrDrawState* drawState, GrColor color) {
443 this->reset();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000444 fDrawState = drawState;
445 fOldColor = fDrawState->getColor();
446 fDrawState->setColor(color);
447 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000448
449 ~AutoColorRestore() { this->reset(); }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000450 private:
451 GrDrawState* fDrawState;
452 GrColor fOldColor;
453 };
454
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000455 /// @}
456
457 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000458 /// @name Coverage
459 ////
460
461 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000462 * Sets a constant fractional coverage to be applied to the draw. The
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000463 * initial value (after construction or reset()) is 0xff. The constant
464 * coverage is ignored when per-vertex coverage is provided.
465 */
466 void setCoverage(uint8_t coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000467 fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000468 }
469
470 /**
471 * Version of above that specifies 4 channel per-vertex color. The value
472 * should be premultiplied.
473 */
474 void setCoverage4(GrColor coverage) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000475 fCommon.fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000476 }
477
478 GrColor getCoverage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000479 return fCommon.fCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000480 }
481
482 /// @}
483
484 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comadc65362013-01-28 14:26:09 +0000485 /// @name Effect Stages
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000486 ////
487
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000488 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect,
489 const int* indices = NULL) {
490 fStages[stageIdx].setEffect(effect, indices);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000491 return effect;
492 }
493
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000494 /**
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000495 * Creates a GrSimpleTextureEffect.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000496 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000497 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000498 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000499 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000500 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000501 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000502 void createTextureEffect(int stageIdx,
503 GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000504 const SkMatrix& matrix,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000505 const GrTextureParams& params) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000506 GrAssert(!this->getStage(stageIdx).getEffect());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000507 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000508 this->setEffect(stageIdx, effect)->unref();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000509 }
510
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000511 bool stagesDisabled() {
512 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000513 if (NULL != fStages[i].getEffect()) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000514 return false;
515 }
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000516 }
tomhudson@google.com3eee8fb2012-06-25 12:30:34 +0000517 return true;
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000518 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000519
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000520 void disableStage(int stageIdx) {
521 this->setEffect(stageIdx, NULL, NULL);
522 }
tomhudson@google.com676e6602012-07-10 17:21:48 +0000523
robertphillips@google.com972265d2012-06-13 18:49:30 +0000524 /**
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000525 * Release all the GrEffects referred to by this draw state.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000526 */
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000527 void disableStages() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000528 for (int i = 0; i < kNumStages; ++i) {
tomhudson@google.com676e6602012-07-10 17:21:48 +0000529 this->disableStage(i);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000530 }
531 }
532
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000533 class AutoStageDisable : public ::GrNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000534 public:
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000535 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
536 ~AutoStageDisable() {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000537 if (NULL != fDrawState) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000538 fDrawState->disableStages();
robertphillips@google.com972265d2012-06-13 18:49:30 +0000539 }
540 }
541 private:
542 GrDrawState* fDrawState;
543 };
544
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000545 /**
bsalomon@google.com08283af2012-10-26 13:01:20 +0000546 * Returns the current stage by index.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000548 const GrEffectStage& getStage(int stageIdx) const {
549 GrAssert((unsigned)stageIdx < kNumStages);
550 return fStages[stageIdx];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 }
552
553 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000554 * Called when the source coord system is changing. preConcat gives the transformation from the
555 * old coord system to the new coord system.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000556 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000557 void preConcatStageMatrices(const SkMatrix& preConcat) {
bsalomon@google.comcabe00e2013-01-28 16:46:55 +0000558 this->preConcatStageMatrices(~0U, preConcat);
559 }
560 /**
561 * Version of above that applies the update matrix selectively to stages via a mask.
562 */
563 void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000564 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comcabe00e2013-01-28 16:46:55 +0000565 if (((1 << i) & stageMask) && this->isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000566 fStages[i].preConcatCoordChange(preConcat);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000567 }
568 }
569 }
570
bsalomon@google.come3d32162012-07-20 13:37:06 +0000571 /**
bsalomon@google.com288d9542012-10-17 12:53:54 +0000572 * Called when the source coord system is changing. preConcatInverse is the inverse of the
573 * transformation from the old coord system to the new coord system. Returns false if the matrix
574 * cannot be inverted.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000575 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000576 bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
577 SkMatrix inv;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000578 bool computed = false;
579 for (int i = 0; i < kNumStages; ++i) {
580 if (this->isStageEnabled(i)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000581 if (!computed && !preConcatInverse.invert(&inv)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000582 return false;
583 } else {
584 computed = true;
585 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000586 fStages[i].preConcatCoordChange(preConcatInverse);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000587 }
588 }
589 return true;
590 }
591
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592 /// @}
593
594 ///////////////////////////////////////////////////////////////////////////
595 /// @name Coverage / Color Stages
596 ////
597
598 /**
599 * A common pattern is to compute a color with the initial stages and then
600 * modulate that color by a coverage value in later stage(s) (AA, mask-
rmistry@google.comd6176b02012-08-23 18:14:13 +0000601 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
602 * computed based on the pre-coverage-modulated color. The division of
603 * stages between color-computing and coverage-computing is specified by
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000604 * this method. Initially this is kNumStages (all stages
605 * are color-computing).
606 */
607 void setFirstCoverageStage(int firstCoverageStage) {
608 GrAssert((unsigned)firstCoverageStage <= kNumStages);
bsalomon@google.comca432082013-01-23 19:53:46 +0000609 fCommon.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 }
611
612 /**
613 * Gets the index of the first coverage-computing stage.
614 */
615 int getFirstCoverageStage() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000616 return fCommon.fFirstCoverageStage;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000617 }
618
619 ///@}
620
621 ///////////////////////////////////////////////////////////////////////////
622 /// @name Blending
623 ////
624
625 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000626 * Sets the blending function coefficients.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000627 *
628 * The blend function will be:
629 * D' = sat(S*srcCoef + D*dstCoef)
630 *
631 * where D is the existing destination color, S is the incoming source
632 * color, and D' is the new destination color that will be written. sat()
633 * is the saturation function.
634 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000635 * @param srcCoef coefficient applied to the src color.
636 * @param dstCoef coefficient applied to the dst color.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000637 */
638 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000639 fCommon.fSrcBlend = srcCoeff;
640 fCommon.fDstBlend = dstCoeff;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000641 #if GR_DEBUG
642 switch (dstCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000643 case kDC_GrBlendCoeff:
644 case kIDC_GrBlendCoeff:
645 case kDA_GrBlendCoeff:
646 case kIDA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000647 GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
648 "coverage stages.\n");
649 break;
650 default:
651 break;
652 }
653 switch (srcCoeff) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000654 case kSC_GrBlendCoeff:
655 case kISC_GrBlendCoeff:
656 case kSA_GrBlendCoeff:
657 case kISA_GrBlendCoeff:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000658 GrPrintf("Unexpected src blend coeff. Won't work correctly with"
659 "coverage stages.\n");
660 break;
661 default:
662 break;
663 }
664 #endif
665 }
666
bsalomon@google.comca432082013-01-23 19:53:46 +0000667 GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
668 GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000669
670 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
671 GrBlendCoeff* dstBlendCoeff) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000672 *srcBlendCoeff = fCommon.fSrcBlend;
673 *dstBlendCoeff = fCommon.fDstBlend;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000674 }
675
676 /**
677 * Sets the blending function constant referenced by the following blending
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000678 * coefficients:
bsalomon@google.com47059542012-06-06 20:51:20 +0000679 * kConstC_GrBlendCoeff
680 * kIConstC_GrBlendCoeff
681 * kConstA_GrBlendCoeff
682 * kIConstA_GrBlendCoeff
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 *
684 * @param constant the constant to set
685 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000686 void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000687
688 /**
689 * Retrieves the last value set by setBlendConstant()
690 * @return the blending constant value
691 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000692 GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000693
bsalomon@google.com2b446732013-02-12 16:47:41 +0000694 /**
695 * Determines whether multiplying the computed per-pixel color by the pixel's fractional
696 * coverage before the blend will give the correct final destination color. In general it
697 * will not as coverage is applied after blending.
698 */
699 bool canTweakAlphaForCoverage() const;
700
701 /**
702 * Optimizations for blending / coverage to that can be applied based on the current state.
703 */
704 enum BlendOptFlags {
705 /**
706 * No optimization
707 */
708 kNone_BlendOpt = 0,
709 /**
710 * Don't draw at all
711 */
712 kSkipDraw_BlendOptFlag = 0x1,
713 /**
714 * Emit the src color, disable HW blending (replace dst with src)
715 */
716 kDisableBlend_BlendOptFlag = 0x2,
717 /**
718 * The coverage value does not have to be computed separately from alpha, the the output
719 * color can be the modulation of the two.
720 */
721 kCoverageAsAlpha_BlendOptFlag = 0x4,
722 /**
723 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
724 * "don't cares".
725 */
726 kEmitCoverage_BlendOptFlag = 0x8,
727 /**
728 * Emit transparent black instead of the src color, no need to compute coverage.
729 */
730 kEmitTransBlack_BlendOptFlag = 0x10,
731 };
732 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
733
734 /**
735 * Determines what optimizations can be applied based on the blend. The coefficients may have
736 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
737 * params that receive the tweaked coefficients. Normally the function looks at the current
738 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
739 * determine the blend optimizations that would be used if there was partial pixel coverage.
740 *
741 * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
742 * playback) must call this function and respect the flags that replace the output color.
743 */
744 BlendOptFlags getBlendOpts(bool forceCoverage = false,
745 GrBlendCoeff* srcCoeff = NULL,
746 GrBlendCoeff* dstCoeff = NULL) const;
747
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000748 /// @}
749
750 ///////////////////////////////////////////////////////////////////////////
751 /// @name View Matrix
752 ////
753
754 /**
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000755 * Sets the matrix applied to vertex positions.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000756 *
757 * In the post-view-matrix space the rectangle [0,w]x[0,h]
758 * fully covers the render target. (w and h are the width and height of the
bsalomon@google.comca432082013-01-23 19:53:46 +0000759 * the render-target.)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000760 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000761 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762
763 /**
764 * Gets a writable pointer to the view matrix.
765 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000766 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000767
768 /**
769 * Multiplies the current view matrix by a matrix
770 *
771 * After this call V' = V*m where V is the old view matrix,
772 * m is the parameter to this function, and V' is the new view matrix.
773 * (We consider positions to be column vectors so position vector p is
774 * transformed by matrix X as p' = X*p.)
775 *
776 * @param m the matrix used to modify the view matrix.
777 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000778 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000779
780 /**
781 * Multiplies the current view matrix by a matrix
782 *
783 * After this call V' = m*V where V is the old view matrix,
784 * m is the parameter to this function, and V' is the new view matrix.
785 * (We consider positions to be column vectors so position vector p is
786 * transformed by matrix X as p' = X*p.)
787 *
788 * @param m the matrix used to modify the view matrix.
789 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000790 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791
792 /**
793 * Retrieves the current view matrix
794 * @return the current view matrix.
795 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000796 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000797
798 /**
799 * Retrieves the inverse of the current view matrix.
800 *
801 * If the current view matrix is invertible, return true, and if matrix
802 * is non-null, copy the inverse into it. If the current view matrix is
803 * non-invertible, return false and ignore the matrix parameter.
804 *
805 * @param matrix if not null, will receive a copy of the current inverse.
806 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000807 bool getViewInverse(SkMatrix* matrix) const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000808 // TODO: determine whether we really need to leave matrix unmodified
809 // at call sites when inversion fails.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000810 SkMatrix inverse;
bsalomon@google.comca432082013-01-23 19:53:46 +0000811 if (fCommon.fViewMatrix.invert(&inverse)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 if (matrix) {
813 *matrix = inverse;
814 }
815 return true;
816 }
817 return false;
818 }
819
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000820 ////////////////////////////////////////////////////////////////////////////
821
822 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000823 * Preconcats the current view matrix and restores the previous view matrix in the destructor.
bsalomon@google.comc196b522012-10-25 21:52:43 +0000824 * Effect matrices are automatically adjusted to compensate.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000825 */
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000826 class AutoViewMatrixRestore : public ::GrNoncopyable {
827 public:
828 AutoViewMatrixRestore() : fDrawState(NULL) {}
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000829
830 AutoViewMatrixRestore(GrDrawState* ds,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000831 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000832 uint32_t explicitCoordStageMask = 0) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000833 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000834 this->set(ds, preconcatMatrix, explicitCoordStageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000835 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000836
837 ~AutoViewMatrixRestore() { this->restore(); }
838
bsalomon@google.coma8347462012-10-08 18:59:39 +0000839 /**
840 * Can be called prior to destructor to restore the original matrix.
841 */
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000842 void restore();
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000843
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000844 void set(GrDrawState* drawState,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000845 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000846 uint32_t explicitCoordStageMask = 0);
847
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000848 bool isSet() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000849
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000851 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000852 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000853 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000854 uint32_t fRestoreMask;
tomhudson@google.com93813632011-10-27 20:21:16 +0000855 };
856
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000857 ////////////////////////////////////////////////////////////////////////////
858
859 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000860 * This sets the view matrix to identity and adjusts stage matrices to compensate. The
861 * destructor undoes the changes, restoring the view matrix that was set before the
862 * constructor. It is similar to passing the inverse of the current view matrix to
863 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000864 */
865 class AutoDeviceCoordDraw : ::GrNoncopyable {
866 public:
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000867 AutoDeviceCoordDraw() : fDrawState(NULL) {}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000868 /**
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000869 * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
870 * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
871 * to specify such stages.
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000872 */
873 AutoDeviceCoordDraw(GrDrawState* drawState,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000874 uint32_t explicitCoordStageMask = 0) {
875 fDrawState = NULL;
876 this->set(drawState, explicitCoordStageMask);
877 }
878
bsalomon@google.coma8347462012-10-08 18:59:39 +0000879 ~AutoDeviceCoordDraw() { this->restore(); }
880
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000881 bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
882
bsalomon@google.coma8347462012-10-08 18:59:39 +0000883 /**
884 * Returns true if this object was successfully initialized on to a GrDrawState. It may
885 * return false because a non-default constructor or set() were never called or because
886 * the view matrix was not invertible.
887 */
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000888 bool succeeded() const { return NULL != fDrawState; }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000889
bsalomon@google.coma8347462012-10-08 18:59:39 +0000890 /**
891 * Returns the matrix that was set previously set on the drawState. This is only valid
892 * if succeeded returns true.
893 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000894 const SkMatrix& getOriginalMatrix() const {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000895 GrAssert(this->succeeded());
896 return fViewMatrix;
897 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000898
bsalomon@google.coma8347462012-10-08 18:59:39 +0000899 /**
900 * Can be called prior to destructor to restore the original matrix.
901 */
902 void restore();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000903
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000904 private:
bsalomon@google.com288d9542012-10-17 12:53:54 +0000905 GrDrawState* fDrawState;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000906 SkMatrix fViewMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000907 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000908 uint32_t fRestoreMask;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000909 };
910
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000911 /// @}
912
913 ///////////////////////////////////////////////////////////////////////////
914 /// @name Render Target
915 ////
916
917 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000918 * Sets the render-target used at the next drawing call
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000919 *
920 * @param target The render target to set.
921 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000922 void setRenderTarget(GrRenderTarget* target) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000923 fRenderTarget.reset(SkSafeRef(target));
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000924 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000925
926 /**
bsalomon@google.comca432082013-01-23 19:53:46 +0000927 * Retrieves the currently set render-target.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000928 *
929 * @return The currently set render target.
930 */
bsalomon@google.comca432082013-01-23 19:53:46 +0000931 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
932 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000933
934 class AutoRenderTargetRestore : public ::GrNoncopyable {
935 public:
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000936 AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000937 AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
938 fDrawState = NULL;
robertphillips@google.com7460b372012-04-25 16:54:51 +0000939 fSavedTarget = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000940 this->set(ds, newTarget);
941 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000942 ~AutoRenderTargetRestore() { this->restore(); }
943
944 void restore() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000945 if (NULL != fDrawState) {
946 fDrawState->setRenderTarget(fSavedTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000947 fDrawState = NULL;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000948 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000949 GrSafeSetNull(fSavedTarget);
950 }
951
952 void set(GrDrawState* ds, GrRenderTarget* newTarget) {
953 this->restore();
954
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000955 if (NULL != ds) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000956 GrAssert(NULL == fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000957 fSavedTarget = ds->getRenderTarget();
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000958 SkSafeRef(fSavedTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000959 ds->setRenderTarget(newTarget);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000960 fDrawState = ds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000961 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000962 }
963 private:
964 GrDrawState* fDrawState;
965 GrRenderTarget* fSavedTarget;
966 };
967
968 /// @}
969
970 ///////////////////////////////////////////////////////////////////////////
971 /// @name Stencil
972 ////
973
974 /**
975 * Sets the stencil settings to use for the next draw.
976 * Changing the clip has the side-effect of possibly zeroing
977 * out the client settable stencil bits. So multipass algorithms
978 * using stencil should not change the clip between passes.
979 * @param settings the stencil settings to use.
980 */
981 void setStencil(const GrStencilSettings& settings) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000982 fCommon.fStencilSettings = settings;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000983 }
984
985 /**
986 * Shortcut to disable stencil testing and ops.
987 */
988 void disableStencil() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000989 fCommon.fStencilSettings.setDisabled();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000990 }
991
bsalomon@google.comca432082013-01-23 19:53:46 +0000992 const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000993
bsalomon@google.comca432082013-01-23 19:53:46 +0000994 GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000995
996 /// @}
997
998 ///////////////////////////////////////////////////////////////////////////
999 // @name Edge AA
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001000 // Edge equations can be specified to perform anti-aliasing. Because the
bsalomon@google.com7ffe6812012-05-11 17:32:43 +00001001 // edges are specified as per-vertex data, vertices that are shared by
1002 // multiple edges must be split.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001003 //
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001004 ////
1005
1006 /**
tomhudson@google.com93813632011-10-27 20:21:16 +00001007 * When specifying edges as vertex data this enum specifies what type of
bsalomon@google.com81712882012-11-01 17:12:34 +00001008 * edges are in use. The edges are always 4 SkScalars in memory, even when
tomhudson@google.com93813632011-10-27 20:21:16 +00001009 * the edge type requires fewer than 4.
bsalomon@google.com93c96602012-04-27 13:05:21 +00001010 *
1011 * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
1012 * (either adjust in VS or use origin_upper_left in GLSL)
tomhudson@google.com93813632011-10-27 20:21:16 +00001013 */
1014 enum VertexEdgeType {
1015 /* 1-pixel wide line
1016 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
1017 kHairLine_EdgeType,
rmistry@google.comd6176b02012-08-23 18:14:13 +00001018 /* Quadratic specified by u^2-v canonical coords (only 2
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +00001019 components used). Coverage based on signed distance with negative
bsalomon@google.com93c96602012-04-27 13:05:21 +00001020 being inside, positive outside. Edge specified in window space
1021 (y-down) */
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +00001022 kQuad_EdgeType,
1023 /* Same as above but for hairline quadratics. Uses unsigned distance.
1024 Coverage is min(0, 1-distance). */
1025 kHairQuad_EdgeType,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001026 /* Circle specified as center_x, center_y, outer_radius, inner_radius
1027 all in window space (y-down). */
1028 kCircle_EdgeType,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +00001029
1030 kVertexEdgeTypeCnt
tomhudson@google.com93813632011-10-27 20:21:16 +00001031 };
1032
1033 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +00001034 * Determines the interpretation per-vertex edge data when the
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001035 * kEdge_AttribBindingsBit is set (see GrDrawTarget). When per-vertex edges
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001036 * are not specified the value of this setting has no effect.
1037 */
1038 void setVertexEdgeType(VertexEdgeType type) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +00001039 GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
bsalomon@google.comca432082013-01-23 19:53:46 +00001040 fCommon.fVertexEdgeType = type;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001041 }
1042
bsalomon@google.comca432082013-01-23 19:53:46 +00001043 VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001044
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001045 /// @}
tomhudson@google.com62b09682011-11-09 16:39:17 +00001046
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001047 ///////////////////////////////////////////////////////////////////////////
1048 /// @name State Flags
1049 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +00001050
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001051 /**
1052 * Flags that affect rendering. Controlled using enable/disableState(). All
1053 * default to disabled.
1054 */
1055 enum StateBits {
1056 /**
1057 * Perform dithering. TODO: Re-evaluate whether we need this bit
1058 */
1059 kDither_StateBit = 0x01,
1060 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001061 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
1062 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
1063 * the 3D API.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001064 */
1065 kHWAntialias_StateBit = 0x02,
1066 /**
1067 * Draws will respect the clip, otherwise the clip is ignored.
1068 */
1069 kClip_StateBit = 0x04,
1070 /**
1071 * Disables writing to the color buffer. Useful when performing stencil
1072 * operations.
1073 */
1074 kNoColorWrites_StateBit = 0x08,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001075
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001076 /**
1077 * Usually coverage is applied after color blending. The color is blended using the coeffs
1078 * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
1079 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
1080 * this case there is no distinction between coverage and color and the caller needs direct
1081 * control over the blend coeffs. When set, there will be a single blend step controlled by
1082 * setBlendFunc() which will use coverage*color as the src color.
1083 */
1084 kCoverageDrawing_StateBit = 0x10,
1085
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001086 // Users of the class may add additional bits to the vector
1087 kDummyStateBit,
1088 kLastPublicStateBit = kDummyStateBit-1,
1089 };
1090
1091 void resetStateFlags() {
bsalomon@google.comca432082013-01-23 19:53:46 +00001092 fCommon.fFlagBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001093 }
1094
1095 /**
1096 * Enable render state settings.
1097 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001098 * @param stateBits bitfield of StateBits specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001099 */
1100 void enableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001101 fCommon.fFlagBits |= stateBits;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001102 }
1103
1104 /**
1105 * Disable render state settings.
1106 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001107 * @param stateBits bitfield of StateBits specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001108 */
1109 void disableState(uint32_t stateBits) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001110 fCommon.fFlagBits &= ~(stateBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001111 }
1112
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +00001113 /**
1114 * Enable or disable stateBits based on a boolean.
1115 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001116 * @param stateBits bitfield of StateBits to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +00001117 * @param enable if true enable stateBits, otherwise disable
1118 */
1119 void setState(uint32_t stateBits, bool enable) {
1120 if (enable) {
1121 this->enableState(stateBits);
1122 } else {
1123 this->disableState(stateBits);
1124 }
1125 }
1126
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001127 bool isDitherState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001128 return 0 != (fCommon.fFlagBits & kDither_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001129 }
1130
1131 bool isHWAntialiasState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001132 return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001133 }
1134
1135 bool isClipState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001136 return 0 != (fCommon.fFlagBits & kClip_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001137 }
1138
1139 bool isColorWriteDisabled() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001140 return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001141 }
1142
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001143 bool isCoverageDrawing() const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001144 return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +00001145 }
1146
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001147 bool isStateFlagEnabled(uint32_t stateBit) const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001148 return 0 != (stateBit & fCommon.fFlagBits);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001149 }
1150
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001151 /// @}
1152
1153 ///////////////////////////////////////////////////////////////////////////
1154 /// @name Face Culling
1155 ////
1156
1157 enum DrawFace {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001158 kInvalid_DrawFace = -1,
1159
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001160 kBoth_DrawFace,
1161 kCCW_DrawFace,
1162 kCW_DrawFace,
1163 };
1164
1165 /**
1166 * Controls whether clockwise, counterclockwise, or both faces are drawn.
1167 * @param face the face(s) to draw.
1168 */
1169 void setDrawFace(DrawFace face) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001170 GrAssert(kInvalid_DrawFace != face);
bsalomon@google.comca432082013-01-23 19:53:46 +00001171 fCommon.fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001172 }
1173
1174 /**
1175 * Gets whether the target is drawing clockwise, counterclockwise,
1176 * or both faces.
1177 * @return the current draw face(s).
1178 */
bsalomon@google.comca432082013-01-23 19:53:46 +00001179 DrawFace getDrawFace() const { return fCommon.fDrawFace; }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001180
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001181 /// @}
1182
1183 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +00001184
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001185 bool isStageEnabled(int s) const {
1186 GrAssert((unsigned)s < kNumStages);
bsalomon@google.com08283af2012-10-26 13:01:20 +00001187 return (NULL != fStages[s].getEffect());
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001188 }
1189
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001190 bool operator ==(const GrDrawState& s) const {
bsalomon@google.comca432082013-01-23 19:53:46 +00001191 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001192 return false;
1193 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001194 if (fVertexAttribs != s.fVertexAttribs) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001195 return false;
1196 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001197 for (int i = 0; i < kAttribIndexCount; ++i) {
skia.committer@gmail.comf140f182013-03-02 07:01:56 +00001198 if ((i == kPosition_AttribIndex ||
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001199 s.fCommon.fAttribBindings & kAttribIndexMasks[i]) &&
1200 fAttribIndices[i] != s.fAttribIndices[i]) {
1201 return false;
1202 }
1203 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001204 for (int i = 0; i < kNumStages; i++) {
bsalomon@google.comf2f8fc32012-07-18 18:25:07 +00001205 bool enabled = this->isStageEnabled(i);
1206 if (enabled != s.isStageEnabled(i)) {
1207 return false;
1208 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001209 if (enabled && this->fStages[i] != s.fStages[i]) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001210 return false;
1211 }
1212 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001213 return true;
1214 }
1215 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
1216
bsalomon@google.comca432082013-01-23 19:53:46 +00001217 GrDrawState& operator= (const GrDrawState& s) {
1218 this->setRenderTarget(s.fRenderTarget.get());
1219 fCommon = s.fCommon;
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001220 fVertexAttribs = s.fVertexAttribs;
1221 for (int i = 0; i < kAttribIndexCount; i++) {
1222 fAttribIndices[i] = s.fAttribIndices[i];
1223 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001224 for (int i = 0; i < kNumStages; i++) {
tomhudson@google.come742bf02012-07-13 19:54:19 +00001225 if (s.isStageEnabled(i)) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001226 this->fStages[i] = s.fStages[i];
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001227 }
1228 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001229 return *this;
1230 }
1231
1232private:
bsalomon@google.com2e3d1442012-03-26 20:33:54 +00001233
bsalomon@google.comca432082013-01-23 19:53:46 +00001234 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
1235 struct CommonState {
1236 // These fields are roughly sorted by decreasing likelihood of being different in op==
1237 GrColor fColor;
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001238 GrAttribBindings fAttribBindings;
bsalomon@google.comca432082013-01-23 19:53:46 +00001239 SkMatrix fViewMatrix;
1240 GrBlendCoeff fSrcBlend;
1241 GrBlendCoeff fDstBlend;
1242 GrColor fBlendConstant;
1243 uint32_t fFlagBits;
1244 VertexEdgeType fVertexEdgeType;
1245 GrStencilSettings fStencilSettings;
1246 int fFirstCoverageStage;
1247 GrColor fCoverage;
1248 SkXfermode::Mode fColorFilterMode;
1249 GrColor fColorFilterColor;
1250 DrawFace fDrawFace;
1251 bool operator== (const CommonState& other) const {
1252 return fColor == other.fColor &&
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001253 fAttribBindings == other.fAttribBindings &&
bsalomon@google.comca432082013-01-23 19:53:46 +00001254 fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
1255 fSrcBlend == other.fSrcBlend &&
1256 fDstBlend == other.fDstBlend &&
1257 fBlendConstant == other.fBlendConstant &&
1258 fFlagBits == other.fFlagBits &&
1259 fVertexEdgeType == other.fVertexEdgeType &&
1260 fStencilSettings == other.fStencilSettings &&
1261 fFirstCoverageStage == other.fFirstCoverageStage &&
1262 fCoverage == other.fCoverage &&
1263 fColorFilterMode == other.fColorFilterMode &&
1264 fColorFilterColor == other.fColorFilterColor &&
1265 fDrawFace == other.fDrawFace;
1266 }
1267 bool operator!= (const CommonState& other) const { return !(*this == other); }
1268 };
bsalomon@google.com8fe84b52012-03-26 15:24:27 +00001269
bsalomon@google.comca432082013-01-23 19:53:46 +00001270 /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
1271 DeferredState must directly reference GrEffects, however. */
1272 struct SavedEffectStage {
1273 SavedEffectStage() : fEffect(NULL) {}
1274 const GrEffect* fEffect;
1275 GrEffectStage::SavedCoordChange fCoordChange;
1276 };
1277
1278public:
1279 /**
1280 * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
1281 * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
1282 * dispose mechanism returns them to the cache. This allows recycling resources through the
1283 * the cache while they are in a deferred draw queue.
1284 */
1285 class DeferredState {
1286 public:
1287 DeferredState() : fRenderTarget(NULL) {
1288 GR_DEBUGCODE(fInitialized = false;)
1289 }
1290 // TODO: Remove this when DeferredState no longer holds a ref to the RT
1291 ~DeferredState() { SkSafeUnref(fRenderTarget); }
1292
1293 void saveFrom(const GrDrawState& drawState) {
1294 fCommon = drawState.fCommon;
1295 // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1296 fRenderTarget = drawState.fRenderTarget.get();
1297 SkSafeRef(fRenderTarget);
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001298 fVertexAttribs = drawState.fVertexAttribs;
1299 for (int i = 0; i < kAttribIndexCount; i++) {
1300 fAttribIndices[i] = drawState.fAttribIndices[i];
1301 }
bsalomon@google.comca432082013-01-23 19:53:46 +00001302 // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1303 // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1304 // and recycle them to the cache (if no one else is holding a ref to the resources).
1305 for (int i = 0; i < kNumStages; ++i) {
1306 fStages[i].saveFrom(drawState.fStages[i]);
1307 }
1308 GR_DEBUGCODE(fInitialized = true;)
1309 }
1310
1311 void restoreTo(GrDrawState* drawState) {
1312 GrAssert(fInitialized);
1313 drawState->fCommon = fCommon;
1314 drawState->setRenderTarget(fRenderTarget);
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001315 drawState->fVertexAttribs = fVertexAttribs;
1316 for (int i = 0; i < kAttribIndexCount; i++) {
1317 drawState->fAttribIndices[i] = fAttribIndices[i];
1318 }
bsalomon@google.comca432082013-01-23 19:53:46 +00001319 for (int i = 0; i < kNumStages; ++i) {
1320 fStages[i].restoreTo(&drawState->fStages[i]);
1321 }
1322 }
1323
1324 bool isEqual(const GrDrawState& state) const {
1325 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
1326 return false;
1327 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001328 for (int i = 0; i < kAttribIndexCount; ++i) {
skia.committer@gmail.comf140f182013-03-02 07:01:56 +00001329 if ((i == kPosition_AttribIndex ||
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001330 state.fCommon.fAttribBindings & kAttribIndexMasks[i]) &&
1331 fAttribIndices[i] != state.fAttribIndices[i]) {
1332 return false;
1333 }
1334 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001335 if (fVertexAttribs != state.fVertexAttribs) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001336 return false;
1337 }
bsalomon@google.comca432082013-01-23 19:53:46 +00001338 for (int i = 0; i < kNumStages; ++i) {
bsalomon@google.comdcd69bf2013-01-24 18:28:51 +00001339 if (!fStages[i].isEqual(state.fStages[i])) {
bsalomon@google.comca432082013-01-23 19:53:46 +00001340 return false;
1341 }
1342 }
1343 return true;
1344 }
1345
1346 private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001347 GrRenderTarget* fRenderTarget;
1348 CommonState fCommon;
1349 int fAttribIndices[kAttribIndexCount];
1350 GrVertexAttribArray<kVertexAttribCnt> fVertexAttribs;
1351 GrEffectStage::DeferredStage fStages[kNumStages];
bsalomon@google.comca432082013-01-23 19:53:46 +00001352
1353 GR_DEBUGCODE(bool fInitialized;)
1354 };
1355
1356private:
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001357 // helper array to let us check the current bindings so we know what bound attrib indices
1358 // we care about
1359 static const GrAttribBindings kAttribIndexMasks[kAttribIndexCount];
1360
1361 SkAutoTUnref<GrRenderTarget> fRenderTarget;
1362 CommonState fCommon;
1363 int fAttribIndices[kAttribIndexCount];
1364 GrVertexAttribArray<kVertexAttribCnt> fVertexAttribs;
1365 GrEffectStage fStages[kNumStages];
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001366
reed@google.comfa35e3d2012-06-26 20:16:17 +00001367 typedef GrRefCnt INHERITED;
tomhudson@google.com93813632011-10-27 20:21:16 +00001368};
1369
bsalomon@google.com2b446732013-02-12 16:47:41 +00001370GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags);
1371
tomhudson@google.com93813632011-10-27 20:21:16 +00001372#endif