Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: src/effects/SkMatrixConvolutionImageFilter.cpp

Issue 379253003: Initial change to move 2D kernel to its own file (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Correctly wrapping asNewEffect definition with ifdefs Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "SkReadBuffer.h" 11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h" 12 #include "SkWriteBuffer.h"
13 #include "SkRect.h" 13 #include "SkRect.h"
14 #include "SkUnPreMultiply.h" 14 #include "SkUnPreMultiply.h"
15 15
16 #if SK_SUPPORT_GPU 16 #if SK_SUPPORT_GPU
17 #include "gl/GrGLEffect.h" 17 #include "gl/GrGLEffect.h"
18 #include "effects/GrSingleTextureEffect.h" 18 #include "effects/GrMatrixConvolutionEffect.h"
19 #include "GrTBackendEffectFactory.h" 19 #include "GrTBackendEffectFactory.h"
20 #include "GrTexture.h" 20 #include "GrTexture.h"
21 #include "SkMatrix.h" 21 #include "SkMatrix.h"
22 #endif 22 #endif
23 23
24 static bool tile_mode_is_valid(SkMatrixConvolutionImageFilter::TileMode tileMode ) { 24 static bool tile_mode_is_valid(SkMatrixConvolutionImageFilter::TileMode tileMode ) {
25 switch (tileMode) { 25 switch (tileMode) {
26 case SkMatrixConvolutionImageFilter::kClamp_TileMode: 26 case SkMatrixConvolutionImageFilter::kClamp_TileMode:
27 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: 27 case SkMatrixConvolutionImageFilter::kRepeat_TileMode:
28 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: 28 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode:
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 bounds.fBottom += fKernelSize.height() - 1; 315 bounds.fBottom += fKernelSize.height() - 1;
316 bounds.offset(-fKernelOffset); 316 bounds.offset(-fKernelOffset);
317 if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) { 317 if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) {
318 return false; 318 return false;
319 } 319 }
320 *dst = bounds; 320 *dst = bounds;
321 return true; 321 return true;
322 } 322 }
323 323
324 #if SK_SUPPORT_GPU 324 #if SK_SUPPORT_GPU
325
326 ///////////////////////////////////////////////////////////////////////////////
327
328 class GrGLMatrixConvolutionEffect;
329
330 class GrMatrixConvolutionEffect : public GrSingleTextureEffect {
331 public:
332 typedef SkMatrixConvolutionImageFilter::TileMode TileMode;
333 static GrEffect* Create(GrTexture* texture,
334 const SkIRect& bounds,
335 const SkISize& kernelSize,
336 const SkScalar* kernel,
337 SkScalar gain,
338 SkScalar bias,
339 const SkIPoint& kernelOffset,
340 TileMode tileMode,
341 bool convolveAlpha) {
342 return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
343 bounds,
344 kernelSize,
345 kernel,
346 gain,
347 bias,
348 kernelOffset,
349 tileMode,
350 convolveAlpha));
351 }
352 virtual ~GrMatrixConvolutionEffect();
353
354 virtual void getConstantColorComponents(GrColor* color,
355 uint32_t* validFlags) const SK_OVERR IDE {
356 // TODO: Try to do better?
357 *validFlags = 0;
358 }
359
360 static const char* Name() { return "MatrixConvolution"; }
361 const SkIRect& bounds() const { return fBounds; }
362 const SkISize& kernelSize() const { return fKernelSize; }
363 const float* kernelOffset() const { return fKernelOffset; }
364 const float* kernel() const { return fKernel; }
365 float gain() const { return fGain; }
366 float bias() const { return fBias; }
367 TileMode tileMode() const { return fTileMode; }
368 bool convolveAlpha() const { return fConvolveAlpha; }
369
370 typedef GrGLMatrixConvolutionEffect GLEffect;
371
372 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
373
374 private:
375 GrMatrixConvolutionEffect(GrTexture*,
376 const SkIRect& bounds,
377 const SkISize& kernelSize,
378 const SkScalar* kernel,
379 SkScalar gain,
380 SkScalar bias,
381 const SkIPoint& kernelOffset,
382 TileMode tileMode,
383 bool convolveAlpha);
384
385 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
386
387 SkIRect fBounds;
388 SkISize fKernelSize;
389 float *fKernel;
390 float fGain;
391 float fBias;
392 float fKernelOffset[2];
393 TileMode fTileMode;
394 bool fConvolveAlpha;
395
396 GR_DECLARE_EFFECT_TEST;
397
398 typedef GrSingleTextureEffect INHERITED;
399 };
400
401 class GrGLMatrixConvolutionEffect : public GrGLEffect {
402 public:
403 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory,
404 const GrDrawEffect& effect);
405 virtual void emitCode(GrGLShaderBuilder*,
406 const GrDrawEffect&,
407 EffectKey,
408 const char* outputColor,
409 const char* inputColor,
410 const TransformedCoordsArray&,
411 const TextureSamplerArray&) SK_OVERRIDE;
412
413 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
414
415 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
416
417 private:
418 typedef GrGLUniformManager::UniformHandle UniformHandle;
419 typedef SkMatrixConvolutionImageFilter::TileMode TileMode;
420 SkISize fKernelSize;
421 TileMode fTileMode;
422 bool fConvolveAlpha;
423
424 UniformHandle fBoundsUni;
425 UniformHandle fKernelUni;
426 UniformHandle fImageIncrementUni;
427 UniformHandle fKernelOffsetUni;
428 UniformHandle fGainUni;
429 UniformHandle fBiasUni;
430
431 typedef GrGLEffect INHERITED;
432 };
433
434 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa ctory& factory,
435 const GrDrawEffect& dra wEffect)
436 : INHERITED(factory) {
437 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti onEffect>();
438 fKernelSize = m.kernelSize();
439 fTileMode = m.tileMode();
440 fConvolveAlpha = m.convolveAlpha();
441 }
442
443 static void appendTextureLookup(GrGLShaderBuilder* builder,
444 const GrGLShaderBuilder::TextureSampler& sampler ,
445 const char* coord,
446 const char* bounds,
447 SkMatrixConvolutionImageFilter::TileMode tileMod e) {
448 SkString clampedCoord;
449 switch (tileMode) {
450 case SkMatrixConvolutionImageFilter::kClamp_TileMode:
451 clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds );
452 coord = clampedCoord.c_str();
453 break;
454 case SkMatrixConvolutionImageFilter::kRepeat_TileMode:
455 clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds);
456 coord = clampedCoord.c_str();
457 break;
458 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode:
459 builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0 , 0) : ", coord, bounds, bounds, coord);
460 break;
461 }
462 builder->fsAppendTextureLookup(sampler, coord);
463 }
464
465 void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
466 const GrDrawEffect&,
467 EffectKey key,
468 const char* outputColor,
469 const char* inputColor,
470 const TransformedCoordsArray& coords,
471 const TextureSamplerArray& samplers) {
472 sk_ignore_unused_variable(inputColor);
473 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
474 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
475 kVec4f_GrSLType, "Bounds");
476 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity,
477 kVec2f_GrSLType, "ImageIncrement");
478 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibilit y,
479 kFloat_GrSLType,
480 "Kernel",
481 fKernelSize.width() * fKernelSize.h eight());
482 fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty,
483 kVec2f_GrSLType, "KernelOffset");
484 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
485 kFloat_GrSLType, "Gain");
486 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
487 kFloat_GrSLType, "Bias");
488
489 const char* bounds = builder->getUniformCStr(fBoundsUni);
490 const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni);
491 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
492 const char* kernel = builder->getUniformCStr(fKernelUni);
493 const char* gain = builder->getUniformCStr(fGainUni);
494 const char* bias = builder->getUniformCStr(fBiasUni);
495 int kWidth = fKernelSize.width();
496 int kHeight = fKernelSize.height();
497
498 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n");
499 builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), kernelOffset, imgInc);
500 builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight);
501 builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth);
502 builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth );
503 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", i mgInc);
504 builder->fsCodeAppend("\t\t\t\tvec4 c = ");
505 appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode);
506 builder->fsCodeAppend(";\n");
507 if (!fConvolveAlpha) {
508 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n");
509 }
510 builder->fsCodeAppend("\t\t\t\tsum += c * k;\n");
511 builder->fsCodeAppend("\t\t\t}\n");
512 builder->fsCodeAppend("\t\t}\n");
513 if (fConvolveAlpha) {
514 builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, b ias);
515 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n",
516 outputColor, outputColor, outputColor);
517 } else {
518 builder->fsCodeAppend("\t\tvec4 c = ");
519 appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTil eMode);
520 builder->fsCodeAppend(";\n");
521 builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor);
522 builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias);
523 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor );
524 }
525 }
526
527 namespace {
528
529 int encodeXY(int x, int y) {
530 SkASSERT(x >= 1 && y >= 1 && x * y <= 32);
531 if (y < x)
532 return 0x40 | encodeXY(y, x);
533 else
534 return (0x40 >> x) | (y - x);
535 }
536
537 };
538
539 GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& dr awEffect,
540 const GrGLCaps&) {
541 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti onEffect>();
542 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height());
543 key |= m.tileMode() << 7;
544 key |= m.convolveAlpha() ? 1 << 9 : 0;
545 return key;
546 }
547
548 void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman,
549 const GrDrawEffect& drawEffect) {
550 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol utionEffect>();
551 GrTexture& texture = *conv.texture(0);
552 // the code we generated was for a specific kernel size
553 SkASSERT(conv.kernelSize() == fKernelSize);
554 SkASSERT(conv.tileMode() == fTileMode);
555 float imageIncrement[2];
556 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
557 imageIncrement[0] = 1.0f / texture.width();
558 imageIncrement[1] = ySign / texture.height();
559 uman.set2fv(fImageIncrementUni, 1, imageIncrement);
560 uman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset());
561 uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ker nel());
562 uman.set1f(fGainUni, conv.gain());
563 uman.set1f(fBiasUni, conv.bias());
564 const SkIRect& bounds = conv.bounds();
565 float left = (float) bounds.left() / texture.width();
566 float top = (float) bounds.top() / texture.height();
567 float right = (float) bounds.right() / texture.width();
568 float bottom = (float) bounds.bottom() / texture.height();
569 if (texture.origin() == kBottomLeft_GrSurfaceOrigin) {
570 uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top);
571 } else {
572 uman.set4f(fBoundsUni, left, top, right, bottom);
573 }
574 }
575
576 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture,
577 const SkIRect& bounds,
578 const SkISize& kernelSize,
579 const SkScalar* kernel,
580 SkScalar gain,
581 SkScalar bias,
582 const SkIPoint& kernelOffse t,
583 TileMode tileMode,
584 bool convolveAlpha)
585 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)),
586 fBounds(bounds),
587 fKernelSize(kernelSize),
588 fGain(SkScalarToFloat(gain)),
589 fBias(SkScalarToFloat(bias) / 255.0f),
590 fTileMode(tileMode),
591 fConvolveAlpha(convolveAlpha) {
592 fKernel = new float[kernelSize.width() * kernelSize.height()];
593 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) {
594 fKernel[i] = SkScalarToFloat(kernel[i]);
595 }
596 fKernelOffset[0] = static_cast<float>(kernelOffset.x());
597 fKernelOffset[1] = static_cast<float>(kernelOffset.y());
598 this->setWillNotUseInputColor();
599 }
600
601 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() {
602 delete[] fKernel;
603 }
604
605 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const {
606 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance();
607 }
608
609 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
610 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(s Base);
611 return this->texture(0) == s.texture(0) &&
612 fKernelSize == s.kernelSize() &&
613 !memcmp(fKernel, s.kernel(),
614 fKernelSize.width() * fKernelSize.height() * sizeof(float)) & &
615 fGain == s.gain() &&
616 fBias == s.bias() &&
617 fKernelOffset == s.kernelOffset() &&
618 fTileMode == s.tileMode() &&
619 fConvolveAlpha == s.convolveAlpha();
620 }
621
622 GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect);
623
624 // A little bit less than the minimum # uniforms required by DX9SM2 (32).
625 // Allows for a 5x5 kernel (or 25x1, for that matter).
626 #define MAX_KERNEL_SIZE 25
627
628 GrEffect* GrMatrixConvolutionEffect::TestCreate(SkRandom* random,
629 GrContext* context,
630 const GrDrawTargetCaps&,
631 GrTexture* textures[]) {
632 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
633 GrEffectUnitTest::kAlphaTextureIdx;
634 int width = random->nextRangeU(1, MAX_KERNEL_SIZE);
635 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width);
636 SkISize kernelSize = SkISize::Make(width, height);
637 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]);
638 for (int i = 0; i < width * height; i++) {
639 kernel.get()[i] = random->nextSScalar1();
640 }
641 SkScalar gain = random->nextSScalar1();
642 SkScalar bias = random->nextSScalar1();
643 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.widt h()),
644 random->nextRangeU(0, kernelSize.heig ht()));
645 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->w idth()),
646 random->nextRangeU(0, textures[texIdx]->h eight()),
647 random->nextRangeU(0, textures[texIdx]->w idth()),
648 random->nextRangeU(0, textures[texIdx]->h eight()));
649 TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2));
650 bool convolveAlpha = random->nextBool();
651 return GrMatrixConvolutionEffect::Create(textures[texIdx],
652 bounds,
653 kernelSize,
654 kernel.get(),
655 gain,
656 bias,
657 kernelOffset,
658 tileMode,
659 convolveAlpha);
660 }
661
662 bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffect** effect, 325 bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffect** effect,
663 GrTexture* texture, 326 GrTexture* texture,
664 const SkMatrix&, 327 const SkMatrix&,
665 const SkIRect& bounds 328 const SkIRect& bounds
666 ) const { 329 ) const {
667 if (!effect) { 330 if (!effect) {
668 return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; 331 return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE;
669 } 332 }
670 SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); 333 SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE);
671 *effect = GrMatrixConvolutionEffect::Create(texture, 334 *effect = GrMatrixConvolutionEffect::Create(texture,
672 bounds, 335 bounds,
673 fKernelSize, 336 fKernelSize,
674 fKernel, 337 fKernel,
675 fGain, 338 fGain,
676 fBias, 339 fBias,
677 fKernelOffset, 340 fKernelOffset,
678 fTileMode, 341 fTileMode,
679 fConvolveAlpha); 342 fConvolveAlpha);
680 return true; 343 return true;
681 } 344 }
682
683 ///////////////////////////////////////////////////////////////////////////////
684
685 #endif 345 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698