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