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