Chromium Code Reviews| Index: src/effects/SkBlurMaskFilter.cpp |
| diff --git a/src/effects/SkBlurMaskFilter.cpp b/src/effects/SkBlurMaskFilter.cpp |
| index fa31743bb3adac1033b2da39725ea30bfe57bc62..f51872a260224b4f5bd15f845c974de2f8ac2370 100644 |
| --- a/src/effects/SkBlurMaskFilter.cpp |
| +++ b/src/effects/SkBlurMaskFilter.cpp |
| @@ -19,7 +19,10 @@ |
| #if SK_SUPPORT_GPU |
| #include "GrContext.h" |
| #include "GrTexture.h" |
| +#include "GrEffect.h" |
| +#include "gl/GrGLEffect.h" |
| #include "effects/GrSimpleTextureEffect.h" |
| +#include "GrTBackendEffectFactory.h" |
| #include "SkGrPixelRef.h" |
| #endif |
| @@ -37,6 +40,11 @@ public: |
| const SkIRect& clipBounds, |
| const SkMatrix& ctm, |
| SkRect* maskRect) const SK_OVERRIDE; |
| + virtual bool directFilterMaskGPU(GrContext* context, |
| + GrPaint* grp, |
| + const SkStrokeRec& strokeRec, |
| + const SkPath& path) const SK_OVERRIDE; |
| + |
| virtual bool filterMaskGPU(GrTexture* src, |
| const SkMatrix& ctm, |
| const SkRect& maskRect, |
| @@ -503,6 +511,274 @@ void SkBlurMaskFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const { |
| #if SK_SUPPORT_GPU |
| +class GrGLRectBlurEffect; |
| + |
| +class GrRectBlurEffect : public GrEffect { |
| +public: |
| + virtual ~GrRectBlurEffect(); |
| + |
| + static const char* Name() { return "RectBlur"; } |
| + |
| + typedef GrGLRectBlurEffect GLEffect; |
| + |
| + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| + virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| + |
| + /** |
| + * Create a simple filter effect with custom bicubic coefficients. |
| + */ |
| + static GrEffectRef* Create(GrContext *context, const SkRect& rect, |
| + float sigma) { |
| + GrTexture *horizontalScanline, *verticalScanline; |
| + bool createdScanlines = CreateScanlineTextures(context, sigma, |
| + SkScalarCeilToInt(rect.width()), |
| + SkScalarCeilToInt(rect.height()), |
| + &horizontalScanline, &verticalScanline); |
| + if (!createdScanlines) { |
| + return NULL; |
| + } |
| + AutoEffectUnref effect(SkNEW_ARGS(GrRectBlurEffect, (rect, sigma, |
| + horizontalScanline, verticalScanline))); |
| + return CreateEffectRef(effect); |
| + } |
| + |
| + unsigned int getWidth() const { return fWidth; } |
| + unsigned int getHeight() const { return fHeight; } |
| + float getSigma() const { return fSigma; } |
| + |
| +private: |
| + GrRectBlurEffect(const SkRect& rect, float sigma, |
| + GrTexture *horizontal_scanline, GrTexture *vertical_scanline); |
| + virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
| + |
| + static bool CreateScanlineTextures(GrContext *context, float sigma, |
| + unsigned int width, unsigned int height, |
| + GrTexture **horizontalScanline, |
| + GrTexture **verticalScanline); |
| + |
| + unsigned int fWidth, fHeight; |
| + float fSigma; |
| + GrTextureAccess fHorizontalScanlineAccess; |
| + GrTextureAccess fVerticalScanlineAccess; |
| + GrCoordTransform fTrivialTransform; |
| + |
| + GR_DECLARE_EFFECT_TEST; |
| + |
| + typedef GrEffect INHERITED; |
| +}; |
| + |
| +class GrGLRectBlurEffect : public GrGLEffect { |
| +public: |
| + GrGLRectBlurEffect(const GrBackendEffectFactory& factory, |
| + const GrDrawEffect&); |
| + virtual void emitCode(GrGLShaderBuilder*, |
| + const GrDrawEffect&, |
| + EffectKey, |
| + const char* outputColor, |
| + const char* inputColor, |
| + const TransformedCoordsArray&, |
| + const TextureSamplerArray&) SK_OVERRIDE; |
| + |
| + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| + |
| +private: |
| + typedef GrGLUniformManager::UniformHandle UniformHandle; |
| + |
| + UniformHandle fWidthUni; |
| + UniformHandle fHeightUni; |
| + |
| + typedef GrGLEffect INHERITED; |
| +}; |
| + |
| +GrGLRectBlurEffect::GrGLRectBlurEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| + : INHERITED(factory) { |
| +} |
| + |
| +void GrGLRectBlurEffect::emitCode(GrGLShaderBuilder* builder, |
| + const GrDrawEffect&, |
| + EffectKey key, |
| + const char* outputColor, |
| + const char* inputColor, |
| + const TransformedCoordsArray& coords, |
| + const TextureSamplerArray& samplers) { |
| + |
| + SkString texture_coords = builder->ensureFSCoords2D(coords, 0); |
| + |
| + if (inputColor) { |
| + builder->fsCodeAppendf("\tvec4 src=%s;\n", inputColor); |
| + } else { |
| + builder->fsCodeAppendf("\tvec4 src=vec4(1)\n;"); |
| + } |
| + |
| + builder->fsCodeAppendf("\tvec4 horiz = "); |
| + builder->fsAppendTextureLookup( samplers[0], texture_coords.c_str() ); |
| + builder->fsCodeAppendf(";\n"); |
| + builder->fsCodeAppendf("\tvec4 vert = "); |
| + builder->fsAppendTextureLookup( samplers[1], texture_coords.c_str() ); |
| + builder->fsCodeAppendf(";\n"); |
| + |
| + builder->fsCodeAppendf("\tfloat final = (horiz*vert).r;\n"); |
| + builder->fsCodeAppendf("\t%s = final*src;\n", outputColor); |
| + // builder->fsCodeAppendf("\t%s = vec4(%s.r,%s.g,0,1);\n", outputColor, texture_coords.c_str(), texture_coords.c_str()); |
|
bsalomon
2014/01/27 19:07:00
should we remove this line?
|
| +} |
| + |
| +void GrGLRectBlurEffect::setData(const GrGLUniformManager& uman, |
| + const GrDrawEffect& drawEffect) { |
| +} |
| + |
| +bool GrRectBlurEffect::CreateScanlineTextures(GrContext *context, float sigma, |
| + unsigned int width, unsigned int height, |
| + GrTexture **horizontalScanline, |
| + GrTexture **verticalScanline) { |
| + GrTextureParams params; |
| + GrTextureDesc texDesc; |
| + |
| + unsigned int profile_size = SkScalarFloorToInt(6*sigma); |
| + |
| + texDesc.fWidth = width; |
| + texDesc.fHeight = 1; |
| + texDesc.fConfig = kAlpha_8_GrPixelConfig; |
| + |
| + static const GrCacheID::Domain gBlurProfileDomain = GrCacheID::GenerateDomain(); |
| + GrCacheID::Key key; |
| + memset(&key, 0, sizeof(key)); |
| + key.fData32[0] = profile_size; |
| + key.fData32[1] = width; |
| + key.fData32[2] = 1; |
| + GrCacheID horizontalCacheID(gBlurProfileDomain, key); |
|
bsalomon
2014/01/27 19:07:00
should we make both the horizontal and vertical te
|
| + |
| + uint8_t *profile = NULL; |
| + SkAutoTDeleteArray<uint8_t> ada(profile); |
| + |
| + *horizontalScanline = context->findAndRefTexture(texDesc, horizontalCacheID, ¶ms); |
| + |
| + if (NULL == *horizontalScanline) { |
| + |
| + SkBlurMask::ComputeBlurProfile(sigma, &profile); |
| + |
| + SkAutoTMalloc<uint8_t> horizontalPixels(width); |
| + SkBlurMask::ComputeBlurredScanline(horizontalPixels, profile, width, sigma); |
| + |
| + *horizontalScanline = context->createTexture(¶ms, texDesc, horizontalCacheID, |
| + horizontalPixels, 0); |
| + |
| + if (NULL == *horizontalScanline) { |
| + return false; |
| + } |
| + } |
| + |
| + texDesc.fWidth = 1; |
| + texDesc.fHeight = height; |
| + key.fData32[1] = 1; |
| + key.fData32[2] = height; |
| + GrCacheID verticalCacheID(gBlurProfileDomain, key); |
| + |
| + *verticalScanline = context->findAndRefTexture(texDesc, verticalCacheID, ¶ms); |
| + if (NULL == *verticalScanline) { |
| + if (NULL == profile) { |
| + SkBlurMask::ComputeBlurProfile(sigma, &profile); |
| + } |
| + |
| + SkAutoTMalloc<uint8_t> verticalPixels(height); |
| + SkBlurMask::ComputeBlurredScanline(verticalPixels, profile, height, sigma); |
| + |
| + *verticalScanline = context->createTexture(¶ms, texDesc, verticalCacheID, |
| + verticalPixels, 0); |
| + |
| + if (NULL == *verticalScanline) { |
| + return false; |
| + } |
| + |
| + } |
| + return true; |
| +} |
| + |
| +GrRectBlurEffect::GrRectBlurEffect(const SkRect& rect, float sigma, |
| + GrTexture *horizontal_scanline, GrTexture *vertical_scanline) |
| + : INHERITED(), |
| + fWidth(horizontal_scanline->width()), |
| + fHeight(vertical_scanline->width()), |
| + fSigma(sigma), |
| + fHorizontalScanlineAccess(horizontal_scanline), |
| + fVerticalScanlineAccess(vertical_scanline) { |
| + SkMatrix mat; |
| + mat.setRectToRect(rect, SkRect::MakeWH(1,1), SkMatrix::kFill_ScaleToFit); |
| + fTrivialTransform = GrCoordTransform(kLocal_GrCoordSet, mat); |
|
bsalomon
2014/01/27 19:07:00
Is this the right name for this now (trivial)?
|
| + this->addTextureAccess(&fHorizontalScanlineAccess); |
| + this->addTextureAccess(&fVerticalScanlineAccess); |
| + this->addCoordTransform(&fTrivialTransform); |
| +} |
| + |
| +GrRectBlurEffect::~GrRectBlurEffect() { |
| +} |
| + |
| +const GrBackendEffectFactory& GrRectBlurEffect::getFactory() const { |
| + return GrTBackendEffectFactory<GrRectBlurEffect>::getInstance(); |
| +} |
| + |
| +bool GrRectBlurEffect::onIsEqual(const GrEffect& sBase) const { |
| + const GrRectBlurEffect& s = CastEffect<GrRectBlurEffect>(sBase); |
| + return this->getWidth() == s.getWidth() && |
| + this->getHeight() == s.getHeight() && |
| + this->getSigma() == s.getSigma(); |
| +} |
| + |
| +void GrRectBlurEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| + *validFlags = 0; |
| + return; |
| +} |
| + |
| +GR_DEFINE_EFFECT_TEST(GrRectBlurEffect); |
| + |
| +GrEffectRef* GrRectBlurEffect::TestCreate(SkRandom* random, |
| + GrContext* context, |
| + const GrDrawTargetCaps&, |
| + GrTexture**) { |
| + float sigma = random->nextRangeF(3,8); |
| + float width = random->nextRangeF(200,300); |
| + float height = random->nextRangeF(200,300); |
| + return GrRectBlurEffect::Create(context, SkRect::MakeWH(width, height), sigma); |
| +} |
| + |
| + |
| +bool SkBlurMaskFilterImpl::directFilterMaskGPU(GrContext* context, |
| + GrPaint* grp, |
| + const SkStrokeRec& strokeRec, |
| + const SkPath& path) const { |
| + if (fBlurStyle != SkBlurMaskFilter::kNormal_BlurStyle) { |
| + return false; |
| + } |
| + |
| + SkRect rect; |
| + if (!path.isRect(&rect)) { |
| + return false; |
| + } |
| + |
| + if (!strokeRec.isFillStyle()) { |
| + return false; |
| + } |
| + |
| + GrContext::AutoMatrix am; |
| + if (!am.setIdentity(context, grp)) { |
| + return false; |
| + } |
| + |
| + SkMatrix ctm = context->getMatrix(); |
|
bsalomon
2014/01/27 19:07:00
this is now guaranteed to be identity because of t
|
| + SkScalar xformedSigma = this->computeXformedSigma(ctm); |
| + rect.outset(3*xformedSigma, 3*xformedSigma); |
| + |
| + SkAutoTUnref<GrEffectRef> effect(GrRectBlurEffect::Create( |
| + context, rect, xformedSigma)); |
| + if (!effect) { |
| + return false; |
| + } |
| + grp->addCoverageEffect(effect); |
| + |
| + context->drawRect(*grp, rect); |
| + // context->drawRectToRect(*grp, rect, SkRect::MakeWH(1,1)); |
| + return true; |
| +} |
| + |
| bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds, |
| const SkIRect& clipBounds, |
| const SkMatrix& ctm, |