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

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: Second rework of 119343003 for 10.6 and excluding GMs pending rebaseline Created 6 years, 10 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
« no previous file with comments | « src/effects/SkBlurMask.cpp ('k') | src/gpu/SkGpuDevice.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 SkMatrix& ctm, 49 const SkMatrix& ctm,
42 const SkRect& maskRect, 50 const SkRect& maskRect,
43 GrTexture** result, 51 GrTexture** result,
44 bool canOverwriteSrc) const SK_OVERRIDE; 52 bool canOverwriteSrc) const SK_OVERRIDE;
45 #endif 53 #endif
46 54
47 virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE; 55 virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE;
48 56
49 SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;) 57 SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;)
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 501
494 void SkBlurMaskFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const { 502 void SkBlurMaskFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const {
495 this->INHERITED::flatten(buffer); 503 this->INHERITED::flatten(buffer);
496 buffer.writeScalar(fSigma); 504 buffer.writeScalar(fSigma);
497 buffer.writeInt(fBlurStyle); 505 buffer.writeInt(fBlurStyle);
498 buffer.writeUInt(fBlurFlags); 506 buffer.writeUInt(fBlurFlags);
499 } 507 }
500 508
501 #if SK_SUPPORT_GPU 509 #if SK_SUPPORT_GPU
502 510
511 class GrGLRectBlurEffect;
512
513 class GrRectBlurEffect : public GrEffect {
514 public:
515 virtual ~GrRectBlurEffect();
516
517 static const char* Name() { return "RectBlur"; }
518
519 typedef GrGLRectBlurEffect GLEffect;
520
521 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
522 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const SK_OVERRIDE;
523
524 /**
525 * Create a simple filter effect with custom bicubic coefficients.
526 */
527 static GrEffectRef* Create(GrContext *context, const SkRect& rect,
528 float sigma) {
529 GrTexture *horizontalScanline, *verticalScanline;
530 bool createdScanlines = CreateScanlineTextures(context, sigma,
531 SkScalarCeilToInt(rect.wi dth()),
532 SkScalarCeilToInt(rect.he ight()),
533 &horizontalScanline, &ver ticalScanline);
534 if (!createdScanlines) {
535 return NULL;
536 }
537 AutoEffectUnref effect(SkNEW_ARGS(GrRectBlurEffect, (rect, sigma,
538 horizontalScanline, verticalScanline)));
539 return CreateEffectRef(effect);
540 }
541
542 unsigned int getWidth() const { return fWidth; }
543 unsigned int getHeight() const { return fHeight; }
544 float getSigma() const { return fSigma; }
545
546 private:
547 GrRectBlurEffect(const SkRect& rect, 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 fTransform;
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 = SkScalarFloorToInt(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, width, sig ma);
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, height, sigm a);
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(const SkRect& rect, float sigma,
693 GrTexture *horizontal_scanline, GrTexture *ve rtical_scanline)
694 : INHERITED(),
695 fWidth(horizontal_scanline->width()),
696 fHeight(vertical_scanline->width()),
697 fSigma(sigma),
698 fHorizontalScanlineAccess(horizontal_scanline),
699 fVerticalScanlineAccess(vertical_scanline) {
700 SkMatrix mat;
701 mat.setRectToRect(rect, SkRect::MakeWH(1,1), SkMatrix::kFill_ScaleToFit);
702 fTransform.reset(kLocal_GrCoordSet, mat);
703 this->addTextureAccess(&fHorizontalScanlineAccess);
704 this->addTextureAccess(&fVerticalScanlineAccess);
705 this->addCoordTransform(&fTransform);
706 }
707
708 GrRectBlurEffect::~GrRectBlurEffect() {
709 }
710
711 const GrBackendEffectFactory& GrRectBlurEffect::getFactory() const {
712 return GrTBackendEffectFactory<GrRectBlurEffect>::getInstance();
713 }
714
715 bool GrRectBlurEffect::onIsEqual(const GrEffect& sBase) const {
716 const GrRectBlurEffect& s = CastEffect<GrRectBlurEffect>(sBase);
717 return this->getWidth() == s.getWidth() &&
718 this->getHeight() == s.getHeight() &&
719 this->getSigma() == s.getSigma();
720 }
721
722 void GrRectBlurEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const {
723 *validFlags = 0;
724 return;
725 }
726
727 GR_DEFINE_EFFECT_TEST(GrRectBlurEffect);
728
729 GrEffectRef* GrRectBlurEffect::TestCreate(SkRandom* random,
730 GrContext* context,
731 const GrDrawTargetCaps&,
732 GrTexture**) {
733 float sigma = random->nextRangeF(3,8);
734 float width = random->nextRangeF(200,300);
735 float height = random->nextRangeF(200,300);
736 return GrRectBlurEffect::Create(context, SkRect::MakeWH(width, height), sigm a);
737 }
738
739
740 bool SkBlurMaskFilterImpl::directFilterMaskGPU(GrContext* context,
741 GrPaint* grp,
742 const SkStrokeRec& strokeRec,
743 const SkPath& path) const {
744 if (fBlurStyle != SkBlurMaskFilter::kNormal_BlurStyle) {
745 return false;
746 }
747
748 SkRect rect;
749 if (!path.isRect(&rect)) {
750 return false;
751 }
752
753 if (!strokeRec.isFillStyle()) {
754 return false;
755 }
756
757 SkMatrix ctm = context->getMatrix();
758 SkScalar xformedSigma = this->computeXformedSigma(ctm);
759 rect.outset(3*xformedSigma, 3*xformedSigma);
760
761 SkAutoTUnref<GrEffectRef> effect(GrRectBlurEffect::Create(
762 context, rect, xformedSigma));
763 if (!effect) {
764 return false;
765 }
766
767 GrContext::AutoMatrix am;
768 if (!am.setIdentity(context, grp)) {
769 return false;
770 }
771
772
773 grp->addCoverageEffect(effect);
774
775 context->drawRect(*grp, rect);
776 return true;
777 }
778
503 bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds, 779 bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds,
504 const SkIRect& clipBounds, 780 const SkIRect& clipBounds,
505 const SkMatrix& ctm, 781 const SkMatrix& ctm,
506 SkRect* maskRect) const { 782 SkRect* maskRect) const {
507 SkScalar xformedSigma = this->computeXformedSigma(ctm); 783 SkScalar xformedSigma = this->computeXformedSigma(ctm);
508 if (xformedSigma <= 0) { 784 if (xformedSigma <= 0) {
509 return false; 785 return false;
510 } 786 }
511 787
512 static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64); 788 static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64);
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 } else { 890 } else {
615 str->append("None"); 891 str->append("None");
616 } 892 }
617 str->append("))"); 893 str->append("))");
618 } 894 }
619 #endif 895 #endif
620 896
621 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter) 897 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter)
622 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl) 898 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl)
623 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 899 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW
« no previous file with comments | « src/effects/SkBlurMask.cpp ('k') | src/gpu/SkGpuDevice.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698