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