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