bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #include "GrDrawState.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 9 | |
egdaniel | b1cff03 | 2014-11-13 06:19:25 -0800 | [diff] [blame] | 10 | #include "GrBlend.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 11 | #include "GrOptDrawState.h" |
| 12 | #include "GrPaint.h" |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 13 | #include "GrProcOptInfo.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
| 15 | #include "effects/GrPorterDuffXferProcessor.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 16 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 17 | bool GrDrawState::isEqual(const GrDrawState& that, bool explicitLocalCoords) const { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 18 | if (this->getRenderTarget() != that.getRenderTarget() || |
| 19 | this->fColorStages.count() != that.fColorStages.count() || |
| 20 | this->fCoverageStages.count() != that.fCoverageStages.count() || |
| 21 | !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 22 | this->fFlagBits != that.fFlagBits || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 23 | this->fStencilSettings != that.fStencilSettings || |
| 24 | this->fDrawFace != that.fDrawFace) { |
| 25 | return false; |
| 26 | } |
| 27 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 28 | if (!this->getXPFactory()->isEqual(*that.getXPFactory())) { |
| 29 | return false; |
| 30 | } |
| 31 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 32 | for (int i = 0; i < this->numColorStages(); i++) { |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame] | 33 | if (this->getColorStage(i) != that.getColorStage(i)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | for (int i = 0; i < this->numCoverageStages(); i++) { |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame] | 38 | if (this->getCoverageStage(i) != that.getCoverageStage(i)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame] | 46 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 47 | |
| 48 | GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 49 | fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 50 | fViewMatrix = that.fViewMatrix; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 51 | fFlagBits = that.fFlagBits; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 52 | fStencilSettings = that.fStencilSettings; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 53 | fDrawFace = that.fDrawFace; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 54 | fXPFactory.reset(SkRef(that.getXPFactory())); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 55 | fColorStages = that.fColorStages; |
| 56 | fCoverageStages = that.fCoverageStages; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 57 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 58 | fColorProcInfoValid = that.fColorProcInfoValid; |
| 59 | fCoverageProcInfoValid = that.fCoverageProcInfoValid; |
joshualitt | f364b61 | 2014-12-11 06:52:01 -0800 | [diff] [blame] | 60 | fColorCache = that.fColorCache; |
| 61 | fCoverageCache = that.fCoverageCache; |
joshualitt | e832b95 | 2014-12-12 08:41:15 -0800 | [diff] [blame] | 62 | fColorPrimProc = that.fColorPrimProc; |
| 63 | fCoveragePrimProc = that.fCoveragePrimProc; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 64 | if (fColorProcInfoValid) { |
| 65 | fColorProcInfo = that.fColorProcInfo; |
| 66 | } |
| 67 | if (fCoverageProcInfoValid) { |
| 68 | fCoverageProcInfo = that.fCoverageProcInfo; |
| 69 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 74 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 75 | fRenderTarget.reset(NULL); |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 76 | |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 77 | fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 78 | fColorStages.reset(); |
| 79 | fCoverageStages.reset(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 80 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 81 | if (NULL == initialViewMatrix) { |
| 82 | fViewMatrix.reset(); |
| 83 | } else { |
| 84 | fViewMatrix = *initialViewMatrix; |
| 85 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 86 | fFlagBits = 0x0; |
| 87 | fStencilSettings.setDisabled(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 88 | fDrawFace = kBoth_DrawFace; |
| 89 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 90 | fColorProcInfoValid = false; |
| 91 | fCoverageProcInfoValid = false; |
joshualitt | 9b33822 | 2014-12-10 12:28:08 -0800 | [diff] [blame] | 92 | |
| 93 | fColorCache = GrColor_ILLEGAL; |
| 94 | fCoverageCache = GrColor_ILLEGAL; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 95 | |
| 96 | fColorPrimProc = NULL; |
| 97 | fCoveragePrimProc = NULL; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 98 | } |
| 99 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 100 | void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRenderTarget* rt) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 101 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 102 | |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 103 | fColorStages.reset(); |
| 104 | fCoverageStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 105 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 106 | for (int i = 0; i < paint.numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 107 | fColorStages.push_back(paint.getColorStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 108 | } |
| 109 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 110 | for (int i = 0; i < paint.numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 111 | fCoverageStages.push_back(paint.getCoverageStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 114 | fXPFactory.reset(SkRef(paint.getXPFactory())); |
| 115 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 116 | this->setRenderTarget(rt); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 117 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 118 | fViewMatrix = vm; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 119 | |
| 120 | // These have no equivalent in GrPaint, set them to defaults |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 121 | fDrawFace = kBoth_DrawFace; |
| 122 | fStencilSettings.setDisabled(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 123 | fFlagBits = 0; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 124 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 125 | // Enable the clip bit |
| 126 | this->enableState(GrDrawState::kClip_StateBit); |
| 127 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 128 | this->setState(GrDrawState::kDither_StateBit, paint.isDither()); |
| 129 | this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 130 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 131 | fColorProcInfoValid = false; |
| 132 | fCoverageProcInfoValid = false; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 133 | |
| 134 | fColorCache = GrColor_ILLEGAL; |
| 135 | fCoverageCache = GrColor_ILLEGAL; |
joshualitt | e832b95 | 2014-12-12 08:41:15 -0800 | [diff] [blame] | 136 | |
| 137 | fColorPrimProc = NULL; |
| 138 | fCoveragePrimProc = NULL; |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 139 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 140 | |
| 141 | //////////////////////////////////////////////////////////////////////////////// |
| 142 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 143 | bool GrDrawState::canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const { |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 144 | if (caps.dualSourceBlendingSupport()) { |
| 145 | return true; |
| 146 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 147 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 148 | this->calcColorInvariantOutput(color); |
| 149 | |
| 150 | // The coverage isn't actually white, its unknown, but this will produce the same effect |
| 151 | // TODO we want to cache the result of this call, but we can probably clean up the interface |
| 152 | // so we don't have to pass in a seemingly known coverage |
| 153 | this->calcCoverageInvariantOutput(GrColor_WHITE); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 154 | return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 157 | //////////////////////////////////////////////////////////////////////////////s |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 158 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 159 | bool GrDrawState::willEffectReadDstColor(const GrPrimitiveProcessor* pp) const { |
| 160 | this->calcColorInvariantOutput(pp); |
| 161 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 162 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 163 | return fXPFactory->willReadDst(fColorProcInfo, fCoverageProcInfo); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 164 | } |
| 165 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 166 | void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 167 | if (fDrawState) { |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 168 | int m = fDrawState->numColorStages() - fColorEffectCnt; |
| 169 | SkASSERT(m >= 0); |
| 170 | fDrawState->fColorStages.pop_back_n(m); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 171 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 172 | int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
| 173 | SkASSERT(n >= 0); |
| 174 | fDrawState->fCoverageStages.pop_back_n(n); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 175 | if (m + n > 0) { |
| 176 | fDrawState->fColorProcInfoValid = false; |
| 177 | fDrawState->fCoverageProcInfoValid = false; |
| 178 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 179 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 180 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 181 | fDrawState = ds; |
| 182 | if (NULL != ds) { |
| 183 | fColorEffectCnt = ds->numColorStages(); |
| 184 | fCoverageEffectCnt = ds->numCoverageStages(); |
| 185 | SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 186 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 187 | } |
| 188 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 189 | //////////////////////////////////////////////////////////////////////////////// |
| 190 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 191 | // Some blend modes allow folding a fractional coverage value into the color's alpha channel, while |
| 192 | // others will blend incorrectly. |
| 193 | bool GrDrawState::canTweakAlphaForCoverage() const { |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 194 | return fXPFactory->canTweakAlphaForCoverage(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | //////////////////////////////////////////////////////////////////////////////// |
| 198 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 199 | GrDrawState::~GrDrawState() { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 200 | SkASSERT(0 == fBlockEffectRemovalCnt); |
| 201 | } |
| 202 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 203 | //////////////////////////////////////////////////////////////////////////////// |
| 204 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 205 | bool GrDrawState::willBlendWithDst(const GrPrimitiveProcessor* pp) const { |
| 206 | this->calcColorInvariantOutput(pp); |
| 207 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 208 | |
| 209 | GrXPFactory::InvariantOutput output; |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 210 | fXPFactory->getInvariantOutput(fColorProcInfo, fCoverageProcInfo, &output); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 211 | return output.fWillBlendWithDst; |
egdaniel | cd8b630 | 2014-11-11 14:46:05 -0800 | [diff] [blame] | 212 | } |
| 213 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 214 | void GrDrawState::calcColorInvariantOutput(const GrPrimitiveProcessor* pp) const { |
| 215 | if (!fColorProcInfoValid || fColorPrimProc != pp) { |
| 216 | fColorProcInfo.calcColorWithPrimProc(pp, fColorStages.begin(), this->numColorStages()); |
| 217 | fColorProcInfoValid = true; |
| 218 | fColorPrimProc = pp; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void GrDrawState::calcCoverageInvariantOutput(const GrPrimitiveProcessor* pp) const { |
| 223 | if (!fCoverageProcInfoValid || fCoveragePrimProc != pp) { |
| 224 | fCoverageProcInfo.calcCoverageWithPrimProc(pp, fCoverageStages.begin(), |
| 225 | this->numCoverageStages()); |
| 226 | fCoverageProcInfoValid = true; |
| 227 | fCoveragePrimProc = pp; |
| 228 | } |
| 229 | } |
| 230 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 231 | void GrDrawState::calcColorInvariantOutput(GrColor color) const { |
| 232 | if (!fColorProcInfoValid || color != fColorCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 233 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 234 | fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(), color, |
| 235 | flags, false); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 236 | fColorProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 237 | fColorCache = color; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 241 | void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const { |
| 242 | if (!fCoverageProcInfoValid || coverage != fCoverageCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 243 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 244 | fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), |
| 245 | this->numCoverageStages(), coverage, flags, |
| 246 | true); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 247 | fCoverageProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 248 | fCoverageCache = coverage; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 249 | } |
| 250 | } |