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