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