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 | |
| 11 | void GrDrawState::setFromPaint(const GrPaint& paint) { |
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 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 37 | this->setColor(paint.getColor()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 39 | this->setState(GrDrawState::kDither_StateBit, paint.isDither()); |
| 40 | this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 42 | this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff()); |
| 43 | this->setColorFilter(paint.getColorFilterColor(), paint.getColorFilterMode()); |
| 44 | this->setCoverage(paint.getCoverage()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 45 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 46 | |
| 47 | //////////////////////////////////////////////////////////////////////////////// |
| 48 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | /** |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 52 | * This function generates a mask that we like to have known at compile |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 53 | * time. When the number of stages is bumped or the way bits are defined in |
| 54 | * GrDrawState.h changes this function should be rerun to generate the new mask. |
| 55 | * (We attempted to force the compiler to generate the mask using recursive |
| 56 | * templates but always wound up with static initializers under gcc, even if |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 57 | * they were just a series of immediate->memory moves.) |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 58 | * |
| 59 | */ |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 60 | void gen_tex_coord_mask(GrAttribBindings* texCoordMask) { |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 61 | *texCoordMask = 0; |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 62 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 63 | *texCoordMask |= GrDrawState::ExplicitTexCoordAttribBindingsBit(s); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 67 | const GrAttribBindings kTexCoord_AttribBindingsMask = (1 << GrDrawState::kNumStages)-1; |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 68 | |
| 69 | } //unnamed namespace |
| 70 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 71 | const size_t GrDrawState::kVertexAttribSizes[kGrVertexAttribTypeCount] = { |
| 72 | sizeof(float), // kFloat_GrVertexAttribType |
| 73 | 2*sizeof(float), // kVec2_GrVertexAttribType |
| 74 | 3*sizeof(float), // kVec3_GrVertexAttribType |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame^] | 75 | 4*sizeof(float), // kVec4_GrVertexAttribType |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 76 | 4*sizeof(char) // kCVec4_GrVertexAttribType |
| 77 | }; |
bsalomon@google.com | 8598328 | 2013-02-07 22:00:29 +0000 | [diff] [blame] | 78 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 79 | static size_t vertex_size(const GrVertexAttrib* attribs, int count) { |
| 80 | // this works as long as we're 4 byte-aligned |
| 81 | #if GR_DEBUG |
| 82 | uint32_t overlapCheck = 0; |
| 83 | #endif |
| 84 | GrAssert(count <= GrDrawState::kAttribIndexCount); |
| 85 | size_t size = 0; |
| 86 | for (int index = 0; index < count; ++index) { |
| 87 | size_t attribSize = GrDrawState::kVertexAttribSizes[attribs[index].fType]; |
| 88 | size += attribSize; |
| 89 | #if GR_DEBUG |
| 90 | size_t dwordCount = attribSize >> 2; |
| 91 | uint32_t mask = (1 << dwordCount)-1; |
| 92 | size_t offsetShift = attribs[index].fOffset >> 2; |
| 93 | GrAssert(!(overlapCheck & (mask << offsetShift))); |
| 94 | overlapCheck |= (mask << offsetShift); |
| 95 | #endif |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 96 | } |
| 97 | return size; |
| 98 | } |
| 99 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 100 | size_t GrDrawState::getVertexSize() const { |
| 101 | return vertex_size(fVertexAttribs.begin(), fVertexAttribs.count()); |
| 102 | } |
| 103 | |
| 104 | const GrAttribBindings GrDrawState::kAttribIndexMasks[kAttribIndexCount] = { |
| 105 | 0, // position is not reflected in the bindings |
| 106 | kColor_AttribBindingsBit, |
| 107 | kCoverage_AttribBindingsBit, |
| 108 | kEdge_AttribBindingsBit, |
| 109 | kTexCoord_AttribBindingsMask |
| 110 | }; |
| 111 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 112 | //////////////////////////////////////////////////////////////////////////////// |
| 113 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 114 | void GrDrawState::setVertexAttribs(const GrVertexAttrib* attribs, int count) { |
| 115 | GrAssert(count <= GrDrawState::kAttribIndexCount); |
| 116 | fVertexAttribs.reset(); |
| 117 | for (int index = 0; index < count; ++index) { |
| 118 | fVertexAttribs.push_back(attribs[index]); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 119 | } |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | //////////////////////////////////////////////////////////////////////////////// |
| 123 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 124 | void GrDrawState::setDefaultVertexAttribs() { |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 125 | static const GrVertexAttrib kPositionAttrib = {kVec2f_GrVertexAttribType, 0}; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 126 | fVertexAttribs.reset(); |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 127 | fVertexAttribs.push_back(kPositionAttrib); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame^] | 128 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 129 | fCommon.fAttribBindings = kDefault_AttribBindings; |
| 130 | |
| 131 | fAttribIndices[kPosition_AttribIndex] = 0; |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | //////////////////////////////////////////////////////////////////////////////// |
| 135 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 136 | bool GrDrawState::AttributesBindExplicitTexCoords(GrAttribBindings attribBindings) { |
| 137 | return SkToBool(kTexCoord_AttribBindingsMask & attribBindings); |
| 138 | } |
| 139 | |
| 140 | //////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | void GrDrawState::VertexAttributesUnitTest() { |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 143 | // Ensure that our tex coord mask is correct |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 144 | GrAttribBindings texCoordMask; |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 145 | gen_tex_coord_mask(&texCoordMask); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 146 | GrAssert(texCoordMask == kTexCoord_AttribBindingsMask); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 147 | |
| 148 | // not necessarily exhaustive |
| 149 | static bool run; |
| 150 | if (!run) { |
| 151 | run = true; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 152 | |
| 153 | GrVertexAttribArray<6> attribs; |
| 154 | GrAssert(0 == vertex_size(attribs.begin(), attribs.count())); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame^] | 155 | |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 156 | GrVertexAttrib currAttrib = {kFloat_GrVertexAttribType, 0}; |
| 157 | attribs.push_back(currAttrib); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 158 | GrAssert(sizeof(float) == vertex_size(attribs.begin(), attribs.count())); |
| 159 | attribs[0].fType = kVec2f_GrVertexAttribType; |
| 160 | GrAssert(2*sizeof(float) == vertex_size(attribs.begin(), attribs.count())); |
| 161 | attribs[0].fType = kVec3f_GrVertexAttribType; |
| 162 | GrAssert(3*sizeof(float) == vertex_size(attribs.begin(), attribs.count())); |
| 163 | attribs[0].fType = kVec4f_GrVertexAttribType; |
| 164 | GrAssert(4*sizeof(float) == vertex_size(attribs.begin(), attribs.count())); |
| 165 | attribs[0].fType = kVec4ub_GrVertexAttribType; |
| 166 | GrAssert(4*sizeof(char) == vertex_size(attribs.begin(), attribs.count())); |
| 167 | |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 168 | currAttrib.set(kVec2f_GrVertexAttribType, attribs[0].fOffset + 4*sizeof(char)); |
| 169 | attribs.push_back(currAttrib); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 170 | GrAssert(4*sizeof(char) + 2*sizeof(float) == vertex_size(attribs.begin(), attribs.count())); |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 171 | currAttrib.set(kVec3f_GrVertexAttribType, attribs[1].fOffset + 2*sizeof(float)); |
| 172 | attribs.push_back(currAttrib); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame^] | 173 | GrAssert(4*sizeof(char) + 2*sizeof(float) + 3*sizeof(float) == |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 174 | vertex_size(attribs.begin(), attribs.count())); |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 175 | currAttrib.set(kFloat_GrVertexAttribType, attribs[2].fOffset + 3*sizeof(float)); |
| 176 | attribs.push_back(currAttrib); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame^] | 177 | GrAssert(4*sizeof(char) + 2*sizeof(float) + 3*sizeof(float) + sizeof(float) == |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 178 | vertex_size(attribs.begin(), attribs.count())); |
jvanverth@google.com | 3b0d631 | 2013-03-01 20:30:01 +0000 | [diff] [blame] | 179 | currAttrib.set(kVec4f_GrVertexAttribType, attribs[3].fOffset + sizeof(float)); |
| 180 | attribs.push_back(currAttrib); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame^] | 181 | GrAssert(4*sizeof(char) + 2*sizeof(float) + 3*sizeof(float) + sizeof(float) + 4*sizeof(float) == |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 182 | vertex_size(attribs.begin(), attribs.count())); |
| 183 | |
| 184 | GrAttribBindings tcMask = 0; |
| 185 | GrAssert(!AttributesBindExplicitTexCoords(0)); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 186 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 187 | tcMask |= ExplicitTexCoordAttribBindingsBit(s); |
| 188 | GrAssert(AttributesBindExplicitTexCoords(tcMask)); |
| 189 | GrAssert(StageBindsExplicitTexCoords(tcMask, s)); |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 190 | for (int s2 = s + 1; s2 < GrDrawState::kNumStages; ++s2) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 191 | GrAssert(!StageBindsExplicitTexCoords(tcMask, s2)); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 192 | } |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 193 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 194 | GrAssert(kTexCoord_AttribBindingsMask == tcMask); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | //////////////////////////////////////////////////////////////////////////////// |
| 199 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 200 | bool GrDrawState::StageBindsExplicitTexCoords(GrAttribBindings bindings, int stageIdx) { |
| 201 | return SkToBool(bindings & ExplicitTexCoordAttribBindingsBit(stageIdx)); |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 202 | } |
| 203 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 204 | bool GrDrawState::srcAlphaWillBeOne(GrAttribBindings bindings) const { |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 205 | |
| 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 | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 209 | if (bindings & kColor_AttribBindingsBit) { |
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 { |
| 213 | validComponentFlags = GrEffect::kAll_ValidComponentFlags; |
| 214 | color = this->getColor(); |
| 215 | } |
| 216 | |
| 217 | // Run through the color stages |
| 218 | int stageCnt = getFirstCoverageStage(); |
| 219 | for (int s = 0; s < stageCnt; ++s) { |
| 220 | const GrEffectRef* effect = this->getStage(s).getEffect(); |
| 221 | if (NULL != effect) { |
| 222 | (*effect)->getConstantColorComponents(&color, &validComponentFlags); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Check if the color filter could introduce an alpha. |
| 227 | // We could skip the above work when this is true, but it is rare and the right fix is to make |
| 228 | // the color filter a GrEffect and implement getConstantColorComponents() for it. |
| 229 | if (SkXfermode::kDst_Mode != this->getColorFilterMode()) { |
| 230 | validComponentFlags = 0; |
| 231 | } |
| 232 | |
| 233 | // Check whether coverage is treated as color. If so we run through the coverage computation. |
| 234 | if (this->isCoverageDrawing()) { |
| 235 | GrColor coverageColor = this->getCoverage(); |
| 236 | GrColor oldColor = color; |
| 237 | color = 0; |
| 238 | for (int c = 0; c < 4; ++c) { |
| 239 | if (validComponentFlags & (1 << c)) { |
| 240 | U8CPU a = (oldColor >> (c * 8)) & 0xff; |
| 241 | U8CPU b = (coverageColor >> (c * 8)) & 0xff; |
| 242 | color |= (SkMulDiv255Round(a, b) << (c * 8)); |
| 243 | } |
| 244 | } |
| 245 | for (int s = this->getFirstCoverageStage(); s < GrDrawState::kNumStages; ++s) { |
| 246 | const GrEffectRef* effect = this->getStage(s).getEffect(); |
| 247 | if (NULL != effect) { |
| 248 | (*effect)->getConstantColorComponents(&color, &validComponentFlags); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | return (GrEffect::kA_ValidComponentFlag & validComponentFlags) && 0xff == GrColorUnpackA(color); |
| 253 | } |
| 254 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 255 | bool GrDrawState::hasSolidCoverage(GrAttribBindings bindings) const { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 256 | // If we're drawing coverage directly then coverage is effectively treated as color. |
| 257 | if (this->isCoverageDrawing()) { |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | GrColor coverage; |
| 262 | uint32_t validComponentFlags; |
| 263 | // Initialize to an unknown starting coverage if per-vertex coverage is specified. |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 264 | if (bindings & kCoverage_AttribBindingsBit) { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 265 | validComponentFlags = 0; |
| 266 | } else { |
| 267 | coverage = fCommon.fCoverage; |
| 268 | validComponentFlags = GrEffect::kAll_ValidComponentFlags; |
| 269 | } |
| 270 | |
| 271 | // Run through the coverage stages and see if the coverage will be all ones at the end. |
| 272 | for (int s = this->getFirstCoverageStage(); s < GrDrawState::kNumStages; ++s) { |
| 273 | const GrEffectRef* effect = this->getStage(s).getEffect(); |
| 274 | if (NULL != effect) { |
| 275 | (*effect)->getConstantColorComponents(&coverage, &validComponentFlags); |
| 276 | } |
| 277 | } |
| 278 | return (GrEffect::kAll_ValidComponentFlags == validComponentFlags) && (0xffffffff == coverage); |
| 279 | } |
| 280 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 281 | //////////////////////////////////////////////////////////////////////////////// |
| 282 | |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 283 | // Some blend modes allow folding a fractional coverage value into the color's alpha channel, while |
| 284 | // others will blend incorrectly. |
| 285 | bool GrDrawState::canTweakAlphaForCoverage() const { |
| 286 | /* |
| 287 | The fractional coverage is f. |
| 288 | The src and dst coeffs are Cs and Cd. |
| 289 | The dst and src colors are S and D. |
| 290 | We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D. By tweaking the source color's alpha |
| 291 | we're replacing S with S'=fS. It's obvious that that first term will always be ok. The second |
| 292 | term can be rearranged as [1-(1-Cd)f]D. By substituting in the various possibilities for Cd we |
| 293 | find that only 1, ISA, and ISC produce the correct destination when applied to S' and D. |
| 294 | Also, if we're directly rendering coverage (isCoverageDrawing) then coverage is treated as |
| 295 | color by definition. |
| 296 | */ |
| 297 | return kOne_GrBlendCoeff == fCommon.fDstBlend || |
| 298 | kISA_GrBlendCoeff == fCommon.fDstBlend || |
| 299 | kISC_GrBlendCoeff == fCommon.fDstBlend || |
| 300 | this->isCoverageDrawing(); |
| 301 | } |
| 302 | |
| 303 | GrDrawState::BlendOptFlags GrDrawState::getBlendOpts(bool forceCoverage, |
| 304 | GrBlendCoeff* srcCoeff, |
| 305 | GrBlendCoeff* dstCoeff) const { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 306 | GrAttribBindings bindings = this->getAttribBindings(); |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 307 | |
| 308 | GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; |
| 309 | if (NULL == srcCoeff) { |
| 310 | srcCoeff = &bogusSrcCoeff; |
| 311 | } |
| 312 | *srcCoeff = this->getSrcBlendCoeff(); |
| 313 | |
| 314 | if (NULL == dstCoeff) { |
| 315 | dstCoeff = &bogusDstCoeff; |
| 316 | } |
| 317 | *dstCoeff = this->getDstBlendCoeff(); |
| 318 | |
| 319 | if (this->isColorWriteDisabled()) { |
| 320 | *srcCoeff = kZero_GrBlendCoeff; |
| 321 | *dstCoeff = kOne_GrBlendCoeff; |
| 322 | } |
| 323 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 324 | bool srcAIsOne = this->srcAlphaWillBeOne(bindings); |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 325 | bool dstCoeffIsOne = kOne_GrBlendCoeff == *dstCoeff || |
| 326 | (kSA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 327 | bool dstCoeffIsZero = kZero_GrBlendCoeff == *dstCoeff || |
| 328 | (kISA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 329 | |
| 330 | bool covIsZero = !this->isCoverageDrawing() && |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 331 | !(bindings & GrDrawState::kCoverage_AttribBindingsBit) && |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 332 | 0 == this->getCoverage(); |
| 333 | // When coeffs are (0,1) there is no reason to draw at all, unless |
| 334 | // stenciling is enabled. Having color writes disabled is effectively |
| 335 | // (0,1). The same applies when coverage is known to be 0. |
| 336 | if ((kZero_GrBlendCoeff == *srcCoeff && dstCoeffIsOne) || covIsZero) { |
| 337 | if (this->getStencil().doesWrite()) { |
| 338 | return kDisableBlend_BlendOptFlag | |
| 339 | kEmitTransBlack_BlendOptFlag; |
| 340 | } else { |
| 341 | return kSkipDraw_BlendOptFlag; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // check for coverage due to constant coverage, per-vertex coverage, |
| 346 | // edge aa or coverage stage |
| 347 | bool hasCoverage = forceCoverage || |
| 348 | 0xffffffff != this->getCoverage() || |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 349 | (bindings & GrDrawState::kCoverage_AttribBindingsBit) || |
| 350 | (bindings & GrDrawState::kEdge_AttribBindingsBit); |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 351 | for (int s = this->getFirstCoverageStage(); |
| 352 | !hasCoverage && s < GrDrawState::kNumStages; |
| 353 | ++s) { |
| 354 | if (this->isStageEnabled(s)) { |
| 355 | hasCoverage = true; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // if we don't have coverage we can check whether the dst |
| 360 | // has to read at all. If not, we'll disable blending. |
| 361 | if (!hasCoverage) { |
| 362 | if (dstCoeffIsZero) { |
| 363 | if (kOne_GrBlendCoeff == *srcCoeff) { |
| 364 | // if there is no coverage and coeffs are (1,0) then we |
| 365 | // won't need to read the dst at all, it gets replaced by src |
| 366 | return kDisableBlend_BlendOptFlag; |
| 367 | } else if (kZero_GrBlendCoeff == *srcCoeff) { |
| 368 | // if the op is "clear" then we don't need to emit a color |
| 369 | // or blend, just write transparent black into the dst. |
| 370 | *srcCoeff = kOne_GrBlendCoeff; |
| 371 | *dstCoeff = kZero_GrBlendCoeff; |
| 372 | return kDisableBlend_BlendOptFlag | kEmitTransBlack_BlendOptFlag; |
| 373 | } |
| 374 | } |
| 375 | } else if (this->isCoverageDrawing()) { |
| 376 | // we have coverage but we aren't distinguishing it from alpha by request. |
| 377 | return kCoverageAsAlpha_BlendOptFlag; |
| 378 | } else { |
| 379 | // check whether coverage can be safely rolled into alpha |
| 380 | // of if we can skip color computation and just emit coverage |
| 381 | if (this->canTweakAlphaForCoverage()) { |
| 382 | return kCoverageAsAlpha_BlendOptFlag; |
| 383 | } |
| 384 | if (dstCoeffIsZero) { |
| 385 | if (kZero_GrBlendCoeff == *srcCoeff) { |
| 386 | // the source color is not included in the blend |
| 387 | // the dst coeff is effectively zero so blend works out to: |
| 388 | // (c)(0)D + (1-c)D = (1-c)D. |
| 389 | *dstCoeff = kISA_GrBlendCoeff; |
| 390 | return kEmitCoverage_BlendOptFlag; |
| 391 | } else if (srcAIsOne) { |
| 392 | // the dst coeff is effectively zero so blend works out to: |
| 393 | // cS + (c)(0)D + (1-c)D = cS + (1-c)D. |
| 394 | // If Sa is 1 then we can replace Sa with c |
| 395 | // and set dst coeff to 1-Sa. |
| 396 | *dstCoeff = kISA_GrBlendCoeff; |
| 397 | return kCoverageAsAlpha_BlendOptFlag; |
| 398 | } |
| 399 | } else if (dstCoeffIsOne) { |
| 400 | // the dst coeff is effectively one so blend works out to: |
| 401 | // cS + (c)(1)D + (1-c)D = cS + D. |
| 402 | *dstCoeff = kOne_GrBlendCoeff; |
| 403 | return kCoverageAsAlpha_BlendOptFlag; |
| 404 | } |
| 405 | } |
| 406 | return kNone_BlendOpt; |
| 407 | } |
| 408 | |
| 409 | //////////////////////////////////////////////////////////////////////////////// |
| 410 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 411 | void GrDrawState::AutoViewMatrixRestore::restore() { |
| 412 | if (NULL != fDrawState) { |
| 413 | fDrawState->setViewMatrix(fViewMatrix); |
| 414 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 415 | if (fRestoreMask & (1 << s)) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 416 | fDrawState->fStages[s].restoreCoordChange(fSavedCoordChanges[s]); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | fDrawState = NULL; |
| 421 | } |
| 422 | |
| 423 | void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 424 | const SkMatrix& preconcatMatrix, |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 425 | uint32_t explicitCoordStageMask) { |
| 426 | this->restore(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 427 | |
| 428 | fDrawState = drawState; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 429 | if (NULL == drawState) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | fRestoreMask = 0; |
| 434 | fViewMatrix = drawState->getViewMatrix(); |
| 435 | drawState->preConcatViewMatrix(preconcatMatrix); |
| 436 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 437 | if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) { |
| 438 | fRestoreMask |= (1 << s); |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 439 | fDrawState->fStages[s].saveCoordChange(&fSavedCoordChanges[s]); |
| 440 | drawState->fStages[s].preConcatCoordChange(preconcatMatrix); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | //////////////////////////////////////////////////////////////////////////////// |
| 446 | |
| 447 | void GrDrawState::AutoDeviceCoordDraw::restore() { |
| 448 | if (NULL != fDrawState) { |
| 449 | fDrawState->setViewMatrix(fViewMatrix); |
| 450 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 451 | if (fRestoreMask & (1 << s)) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 452 | fDrawState->fStages[s].restoreCoordChange(fSavedCoordChanges[s]); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | fDrawState = NULL; |
| 457 | } |
| 458 | |
| 459 | bool GrDrawState::AutoDeviceCoordDraw::set(GrDrawState* drawState, |
| 460 | uint32_t explicitCoordStageMask) { |
| 461 | GrAssert(NULL != drawState); |
| 462 | |
| 463 | this->restore(); |
| 464 | |
| 465 | fDrawState = drawState; |
| 466 | if (NULL == fDrawState) { |
| 467 | return false; |
skia.committer@gmail.com | f467ce7 | 2012-10-09 02:01:37 +0000 | [diff] [blame] | 468 | } |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 469 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 470 | fViewMatrix = drawState->getViewMatrix(); |
| 471 | fRestoreMask = 0; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 472 | SkMatrix invVM; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 473 | bool inverted = false; |
| 474 | |
| 475 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 476 | if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) { |
| 477 | if (!inverted && !fViewMatrix.invert(&invVM)) { |
| 478 | // sad trombone sound |
| 479 | fDrawState = NULL; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 480 | return false; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 481 | } else { |
| 482 | inverted = true; |
| 483 | } |
| 484 | fRestoreMask |= (1 << s); |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 485 | GrEffectStage* stage = drawState->fStages + s; |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 486 | stage->saveCoordChange(&fSavedCoordChanges[s]); |
| 487 | stage->preConcatCoordChange(invVM); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | drawState->viewMatrix()->reset(); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 491 | return true; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 492 | } |