Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrDistanceFieldTextureEffect.h" | 8 #include "GrDistanceFieldTextureEffect.h" |
| 9 #include "gl/builders/GrGLFullProgramBuilder.h" | 9 #include "gl/builders/GrGLFullProgramBuilder.h" |
| 10 #include "gl/GrGLProcessor.h" | 10 #include "gl/GrGLProcessor.h" |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 #ifdef SK_GAMMA_APPLY_TO_A8 | 254 #ifdef SK_GAMMA_APPLY_TO_A8 |
| 255 textures[texIdx2], params2, | 255 textures[texIdx2], params2, |
| 256 random->nextF(), | 256 random->nextF(), |
| 257 #endif | 257 #endif |
| 258 random->nextBool() ? | 258 random->nextBool() ? |
| 259 kSimilarity_DistanceFieldEff ectFlag : 0); | 259 kSimilarity_DistanceFieldEff ectFlag : 0); |
| 260 } | 260 } |
| 261 | 261 |
| 262 /////////////////////////////////////////////////////////////////////////////// | 262 /////////////////////////////////////////////////////////////////////////////// |
| 263 | 263 |
| 264 class GrGLDistanceFieldNoGammaTextureEffect : public GrGLGeometryProcessor { | |
| 265 public: | |
| 266 GrGLDistanceFieldNoGammaTextureEffect(const GrBackendProcessorFactory& facto ry, | |
|
robertphillips
2014/09/26 14:00:31
line this up ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 267 const GrProcessor& effect) | |
| 268 : INHERITED(factory) | |
| 269 , fTextureSize(SkISize::Make(-1, -1)) | |
|
robertphillips
2014/09/26 14:00:30
Have you switched over to Mike R.'s bracket style
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 270 {} | |
| 271 | |
| 272 virtual void emitCode(GrGLFullProgramBuilder* builder, | |
|
robertphillips
2014/09/26 14:00:31
line up ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 273 const GrGeometryProcessor& effect, | |
| 274 const GrProcessorKey& key, | |
| 275 const char* outputColor, | |
| 276 const char* inputColor, | |
| 277 const TransformedCoordsArray&, | |
| 278 const TextureSamplerArray& samplers) SK_OVERRIDE{ | |
| 279 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = | |
|
robertphillips
2014/09/26 14:00:31
tab over ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 280 effect.cast<GrDistanceFieldNoGammaTextureEffect>(); | |
| 281 SkASSERT(1 == dfTexEffect.getVertexAttribs().count()); | |
| 282 | |
| 283 GrGLProcessorFragmentShaderBuilder* fsBuilder = builder->getFragmentShad erBuilder(); | |
| 284 SkAssertResult(fsBuilder->enableFeature( | |
| 285 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | |
| 286 | |
| 287 SkString fsCoordName; | |
| 288 const char* vsCoordName; | |
| 289 const char* fsCoordNamePtr; | |
| 290 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC oordNamePtr); | |
| 291 fsCoordName = fsCoordNamePtr; | |
| 292 | |
| 293 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); | |
|
robertphillips
2014/09/26 14:00:30
rm '\t's and '\n's ?
jvanverth1
2014/10/03 17:28:24
Done.
| |
| 294 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, dfTexEffect.inTextur eCoords().c_str()); | |
| 295 | |
| 296 const char* textureSizeUniName = NULL; | |
| 297 fTextureSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visi bility, | |
|
robertphillips
2014/09/26 14:00:30
line up ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 298 kVec2f_GrSLType, "TextureSize", | |
| 299 &textureSizeUniName); | |
| 300 | |
| 301 fsBuilder->codeAppend("\tvec4 texColor = "); | |
| 302 fsBuilder->appendTextureLookup(samplers[0], | |
| 303 fsCoordName.c_str(), | |
| 304 kVec2f_GrSLType); | |
| 305 fsBuilder->codeAppend(";\n"); | |
| 306 fsBuilder->codeAppend("\tfloat distance = " | |
| 307 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThresho ld ")" | |
| 308 "+ " SK_DistanceFieldNonLCDFactor ";\n"); | |
| 309 | |
| 310 // we adjust for the effect of the transformation on the distance by usi ng | |
| 311 // the length of the gradient of the texture coordinates. We use st coor dinates | |
| 312 // to ensure we're mapping 1:1 from texel space to pixel space. | |
| 313 fsBuilder->codeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str()); | |
| 314 fsBuilder->codeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); | |
| 315 fsBuilder->codeAppend("\tfloat afwidth;\n"); | |
| 316 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { | |
| 317 // this gives us a smooth step across approximately one fragment | |
| 318 fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*dFdx (st.x);\n"); | |
| 319 } | |
| 320 else { | |
| 321 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); | |
| 322 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); | |
| 323 | |
| 324 fsBuilder->codeAppend("\tvec2 uv_grad;\n"); | |
| 325 if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) { | |
| 326 // this is to compensate for the Adreno, which likes to drop til es on division by 0 | |
| 327 fsBuilder->codeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); | |
| 328 fsBuilder->codeAppend("\tif (uv_len2 < 0.0001) {\n"); | |
| 329 fsBuilder->codeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); | |
| 330 fsBuilder->codeAppend("\t} else {\n"); | |
| 331 fsBuilder->codeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n" ); | |
| 332 fsBuilder->codeAppend("\t}\n"); | |
| 333 } | |
| 334 else { | |
| 335 fsBuilder->codeAppend("\tuv_grad = normalize(uv);\n"); | |
| 336 } | |
| 337 fsBuilder->codeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad. y*Jdy.x,\n"); | |
| 338 fsBuilder->codeAppend("\t uv_grad.x*Jdx.y + uv_grad. y*Jdy.y);\n"); | |
| 339 | |
| 340 // this gives us a smooth step across approximately one fragment | |
| 341 fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*leng th(grad);\n"); | |
| 342 } | |
| 343 fsBuilder->codeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista nce);\n"); | |
| 344 | |
| 345 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, | |
| 346 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str()); | |
| 347 } | |
| 348 | |
| 349 virtual void setData(const GrGLProgramDataManager& pdman, | |
|
robertphillips
2014/09/26 14:00:31
line up ?
jvanverth1
2014/10/03 17:28:24
Done.
| |
| 350 const GrProcessor& effect) SK_OVERRIDE{ | |
| 351 SkASSERT(fTextureSizeUni.isValid()); | |
| 352 | |
| 353 GrTexture* texture = effect.texture(0); | |
|
robertphillips
2014/09/26 14:00:31
one line ?
jvanverth1
2014/10/03 17:28:23
Too wide...
| |
| 354 if (texture->width() != fTextureSize.width() || | |
| 355 texture->height() != fTextureSize.height()) { | |
| 356 fTextureSize = SkISize::Make(texture->width(), texture->height()); | |
| 357 pdman.set2f(fTextureSizeUni, | |
|
robertphillips
2014/09/26 14:00:31
line up ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 358 SkIntToScalar(fTextureSize.width()), | |
| 359 SkIntToScalar(fTextureSize.height())); | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 static inline void GenKey(const GrProcessor& effect, const GrGLCaps&, | |
|
robertphillips
2014/09/26 14:00:31
line up ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 364 GrProcessorKeyBuilder* b) { | |
| 365 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = | |
| 366 effect.cast<GrDistanceFieldNoGammaTextureEffect>(); | |
| 367 | |
| 368 b->add32(dfTexEffect.getFlags()); | |
| 369 } | |
| 370 | |
| 371 private: | |
| 372 GrGLProgramDataManager::UniformHandle fTextureSizeUni; | |
| 373 SkISize fTextureSize; | |
| 374 | |
| 375 typedef GrGLGeometryProcessor INHERITED; | |
| 376 }; | |
| 377 | |
| 378 /////////////////////////////////////////////////////////////////////////////// | |
| 379 | |
| 380 GrDistanceFieldNoGammaTextureEffect::GrDistanceFieldNoGammaTextureEffect(GrTextu re* texture, | |
| 381 const GrText ureParams& params, | |
| 382 uint32_t fla gs) | |
| 383 : fTextureAccess(texture, params) | |
| 384 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) | |
| 385 , fInTextureCoords(this->addVertexAttrib(GrShaderVar("inTextureCoords", | |
|
robertphillips
2014/09/26 14:00:31
this indenting (or lack thereof) is confusing.
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 386 kVec2f_GrSLType, | |
| 387 GrShaderVar::kAttribute_TypeModifier))) { | |
| 388 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); | |
| 389 this->addTextureAccess(&fTextureAccess); | |
| 390 } | |
| 391 | |
| 392 bool GrDistanceFieldNoGammaTextureEffect::onIsEqual(const GrProcessor& other) co nst { | |
| 393 const GrDistanceFieldNoGammaTextureEffect& cte = other.cast<GrDistanceFieldN oGammaTextureEffect>(); | |
|
egdaniel
2014/09/26 13:30:25
line wrap
jvanverth1
2014/10/03 17:28:24
Done.
| |
| 394 return fTextureAccess == cte.fTextureAccess && | |
|
robertphillips
2014/09/26 14:00:31
one line ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 395 fFlags == cte.fFlags; | |
| 396 } | |
| 397 | |
| 398 void GrDistanceFieldNoGammaTextureEffect::getConstantColorComponents(GrColor* co lor, | |
|
robertphillips
2014/09/26 14:00:31
line up?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 399 uint32_t* validFlags) const { | |
| 400 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) && | |
| 401 GrPixelConfigIsOpaque(this->texture(0)->config())) { | |
| 402 *validFlags = kA_GrColorComponentFlag; | |
| 403 } | |
|
robertphillips
2014/09/26 14:00:31
put else on prior line ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 404 else { | |
| 405 *validFlags = 0; | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 const GrBackendGeometryProcessorFactory& GrDistanceFieldNoGammaTextureEffect::ge tFactory() const { | |
| 410 return GrTBackendGeometryProcessorFactory<GrDistanceFieldNoGammaTextureEffec t>::getInstance(); | |
| 411 } | |
| 412 | |
| 413 /////////////////////////////////////////////////////////////////////////////// | |
| 414 | |
| 415 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldNoGammaTextureEffect); | |
| 416 | |
| 417 GrGeometryProcessor* GrDistanceFieldNoGammaTextureEffect::TestCreate(SkRandom* r andom, | |
|
robertphillips
2014/09/26 14:00:31
line up ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 418 GrContext*, | |
| 419 const GrDrawTargetCaps&, | |
| 420 GrTexture* textures[]) { | |
|
robertphillips
2014/09/26 14:00:30
put ':' on next line and line up with '?' ?
jvanverth1
2014/10/03 17:28:23
Done.
| |
| 421 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : | |
| 422 GrProcessorUnitTest::kAlphaTextureIdx; | |
| 423 static const SkShader::TileMode kTileModes[] = { | |
| 424 SkShader::kClamp_TileMode, | |
| 425 SkShader::kRepeat_TileMode, | |
| 426 SkShader::kMirror_TileMode, | |
| 427 }; | |
| 428 SkShader::TileMode tileModes[] = { | |
| 429 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | |
| 430 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | |
| 431 }; | |
| 432 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode : | |
|
robertphillips
2014/09/26 14:00:30
same here with ':' and '?' ?
jvanverth1
2014/10/03 17:28:24
Done.
| |
| 433 GrTextureParams::kNone_FilterMode); | |
| 434 | |
| 435 return GrDistanceFieldNoGammaTextureEffect::Create(textures[texIdx], params, | |
| 436 random->nextBool() ? | |
| 437 kSimilarity_DistanceFieldEffectFlag : 0); | |
| 438 } | |
| 439 | |
| 440 /////////////////////////////////////////////////////////////////////////////// | |
| 441 | |
| 264 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { | 442 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { |
| 265 public: | 443 public: |
| 266 GrGLDistanceFieldLCDTextureEffect(const GrBackendProcessorFactory& factory, | 444 GrGLDistanceFieldLCDTextureEffect(const GrBackendProcessorFactory& factory, |
| 267 const GrProcessor&) | 445 const GrProcessor&) |
| 268 : INHERITED (factory) | 446 : INHERITED (factory) |
| 269 , fTextureSize(SkISize::Make(-1,-1)) | 447 , fTextureSize(SkISize::Make(-1,-1)) |
| 270 , fTextColor(GrColor_ILLEGAL) {} | 448 , fTextColor(GrColor_ILLEGAL) {} |
| 271 | 449 |
| 272 virtual void emitCode(GrGLFullProgramBuilder* builder, | 450 virtual void emitCode(GrGLFullProgramBuilder* builder, |
| 273 const GrGeometryProcessor& geometryProcessor, | 451 const GrGeometryProcessor& geometryProcessor, |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 520 random->nextULessThan(256), | 698 random->nextULessThan(256), |
| 521 random->nextULessThan(256)); | 699 random->nextULessThan(256)); |
| 522 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 700 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
| 523 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 701 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
| 524 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 702 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
| 525 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, | 703 return GrDistanceFieldLCDTextureEffect::Create(textures[texIdx], params, |
| 526 textures[texIdx2], params2, | 704 textures[texIdx2], params2, |
| 527 textColor, | 705 textColor, |
| 528 flags); | 706 flags); |
| 529 } | 707 } |
| OLD | NEW |