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" |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 const SkISize& kernelSize() const { return fKernelSize; } | 277 const SkISize& kernelSize() const { return fKernelSize; } |
278 const float* target() const { return fTarget; } | 278 const float* target() const { return fTarget; } |
279 const float* kernel() const { return fKernel; } | 279 const float* kernel() const { return fKernel; } |
280 float gain() const { return fGain; } | 280 float gain() const { return fGain; } |
281 float bias() const { return fBias; } | 281 float bias() const { return fBias; } |
282 TileMode tileMode() const { return fTileMode; } | 282 TileMode tileMode() const { return fTileMode; } |
283 bool convolveAlpha() const { return fConvolveAlpha; } | 283 bool convolveAlpha() const { return fConvolveAlpha; } |
284 | 284 |
285 typedef GrGLMatrixConvolutionEffect GLEffect; | 285 typedef GrGLMatrixConvolutionEffect GLEffect; |
286 | 286 |
287 | |
288 | |
289 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 287 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
290 | 288 |
291 private: | 289 private: |
292 GrMatrixConvolutionEffect(GrTexture*, | 290 GrMatrixConvolutionEffect(GrTexture*, |
293 const SkISize& kernelSize, | 291 const SkISize& kernelSize, |
294 const SkScalar* kernel, | 292 const SkScalar* kernel, |
295 SkScalar gain, | 293 SkScalar gain, |
296 SkScalar bias, | 294 SkScalar bias, |
297 const SkIPoint& target, | 295 const SkIPoint& target, |
298 TileMode tileMode, | 296 TileMode tileMode, |
(...skipping 10 matching lines...) Expand all Loading... |
309 bool fConvolveAlpha; | 307 bool fConvolveAlpha; |
310 | 308 |
311 GR_DECLARE_EFFECT_TEST; | 309 GR_DECLARE_EFFECT_TEST; |
312 | 310 |
313 typedef GrSingleTextureEffect INHERITED; | 311 typedef GrSingleTextureEffect INHERITED; |
314 }; | 312 }; |
315 | 313 |
316 class GrGLMatrixConvolutionEffect : public GrGLEffect { | 314 class GrGLMatrixConvolutionEffect : public GrGLEffect { |
317 public: | 315 public: |
318 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, | 316 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
319 const GrEffectRef& effect); | 317 const GrDrawEffect& effect); |
320 virtual void emitCode(GrGLShaderBuilder*, | 318 virtual void emitCode(GrGLShaderBuilder*, |
321 const GrEffectStage&, | 319 const GrDrawEffect&, |
322 EffectKey, | 320 EffectKey, |
323 const char* vertexCoords, | |
324 const char* outputColor, | 321 const char* outputColor, |
325 const char* inputColor, | 322 const char* inputColor, |
326 const TextureSamplerArray&) SK_OVERRIDE; | 323 const TextureSamplerArray&) SK_OVERRIDE; |
327 | 324 |
328 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); | 325 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
329 | 326 |
330 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVE
RRIDE; | 327 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
331 | 328 |
332 private: | 329 private: |
333 typedef GrGLUniformManager::UniformHandle UniformHandle; | 330 typedef GrGLUniformManager::UniformHandle UniformHandle; |
334 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; | 331 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; |
335 SkISize fKernelSize; | 332 SkISize fKernelSize; |
336 TileMode fTileMode; | 333 TileMode fTileMode; |
337 bool fConvolveAlpha; | 334 bool fConvolveAlpha; |
338 | 335 |
339 UniformHandle fKernelUni; | 336 UniformHandle fKernelUni; |
340 UniformHandle fImageIncrementUni; | 337 UniformHandle fImageIncrementUni; |
341 UniformHandle fTargetUni; | 338 UniformHandle fTargetUni; |
342 UniformHandle fGainUni; | 339 UniformHandle fGainUni; |
343 UniformHandle fBiasUni; | 340 UniformHandle fBiasUni; |
344 | 341 |
345 GrGLEffectMatrix fEffectMatrix; | 342 GrGLEffectMatrix fEffectMatrix; |
346 | 343 |
347 typedef GrGLEffect INHERITED; | 344 typedef GrGLEffect INHERITED; |
348 }; | 345 }; |
349 | 346 |
350 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, | 347 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, |
351 const GrEffectRef& effe
ct) | 348 const GrDrawEffect& dra
wEffect) |
352 : INHERITED(factory) | 349 : INHERITED(factory) |
353 , fKernelUni(GrGLUniformManager::kInvalidUniformHandle) | 350 , fKernelUni(GrGLUniformManager::kInvalidUniformHandle) |
354 , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) | 351 , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) |
355 , fTargetUni(GrGLUniformManager::kInvalidUniformHandle) | 352 , fTargetUni(GrGLUniformManager::kInvalidUniformHandle) |
356 , fGainUni(GrGLUniformManager::kInvalidUniformHandle) | 353 , fGainUni(GrGLUniformManager::kInvalidUniformHandle) |
357 , fBiasUni(GrGLUniformManager::kInvalidUniformHandle) { | 354 , fBiasUni(GrGLUniformManager::kInvalidUniformHandle) |
358 const GrMatrixConvolutionEffect& m = CastEffect<GrMatrixConvolutionEffect>(e
ffect); | 355 , fEffectMatrix(drawEffect.castEffect<GrMatrixConvolutionEffect>().coordsTyp
e()) { |
| 356 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); |
359 fKernelSize = m.kernelSize(); | 357 fKernelSize = m.kernelSize(); |
360 fTileMode = m.tileMode(); | 358 fTileMode = m.tileMode(); |
361 fConvolveAlpha = m.convolveAlpha(); | 359 fConvolveAlpha = m.convolveAlpha(); |
362 } | 360 } |
363 | 361 |
364 static void appendTextureLookup(GrGLShaderBuilder* builder, | 362 static void appendTextureLookup(GrGLShaderBuilder* builder, |
365 const GrGLShaderBuilder::TextureSampler& sampler
, | 363 const GrGLShaderBuilder::TextureSampler& sampler
, |
366 const char* coord, | 364 const char* coord, |
367 SkMatrixConvolutionImageFilter::TileMode tileMod
e) { | 365 SkMatrixConvolutionImageFilter::TileMode tileMod
e) { |
368 SkString clampedCoord; | 366 SkString clampedCoord; |
369 switch (tileMode) { | 367 switch (tileMode) { |
370 case SkMatrixConvolutionImageFilter::kClamp_TileMode: | 368 case SkMatrixConvolutionImageFilter::kClamp_TileMode: |
371 clampedCoord.printf("clamp(%s, 0.0, 1.0)", coord); | 369 clampedCoord.printf("clamp(%s, 0.0, 1.0)", coord); |
372 coord = clampedCoord.c_str(); | 370 coord = clampedCoord.c_str(); |
373 break; | 371 break; |
374 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: | 372 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: |
375 clampedCoord.printf("fract(%s)", coord); | 373 clampedCoord.printf("fract(%s)", coord); |
376 coord = clampedCoord.c_str(); | 374 coord = clampedCoord.c_str(); |
377 break; | 375 break; |
378 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: | 376 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: |
379 builder->fsCodeAppendf("clamp(%s, 0.0, 1.0) != %s ? vec4(0, 0, 0, 0)
: ", coord, coord); | 377 builder->fsCodeAppendf("clamp(%s, 0.0, 1.0) != %s ? vec4(0, 0, 0, 0)
: ", coord, coord); |
380 break; | 378 break; |
381 } | 379 } |
382 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sample
r, coord); | 380 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sample
r, coord); |
383 } | 381 } |
384 | 382 |
385 void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, | 383 void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
386 const GrEffectStage&, | 384 const GrDrawEffect&, |
387 EffectKey key, | 385 EffectKey key, |
388 const char* vertexCoords, | |
389 const char* outputColor, | 386 const char* outputColor, |
390 const char* inputColor, | 387 const char* inputColor, |
391 const TextureSamplerArray& samplers)
{ | 388 const TextureSamplerArray& samplers)
{ |
392 const char* coords; | 389 const char* coords; |
393 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); | 390 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
394 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, | 391 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, |
395 kVec2f_GrSLType, "ImageIncrement"); | 392 kVec2f_GrSLType, "ImageIncrement"); |
396 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderTyp
e, | 393 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderTyp
e, |
397 kFloat_GrSLType, "Kernel", fKernelS
ize.width() * fKernelSize.height()); | 394 kFloat_GrSLType, "Kernel", fKernelS
ize.width() * fKernelSize.height()); |
398 fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 395 fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
399 kVec2f_GrSLType, "Target"); | 396 kVec2f_GrSLType, "Target"); |
400 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 397 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
401 kFloat_GrSLType, "Gain"); | 398 kFloat_GrSLType, "Gain"); |
402 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 399 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
403 kFloat_GrSLType, "Bias"); | 400 kFloat_GrSLType, "Bias"); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 int encodeXY(int x, int y) { | 440 int encodeXY(int x, int y) { |
444 SkASSERT(x >= 1 && y >= 1 && x * y <= 32); | 441 SkASSERT(x >= 1 && y >= 1 && x * y <= 32); |
445 if (y < x) | 442 if (y < x) |
446 return 0x40 | encodeXY(y, x); | 443 return 0x40 | encodeXY(y, x); |
447 else | 444 else |
448 return (0x40 >> x) | (y - x); | 445 return (0x40 >> x) | (y - x); |
449 } | 446 } |
450 | 447 |
451 }; | 448 }; |
452 | 449 |
453 GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrEffectStage& s
, const GrGLCaps&) { | 450 GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& dr
awEffect, |
454 const GrMatrixConvolutionEffect& m = GetEffectFromStage<GrMatrixConvolutionE
ffect>(s); | 451 const GrGLCaps&) { |
| 452 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); |
455 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); | 453 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); |
456 key |= m.tileMode() << 7; | 454 key |= m.tileMode() << 7; |
457 key |= m.convolveAlpha() ? 1 << 9 : 0; | 455 key |= m.convolveAlpha() ? 1 << 9 : 0; |
458 key <<= GrGLEffectMatrix::kKeyBits; | 456 key <<= GrGLEffectMatrix::kKeyBits; |
459 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), | 457 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), |
460 s.getCoordChangeMatrix(), | 458 drawEffect, |
| 459 m.coordsType(), |
461 m.texture(0)); | 460 m.texture(0)); |
462 return key | matrixKey; | 461 return key | matrixKey; |
463 } | 462 } |
464 | 463 |
465 void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, | 464 void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, |
466 const GrEffectStage& stage) { | 465 const GrDrawEffect& drawEffect) { |
467 const GrMatrixConvolutionEffect& effect = GetEffectFromStage<GrMatrixConvolu
tionEffect>(stage); | 466 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); |
468 GrTexture& texture = *effect.texture(0); | 467 GrTexture& texture = *conv.texture(0); |
469 // the code we generated was for a specific kernel size | 468 // the code we generated was for a specific kernel size |
470 GrAssert(effect.kernelSize() == fKernelSize); | 469 GrAssert(conv.kernelSize() == fKernelSize); |
471 GrAssert(effect.tileMode() == fTileMode); | 470 GrAssert(conv.tileMode() == fTileMode); |
472 float imageIncrement[2]; | 471 float imageIncrement[2]; |
473 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 472 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
474 imageIncrement[0] = 1.0f / texture.width(); | 473 imageIncrement[0] = 1.0f / texture.width(); |
475 imageIncrement[1] = ySign / texture.height(); | 474 imageIncrement[1] = ySign / texture.height(); |
476 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); | 475 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
477 uman.set2fv(fTargetUni, 0, 1, effect.target()); | 476 uman.set2fv(fTargetUni, 0, 1, conv.target()); |
478 uman.set1fv(fKernelUni, 0, fKernelSize.width() * fKernelSize.height(), effec
t.kernel()); | 477 uman.set1fv(fKernelUni, 0, fKernelSize.width() * fKernelSize.height(), conv.
kernel()); |
479 uman.set1f(fGainUni, effect.gain()); | 478 uman.set1f(fGainUni, conv.gain()); |
480 uman.set1f(fBiasUni, effect.bias()); | 479 uman.set1f(fBiasUni, conv.bias()); |
481 fEffectMatrix.setData(uman, | 480 fEffectMatrix.setData(uman, |
482 effect.getMatrix(), | 481 conv.getMatrix(), |
483 stage.getCoordChangeMatrix(), | 482 drawEffect, |
484 effect.texture(0)); | 483 conv.texture(0)); |
485 } | 484 } |
486 | 485 |
487 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, | 486 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
488 const SkISize& kernelSize, | 487 const SkISize& kernelSize, |
489 const SkScalar* kernel, | 488 const SkScalar* kernel, |
490 SkScalar gain, | 489 SkScalar gain, |
491 SkScalar bias, | 490 SkScalar bias, |
492 const SkIPoint& target, | 491 const SkIPoint& target, |
493 TileMode tileMode, | 492 TileMode tileMode, |
494 bool convolveAlpha) | 493 bool convolveAlpha) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 fTarget, | 572 fTarget, |
574 fTileMode, | 573 fTileMode, |
575 fConvolveAlpha); | 574 fConvolveAlpha); |
576 } | 575 } |
577 return ok; | 576 return ok; |
578 } | 577 } |
579 | 578 |
580 /////////////////////////////////////////////////////////////////////////////// | 579 /////////////////////////////////////////////////////////////////////////////// |
581 | 580 |
582 #endif | 581 #endif |
OLD | NEW |