robertphillips 2016-05-12 11:13:00 -07:00 committed by Commit bot
parent f05ab1b820
commit 04c84af877
4 changed files with 81 additions and 129 deletions

View File

@ -121,20 +121,21 @@ sk_sp<SkSpecialImage> SkBlurImageFilter::onFilterImage(SkSpecialImage* source,
inputBounds.offset(-inputOffset); inputBounds.offset(-inputOffset);
dstBounds.offset(-inputOffset); dstBounds.offset(-inputOffset);
SkRect inputBoundsF(SkRect::Make(inputBounds)); SkRect inputBoundsF(SkRect::Make(inputBounds));
sk_sp<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(context, sk_sp<GrDrawContext> drawContext(SkGpuBlurUtils::GaussianBlur(
inputTexture.get(), context,
source->props().isGammaCorrect(), inputTexture.get(),
SkRect::Make(dstBounds), source->props().isGammaCorrect(),
&inputBoundsF, SkRect::Make(dstBounds),
sigma.x(), &inputBoundsF,
sigma.y())); sigma.x(),
if (!tex) { sigma.y()));
if (!drawContext) {
return nullptr; return nullptr;
} }
return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(dstBounds.width(), dstBounds.height()), return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(dstBounds.width(), dstBounds.height()),
kNeedNewImageUniqueID_SpecialImage, kNeedNewImageUniqueID_SpecialImage,
std::move(tex), &source->props()); drawContext->asTexture(), &source->props());
} }
#endif #endif

View File

@ -1248,10 +1248,11 @@ bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src,
// If we're doing a normal blur, we can clobber the pathTexture in the // If we're doing a normal blur, we can clobber the pathTexture in the
// gaussianBlur. Otherwise, we need to save it for later compositing. // gaussianBlur. Otherwise, we need to save it for later compositing.
bool isNormalBlur = (kNormal_SkBlurStyle == fBlurStyle); bool isNormalBlur = (kNormal_SkBlurStyle == fBlurStyle);
*result = SkGpuBlurUtils::GaussianBlur(context, src, isNormalBlur && canOverwriteSrc, sk_sp<GrDrawContext> drawContext(SkGpuBlurUtils::GaussianBlur(context, src,
clipRect, nullptr, isNormalBlur && canOverwriteSrc,
xformedSigma, xformedSigma); clipRect, nullptr,
if (nullptr == *result) { xformedSigma, xformedSigma));
if (!drawContext) {
return false; return false;
} }
@ -1276,15 +1277,10 @@ bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src,
paint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); paint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
} }
sk_sp<GrDrawContext> drawContext(
context->drawContext(sk_ref_sp((*result)->asRenderTarget())));
if (!drawContext) {
return false;
}
drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), clipRect); drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), clipRect);
} }
*result = drawContext->asTexture().release();
return true; return true;
} }

View File

