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