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