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 | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 33 | if (!GrFragmentStage::AreCompatible(this->getColorStage(i), that.getColorStage(i), |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 34 | explicitLocalCoords)) { |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | for (int i = 0; i < this->numCoverageStages(); i++) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 39 | if (!GrFragmentStage::AreCompatible(this->getCoverageStage(i), that.getCoverageStage(i), |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 40 | explicitLocalCoords)) { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 48 | //////////////////////////////////////////////////////////////////////////////s |
| 49 | |
egdaniel | 69bb90c | 2014-11-11 07:32:45 -0800 | [diff] [blame] | 50 | GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) { |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 51 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 52 | *this = state; |
| 53 | if (!preConcatMatrix.isIdentity()) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 54 | for (int i = 0; i < this->numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 55 | fColorStages[i].localCoordChange(preConcatMatrix); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 56 | } |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 57 | for (int i = 0; i < this->numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 58 | fCoverageStages[i].localCoordChange(preConcatMatrix); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 59 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 64 | fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 65 | fViewMatrix = that.fViewMatrix; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 66 | fFlagBits = that.fFlagBits; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 67 | fStencilSettings = that.fStencilSettings; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 68 | fDrawFace = that.fDrawFace; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 69 | fXPFactory.reset(SkRef(that.getXPFactory())); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 70 | fColorStages = that.fColorStages; |
| 71 | fCoverageStages = that.fCoverageStages; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 72 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 73 | fColorProcInfoValid = that.fColorProcInfoValid; |
| 74 | fCoverageProcInfoValid = that.fCoverageProcInfoValid; |
joshualitt | f364b61 | 2014-12-11 06:52:01 -0800 | [diff] [blame] | 75 | fColorCache = that.fColorCache; |
| 76 | fCoverageCache = that.fCoverageCache; |
joshualitt | e832b95 | 2014-12-12 08:41:15 -0800 | [diff] [blame] | 77 | fColorPrimProc = that.fColorPrimProc; |
| 78 | fCoveragePrimProc = that.fCoveragePrimProc; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 79 | if (fColorProcInfoValid) { |
| 80 | fColorProcInfo = that.fColorProcInfo; |
| 81 | } |
| 82 | if (fCoverageProcInfoValid) { |
| 83 | fCoverageProcInfo = that.fCoverageProcInfo; |
| 84 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 89 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 90 | fRenderTarget.reset(NULL); |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 91 | |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 92 | fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 93 | fColorStages.reset(); |
| 94 | fCoverageStages.reset(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 95 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 96 | if (NULL == initialViewMatrix) { |
| 97 | fViewMatrix.reset(); |
| 98 | } else { |
| 99 | fViewMatrix = *initialViewMatrix; |
| 100 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 101 | fFlagBits = 0x0; |
| 102 | fStencilSettings.setDisabled(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 103 | fDrawFace = kBoth_DrawFace; |
| 104 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 105 | fColorProcInfoValid = false; |
| 106 | fCoverageProcInfoValid = false; |
joshualitt | 9b33822 | 2014-12-10 12:28:08 -0800 | [diff] [blame] | 107 | |
| 108 | fColorCache = GrColor_ILLEGAL; |
| 109 | fCoverageCache = GrColor_ILLEGAL; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 110 | |
| 111 | fColorPrimProc = NULL; |
| 112 | fCoveragePrimProc = NULL; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 113 | } |
| 114 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 115 | bool GrDrawState::setIdentityViewMatrix() { |
joshualitt | 4dd9988 | 2014-11-11 08:51:30 -0800 | [diff] [blame] | 116 | if (this->numFragmentStages()) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 117 | SkMatrix invVM; |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 118 | if (!fViewMatrix.invert(&invVM)) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 119 | // sad trombone sound |
| 120 | return false; |
| 121 | } |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 122 | for (int s = 0; s < this->numColorStages(); ++s) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 123 | fColorStages[s].localCoordChange(invVM); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 124 | } |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 125 | for (int s = 0; s < this->numCoverageStages(); ++s) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 126 | fCoverageStages[s].localCoordChange(invVM); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 129 | fViewMatrix.reset(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 133 | void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRenderTarget* rt) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 134 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 135 | |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 136 | fColorStages.reset(); |
| 137 | fCoverageStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 138 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 139 | for (int i = 0; i < paint.numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 140 | fColorStages.push_back(paint.getColorStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 143 | for (int i = 0; i < paint.numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 144 | fCoverageStages.push_back(paint.getCoverageStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 147 | fXPFactory.reset(SkRef(paint.getXPFactory())); |
| 148 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 149 | this->setRenderTarget(rt); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 150 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 151 | fViewMatrix = vm; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 152 | |
| 153 | // These have no equivalent in GrPaint, set them to defaults |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 154 | fDrawFace = kBoth_DrawFace; |
| 155 | fStencilSettings.setDisabled(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 156 | fFlagBits = 0; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 157 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 158 | // Enable the clip bit |
| 159 | this->enableState(GrDrawState::kClip_StateBit); |
| 160 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 161 | this->setState(GrDrawState::kDither_StateBit, paint.isDither()); |
| 162 | this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 163 | |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 164 | fColorProcInfoValid = false; |
| 165 | fCoverageProcInfoValid = false; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 166 | |
| 167 | fColorCache = GrColor_ILLEGAL; |
| 168 | fCoverageCache = GrColor_ILLEGAL; |
joshualitt | e832b95 | 2014-12-12 08:41:15 -0800 | [diff] [blame] | 169 | |
| 170 | fColorPrimProc = NULL; |
| 171 | fCoveragePrimProc = NULL; |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 172 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 173 | |
| 174 | //////////////////////////////////////////////////////////////////////////////// |
| 175 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 176 | bool GrDrawState::canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const { |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 177 | if (caps.dualSourceBlendingSupport()) { |
| 178 | return true; |
| 179 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 180 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 181 | this->calcColorInvariantOutput(color); |
| 182 | |
| 183 | // The coverage isn't actually white, its unknown, but this will produce the same effect |
| 184 | // TODO we want to cache the result of this call, but we can probably clean up the interface |
| 185 | // so we don't have to pass in a seemingly known coverage |
| 186 | this->calcCoverageInvariantOutput(GrColor_WHITE); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 187 | return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo, |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 188 | this->isColorWriteDisabled()); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 189 | } |
| 190 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 191 | //////////////////////////////////////////////////////////////////////////////s |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 192 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 193 | bool GrDrawState::willEffectReadDstColor(const GrPrimitiveProcessor* pp) const { |
| 194 | this->calcColorInvariantOutput(pp); |
| 195 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 196 | // TODO: Remove need to create the XP here. |
| 197 | // Also once all custom blends are turned into XPs we can remove the need |
| 198 | // to check other stages since only xp's will be able to read dst |
| 199 | SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(fColorProcInfo, |
| 200 | fCoverageProcInfo)); |
| 201 | if (xferProcessor && xferProcessor->willReadDstColor()) { |
| 202 | return true; |
| 203 | } |
| 204 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 205 | if (!this->isColorWriteDisabled()) { |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 206 | if (fColorProcInfo.readsDst()) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | } |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 210 | return fCoverageProcInfo.readsDst(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 211 | } |
| 212 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 213 | void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 214 | if (fDrawState) { |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 215 | int m = fDrawState->numColorStages() - fColorEffectCnt; |
| 216 | SkASSERT(m >= 0); |
| 217 | fDrawState->fColorStages.pop_back_n(m); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 218 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 219 | int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
| 220 | SkASSERT(n >= 0); |
| 221 | fDrawState->fCoverageStages.pop_back_n(n); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 222 | if (m + n > 0) { |
| 223 | fDrawState->fColorProcInfoValid = false; |
| 224 | fDrawState->fCoverageProcInfoValid = false; |
| 225 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 226 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 227 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 228 | fDrawState = ds; |
| 229 | if (NULL != ds) { |
| 230 | fColorEffectCnt = ds->numColorStages(); |
| 231 | fCoverageEffectCnt = ds->numCoverageStages(); |
| 232 | SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 233 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 234 | } |
| 235 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 236 | //////////////////////////////////////////////////////////////////////////////// |
| 237 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 238 | // Some blend modes allow folding a fractional coverage value into the color's alpha channel, while |
| 239 | // others will blend incorrectly. |
| 240 | bool GrDrawState::canTweakAlphaForCoverage() const { |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 241 | return fXPFactory->canTweakAlphaForCoverage(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | //////////////////////////////////////////////////////////////////////////////// |
| 245 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 246 | void GrDrawState::AutoViewMatrixRestore::restore() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 247 | if (fDrawState) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 248 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 249 | fDrawState->fViewMatrix = fViewMatrix; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 250 | SkASSERT(fDrawState->numColorStages() >= fNumColorStages); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 251 | int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 252 | SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 253 | |
| 254 | int i = 0; |
| 255 | for (int s = 0; s < fNumColorStages; ++s, ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 256 | fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 257 | } |
| 258 | for (int s = 0; s < numCoverageStages; ++s, ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 259 | fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges[i]); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 260 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 261 | fDrawState = NULL; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 262 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 266 | const SkMatrix& preconcatMatrix) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 267 | this->restore(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 268 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 269 | SkASSERT(NULL == fDrawState); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 270 | if (NULL == drawState || preconcatMatrix.isIdentity()) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 271 | return; |
| 272 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 273 | fDrawState = drawState; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 274 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 275 | fViewMatrix = drawState->getViewMatrix(); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 276 | drawState->fViewMatrix.preConcat(preconcatMatrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 277 | |
| 278 | this->doEffectCoordChanges(preconcatMatrix); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 279 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 280 | } |
| 281 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 282 | bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 283 | this->restore(); |
| 284 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 285 | if (NULL == drawState) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 286 | return false; |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 287 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 288 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 289 | if (drawState->getViewMatrix().isIdentity()) { |
| 290 | return true; |
| 291 | } |
| 292 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 293 | fViewMatrix = drawState->getViewMatrix(); |
joshualitt | 4dd9988 | 2014-11-11 08:51:30 -0800 | [diff] [blame] | 294 | if (0 == drawState->numFragmentStages()) { |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 295 | drawState->fViewMatrix.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 296 | fDrawState = drawState; |
| 297 | fNumColorStages = 0; |
| 298 | fSavedCoordChanges.reset(0); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 299 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 300 | return true; |
| 301 | } else { |
| 302 | SkMatrix inv; |
| 303 | if (!fViewMatrix.invert(&inv)) { |
| 304 | return false; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 305 | } |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 306 | drawState->fViewMatrix.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 307 | fDrawState = drawState; |
| 308 | this->doEffectCoordChanges(inv); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 309 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 310 | return true; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 311 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void GrDrawState::AutoViewMatrixRestore::doEffectCoordChanges(const SkMatrix& coordChangeMatrix) { |
joshualitt | 4dd9988 | 2014-11-11 08:51:30 -0800 | [diff] [blame] | 315 | fSavedCoordChanges.reset(fDrawState->numFragmentStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 316 | int i = 0; |
| 317 | |
| 318 | fNumColorStages = fDrawState->numColorStages(); |
| 319 | for (int s = 0; s < fNumColorStages; ++s, ++i) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 320 | fDrawState->getColorStage(s).saveCoordChange(&fSavedCoordChanges[i]); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 321 | fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 322 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 323 | |
| 324 | int numCoverageStages = fDrawState->numCoverageStages(); |
| 325 | for (int s = 0; s < numCoverageStages; ++s, ++i) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 326 | fDrawState->getCoverageStage(s).saveCoordChange(&fSavedCoordChanges[i]); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 327 | fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 328 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 329 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 330 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 331 | //////////////////////////////////////////////////////////////////////////////// |
| 332 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 333 | GrDrawState::~GrDrawState() { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 334 | SkASSERT(0 == fBlockEffectRemovalCnt); |
| 335 | } |
| 336 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 337 | //////////////////////////////////////////////////////////////////////////////// |
| 338 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 339 | bool GrDrawState::willBlendWithDst(const GrPrimitiveProcessor* pp) const { |
| 340 | this->calcColorInvariantOutput(pp); |
| 341 | this->calcCoverageInvariantOutput(pp); |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 342 | |
| 343 | GrXPFactory::InvariantOutput output; |
| 344 | fXPFactory->getInvariantOutput(fColorProcInfo, fCoverageProcInfo, this->isColorWriteDisabled(), |
| 345 | &output); |
| 346 | return output.fWillBlendWithDst; |
egdaniel | cd8b630 | 2014-11-11 14:46:05 -0800 | [diff] [blame] | 347 | } |
| 348 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 349 | void GrDrawState::calcColorInvariantOutput(const GrPrimitiveProcessor* pp) const { |
| 350 | if (!fColorProcInfoValid || fColorPrimProc != pp) { |
| 351 | fColorProcInfo.calcColorWithPrimProc(pp, fColorStages.begin(), this->numColorStages()); |
| 352 | fColorProcInfoValid = true; |
| 353 | fColorPrimProc = pp; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void GrDrawState::calcCoverageInvariantOutput(const GrPrimitiveProcessor* pp) const { |
| 358 | if (!fCoverageProcInfoValid || fCoveragePrimProc != pp) { |
| 359 | fCoverageProcInfo.calcCoverageWithPrimProc(pp, fCoverageStages.begin(), |
| 360 | this->numCoverageStages()); |
| 361 | fCoverageProcInfoValid = true; |
| 362 | fCoveragePrimProc = pp; |
| 363 | } |
| 364 | } |
| 365 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 366 | void GrDrawState::calcColorInvariantOutput(GrColor color) const { |
| 367 | if (!fColorProcInfoValid || color != fColorCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 368 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 369 | fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(), color, |
| 370 | flags, false); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 371 | fColorProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 372 | fColorCache = color; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 376 | void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const { |
| 377 | if (!fCoverageProcInfoValid || coverage != fCoverageCache) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 378 | GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; |
| 379 | fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), |
| 380 | this->numCoverageStages(), coverage, flags, |
| 381 | true); |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 382 | fCoverageProcInfoValid = true; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 383 | fCoverageCache = coverage; |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 384 | } |
| 385 | } |