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

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

Issue 119343003: Fast blurred rectangles on GPU (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: handle null input color, and some comments from Brian Created 7 years 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 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "SkBlurMaskFilter.h" 9 #include "SkBlurMaskFilter.h"
10 #include "SkBlurMask.h" 10 #include "SkBlurMask.h"
11 #include "SkGpuBlurUtils.h" 11 #include "SkGpuBlurUtils.h"
12 #include "SkFlattenableBuffers.h" 12 #include "SkFlattenableBuffers.h"
13 #include "SkMaskFilter.h" 13 #include "SkMaskFilter.h"
14 #include "SkRRect.h" 14 #include "SkRRect.h"
15 #include "SkRTConf.h" 15 #include "SkRTConf.h"
16 #include "SkStringUtils.h" 16 #include "SkStringUtils.h"
17 #include "SkStrokeRec.h" 17 #include "SkStrokeRec.h"
18 18
19 #if SK_SUPPORT_GPU 19 #if SK_SUPPORT_GPU
20 #include "GrContext.h" 20 #include "GrContext.h"
21 #include "GrTexture.h" 21 #include "GrTexture.h"
22 #include "GrEffect.h"
23 #include "gl/GrGLEffect.h"
22 #include "effects/GrSimpleTextureEffect.h" 24 #include "effects/GrSimpleTextureEffect.h"
25 #include "GrTBackendEffectFactory.h"
23 #include "SkGrPixelRef.h" 26 #include "SkGrPixelRef.h"
24 #endif 27 #endif
25 28
26 class SkBlurMaskFilterImpl : public SkMaskFilter { 29 class SkBlurMaskFilterImpl : public SkMaskFilter {
27 public: 30 public:
28 SkBlurMaskFilterImpl(SkScalar sigma, SkBlurMaskFilter::BlurStyle, uint32_t f lags); 31 SkBlurMaskFilterImpl(SkScalar sigma, SkBlurMaskFilter::BlurStyle, uint32_t f lags);
29 32
30 // overrides from SkMaskFilter 33 // overrides from SkMaskFilter
31 virtual SkMask::Format getFormat() const SK_OVERRIDE; 34 virtual SkMask::Format getFormat() const SK_OVERRIDE;
32 virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, 35 virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
33 SkIPoint* margin) const SK_OVERRIDE; 36 SkIPoint* margin) const SK_OVERRIDE;
34 37
35 #if SK_SUPPORT_GPU 38 #if SK_SUPPORT_GPU
36 virtual bool canFilterMaskGPU(const SkRect& devBounds, 39 virtual bool canFilterMaskGPU(const SkRect& devBounds,
37 const SkIRect& clipBounds, 40 const SkIRect& clipBounds,
38 const SkMatrix& ctm, 41 const SkMatrix& ctm,
39 SkRect* maskRect) const SK_OVERRIDE; 42 SkRect* maskRect) const SK_OVERRIDE;
43 virtual bool directFilterMaskGPU(GrContext *context,
44 GrPaint *grp,
45 const SkStrokeRec& strokeRec,
46 const SkPath *path) const SK_OVERRIDE;
47
40 virtual bool filterMaskGPU(GrTexture* src, 48 virtual bool filterMaskGPU(GrTexture* src,
41 const SkRect& maskRect, 49 const SkRect& maskRect,
42 GrTexture** result, 50 GrTexture** result,
43 bool canOverwriteSrc) const; 51 bool canOverwriteSrc) const;
44 #endif 52 #endif
45 53
46 virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE; 54 virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE;
47 55
48 SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;) 56 SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;)
49 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurMaskFilterImpl) 57 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurMaskFilterImpl)
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 503
496 void SkBlurMaskFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const { 504 void SkBlurMaskFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const {
497 this->INHERITED::flatten(buffer); 505 this->INHERITED::flatten(buffer);
498 buffer.writeScalar(fSigma); 506 buffer.writeScalar(fSigma);
499 buffer.writeInt(fBlurStyle); 507 buffer.writeInt(fBlurStyle);
500 buffer.writeUInt(fBlurFlags); 508 buffer.writeUInt(fBlurFlags);
501 } 509 }
502 510
503 #if SK_SUPPORT_GPU 511 #if SK_SUPPORT_GPU
504 512
513 class GrGLRectBlurEffect;
514
515 class GrRectBlurEffect : public GrEffect {
516 public:
517 virtual ~GrRectBlurEffect();
518
519 static const char* Name() { return "RectBlur"; }
520
521 typedef GrGLRectBlurEffect GLEffect;
522
523 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
524 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const SK_OVERRIDE;
525
526 /**
527 * Create a simple filter effect with custom bicubic coefficients.
528 */
529 static GrEffectRef* Create(GrContext *context, unsigned int width, unsigned int height,
530 float sigma) {
531 GrTexture *horizontalScanline, *verticalScanline;
532 bool createdScanlines = CreateScanlineTextures(context, sigma, width, he ight,
533 &horizontalScanline, &ver ticalScanline);
534 if (!createdScanlines) {
535 return NULL;
536 }
537 AutoEffectUnref effect(SkNEW_ARGS(GrRectBlurEffect, (width, height, sigm a,
538 horizontalScanline, verticalScanline)));
539 return CreateEffectRef(effect);
540 }
541
542 unsigned int getWidth() const { return fWidth; }
543 unsigned int getHeight() const { return fHeight; }
544 unsigned int getSigma() const { return fSigma; }
545
546 private:
547 GrRectBlurEffect(unsigned int width, unsigned int height, float sigma,
548 GrTexture *horizontal_scanline, GrTexture *vertical_scanlin e);
549 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
550
551 static bool CreateScanlineTextures(GrContext *context, float sigma,
552 unsigned int width, unsigned int height,
553 GrTexture **horizontalScanline,
554 GrTexture **verticalScanline);
555
556 unsigned int fWidth, fHeight;
557 float fSigma;
558 GrTextureAccess fHorizontalScanlineAccess;
559 GrTextureAccess fVerticalScanlineAccess;
560 GrCoordTransform fTrivialTransform;
561
562 GR_DECLARE_EFFECT_TEST;
563
564 typedef GrEffect INHERITED;
565 };
566
567 class GrGLRectBlurEffect : public GrGLEffect {
568 public:
569 GrGLRectBlurEffect(const GrBackendEffectFactory& factory,
570 const GrDrawEffect&);
571 virtual void emitCode(GrGLShaderBuilder*,
572 const GrDrawEffect&,
573 EffectKey,
574 const char* outputColor,
575 const char* inputColor,
576 const TransformedCoordsArray&,
577 const TextureSamplerArray&) SK_OVERRIDE;
578
579 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
580
581 private:
582 typedef GrGLUniformManager::UniformHandle UniformHandle;
583
584 UniformHandle fWidthUni;
585 UniformHandle fHeightUni;
586
587 typedef GrGLEffect INHERITED;
588 };
589
590 GrGLRectBlurEffect::GrGLRectBlurEffect(const GrBackendEffectFactory& factory, co nst GrDrawEffect&)
591 : INHERITED(factory) {
592 }
593
594 void GrGLRectBlurEffect::emitCode(GrGLShaderBuilder* builder,
595 const GrDrawEffect&,
596 EffectKey key,
597 const char* outputColor,
598 const char* inputColor,
599 const TransformedCoordsArray& coords,
600 const TextureSamplerArray& samplers) {
601
602 SkString texture_coords = builder->ensureFSCoords2D(coords, 0);
603
604 if (inputColor) {
605 builder->fsCodeAppendf("\tvec4 src=%s;\n", inputColor);
606 } else {
607 builder->fsCodeAppendf("\tvec4 src=vec4(1)\n;");
608 }
609
610 builder->fsCodeAppendf("\tvec4 horiz = ");
611 builder->fsAppendTextureLookup( samplers[0], texture_coords.c_str() );
612 builder->fsCodeAppendf(";\n");
613 builder->fsCodeAppendf("\tvec4 vert = ");
614 builder->fsAppendTextureLookup( samplers[1], texture_coords.c_str() );
615 builder->fsCodeAppendf(";\n");
616
617 builder->fsCodeAppendf("\tfloat final = (horiz*vert).r;\n");
618 builder->fsCodeAppendf("\t%s = final*src;\n", outputColor);
619 }
620
621 void GrGLRectBlurEffect::setData(const GrGLUniformManager& uman,
622 const GrDrawEffect& drawEffect) {
623 }
624
625 bool GrRectBlurEffect::CreateScanlineTextures(GrContext *context, float sigma,
626 unsigned int width, unsigned int h eight,
627 GrTexture **horizontalScanline,
628 GrTexture **verticalScanline) {
629 GrTextureParams params;
630 GrTextureDesc texDesc;
631
632 unsigned int profile_size = 6*sigma;
633
634 texDesc.fWidth = width;
635 texDesc.fHeight = 1;
636 texDesc.fConfig = kAlpha_8_GrPixelConfig;
637
638 static const GrCacheID::Domain gBlurProfileDomain = GrCacheID::GenerateDomai n();
639 GrCacheID::Key key;
640 memset(&key, 0, sizeof(key));
641 key.fData32[0] = profile_size;
642 key.fData32[1] = width;
643 key.fData32[2] = 1;
644 GrCacheID horizontalCacheID(gBlurProfileDomain, key);
645
646 uint8_t *profile = NULL;
647 SkAutoTDeleteArray<uint8_t> ada(profile);
648
649 *horizontalScanline = context->findAndRefTexture(texDesc, horizontalCacheID, &params);
650
651 if (NULL == *horizontalScanline) {
652
653 SkBlurMask::ComputeBlurProfile(sigma, &profile);
654
655 SkAutoTMalloc<uint8_t> horizontalPixels(width);
656 SkBlurMask::ComputeBlurredScanline(horizontalPixels, profile, profile_si ze, width, sigma);
657
658 *horizontalScanline = context->createTexture(&params, texDesc, horizonta lCacheID,
659 horizontalPixels, 0);
660
661 if (NULL == *horizontalScanline) {
662 return false;
663 }
664 }
665
666 texDesc.fWidth = 1;
667 texDesc.fHeight = height;
668 key.fData32[1] = 1;
669 key.fData32[2] = height;
670 GrCacheID verticalCacheID(gBlurProfileDomain, key);
671
672 *verticalScanline = context->findAndRefTexture(texDesc, verticalCacheID, &pa rams);
673 if (NULL == *verticalScanline) {
674 if (NULL == profile) {
675 SkBlurMask::ComputeBlurProfile(sigma, &profile);
676 }
677
678 SkAutoTMalloc<uint8_t> verticalPixels(height);
679 SkBlurMask::ComputeBlurredScanline(verticalPixels, profile, profile_size , height, sigma);
680
681 *verticalScanline = context->createTexture(&params, texDesc, verticalCac heID,
682 verticalPixels, 0);
683
684 if (NULL == *verticalScanline) {
685 return false;
686 }
687
688 }
689 return true;
690 }
691
692 GrRectBlurEffect::GrRectBlurEffect(unsigned int width, unsigned int height, floa t sigma,
693 GrTexture *horizontal_scanline, GrTexture *ve rtical_scanline)
694 : INHERITED(),
695 fWidth(width),
696 fHeight(height),
697 fSigma(sigma),
698 fHorizontalScanlineAccess(horizontal_scanline),
699 fVerticalScanlineAccess(vertical_scanline),
700 fTrivialTransform(kLocal_GrCoordSet, SkMatrix::I()) {
701 this->addTextureAccess(&fHorizontalScanlineAccess);
702 this->addTextureAccess(&fVerticalScanlineAccess);
703 this->addCoordTransform(&fTrivialTransform);
704 }
705
706 GrRectBlurEffect::~GrRectBlurEffect() {
707 }
708
709 const GrBackendEffectFactory& GrRectBlurEffect::getFactory() const {
710 return GrTBackendEffectFactory<GrRectBlurEffect>::getInstance();
711 }
712
713 bool GrRectBlurEffect::onIsEqual(const GrEffect& sBase) const {
714 const GrRectBlurEffect& s = CastEffect<GrRectBlurEffect>(sBase);
715 return this->getWidth() == s.getWidth() &&
716 this->getHeight() == s.getHeight() &&
717 this->getSigma() == s.getSigma();
718 }
719
720 void GrRectBlurEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const {
721 *validFlags = 0;
722 return;
723 }
724
725 GR_DEFINE_EFFECT_TEST(GrRectBlurEffect);
726
727 GrEffectRef* GrRectBlurEffect::TestCreate(SkRandom* random,
728 GrContext* context,
729 const GrDrawTargetCaps&,
730 GrTexture**) {
731 float sigma = random->nextRangeF(3,8);
732 unsigned int width = random->nextRangeU(200,300);
733 unsigned int height = random->nextRangeU(200,300);
734 return GrRectBlurEffect::Create(context, width, height, sigma);
735 }
736
737
738 bool SkBlurMaskFilterImpl::directFilterMaskGPU(GrContext *context,
739 GrPaint *grp,
740 const SkStrokeRec& strokeRec,
741 const SkPath *path) const {
742 GrContext::AutoMatrix am;
743 if (!am.setIdentity(context, grp)) {
744 return false;
745 }
746
747 SkRect rect;
748 if (!path->isRect(&rect)) {
749 return false;
750 }
751
752 if (!strokeRec.isFillStyle()) {
753 return false;
754 }
755
756 rect.outset(3*fSigma, 3*fSigma);
757
758 SkAutoTUnref<GrEffectRef> effect(GrRectBlurEffect::Create(
759 context, rect.width(), rect.height(), fSigma));
760 if (!effect) {
761 return false;
762 }
763 grp->addCoverageEffect(effect);
764
765 context->drawRectToRect(*grp, rect, SkRect::MakeWH(1,1));
766 return true;
767 }
768
505 bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds, 769 bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds,
506 const SkIRect& clipBounds, 770 const SkIRect& clipBounds,
507 const SkMatrix& ctm, 771 const SkMatrix& ctm,
508 SkRect* maskRect) const { 772 SkRect* maskRect) const {
509 SkScalar xformedSigma = this->computeXformedSigma(ctm); 773 SkScalar xformedSigma = this->computeXformedSigma(ctm);
510 if (xformedSigma <= 0) { 774 if (xformedSigma <= 0) {
511 return false; 775 return false;
512 } 776 }
513 777
514 static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64); 778 static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 } else { 879 } else {
616 str->append("None"); 880 str->append("None");
617 } 881 }
618 str->append("))"); 882 str->append("))");
619 } 883 }
620 #endif 884 #endif
621 885
622 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter) 886 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter)
623 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl) 887 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl)
624 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 888 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698