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 | |
| 11 | #include "GrColor.h" |
| 12 | #include "GrMatrix.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 13 | #include "GrNoncopyable.h" |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 14 | #include "GrRefCnt.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 15 | #include "GrSamplerState.h" |
| 16 | #include "GrStencil.h" |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 17 | #include "GrTexture.h" |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 18 | #include "GrRenderTarget.h" |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 19 | #include "effects/GrSingleTextureEffect.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 20 | |
| 21 | #include "SkXfermode.h" |
| 22 | |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 23 | class GrPaint; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 25 | class GrDrawState : public GrRefCnt { |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 26 | public: |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 27 | SK_DECLARE_INST_COUNT(GrDrawState) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Number of texture stages. Each stage takes as input a color and |
| 31 | * 2D texture coordinates. The color input to the first enabled stage is the |
| 32 | * per-vertex color or the constant color (setColor/setAlpha) if there are |
| 33 | * no per-vertex colors. For subsequent stages the input color is the output |
| 34 | * color from the previous enabled stage. The output color of each stage is |
| 35 | * the input color modulated with the result of a texture lookup. Texture |
| 36 | * lookups are specified by a texture a sampler (setSamplerState). Texture |
| 37 | * coordinates for each stage come from the vertices based on a |
| 38 | * GrVertexLayout bitfield. The output fragment color is the output color of |
| 39 | * the last enabled stage. The presence or absence of texture coordinates |
| 40 | * for each stage in the vertex layout indicates whether a stage is enabled |
| 41 | * or not. |
robertphillips@google.com | bf5cad4 | 2012-05-10 12:40:40 +0000 | [diff] [blame] | 42 | * |
| 43 | * Stages 0 through GrPaint::kTotalStages-1 are reserved for setting up |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | * the draw (i.e., textures and filter masks). Stages GrPaint::kTotalStages |
| 45 | * through kNumStages-1 are earmarked for use by GrTextContext and |
robertphillips@google.com | bf5cad4 | 2012-05-10 12:40:40 +0000 | [diff] [blame] | 46 | * GrPathRenderer-derived classes. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 47 | */ |
| 48 | enum { |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 49 | kNumStages = 5, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 50 | kMaxTexCoords = kNumStages |
| 51 | }; |
| 52 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | GrDrawState() |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 54 | : fRenderTarget(NULL) { |
| 55 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 56 | this->reset(); |
| 57 | } |
bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 58 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 59 | GrDrawState(const GrDrawState& state) |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 60 | : fRenderTarget(NULL) { |
| 61 | |
bsalomon@google.com | 46f7afb | 2012-01-18 19:51:55 +0000 | [diff] [blame] | 62 | *this = state; |
| 63 | } |
| 64 | |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 65 | virtual ~GrDrawState() { |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 66 | this->disableStages(); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 67 | GrSafeSetNull(fRenderTarget); |
| 68 | } |
| 69 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 70 | /** |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 71 | * Resets to the default state. |
| 72 | * Sampler states *will* be modified: textures or CustomStage objects |
| 73 | * will be released. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | */ |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 75 | void reset() { |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 76 | |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 77 | this->disableStages(); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 78 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 79 | fColor = 0xffffffff; |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 80 | fViewMatrix.reset(); |
| 81 | GrSafeSetNull(fRenderTarget); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 82 | fSrcBlend = kOne_GrBlendCoeff; |
| 83 | fDstBlend = kZero_GrBlendCoeff; |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 84 | fBlendConstant = 0x0; |
| 85 | fFlagBits = 0x0; |
| 86 | fVertexEdgeType = kHairLine_EdgeType; |
| 87 | fStencilSettings.setDisabled(); |
| 88 | fFirstCoverageStage = kNumStages; |
| 89 | fCoverage = 0xffffffff; |
| 90 | fColorFilterMode = SkXfermode::kDst_Mode; |
| 91 | fColorFilterColor = 0x0; |
| 92 | fDrawFace = kBoth_DrawFace; |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState |
| 97 | * encompases more than GrPaint. Aspects of GrDrawState that have no |
| 98 | * GrPaint equivalents are not modified. GrPaint has fewer stages than |
| 99 | * GrDrawState. The extra GrDrawState stages are disabled. |
| 100 | */ |
| 101 | void setFromPaint(const GrPaint& paint); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////// |
| 104 | /// @name Color |
| 105 | //// |
| 106 | |
| 107 | /** |
| 108 | * Sets color for next draw to a premultiplied-alpha color. |
| 109 | * |
| 110 | * @param color the color to set. |
| 111 | */ |
| 112 | void setColor(GrColor color) { fColor = color; } |
| 113 | |
| 114 | GrColor getColor() const { return fColor; } |
| 115 | |
| 116 | /** |
| 117 | * Sets the color to be used for the next draw to be |
| 118 | * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
| 119 | * |
| 120 | * @param alpha The alpha value to set as the color. |
| 121 | */ |
| 122 | void setAlpha(uint8_t a) { |
| 123 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Add a color filter that can be represented by a color and a mode. Applied |
| 128 | * after color-computing texture stages. |
| 129 | */ |
| 130 | void setColorFilter(GrColor c, SkXfermode::Mode mode) { |
| 131 | fColorFilterColor = c; |
| 132 | fColorFilterMode = mode; |
| 133 | } |
| 134 | |
| 135 | GrColor getColorFilterColor() const { return fColorFilterColor; } |
| 136 | SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; } |
| 137 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Constructor sets the color to be 'color' which is undone by the destructor. |
| 140 | */ |
| 141 | class AutoColorRestore : public ::GrNoncopyable { |
| 142 | public: |
| 143 | AutoColorRestore(GrDrawState* drawState, GrColor color) { |
| 144 | fDrawState = drawState; |
| 145 | fOldColor = fDrawState->getColor(); |
| 146 | fDrawState->setColor(color); |
| 147 | } |
| 148 | ~AutoColorRestore() { |
| 149 | fDrawState->setColor(fOldColor); |
| 150 | } |
| 151 | private: |
| 152 | GrDrawState* fDrawState; |
| 153 | GrColor fOldColor; |
| 154 | }; |
| 155 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 156 | /// @} |
| 157 | |
| 158 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 159 | /// @name Coverage |
| 160 | //// |
| 161 | |
| 162 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 163 | * 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] | 164 | * initial value (after construction or reset()) is 0xff. The constant |
| 165 | * coverage is ignored when per-vertex coverage is provided. |
| 166 | */ |
| 167 | void setCoverage(uint8_t coverage) { |
| 168 | fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Version of above that specifies 4 channel per-vertex color. The value |
| 173 | * should be premultiplied. |
| 174 | */ |
| 175 | void setCoverage4(GrColor coverage) { |
| 176 | fCoverage = coverage; |
| 177 | } |
| 178 | |
| 179 | GrColor getCoverage() const { |
| 180 | return fCoverage; |
| 181 | } |
| 182 | |
| 183 | /// @} |
| 184 | |
| 185 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 186 | /// @name Textures |
| 187 | //// |
| 188 | |
| 189 | /** |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 190 | * Creates a GrSingleTextureEffect. |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 191 | */ |
| 192 | void createTextureEffect(int stage, GrTexture* texture) { |
| 193 | GrAssert(!this->getSampler(stage).getCustomStage()); |
| 194 | this->sampler(stage)->setCustomStage( |
| 195 | SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref(); |
| 196 | } |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 197 | void createTextureEffect(int stage, GrTexture* texture, const GrTextureParams& params) { |
| 198 | GrAssert(!this->getSampler(stage).getCustomStage()); |
| 199 | this->sampler(stage)->setCustomStage( |
| 200 | SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref(); |
| 201 | } |
| 202 | |
tomhudson@google.com | 1e8f016 | 2012-07-20 16:25:18 +0000 | [diff] [blame] | 203 | |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 204 | bool stagesDisabled() { |
| 205 | for (int i = 0; i < kNumStages; ++i) { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 206 | if (NULL != fSamplerStates[i].getCustomStage()) { |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 207 | return false; |
| 208 | } |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 209 | } |
tomhudson@google.com | 3eee8fb | 2012-06-25 12:30:34 +0000 | [diff] [blame] | 210 | return true; |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 211 | } |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 212 | |
| 213 | void disableStage(int index) { |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 214 | fSamplerStates[index].setCustomStage(NULL); |
| 215 | } |
| 216 | |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 217 | /** |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 218 | * Release all the textures and custom stages referred to by this |
| 219 | * draw state. |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 220 | */ |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 221 | void disableStages() { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 222 | for (int i = 0; i < kNumStages; ++i) { |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 223 | this->disableStage(i); |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 227 | class AutoStageDisable : public ::GrNoncopyable { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 228 | public: |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 229 | AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {} |
| 230 | ~AutoStageDisable() { |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 231 | if (NULL != fDrawState) { |
tomhudson@google.com | 7d6afdd | 2012-06-22 20:10:50 +0000 | [diff] [blame] | 232 | fDrawState->disableStages(); |
robertphillips@google.com | 972265d | 2012-06-13 18:49:30 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | private: |
| 236 | GrDrawState* fDrawState; |
| 237 | }; |
| 238 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 239 | /// @} |
| 240 | |
| 241 | /////////////////////////////////////////////////////////////////////////// |
| 242 | /// @name Samplers |
| 243 | //// |
| 244 | |
| 245 | /** |
| 246 | * Returns the current sampler for a stage. |
| 247 | */ |
| 248 | const GrSamplerState& getSampler(int stage) const { |
| 249 | GrAssert((unsigned)stage < kNumStages); |
| 250 | return fSamplerStates[stage]; |
| 251 | } |
| 252 | |
| 253 | /** |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 254 | * Writable pointer to a stage's sampler. |
| 255 | */ |
| 256 | GrSamplerState* sampler(int stage) { |
| 257 | GrAssert((unsigned)stage < kNumStages); |
| 258 | return fSamplerStates + stage; |
| 259 | } |
| 260 | |
| 261 | /** |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 262 | * Preconcats the matrix of all samplers of enabled stages with a matrix. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 263 | */ |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 264 | void preConcatSamplerMatrices(const GrMatrix& matrix) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 265 | for (int i = 0; i < kNumStages; ++i) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 266 | if (this->isStageEnabled(i)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 267 | fSamplerStates[i].preConcatMatrix(matrix); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 272 | /** |
| 273 | * Preconcats the matrix of all samplers in the mask with the inverse of a |
| 274 | * matrix. If the matrix inverse cannot be computed (and there is at least |
| 275 | * one enabled stage) then false is returned. |
| 276 | */ |
| 277 | bool preConcatSamplerMatricesWithInverse(const GrMatrix& matrix) { |
| 278 | GrMatrix inv; |
| 279 | bool computed = false; |
| 280 | for (int i = 0; i < kNumStages; ++i) { |
| 281 | if (this->isStageEnabled(i)) { |
| 282 | if (!computed && !matrix.invert(&inv)) { |
| 283 | return false; |
| 284 | } else { |
| 285 | computed = true; |
| 286 | } |
| 287 | fSamplerStates[i].preConcatMatrix(inv); |
| 288 | } |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 293 | /// @} |
| 294 | |
| 295 | /////////////////////////////////////////////////////////////////////////// |
| 296 | /// @name Coverage / Color Stages |
| 297 | //// |
| 298 | |
| 299 | /** |
| 300 | * A common pattern is to compute a color with the initial stages and then |
| 301 | * 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] | 302 | * filters, glyph mask, etc). Color-filters, xfermodes, etc should be |
| 303 | * computed based on the pre-coverage-modulated color. The division of |
| 304 | * stages between color-computing and coverage-computing is specified by |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 305 | * this method. Initially this is kNumStages (all stages |
| 306 | * are color-computing). |
| 307 | */ |
| 308 | void setFirstCoverageStage(int firstCoverageStage) { |
| 309 | GrAssert((unsigned)firstCoverageStage <= kNumStages); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 310 | fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Gets the index of the first coverage-computing stage. |
| 315 | */ |
| 316 | int getFirstCoverageStage() const { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 317 | return fFirstCoverageStage; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | ///@} |
| 321 | |
| 322 | /////////////////////////////////////////////////////////////////////////// |
| 323 | /// @name Blending |
| 324 | //// |
| 325 | |
| 326 | /** |
| 327 | * Sets the blending function coeffecients. |
| 328 | * |
| 329 | * The blend function will be: |
| 330 | * D' = sat(S*srcCoef + D*dstCoef) |
| 331 | * |
| 332 | * where D is the existing destination color, S is the incoming source |
| 333 | * color, and D' is the new destination color that will be written. sat() |
| 334 | * is the saturation function. |
| 335 | * |
| 336 | * @param srcCoef coeffecient applied to the src color. |
| 337 | * @param dstCoef coeffecient applied to the dst color. |
| 338 | */ |
| 339 | void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
| 340 | fSrcBlend = srcCoeff; |
| 341 | fDstBlend = dstCoeff; |
| 342 | #if GR_DEBUG |
| 343 | switch (dstCoeff) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 344 | case kDC_GrBlendCoeff: |
| 345 | case kIDC_GrBlendCoeff: |
| 346 | case kDA_GrBlendCoeff: |
| 347 | case kIDA_GrBlendCoeff: |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 348 | GrPrintf("Unexpected dst blend coeff. Won't work correctly with" |
| 349 | "coverage stages.\n"); |
| 350 | break; |
| 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | switch (srcCoeff) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 355 | case kSC_GrBlendCoeff: |
| 356 | case kISC_GrBlendCoeff: |
| 357 | case kSA_GrBlendCoeff: |
| 358 | case kISA_GrBlendCoeff: |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 359 | GrPrintf("Unexpected src blend coeff. Won't work correctly with" |
| 360 | "coverage stages.\n"); |
| 361 | break; |
| 362 | default: |
| 363 | break; |
| 364 | } |
| 365 | #endif |
| 366 | } |
| 367 | |
| 368 | GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 369 | GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 370 | |
| 371 | void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff, |
| 372 | GrBlendCoeff* dstBlendCoeff) const { |
| 373 | *srcBlendCoeff = fSrcBlend; |
| 374 | *dstBlendCoeff = fDstBlend; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Sets the blending function constant referenced by the following blending |
| 379 | * coeffecients: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 380 | * kConstC_GrBlendCoeff |
| 381 | * kIConstC_GrBlendCoeff |
| 382 | * kConstA_GrBlendCoeff |
| 383 | * kIConstA_GrBlendCoeff |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 384 | * |
| 385 | * @param constant the constant to set |
| 386 | */ |
| 387 | void setBlendConstant(GrColor constant) { fBlendConstant = constant; } |
| 388 | |
| 389 | /** |
| 390 | * Retrieves the last value set by setBlendConstant() |
| 391 | * @return the blending constant value |
| 392 | */ |
| 393 | GrColor getBlendConstant() const { return fBlendConstant; } |
| 394 | |
| 395 | /// @} |
| 396 | |
| 397 | /////////////////////////////////////////////////////////////////////////// |
| 398 | /// @name View Matrix |
| 399 | //// |
| 400 | |
| 401 | /** |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 402 | * Sets the matrix applied to vertex positions. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 403 | * |
| 404 | * In the post-view-matrix space the rectangle [0,w]x[0,h] |
| 405 | * fully covers the render target. (w and h are the width and height of the |
| 406 | * the rendertarget.) |
| 407 | */ |
| 408 | void setViewMatrix(const GrMatrix& m) { fViewMatrix = m; } |
| 409 | |
| 410 | /** |
| 411 | * Gets a writable pointer to the view matrix. |
| 412 | */ |
| 413 | GrMatrix* viewMatrix() { return &fViewMatrix; } |
| 414 | |
| 415 | /** |
| 416 | * Multiplies the current view matrix by a matrix |
| 417 | * |
| 418 | * After this call V' = V*m where V is the old view matrix, |
| 419 | * m is the parameter to this function, and V' is the new view matrix. |
| 420 | * (We consider positions to be column vectors so position vector p is |
| 421 | * transformed by matrix X as p' = X*p.) |
| 422 | * |
| 423 | * @param m the matrix used to modify the view matrix. |
| 424 | */ |
| 425 | void preConcatViewMatrix(const GrMatrix& m) { fViewMatrix.preConcat(m); } |
| 426 | |
| 427 | /** |
| 428 | * Multiplies the current view matrix by a matrix |
| 429 | * |
| 430 | * After this call V' = m*V where V is the old view matrix, |
| 431 | * m is the parameter to this function, and V' is the new view matrix. |
| 432 | * (We consider positions to be column vectors so position vector p is |
| 433 | * transformed by matrix X as p' = X*p.) |
| 434 | * |
| 435 | * @param m the matrix used to modify the view matrix. |
| 436 | */ |
| 437 | void postConcatViewMatrix(const GrMatrix& m) { fViewMatrix.postConcat(m); } |
| 438 | |
| 439 | /** |
| 440 | * Retrieves the current view matrix |
| 441 | * @return the current view matrix. |
| 442 | */ |
| 443 | const GrMatrix& getViewMatrix() const { return fViewMatrix; } |
| 444 | |
| 445 | /** |
| 446 | * Retrieves the inverse of the current view matrix. |
| 447 | * |
| 448 | * If the current view matrix is invertible, return true, and if matrix |
| 449 | * is non-null, copy the inverse into it. If the current view matrix is |
| 450 | * non-invertible, return false and ignore the matrix parameter. |
| 451 | * |
| 452 | * @param matrix if not null, will receive a copy of the current inverse. |
| 453 | */ |
| 454 | bool getViewInverse(GrMatrix* matrix) const { |
| 455 | // TODO: determine whether we really need to leave matrix unmodified |
| 456 | // at call sites when inversion fails. |
| 457 | GrMatrix inverse; |
| 458 | if (fViewMatrix.invert(&inverse)) { |
| 459 | if (matrix) { |
| 460 | *matrix = inverse; |
| 461 | } |
| 462 | return true; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 467 | //////////////////////////////////////////////////////////////////////////// |
| 468 | |
| 469 | /** |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 470 | * Preconcats the current view matrix and restores the previous view matrix in the destructor. |
| 471 | * Stage matrices are automatically adjusted to compensate. |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 472 | */ |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 473 | class AutoViewMatrixRestore : public ::GrNoncopyable { |
| 474 | public: |
| 475 | AutoViewMatrixRestore() : fDrawState(NULL) {} |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 476 | |
| 477 | AutoViewMatrixRestore(GrDrawState* ds, |
| 478 | const GrMatrix& preconcatMatrix, |
| 479 | uint32_t explicitCoordStageMask = 0) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 480 | fDrawState = NULL; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 481 | this->set(ds, preconcatMatrix, explicitCoordStageMask); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 482 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 483 | |
| 484 | ~AutoViewMatrixRestore() { this->restore(); } |
| 485 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 486 | /** |
| 487 | * Can be called prior to destructor to restore the original matrix. |
| 488 | */ |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 489 | void restore(); |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame^] | 490 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 491 | void set(GrDrawState* drawState, |
| 492 | const GrMatrix& preconcatMatrix, |
| 493 | uint32_t explicitCoordStageMask = 0); |
| 494 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 495 | bool isSet() const { return NULL != fDrawState; } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 496 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 497 | private: |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 498 | GrDrawState* fDrawState; |
| 499 | GrMatrix fViewMatrix; |
| 500 | GrMatrix fSamplerMatrices[GrDrawState::kNumStages]; |
| 501 | uint32_t fRestoreMask; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 502 | }; |
| 503 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 504 | //////////////////////////////////////////////////////////////////////////// |
| 505 | |
| 506 | /** |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 507 | * This sets the view matrix to identity and adjusts stage matrices to compensate. The |
| 508 | * destructor undoes the changes, restoring the view matrix that was set before the |
| 509 | * constructor. It is similar to passing the inverse of the current view matrix to |
| 510 | * AutoViewMatrixRestore, but lazily computes the inverse only if necessary. |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 511 | */ |
| 512 | class AutoDeviceCoordDraw : ::GrNoncopyable { |
| 513 | public: |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 514 | AutoDeviceCoordDraw() : fDrawState(NULL) {} |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 515 | /** |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 516 | * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to |
| 517 | * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used |
| 518 | * to specify such stages. |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 519 | */ |
| 520 | AutoDeviceCoordDraw(GrDrawState* drawState, |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 521 | uint32_t explicitCoordStageMask = 0) { |
| 522 | fDrawState = NULL; |
| 523 | this->set(drawState, explicitCoordStageMask); |
| 524 | } |
| 525 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 526 | ~AutoDeviceCoordDraw() { this->restore(); } |
| 527 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 528 | bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0); |
| 529 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 530 | /** |
| 531 | * Returns true if this object was successfully initialized on to a GrDrawState. It may |
| 532 | * return false because a non-default constructor or set() were never called or because |
| 533 | * the view matrix was not invertible. |
| 534 | */ |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 535 | bool succeeded() const { return NULL != fDrawState; } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 536 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 537 | /** |
| 538 | * Returns the matrix that was set previously set on the drawState. This is only valid |
| 539 | * if succeeded returns true. |
| 540 | */ |
| 541 | const GrMatrix& getOriginalMatrix() const { |
| 542 | GrAssert(this->succeeded()); |
| 543 | return fViewMatrix; |
| 544 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 545 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 546 | /** |
| 547 | * Can be called prior to destructor to restore the original matrix. |
| 548 | */ |
| 549 | void restore(); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 550 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 551 | private: |
| 552 | GrDrawState* fDrawState; |
| 553 | GrMatrix fViewMatrix; |
| 554 | GrMatrix fSamplerMatrices[GrDrawState::kNumStages]; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 555 | uint32_t fRestoreMask; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 556 | }; |
| 557 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 558 | /// @} |
| 559 | |
| 560 | /////////////////////////////////////////////////////////////////////////// |
| 561 | /// @name Render Target |
| 562 | //// |
| 563 | |
| 564 | /** |
| 565 | * Sets the rendertarget used at the next drawing call |
| 566 | * |
| 567 | * @param target The render target to set. |
| 568 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 569 | void setRenderTarget(GrRenderTarget* target) { |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 570 | GrSafeAssign(fRenderTarget, target); |
| 571 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * Retrieves the currently set rendertarget. |
| 575 | * |
| 576 | * @return The currently set render target. |
| 577 | */ |
| 578 | const GrRenderTarget* getRenderTarget() const { return fRenderTarget; } |
| 579 | GrRenderTarget* getRenderTarget() { return fRenderTarget; } |
| 580 | |
| 581 | class AutoRenderTargetRestore : public ::GrNoncopyable { |
| 582 | public: |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 583 | AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {} |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 584 | AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 585 | fDrawState = NULL; |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 586 | fSavedTarget = NULL; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 587 | this->set(ds, newTarget); |
| 588 | } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 589 | ~AutoRenderTargetRestore() { this->restore(); } |
| 590 | |
| 591 | void restore() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 592 | if (NULL != fDrawState) { |
| 593 | fDrawState->setRenderTarget(fSavedTarget); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 594 | fDrawState = NULL; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 595 | } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 596 | GrSafeSetNull(fSavedTarget); |
| 597 | } |
| 598 | |
| 599 | void set(GrDrawState* ds, GrRenderTarget* newTarget) { |
| 600 | this->restore(); |
| 601 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 602 | if (NULL != ds) { |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 603 | GrAssert(NULL == fSavedTarget); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 604 | fSavedTarget = ds->getRenderTarget(); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 605 | SkSafeRef(fSavedTarget); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 606 | ds->setRenderTarget(newTarget); |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 607 | fDrawState = ds; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 608 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 609 | } |
| 610 | private: |
| 611 | GrDrawState* fDrawState; |
| 612 | GrRenderTarget* fSavedTarget; |
| 613 | }; |
| 614 | |
| 615 | /// @} |
| 616 | |
| 617 | /////////////////////////////////////////////////////////////////////////// |
| 618 | /// @name Stencil |
| 619 | //// |
| 620 | |
| 621 | /** |
| 622 | * Sets the stencil settings to use for the next draw. |
| 623 | * Changing the clip has the side-effect of possibly zeroing |
| 624 | * out the client settable stencil bits. So multipass algorithms |
| 625 | * using stencil should not change the clip between passes. |
| 626 | * @param settings the stencil settings to use. |
| 627 | */ |
| 628 | void setStencil(const GrStencilSettings& settings) { |
| 629 | fStencilSettings = settings; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Shortcut to disable stencil testing and ops. |
| 634 | */ |
| 635 | void disableStencil() { |
| 636 | fStencilSettings.setDisabled(); |
| 637 | } |
| 638 | |
| 639 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 640 | |
| 641 | GrStencilSettings* stencil() { return &fStencilSettings; } |
| 642 | |
| 643 | /// @} |
| 644 | |
| 645 | /////////////////////////////////////////////////////////////////////////// |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 646 | /// @name Color Matrix |
| 647 | //// |
| 648 | |
| 649 | /** |
| 650 | * Sets the color matrix to use for the next draw. |
| 651 | * @param matrix the 5x4 matrix to apply to the incoming color |
| 652 | */ |
| 653 | void setColorMatrix(const float matrix[20]) { |
| 654 | memcpy(fColorMatrix, matrix, sizeof(fColorMatrix)); |
| 655 | } |
| 656 | |
| 657 | const float* getColorMatrix() const { return fColorMatrix; } |
| 658 | |
| 659 | /// @} |
| 660 | |
| 661 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 662 | // @name Edge AA |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 663 | // Edge equations can be specified to perform antialiasing. Because the |
| 664 | // edges are specified as per-vertex data, vertices that are shared by |
| 665 | // multiple edges must be split. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 666 | // |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 667 | //// |
| 668 | |
| 669 | /** |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 670 | * When specifying edges as vertex data this enum specifies what type of |
| 671 | * edges are in use. The edges are always 4 GrScalars in memory, even when |
| 672 | * the edge type requires fewer than 4. |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 673 | * |
| 674 | * TODO: Fix the fact that HairLine and Circle edge types use y-down coords. |
| 675 | * (either adjust in VS or use origin_upper_left in GLSL) |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 676 | */ |
| 677 | enum VertexEdgeType { |
| 678 | /* 1-pixel wide line |
| 679 | 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */ |
| 680 | kHairLine_EdgeType, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 681 | /* Quadratic specified by u^2-v canonical coords (only 2 |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 682 | components used). Coverage based on signed distance with negative |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 683 | being inside, positive outside. Edge specified in window space |
| 684 | (y-down) */ |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 685 | kQuad_EdgeType, |
| 686 | /* Same as above but for hairline quadratics. Uses unsigned distance. |
| 687 | Coverage is min(0, 1-distance). */ |
| 688 | kHairQuad_EdgeType, |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 689 | /* Circle specified as center_x, center_y, outer_radius, inner_radius |
| 690 | all in window space (y-down). */ |
| 691 | kCircle_EdgeType, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 692 | |
| 693 | kVertexEdgeTypeCnt |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 694 | }; |
| 695 | |
| 696 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 697 | * Determines the interpretation per-vertex edge data when the |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 698 | * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges |
| 699 | * are not specified the value of this setting has no effect. |
| 700 | */ |
| 701 | void setVertexEdgeType(VertexEdgeType type) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 702 | GrAssert(type >=0 && type < kVertexEdgeTypeCnt); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 703 | fVertexEdgeType = type; |
| 704 | } |
| 705 | |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 706 | VertexEdgeType getVertexEdgeType() const { return fVertexEdgeType; } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 707 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 708 | /// @} |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 709 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 710 | /////////////////////////////////////////////////////////////////////////// |
| 711 | /// @name State Flags |
| 712 | //// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 713 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 714 | /** |
| 715 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 716 | * default to disabled. |
| 717 | */ |
| 718 | enum StateBits { |
| 719 | /** |
| 720 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 721 | */ |
| 722 | kDither_StateBit = 0x01, |
| 723 | /** |
| 724 | * Perform HW anti-aliasing. This means either HW FSAA, if supported |
| 725 | * by the render target, or smooth-line rendering if a line primitive |
| 726 | * is drawn and line smoothing is supported by the 3D API. |
| 727 | */ |
| 728 | kHWAntialias_StateBit = 0x02, |
| 729 | /** |
| 730 | * Draws will respect the clip, otherwise the clip is ignored. |
| 731 | */ |
| 732 | kClip_StateBit = 0x04, |
| 733 | /** |
| 734 | * Disables writing to the color buffer. Useful when performing stencil |
| 735 | * operations. |
| 736 | */ |
| 737 | kNoColorWrites_StateBit = 0x08, |
| 738 | /** |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 739 | * Draws will apply the color matrix, otherwise the color matrix is |
| 740 | * ignored. |
| 741 | */ |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 742 | kColorMatrix_StateBit = 0x10, |
| 743 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 744 | // Users of the class may add additional bits to the vector |
| 745 | kDummyStateBit, |
| 746 | kLastPublicStateBit = kDummyStateBit-1, |
| 747 | }; |
| 748 | |
| 749 | void resetStateFlags() { |
| 750 | fFlagBits = 0; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Enable render state settings. |
| 755 | * |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 756 | * @param stateBits bitfield of StateBits specifing the states to enable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 757 | */ |
| 758 | void enableState(uint32_t stateBits) { |
| 759 | fFlagBits |= stateBits; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Disable render state settings. |
| 764 | * |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 765 | * @param stateBits bitfield of StateBits specifing the states to disable |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 766 | */ |
| 767 | void disableState(uint32_t stateBits) { |
| 768 | fFlagBits &= ~(stateBits); |
| 769 | } |
| 770 | |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 771 | /** |
| 772 | * Enable or disable stateBits based on a boolean. |
| 773 | * |
| 774 | * @param stateBits bitfield of StateBits to enable or disablt |
| 775 | * @param enable if true enable stateBits, otherwise disable |
| 776 | */ |
| 777 | void setState(uint32_t stateBits, bool enable) { |
| 778 | if (enable) { |
| 779 | this->enableState(stateBits); |
| 780 | } else { |
| 781 | this->disableState(stateBits); |
| 782 | } |
| 783 | } |
| 784 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 785 | bool isDitherState() const { |
| 786 | return 0 != (fFlagBits & kDither_StateBit); |
| 787 | } |
| 788 | |
| 789 | bool isHWAntialiasState() const { |
| 790 | return 0 != (fFlagBits & kHWAntialias_StateBit); |
| 791 | } |
| 792 | |
| 793 | bool isClipState() const { |
| 794 | return 0 != (fFlagBits & kClip_StateBit); |
| 795 | } |
| 796 | |
| 797 | bool isColorWriteDisabled() const { |
| 798 | return 0 != (fFlagBits & kNoColorWrites_StateBit); |
| 799 | } |
| 800 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 801 | bool isStateFlagEnabled(uint32_t stateBit) const { |
| 802 | return 0 != (stateBit & fFlagBits); |
| 803 | } |
| 804 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 805 | /// @} |
| 806 | |
| 807 | /////////////////////////////////////////////////////////////////////////// |
| 808 | /// @name Face Culling |
| 809 | //// |
| 810 | |
| 811 | enum DrawFace { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 812 | kInvalid_DrawFace = -1, |
| 813 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 814 | kBoth_DrawFace, |
| 815 | kCCW_DrawFace, |
| 816 | kCW_DrawFace, |
| 817 | }; |
| 818 | |
| 819 | /** |
| 820 | * Controls whether clockwise, counterclockwise, or both faces are drawn. |
| 821 | * @param face the face(s) to draw. |
| 822 | */ |
| 823 | void setDrawFace(DrawFace face) { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 824 | GrAssert(kInvalid_DrawFace != face); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 825 | fDrawFace = face; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 830 | * or both faces. |
| 831 | * @return the current draw face(s). |
| 832 | */ |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 833 | DrawFace getDrawFace() const { return fDrawFace; } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 834 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 835 | /// @} |
| 836 | |
| 837 | /////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 838 | |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 839 | bool isStageEnabled(int s) const { |
| 840 | GrAssert((unsigned)s < kNumStages); |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 841 | return (NULL != fSamplerStates[s].getCustomStage()); |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 842 | } |
| 843 | |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 844 | // Most stages are usually not used, so conditionals here |
| 845 | // reduce the expected number of bytes touched by 50%. |
| 846 | bool operator ==(const GrDrawState& s) const { |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 847 | if (fColor != s.fColor || |
| 848 | !s.fViewMatrix.cheapEqualTo(fViewMatrix) || |
| 849 | fRenderTarget != s.fRenderTarget || |
| 850 | fSrcBlend != s.fSrcBlend || |
| 851 | fDstBlend != s.fDstBlend || |
| 852 | fBlendConstant != s.fBlendConstant || |
| 853 | fFlagBits != s.fFlagBits || |
| 854 | fVertexEdgeType != s.fVertexEdgeType || |
| 855 | fStencilSettings != s.fStencilSettings || |
| 856 | fFirstCoverageStage != s.fFirstCoverageStage || |
| 857 | fCoverage != s.fCoverage || |
| 858 | fColorFilterMode != s.fColorFilterMode || |
| 859 | fColorFilterColor != s.fColorFilterColor || |
| 860 | fDrawFace != s.fDrawFace) { |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 861 | return false; |
| 862 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 863 | |
| 864 | for (int i = 0; i < kNumStages; i++) { |
bsalomon@google.com | f2f8fc3 | 2012-07-18 18:25:07 +0000 | [diff] [blame] | 865 | bool enabled = this->isStageEnabled(i); |
| 866 | if (enabled != s.isStageEnabled(i)) { |
| 867 | return false; |
| 868 | } |
| 869 | if (enabled && this->fSamplerStates[i] != s.fSamplerStates[i]) { |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 870 | return false; |
| 871 | } |
| 872 | } |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 873 | if (kColorMatrix_StateBit & s.fFlagBits) { |
| 874 | if (memcmp(fColorMatrix, |
| 875 | s.fColorMatrix, |
| 876 | sizeof(fColorMatrix))) { |
| 877 | return false; |
| 878 | } |
| 879 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 880 | |
| 881 | return true; |
| 882 | } |
| 883 | bool operator !=(const GrDrawState& s) const { return !(*this == s); } |
| 884 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 885 | // Most stages are usually not used, so conditionals here |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 886 | // reduce the expected number of bytes touched by 50%. |
| 887 | GrDrawState& operator =(const GrDrawState& s) { |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 888 | fColor = s.fColor; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 889 | fViewMatrix = s.fViewMatrix; |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 890 | SkRefCnt_SafeAssign(fRenderTarget, s.fRenderTarget); |
| 891 | fSrcBlend = s.fSrcBlend; |
| 892 | fDstBlend = s.fDstBlend; |
| 893 | fBlendConstant = s.fBlendConstant; |
| 894 | fFlagBits = s.fFlagBits; |
| 895 | fVertexEdgeType = s.fVertexEdgeType; |
| 896 | fStencilSettings = s.fStencilSettings; |
| 897 | fFirstCoverageStage = s.fFirstCoverageStage; |
| 898 | fCoverage = s.fCoverage; |
| 899 | fColorFilterMode = s.fColorFilterMode; |
| 900 | fColorFilterColor = s.fColorFilterColor; |
| 901 | fDrawFace = s.fDrawFace; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 902 | |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 903 | for (int i = 0; i < kNumStages; i++) { |
tomhudson@google.com | e742bf0 | 2012-07-13 19:54:19 +0000 | [diff] [blame] | 904 | if (s.isStageEnabled(i)) { |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 905 | this->fSamplerStates[i] = s.fSamplerStates[i]; |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 908 | |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 909 | if (kColorMatrix_StateBit & s.fFlagBits) { |
| 910 | memcpy(this->fColorMatrix, s.fColorMatrix, sizeof(fColorMatrix)); |
| 911 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 912 | |
| 913 | return *this; |
| 914 | } |
| 915 | |
| 916 | private: |
bsalomon@google.com | 2e3d144 | 2012-03-26 20:33:54 +0000 | [diff] [blame] | 917 | |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 918 | // These fields are roughly sorted by decreasing liklihood of being different in op== |
| 919 | GrColor fColor; |
| 920 | GrMatrix fViewMatrix; |
| 921 | GrRenderTarget* fRenderTarget; |
| 922 | GrBlendCoeff fSrcBlend; |
| 923 | GrBlendCoeff fDstBlend; |
| 924 | GrColor fBlendConstant; |
| 925 | uint32_t fFlagBits; |
robertphillips@google.com | c077d1e | 2012-05-28 14:10:15 +0000 | [diff] [blame] | 926 | VertexEdgeType fVertexEdgeType; |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 927 | GrStencilSettings fStencilSettings; |
robertphillips@google.com | 69ffcf0 | 2012-06-26 21:01:05 +0000 | [diff] [blame] | 928 | int fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 929 | GrColor fCoverage; |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 930 | SkXfermode::Mode fColorFilterMode; |
bsalomon@google.com | 861b3a2 | 2012-09-26 17:28:25 +0000 | [diff] [blame] | 931 | GrColor fColorFilterColor; |
| 932 | DrawFace fDrawFace; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 933 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 934 | // This field must be last; it will not be copied or compared |
| 935 | // if the corresponding fTexture[] is NULL. |
bsalomon@google.com | 52a5dcb | 2012-01-17 16:01:37 +0000 | [diff] [blame] | 936 | GrSamplerState fSamplerStates[kNumStages]; |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 937 | // only compared if the color matrix enable flag is set |
| 938 | float fColorMatrix[20]; // 5 x 4 matrix |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 939 | |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 940 | typedef GrRefCnt INHERITED; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 941 | }; |
| 942 | |
| 943 | #endif |