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