| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 11 | #include "GrBackendEffectFactory.h" |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 12 | #include "GrColor.h" |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 13 | #include "GrEffectStage.h" |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 14 | #include "GrRefCnt.h" |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 16 | #include "GrStencil.h" |
| 17 | #include "GrTemplates.h" |
| 18 | #include "GrTexture.h" |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 19 | #include "effects/GrSimpleTextureEffect.h" |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 20 | |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 21 | #include "SkMatrix.h" |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 22 | #include "SkXfermode.h" |
| 23 | |
| bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 24 | class GrPaint; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 25 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Types used to describe format of vertices in arrays |
| 28 | */ |
| 29 | enum 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 | }; |
| 38 | static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1; |
| 39 | |
| 40 | struct GrVertexAttrib { |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 41 | inline void set(GrVertexAttribType type, size_t offset) { |
| jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 42 | fType = type; fOffset = offset; |
| 43 | } |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 44 | 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 | |
| 53 | template <int N> |
| 54 | class GrVertexAttribArray : public SkSTArray<N, GrVertexAttrib, true> {}; |
| 55 | |
| 56 | /** |
| 57 | * Type used to describe how attributes bind to program usage |
| 58 | */ |
| 59 | typedef int GrAttribBindings; |
| 60 | |
| bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 61 | class GrDrawState : public GrRefCnt { |
| bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 62 | public: |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 63 | SK_DECLARE_INST_COUNT(GrDrawState) |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 64 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 65 | /** |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 66 | * 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.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 70 | * GrAttribBindings used to draw. |
| robertphillips@google.com | bf5cad4 | 2012-05-10 12:40:40 +0000 | [diff] [blame] | 71 | * |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 72 | * 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.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 78 | * per-vertex colors, depending upon GrAttribBindings. The input to the first coverage stage is |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 79 | * 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.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 83 | * See the documentation of kCoverageDrawing_StateBit for information about disabling the |
| 84 | * the color / coverage distinction. |
| 85 | * |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 86 | * 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.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 89 | * by GrClipMaskManager. |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 90 | */ |
| 91 | enum { |
| twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 92 | kNumStages = 5, |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 95 | GrDrawState() { |
| reed@google.com | 7584719 | 2013-01-28 20:53:22 +0000 | [diff] [blame] | 96 | #if GR_DEBUG |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 97 | VertexAttributesUnitTest(); |
| reed@google.com | 7584719 | 2013-01-28 20:53:22 +0000 | [diff] [blame] | 98 | #endif |
| bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 99 | this->reset(); |
| 100 | } |
| bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 101 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 102 | GrDrawState(const GrDrawState& state) { |
| bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 103 | *this = state; |
| 104 | } |
| 105 | |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 106 | virtual ~GrDrawState() { |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 107 | this->disableStages(); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 110 | /** |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 111 | * Resets to the default state. |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 112 | * GrEffects will be removed from all stages. |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | */ |
| bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 114 | void reset() { |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 115 | |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 116 | this->disableStages(); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 117 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 118 | fRenderTarget.reset(NULL); |
| 119 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 120 | this->setDefaultVertexAttribs(); |
| 121 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 122 | 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.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 139 | * encompasses more than GrPaint. Aspects of GrDrawState that have no |
| bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 140 | * 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.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 144 | |
| 145 | /////////////////////////////////////////////////////////////////////////// |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 146 | /// @name Vertex Attributes |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 147 | //// |
| 148 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 149 | enum { |
| 150 | kVertexAttribCnt = 6, |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 153 | /** |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 154 | * 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.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 157 | * |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 158 | * This will only set up the vertex geometry. To bind the attributes in |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 159 | * the shaders, attribute indices and attribute bindings need to be set |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 160 | * as well. |
| jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 161 | */ |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 162 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 163 | /** |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 164 | * Sets vertex attributes for next draw. |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 165 | * |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 166 | * @param attribs the array of vertex attributes to set. |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 167 | * @param count the number of attributes being set. |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 168 | * limited to a count of kVertexAttribCnt. |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 169 | */ |
| 170 | void setVertexAttribs(const GrVertexAttrib attribs[], int count); |
| jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 171 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 172 | 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.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 178 | * Sets default vertex attributes for next draw. |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 179 | * |
| 180 | * This will also set default vertex attribute indices and bindings |
| 181 | */ |
| 182 | void setDefaultVertexAttribs(); |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 183 | |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 184 | bool validateVertexAttribs() const; |
| 185 | |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 186 | //////////////////////////////////////////////////////////////////////////// |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 187 | // Helpers for picking apart vertex attributes |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 188 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 189 | // 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.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 192 | |
| 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.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 202 | * @param vertices the vertex array |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 203 | * @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.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 251 | /// @} |
| 252 | |
| 253 | /////////////////////////////////////////////////////////////////////////// |
| 254 | /// @name Attribute Bindings |
| 255 | //// |
| 256 | |
| 257 | /** |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 258 | * 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 |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 260 | * coordinates, per-vertex colors, per-vertex coverage and edge data. Each |
| 261 | * stage can use the explicit texture coordinates as its input texture |
| 262 | * coordinates or it may use the positions as texture coordinates. |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 263 | */ |
| 264 | |
| 265 | /** |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 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 | /** |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 280 | * Additional Bits that can be specified in GrAttribBindings. |
| 281 | */ |
| 282 | enum AttribBindingsBits { |
| 283 | /* program uses colors (GrColor) */ |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 284 | kColor_AttribBindingsBit = 1 << (kNumStages + 0), |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 285 | /* program uses coverage (GrColor) |
| 286 | */ |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 287 | kCoverage_AttribBindingsBit = 1 << (kNumStages + 1), |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 288 | /* program uses edge data. Distance to the edge is used to |
| 289 | * compute a coverage. See GrDrawState::setVertexEdgeType(). |
| 290 | */ |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 291 | kEdge_AttribBindingsBit = 1 << (kNumStages + 2), |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 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 | /** |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 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 | /** |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 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(); |
| skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 337 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 338 | /// @} |
| 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.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 347 | * 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.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 349 | * attribute index if we're binding attributes in GL. |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 350 | * |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 351 | * 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, |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 359 | kTexCoord_AttribIndex, |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 360 | |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 361 | kLast_AttribIndex = kTexCoord_AttribIndex |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 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.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 372 | // Helpers to set attribute indices. These should match the index in the |
| 373 | // current attribute index array. |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 374 | |
| 375 | /** |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 376 | * Sets index for next draw. This is used to look up the offset |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 377 | * 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.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 385 | |
| 386 | /// @} |
| 387 | |
| 388 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 389 | /// @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.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 397 | void setColor(GrColor color) { fCommon.fColor = color; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 398 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 399 | GrColor getColor() const { return fCommon.fColor; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 400 | |
| 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 |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 413 | * after color-computing texture stages. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 414 | */ |
| 415 | void setColorFilter(GrColor c, SkXfermode::Mode mode) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 416 | fCommon.fColorFilterColor = c; |
| 417 | fCommon.fColorFilterMode = mode; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 420 | GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; } |
| 421 | SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 422 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 423 | /** |
| 424 | * Constructor sets the color to be 'color' which is undone by the destructor. |
| 425 | */ |
| 426 | class AutoColorRestore : public ::GrNoncopyable { |
| 427 | public: |
| sugoi@google.com | 66a58ac | 2013-03-05 20:40:52 +0000 | [diff] [blame] | 428 | AutoColorRestore() : fDrawState(NULL), fOldColor(0) {} |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 429 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 430 | AutoColorRestore(GrDrawState* drawState, GrColor color) { |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 431 | 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.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 444 | fDrawState = drawState; |
| 445 | fOldColor = fDrawState->getColor(); |
| 446 | fDrawState->setColor(color); |
| 447 | } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 448 | |
| 449 | ~AutoColorRestore() { this->reset(); } |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 450 | private: |
| 451 | GrDrawState* fDrawState; |
| 452 | GrColor fOldColor; |
| 453 | }; |
| 454 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 455 | /// @} |
| 456 | |
| 457 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 458 | /// @name Coverage |
| 459 | //// |
| 460 | |
| 461 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 462 | * Sets a constant fractional coverage to be applied to the draw. The |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 463 | * 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.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 467 | fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 468 | } |
| 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.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 475 | fCommon.fCoverage = coverage; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | GrColor getCoverage() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 479 | return fCommon.fCoverage; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /// @} |
| 483 | |
| 484 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 485 | /// @name Effect Stages |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 486 | //// |
| 487 | |
| jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 488 | const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) { |
| 489 | fStages[stageIdx].setEffect(effect); |
| 490 | return effect; |
| 491 | } |
| skia.committer@gmail.com | 01c34ee | 2013-03-20 07:01:02 +0000 | [diff] [blame] | 492 | |
| skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 493 | const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect, |
| jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 494 | int attr0, int attr1 = -1) { |
| 495 | fStages[stageIdx].setEffect(effect, attr0, attr1); |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 496 | return effect; |
| 497 | } |
| 498 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 499 | /** |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 500 | * Creates a GrSimpleTextureEffect. |
| tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 501 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 502 | void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 503 | GrAssert(!this->getStage(stageIdx).getEffect()); |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 504 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 505 | this->setEffect(stageIdx, effect)->unref(); |
| bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 506 | } |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 507 | void createTextureEffect(int stageIdx, |
| 508 | GrTexture* texture, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 509 | const SkMatrix& matrix, |
| bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 510 | const GrTextureParams& params) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 511 | GrAssert(!this->getStage(stageIdx).getEffect()); |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 512 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params); |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 513 | this->setEffect(stageIdx, effect)->unref(); |
| bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 516 | bool stagesDisabled() { |
| 517 | for (int i = 0; i < kNumStages; ++i) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 518 | if (NULL != fStages[i].getEffect()) { |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 519 | return false; |
| 520 | } |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 521 | } |
| tomhudson@google.com | 3eee8fb | 2012-06-25 12:30:34 +0000 | [diff] [blame] | 522 | return true; |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 523 | } |
| tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 524 | |
| skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 525 | void disableStage(int stageIdx) { |
| jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 526 | this->setEffect(stageIdx, NULL); |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 527 | } |
| tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 528 | |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 529 | /** |
| bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 530 | * Release all the GrEffects referred to by this draw state. |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 531 | */ |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 532 | void disableStages() { |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 533 | for (int i = 0; i < kNumStages; ++i) { |
| tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 534 | this->disableStage(i); |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 538 | class AutoStageDisable : public ::GrNoncopyable { |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 539 | public: |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 540 | AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {} |
| 541 | ~AutoStageDisable() { |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 542 | if (NULL != fDrawState) { |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 543 | fDrawState->disableStages(); |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | private: |
| 547 | GrDrawState* fDrawState; |
| 548 | }; |
| 549 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 550 | /** |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 551 | * Returns the current stage by index. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 552 | */ |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 553 | const GrEffectStage& getStage(int stageIdx) const { |
| 554 | GrAssert((unsigned)stageIdx < kNumStages); |
| 555 | return fStages[stageIdx]; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 559 | * Called when the source coord system is changing. preConcat gives the transformation from the |
| 560 | * old coord system to the new coord system. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 561 | */ |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 562 | void preConcatStageMatrices(const SkMatrix& preConcat) { |
| 563 | this->preConcatStageMatrices(~0U, preConcat); |
| 564 | } |
| 565 | /** |
| 566 | * Version of above that applies the update matrix selectively to stages via a mask. |
| 567 | */ |
| 568 | void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) { |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 569 | for (int i = 0; i < kNumStages; ++i) { |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 570 | if (((1 << i) & stageMask) && this->isStageEnabled(i)) { |
| 571 | fStages[i].preConcatCoordChange(preConcat); |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 576 | /** |
| 577 | * Called when the source coord system is changing. preConcatInverse is the inverse of the |
| 578 | * transformation from the old coord system to the new coord system. Returns false if the matrix |
| 579 | * cannot be inverted. |
| 580 | */ |
| 581 | bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) { |
| 582 | SkMatrix inv; |
| 583 | bool computed = false; |
| 584 | for (int i = 0; i < kNumStages; ++i) { |
| 585 | if (this->isStageEnabled(i)) { |
| 586 | if (!computed && !preConcatInverse.invert(&inv)) { |
| 587 | return false; |
| 588 | } else { |
| 589 | computed = true; |
| 590 | } |
| 591 | fStages[i].preConcatCoordChange(preConcatInverse); |
| 592 | } |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 597 | /// @} |
| 598 | |
| 599 | /////////////////////////////////////////////////////////////////////////// |
| 600 | /// @name Coverage / Color Stages |
| 601 | //// |
| 602 | |
| 603 | /** |
| 604 | * A common pattern is to compute a color with the initial stages and then |
| 605 | * modulate that color by a coverage value in later stage(s) (AA, mask- |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 606 | * filters, glyph mask, etc). Color-filters, xfermodes, etc should be |
| 607 | * computed based on the pre-coverage-modulated color. The division of |
| 608 | * stages between color-computing and coverage-computing is specified by |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 609 | * this method. Initially this is kNumStages (all stages |
| 610 | * are color-computing). |
| 611 | */ |
| 612 | void setFirstCoverageStage(int firstCoverageStage) { |
| 613 | GrAssert((unsigned)firstCoverageStage <= kNumStages); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 614 | fCommon.fFirstCoverageStage = firstCoverageStage; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Gets the index of the first coverage-computing stage. |
| 619 | */ |
| 620 | int getFirstCoverageStage() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 621 | return fCommon.fFirstCoverageStage; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | ///@} |
| 625 | |
| 626 | /////////////////////////////////////////////////////////////////////////// |
| 627 | /// @name Blending |
| 628 | //// |
| 629 | |
| 630 | /** |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 631 | * Sets the blending function coefficients. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 632 | * |
| 633 | * The blend function will be: |
| 634 | * D' = sat(S*srcCoef + D*dstCoef) |
| 635 | * |
| 636 | * where D is the existing destination color, S is the incoming source |
| 637 | * color, and D' is the new destination color that will be written. sat() |
| 638 | * is the saturation function. |
| 639 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 640 | * @param srcCoef coefficient applied to the src color. |
| 641 | * @param dstCoef coefficient applied to the dst color. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 642 | */ |
| 643 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 644 | fCommon.fSrcBlend = srcCoeff; |
| 645 | fCommon.fDstBlend = dstCoeff; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 646 | #if GR_DEBUG |
| 647 | switch (dstCoeff) { |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 648 | case kDC_GrBlendCoeff: |
| 649 | case kIDC_GrBlendCoeff: |
| 650 | case kDA_GrBlendCoeff: |
| 651 | case kIDA_GrBlendCoeff: |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 652 | GrPrintf("Unexpected dst blend coeff. Won't work correctly with" |
| 653 | "coverage stages.\n"); |
| 654 | break; |
| 655 | default: |
| 656 | break; |
| 657 | } |
| 658 | switch (srcCoeff) { |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 659 | case kSC_GrBlendCoeff: |
| 660 | case kISC_GrBlendCoeff: |
| 661 | case kSA_GrBlendCoeff: |
| 662 | case kISA_GrBlendCoeff: |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 663 | GrPrintf("Unexpected src blend coeff. Won't work correctly with" |
| 664 | "coverage stages.\n"); |
| 665 | break; |
| 666 | default: |
| 667 | break; |
| 668 | } |
| 669 | #endif |
| 670 | } |
| 671 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 672 | GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; } |
| 673 | GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 674 | |
| 675 | void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff, |
| 676 | GrBlendCoeff* dstBlendCoeff) const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 677 | *srcBlendCoeff = fCommon.fSrcBlend; |
| 678 | *dstBlendCoeff = fCommon.fDstBlend; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Sets the blending function constant referenced by the following blending |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 683 | * coefficients: |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 684 | * kConstC_GrBlendCoeff |
| 685 | * kIConstC_GrBlendCoeff |
| 686 | * kConstA_GrBlendCoeff |
| 687 | * kIConstA_GrBlendCoeff |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 688 | * |
| 689 | * @param constant the constant to set |
| 690 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 691 | void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 692 | |
| 693 | /** |
| 694 | * Retrieves the last value set by setBlendConstant() |
| 695 | * @return the blending constant value |
| 696 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 697 | GrColor getBlendConstant() const { return fCommon.fBlendConstant; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 698 | |
| bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 699 | /** |
| 700 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 701 | * coverage before the blend will give the correct final destination color. In general it |
| 702 | * will not as coverage is applied after blending. |
| 703 | */ |
| 704 | bool canTweakAlphaForCoverage() const; |
| 705 | |
| 706 | /** |
| 707 | * Optimizations for blending / coverage to that can be applied based on the current state. |
| 708 | */ |
| 709 | enum BlendOptFlags { |
| 710 | /** |
| 711 | * No optimization |
| 712 | */ |
| 713 | kNone_BlendOpt = 0, |
| 714 | /** |
| 715 | * Don't draw at all |
| 716 | */ |
| 717 | kSkipDraw_BlendOptFlag = 0x1, |
| 718 | /** |
| 719 | * Emit the src color, disable HW blending (replace dst with src) |
| 720 | */ |
| 721 | kDisableBlend_BlendOptFlag = 0x2, |
| 722 | /** |
| 723 | * The coverage value does not have to be computed separately from alpha, the the output |
| 724 | * color can be the modulation of the two. |
| 725 | */ |
| 726 | kCoverageAsAlpha_BlendOptFlag = 0x4, |
| 727 | /** |
| 728 | * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are |
| 729 | * "don't cares". |
| 730 | */ |
| 731 | kEmitCoverage_BlendOptFlag = 0x8, |
| 732 | /** |
| 733 | * Emit transparent black instead of the src color, no need to compute coverage. |
| 734 | */ |
| 735 | kEmitTransBlack_BlendOptFlag = 0x10, |
| 736 | }; |
| 737 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 738 | |
| 739 | /** |
| 740 | * Determines what optimizations can be applied based on the blend. The coefficients may have |
| 741 | * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional |
| 742 | * params that receive the tweaked coefficients. Normally the function looks at the current |
| 743 | * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively |
| 744 | * determine the blend optimizations that would be used if there was partial pixel coverage. |
| 745 | * |
| 746 | * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for |
| 747 | * playback) must call this function and respect the flags that replace the output color. |
| 748 | */ |
| 749 | BlendOptFlags getBlendOpts(bool forceCoverage = false, |
| 750 | GrBlendCoeff* srcCoeff = NULL, |
| 751 | GrBlendCoeff* dstCoeff = NULL) const; |
| 752 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 753 | /// @} |
| 754 | |
| 755 | /////////////////////////////////////////////////////////////////////////// |
| 756 | /// @name View Matrix |
| 757 | //// |
| 758 | |
| 759 | /** |
| robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 760 | * Sets the matrix applied to vertex positions. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 761 | * |
| 762 | * In the post-view-matrix space the rectangle [0,w]x[0,h] |
| 763 | * fully covers the render target. (w and h are the width and height of the |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 764 | * the render-target.) |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 765 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 766 | void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 767 | |
| 768 | /** |
| 769 | * Gets a writable pointer to the view matrix. |
| 770 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 771 | SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 772 | |
| 773 | /** |
| 774 | * Multiplies the current view matrix by a matrix |
| 775 | * |
| 776 | * After this call V' = V*m where V is the old view matrix, |
| 777 | * m is the parameter to this function, and V' is the new view matrix. |
| 778 | * (We consider positions to be column vectors so position vector p is |
| 779 | * transformed by matrix X as p' = X*p.) |
| 780 | * |
| 781 | * @param m the matrix used to modify the view matrix. |
| 782 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 783 | void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 784 | |
| 785 | /** |
| 786 | * Multiplies the current view matrix by a matrix |
| 787 | * |
| 788 | * After this call V' = m*V where V is the old view matrix, |
| 789 | * m is the parameter to this function, and V' is the new view matrix. |
| 790 | * (We consider positions to be column vectors so position vector p is |
| 791 | * transformed by matrix X as p' = X*p.) |
| 792 | * |
| 793 | * @param m the matrix used to modify the view matrix. |
| 794 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 795 | void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 796 | |
| 797 | /** |
| 798 | * Retrieves the current view matrix |
| 799 | * @return the current view matrix. |
| 800 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 801 | const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 802 | |
| 803 | /** |
| 804 | * Retrieves the inverse of the current view matrix. |
| 805 | * |
| 806 | * If the current view matrix is invertible, return true, and if matrix |
| 807 | * is non-null, copy the inverse into it. If the current view matrix is |
| 808 | * non-invertible, return false and ignore the matrix parameter. |
| 809 | * |
| 810 | * @param matrix if not null, will receive a copy of the current inverse. |
| 811 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 812 | bool getViewInverse(SkMatrix* matrix) const { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 813 | // TODO: determine whether we really need to leave matrix unmodified |
| 814 | // at call sites when inversion fails. |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 815 | SkMatrix inverse; |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 816 | if (fCommon.fViewMatrix.invert(&inverse)) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 817 | if (matrix) { |
| 818 | *matrix = inverse; |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | return false; |
| 823 | } |
| 824 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 825 | //////////////////////////////////////////////////////////////////////////// |
| 826 | |
| 827 | /** |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 828 | * Preconcats the current view matrix and restores the previous view matrix in the destructor. |
| bsalomon@google.com | c196b52 | 2012-10-25 21:52:43 +0000 | [diff] [blame] | 829 | * Effect matrices are automatically adjusted to compensate. |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 830 | */ |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 831 | class AutoViewMatrixRestore : public ::GrNoncopyable { |
| 832 | public: |
| 833 | AutoViewMatrixRestore() : fDrawState(NULL) {} |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 834 | |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 835 | AutoViewMatrixRestore(GrDrawState* ds, |
| 836 | const SkMatrix& preconcatMatrix, |
| 837 | uint32_t explicitCoordStageMask = 0) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 838 | fDrawState = NULL; |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 839 | this->set(ds, preconcatMatrix, explicitCoordStageMask); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 840 | } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 841 | |
| 842 | ~AutoViewMatrixRestore() { this->restore(); } |
| 843 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 844 | /** |
| 845 | * Can be called prior to destructor to restore the original matrix. |
| 846 | */ |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 847 | void restore(); |
| skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 848 | |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 849 | void set(GrDrawState* drawState, |
| 850 | const SkMatrix& preconcatMatrix, |
| 851 | uint32_t explicitCoordStageMask = 0); |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 852 | |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 853 | bool isSet() const { return NULL != fDrawState; } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 854 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 855 | private: |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 856 | GrDrawState* fDrawState; |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 857 | SkMatrix fViewMatrix; |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 858 | GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages]; |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 859 | uint32_t fRestoreMask; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 860 | }; |
| 861 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 862 | //////////////////////////////////////////////////////////////////////////// |
| 863 | |
| 864 | /** |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 865 | * This sets the view matrix to identity and adjusts stage matrices to compensate. The |
| 866 | * destructor undoes the changes, restoring the view matrix that was set before the |
| 867 | * constructor. It is similar to passing the inverse of the current view matrix to |
| 868 | * AutoViewMatrixRestore, but lazily computes the inverse only if necessary. |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 869 | */ |
| 870 | class AutoDeviceCoordDraw : ::GrNoncopyable { |
| 871 | public: |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 872 | AutoDeviceCoordDraw() : fDrawState(NULL) {} |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 873 | /** |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 874 | * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to |
| 875 | * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used |
| 876 | * to specify such stages. |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 877 | */ |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 878 | AutoDeviceCoordDraw(GrDrawState* drawState, |
| 879 | uint32_t explicitCoordStageMask = 0) { |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 880 | fDrawState = NULL; |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 881 | this->set(drawState, explicitCoordStageMask); |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 884 | ~AutoDeviceCoordDraw() { this->restore(); } |
| 885 | |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 886 | bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0); |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 887 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 888 | /** |
| 889 | * Returns true if this object was successfully initialized on to a GrDrawState. It may |
| 890 | * return false because a non-default constructor or set() were never called or because |
| 891 | * the view matrix was not invertible. |
| 892 | */ |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 893 | bool succeeded() const { return NULL != fDrawState; } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 894 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 895 | /** |
| 896 | * Returns the matrix that was set previously set on the drawState. This is only valid |
| 897 | * if succeeded returns true. |
| 898 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 899 | const SkMatrix& getOriginalMatrix() const { |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 900 | GrAssert(this->succeeded()); |
| 901 | return fViewMatrix; |
| 902 | } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 903 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 904 | /** |
| 905 | * Can be called prior to destructor to restore the original matrix. |
| 906 | */ |
| 907 | void restore(); |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 908 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 909 | private: |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 910 | GrDrawState* fDrawState; |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 911 | SkMatrix fViewMatrix; |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 912 | GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages]; |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 913 | uint32_t fRestoreMask; |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 914 | }; |
| 915 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 916 | /// @} |
| 917 | |
| 918 | /////////////////////////////////////////////////////////////////////////// |
| 919 | /// @name Render Target |
| 920 | //// |
| 921 | |
| 922 | /** |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 923 | * Sets the render-target used at the next drawing call |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 924 | * |
| 925 | * @param target The render target to set. |
| 926 | */ |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 927 | void setRenderTarget(GrRenderTarget* target) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 928 | fRenderTarget.reset(SkSafeRef(target)); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 929 | } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 930 | |
| 931 | /** |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 932 | * Retrieves the currently set render-target. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 933 | * |
| 934 | * @return The currently set render target. |
| 935 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 936 | const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
| 937 | GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 938 | |
| 939 | class AutoRenderTargetRestore : public ::GrNoncopyable { |
| 940 | public: |
| bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 941 | AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {} |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 942 | AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 943 | fDrawState = NULL; |
| robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 944 | fSavedTarget = NULL; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 945 | this->set(ds, newTarget); |
| 946 | } |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 947 | ~AutoRenderTargetRestore() { this->restore(); } |
| 948 | |
| 949 | void restore() { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 950 | if (NULL != fDrawState) { |
| 951 | fDrawState->setRenderTarget(fSavedTarget); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 952 | fDrawState = NULL; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 953 | } |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 954 | GrSafeSetNull(fSavedTarget); |
| 955 | } |
| 956 | |
| 957 | void set(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 958 | this->restore(); |
| 959 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 960 | if (NULL != ds) { |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 961 | GrAssert(NULL == fSavedTarget); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 962 | fSavedTarget = ds->getRenderTarget(); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 963 | SkSafeRef(fSavedTarget); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 964 | ds->setRenderTarget(newTarget); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 965 | fDrawState = ds; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 966 | } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 967 | } |
| 968 | private: |
| 969 | GrDrawState* fDrawState; |
| 970 | GrRenderTarget* fSavedTarget; |
| 971 | }; |
| 972 | |
| 973 | /// @} |
| 974 | |
| 975 | /////////////////////////////////////////////////////////////////////////// |
| 976 | /// @name Stencil |
| 977 | //// |
| 978 | |
| 979 | /** |
| 980 | * Sets the stencil settings to use for the next draw. |
| 981 | * Changing the clip has the side-effect of possibly zeroing |
| 982 | * out the client settable stencil bits. So multipass algorithms |
| 983 | * using stencil should not change the clip between passes. |
| 984 | * @param settings the stencil settings to use. |
| 985 | */ |
| 986 | void setStencil(const GrStencilSettings& settings) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 987 | fCommon.fStencilSettings = settings; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Shortcut to disable stencil testing and ops. |
| 992 | */ |
| 993 | void disableStencil() { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 994 | fCommon.fStencilSettings.setDisabled(); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 997 | const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 998 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 999 | GrStencilSettings* stencil() { return &fCommon.fStencilSettings; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1000 | |
| 1001 | /// @} |
| 1002 | |
| 1003 | /////////////////////////////////////////////////////////////////////////// |
| 1004 | // @name Edge AA |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1005 | // Edge equations can be specified to perform anti-aliasing. Because the |
| bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 1006 | // edges are specified as per-vertex data, vertices that are shared by |
| 1007 | // multiple edges must be split. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1008 | // |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1009 | //// |
| 1010 | |
| 1011 | /** |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1012 | * When specifying edges as vertex data this enum specifies what type of |
| bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 1013 | * edges are in use. The edges are always 4 SkScalars in memory, even when |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1014 | * the edge type requires fewer than 4. |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1015 | * |
| 1016 | * TODO: Fix the fact that HairLine and Circle edge types use y-down coords. |
| 1017 | * (either adjust in VS or use origin_upper_left in GLSL) |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1018 | */ |
| 1019 | enum VertexEdgeType { |
| 1020 | /* 1-pixel wide line |
| 1021 | 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */ |
| 1022 | kHairLine_EdgeType, |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1023 | /* Quadratic specified by u^2-v canonical coords (only 2 |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 1024 | components used). Coverage based on signed distance with negative |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1025 | being inside, positive outside. Edge specified in window space |
| 1026 | (y-down) */ |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 1027 | kQuad_EdgeType, |
| 1028 | /* Same as above but for hairline quadratics. Uses unsigned distance. |
| 1029 | Coverage is min(0, 1-distance). */ |
| 1030 | kHairQuad_EdgeType, |
| 1031 | |
| 1032 | kVertexEdgeTypeCnt |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1033 | }; |
| 1034 | |
| 1035 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1036 | * Determines the interpretation per-vertex edge data when the |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1037 | * kEdge_AttribBindingsBit is set (see GrDrawTarget). When per-vertex edges |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1038 | * are not specified the value of this setting has no effect. |
| 1039 | */ |
| 1040 | void setVertexEdgeType(VertexEdgeType type) { |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 1041 | GrAssert(type >=0 && type < kVertexEdgeTypeCnt); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1042 | fCommon.fVertexEdgeType = type; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1045 | VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1046 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1047 | /// @} |
| tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1048 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1049 | /////////////////////////////////////////////////////////////////////////// |
| 1050 | /// @name State Flags |
| 1051 | //// |
| tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1052 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1053 | /** |
| 1054 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 1055 | * default to disabled. |
| 1056 | */ |
| 1057 | enum StateBits { |
| 1058 | /** |
| 1059 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 1060 | */ |
| 1061 | kDither_StateBit = 0x01, |
| 1062 | /** |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1063 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 1064 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 1065 | * the 3D API. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1066 | */ |
| 1067 | kHWAntialias_StateBit = 0x02, |
| 1068 | /** |
| 1069 | * Draws will respect the clip, otherwise the clip is ignored. |
| 1070 | */ |
| 1071 | kClip_StateBit = 0x04, |
| 1072 | /** |
| 1073 | * Disables writing to the color buffer. Useful when performing stencil |
| 1074 | * operations. |
| 1075 | */ |
| 1076 | kNoColorWrites_StateBit = 0x08, |
| bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1077 | |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1078 | /** |
| 1079 | * Usually coverage is applied after color blending. The color is blended using the coeffs |
| 1080 | * specified by setBlendFunc(). The blended color is then combined with dst using coeffs |
| 1081 | * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In |
| 1082 | * this case there is no distinction between coverage and color and the caller needs direct |
| 1083 | * control over the blend coeffs. When set, there will be a single blend step controlled by |
| 1084 | * setBlendFunc() which will use coverage*color as the src color. |
| 1085 | */ |
| 1086 | kCoverageDrawing_StateBit = 0x10, |
| 1087 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1088 | // Users of the class may add additional bits to the vector |
| 1089 | kDummyStateBit, |
| 1090 | kLastPublicStateBit = kDummyStateBit-1, |
| 1091 | }; |
| 1092 | |
| 1093 | void resetStateFlags() { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1094 | fCommon.fFlagBits = 0; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * Enable render state settings. |
| 1099 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1100 | * @param stateBits bitfield of StateBits specifying the states to enable |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1101 | */ |
| 1102 | void enableState(uint32_t stateBits) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1103 | fCommon.fFlagBits |= stateBits; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Disable render state settings. |
| 1108 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1109 | * @param stateBits bitfield of StateBits specifying the states to disable |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1110 | */ |
| 1111 | void disableState(uint32_t stateBits) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1112 | fCommon.fFlagBits &= ~(stateBits); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 1115 | /** |
| 1116 | * Enable or disable stateBits based on a boolean. |
| 1117 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1118 | * @param stateBits bitfield of StateBits to enable or disable |
| bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 1119 | * @param enable if true enable stateBits, otherwise disable |
| 1120 | */ |
| 1121 | void setState(uint32_t stateBits, bool enable) { |
| 1122 | if (enable) { |
| 1123 | this->enableState(stateBits); |
| 1124 | } else { |
| 1125 | this->disableState(stateBits); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1129 | bool isDitherState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1130 | return 0 != (fCommon.fFlagBits & kDither_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | bool isHWAntialiasState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1134 | return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | bool isClipState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1138 | return 0 != (fCommon.fFlagBits & kClip_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | bool isColorWriteDisabled() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1142 | return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1145 | bool isCoverageDrawing() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1146 | return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit); |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1149 | bool isStateFlagEnabled(uint32_t stateBit) const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1150 | return 0 != (stateBit & fCommon.fFlagBits); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1153 | /// @} |
| 1154 | |
| 1155 | /////////////////////////////////////////////////////////////////////////// |
| 1156 | /// @name Face Culling |
| 1157 | //// |
| 1158 | |
| 1159 | enum DrawFace { |
| bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 1160 | kInvalid_DrawFace = -1, |
| 1161 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1162 | kBoth_DrawFace, |
| 1163 | kCCW_DrawFace, |
| 1164 | kCW_DrawFace, |
| 1165 | }; |
| 1166 | |
| 1167 | /** |
| 1168 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 1169 | * @param face the face(s) to draw. |
| 1170 | */ |
| 1171 | void setDrawFace(DrawFace face) { |
| bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 1172 | GrAssert(kInvalid_DrawFace != face); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1173 | fCommon.fDrawFace = face; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 1178 | * or both faces. |
| 1179 | * @return the current draw face(s). |
| 1180 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1181 | DrawFace getDrawFace() const { return fCommon.fDrawFace; } |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1182 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1183 | /// @} |
| 1184 | |
| 1185 | /////////////////////////////////////////////////////////////////////////// |
| tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1186 | |
| tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 1187 | bool isStageEnabled(int s) const { |
| 1188 | GrAssert((unsigned)s < kNumStages); |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1189 | return (NULL != fStages[s].getEffect()); |
| tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1192 | bool operator ==(const GrDrawState& s) const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1193 | if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) { |
| bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 1194 | return false; |
| 1195 | } |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 1196 | if (fVertexAttribs != s.fVertexAttribs) { |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1197 | return false; |
| 1198 | } |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1199 | for (int i = 0; i < kAttribIndexCount; ++i) { |
| reed@google.com | 67e7cde | 2013-03-20 17:47:16 +0000 | [diff] [blame^] | 1200 | if ((i == kPosition_AttribIndex || |
| 1201 | s.fCommon.fAttribBindings & kAttribIndexMasks[i]) && |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1202 | fAttribIndices[i] != s.fAttribIndices[i]) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1206 | for (int i = 0; i < kNumStages; i++) { |
| bsalomon@google.com | f2f8fc3 | 2012-07-18 18:25:07 +0000 | [diff] [blame] | 1207 | bool enabled = this->isStageEnabled(i); |
| 1208 | if (enabled != s.isStageEnabled(i)) { |
| 1209 | return false; |
| 1210 | } |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1211 | if (enabled && this->fStages[i] != s.fStages[i]) { |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | } |
| 1214 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1215 | return true; |
| 1216 | } |
| 1217 | bool operator !=(const GrDrawState& s) const { return !(*this == s); } |
| 1218 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1219 | GrDrawState& operator= (const GrDrawState& s) { |
| 1220 | this->setRenderTarget(s.fRenderTarget.get()); |
| 1221 | fCommon = s.fCommon; |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1222 | fVertexAttribs = s.fVertexAttribs; |
| 1223 | for (int i = 0; i < kAttribIndexCount; i++) { |
| 1224 | fAttribIndices[i] = s.fAttribIndices[i]; |
| 1225 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1226 | for (int i = 0; i < kNumStages; i++) { |
| tomhudson@google.com | e742bf0 | 2012-07-13 19:54:19 +0000 | [diff] [blame] | 1227 | if (s.isStageEnabled(i)) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1228 | this->fStages[i] = s.fStages[i]; |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1231 | return *this; |
| 1232 | } |
| 1233 | |
| 1234 | private: |
| bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 1235 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1236 | /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */ |
| 1237 | struct CommonState { |
| 1238 | // These fields are roughly sorted by decreasing likelihood of being different in op== |
| 1239 | GrColor fColor; |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1240 | GrAttribBindings fAttribBindings; |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1241 | SkMatrix fViewMatrix; |
| 1242 | GrBlendCoeff fSrcBlend; |
| 1243 | GrBlendCoeff fDstBlend; |
| 1244 | GrColor fBlendConstant; |
| 1245 | uint32_t fFlagBits; |
| 1246 | VertexEdgeType fVertexEdgeType; |
| 1247 | GrStencilSettings fStencilSettings; |
| 1248 | int fFirstCoverageStage; |
| 1249 | GrColor fCoverage; |
| 1250 | SkXfermode::Mode fColorFilterMode; |
| 1251 | GrColor fColorFilterColor; |
| 1252 | DrawFace fDrawFace; |
| 1253 | bool operator== (const CommonState& other) const { |
| 1254 | return fColor == other.fColor && |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1255 | fAttribBindings == other.fAttribBindings && |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1256 | fViewMatrix.cheapEqualTo(other.fViewMatrix) && |
| 1257 | fSrcBlend == other.fSrcBlend && |
| 1258 | fDstBlend == other.fDstBlend && |
| 1259 | fBlendConstant == other.fBlendConstant && |
| 1260 | fFlagBits == other.fFlagBits && |
| 1261 | fVertexEdgeType == other.fVertexEdgeType && |
| 1262 | fStencilSettings == other.fStencilSettings && |
| 1263 | fFirstCoverageStage == other.fFirstCoverageStage && |
| 1264 | fCoverage == other.fCoverage && |
| 1265 | fColorFilterMode == other.fColorFilterMode && |
| 1266 | fColorFilterColor == other.fColorFilterColor && |
| 1267 | fDrawFace == other.fDrawFace; |
| 1268 | } |
| 1269 | bool operator!= (const CommonState& other) const { return !(*this == other); } |
| 1270 | }; |
| bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 1271 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1272 | /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef. |
| 1273 | DeferredState must directly reference GrEffects, however. */ |
| 1274 | struct SavedEffectStage { |
| 1275 | SavedEffectStage() : fEffect(NULL) {} |
| 1276 | const GrEffect* fEffect; |
| 1277 | GrEffectStage::SavedCoordChange fCoordChange; |
| 1278 | }; |
| 1279 | |
| 1280 | public: |
| 1281 | /** |
| 1282 | * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource |
| 1283 | * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal |
| 1284 | * dispose mechanism returns them to the cache. This allows recycling resources through the |
| 1285 | * the cache while they are in a deferred draw queue. |
| 1286 | */ |
| 1287 | class DeferredState { |
| 1288 | public: |
| 1289 | DeferredState() : fRenderTarget(NULL) { |
| 1290 | GR_DEBUGCODE(fInitialized = false;) |
| 1291 | } |
| 1292 | // TODO: Remove this when DeferredState no longer holds a ref to the RT |
| 1293 | ~DeferredState() { SkSafeUnref(fRenderTarget); } |
| 1294 | |
| 1295 | void saveFrom(const GrDrawState& drawState) { |
| 1296 | fCommon = drawState.fCommon; |
| 1297 | // TODO: Here we will copy the GrRenderTarget pointer without taking a ref. |
| 1298 | fRenderTarget = drawState.fRenderTarget.get(); |
| 1299 | SkSafeRef(fRenderTarget); |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1300 | fVertexAttribs = drawState.fVertexAttribs; |
| 1301 | for (int i = 0; i < kAttribIndexCount; i++) { |
| 1302 | fAttribIndices[i] = drawState.fAttribIndices[i]; |
| 1303 | } |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1304 | // Here we ref the effects directly rather than the effect-refs. TODO: When the effect- |
| 1305 | // ref gets fully unref'ed it will cause the underlying effect to unref its resources |
| 1306 | // and recycle them to the cache (if no one else is holding a ref to the resources). |
| 1307 | for (int i = 0; i < kNumStages; ++i) { |
| 1308 | fStages[i].saveFrom(drawState.fStages[i]); |
| 1309 | } |
| 1310 | GR_DEBUGCODE(fInitialized = true;) |
| 1311 | } |
| 1312 | |
| 1313 | void restoreTo(GrDrawState* drawState) { |
| 1314 | GrAssert(fInitialized); |
| 1315 | drawState->fCommon = fCommon; |
| 1316 | drawState->setRenderTarget(fRenderTarget); |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1317 | drawState->fVertexAttribs = fVertexAttribs; |
| 1318 | for (int i = 0; i < kAttribIndexCount; i++) { |
| 1319 | drawState->fAttribIndices[i] = fAttribIndices[i]; |
| 1320 | } |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1321 | for (int i = 0; i < kNumStages; ++i) { |
| 1322 | fStages[i].restoreTo(&drawState->fStages[i]); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | bool isEqual(const GrDrawState& state) const { |
| 1327 | if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) { |
| 1328 | return false; |
| 1329 | } |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1330 | for (int i = 0; i < kAttribIndexCount; ++i) { |
| skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 1331 | if ((i == kPosition_AttribIndex || |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1332 | state.fCommon.fAttribBindings & kAttribIndexMasks[i]) && |
| 1333 | fAttribIndices[i] != state.fAttribIndices[i]) { |
| 1334 | return false; |
| 1335 | } |
| 1336 | } |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 1337 | if (fVertexAttribs != state.fVertexAttribs) { |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1338 | return false; |
| 1339 | } |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1340 | for (int i = 0; i < kNumStages; ++i) { |
| bsalomon@google.com | dcd69bf | 2013-01-24 18:28:51 +0000 | [diff] [blame] | 1341 | if (!fStages[i].isEqual(state.fStages[i])) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1342 | return false; |
| 1343 | } |
| 1344 | } |
| 1345 | return true; |
| 1346 | } |
| 1347 | |
| 1348 | private: |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1349 | GrRenderTarget* fRenderTarget; |
| 1350 | CommonState fCommon; |
| 1351 | int fAttribIndices[kAttribIndexCount]; |
| 1352 | GrVertexAttribArray<kVertexAttribCnt> fVertexAttribs; |
| 1353 | GrEffectStage::DeferredStage fStages[kNumStages]; |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1354 | |
| 1355 | GR_DEBUGCODE(bool fInitialized;) |
| 1356 | }; |
| 1357 | |
| 1358 | private: |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1359 | // helper array to let us check the current bindings so we know what bound attrib indices |
| 1360 | // we care about |
| 1361 | static const GrAttribBindings kAttribIndexMasks[kAttribIndexCount]; |
| 1362 | |
| 1363 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 1364 | CommonState fCommon; |
| 1365 | int fAttribIndices[kAttribIndexCount]; |
| 1366 | GrVertexAttribArray<kVertexAttribCnt> fVertexAttribs; |
| 1367 | GrEffectStage fStages[kNumStages]; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1368 | |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 1369 | typedef GrRefCnt INHERITED; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1370 | }; |
| 1371 | |
| bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 1372 | GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
| 1373 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1374 | #endif |