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