@ -163,13 +163,13 @@ static void convolve_gaussian(GrDrawContext* drawContext,
} }
} }
GrTexture* GaussianBlur(GrContext* context, sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
GrTexture* srcTexture, GrTexture* origSrc,
bool gammaCorrect, bool gammaCorrect,
const SkRect& dstBounds, const SkRect& dstBounds,
const SkRect* srcBounds, const SkRect* srcBounds,
float sigmaX, float sigmaX,
float sigmaY) { float sigmaY) {
SkASSERT(context); SkASSERT(context);
SkIRect clearRect; SkIRect clearRect;
int scaleFactorX, radiusX; int scaleFactorX, radiusX;
@ -198,6 +198,8 @@ GrTexture* GaussianBlur(GrContext* context,
// setup new clip // setup new clip
GrClip clip(localDstBounds); GrClip clip(localDstBounds);
sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc));
SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
kRGBA_8888_GrPixelConfig == srcTexture->config() || kRGBA_8888_GrPixelConfig == srcTexture->config() ||
kSRGBA_8888_GrPixelConfig == srcTexture->config() || kSRGBA_8888_GrPixelConfig == srcTexture->config() ||
@ -211,6 +213,14 @@ GrTexture* GaussianBlur(GrContext* context,
const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0, const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0,
SkSurfaceProps::kLegacyFontHost_InitType); SkSurfaceProps::kLegacyFontHost_InitType);
sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox,
width, height, config,
0, kDefault_GrSurfaceOrigin,
&props));
if (!dstDrawContext) {
return nullptr;
}
// For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
// launch a single non separable kernel vs two launches // launch a single non separable kernel vs two launches
if (sigmaX > 0.0f && sigmaY > 0.0f && if (sigmaX > 0.0f && sigmaY > 0.0f &&
@ -218,35 +228,17 @@ GrTexture* GaussianBlur(GrContext* context,
// We shouldn't be scaling because this is a small size blur // We shouldn't be scaling because this is a small size blur
SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox,
width, height, config,
0, kDefault_GrSurfaceOrigin,
&props));
if (!dstDrawContext) {
return nullptr;
}
convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset, convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset,
srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBounds); srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds);
return dstDrawContext->asTexture().release(); return dstDrawContext;
} }
GrSurfaceDesc desc; sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kApprox,
desc.fFlags = kRenderTarget_GrSurfaceFlag; width, height, config,
desc.fWidth = width; 0, kDefault_GrSurfaceOrigin,
desc.fHeight = height; &props));
desc.fConfig = config; if (!tmpDrawContext) {
GrTexture* dstTexture;
GrTexture* tempTexture;
SkAutoTUnref<GrTexture> temp1, temp2;
temp1.reset(context->textureProvider()->createApproxTexture(desc));
dstTexture = temp1.get();
temp2.reset(context->textureProvider()->createApproxTexture(desc));
tempTexture = temp2.get();
if (!dstTexture || !tempTexture) {
return nullptr; return nullptr;
} }
@ -264,7 +256,7 @@ GrTexture* GaussianBlur(GrContext* context,
domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f,
(i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f);
sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create( sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
srcTexture, srcTexture.get(),
matrix, matrix,
domain, domain,
GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode,
@ -274,23 +266,18 @@ GrTexture* GaussianBlur(GrContext* context,
srcOffset.set(0, 0); srcOffset.set(0, 0);
} else { } else {
GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
paint.addColorTextureProcessor(srcTexture, matrix, params); paint.addColorTextureProcessor(srcTexture.get(), matrix, params);
} }
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
i < scaleFactorY ? 0.5f : 1.0f); i < scaleFactorY ? 0.5f : 1.0f);
sk_sp<GrDrawContext> dstDrawContext(
context->drawContext(sk_ref_sp(dstTexture->asRenderTarget())));
if (!dstDrawContext) {
return nullptr;
}
dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
srcDrawContext.swap(dstDrawContext); srcDrawContext = dstDrawContext;
srcRect = dstRect; srcRect = dstRect;
srcTexture = dstTexture; srcTexture = srcDrawContext->asTexture();
SkTSwap(dstTexture, tempTexture); dstDrawContext.swap(tmpDrawContext);
localSrcBounds = srcRect; localSrcBounds = srcRect;
} }
@ -301,13 +288,7 @@ GrTexture* GaussianBlur(GrContext* context,
SkIRect srcIRect = srcRect.roundOut(); SkIRect srcIRect = srcRect.roundOut();
if (sigmaX > 0.0f) { if (sigmaX > 0.0f) {
if (scaleFactorX > 1) { if (scaleFactorX > 1) {
// TODO: if we pass in the source draw context we don't need this here SkASSERT(srcDrawContext);
if (!srcDrawContext) {
srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRenderTarget()));
if (!srcDrawContext) {
return nullptr;
}
}
// Clear out a radius to the right of the srcRect to prevent the // Clear out a radius to the right of the srcRect to prevent the
// X convolution from reading garbage. // X convolution from reading garbage.
@ -316,31 +297,20 @@ GrTexture* GaussianBlur(GrContext* context,
srcDrawContext->clear(&clearRect, 0x0, false); srcDrawContext->clear(&clearRect, 0x0, false);
} }
sk_sp<GrDrawContext> dstDrawContext(
context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props));
if (!dstDrawContext) {
return nullptr;
}
convolve_gaussian(dstDrawContext.get(), clip, srcRect, convolve_gaussian(dstDrawContext.get(), clip, srcRect,
srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
srcBounds, srcOffset); srcBounds, srcOffset);
srcDrawContext.swap(dstDrawContext); srcDrawContext = dstDrawContext;
srcTexture = dstTexture; srcTexture = srcDrawContext->asTexture();
srcRect.offsetTo(0, 0); srcRect.offsetTo(0, 0);
SkTSwap(dstTexture, tempTexture); dstDrawContext.swap(tmpDrawContext);
localSrcBounds = srcRect; localSrcBounds = srcRect;
srcOffset.set(0, 0); srcOffset.set(0, 0);
} }
if (sigmaY > 0.0f) { if (sigmaY > 0.0f) {
if (scaleFactorY > 1 || sigmaX > 0.0f) { if (scaleFactorY > 1 || sigmaX > 0.0f) {
// TODO: if we pass in the source draw context we don't need this here SkASSERT(srcDrawContext);
if (!srcDrawContext) {
srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRenderTarget()));
if (!srcDrawContext) {
return nullptr;
}
}
// Clear out a radius below the srcRect to prevent the Y // Clear out a radius below the srcRect to prevent the Y
// convolution from reading garbage. // convolution from reading garbage.
@ -349,61 +319,48 @@ GrTexture* GaussianBlur(GrContext* context,
srcDrawContext->clear(&clearRect, 0x0, false); srcDrawContext->clear(&clearRect, 0x0, false);
} }
sk_sp<GrDrawContext> dstDrawContext(
context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props));
if (!dstDrawContext) {
return nullptr;
}
convolve_gaussian(dstDrawContext.get(), clip, srcRect, convolve_gaussian(dstDrawContext.get(), clip, srcRect,
srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
srcBounds, srcOffset); srcBounds, srcOffset);
srcDrawContext.swap(dstDrawContext); srcDrawContext = dstDrawContext;
srcTexture = dstTexture;
srcRect.offsetTo(0, 0); srcRect.offsetTo(0, 0);
SkTSwap(dstTexture, tempTexture); dstDrawContext.swap(tmpDrawContext);
} }
SkASSERT(srcDrawContext);
srcTexture = nullptr; // we don't use this from here on out
srcIRect = srcRect.roundOut(); srcIRect = srcRect.roundOut();
if (scaleFactorX > 1 || scaleFactorY > 1) { if (scaleFactorX > 1 || scaleFactorY > 1) {
SkASSERT(srcDrawContext); // Clear one pixel to the right and below, to accommodate bilinear upsampling.
clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect.width() + 1, 1);
srcDrawContext->clear(&clearRect, 0x0, false);
clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 1, srcIRect.height());
srcDrawContext->clear(&clearRect, 0x0, false);
// Clear one pixel to the right and below, to accommodate bilinear
// upsampling.
clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
srcIRect.width() + 1, 1);
srcDrawContext->clear(&clearRect, 0x0, false);
clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1, srcIRect.height());
srcDrawContext->clear(&clearRect, 0x0, false);
SkMatrix matrix; SkMatrix matrix;
matrix.setIDiv(srcTexture->width(), srcTexture->height()); matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height());
GrPaint paint; GrPaint paint;
paint.setGammaCorrect(gammaCorrect); paint.setGammaCorrect(gammaCorrect);
// FIXME: this should be mitchell, not bilinear. // FIXME: this should be mitchell, not bilinear.
GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
paint.addColorTextureProcessor(srcTexture, matrix, params); sk_sp<GrTexture> tex(srcDrawContext->asTexture());
paint.addColorTextureProcessor(tex.get(), matrix, params);
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
SkRect dstRect(srcRect); SkRect dstRect(srcRect);
scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
sk_sp<GrDrawContext> dstDrawContext(
context->drawContext(sk_ref_sp(dstTexture->asRenderTarget())));
if (!dstDrawContext) {
return nullptr;
}
dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
srcDrawContext.swap(dstDrawContext); srcDrawContext = dstDrawContext;
srcRect = dstRect; srcRect = dstRect;
srcTexture = dstTexture; dstDrawContext.swap(tmpDrawContext);
SkTSwap(dstTexture, tempTexture);
} }
return SkRef(srcTexture); return srcDrawContext;
} }
#endif #endif

