blob: 37baee823cacf111722e14a16f27e7fda2fcf3d3 [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
11#include "GrColor.h"
12#include "GrMatrix.h"
13#include "GrSamplerState.h"
14#include "GrStencil.h"
15
16#include "SkXfermode.h"
17
18class GrRenderTarget;
19class GrTexture;
20
21struct GrDrawState {
22
23 /**
24 * Number of texture stages. Each stage takes as input a color and
25 * 2D texture coordinates. The color input to the first enabled stage is the
26 * per-vertex color or the constant color (setColor/setAlpha) if there are
27 * no per-vertex colors. For subsequent stages the input color is the output
28 * color from the previous enabled stage. The output color of each stage is
29 * the input color modulated with the result of a texture lookup. Texture
30 * lookups are specified by a texture a sampler (setSamplerState). Texture
31 * coordinates for each stage come from the vertices based on a
32 * GrVertexLayout bitfield. The output fragment color is the output color of
33 * the last enabled stage. The presence or absence of texture coordinates
34 * for each stage in the vertex layout indicates whether a stage is enabled
35 * or not.
36 */
37 enum {
38 kNumStages = 3,
39 kMaxTexCoords = kNumStages
40 };
41
42 enum DrawFace {
43 kBoth_DrawFace,
44 kCCW_DrawFace,
45 kCW_DrawFace,
46 };
47
48 /**
49 * When specifying edges as vertex data this enum specifies what type of
50 * edges are in use. The edges are always 4 GrScalars in memory, even when
51 * the edge type requires fewer than 4.
52 */
53 enum VertexEdgeType {
54 /* 1-pixel wide line
55 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
56 kHairLine_EdgeType,
57 /* 1-pixel wide quadratic
58 u^2-v canonical coords (only 2 components used) */
59 kHairQuad_EdgeType
60 };
61
62 /**
63 * The absolute maximum number of edges that may be specified for
64 * a single draw call when performing edge antialiasing. This is used for
65 * the size of several static buffers, so implementations of getMaxEdges()
66 * (below) should clamp to this value.
67 */
68 enum {
69 kMaxEdges = 32
70 };
71
72 class Edge {
73 public:
74 Edge() {}
75 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
76 GrPoint intersect(const Edge& other) {
77 return GrPoint::Make(
78 (fY * other.fZ - other.fY * fZ) /
79 (fX * other.fY - other.fX * fY),
80 (fX * other.fZ - other.fX * fZ) /
81 (other.fX * fY - fX * other.fY));
82 }
83 float fX, fY, fZ;
84 };
85
86 GrDrawState() {
87 // make sure any pad is zero for memcmp
88 // all GrDrawState members should default to something
89 // valid by the memset
90 memset(this, 0, sizeof(GrDrawState));
91
92 // memset exceptions
93 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
94 fFirstCoverageStage = kNumStages;
95
96 // pedantic assertion that our ptrs will
97 // be NULL (0 ptr is mem addr 0)
98 GrAssert((intptr_t)(void*)NULL == 0LL);
99
100 // default stencil setting should be disabled
101 GrAssert(fStencilSettings.isDisabled());
102 fFirstCoverageStage = kNumStages;
103 }
104
105 uint32_t fFlagBits;
106 GrBlendCoeff fSrcBlend;
107 GrBlendCoeff fDstBlend;
108 GrColor fBlendConstant;
109 GrTexture* fTextures[kNumStages];
110 GrSamplerState fSamplerStates[kNumStages];
111 int fFirstCoverageStage;
112 GrRenderTarget* fRenderTarget;
113 GrColor fColor;
114 DrawFace fDrawFace;
115 GrColor fColorFilterColor;
116 SkXfermode::Mode fColorFilterXfermode;
117
118 GrStencilSettings fStencilSettings;
119 GrMatrix fViewMatrix;
120 VertexEdgeType fVertexEdgeType;
121 Edge fEdgeAAEdges[kMaxEdges];
122 int fEdgeAANumEdges;
123 bool operator ==(const GrDrawState& s) const {
124 return 0 == memcmp(this, &s, sizeof(GrDrawState));
125 }
126 bool operator !=(const GrDrawState& s) const { return !(*this == s); }
127};
128
129#endif