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