| 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 | |
| bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 26 | class GrDrawState : public GrRefCnt { |
| bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 27 | public: |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 28 | SK_DECLARE_INST_COUNT(GrDrawState) |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 29 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 30 | /** |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 31 | * Total number of effect stages. Each stage can host a GrEffect. A stage is enabled if it has a |
| 32 | * GrEffect. The effect produces an output color in the fragment shader. It's inputs are the |
| 33 | * output from the previous enabled stage and a position. The position is either derived from |
| 34 | * the interpolated vertex positions or explicit per-vertex coords, depending upon the |
| 35 | * GrVertexLayout used to draw. |
| robertphillips@google.com | bf5cad4 | 2012-05-10 12:40:40 +0000 | [diff] [blame] | 36 | * |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 37 | * The stages are divided into two sets, color-computing and coverage-computing. The final color |
| 38 | * stage produces the final pixel color. The coverage-computing stages function exactly as the |
| 39 | * color-computing but the output of the final coverage stage is treated as a fractional pixel |
| 40 | * coverage rather than as input to the src/dst color blend step. |
| 41 | * |
| 42 | * The input color to the first enabled color-stage is either the constant color or interpolated |
| 43 | * per-vertex colors, depending upon GrVertexLayout. The input to the first coverage stage is |
| 44 | * either a constant coverage (usually full-coverage), interpolated per-vertex coverage, or |
| 45 | * edge-AA computed coverage. (This latter is going away as soon as it can be rewritten as a |
| 46 | * GrEffect). |
| 47 | * |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 48 | * See the documentation of kCoverageDrawing_StateBit for information about disabling the |
| 49 | * the color / coverage distinction. |
| 50 | * |
| bsalomon@google.com | 1322134 | 2012-10-26 13:41:59 +0000 | [diff] [blame] | 51 | * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied from the client's |
| 52 | * GrPaint. Stages GrPaint::kTotalStages through kNumStages-2 are earmarked for use by |
| 53 | * GrTextContext and GrPathRenderer-derived classes. kNumStages-1 is earmarked for clipping |
| bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 54 | * by GrClipMaskManager. |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 55 | */ |
| 56 | enum { |
| twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 57 | kNumStages = 5, |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 58 | kMaxTexCoords = kNumStages |
| 59 | }; |
| 60 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 61 | GrDrawState() { |
| reed@google.com | 7584719 | 2013-01-28 20:53:22 +0000 | [diff] [blame] | 62 | #if GR_DEBUG |
| 63 | VertexLayoutUnitTest(); |
| 64 | #endif |
| bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 65 | this->reset(); |
| 66 | } |
| bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 67 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 68 | GrDrawState(const GrDrawState& state) { |
| bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 69 | *this = state; |
| 70 | } |
| 71 | |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 72 | virtual ~GrDrawState() { |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 73 | this->disableStages(); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 76 | /** |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 77 | * Resets to the default state. |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 78 | * GrEffects will be removed from all stages. |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | */ |
| bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 80 | void reset() { |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 81 | |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 82 | this->disableStages(); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 83 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 84 | fRenderTarget.reset(NULL); |
| 85 | |
| 86 | fCommon.fColor = 0xffffffff; |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 87 | fCommon.fVertexLayout = kDefault_VertexLayout; |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 88 | fCommon.fViewMatrix.reset(); |
| 89 | fCommon.fSrcBlend = kOne_GrBlendCoeff; |
| 90 | fCommon.fDstBlend = kZero_GrBlendCoeff; |
| 91 | fCommon.fBlendConstant = 0x0; |
| 92 | fCommon.fFlagBits = 0x0; |
| 93 | fCommon.fVertexEdgeType = kHairLine_EdgeType; |
| 94 | fCommon.fStencilSettings.setDisabled(); |
| 95 | fCommon.fFirstCoverageStage = kNumStages; |
| 96 | fCommon.fCoverage = 0xffffffff; |
| 97 | fCommon.fColorFilterMode = SkXfermode::kDst_Mode; |
| 98 | fCommon.fColorFilterColor = 0x0; |
| 99 | fCommon.fDrawFace = kBoth_DrawFace; |
| bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 104 | * encompasses more than GrPaint. Aspects of GrDrawState that have no |
| bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 105 | * GrPaint equivalents are not modified. GrPaint has fewer stages than |
| 106 | * GrDrawState. The extra GrDrawState stages are disabled. |
| 107 | */ |
| 108 | void setFromPaint(const GrPaint& paint); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 109 | |
| 110 | /////////////////////////////////////////////////////////////////////////// |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 111 | /// @name Vertex Layout |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 112 | //// |
| 113 | |
| 114 | /** |
| 115 | * The format of vertices is represented as a bitfield of flags. |
| 116 | * Flags that indicate the layout of vertex data. Vertices always contain |
| 117 | * positions and may also contain up to GrDrawState::kMaxTexCoords sets |
| 118 | * of 2D texture coordinates, per-vertex colors, and per-vertex coverage. |
| 119 | * Each stage can |
| 120 | * use any of the texture coordinates as its input texture coordinates or it |
| 121 | * may use the positions as texture coordinates. |
| 122 | * |
| 123 | * If no texture coordinates are specified for a stage then the stage is |
| 124 | * disabled. |
| 125 | * |
| 126 | * Only one type of texture coord can be specified per stage. For |
| 127 | * example StageTexCoordVertexLayoutBit(0, 2) and |
| 128 | * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified. |
| 129 | * |
| 130 | * The order in memory is always (position, texture coord 0, ..., color, |
| 131 | * coverage) with any unused fields omitted. Note that this means that if |
| 132 | * only texture coordinates 1 is referenced then there is no texture |
| 133 | * coordinates 0 and the order would be (position, texture coordinate 1 |
| 134 | * [, color][, coverage]). |
| 135 | */ |
| 136 | |
| 137 | /** |
| 138 | * Generates a bit indicating that a texture stage uses texture coordinates |
| 139 | * |
| 140 | * @param stageIdx the stage that will use texture coordinates. |
| 141 | * @param texCoordIdx the index of the texture coordinates to use |
| 142 | * |
| 143 | * @return the bit to add to a GrVertexLayout bitfield. |
| 144 | */ |
| 145 | static int StageTexCoordVertexLayoutBit(int stageIdx, int texCoordIdx) { |
| 146 | GrAssert(stageIdx < kNumStages); |
| 147 | GrAssert(texCoordIdx < kMaxTexCoords); |
| 148 | return 1 << (stageIdx + (texCoordIdx * kNumStages)); |
| 149 | } |
| 150 | |
| 151 | static bool StageUsesTexCoords(GrVertexLayout layout, int stageIdx); |
| skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 152 | |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 153 | private: |
| 154 | // non-stage bits start at this index. |
| 155 | static const int STAGE_BIT_CNT = kNumStages * kMaxTexCoords; |
| 156 | public: |
| 157 | |
| 158 | /** |
| 159 | * Additional Bits that can be specified in GrVertexLayout. |
| 160 | */ |
| 161 | enum VertexLayoutBits { |
| 162 | /* vertices have colors (GrColor) */ |
| 163 | kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0), |
| 164 | /* vertices have coverage (GrColor) |
| 165 | */ |
| 166 | kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1), |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 167 | /* Each vertex specificies an edge. Distance to the edge is used to |
| 168 | * compute a coverage. See GrDrawState::setVertexEdgeType(). |
| 169 | */ |
| bsalomon@google.com | 8598328 | 2013-02-07 22:00:29 +0000 | [diff] [blame^] | 170 | kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2), |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 171 | // for below assert |
| 172 | kDummyVertexLayoutBit, |
| 173 | kHighVertexLayoutBit = kDummyVertexLayoutBit - 1 |
| 174 | }; |
| 175 | // make sure we haven't exceeded the number of bits in GrVertexLayout. |
| 176 | GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout))); |
| 177 | |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 178 | enum VertexLayout { |
| 179 | kDefault_VertexLayout = 0 |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Sets vertex layout for next draw. |
| 184 | * |
| 185 | * @param layout the vertex layout to set. |
| 186 | */ |
| 187 | void setVertexLayout(GrVertexLayout layout) { fCommon.fVertexLayout = layout; } |
| 188 | |
| 189 | GrVertexLayout getVertexLayout() const { return fCommon.fVertexLayout; } |
| 190 | size_t getVertexSize() const { return VertexSize(fCommon.fVertexLayout); } |
| 191 | |
| 192 | |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 193 | //////////////////////////////////////////////////////////////////////////// |
| 194 | // Helpers for picking apart vertex layouts |
| 195 | |
| 196 | /** |
| 197 | * Helper function to compute the size of a vertex from a vertex layout |
| 198 | * @return size of a single vertex. |
| 199 | */ |
| 200 | static size_t VertexSize(GrVertexLayout vertexLayout); |
| 201 | |
| 202 | /** |
| 203 | * Helper function for determining the index of texture coordinates that |
| 204 | * is input for a texture stage. Note that a stage may instead use positions |
| 205 | * as texture coordinates, in which case the result of the function is |
| 206 | * indistinguishable from the case when the stage is disabled. |
| 207 | * |
| 208 | * @param stageIdx the stage to query |
| 209 | * @param vertexLayout layout to query |
| 210 | * |
| 211 | * @return the texture coordinate index or -1 if the stage doesn't use |
| 212 | * separate (non-position) texture coordinates. |
| 213 | */ |
| 214 | static int VertexTexCoordsForStage(int stageIdx, GrVertexLayout vertexLayout); |
| 215 | |
| 216 | /** |
| 217 | * Helper function to compute the offset of texture coordinates in a vertex |
| 218 | * @return offset of texture coordinates in vertex layout or -1 if the |
| 219 | * layout has no texture coordinates. Will be 0 if positions are |
| 220 | * used as texture coordinates for the stage. |
| 221 | */ |
| 222 | static int VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout); |
| 223 | |
| 224 | /** |
| 225 | * Helper function to compute the offset of the color in a vertex |
| 226 | * @return offset of color in vertex layout or -1 if the |
| 227 | * layout has no color. |
| 228 | */ |
| 229 | static int VertexColorOffset(GrVertexLayout vertexLayout); |
| 230 | |
| 231 | /** |
| 232 | * Helper function to compute the offset of the coverage in a vertex |
| 233 | * @return offset of coverage in vertex layout or -1 if the |
| 234 | * layout has no coverage. |
| 235 | */ |
| 236 | static int VertexCoverageOffset(GrVertexLayout vertexLayout); |
| 237 | |
| 238 | /** |
| 239 | * Helper function to compute the offset of the edge pts in a vertex |
| 240 | * @return offset of edge in vertex layout or -1 if the |
| 241 | * layout has no edge. |
| 242 | */ |
| 243 | static int VertexEdgeOffset(GrVertexLayout vertexLayout); |
| 244 | |
| 245 | /** |
| 246 | * Helper function to determine if vertex layout contains explicit texture |
| 247 | * coordinates of some index. |
| 248 | * |
| 249 | * @param coordIndex the tex coord index to query |
| 250 | * @param vertexLayout layout to query |
| 251 | * |
| 252 | * @return true if vertex specifies texture coordinates for the index, |
| 253 | * false otherwise. |
| 254 | */ |
| 255 | static bool VertexUsesTexCoordIdx(int coordIndex, |
| 256 | GrVertexLayout vertexLayout); |
| 257 | |
| 258 | /** |
| 259 | * Helper function to compute the size of each vertex and the offsets of |
| 260 | * texture coordinates and color. Determines tex coord offsets by tex coord |
| 261 | * index rather than by stage. (Each stage can be mapped to any t.c. index |
| 262 | * by StageTexCoordVertexLayoutBit.) |
| 263 | * |
| 264 | * @param vertexLayout the layout to query |
| 265 | * @param texCoordOffsetsByIdx after return it is the offset of each |
| 266 | * tex coord index in the vertex or -1 if |
| 267 | * index isn't used. (optional) |
| 268 | * @param colorOffset after return it is the offset of the |
| 269 | * color field in each vertex, or -1 if |
| 270 | * there aren't per-vertex colors. (optional) |
| 271 | * @param coverageOffset after return it is the offset of the |
| 272 | * coverage field in each vertex, or -1 if |
| 273 | * there aren't per-vertex coeverages. |
| 274 | * (optional) |
| 275 | * @param edgeOffset after return it is the offset of the |
| 276 | * edge eq field in each vertex, or -1 if |
| 277 | * there aren't per-vertex edge equations. |
| 278 | * (optional) |
| 279 | * @return size of a single vertex |
| 280 | */ |
| 281 | static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, |
| 282 | int texCoordOffsetsByIdx[kMaxTexCoords], |
| 283 | int *colorOffset, |
| 284 | int *coverageOffset, |
| 285 | int* edgeOffset); |
| 286 | |
| 287 | /** |
| 288 | * Helper function to compute the size of each vertex and the offsets of |
| 289 | * texture coordinates and color. Determines tex coord offsets by stage |
| 290 | * rather than by index. (Each stage can be mapped to any t.c. index |
| 291 | * by StageTexCoordVertexLayoutBit.) If a stage uses positions for |
| 292 | * tex coords then that stage's offset will be 0 (positions are always at 0). |
| 293 | * |
| 294 | * @param vertexLayout the layout to query |
| 295 | * @param texCoordOffsetsByStage after return it is the offset of each |
| 296 | * tex coord index in the vertex or -1 if |
| 297 | * index isn't used. (optional) |
| 298 | * @param colorOffset after return it is the offset of the |
| 299 | * color field in each vertex, or -1 if |
| 300 | * there aren't per-vertex colors. |
| 301 | * (optional) |
| 302 | * @param coverageOffset after return it is the offset of the |
| 303 | * coverage field in each vertex, or -1 if |
| 304 | * there aren't per-vertex coeverages. |
| 305 | * (optional) |
| 306 | * @param edgeOffset after return it is the offset of the |
| 307 | * edge eq field in each vertex, or -1 if |
| 308 | * there aren't per-vertex edge equations. |
| 309 | * (optional) |
| 310 | * @return size of a single vertex |
| 311 | */ |
| 312 | static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, |
| 313 | int texCoordOffsetsByStage[kNumStages], |
| 314 | int* colorOffset, |
| 315 | int* coverageOffset, |
| 316 | int* edgeOffset); |
| 317 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 318 | /** |
| 319 | * Determines whether src alpha is guaranteed to be one for all src pixels |
| 320 | */ |
| 321 | bool srcAlphaWillBeOne(GrVertexLayout) const; |
| 322 | |
| 323 | /** |
| 324 | * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw. |
| 325 | */ |
| 326 | bool hasSolidCoverage(GrVertexLayout) const; |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 327 | |
| 328 | /** |
| 329 | * Accessing positions, texture coords, or colors, of a vertex within an |
| 330 | * array is a hassle involving casts and simple math. These helpers exist |
| 331 | * to keep GrDrawTarget clients' code a bit nicer looking. |
| 332 | */ |
| 333 | |
| 334 | /** |
| 335 | * Gets a pointer to a GrPoint of a vertex's position or texture |
| 336 | * coordinate. |
| 337 | * @param vertices the vetex array |
| 338 | * @param vertexIndex the index of the vertex in the array |
| 339 | * @param vertexSize the size of each vertex in the array |
| 340 | * @param offset the offset in bytes of the vertex component. |
| 341 | * Defaults to zero (corresponding to vertex position) |
| 342 | * @return pointer to the vertex component as a GrPoint |
| 343 | */ |
| 344 | static GrPoint* GetVertexPoint(void* vertices, |
| 345 | int vertexIndex, |
| 346 | int vertexSize, |
| 347 | int offset = 0) { |
| 348 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 349 | return GrTCast<GrPoint*>(start + offset + |
| 350 | vertexIndex * vertexSize); |
| 351 | } |
| 352 | static const GrPoint* GetVertexPoint(const void* vertices, |
| 353 | int vertexIndex, |
| 354 | int vertexSize, |
| 355 | int offset = 0) { |
| 356 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 357 | return GrTCast<const GrPoint*>(start + offset + |
| 358 | vertexIndex * vertexSize); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Gets a pointer to a GrColor inside a vertex within a vertex array. |
| 363 | * @param vertices the vetex array |
| 364 | * @param vertexIndex the index of the vertex in the array |
| 365 | * @param vertexSize the size of each vertex in the array |
| 366 | * @param offset the offset in bytes of the vertex color |
| 367 | * @return pointer to the vertex component as a GrColor |
| 368 | */ |
| 369 | static GrColor* GetVertexColor(void* vertices, |
| 370 | int vertexIndex, |
| 371 | int vertexSize, |
| 372 | int offset) { |
| 373 | intptr_t start = GrTCast<intptr_t>(vertices); |
| 374 | return GrTCast<GrColor*>(start + offset + |
| 375 | vertexIndex * vertexSize); |
| 376 | } |
| 377 | static const GrColor* GetVertexColor(const void* vertices, |
| 378 | int vertexIndex, |
| 379 | int vertexSize, |
| 380 | int offset) { |
| 381 | const intptr_t start = GrTCast<intptr_t>(vertices); |
| 382 | return GrTCast<const GrColor*>(start + offset + |
| 383 | vertexIndex * vertexSize); |
| 384 | } |
| 385 | |
| 386 | static void VertexLayoutUnitTest(); |
| 387 | |
| 388 | /// @} |
| 389 | |
| 390 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 391 | /// @name Color |
| 392 | //// |
| 393 | |
| 394 | /** |
| 395 | * Sets color for next draw to a premultiplied-alpha color. |
| 396 | * |
| 397 | * @param color the color to set. |
| 398 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 399 | void setColor(GrColor color) { fCommon.fColor = color; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 400 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 401 | GrColor getColor() const { return fCommon.fColor; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * Sets the color to be used for the next draw to be |
| 405 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 406 | * |
| 407 | * @param alpha The alpha value to set as the color. |
| 408 | */ |
| 409 | void setAlpha(uint8_t a) { |
| 410 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Add a color filter that can be represented by a color and a mode. Applied |
| 415 | * after color-computing texture stages. |
| 416 | */ |
| 417 | void setColorFilter(GrColor c, SkXfermode::Mode mode) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 418 | fCommon.fColorFilterColor = c; |
| 419 | fCommon.fColorFilterMode = mode; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 422 | GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; } |
| 423 | SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 424 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 425 | /** |
| 426 | * Constructor sets the color to be 'color' which is undone by the destructor. |
| 427 | */ |
| 428 | class AutoColorRestore : public ::GrNoncopyable { |
| 429 | public: |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 430 | AutoColorRestore() : fDrawState(NULL) {} |
| 431 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 432 | AutoColorRestore(GrDrawState* drawState, GrColor color) { |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 433 | fDrawState = NULL; |
| 434 | this->set(drawState, color); |
| 435 | } |
| 436 | |
| 437 | void reset() { |
| 438 | if (NULL != fDrawState) { |
| 439 | fDrawState->setColor(fOldColor); |
| 440 | fDrawState = NULL; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void set(GrDrawState* drawState, GrColor color) { |
| 445 | this->reset(); |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 446 | fDrawState = drawState; |
| 447 | fOldColor = fDrawState->getColor(); |
| 448 | fDrawState->setColor(color); |
| 449 | } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 450 | |
| 451 | ~AutoColorRestore() { this->reset(); } |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 452 | private: |
| 453 | GrDrawState* fDrawState; |
| 454 | GrColor fOldColor; |
| 455 | }; |
| 456 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 457 | /// @} |
| 458 | |
| 459 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 460 | /// @name Coverage |
| 461 | //// |
| 462 | |
| 463 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 464 | * 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] | 465 | * initial value (after construction or reset()) is 0xff. The constant |
| 466 | * coverage is ignored when per-vertex coverage is provided. |
| 467 | */ |
| 468 | void setCoverage(uint8_t coverage) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 469 | fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Version of above that specifies 4 channel per-vertex color. The value |
| 474 | * should be premultiplied. |
| 475 | */ |
| 476 | void setCoverage4(GrColor coverage) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 477 | fCommon.fCoverage = coverage; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | GrColor getCoverage() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 481 | return fCommon.fCoverage; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /// @} |
| 485 | |
| 486 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 487 | /// @name Effect Stages |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 488 | //// |
| 489 | |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 490 | const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) { |
| 491 | fStages[stageIdx].setEffect(effect); |
| 492 | return effect; |
| 493 | } |
| 494 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 495 | /** |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 496 | * Creates a GrSimpleTextureEffect. |
| tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 497 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 498 | void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 499 | GrAssert(!this->getStage(stageIdx).getEffect()); |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 500 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 501 | this->setEffect(stageIdx, effect)->unref(); |
| bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 502 | } |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 503 | void createTextureEffect(int stageIdx, |
| 504 | GrTexture* texture, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 505 | const SkMatrix& matrix, |
| bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 506 | const GrTextureParams& params) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 507 | GrAssert(!this->getStage(stageIdx).getEffect()); |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 508 | GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params); |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 509 | this->setEffect(stageIdx, effect)->unref(); |
| bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 512 | bool stagesDisabled() { |
| 513 | for (int i = 0; i < kNumStages; ++i) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 514 | if (NULL != fStages[i].getEffect()) { |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 515 | return false; |
| 516 | } |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 517 | } |
| tomhudson@google.com | 3eee8fb | 2012-06-25 12:30:34 +0000 | [diff] [blame] | 518 | return true; |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 519 | } |
| tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 520 | |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 521 | void disableStage(int stageIdx) { this->setEffect(stageIdx, NULL); } |
| tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 522 | |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 523 | /** |
| bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 524 | * Release all the GrEffects referred to by this draw state. |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 525 | */ |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 526 | void disableStages() { |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 527 | for (int i = 0; i < kNumStages; ++i) { |
| tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 528 | this->disableStage(i); |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 532 | class AutoStageDisable : public ::GrNoncopyable { |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 533 | public: |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 534 | AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {} |
| 535 | ~AutoStageDisable() { |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 536 | if (NULL != fDrawState) { |
| tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 537 | fDrawState->disableStages(); |
| robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | private: |
| 541 | GrDrawState* fDrawState; |
| 542 | }; |
| 543 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 544 | /** |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 545 | * Returns the current stage by index. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 546 | */ |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 547 | const GrEffectStage& getStage(int stageIdx) const { |
| 548 | GrAssert((unsigned)stageIdx < kNumStages); |
| 549 | return fStages[stageIdx]; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 553 | * Called when the source coord system is changing. preConcat gives the transformation from the |
| 554 | * old coord system to the new coord system. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 555 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 556 | void preConcatStageMatrices(const SkMatrix& preConcat) { |
| bsalomon@google.com | cabe00e | 2013-01-28 16:46:55 +0000 | [diff] [blame] | 557 | this->preConcatStageMatrices(~0U, preConcat); |
| 558 | } |
| 559 | /** |
| 560 | * Version of above that applies the update matrix selectively to stages via a mask. |
| 561 | */ |
| 562 | void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 563 | for (int i = 0; i < kNumStages; ++i) { |
| bsalomon@google.com | cabe00e | 2013-01-28 16:46:55 +0000 | [diff] [blame] | 564 | if (((1 << i) & stageMask) && this->isStageEnabled(i)) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 565 | fStages[i].preConcatCoordChange(preConcat); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 570 | /** |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 571 | * Called when the source coord system is changing. preConcatInverse is the inverse of the |
| 572 | * transformation from the old coord system to the new coord system. Returns false if the matrix |
| 573 | * cannot be inverted. |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 574 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 575 | bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) { |
| 576 | SkMatrix inv; |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 577 | bool computed = false; |
| 578 | for (int i = 0; i < kNumStages; ++i) { |
| 579 | if (this->isStageEnabled(i)) { |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 580 | if (!computed && !preConcatInverse.invert(&inv)) { |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } else { |
| 583 | computed = true; |
| 584 | } |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 585 | fStages[i].preConcatCoordChange(preConcatInverse); |
| bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | return true; |
| 589 | } |
| 590 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 591 | /// @} |
| 592 | |
| 593 | /////////////////////////////////////////////////////////////////////////// |
| 594 | /// @name Coverage / Color Stages |
| 595 | //// |
| 596 | |
| 597 | /** |
| 598 | * A common pattern is to compute a color with the initial stages and then |
| 599 | * 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] | 600 | * filters, glyph mask, etc). Color-filters, xfermodes, etc should be |
| 601 | * computed based on the pre-coverage-modulated color. The division of |
| 602 | * stages between color-computing and coverage-computing is specified by |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 603 | * this method. Initially this is kNumStages (all stages |
| 604 | * are color-computing). |
| 605 | */ |
| 606 | void setFirstCoverageStage(int firstCoverageStage) { |
| 607 | GrAssert((unsigned)firstCoverageStage <= kNumStages); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 608 | fCommon.fFirstCoverageStage = firstCoverageStage; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Gets the index of the first coverage-computing stage. |
| 613 | */ |
| 614 | int getFirstCoverageStage() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 615 | return fCommon.fFirstCoverageStage; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | ///@} |
| 619 | |
| 620 | /////////////////////////////////////////////////////////////////////////// |
| 621 | /// @name Blending |
| 622 | //// |
| 623 | |
| 624 | /** |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 625 | * Sets the blending function coefficients. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 626 | * |
| 627 | * The blend function will be: |
| 628 | * D' = sat(S*srcCoef + D*dstCoef) |
| 629 | * |
| 630 | * where D is the existing destination color, S is the incoming source |
| 631 | * color, and D' is the new destination color that will be written. sat() |
| 632 | * is the saturation function. |
| 633 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 634 | * @param srcCoef coefficient applied to the src color. |
| 635 | * @param dstCoef coefficient applied to the dst color. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 636 | */ |
| 637 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 638 | fCommon.fSrcBlend = srcCoeff; |
| 639 | fCommon.fDstBlend = dstCoeff; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 640 | #if GR_DEBUG |
| 641 | switch (dstCoeff) { |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 642 | case kDC_GrBlendCoeff: |
| 643 | case kIDC_GrBlendCoeff: |
| 644 | case kDA_GrBlendCoeff: |
| 645 | case kIDA_GrBlendCoeff: |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 646 | GrPrintf("Unexpected dst blend coeff. Won't work correctly with" |
| 647 | "coverage stages.\n"); |
| 648 | break; |
| 649 | default: |
| 650 | break; |
| 651 | } |
| 652 | switch (srcCoeff) { |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 653 | case kSC_GrBlendCoeff: |
| 654 | case kISC_GrBlendCoeff: |
| 655 | case kSA_GrBlendCoeff: |
| 656 | case kISA_GrBlendCoeff: |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 657 | GrPrintf("Unexpected src blend coeff. Won't work correctly with" |
| 658 | "coverage stages.\n"); |
| 659 | break; |
| 660 | default: |
| 661 | break; |
| 662 | } |
| 663 | #endif |
| 664 | } |
| 665 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 666 | GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; } |
| 667 | GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 668 | |
| 669 | void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff, |
| 670 | GrBlendCoeff* dstBlendCoeff) const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 671 | *srcBlendCoeff = fCommon.fSrcBlend; |
| 672 | *dstBlendCoeff = fCommon.fDstBlend; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Sets the blending function constant referenced by the following blending |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 677 | * coefficients: |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 678 | * kConstC_GrBlendCoeff |
| 679 | * kIConstC_GrBlendCoeff |
| 680 | * kConstA_GrBlendCoeff |
| 681 | * kIConstA_GrBlendCoeff |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 682 | * |
| 683 | * @param constant the constant to set |
| 684 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 685 | void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 686 | |
| 687 | /** |
| 688 | * Retrieves the last value set by setBlendConstant() |
| 689 | * @return the blending constant value |
| 690 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 691 | GrColor getBlendConstant() const { return fCommon.fBlendConstant; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 692 | |
| 693 | /// @} |
| 694 | |
| 695 | /////////////////////////////////////////////////////////////////////////// |
| 696 | /// @name View Matrix |
| 697 | //// |
| 698 | |
| 699 | /** |
| robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 700 | * Sets the matrix applied to vertex positions. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 701 | * |
| 702 | * In the post-view-matrix space the rectangle [0,w]x[0,h] |
| 703 | * 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] | 704 | * the render-target.) |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 705 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 706 | void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 707 | |
| 708 | /** |
| 709 | * Gets a writable pointer to the view matrix. |
| 710 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 711 | SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 712 | |
| 713 | /** |
| 714 | * Multiplies the current view matrix by a matrix |
| 715 | * |
| 716 | * After this call V' = V*m where V is the old view matrix, |
| 717 | * m is the parameter to this function, and V' is the new view matrix. |
| 718 | * (We consider positions to be column vectors so position vector p is |
| 719 | * transformed by matrix X as p' = X*p.) |
| 720 | * |
| 721 | * @param m the matrix used to modify the view matrix. |
| 722 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 723 | void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 724 | |
| 725 | /** |
| 726 | * Multiplies the current view matrix by a matrix |
| 727 | * |
| 728 | * After this call V' = m*V where V is the old view matrix, |
| 729 | * m is the parameter to this function, and V' is the new view matrix. |
| 730 | * (We consider positions to be column vectors so position vector p is |
| 731 | * transformed by matrix X as p' = X*p.) |
| 732 | * |
| 733 | * @param m the matrix used to modify the view matrix. |
| 734 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 735 | void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 736 | |
| 737 | /** |
| 738 | * Retrieves the current view matrix |
| 739 | * @return the current view matrix. |
| 740 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 741 | const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 742 | |
| 743 | /** |
| 744 | * Retrieves the inverse of the current view matrix. |
| 745 | * |
| 746 | * If the current view matrix is invertible, return true, and if matrix |
| 747 | * is non-null, copy the inverse into it. If the current view matrix is |
| 748 | * non-invertible, return false and ignore the matrix parameter. |
| 749 | * |
| 750 | * @param matrix if not null, will receive a copy of the current inverse. |
| 751 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 752 | bool getViewInverse(SkMatrix* matrix) const { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 753 | // TODO: determine whether we really need to leave matrix unmodified |
| 754 | // at call sites when inversion fails. |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 755 | SkMatrix inverse; |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 756 | if (fCommon.fViewMatrix.invert(&inverse)) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 757 | if (matrix) { |
| 758 | *matrix = inverse; |
| 759 | } |
| 760 | return true; |
| 761 | } |
| 762 | return false; |
| 763 | } |
| 764 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 765 | //////////////////////////////////////////////////////////////////////////// |
| 766 | |
| 767 | /** |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 768 | * 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] | 769 | * Effect matrices are automatically adjusted to compensate. |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 770 | */ |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 771 | class AutoViewMatrixRestore : public ::GrNoncopyable { |
| 772 | public: |
| 773 | AutoViewMatrixRestore() : fDrawState(NULL) {} |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 774 | |
| 775 | AutoViewMatrixRestore(GrDrawState* ds, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 776 | const SkMatrix& preconcatMatrix, |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 777 | uint32_t explicitCoordStageMask = 0) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 778 | fDrawState = NULL; |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 779 | this->set(ds, preconcatMatrix, explicitCoordStageMask); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 780 | } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 781 | |
| 782 | ~AutoViewMatrixRestore() { this->restore(); } |
| 783 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 784 | /** |
| 785 | * Can be called prior to destructor to restore the original matrix. |
| 786 | */ |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 787 | void restore(); |
| skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 788 | |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 789 | void set(GrDrawState* drawState, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 790 | const SkMatrix& preconcatMatrix, |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 791 | uint32_t explicitCoordStageMask = 0); |
| 792 | |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 793 | bool isSet() const { return NULL != fDrawState; } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 794 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 795 | private: |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 796 | GrDrawState* fDrawState; |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 797 | SkMatrix fViewMatrix; |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 798 | GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages]; |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 799 | uint32_t fRestoreMask; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 800 | }; |
| 801 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 802 | //////////////////////////////////////////////////////////////////////////// |
| 803 | |
| 804 | /** |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 805 | * This sets the view matrix to identity and adjusts stage matrices to compensate. The |
| 806 | * destructor undoes the changes, restoring the view matrix that was set before the |
| 807 | * constructor. It is similar to passing the inverse of the current view matrix to |
| 808 | * AutoViewMatrixRestore, but lazily computes the inverse only if necessary. |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 809 | */ |
| 810 | class AutoDeviceCoordDraw : ::GrNoncopyable { |
| 811 | public: |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 812 | AutoDeviceCoordDraw() : fDrawState(NULL) {} |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 813 | /** |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 814 | * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to |
| 815 | * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used |
| 816 | * to specify such stages. |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 817 | */ |
| 818 | AutoDeviceCoordDraw(GrDrawState* drawState, |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 819 | uint32_t explicitCoordStageMask = 0) { |
| 820 | fDrawState = NULL; |
| 821 | this->set(drawState, explicitCoordStageMask); |
| 822 | } |
| 823 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 824 | ~AutoDeviceCoordDraw() { this->restore(); } |
| 825 | |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 826 | bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0); |
| 827 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 828 | /** |
| 829 | * Returns true if this object was successfully initialized on to a GrDrawState. It may |
| 830 | * return false because a non-default constructor or set() were never called or because |
| 831 | * the view matrix was not invertible. |
| 832 | */ |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 833 | bool succeeded() const { return NULL != fDrawState; } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 834 | |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 835 | /** |
| 836 | * Returns the matrix that was set previously set on the drawState. This is only valid |
| 837 | * if succeeded returns true. |
| 838 | */ |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 839 | const SkMatrix& getOriginalMatrix() const { |
| bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 840 | GrAssert(this->succeeded()); |
| 841 | return fViewMatrix; |
| 842 | } |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 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 | */ |
| 847 | void restore(); |
| bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 848 | |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 849 | private: |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 850 | GrDrawState* fDrawState; |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 851 | SkMatrix fViewMatrix; |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 852 | GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNumStages]; |
| bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 853 | uint32_t fRestoreMask; |
| bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 854 | }; |
| 855 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 856 | /// @} |
| 857 | |
| 858 | /////////////////////////////////////////////////////////////////////////// |
| 859 | /// @name Render Target |
| 860 | //// |
| 861 | |
| 862 | /** |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 863 | * Sets the render-target used at the next drawing call |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 864 | * |
| 865 | * @param target The render target to set. |
| 866 | */ |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 867 | void setRenderTarget(GrRenderTarget* target) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 868 | fRenderTarget.reset(SkSafeRef(target)); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 869 | } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 870 | |
| 871 | /** |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 872 | * Retrieves the currently set render-target. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 873 | * |
| 874 | * @return The currently set render target. |
| 875 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 876 | const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
| 877 | GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 878 | |
| 879 | class AutoRenderTargetRestore : public ::GrNoncopyable { |
| 880 | public: |
| bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 881 | AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {} |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 882 | AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 883 | fDrawState = NULL; |
| robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 884 | fSavedTarget = NULL; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 885 | this->set(ds, newTarget); |
| 886 | } |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 887 | ~AutoRenderTargetRestore() { this->restore(); } |
| 888 | |
| 889 | void restore() { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 890 | if (NULL != fDrawState) { |
| 891 | fDrawState->setRenderTarget(fSavedTarget); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 892 | fDrawState = NULL; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 893 | } |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 894 | GrSafeSetNull(fSavedTarget); |
| 895 | } |
| 896 | |
| 897 | void set(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 898 | this->restore(); |
| 899 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 900 | if (NULL != ds) { |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 901 | GrAssert(NULL == fSavedTarget); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 902 | fSavedTarget = ds->getRenderTarget(); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 903 | SkSafeRef(fSavedTarget); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 904 | ds->setRenderTarget(newTarget); |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 905 | fDrawState = ds; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 906 | } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 907 | } |
| 908 | private: |
| 909 | GrDrawState* fDrawState; |
| 910 | GrRenderTarget* fSavedTarget; |
| 911 | }; |
| 912 | |
| 913 | /// @} |
| 914 | |
| 915 | /////////////////////////////////////////////////////////////////////////// |
| 916 | /// @name Stencil |
| 917 | //// |
| 918 | |
| 919 | /** |
| 920 | * Sets the stencil settings to use for the next draw. |
| 921 | * Changing the clip has the side-effect of possibly zeroing |
| 922 | * out the client settable stencil bits. So multipass algorithms |
| 923 | * using stencil should not change the clip between passes. |
| 924 | * @param settings the stencil settings to use. |
| 925 | */ |
| 926 | void setStencil(const GrStencilSettings& settings) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 927 | fCommon.fStencilSettings = settings; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Shortcut to disable stencil testing and ops. |
| 932 | */ |
| 933 | void disableStencil() { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 934 | fCommon.fStencilSettings.setDisabled(); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 937 | const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 938 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 939 | GrStencilSettings* stencil() { return &fCommon.fStencilSettings; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 940 | |
| 941 | /// @} |
| 942 | |
| 943 | /////////////////////////////////////////////////////////////////////////// |
| 944 | // @name Edge AA |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 945 | // Edge equations can be specified to perform anti-aliasing. Because the |
| bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 946 | // edges are specified as per-vertex data, vertices that are shared by |
| 947 | // multiple edges must be split. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 948 | // |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 949 | //// |
| 950 | |
| 951 | /** |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 952 | * 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] | 953 | * 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] | 954 | * the edge type requires fewer than 4. |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 955 | * |
| 956 | * TODO: Fix the fact that HairLine and Circle edge types use y-down coords. |
| 957 | * (either adjust in VS or use origin_upper_left in GLSL) |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 958 | */ |
| 959 | enum VertexEdgeType { |
| 960 | /* 1-pixel wide line |
| 961 | 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */ |
| 962 | kHairLine_EdgeType, |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 963 | /* Quadratic specified by u^2-v canonical coords (only 2 |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 964 | components used). Coverage based on signed distance with negative |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 965 | being inside, positive outside. Edge specified in window space |
| 966 | (y-down) */ |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 967 | kQuad_EdgeType, |
| 968 | /* Same as above but for hairline quadratics. Uses unsigned distance. |
| 969 | Coverage is min(0, 1-distance). */ |
| 970 | kHairQuad_EdgeType, |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 971 | /* Circle specified as center_x, center_y, outer_radius, inner_radius |
| 972 | all in window space (y-down). */ |
| 973 | kCircle_EdgeType, |
| jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 974 | /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius |
| 975 | all in window space (y-down). */ |
| 976 | kEllipse_EdgeType, |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 977 | |
| 978 | kVertexEdgeTypeCnt |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 982 | * Determines the interpretation per-vertex edge data when the |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 983 | * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges |
| 984 | * are not specified the value of this setting has no effect. |
| 985 | */ |
| 986 | void setVertexEdgeType(VertexEdgeType type) { |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 987 | GrAssert(type >=0 && type < kVertexEdgeTypeCnt); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 988 | fCommon.fVertexEdgeType = type; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 991 | VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 992 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 993 | /// @} |
| tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 994 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 995 | /////////////////////////////////////////////////////////////////////////// |
| 996 | /// @name State Flags |
| 997 | //// |
| tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 998 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 999 | /** |
| 1000 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 1001 | * default to disabled. |
| 1002 | */ |
| 1003 | enum StateBits { |
| 1004 | /** |
| 1005 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 1006 | */ |
| 1007 | kDither_StateBit = 0x01, |
| 1008 | /** |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1009 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 1010 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 1011 | * the 3D API. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1012 | */ |
| 1013 | kHWAntialias_StateBit = 0x02, |
| 1014 | /** |
| 1015 | * Draws will respect the clip, otherwise the clip is ignored. |
| 1016 | */ |
| 1017 | kClip_StateBit = 0x04, |
| 1018 | /** |
| 1019 | * Disables writing to the color buffer. Useful when performing stencil |
| 1020 | * operations. |
| 1021 | */ |
| 1022 | kNoColorWrites_StateBit = 0x08, |
| bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1023 | |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1024 | /** |
| 1025 | * Usually coverage is applied after color blending. The color is blended using the coeffs |
| 1026 | * specified by setBlendFunc(). The blended color is then combined with dst using coeffs |
| 1027 | * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In |
| 1028 | * this case there is no distinction between coverage and color and the caller needs direct |
| 1029 | * control over the blend coeffs. When set, there will be a single blend step controlled by |
| 1030 | * setBlendFunc() which will use coverage*color as the src color. |
| 1031 | */ |
| 1032 | kCoverageDrawing_StateBit = 0x10, |
| 1033 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1034 | // Users of the class may add additional bits to the vector |
| 1035 | kDummyStateBit, |
| 1036 | kLastPublicStateBit = kDummyStateBit-1, |
| 1037 | }; |
| 1038 | |
| 1039 | void resetStateFlags() { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1040 | fCommon.fFlagBits = 0; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * Enable render state settings. |
| 1045 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1046 | * @param stateBits bitfield of StateBits specifying the states to enable |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1047 | */ |
| 1048 | void enableState(uint32_t stateBits) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1049 | fCommon.fFlagBits |= stateBits; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * Disable render state settings. |
| 1054 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1055 | * @param stateBits bitfield of StateBits specifying the states to disable |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1056 | */ |
| 1057 | void disableState(uint32_t stateBits) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1058 | fCommon.fFlagBits &= ~(stateBits); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 1061 | /** |
| 1062 | * Enable or disable stateBits based on a boolean. |
| 1063 | * |
| bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 1064 | * @param stateBits bitfield of StateBits to enable or disable |
| bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 1065 | * @param enable if true enable stateBits, otherwise disable |
| 1066 | */ |
| 1067 | void setState(uint32_t stateBits, bool enable) { |
| 1068 | if (enable) { |
| 1069 | this->enableState(stateBits); |
| 1070 | } else { |
| 1071 | this->disableState(stateBits); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1075 | bool isDitherState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1076 | return 0 != (fCommon.fFlagBits & kDither_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | bool isHWAntialiasState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1080 | return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | bool isClipState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1084 | return 0 != (fCommon.fFlagBits & kClip_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | bool isColorWriteDisabled() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1088 | return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1091 | bool isCoverageDrawing() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1092 | return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit); |
| bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1095 | bool isStateFlagEnabled(uint32_t stateBit) const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1096 | return 0 != (stateBit & fCommon.fFlagBits); |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1099 | /// @} |
| 1100 | |
| 1101 | /////////////////////////////////////////////////////////////////////////// |
| 1102 | /// @name Face Culling |
| 1103 | //// |
| 1104 | |
| 1105 | enum DrawFace { |
| bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 1106 | kInvalid_DrawFace = -1, |
| 1107 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1108 | kBoth_DrawFace, |
| 1109 | kCCW_DrawFace, |
| 1110 | kCW_DrawFace, |
| 1111 | }; |
| 1112 | |
| 1113 | /** |
| 1114 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 1115 | * @param face the face(s) to draw. |
| 1116 | */ |
| 1117 | void setDrawFace(DrawFace face) { |
| bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 1118 | GrAssert(kInvalid_DrawFace != face); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1119 | fCommon.fDrawFace = face; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 1124 | * or both faces. |
| 1125 | * @return the current draw face(s). |
| 1126 | */ |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1127 | DrawFace getDrawFace() const { return fCommon.fDrawFace; } |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1128 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1129 | /// @} |
| 1130 | |
| 1131 | /////////////////////////////////////////////////////////////////////////// |
| tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1132 | |
| tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 1133 | bool isStageEnabled(int s) const { |
| 1134 | GrAssert((unsigned)s < kNumStages); |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1135 | return (NULL != fStages[s].getEffect()); |
| tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1138 | // Most stages are usually not used, so conditionals here |
| 1139 | // reduce the expected number of bytes touched by 50%. |
| 1140 | bool operator ==(const GrDrawState& s) const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1141 | if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) { |
| bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1144 | |
| 1145 | for (int i = 0; i < kNumStages; i++) { |
| bsalomon@google.com | f2f8fc3 | 2012-07-18 18:25:07 +0000 | [diff] [blame] | 1146 | bool enabled = this->isStageEnabled(i); |
| 1147 | if (enabled != s.isStageEnabled(i)) { |
| 1148 | return false; |
| 1149 | } |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1150 | if (enabled && this->fStages[i] != s.fStages[i]) { |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1151 | return false; |
| 1152 | } |
| 1153 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1154 | return true; |
| 1155 | } |
| 1156 | bool operator !=(const GrDrawState& s) const { return !(*this == s); } |
| 1157 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1158 | GrDrawState& operator= (const GrDrawState& s) { |
| 1159 | this->setRenderTarget(s.fRenderTarget.get()); |
| 1160 | fCommon = s.fCommon; |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1161 | for (int i = 0; i < kNumStages; i++) { |
| tomhudson@google.com | e742bf0 | 2012-07-13 19:54:19 +0000 | [diff] [blame] | 1162 | if (s.isStageEnabled(i)) { |
| bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1163 | this->fStages[i] = s.fStages[i]; |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 1166 | return *this; |
| 1167 | } |
| 1168 | |
| 1169 | private: |
| bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 1170 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1171 | /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */ |
| 1172 | struct CommonState { |
| 1173 | // These fields are roughly sorted by decreasing likelihood of being different in op== |
| 1174 | GrColor fColor; |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 1175 | GrVertexLayout fVertexLayout; |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1176 | SkMatrix fViewMatrix; |
| 1177 | GrBlendCoeff fSrcBlend; |
| 1178 | GrBlendCoeff fDstBlend; |
| 1179 | GrColor fBlendConstant; |
| 1180 | uint32_t fFlagBits; |
| 1181 | VertexEdgeType fVertexEdgeType; |
| 1182 | GrStencilSettings fStencilSettings; |
| 1183 | int fFirstCoverageStage; |
| 1184 | GrColor fCoverage; |
| 1185 | SkXfermode::Mode fColorFilterMode; |
| 1186 | GrColor fColorFilterColor; |
| 1187 | DrawFace fDrawFace; |
| 1188 | bool operator== (const CommonState& other) const { |
| 1189 | return fColor == other.fColor && |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 1190 | fVertexLayout == other.fVertexLayout && |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1191 | fViewMatrix.cheapEqualTo(other.fViewMatrix) && |
| 1192 | fSrcBlend == other.fSrcBlend && |
| 1193 | fDstBlend == other.fDstBlend && |
| 1194 | fBlendConstant == other.fBlendConstant && |
| 1195 | fFlagBits == other.fFlagBits && |
| 1196 | fVertexEdgeType == other.fVertexEdgeType && |
| 1197 | fStencilSettings == other.fStencilSettings && |
| 1198 | fFirstCoverageStage == other.fFirstCoverageStage && |
| 1199 | fCoverage == other.fCoverage && |
| 1200 | fColorFilterMode == other.fColorFilterMode && |
| 1201 | fColorFilterColor == other.fColorFilterColor && |
| 1202 | fDrawFace == other.fDrawFace; |
| 1203 | } |
| 1204 | bool operator!= (const CommonState& other) const { return !(*this == other); } |
| 1205 | }; |
| bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 1206 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1207 | /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef. |
| 1208 | DeferredState must directly reference GrEffects, however. */ |
| 1209 | struct SavedEffectStage { |
| 1210 | SavedEffectStage() : fEffect(NULL) {} |
| 1211 | const GrEffect* fEffect; |
| 1212 | GrEffectStage::SavedCoordChange fCoordChange; |
| 1213 | }; |
| 1214 | |
| 1215 | public: |
| 1216 | /** |
| 1217 | * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource |
| 1218 | * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal |
| 1219 | * dispose mechanism returns them to the cache. This allows recycling resources through the |
| 1220 | * the cache while they are in a deferred draw queue. |
| 1221 | */ |
| 1222 | class DeferredState { |
| 1223 | public: |
| 1224 | DeferredState() : fRenderTarget(NULL) { |
| 1225 | GR_DEBUGCODE(fInitialized = false;) |
| 1226 | } |
| 1227 | // TODO: Remove this when DeferredState no longer holds a ref to the RT |
| 1228 | ~DeferredState() { SkSafeUnref(fRenderTarget); } |
| 1229 | |
| 1230 | void saveFrom(const GrDrawState& drawState) { |
| 1231 | fCommon = drawState.fCommon; |
| 1232 | // TODO: Here we will copy the GrRenderTarget pointer without taking a ref. |
| 1233 | fRenderTarget = drawState.fRenderTarget.get(); |
| 1234 | SkSafeRef(fRenderTarget); |
| 1235 | // Here we ref the effects directly rather than the effect-refs. TODO: When the effect- |
| 1236 | // ref gets fully unref'ed it will cause the underlying effect to unref its resources |
| 1237 | // and recycle them to the cache (if no one else is holding a ref to the resources). |
| 1238 | for (int i = 0; i < kNumStages; ++i) { |
| 1239 | fStages[i].saveFrom(drawState.fStages[i]); |
| 1240 | } |
| 1241 | GR_DEBUGCODE(fInitialized = true;) |
| 1242 | } |
| 1243 | |
| 1244 | void restoreTo(GrDrawState* drawState) { |
| 1245 | GrAssert(fInitialized); |
| 1246 | drawState->fCommon = fCommon; |
| 1247 | drawState->setRenderTarget(fRenderTarget); |
| 1248 | for (int i = 0; i < kNumStages; ++i) { |
| 1249 | fStages[i].restoreTo(&drawState->fStages[i]); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | bool isEqual(const GrDrawState& state) const { |
| 1254 | if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) { |
| 1255 | return false; |
| 1256 | } |
| 1257 | for (int i = 0; i < kNumStages; ++i) { |
| bsalomon@google.com | dcd69bf | 2013-01-24 18:28:51 +0000 | [diff] [blame] | 1258 | if (!fStages[i].isEqual(state.fStages[i])) { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
| 1261 | } |
| 1262 | return true; |
| 1263 | } |
| 1264 | |
| 1265 | private: |
| 1266 | GrRenderTarget* fRenderTarget; |
| 1267 | CommonState fCommon; |
| 1268 | GrEffectStage::DeferredStage fStages[kNumStages]; |
| 1269 | |
| 1270 | GR_DEBUGCODE(bool fInitialized;) |
| 1271 | }; |
| 1272 | |
| 1273 | private: |
| 1274 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 1275 | CommonState fCommon; |
| 1276 | GrEffectStage fStages[kNumStages]; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1277 | |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 1278 | typedef GrRefCnt INHERITED; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1279 | }; |
| 1280 | |
| 1281 | #endif |