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