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