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