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