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