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