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