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