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) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame^] | 290 | SkASSERT(drawState); |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 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) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame^] | 301 | if (fDrawState) { |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 302 | // See the big comment on the class definition about GPs. |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 303 | if (SK_InvalidUniqueID == fOriginalGPID) { |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 304 | fDrawState->fGeometryProcessor.reset(NULL); |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 305 | } else { |
| 306 | SkASSERT(fDrawState->getGeometryProcessor()->getEffect()->getUniqueID() == |
| 307 | fOriginalGPID); |
| 308 | fOriginalGPID = SK_InvalidUniqueID; |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 309 | } |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 310 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 311 | int m = fDrawState->numColorStages() - fColorEffectCnt; |
| 312 | SkASSERT(m >= 0); |
| 313 | fDrawState->fColorStages.pop_back_n(m); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 314 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 315 | int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
| 316 | SkASSERT(n >= 0); |
| 317 | fDrawState->fCoverageStages.pop_back_n(n); |
| 318 | if (m + n > 0) { |
| 319 | fDrawState->invalidateBlendOptFlags(); |
| 320 | } |
| 321 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 322 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 323 | fDrawState = ds; |
| 324 | if (NULL != ds) { |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 325 | SkASSERT(SK_InvalidUniqueID == fOriginalGPID); |
bsalomon | 9b53652 | 2014-09-05 09:18:51 -0700 | [diff] [blame] | 326 | if (NULL != ds->getGeometryProcessor()) { |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 327 | fOriginalGPID = ds->getGeometryProcessor()->getEffect()->getUniqueID(); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 328 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 329 | fColorEffectCnt = ds->numColorStages(); |
| 330 | fCoverageEffectCnt = ds->numCoverageStages(); |
| 331 | SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 332 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 333 | } |
| 334 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 335 | //////////////////////////////////////////////////////////////////////////////// |
| 336 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 337 | GrRODrawState::BlendOptFlags GrDrawState::getBlendOpts(bool forceCoverage, |
| 338 | GrBlendCoeff* srcCoeff, |
| 339 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 340 | GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; |
| 341 | if (NULL == srcCoeff) { |
| 342 | srcCoeff = &bogusSrcCoeff; |
| 343 | } |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 344 | if (NULL == dstCoeff) { |
| 345 | dstCoeff = &bogusDstCoeff; |
| 346 | } |
egdaniel | 9514d24 | 2014-07-18 06:15:43 -0700 | [diff] [blame] | 347 | |
| 348 | if (forceCoverage) { |
| 349 | return this->calcBlendOpts(true, srcCoeff, dstCoeff); |
| 350 | } |
| 351 | |
| 352 | if (0 == (fBlendOptFlags & kInvalid_BlendOptFlag)) { |
| 353 | *srcCoeff = fOptSrcBlend; |
| 354 | *dstCoeff = fOptDstBlend; |
| 355 | return fBlendOptFlags; |
| 356 | } |
| 357 | |
| 358 | fBlendOptFlags = this->calcBlendOpts(forceCoverage, srcCoeff, dstCoeff); |
| 359 | fOptSrcBlend = *srcCoeff; |
| 360 | fOptDstBlend = *dstCoeff; |
| 361 | |
| 362 | return fBlendOptFlags; |
| 363 | } |
| 364 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 365 | GrRODrawState::BlendOptFlags GrDrawState::calcBlendOpts(bool forceCoverage, |
| 366 | GrBlendCoeff* srcCoeff, |
| 367 | GrBlendCoeff* dstCoeff) const { |
egdaniel | 9514d24 | 2014-07-18 06:15:43 -0700 | [diff] [blame] | 368 | *srcCoeff = this->getSrcBlendCoeff(); |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 369 | *dstCoeff = this->getDstBlendCoeff(); |
| 370 | |
| 371 | if (this->isColorWriteDisabled()) { |
| 372 | *srcCoeff = kZero_GrBlendCoeff; |
| 373 | *dstCoeff = kOne_GrBlendCoeff; |
| 374 | } |
| 375 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 376 | bool srcAIsOne = this->srcAlphaWillBeOne(); |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 377 | bool dstCoeffIsOne = kOne_GrBlendCoeff == *dstCoeff || |
| 378 | (kSA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 379 | bool dstCoeffIsZero = kZero_GrBlendCoeff == *dstCoeff || |
| 380 | (kISA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 381 | |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 382 | // When coeffs are (0,1) there is no reason to draw at all, unless |
| 383 | // stenciling is enabled. Having color writes disabled is effectively |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 384 | // (0,1). |
| 385 | if ((kZero_GrBlendCoeff == *srcCoeff && dstCoeffIsOne)) { |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 386 | if (this->getStencil().doesWrite()) { |
egdaniel | 0f1a7c4 | 2014-07-30 13:18:32 -0700 | [diff] [blame] | 387 | return kEmitCoverage_BlendOptFlag; |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 388 | } else { |
| 389 | return kSkipDraw_BlendOptFlag; |
| 390 | } |
| 391 | } |
| 392 | |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 393 | bool hasCoverage = forceCoverage || !this->hasSolidCoverage(); |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 394 | |
| 395 | // if we don't have coverage we can check whether the dst |
| 396 | // has to read at all. If not, we'll disable blending. |
| 397 | if (!hasCoverage) { |
| 398 | if (dstCoeffIsZero) { |
| 399 | if (kOne_GrBlendCoeff == *srcCoeff) { |
| 400 | // if there is no coverage and coeffs are (1,0) then we |
| 401 | // 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] | 402 | *dstCoeff = kZero_GrBlendCoeff; |
| 403 | return kNone_BlendOpt; |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 404 | } else if (kZero_GrBlendCoeff == *srcCoeff) { |
| 405 | // if the op is "clear" then we don't need to emit a color |
| 406 | // or blend, just write transparent black into the dst. |
| 407 | *srcCoeff = kOne_GrBlendCoeff; |
| 408 | *dstCoeff = kZero_GrBlendCoeff; |
egdaniel | 0f1a7c4 | 2014-07-30 13:18:32 -0700 | [diff] [blame] | 409 | return kEmitTransBlack_BlendOptFlag; |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } else if (this->isCoverageDrawing()) { |
| 413 | // we have coverage but we aren't distinguishing it from alpha by request. |
| 414 | return kCoverageAsAlpha_BlendOptFlag; |
| 415 | } else { |
| 416 | // check whether coverage can be safely rolled into alpha |
| 417 | // of if we can skip color computation and just emit coverage |
| 418 | if (this->canTweakAlphaForCoverage()) { |
| 419 | return kCoverageAsAlpha_BlendOptFlag; |
| 420 | } |
| 421 | if (dstCoeffIsZero) { |
| 422 | if (kZero_GrBlendCoeff == *srcCoeff) { |
| 423 | // the source color is not included in the blend |
| 424 | // the dst coeff is effectively zero so blend works out to: |
| 425 | // (c)(0)D + (1-c)D = (1-c)D. |
| 426 | *dstCoeff = kISA_GrBlendCoeff; |
| 427 | return kEmitCoverage_BlendOptFlag; |
| 428 | } else if (srcAIsOne) { |
| 429 | // the dst coeff is effectively zero so blend works out to: |
| 430 | // cS + (c)(0)D + (1-c)D = cS + (1-c)D. |
| 431 | // If Sa is 1 then we can replace Sa with c |
| 432 | // and set dst coeff to 1-Sa. |
| 433 | *dstCoeff = kISA_GrBlendCoeff; |
| 434 | return kCoverageAsAlpha_BlendOptFlag; |
| 435 | } |
| 436 | } else if (dstCoeffIsOne) { |
| 437 | // the dst coeff is effectively one so blend works out to: |
| 438 | // cS + (c)(1)D + (1-c)D = cS + D. |
| 439 | *dstCoeff = kOne_GrBlendCoeff; |
| 440 | return kCoverageAsAlpha_BlendOptFlag; |
| 441 | } |
| 442 | } |
egdaniel | 0f1a7c4 | 2014-07-30 13:18:32 -0700 | [diff] [blame] | 443 | |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 444 | return kNone_BlendOpt; |
| 445 | } |
| 446 | |
| 447 | //////////////////////////////////////////////////////////////////////////////// |
| 448 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 449 | void GrDrawState::AutoViewMatrixRestore::restore() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame^] | 450 | if (fDrawState) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 451 | SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 452 | fDrawState->fViewMatrix = fViewMatrix; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 453 | SkASSERT(fDrawState->numColorStages() >= fNumColorStages); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 454 | int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 455 | SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 456 | |
| 457 | int i = 0; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 458 | if (fHasGeometryProcessor) { |
| 459 | SkASSERT(fDrawState->hasGeometryProcessor()); |
| 460 | fDrawState->fGeometryProcessor->restoreCoordChange(fSavedCoordChanges[i++]); |
| 461 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 462 | for (int s = 0; s < fNumColorStages; ++s, ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 463 | fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 464 | } |
| 465 | for (int s = 0; s < numCoverageStages; ++s, ++i) { |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 466 | fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges[i]); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 467 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 468 | fDrawState = NULL; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 469 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 473 | const SkMatrix& preconcatMatrix) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 474 | this->restore(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 475 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 476 | SkASSERT(NULL == fDrawState); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 477 | if (NULL == drawState || preconcatMatrix.isIdentity()) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 478 | return; |
| 479 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 480 | fDrawState = drawState; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 481 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 482 | fViewMatrix = drawState->getViewMatrix(); |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 483 | drawState->fViewMatrix.preConcat(preconcatMatrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 484 | |
| 485 | this->doEffectCoordChanges(preconcatMatrix); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 486 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 487 | } |
| 488 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 489 | bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 490 | this->restore(); |
| 491 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 492 | if (NULL == drawState) { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 493 | return false; |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 494 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 495 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 496 | if (drawState->getViewMatrix().isIdentity()) { |
| 497 | return true; |
| 498 | } |
| 499 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 500 | fViewMatrix = drawState->getViewMatrix(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 501 | if (0 == drawState->numTotalStages()) { |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 502 | drawState->fViewMatrix.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 503 | fDrawState = drawState; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 504 | fHasGeometryProcessor = false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 505 | fNumColorStages = 0; |
| 506 | fSavedCoordChanges.reset(0); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 507 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 508 | return true; |
| 509 | } else { |
| 510 | SkMatrix inv; |
| 511 | if (!fViewMatrix.invert(&inv)) { |
| 512 | return false; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 513 | } |
bsalomon | 2ed5ef8 | 2014-07-07 08:44:05 -0700 | [diff] [blame] | 514 | drawState->fViewMatrix.reset(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 515 | fDrawState = drawState; |
| 516 | this->doEffectCoordChanges(inv); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 517 | SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 518 | return true; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 519 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void GrDrawState::AutoViewMatrixRestore::doEffectCoordChanges(const SkMatrix& coordChangeMatrix) { |
| 523 | fSavedCoordChanges.reset(fDrawState->numTotalStages()); |
| 524 | int i = 0; |
| 525 | |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 526 | fHasGeometryProcessor = false; |
| 527 | if (fDrawState->hasGeometryProcessor()) { |
| 528 | fDrawState->fGeometryProcessor->saveCoordChange(&fSavedCoordChanges[i++]); |
| 529 | fDrawState->fGeometryProcessor->localCoordChange(coordChangeMatrix); |
| 530 | fHasGeometryProcessor = true; |
| 531 | } |
| 532 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 533 | fNumColorStages = fDrawState->numColorStages(); |
| 534 | for (int s = 0; s < fNumColorStages; ++s, ++i) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 535 | fDrawState->getColorStage(s).saveCoordChange(&fSavedCoordChanges[i]); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 536 | fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 537 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 538 | |
| 539 | int numCoverageStages = fDrawState->numCoverageStages(); |
| 540 | for (int s = 0; s < numCoverageStages; ++s, ++i) { |
egdaniel | 776bdbd | 2014-08-06 11:07:02 -0700 | [diff] [blame] | 541 | fDrawState->getCoverageStage(s).saveCoordChange(&fSavedCoordChanges[i]); |
egdaniel | 8cbf3d5 | 2014-08-21 06:27:22 -0700 | [diff] [blame] | 542 | fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 543 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 544 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 545 | |
| 546 | bool GrDrawState::srcAlphaWillBeOne() const { |
| 547 | uint32_t validComponentFlags; |
| 548 | GrColor color; |
| 549 | // Check if per-vertex or constant color may have partial alpha |
| 550 | if (this->hasColorVertexAttribute()) { |
| 551 | if (fHints & kVertexColorsAreOpaque_Hint) { |
| 552 | validComponentFlags = kA_GrColorComponentFlag; |
| 553 | color = 0xFF << GrColor_SHIFT_A; |
| 554 | } else { |
| 555 | validComponentFlags = 0; |
| 556 | color = 0; // not strictly necessary but we get false alarms from tools about uninit. |
| 557 | } |
| 558 | } else { |
| 559 | validComponentFlags = kRGBA_GrColorComponentFlags; |
| 560 | color = this->getColor(); |
| 561 | } |
| 562 | |
| 563 | // Run through the color stages |
| 564 | for (int s = 0; s < this->numColorStages(); ++s) { |
| 565 | const GrEffect* effect = this->getColorStage(s).getEffect(); |
| 566 | effect->getConstantColorComponents(&color, &validComponentFlags); |
| 567 | } |
| 568 | |
| 569 | // Check whether coverage is treated as color. If so we run through the coverage computation. |
| 570 | if (this->isCoverageDrawing()) { |
| 571 | // The shader generated for coverage drawing runs the full coverage computation and then |
| 572 | // makes the shader output be the multiplication of color and coverage. We mirror that here. |
| 573 | GrColor coverage; |
| 574 | uint32_t coverageComponentFlags; |
| 575 | if (this->hasCoverageVertexAttribute()) { |
| 576 | coverageComponentFlags = 0; |
| 577 | coverage = 0; // suppresses any warnings. |
| 578 | } else { |
| 579 | coverageComponentFlags = kRGBA_GrColorComponentFlags; |
| 580 | coverage = this->getCoverageColor(); |
| 581 | } |
| 582 | |
| 583 | // Run through the coverage stages |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 584 | if (this->hasGeometryProcessor()) { |
| 585 | const GrEffect* effect = fGeometryProcessor->getEffect(); |
| 586 | effect->getConstantColorComponents(&coverage, &coverageComponentFlags); |
| 587 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 588 | for (int s = 0; s < this->numCoverageStages(); ++s) { |
| 589 | const GrEffect* effect = this->getCoverageStage(s).getEffect(); |
| 590 | effect->getConstantColorComponents(&coverage, &coverageComponentFlags); |
| 591 | } |
| 592 | |
| 593 | // Since the shader will multiply coverage and color, the only way the final A==1 is if |
| 594 | // coverage and color both have A==1. |
| 595 | return (kA_GrColorComponentFlag & validComponentFlags & coverageComponentFlags) && |
| 596 | 0xFF == GrColorUnpackA(color) && 0xFF == GrColorUnpackA(coverage); |
| 597 | |
| 598 | } |
| 599 | |
| 600 | return (kA_GrColorComponentFlag & validComponentFlags) && 0xFF == GrColorUnpackA(color); |
| 601 | } |
| 602 | |
| 603 | //////////////////////////////////////////////////////////////////////////////// |
| 604 | |
| 605 | bool GrDrawState::canIgnoreColorAttribute() const { |
| 606 | if (fBlendOptFlags & kInvalid_BlendOptFlag) { |
| 607 | this->getBlendOpts(); |
| 608 | } |
| 609 | return SkToBool(fBlendOptFlags & (GrRODrawState::kEmitTransBlack_BlendOptFlag | |
| 610 | GrRODrawState::kEmitCoverage_BlendOptFlag)); |
| 611 | } |
| 612 | |