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