jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "GrDistanceFieldTextureEffect.h" |
| 9 | #include "gl/GrGLEffect.h" |
| 10 | #include "gl/GrGLSL.h" |
| 11 | #include "gl/GrGLTexture.h" |
| 12 | #include "gl/GrGLVertexEffect.h" |
| 13 | #include "GrTBackendEffectFactory.h" |
| 14 | #include "GrTexture.h" |
| 15 | |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 16 | #include "SkDistanceFieldGen.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 17 | |
| 18 | class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect { |
| 19 | public: |
skia.committer@gmail.com | a3b5327 | 2014-02-15 03:02:15 +0000 | [diff] [blame] | 20 | GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 21 | const GrDrawEffect& drawEffect) |
| 22 | : INHERITED (factory) |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 23 | , fTextureSize(SkISize::Make(-1,-1)) {} |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 24 | |
| 25 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 26 | const GrDrawEffect& drawEffect, |
| 27 | EffectKey key, |
| 28 | const char* outputColor, |
| 29 | const char* inputColor, |
| 30 | const TransformedCoordsArray&, |
| 31 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 32 | SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs()); |
| 33 | |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 34 | SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 35 | const GrDistanceFieldTextureEffect& dfTexEffect = |
| 36 | drawEffect.castEffect<GrDistanceFieldTextureEffect>(); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 38 | SkString fsCoordName; |
| 39 | const char* vsCoordName; |
| 40 | const char* fsCoordNamePtr; |
| 41 | builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsCoordNamePtr); |
| 42 | fsCoordName = fsCoordNamePtr; |
| 43 | |
| 44 | const char* attrName0 = |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 45 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 46 | builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); |
| 47 | |
| 48 | const char* textureSizeUniName = NULL; |
| 49 | fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 50 | kVec2f_GrSLType, "TextureSize", |
| 51 | &textureSizeUniName); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 52 | |
| 53 | builder->fsCodeAppend("\tvec4 texColor = "); |
| 54 | builder->fsAppendTextureLookup(samplers[0], |
| 55 | fsCoordName.c_str(), |
| 56 | kVec2f_GrSLType); |
| 57 | builder->fsCodeAppend(";\n"); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 58 | builder->fsCodeAppend("\tfloat distance = " |
| 59 | SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");\n"); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 60 | |
| 61 | // we adjust for the effect of the transformation on the distance by using |
| 62 | // the length of the gradient of the texture coordinates. We use st coordinates |
| 63 | // to ensure we're mapping 1:1 from texel space to pixel space. |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 64 | builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str()); |
| 65 | builder->fsCodeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); |
| 66 | builder->fsCodeAppend("\tfloat afwidth;\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 67 | if (dfTexEffect.isSimilarity()) { |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 68 | // this gives us a smooth step across approximately one fragment |
| 69 | // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) |
| 70 | builder->fsCodeAppend("\tafwidth = 0.7071*dFdx(st.x);\n"); |
| 71 | } else { |
| 72 | builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 73 | builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 74 | |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 75 | builder->fsCodeAppend("\tvec2 uv_grad;\n"); |
| 76 | if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
| 77 | // this is to compensate for the Adreno, which likes to drop tiles on division by 0 |
| 78 | builder->fsCodeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); |
| 79 | builder->fsCodeAppend("\tif (uv_len2 < 0.0001) {\n"); |
| 80 | builder->fsCodeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); |
| 81 | builder->fsCodeAppend("\t} else {\n"); |
| 82 | builder->fsCodeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n"); |
| 83 | builder->fsCodeAppend("\t}\n"); |
| 84 | } else { |
| 85 | builder->fsCodeAppend("\tuv_grad = normalize(uv);\n"); |
| 86 | } |
| 87 | builder->fsCodeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad.y*Jdy.x,\n"); |
| 88 | builder->fsCodeAppend("\t uv_grad.x*Jdx.y + uv_grad.y*Jdy.y);\n"); |
| 89 | |
| 90 | // this gives us a smooth step across approximately one fragment |
| 91 | // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) |
| 92 | builder->fsCodeAppend("\tafwidth = 0.7071*length(grad);\n"); |
| 93 | } |
| 94 | |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 95 | builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, distance);\n"); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 96 | |
| 97 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
| 98 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str()); |
| 99 | } |
| 100 | |
| 101 | virtual void setData(const GrGLUniformManager& uman, |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 102 | const GrDrawEffect& drawEffect) SK_OVERRIDE { |
| 103 | SkASSERT(fTextureSizeUni.isValid()); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 104 | |
| 105 | GrTexture* texture = drawEffect.effect()->get()->texture(0); |
| 106 | if (texture->width() != fTextureSize.width() || |
| 107 | texture->height() != fTextureSize.height()) { |
| 108 | fTextureSize = SkISize::Make(texture->width(), texture->height()); |
commit-bot@chromium.org | 66beaf0 | 2014-03-26 18:21:55 +0000 | [diff] [blame] | 109 | uman.set2f(fTextureSizeUni, |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 110 | SkIntToScalar(fTextureSize.width()), |
| 111 | SkIntToScalar(fTextureSize.height())); |
commit-bot@chromium.org | 66beaf0 | 2014-03-26 18:21:55 +0000 | [diff] [blame] | 112 | } |
commit-bot@chromium.org | 8fe2ee1 | 2014-03-26 18:03:05 +0000 | [diff] [blame] | 113 | } |
| 114 | |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 115 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 116 | const GrDistanceFieldTextureEffect& dfTexEffect = |
| 117 | drawEffect.castEffect<GrDistanceFieldTextureEffect>(); |
| 118 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 119 | return dfTexEffect.isSimilarity() ? 0x1 : 0x0; |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 122 | private: |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 123 | GrGLUniformManager::UniformHandle fTextureSizeUni; |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 124 | SkISize fTextureSize; |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 125 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 126 | typedef GrGLVertexEffect INHERITED; |
| 127 | }; |
| 128 | |
| 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 132 | const GrTextureParams& params, |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 133 | bool similarity) |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 134 | : fTextureAccess(texture, params) |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 135 | , fIsSimilarity(similarity) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 136 | this->addTextureAccess(&fTextureAccess); |
| 137 | this->addVertexAttrib(kVec2f_GrSLType); |
| 138 | } |
| 139 | |
| 140 | bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
| 141 | const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureEffect>(other); |
| 142 | return fTextureAccess == cte.fTextureAccess; |
| 143 | } |
| 144 | |
| 145 | void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, |
| 146 | uint32_t* validFlags) const { |
| 147 | if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && |
| 148 | GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 149 | *validFlags = kA_GrColorComponentFlag; |
| 150 | } else { |
| 151 | *validFlags = 0; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const { |
| 156 | return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance(); |
| 157 | } |
| 158 | |
| 159 | /////////////////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect); |
| 162 | |
| 163 | GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random, |
| 164 | GrContext*, |
| 165 | const GrDrawTargetCaps&, |
| 166 | GrTexture* textures[]) { |
| 167 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 168 | GrEffectUnitTest::kAlphaTextureIdx; |
| 169 | static const SkShader::TileMode kTileModes[] = { |
| 170 | SkShader::kClamp_TileMode, |
| 171 | SkShader::kRepeat_TileMode, |
| 172 | SkShader::kMirror_TileMode, |
| 173 | }; |
| 174 | SkShader::TileMode tileModes[] = { |
| 175 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 176 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 177 | }; |
| 178 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 179 | GrTextureParams::kNone_FilterMode); |
| 180 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 181 | return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, |
| 182 | random->nextBool()); |
| 183 | } |
| 184 | |
| 185 | /////////////////////////////////////////////////////////////////////////////// |
| 186 | |
| 187 | class GrGLDistanceFieldLCDTextureEffect : public GrGLVertexEffect { |
| 188 | public: |
| 189 | GrGLDistanceFieldLCDTextureEffect(const GrBackendEffectFactory& factory, |
| 190 | const GrDrawEffect& drawEffect) |
| 191 | : INHERITED (factory) |
| 192 | , fTextureSize(SkISize::Make(-1,-1)) {} |
| 193 | |
| 194 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 195 | const GrDrawEffect& drawEffect, |
| 196 | EffectKey key, |
| 197 | const char* outputColor, |
| 198 | const char* inputColor, |
| 199 | const TransformedCoordsArray&, |
| 200 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 201 | SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>().numVertexAttribs()); |
| 202 | |
| 203 | SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 204 | const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 205 | drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>(); |
| 206 | |
| 207 | SkString fsCoordName; |
| 208 | const char* vsCoordName; |
| 209 | const char* fsCoordNamePtr; |
| 210 | builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsCoordNamePtr); |
| 211 | fsCoordName = fsCoordNamePtr; |
| 212 | |
| 213 | const char* attrName0 = |
| 214 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); |
| 215 | builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); |
| 216 | |
| 217 | const char* textureSizeUniName = NULL; |
| 218 | // width, height, 1/(3*width) |
| 219 | fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 220 | kVec3f_GrSLType, "TextureSize", |
| 221 | &textureSizeUniName); |
| 222 | |
| 223 | // create LCD offset adjusted by inverse of transform |
| 224 | builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str()); |
| 225 | builder->fsCodeAppendf("\tvec2 st = uv*%s.xy;\n", textureSizeUniName); |
| 226 | if (dfTexEffect.isUniformScale()) { |
| 227 | builder->fsCodeAppend("\tfloat dx = dFdx(st.x);\n"); |
| 228 | builder->fsCodeAppendf("\tvec2 offset = vec2(dx*%s.z, 0.0);\n", textureSizeUniName); |
| 229 | } else { |
| 230 | builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 231 | builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); |
| 232 | builder->fsCodeAppendf("\tvec2 offset = %s.z*Jdx;\n", textureSizeUniName); |
| 233 | } |
| 234 | |
| 235 | // green is distance to uv center |
| 236 | builder->fsCodeAppend("\tvec4 texColor = "); |
| 237 | builder->fsAppendTextureLookup(samplers[0], "uv", kVec2f_GrSLType); |
| 238 | builder->fsCodeAppend(";\n"); |
| 239 | builder->fsCodeAppend("\tvec3 distance;\n"); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 240 | builder->fsCodeAppend("\tdistance.y = " |
| 241 | SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 242 | // red is distance to left offset |
| 243 | builder->fsCodeAppend("\tvec2 uv_adjusted = uv - offset;\n"); |
| 244 | builder->fsCodeAppend("\ttexColor = "); |
| 245 | builder->fsAppendTextureLookup(samplers[0], "uv_adjusted", kVec2f_GrSLType); |
| 246 | builder->fsCodeAppend(";\n"); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 247 | builder->fsCodeAppend("\tdistance.x = " |
| 248 | SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 249 | // blue is distance to right offset |
| 250 | builder->fsCodeAppend("\tuv_adjusted = uv + offset;\n"); |
| 251 | builder->fsCodeAppend("\ttexColor = "); |
| 252 | builder->fsAppendTextureLookup(samplers[0], "uv_adjusted", kVec2f_GrSLType); |
| 253 | builder->fsCodeAppend(";\n"); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 254 | builder->fsCodeAppend("\tdistance.z = " |
| 255 | SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 256 | // we adjust for the effect of the transformation on the distance by using |
| 257 | // the length of the gradient of the texture coordinates. We use st coordinates |
| 258 | // to ensure we're mapping 1:1 from texel space to pixel space. |
| 259 | |
| 260 | // To be strictly correct, we should compute the anti-aliasing factor separately |
| 261 | // for each color component. However, this is only important when using perspective |
| 262 | // transformations, and even then using a single factor seems like a reasonable |
| 263 | // trade-off between quality and speed. |
| 264 | builder->fsCodeAppend("\tfloat afwidth;\n"); |
| 265 | if (dfTexEffect.isUniformScale()) { |
| 266 | // this gives us a smooth step across approximately one fragment |
| 267 | // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) |
| 268 | builder->fsCodeAppend("\tafwidth = 0.7071*dx;\n"); |
| 269 | } else { |
| 270 | builder->fsCodeAppend("\tvec2 uv_grad;\n"); |
| 271 | if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
| 272 | // this is to compensate for the Adreno, which likes to drop tiles on division by 0 |
| 273 | builder->fsCodeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); |
| 274 | builder->fsCodeAppend("\tif (uv_len2 < 0.0001) {\n"); |
| 275 | builder->fsCodeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); |
| 276 | builder->fsCodeAppend("\t} else {\n"); |
| 277 | builder->fsCodeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n"); |
| 278 | builder->fsCodeAppend("\t}\n"); |
| 279 | } else { |
| 280 | builder->fsCodeAppend("\tuv_grad = normalize(uv);\n"); |
| 281 | } |
| 282 | builder->fsCodeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad.y*Jdy.x,\n"); |
| 283 | builder->fsCodeAppend("\t uv_grad.x*Jdx.y + uv_grad.y*Jdy.y);\n"); |
| 284 | |
| 285 | // this gives us a smooth step across approximately one fragment |
| 286 | // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) |
| 287 | builder->fsCodeAppend("\tafwidth = 0.7071*length(grad);\n"); |
| 288 | } |
| 289 | |
| 290 | builder->fsCodeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);\n"); |
| 291 | |
| 292 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
| 293 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("val")).c_str()); |
| 294 | } |
| 295 | |
| 296 | virtual void setData(const GrGLUniformManager& uman, |
| 297 | const GrDrawEffect& drawEffect) SK_OVERRIDE { |
| 298 | SkASSERT(fTextureSizeUni.isValid()); |
| 299 | |
| 300 | GrTexture* texture = drawEffect.effect()->get()->texture(0); |
| 301 | if (texture->width() != fTextureSize.width() || |
| 302 | texture->height() != fTextureSize.height()) { |
| 303 | const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 304 | drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>(); |
| 305 | fTextureSize = SkISize::Make(texture->width(), texture->height()); |
| 306 | float delta = 1.0f/(3.0f*texture->width()); |
| 307 | if (dfTexEffect.useBGR()) { |
| 308 | delta = -delta; |
| 309 | } |
| 310 | uman.set3f(fTextureSizeUni, |
| 311 | SkIntToScalar(fTextureSize.width()), |
| 312 | SkIntToScalar(fTextureSize.height()), |
| 313 | delta); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 318 | const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 319 | drawEffect.castEffect<GrDistanceFieldLCDTextureEffect>(); |
| 320 | |
| 321 | int uniformScale = dfTexEffect.isUniformScale() ? 0x01 : 0x00; |
| 322 | int useBGR = dfTexEffect.useBGR() ? 0x10 : 0x00; |
| 323 | return uniformScale | useBGR; |
| 324 | } |
| 325 | |
| 326 | private: |
| 327 | GrGLUniformManager::UniformHandle fTextureSizeUni; |
| 328 | SkISize fTextureSize; |
| 329 | |
| 330 | typedef GrGLVertexEffect INHERITED; |
| 331 | }; |
| 332 | |
| 333 | /////////////////////////////////////////////////////////////////////////////// |
| 334 | |
| 335 | GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect(GrTexture* texture, |
| 336 | const GrTextureParams& params, |
| 337 | bool uniformScale, |
| 338 | bool useBGR) |
| 339 | : fTextureAccess(texture, params) |
| 340 | , fUniformScale(uniformScale) |
| 341 | , fUseBGR(useBGR) { |
| 342 | this->addTextureAccess(&fTextureAccess); |
| 343 | this->addVertexAttrib(kVec2f_GrSLType); |
| 344 | } |
| 345 | |
| 346 | bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrEffect& other) const { |
| 347 | const GrDistanceFieldLCDTextureEffect& cte = CastEffect<GrDistanceFieldLCDTextureEffect>(other); |
| 348 | return fTextureAccess == cte.fTextureAccess; |
| 349 | } |
| 350 | |
| 351 | void GrDistanceFieldLCDTextureEffect::getConstantColorComponents(GrColor* color, |
| 352 | uint32_t* validFlags) const { |
| 353 | if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && |
| 354 | GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 355 | *validFlags = kA_GrColorComponentFlag; |
| 356 | } else { |
| 357 | *validFlags = 0; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | const GrBackendEffectFactory& GrDistanceFieldLCDTextureEffect::getFactory() const { |
| 362 | return GrTBackendEffectFactory<GrDistanceFieldLCDTextureEffect>::getInstance(); |
| 363 | } |
| 364 | |
| 365 | /////////////////////////////////////////////////////////////////////////////// |
| 366 | |
| 367 | GR_DEFINE_EFFECT_TEST(GrDistanceFieldLCDTextureEffect); |
| 368 | |
| 369 | GrEffectRef* GrDistanceFieldLCDTextureEffect::TestCreate(SkRandom* random, |
| 370 | GrContext*, |
| 371 | const GrDrawTargetCaps&, |
| 372 | GrTexture* textures[]) { |
| 373 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 374 | GrEffectUnitTest::kAlphaTextureIdx; |
| 375 | static const SkShader::TileMode kTileModes[] = { |
| 376 | SkShader::kClamp_TileMode, |
| 377 | SkShader::kRepeat_TileMode, |
| 378 | SkShader::kMirror_TileMode, |
| 379 | }; |
| 380 | SkShader::TileMode tileModes[] = { |
| 381 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 382 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 383 | }; |
| 384 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 385 | GrTextureParams::kNone_FilterMode); |
| 386 | |
| 387 | return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, |
| 388 | random->nextBool(), random->nextBool()); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 389 | } |