View File

@ -9,19 +9,18 @@
#define SkGpuBlurUtils_DEFINED #define SkGpuBlurUtils_DEFINED
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
#include "GrTextureProvider.h" #include "GrDrawContext.h"
class GrTexture;
class GrContext; class GrContext;
#endif class GrTexture;
struct SkRect; struct SkRect;
namespace SkGpuBlurUtils { namespace SkGpuBlurUtils {
#if SK_SUPPORT_GPU
/** /**
* Applies a 2D Gaussian blur to a given texture. * Applies a 2D Gaussian blur to a given texture. The blurred result is returned
* as a drawContext in case the caller wishes to future draw into the result.
* Note: one of sigmaX and sigmaY should be non-zero!
* @param context The GPU context * @param context The GPU context
* @param srcTexture The source texture to be blurred. * @param srcTexture The source texture to be blurred.
* @param gammaCorrect Should blur be gamma-correct (sRGB to linear, etc...) * @param gammaCorrect Should blur be gamma-correct (sRGB to linear, etc...)
@ -30,18 +29,17 @@ namespace SkGpuBlurUtils {
* no pixels will be sampled outside of this rectangle. * no pixels will be sampled outside of this rectangle.
* @param sigmaX The blur's standard deviation in X. * @param sigmaX The blur's standard deviation in X.
* @param sigmaY The blur's standard deviation in Y. * @param sigmaY The blur's standard deviation in Y.
* @return the blurred texture, which may be srcTexture reffed, or a * @return The drawContext containing the blurred result.
* new texture. It is the caller's responsibility to unref this texture.
*/ */
GrTexture* GaussianBlur(GrContext* context, sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
GrTexture* srcTexture, GrTexture* srcTexture,
bool gammaCorrect, bool gammaCorrect,
const SkRect& dstBounds, const SkRect& dstBounds,
const SkRect* srcBounds, const SkRect* srcBounds,
float sigmaX, float sigmaX,
float sigmaY); float sigmaY);
#endif
}; };
#endif #endif
#endif