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 | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 10 | #include "GrOptDrawState.h" |
| 11 | #include "GrPaint.h" |
| 12 | |
| 13 | //////////////////////////////////////////////////////////////////////////////s |
| 14 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 15 | bool GrDrawState::isEqual(const GrDrawState& that) const { |
| 16 | bool usingVertexColors = this->hasColorVertexAttribute(); |
| 17 | if (!usingVertexColors && this->fColor != that.fColor) { |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | if (this->getRenderTarget() != that.getRenderTarget() || |
| 22 | this->fColorStages.count() != that.fColorStages.count() || |
| 23 | this->fCoverageStages.count() != that.fCoverageStages.count() || |
| 24 | !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
| 25 | this->fSrcBlend != that.fSrcBlend || |
| 26 | this->fDstBlend != that.fDstBlend || |
| 27 | this->fBlendConstant != that.fBlendConstant || |
| 28 | this->fFlagBits != that.fFlagBits || |
| 29 | this->fVACount != that.fVACount || |
| 30 | this->fVAStride != that.fVAStride || |
| 31 | memcmp(this->fVAPtr, that.fVAPtr, this->fVACount * sizeof(GrVertexAttrib)) || |
| 32 | this->fStencilSettings != that.fStencilSettings || |
| 33 | this->fDrawFace != that.fDrawFace) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | bool usingVertexCoverage = this->hasCoverageVertexAttribute(); |
| 38 | if (!usingVertexCoverage && this->fCoverage != that.fCoverage) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | bool explicitLocalCoords = this->hasLocalCoordAttribute(); |
| 43 | if (this->hasGeometryProcessor()) { |
| 44 | if (!that.hasGeometryProcessor()) { |
| 45 | return false; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 46 | } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProcessor())) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | } else if (that.hasGeometryProcessor()) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 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 | |
| 66 | SkASSERT(0 == memcmp(this->fFixedFunctionVertexAttribIndices, |
| 67 | that.fFixedFunctionVertexAttribIndices, |
| 68 | sizeof(this->fFixedFunctionVertexAttribIndices))); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 73 | GrDrawState::CombinedState GrDrawState::CombineIfPossible( |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 74 | const GrDrawState& a, const GrDrawState& b, const GrDrawTargetCaps& caps) { |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 75 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 76 | if (!a.isEqual(b)) { |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 77 | return kIncompatible_CombinedState; |
| 78 | } |
| 79 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 80 | // If the general draw states are equal (from check above) we know hasColorVertexAttribute() |
| 81 | // is equivalent for both a and b |
| 82 | if (a.hasColorVertexAttribute()) { |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 83 | // If one is opaque and the other is not then the combined state is not opaque. Moreover, |
| 84 | // if the opaqueness affects the ability to get color/coverage blending correct then we |
| 85 | // don't combine the draw states. |
| 86 | bool aIsOpaque = (kVertexColorsAreOpaque_Hint & a.fHints); |
| 87 | bool bIsOpaque = (kVertexColorsAreOpaque_Hint & b.fHints); |
| 88 | if (aIsOpaque != bIsOpaque) { |
| 89 | const GrDrawState* opaque; |
| 90 | const GrDrawState* nonOpaque; |
| 91 | if (aIsOpaque) { |
| 92 | opaque = &a; |
| 93 | nonOpaque = &b; |
| 94 | } else { |
| 95 | opaque = &b; |
| 96 | nonOpaque = &a; |
| 97 | } |
| 98 | if (!opaque->hasSolidCoverage() && opaque->couldApplyCoverage(caps)) { |
| 99 | SkASSERT(!nonOpaque->hasSolidCoverage()); |
| 100 | if (!nonOpaque->couldApplyCoverage(caps)) { |
| 101 | return kIncompatible_CombinedState; |
| 102 | } |
| 103 | } |
| 104 | return aIsOpaque ? kB_CombinedState : kA_CombinedState; |
| 105 | } |
| 106 | } |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 107 | return kAOrB_CombinedState; |
| 108 | } |
| 109 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 110 | //////////////////////////////////////////////////////////////////////////////s |
| 111 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 112 | GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) |
| 113 | : fCachedOptState(NULL) { |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 114 | SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 115 | *this = state; |
| 116 | if (!preConcatMatrix.isIdentity()) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 117 | for (int i = 0; i < this->numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 118 | fColorStages[i].localCoordChange(preConcatMatrix); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 119 | } |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 120 | for (int i = 0; i < this->numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 121 | fCoverageStages[i].localCoordChange(preConcatMatrix); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 122 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 123 | this->invalidateOptState(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
| 128 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 129 | SkASSERT(!that.fRenderTarget.ownsPendingIO()); |
| 130 | SkASSERT(!this->fRenderTarget.ownsPendingIO()); |
| 131 | this->setRenderTarget(that.getRenderTarget()); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 132 | fColor = that.fColor; |
| 133 | fViewMatrix = that.fViewMatrix; |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 134 | fSrcBlend = that.fSrcBlend; |
| 135 | fDstBlend = that.fDstBlend; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 136 | fBlendConstant = that.fBlendConstant; |
| 137 | fFlagBits = that.fFlagBits; |
| 138 | fVACount = that.fVACount; |
| 139 | fVAPtr = that.fVAPtr; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 140 | fVAStride = that.fVAStride; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 141 | fStencilSettings = that.fStencilSettings; |
| 142 | fCoverage = that.fCoverage; |
| 143 | fDrawFace = that.fDrawFace; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 144 | if (that.hasGeometryProcessor()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 145 | fGeometryProcessor.initAndRef(that.fGeometryProcessor); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 146 | } else { |
| 147 | fGeometryProcessor.reset(NULL); |
| 148 | } |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 149 | fColorStages = that.fColorStages; |
| 150 | fCoverageStages = that.fCoverageStages; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 151 | |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 152 | fHints = that.fHints; |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 153 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 154 | SkRefCnt_SafeAssign(fCachedOptState, that.fCachedOptState); |
| 155 | |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 156 | memcpy(fFixedFunctionVertexAttribIndices, |
| 157 | that.fFixedFunctionVertexAttribIndices, |
| 158 | sizeof(fFixedFunctionVertexAttribIndices)); |
| 159 | return *this; |
| 160 | } |
| 161 | |
| 162 | void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
| 163 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 164 | SkASSERT(!fRenderTarget.ownsPendingIO()); |
| 165 | |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 166 | fGeometryProcessor.reset(NULL); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 167 | fColorStages.reset(); |
| 168 | fCoverageStages.reset(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 169 | |
bsalomon | 2a9ca78 | 2014-09-05 14:27:43 -0700 | [diff] [blame] | 170 | fRenderTarget.reset(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 171 | |
| 172 | this->setDefaultVertexAttribs(); |
| 173 | |
| 174 | fColor = 0xffffffff; |
| 175 | if (NULL == initialViewMatrix) { |
| 176 | fViewMatrix.reset(); |
| 177 | } else { |
| 178 | fViewMatrix = *initialViewMatrix; |
| 179 | } |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 180 | fSrcBlend = kOne_GrBlendCoeff; |
| 181 | fDstBlend = kZero_GrBlendCoeff; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 182 | fBlendConstant = 0x0; |
| 183 | fFlagBits = 0x0; |
| 184 | fStencilSettings.setDisabled(); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 185 | fCoverage = 0xff; |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 186 | fDrawFace = kBoth_DrawFace; |
| 187 | |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 188 | fHints = 0; |
| 189 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 190 | this->invalidateOptState(); |
bsalomon | 8f72733 | 2014-08-05 07:50:06 -0700 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 193 | bool GrDrawState::setIdentityViewMatrix() { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 194 | if (this->numTotalStages()) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 195 | SkMatrix invVM; |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 196 | if (!fViewMatrix.invert(&invVM)) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 197 | // sad trombone sound |
| 198 | return false; |
| 199 | } |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 200 | for (int s = 0; s < this->numColorStages(); ++s) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 201 | fColorStages[s].localCoordChange(invVM); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 202 | } |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 203 | for (int s = 0; s < this->numCoverageStages(); ++s) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 204 | fCoverageStages[s].localCoordChange(invVM); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 207 | this->invalidateOptState(); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 208 | fViewMatrix.reset(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 212 | void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRenderTarget* rt) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 213 | SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 214 | |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 215 | fGeometryProcessor.reset(NULL); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 216 | fColorStages.reset(); |
| 217 | fCoverageStages.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 218 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 219 | for (int i = 0; i < paint.numColorStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 220 | fColorStages.push_back(paint.getColorStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 221 | } |
| 222 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 223 | for (int i = 0; i < paint.numCoverageStages(); ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 224 | fCoverageStages.push_back(paint.getCoverageStage(i)); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 227 | this->setRenderTarget(rt); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 228 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 229 | fViewMatrix = vm; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 230 | |
| 231 | // These have no equivalent in GrPaint, set them to defaults |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 232 | fBlendConstant = 0x0; |
| 233 | fDrawFace = kBoth_DrawFace; |
| 234 | fStencilSettings.setDisabled(); |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 235 | this->resetStateFlags(); |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 236 | fHints = 0; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 237 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 238 | // Enable the clip bit |
| 239 | this->enableState(GrDrawState::kClip_StateBit); |
| 240 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 241 | this->setColor(paint.getColor()); |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 242 | this->setState(GrDrawState::kDither_StateBit, paint.isDither()); |
| 243 | this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 244 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 245 | this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff()); |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 246 | this->setCoverage(paint.getCoverage()); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 247 | this->invalidateOptState(); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 248 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 249 | |
| 250 | //////////////////////////////////////////////////////////////////////////////// |
| 251 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 252 | bool GrDrawState::validateVertexAttribs() const { |
| 253 | // check consistency of effects and attributes |
| 254 | GrSLType slTypes[kMaxVertexAttribCnt]; |
| 255 | for (int i = 0; i < kMaxVertexAttribCnt; ++i) { |
| 256 | slTypes[i] = static_cast<GrSLType>(-1); |
| 257 | } |
| 258 | |
| 259 | if (this->hasGeometryProcessor()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 260 | const GrGeometryProcessor* gp = this->getGeometryProcessor(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 261 | // make sure that any attribute indices have the correct binding type, that the attrib |
| 262 | // type and effect's shader lang type are compatible, and that attributes shared by |
| 263 | // multiple effects use the same shader lang type. |
| 264 | const GrGeometryProcessor::VertexAttribArray& s = gp->getVertexAttribs(); |
| 265 | |
| 266 | int effectIndex = 0; |
| 267 | for (int index = 0; index < fVACount; index++) { |
| 268 | if (kGeometryProcessor_GrVertexAttribBinding != fVAPtr[index].fBinding) { |
| 269 | // we only care about effect bindings |
| 270 | continue; |
| 271 | } |
| 272 | SkASSERT(effectIndex < s.count()); |
| 273 | GrSLType effectSLType = s[effectIndex].getType(); |
| 274 | GrVertexAttribType attribType = fVAPtr[index].fType; |
| 275 | int slVecCount = GrSLTypeVectorCount(effectSLType); |
| 276 | int attribVecCount = GrVertexAttribTypeVectorCount(attribType); |
| 277 | if (slVecCount != attribVecCount || |
| 278 | (static_cast<GrSLType>(-1) != slTypes[index] && slTypes[index] != effectSLType)) { |
| 279 | return false; |
| 280 | } |
| 281 | slTypes[index] = effectSLType; |
| 282 | effectIndex++; |
| 283 | } |
| 284 | // Make sure all attributes are consumed and we were able to find everything |
| 285 | SkASSERT(s.count() == effectIndex); |
| 286 | } |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | //////////////////////////////////////////////////////////////////////////////// |
| 292 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 293 | static void validate_vertex_attribs(const GrVertexAttrib* attribs, int count, size_t stride) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 294 | // this works as long as we're 4 byte-aligned |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 295 | #ifdef SK_DEBUG |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 296 | uint32_t overlapCheck = 0; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 297 | SkASSERT(count <= GrDrawState::kMaxVertexAttribCnt); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 298 | for (int index = 0; index < count; ++index) { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 299 | size_t attribSize = GrVertexAttribTypeSize(attribs[index].fType); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 300 | size_t attribOffset = attribs[index].fOffset; |
| 301 | SkASSERT(attribOffset + attribSize <= stride); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 302 | size_t dwordCount = attribSize >> 2; |
| 303 | uint32_t mask = (1 << dwordCount)-1; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 304 | size_t offsetShift = attribOffset >> 2; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 305 | SkASSERT(!(overlapCheck & (mask << offsetShift))); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 306 | overlapCheck |= (mask << offsetShift); |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame] | 307 | } |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 308 | #endif |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | //////////////////////////////////////////////////////////////////////////////// |
| 312 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 313 | void GrDrawState::internalSetVertexAttribs(const GrVertexAttrib* attribs, int count, |
| 314 | size_t stride) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 315 | SkASSERT(count <= kMaxVertexAttribCnt); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 316 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 317 | fVAPtr = attribs; |
| 318 | fVACount = count; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 319 | fVAStride = stride; |
| 320 | validate_vertex_attribs(fVAPtr, fVACount, fVAStride); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 321 | |
| 322 | // Set all the indices to -1 |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 323 | memset(fFixedFunctionVertexAttribIndices, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 324 | 0xff, |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 325 | sizeof(fFixedFunctionVertexAttribIndices)); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 326 | #ifdef SK_DEBUG |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 327 | uint32_t overlapCheck = 0; |
| 328 | #endif |
| 329 | for (int i = 0; i < count; ++i) { |
| 330 | if (attribs[i].fBinding < kGrFixedFunctionVertexAttribBindingCnt) { |
| 331 | // The fixed function attribs can only be specified once |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 332 | SkASSERT(-1 == fFixedFunctionVertexAttribIndices[attribs[i].fBinding]); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 333 | SkASSERT(GrFixedFunctionVertexAttribVectorCount(attribs[i].fBinding) == |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 334 | GrVertexAttribTypeVectorCount(attribs[i].fType)); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 335 | fFixedFunctionVertexAttribIndices[attribs[i].fBinding] = i; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 336 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 337 | #ifdef SK_DEBUG |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 338 | size_t dwordCount = GrVertexAttribTypeSize(attribs[i].fType) >> 2; |
| 339 | uint32_t mask = (1 << dwordCount)-1; |
| 340 | size_t offsetShift = attribs[i].fOffset >> 2; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 341 | SkASSERT(!(overlapCheck & (mask << offsetShift))); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 342 | overlapCheck |= (mask << offsetShift); |
| 343 | #endif |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 344 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 345 | this->invalidateOptState(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 346 | // Positions must be specified. |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 347 | SkASSERT(-1 != fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding]); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | //////////////////////////////////////////////////////////////////////////////// |
| 351 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 352 | void GrDrawState::setDefaultVertexAttribs() { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 353 | static const GrVertexAttrib kPositionAttrib = |
| 354 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}; |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 355 | |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 356 | fVAPtr = &kPositionAttrib; |
| 357 | fVACount = 1; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 358 | fVAStride = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 359 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 360 | // set all the fixed function indices to -1 except position. |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 361 | memset(fFixedFunctionVertexAttribIndices, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 362 | 0xff, |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 363 | sizeof(fFixedFunctionVertexAttribIndices)); |
| 364 | fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding] = 0; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 365 | this->invalidateOptState(); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | //////////////////////////////////////////////////////////////////////////////// |
| 369 | |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 370 | bool GrDrawState::couldApplyCoverage(const GrDrawTargetCaps& caps) const { |
| 371 | if (caps.dualSourceBlendingSupport()) { |
| 372 | return true; |
| 373 | } |
| 374 | // we can correctly apply coverage if a) we have dual source blending |
| 375 | // or b) one of our blend optimizations applies |
| 376 | // or c) the src, dst blend coeffs are 1,0 and we will read Dst Color |
| 377 | GrBlendCoeff srcCoeff; |
| 378 | GrBlendCoeff dstCoeff; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 379 | BlendOptFlags flag = this->getBlendOpts(true, &srcCoeff, &dstCoeff); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 380 | return GrDrawState::kNone_BlendOpt != flag || |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 381 | (this->willEffectReadDstColor() && |
| 382 | kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff); |
| 383 | } |
| 384 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 385 | bool GrDrawState::hasSolidCoverage() const { |
| 386 | // If we're drawing coverage directly then coverage is effectively treated as color. |
| 387 | if (this->isCoverageDrawing()) { |
| 388 | return true; |
| 389 | } |
| 390 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 391 | GrProcessor::InvariantOutput inout; |
| 392 | inout.fIsSingleComponent = false; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 393 | // Initialize to an unknown starting coverage if per-vertex coverage is specified. |
| 394 | if (this->hasCoverageVertexAttribute()) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 395 | inout.fValidFlags = 0; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 396 | } else { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 397 | inout.fColor = this->getCoverageColor(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 398 | inout.fValidFlags = kRGBA_GrColorComponentFlags; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Run through the coverage stages and see if the coverage will be all ones at the end. |
| 402 | if (this->hasGeometryProcessor()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 403 | fGeometryProcessor->computeInvariantOutput(&inout); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 404 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 405 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 406 | for (int s = 0; s < this->numCoverageStages(); ++s) { |
| 407 | const GrProcessor* processor = this->getCoverageStage(s).getProcessor(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 408 | processor->computeInvariantOutput(&inout); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 409 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 410 | return inout.isSolidWhite(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 411 | } |
| 412 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 413 | ////////////////////////////////////////////////////////////////////////////// |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 414 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 415 | GrDrawState::AutoVertexAttribRestore::AutoVertexAttribRestore(GrDrawState* drawState) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 416 | SkASSERT(drawState); |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 417 | fDrawState = drawState; |
| 418 | fVAPtr = drawState->fVAPtr; |
| 419 | fVACount = drawState->fVACount; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 420 | fVAStride = drawState->fVAStride; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 421 | fDrawState->setDefaultVertexAttribs(); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 422 | } |
| 423 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 424 | //////////////////////////////////////////////////////////////////////////////s |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 425 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 426 | bool GrDrawState::willEffectReadDstColor() const { |
| 427 | if (!this->isColorWriteDisabled()) { |
| 428 | for (int s = 0; s < this->numColorStages(); ++s) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 429 | if (this->getColorStage(s).getProcessor()->willReadDstColor()) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | for (int s = 0; s < this->numCoverageStages(); ++s) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 435 | if (this->getCoverageStage(s).getProcessor()->willReadDstColor()) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | } |
| 439 | return false; |
| 440 | } |
| 441 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 442 | void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 443 | if (fDrawState) { |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 444 | // See the big comment on the class definition about GPs. |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 445 | if (SK_InvalidUniqueID == fOriginalGPID) { |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 446 | fDrawState->fGeometryProcessor.reset(NULL); |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 447 | } else { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 448 | SkASSERT(fDrawState->getGeometryProcessor()->getUniqueID() == |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 449 | fOriginalGPID); |
| 450 | fOriginalGPID = SK_InvalidUniqueID; |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 451 | } |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 452 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 453 | int m = fDrawState->numColorStages() - fColorEffectCnt; |
| 454 | SkASSERT(m >= 0); |
| 455 | fDrawState->fColorStages.pop_back_n(m); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 456 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 457 | int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
| 458 | SkASSERT(n >= 0); |
| 459 | fDrawState->fCoverageStages.pop_back_n(n); |
| 460 | if (m + n > 0) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 461 | fDrawState->invalidateOptState(); |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 462 | } |
| 463 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 464 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 465 | fDrawState = ds; |
| 466 | if (NULL != ds) { |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 467 | SkASSERT(SK_InvalidUniqueID == fOriginalGPID); |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 468 | if (NULL != ds->getGeometryProcessor()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 469 | fOriginalGPID = ds->getGeometryProcessor()->getUniqueID(); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 470 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 471 | fColorEffectCnt = ds->numColorStages(); |
| 472 | fCoverageEffectCnt = ds->numCoverageStages(); |
| 473 | SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 474 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 475 | } |
| 476 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 477 | //////////////////////////////////////////////////////////////////////////////// |
| 478 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 479 | // Some blend modes allow folding a fractional coverage value into the color's alpha channel, while |
| 480 | // others will blend incorrectly. |
| 481 | bool GrDrawState::canTweakAlphaForCoverage() const { |
| 482 | /* |
| 483 | The fractional coverage is f. |
| 484 | The src and dst coeffs are Cs and Cd. |
| 485 | The dst and src colors are S and D. |
| 486 | We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D. By tweaking the source color's alpha |
| 487 | we're replacing S with S'=fS. It's obvious that that first term will always be ok. The second |
| 488 | term can be rearranged as [1-(1-Cd)f]D. By substituting in the various possibilities for Cd we |
| 489 | find that only 1, ISA, and ISC produce the correct destination when applied to S' and D. |
| 490 | Also, if we're directly rendering coverage (isCoverageDrawing) then coverage is treated as |
| 491 | color by definition. |
| 492 | */ |
| 493 | return kOne_GrBlendCoeff == fDstBlend || |
| 494 | kISA_GrBlendCoeff == fDstBlend || |
| 495 | kISC_GrBlendCoeff == fDstBlend || |
| 496 | this->isCoverageDrawing(); |
| 497 | } |
| 498 | |
| 499 | //////////////////////////////////////////////////////////////////////////////// |
| 500 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 501 | void GrDrawState::AutoViewMatrixRestore::restore() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 502 | if (fDrawState) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 503 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 504 | fDrawState->fViewMatrix = fViewMatrix; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 505 | SkASSERT(fDrawState->numColorStages() >= fNumColorStages); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 506 | int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 507 | SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 508 | |
| 509 | int i = 0; |
| 510 | for (int s = 0; s < fNumColorStages; ++s, ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 511 | fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 512 | } |
| 513 | for (int s = 0; s < numCoverageStages; ++s, ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 514 | fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges[i]); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 515 | } |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 516 | fDrawState->invalidateOptState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 517 | fDrawState = NULL; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 518 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 522 | const SkMatrix& preconcatMatrix) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 523 | this->restore(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 524 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 525 | SkASSERT(NULL == fDrawState); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 526 | if (NULL == drawState || preconcatMatrix.isIdentity()) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 527 | return; |
| 528 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 529 | fDrawState = drawState; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 530 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 531 | fViewMatrix = drawState->getViewMatrix(); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 532 | drawState->fViewMatrix.preConcat(preconcatMatrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 533 | |
| 534 | this->doEffectCoordChanges(preconcatMatrix); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 535 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 536 | drawState->invalidateOptState(); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 537 | } |
| 538 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 539 | bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 540 | this->restore(); |
| 541 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 542 | if (NULL == drawState) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 543 | return false; |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 544 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 545 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 546 | if (drawState->getViewMatrix().isIdentity()) { |
| 547 | return true; |
| 548 | } |
| 549 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 550 | drawState->invalidateOptState(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 551 | fViewMatrix = drawState->getViewMatrix(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 552 | if (0 == drawState->numTotalStages()) { |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 553 | drawState->fViewMatrix.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 554 | fDrawState = drawState; |
| 555 | fNumColorStages = 0; |
| 556 | fSavedCoordChanges.reset(0); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 557 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 558 | return true; |
| 559 | } else { |
| 560 | SkMatrix inv; |
| 561 | if (!fViewMatrix.invert(&inv)) { |
| 562 | return false; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 563 | } |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 564 | drawState->fViewMatrix.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 565 | fDrawState = drawState; |
| 566 | this->doEffectCoordChanges(inv); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 567 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 568 | return true; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 569 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void GrDrawState::AutoViewMatrixRestore::doEffectCoordChanges(const SkMatrix& coordChangeMatrix) { |
| 573 | fSavedCoordChanges.reset(fDrawState->numTotalStages()); |
| 574 | int i = 0; |
| 575 | |
| 576 | fNumColorStages = fDrawState->numColorStages(); |
| 577 | for (int s = 0; s < fNumColorStages; ++s, ++i) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 578 | fDrawState->getColorStage(s).saveCoordChange(&fSavedCoordChanges[i]); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 579 | fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 580 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 581 | |
| 582 | int numCoverageStages = fDrawState->numCoverageStages(); |
| 583 | for (int s = 0; s < numCoverageStages; ++s, ++i) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 584 | fDrawState->getCoverageStage(s).saveCoordChange(&fSavedCoordChanges[i]); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 585 | fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 586 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 587 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 588 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 589 | //////////////////////////////////////////////////////////////////////////////// |
| 590 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 591 | void GrDrawState::convertToPendingExec() { |
| 592 | fRenderTarget.markPendingIO(); |
| 593 | fRenderTarget.removeRef(); |
| 594 | for (int i = 0; i < fColorStages.count(); ++i) { |
| 595 | fColorStages[i].convertToPendingExec(); |
| 596 | } |
| 597 | if (fGeometryProcessor) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 598 | fGeometryProcessor.convertToPendingExec(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 599 | } |
| 600 | for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 601 | fCoverageStages[i].convertToPendingExec(); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | //////////////////////////////////////////////////////////////////////////////// |
| 606 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 607 | void GrDrawState::invalidateOptState() const { |
| 608 | SkSafeSetNull(fCachedOptState); |
| 609 | } |
| 610 | |
| 611 | //////////////////////////////////////////////////////////////////////////////// |
| 612 | |
| 613 | GrDrawState::~GrDrawState() { |
| 614 | SkSafeUnref(fCachedOptState); |
| 615 | SkASSERT(0 == fBlockEffectRemovalCnt); |
| 616 | } |
| 617 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 618 | //////////////////////////////////////////////////////////////////////////////// |
| 619 | |
| 620 | GrDrawState::BlendOptFlags GrDrawState::getBlendOpts(bool forceCoverage, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 621 | GrBlendCoeff* srcCoeff, |
| 622 | GrBlendCoeff* dstCoeff) const { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 623 | GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; |
| 624 | if (NULL == srcCoeff) { |
| 625 | srcCoeff = &bogusSrcCoeff; |
| 626 | } |
| 627 | if (NULL == dstCoeff) { |
| 628 | dstCoeff = &bogusDstCoeff; |
| 629 | } |
| 630 | |
| 631 | *srcCoeff = this->getSrcBlendCoeff(); |
| 632 | *dstCoeff = this->getDstBlendCoeff(); |
| 633 | |
| 634 | if (this->isColorWriteDisabled()) { |
| 635 | *srcCoeff = kZero_GrBlendCoeff; |
| 636 | *dstCoeff = kOne_GrBlendCoeff; |
| 637 | } |
| 638 | |
| 639 | bool srcAIsOne = this->srcAlphaWillBeOne(); |
| 640 | bool dstCoeffIsOne = kOne_GrBlendCoeff == *dstCoeff || |
| 641 | (kSA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 642 | bool dstCoeffIsZero = kZero_GrBlendCoeff == *dstCoeff || |
| 643 | (kISA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 644 | |
| 645 | // When coeffs are (0,1) there is no reason to draw at all, unless |
| 646 | // stenciling is enabled. Having color writes disabled is effectively |
| 647 | // (0,1). |
| 648 | if ((kZero_GrBlendCoeff == *srcCoeff && dstCoeffIsOne)) { |
| 649 | if (this->getStencil().doesWrite()) { |
| 650 | return kEmitCoverage_BlendOptFlag; |
| 651 | } else { |
| 652 | *dstCoeff = kOne_GrBlendCoeff; |
| 653 | return kSkipDraw_BlendOptFlag; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | bool hasCoverage = forceCoverage || !this->hasSolidCoverage(); |
| 658 | |
| 659 | // if we don't have coverage we can check whether the dst |
| 660 | // has to read at all. If not, we'll disable blending. |
| 661 | if (!hasCoverage) { |
| 662 | if (dstCoeffIsZero) { |
| 663 | if (kOne_GrBlendCoeff == *srcCoeff) { |
| 664 | // if there is no coverage and coeffs are (1,0) then we |
| 665 | // won't need to read the dst at all, it gets replaced by src |
| 666 | *dstCoeff = kZero_GrBlendCoeff; |
| 667 | return kNone_BlendOpt; |
| 668 | } else if (kZero_GrBlendCoeff == *srcCoeff) { |
| 669 | // if the op is "clear" then we don't need to emit a color |
| 670 | // or blend, just write transparent black into the dst. |
| 671 | *srcCoeff = kOne_GrBlendCoeff; |
| 672 | *dstCoeff = kZero_GrBlendCoeff; |
| 673 | return kEmitTransBlack_BlendOptFlag; |
| 674 | } |
| 675 | } |
| 676 | } else if (this->isCoverageDrawing()) { |
| 677 | // we have coverage but we aren't distinguishing it from alpha by request. |
| 678 | return kCoverageAsAlpha_BlendOptFlag; |
| 679 | } else { |
| 680 | // check whether coverage can be safely rolled into alpha |
| 681 | // of if we can skip color computation and just emit coverage |
| 682 | if (this->canTweakAlphaForCoverage()) { |
| 683 | return kCoverageAsAlpha_BlendOptFlag; |
| 684 | } |
| 685 | if (dstCoeffIsZero) { |
| 686 | if (kZero_GrBlendCoeff == *srcCoeff) { |
| 687 | // the source color is not included in the blend |
| 688 | // the dst coeff is effectively zero so blend works out to: |
| 689 | // (c)(0)D + (1-c)D = (1-c)D. |
| 690 | *dstCoeff = kISA_GrBlendCoeff; |
| 691 | return kEmitCoverage_BlendOptFlag; |
| 692 | } else if (srcAIsOne) { |
| 693 | // the dst coeff is effectively zero so blend works out to: |
| 694 | // cS + (c)(0)D + (1-c)D = cS + (1-c)D. |
| 695 | // If Sa is 1 then we can replace Sa with c |
| 696 | // and set dst coeff to 1-Sa. |
| 697 | *dstCoeff = kISA_GrBlendCoeff; |
| 698 | return kCoverageAsAlpha_BlendOptFlag; |
| 699 | } |
| 700 | } else if (dstCoeffIsOne) { |
| 701 | // the dst coeff is effectively one so blend works out to: |
| 702 | // cS + (c)(1)D + (1-c)D = cS + D. |
| 703 | *dstCoeff = kOne_GrBlendCoeff; |
| 704 | return kCoverageAsAlpha_BlendOptFlag; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | return kNone_BlendOpt; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | bool GrDrawState::srcAlphaWillBeOne() const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 713 | GrProcessor::InvariantOutput inoutColor; |
| 714 | inoutColor.fIsSingleComponent = false; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 715 | // Check if per-vertex or constant color may have partial alpha |
| 716 | if (this->hasColorVertexAttribute()) { |
| 717 | if (fHints & kVertexColorsAreOpaque_Hint) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 718 | inoutColor.fValidFlags = kA_GrColorComponentFlag; |
| 719 | inoutColor.fColor = 0xFF << GrColor_SHIFT_A; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 720 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 721 | inoutColor.fValidFlags = 0; |
| 722 | // not strictly necessary but we get false alarms from tools about uninit. |
| 723 | inoutColor.fColor = 0; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 724 | } |
| 725 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 726 | inoutColor.fValidFlags = kRGBA_GrColorComponentFlags; |
| 727 | inoutColor.fColor = this->getColor(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Run through the color stages |
| 731 | for (int s = 0; s < this->numColorStages(); ++s) { |
| 732 | const GrProcessor* processor = this->getColorStage(s).getProcessor(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 733 | processor->computeInvariantOutput(&inoutColor); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | // Check whether coverage is treated as color. If so we run through the coverage computation. |
| 737 | if (this->isCoverageDrawing()) { |
| 738 | // The shader generated for coverage drawing runs the full coverage computation and then |
| 739 | // makes the shader output be the multiplication of color and coverage. We mirror that here. |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 740 | GrProcessor::InvariantOutput inoutCoverage; |
| 741 | inoutCoverage.fIsSingleComponent = false; |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 742 | if (this->hasCoverageVertexAttribute()) { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 743 | inoutCoverage.fValidFlags = 0; |
| 744 | inoutCoverage.fColor = 0; // suppresses any warnings. |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 745 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 746 | inoutCoverage.fValidFlags = kRGBA_GrColorComponentFlags; |
| 747 | inoutCoverage.fColor = this->getCoverageColor(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // Run through the coverage stages |
| 751 | for (int s = 0; s < this->numCoverageStages(); ++s) { |
| 752 | const GrProcessor* processor = this->getCoverageStage(s).getProcessor(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 753 | processor->computeInvariantOutput(&inoutCoverage); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Since the shader will multiply coverage and color, the only way the final A==1 is if |
| 757 | // coverage and color both have A==1. |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 758 | return (inoutColor.isOpaque() && inoutCoverage.isOpaque()); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 759 | } |
| 760 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 761 | return inoutColor.isOpaque(); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 762 | } |
| 763 | |