| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 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 "SkMatrixConvolutionImageFilter.h" | 8 #include "SkMatrixConvolutionImageFilter.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkFlattenableBuffers.h" | 11 #include "SkFlattenableBuffers.h" |
| 12 #include "SkRect.h" | 12 #include "SkRect.h" |
| 13 #include "SkUnPreMultiply.h" | 13 #include "SkUnPreMultiply.h" |
| 14 | 14 |
| 15 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
| 16 #include "gl/GrGLEffect.h" | 16 #include "gl/GrGLEffect.h" |
| 17 #include "gl/GrGLEffectMatrix.h" | |
| 18 #include "effects/GrSingleTextureEffect.h" | 17 #include "effects/GrSingleTextureEffect.h" |
| 19 #include "GrTBackendEffectFactory.h" | 18 #include "GrTBackendEffectFactory.h" |
| 20 #include "GrTexture.h" | 19 #include "GrTexture.h" |
| 21 #include "SkMatrix.h" | 20 #include "SkMatrix.h" |
| 22 | 21 |
| 23 #endif | 22 #endif |
| 24 | 23 |
| 25 SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(const SkISize& ke
rnelSize, const SkScalar* kernel, SkScalar gain, SkScalar bias, const SkIPoint&
target, TileMode tileMode, bool convolveAlpha, SkImageFilter* input) | 24 SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(const SkISize& ke
rnelSize, const SkScalar* kernel, SkScalar gain, SkScalar bias, const SkIPoint&
target, TileMode tileMode, bool convolveAlpha, SkImageFilter* input) |
| 26 : INHERITED(input), | 25 : INHERITED(input), |
| 27 fKernelSize(kernelSize), | 26 fKernelSize(kernelSize), |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 | 316 |
| 318 class GrGLMatrixConvolutionEffect : public GrGLEffect { | 317 class GrGLMatrixConvolutionEffect : public GrGLEffect { |
| 319 public: | 318 public: |
| 320 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, | 319 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
| 321 const GrDrawEffect& effect); | 320 const GrDrawEffect& effect); |
| 322 virtual void emitCode(GrGLShaderBuilder*, | 321 virtual void emitCode(GrGLShaderBuilder*, |
| 323 const GrDrawEffect&, | 322 const GrDrawEffect&, |
| 324 EffectKey, | 323 EffectKey, |
| 325 const char* outputColor, | 324 const char* outputColor, |
| 326 const char* inputColor, | 325 const char* inputColor, |
| 326 const TransformedCoordsArray&, |
| 327 const TextureSamplerArray&) SK_OVERRIDE; | 327 const TextureSamplerArray&) SK_OVERRIDE; |
| 328 | 328 |
| 329 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 329 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 330 | 330 |
| 331 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 331 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 332 | 332 |
| 333 private: | 333 private: |
| 334 typedef GrGLUniformManager::UniformHandle UniformHandle; | 334 typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 335 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; | 335 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; |
| 336 SkISize fKernelSize; | 336 SkISize fKernelSize; |
| 337 TileMode fTileMode; | 337 TileMode fTileMode; |
| 338 bool fConvolveAlpha; | 338 bool fConvolveAlpha; |
| 339 | 339 |
| 340 UniformHandle fKernelUni; | 340 UniformHandle fKernelUni; |
| 341 UniformHandle fImageIncrementUni; | 341 UniformHandle fImageIncrementUni; |
| 342 UniformHandle fTargetUni; | 342 UniformHandle fTargetUni; |
| 343 UniformHandle fGainUni; | 343 UniformHandle fGainUni; |
| 344 UniformHandle fBiasUni; | 344 UniformHandle fBiasUni; |
| 345 | 345 |
| 346 GrGLEffectMatrix fEffectMatrix; | |
| 347 | |
| 348 typedef GrGLEffect INHERITED; | 346 typedef GrGLEffect INHERITED; |
| 349 }; | 347 }; |
| 350 | 348 |
| 351 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, | 349 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, |
| 352 const GrDrawEffect& dra
wEffect) | 350 const GrDrawEffect& dra
wEffect) |
| 353 : INHERITED(factory) | 351 : INHERITED(factory) { |
| 354 , fEffectMatrix(drawEffect.castEffect<GrMatrixConvolutionEffect>().coordsTyp
e()) { | |
| 355 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); | 352 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); |
| 356 fKernelSize = m.kernelSize(); | 353 fKernelSize = m.kernelSize(); |
| 357 fTileMode = m.tileMode(); | 354 fTileMode = m.tileMode(); |
| 358 fConvolveAlpha = m.convolveAlpha(); | 355 fConvolveAlpha = m.convolveAlpha(); |
| 359 } | 356 } |
| 360 | 357 |
| 361 static void appendTextureLookup(GrGLShaderBuilder* builder, | 358 static void appendTextureLookup(GrGLShaderBuilder* builder, |
| 362 const GrGLShaderBuilder::TextureSampler& sampler
, | 359 const GrGLShaderBuilder::TextureSampler& sampler
, |
| 363 const char* coord, | 360 const char* coord, |
| 364 SkMatrixConvolutionImageFilter::TileMode tileMod
e) { | 361 SkMatrixConvolutionImageFilter::TileMode tileMod
e) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 377 break; | 374 break; |
| 378 } | 375 } |
| 379 builder->fsAppendTextureLookup(sampler, coord); | 376 builder->fsAppendTextureLookup(sampler, coord); |
| 380 } | 377 } |
| 381 | 378 |
| 382 void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, | 379 void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
| 383 const GrDrawEffect&, | 380 const GrDrawEffect&, |
| 384 EffectKey key, | 381 EffectKey key, |
| 385 const char* outputColor, | 382 const char* outputColor, |
| 386 const char* inputColor, | 383 const char* inputColor, |
| 384 const TransformedCoordsArray& coords, |
| 387 const TextureSamplerArray& samplers)
{ | 385 const TextureSamplerArray& samplers)
{ |
| 388 SkString coords; | 386 SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
| 389 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | |
| 390 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | 387 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, |
| 391 kVec2f_GrSLType, "ImageIncrement"); | 388 kVec2f_GrSLType, "ImageIncrement"); |
| 392 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibilit
y, | 389 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibilit
y, |
| 393 kFloat_GrSLType, "Kernel", fKernelS
ize.width() * fKernelSize.height()); | 390 kFloat_GrSLType, "Kernel", fKernelS
ize.width() * fKernelSize.height()); |
| 394 fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 391 fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 395 kVec2f_GrSLType, "Target"); | 392 kVec2f_GrSLType, "Target"); |
| 396 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 393 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 397 kFloat_GrSLType, "Gain"); | 394 kFloat_GrSLType, "Gain"); |
| 398 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 395 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 399 kFloat_GrSLType, "Bias"); | 396 kFloat_GrSLType, "Bias"); |
| 400 | 397 |
| 401 const char* target = builder->getUniformCStr(fTargetUni); | 398 const char* target = builder->getUniformCStr(fTargetUni); |
| 402 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 399 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 403 const char* kernel = builder->getUniformCStr(fKernelUni); | 400 const char* kernel = builder->getUniformCStr(fKernelUni); |
| 404 const char* gain = builder->getUniformCStr(fGainUni); | 401 const char* gain = builder->getUniformCStr(fGainUni); |
| 405 const char* bias = builder->getUniformCStr(fBiasUni); | 402 const char* bias = builder->getUniformCStr(fBiasUni); |
| 406 int kWidth = fKernelSize.width(); | 403 int kWidth = fKernelSize.width(); |
| 407 int kHeight = fKernelSize.height(); | 404 int kHeight = fKernelSize.height(); |
| 408 | 405 |
| 409 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); | 406 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); |
| 410 builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords.c_str(), t
arget, imgInc); | 407 builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(),
target, imgInc); |
| 411 builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight); | 408 builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight); |
| 412 builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth); | 409 builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth); |
| 413 builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth
); | 410 builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth
); |
| 414 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", i
mgInc); | 411 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", i
mgInc); |
| 415 builder->fsCodeAppend("\t\t\t\tvec4 c = "); | 412 builder->fsCodeAppend("\t\t\t\tvec4 c = "); |
| 416 appendTextureLookup(builder, samplers[0], "coord2", fTileMode); | 413 appendTextureLookup(builder, samplers[0], "coord2", fTileMode); |
| 417 builder->fsCodeAppend(";\n"); | 414 builder->fsCodeAppend(";\n"); |
| 418 if (!fConvolveAlpha) { | 415 if (!fConvolveAlpha) { |
| 419 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); | 416 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); |
| 420 } | 417 } |
| 421 builder->fsCodeAppend("\t\t\t\tsum += c * k;\n"); | 418 builder->fsCodeAppend("\t\t\t\tsum += c * k;\n"); |
| 422 builder->fsCodeAppend("\t\t\t}\n"); | 419 builder->fsCodeAppend("\t\t\t}\n"); |
| 423 builder->fsCodeAppend("\t\t}\n"); | 420 builder->fsCodeAppend("\t\t}\n"); |
| 424 if (fConvolveAlpha) { | 421 if (fConvolveAlpha) { |
| 425 builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, b
ias); | 422 builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, b
ias); |
| 426 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n", outpu
tColor, outputColor, outputColor); | 423 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n", outpu
tColor, outputColor, outputColor); |
| 427 } else { | 424 } else { |
| 428 builder->fsCodeAppend("\t\tvec4 c = "); | 425 builder->fsCodeAppend("\t\tvec4 c = "); |
| 429 appendTextureLookup(builder, samplers[0], coords.c_str(), fTileMode); | 426 appendTextureLookup(builder, samplers[0], coords2D.c_str(), fTileMode); |
| 430 builder->fsCodeAppend(";\n"); | 427 builder->fsCodeAppend(";\n"); |
| 431 builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor); | 428 builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor); |
| 432 builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor,
gain, bias); | 429 builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor,
gain, bias); |
| 433 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor
); | 430 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor
); |
| 434 } | 431 } |
| 435 } | 432 } |
| 436 | 433 |
| 437 namespace { | 434 namespace { |
| 438 | 435 |
| 439 int encodeXY(int x, int y) { | 436 int encodeXY(int x, int y) { |
| 440 SkASSERT(x >= 1 && y >= 1 && x * y <= 32); | 437 SkASSERT(x >= 1 && y >= 1 && x * y <= 32); |
| 441 if (y < x) | 438 if (y < x) |
| 442 return 0x40 | encodeXY(y, x); | 439 return 0x40 | encodeXY(y, x); |
| 443 else | 440 else |
| 444 return (0x40 >> x) | (y - x); | 441 return (0x40 >> x) | (y - x); |
| 445 } | 442 } |
| 446 | 443 |
| 447 }; | 444 }; |
| 448 | 445 |
| 449 GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& dr
awEffect, | 446 GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& dr
awEffect, |
| 450 const GrGLCaps&) { | 447 const GrGLCaps&) { |
| 451 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); | 448 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); |
| 452 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); | 449 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); |
| 453 key |= m.tileMode() << 7; | 450 key |= m.tileMode() << 7; |
| 454 key |= m.convolveAlpha() ? 1 << 9 : 0; | 451 key |= m.convolveAlpha() ? 1 << 9 : 0; |
| 455 key <<= GrGLEffectMatrix::kKeyBits; | 452 return key; |
| 456 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), | |
| 457 drawEffect, | |
| 458 m.coordsType(), | |
| 459 m.texture(0)); | |
| 460 return key | matrixKey; | |
| 461 } | 453 } |
| 462 | 454 |
| 463 void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, | 455 void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, |
| 464 const GrDrawEffect& drawEffect) { | 456 const GrDrawEffect& drawEffect) { |
| 465 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); | 457 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); |
| 466 GrTexture& texture = *conv.texture(0); | 458 GrTexture& texture = *conv.texture(0); |
| 467 // the code we generated was for a specific kernel size | 459 // the code we generated was for a specific kernel size |
| 468 SkASSERT(conv.kernelSize() == fKernelSize); | 460 SkASSERT(conv.kernelSize() == fKernelSize); |
| 469 SkASSERT(conv.tileMode() == fTileMode); | 461 SkASSERT(conv.tileMode() == fTileMode); |
| 470 float imageIncrement[2]; | 462 float imageIncrement[2]; |
| 471 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 463 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 472 imageIncrement[0] = 1.0f / texture.width(); | 464 imageIncrement[0] = 1.0f / texture.width(); |
| 473 imageIncrement[1] = ySign / texture.height(); | 465 imageIncrement[1] = ySign / texture.height(); |
| 474 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); | 466 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| 475 uman.set2fv(fTargetUni, 0, 1, conv.target()); | 467 uman.set2fv(fTargetUni, 0, 1, conv.target()); |
| 476 uman.set1fv(fKernelUni, 0, fKernelSize.width() * fKernelSize.height(), conv.
kernel()); | 468 uman.set1fv(fKernelUni, 0, fKernelSize.width() * fKernelSize.height(), conv.
kernel()); |
| 477 uman.set1f(fGainUni, conv.gain()); | 469 uman.set1f(fGainUni, conv.gain()); |
| 478 uman.set1f(fBiasUni, conv.bias()); | 470 uman.set1f(fBiasUni, conv.bias()); |
| 479 fEffectMatrix.setData(uman, | |
| 480 conv.getMatrix(), | |
| 481 drawEffect, | |
| 482 conv.texture(0)); | |
| 483 } | 471 } |
| 484 | 472 |
| 485 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, | 473 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
| 486 const SkISize& kernelSize, | 474 const SkISize& kernelSize, |
| 487 const SkScalar* kernel, | 475 const SkScalar* kernel, |
| 488 SkScalar gain, | 476 SkScalar gain, |
| 489 SkScalar bias, | 477 SkScalar bias, |
| 490 const SkIPoint& target, | 478 const SkIPoint& target, |
| 491 TileMode tileMode, | 479 TileMode tileMode, |
| 492 bool convolveAlpha) | 480 bool convolveAlpha) |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 fBias, | 561 fBias, |
| 574 fTarget, | 562 fTarget, |
| 575 fTileMode, | 563 fTileMode, |
| 576 fConvolveAlpha); | 564 fConvolveAlpha); |
| 577 return true; | 565 return true; |
| 578 } | 566 } |
| 579 | 567 |
| 580 /////////////////////////////////////////////////////////////////////////////// | 568 /////////////////////////////////////////////////////////////////////////////// |
| 581 | 569 |
| 582 #endif | 570 #endif |
| OLD | NEW |