Swap SkGpuBlurUtils over to using SkIRects
We don't have to land this, but I found it more comforting for the blurring code to explicitly deal with SkIRects rather than SkRects with integer values. Split out of: https://codereview.chromium.org/1959493002/ (Retract GrRenderTarget from SkGpuBlurUtils) GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1968603003 Review-Url: https://codereview.chromium.org/1968603003
This commit is contained in:
parent
862cff30ea
commit
f054b1766b
@ -140,7 +140,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual bool filterMaskGPU(GrTexture* src,
|
virtual bool filterMaskGPU(GrTexture* src,
|
||||||
const SkMatrix& ctm,
|
const SkMatrix& ctm,
|
||||||
const SkRect& maskRect,
|
const SkIRect& maskRect,
|
||||||
GrTexture** result,
|
GrTexture** result,
|
||||||
bool canOverwriteSrc) const;
|
bool canOverwriteSrc) const;
|
||||||
#endif
|
#endif
|
||||||
|
@ -101,6 +101,7 @@ typedef double SkScalar;
|
|||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#define SkIntToScalar(x) static_cast<SkScalar>(x)
|
#define SkIntToScalar(x) static_cast<SkScalar>(x)
|
||||||
|
#define SkIntToFloat(x) static_cast<float>(x)
|
||||||
#define SkScalarTruncToInt(x) static_cast<int>(x)
|
#define SkScalarTruncToInt(x) static_cast<int>(x)
|
||||||
|
|
||||||
#define SkScalarToFloat(x) static_cast<float>(x)
|
#define SkScalarToFloat(x) static_cast<float>(x)
|
||||||
|
@ -336,7 +336,7 @@ bool SkMaskFilter::directFilterRRectMaskGPU(GrTextureProvider* texProvider,
|
|||||||
|
|
||||||
bool SkMaskFilter::filterMaskGPU(GrTexture* src,
|
bool SkMaskFilter::filterMaskGPU(GrTexture* src,
|
||||||
const SkMatrix& ctm,
|
const SkMatrix& ctm,
|
||||||
const SkRect& maskRect,
|
const SkIRect& maskRect,
|
||||||
GrTexture** result,
|
GrTexture** result,
|
||||||
bool canOverwriteSrc) const {
|
bool canOverwriteSrc) const {
|
||||||
return false;
|
return false;
|
||||||
|
@ -120,13 +120,12 @@ sk_sp<SkSpecialImage> SkBlurImageFilter::onFilterImage(SkSpecialImage* source,
|
|||||||
offset->fY = dstBounds.fTop;
|
offset->fY = dstBounds.fTop;
|
||||||
inputBounds.offset(-inputOffset);
|
inputBounds.offset(-inputOffset);
|
||||||
dstBounds.offset(-inputOffset);
|
dstBounds.offset(-inputOffset);
|
||||||
SkRect inputBoundsF(SkRect::Make(inputBounds));
|
|
||||||
sk_sp<GrDrawContext> drawContext(SkGpuBlurUtils::GaussianBlur(
|
sk_sp<GrDrawContext> drawContext(SkGpuBlurUtils::GaussianBlur(
|
||||||
context,
|
context,
|
||||||
inputTexture.get(),
|
inputTexture.get(),
|
||||||
source->props().isGammaCorrect(),
|
source->props().isGammaCorrect(),
|
||||||
SkRect::Make(dstBounds),
|
dstBounds,
|
||||||
&inputBoundsF,
|
&inputBounds,
|
||||||
sigma.x(),
|
sigma.x(),
|
||||||
sigma.y()));
|
sigma.y()));
|
||||||
if (!drawContext) {
|
if (!drawContext) {
|
||||||
|
@ -66,7 +66,7 @@ public:
|
|||||||
const SkRRect& rrect) const override;
|
const SkRRect& rrect) const override;
|
||||||
bool filterMaskGPU(GrTexture* src,
|
bool filterMaskGPU(GrTexture* src,
|
||||||
const SkMatrix& ctm,
|
const SkMatrix& ctm,
|
||||||
const SkRect& maskRect,
|
const SkIRect& maskRect,
|
||||||
GrTexture** result,
|
GrTexture** result,
|
||||||
bool canOverwriteSrc) const override;
|
bool canOverwriteSrc) const override;
|
||||||
#endif
|
#endif
|
||||||
@ -1235,10 +1235,11 @@ bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRRect& devRRect,
|
|||||||
|
|
||||||
bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src,
|
bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src,
|
||||||
const SkMatrix& ctm,
|
const SkMatrix& ctm,
|
||||||
const SkRect& maskRect,
|
const SkIRect& maskRect,
|
||||||
GrTexture** result,
|
GrTexture** result,
|
||||||
bool canOverwriteSrc) const {
|
bool canOverwriteSrc) const {
|
||||||
SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
|
// 'maskRect' isn't snapped to the UL corner but the mask in 'src' is.
|
||||||
|
const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
|
||||||
|
|
||||||
GrContext* context = src->getContext();
|
GrContext* context = src->getContext();
|
||||||
|
|
||||||
@ -1277,7 +1278,7 @@ bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src,
|
|||||||
paint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
|
paint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), clipRect);
|
drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), SkRect::Make(clipRect));
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = drawContext->asTexture().release();
|
*result = drawContext->asTexture().release();
|
||||||
|
@ -15,19 +15,38 @@
|
|||||||
#include "GrContext.h"
|
#include "GrContext.h"
|
||||||
#include "GrCaps.h"
|
#include "GrCaps.h"
|
||||||
#include "GrDrawContext.h"
|
#include "GrDrawContext.h"
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace SkGpuBlurUtils {
|
|
||||||
|
|
||||||
#if SK_SUPPORT_GPU
|
|
||||||
|
|
||||||
#define MAX_BLUR_SIGMA 4.0f
|
#define MAX_BLUR_SIGMA 4.0f
|
||||||
|
|
||||||
static void scale_rect(SkRect* rect, float xScale, float yScale) {
|
static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) {
|
||||||
rect->fLeft = SkScalarMul(rect->fLeft, xScale);
|
rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale));
|
||||||
rect->fTop = SkScalarMul(rect->fTop, yScale);
|
rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale));
|
||||||
rect->fRight = SkScalarMul(rect->fRight, xScale);
|
rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale));
|
||||||
rect->fBottom = SkScalarMul(rect->fBottom, yScale);
|
rect->fBottom = SkScalarCeilToInt(SkScalarMul(rect->fBottom, yScale));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void scale_irect(SkIRect* rect, int xScale, int yScale) {
|
||||||
|
rect->fLeft *= xScale;
|
||||||
|
rect->fTop *= yScale;
|
||||||
|
rect->fRight *= xScale;
|
||||||
|
rect->fBottom *= yScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SK_DEBUG
|
||||||
|
static inline int is_even(int x) { return !(x & 1); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) {
|
||||||
|
if (xAxis) {
|
||||||
|
SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight));
|
||||||
|
rect->fLeft /= 2;
|
||||||
|
rect->fRight /= 2;
|
||||||
|
}
|
||||||
|
if (yAxis) {
|
||||||
|
SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom));
|
||||||
|
rect->fTop /= 2;
|
||||||
|
rect->fBottom /= 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) {
|
static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) {
|
||||||
@ -47,8 +66,8 @@ static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int
|
|||||||
|
|
||||||
static void convolve_gaussian_1d(GrDrawContext* drawContext,
|
static void convolve_gaussian_1d(GrDrawContext* drawContext,
|
||||||
const GrClip& clip,
|
const GrClip& clip,
|
||||||
const SkRect& dstRect,
|
const SkIRect& dstRect,
|
||||||
const SkPoint& srcOffset,
|
const SkIPoint& srcOffset,
|
||||||
GrTexture* texture,
|
GrTexture* texture,
|
||||||
Gr1DKernelEffect::Direction direction,
|
Gr1DKernelEffect::Direction direction,
|
||||||
int radius,
|
int radius,
|
||||||
@ -61,31 +80,29 @@ static void convolve_gaussian_1d(GrDrawContext* drawContext,
|
|||||||
texture, direction, radius, sigma, useBounds, bounds));
|
texture, direction, radius, sigma, useBounds, bounds));
|
||||||
paint.addColorFragmentProcessor(conv);
|
paint.addColorFragmentProcessor(conv);
|
||||||
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
|
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
|
||||||
SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y());
|
SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
|
||||||
drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, localMatrix);
|
-SkIntToScalar(srcOffset.y()));
|
||||||
|
drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(),
|
||||||
|
SkRect::Make(dstRect), localMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void convolve_gaussian_2d(GrDrawContext* drawContext,
|
static void convolve_gaussian_2d(GrDrawContext* drawContext,
|
||||||
const GrClip& clip,
|
const GrClip& clip,
|
||||||
const SkRect& dstRect,
|
const SkIRect& dstRect,
|
||||||
const SkPoint& srcOffset,
|
const SkIPoint& srcOffset,
|
||||||
GrTexture* texture,
|
GrTexture* texture,
|
||||||
int radiusX,
|
int radiusX,
|
||||||
int radiusY,
|
int radiusY,
|
||||||
SkScalar sigmaX,
|
SkScalar sigmaX,
|
||||||
SkScalar sigmaY,
|
SkScalar sigmaY,
|
||||||
const SkRect* srcBounds) {
|
const SkIRect* srcBounds) {
|
||||||
SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y());
|
SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
|
||||||
|
-SkIntToScalar(srcOffset.y()));
|
||||||
SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
|
SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
|
||||||
SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
|
SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
|
||||||
GrPaint paint;
|
GrPaint paint;
|
||||||
paint.setGammaCorrect(drawContext->isGammaCorrect());
|
paint.setGammaCorrect(drawContext->isGammaCorrect());
|
||||||
SkIRect bounds;
|
SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect();
|
||||||
if (srcBounds) {
|
|
||||||
srcBounds->roundOut(&bounds);
|
|
||||||
} else {
|
|
||||||
bounds.setEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
|
SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
|
||||||
texture, bounds, size, 1.0, 0.0, kernelOffset,
|
texture, bounds, size, 1.0, 0.0, kernelOffset,
|
||||||
@ -93,51 +110,49 @@ static void convolve_gaussian_2d(GrDrawContext* drawContext,
|
|||||||
true, sigmaX, sigmaY));
|
true, sigmaX, sigmaY));
|
||||||
paint.addColorFragmentProcessor(conv);
|
paint.addColorFragmentProcessor(conv);
|
||||||
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
|
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
|
||||||
drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, localMatrix);
|
drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(),
|
||||||
|
SkRect::Make(dstRect), localMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void convolve_gaussian(GrDrawContext* drawContext,
|
static void convolve_gaussian(GrDrawContext* drawContext,
|
||||||
const GrClip& clip,
|
const GrClip& clip,
|
||||||
const SkRect& srcRect,
|
const SkIRect& srcRect,
|
||||||
GrTexture* texture,
|
GrTexture* texture,
|
||||||
Gr1DKernelEffect::Direction direction,
|
Gr1DKernelEffect::Direction direction,
|
||||||
int radius,
|
int radius,
|
||||||
float sigma,
|
float sigma,
|
||||||
const SkRect* srcBounds,
|
const SkIRect* srcBounds,
|
||||||
const SkPoint& srcOffset) {
|
const SkIPoint& srcOffset) {
|
||||||
float bounds[2] = { 0.0f, 1.0f };
|
float bounds[2] = { 0.0f, 1.0f };
|
||||||
SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
|
SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height());
|
||||||
if (!srcBounds) {
|
if (!srcBounds) {
|
||||||
convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
|
convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
|
||||||
direction, radius, sigma, false, bounds);
|
direction, radius, sigma, false, bounds);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SkRect midRect = *srcBounds, leftRect, rightRect;
|
SkIRect midRect = *srcBounds, leftRect, rightRect;
|
||||||
midRect.offset(srcOffset);
|
midRect.offset(srcOffset);
|
||||||
SkIRect topRect, bottomRect;
|
SkIRect topRect, bottomRect;
|
||||||
SkScalar rad = SkIntToScalar(radius);
|
|
||||||
if (direction == Gr1DKernelEffect::kX_Direction) {
|
if (direction == Gr1DKernelEffect::kX_Direction) {
|
||||||
bounds[0] = SkScalarToFloat(srcBounds->left()) / texture->width();
|
bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width();
|
||||||
bounds[1] = SkScalarToFloat(srcBounds->right()) / texture->width();
|
bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width();
|
||||||
SkRect::MakeLTRB(0, 0, dstRect.right(), midRect.top()).roundOut(&topRect);
|
topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top());
|
||||||
SkRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom())
|
bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom());
|
||||||
.roundOut(&bottomRect);
|
midRect.inset(radius, 0);
|
||||||
midRect.inset(rad, 0);
|
leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom());
|
||||||
leftRect = SkRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom());
|
|
||||||
rightRect =
|
rightRect =
|
||||||
SkRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom());
|
SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom());
|
||||||
dstRect.fTop = midRect.top();
|
dstRect.fTop = midRect.top();
|
||||||
dstRect.fBottom = midRect.bottom();
|
dstRect.fBottom = midRect.bottom();
|
||||||
} else {
|
} else {
|
||||||
bounds[0] = SkScalarToFloat(srcBounds->top()) / texture->height();
|
bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height();
|
||||||
bounds[1] = SkScalarToFloat(srcBounds->bottom()) / texture->height();
|
bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height();
|
||||||
SkRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom()).roundOut(&topRect);
|
topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom());
|
||||||
SkRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom())
|
bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom());
|
||||||
.roundOut(&bottomRect);;
|
midRect.inset(0, radius);
|
||||||
midRect.inset(0, rad);
|
leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top());
|
||||||
leftRect = SkRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top());
|
|
||||||
rightRect =
|
rightRect =
|
||||||
SkRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height());
|
SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height());
|
||||||
dstRect.fLeft = midRect.left();
|
dstRect.fLeft = midRect.left();
|
||||||
dstRect.fRight = midRect.right();
|
dstRect.fRight = midRect.right();
|
||||||
}
|
}
|
||||||
@ -163,11 +178,13 @@ static void convolve_gaussian(GrDrawContext* drawContext,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace SkGpuBlurUtils {
|
||||||
|
|
||||||
sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
||||||
GrTexture* origSrc,
|
GrTexture* origSrc,
|
||||||
bool gammaCorrect,
|
bool gammaCorrect,
|
||||||
const SkRect& dstBounds,
|
const SkIRect& dstBounds,
|
||||||
const SkRect* srcBounds,
|
const SkIRect* srcBounds,
|
||||||
float sigmaX,
|
float sigmaX,
|
||||||
float sigmaY) {
|
float sigmaY) {
|
||||||
SkASSERT(context);
|
SkASSERT(context);
|
||||||
@ -177,11 +194,12 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
int maxTextureSize = context->caps()->maxTextureSize();
|
int maxTextureSize = context->caps()->maxTextureSize();
|
||||||
sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
|
sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
|
||||||
sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
|
sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
|
||||||
|
SkASSERT(sigmaX || sigmaY);
|
||||||
|
|
||||||
SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y());
|
SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y());
|
||||||
SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height());
|
SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height());
|
||||||
SkRect localSrcBounds;
|
SkIRect localSrcBounds;
|
||||||
SkRect srcRect;
|
SkIRect srcRect;
|
||||||
if (srcBounds) {
|
if (srcBounds) {
|
||||||
srcRect = localSrcBounds = *srcBounds;
|
srcRect = localSrcBounds = *srcBounds;
|
||||||
srcRect.offset(srcOffset);
|
srcRect.offset(srcOffset);
|
||||||
@ -190,10 +208,8 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
srcRect = localDstBounds;
|
srcRect = localDstBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
|
scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
|
||||||
srcRect.roundOut(&srcRect);
|
scale_irect(&srcRect, scaleFactorX, scaleFactorY);
|
||||||
scale_rect(&srcRect, static_cast<float>(scaleFactorX),
|
|
||||||
static_cast<float>(scaleFactorY));
|
|
||||||
|
|
||||||
// setup new clip
|
// setup new clip
|
||||||
GrClip clip(localDstBounds);
|
GrClip clip(localDstBounds);
|
||||||
@ -206,8 +222,8 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
kSBGRA_8888_GrPixelConfig == srcTexture->config() ||
|
kSBGRA_8888_GrPixelConfig == srcTexture->config() ||
|
||||||
kAlpha_8_GrPixelConfig == srcTexture->config());
|
kAlpha_8_GrPixelConfig == srcTexture->config());
|
||||||
|
|
||||||
const int width = SkScalarFloorToInt(dstBounds.width());
|
const int width = dstBounds.width();
|
||||||
const int height = SkScalarFloorToInt(dstBounds.height());
|
const int height = dstBounds.height();
|
||||||
const GrPixelConfig config = srcTexture->config();
|
const GrPixelConfig config = srcTexture->config();
|
||||||
|
|
||||||
const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0,
|
const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0,
|
||||||
@ -244,15 +260,17 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
|
|
||||||
sk_sp<GrDrawContext> srcDrawContext;
|
sk_sp<GrDrawContext> srcDrawContext;
|
||||||
|
|
||||||
|
SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY));
|
||||||
|
|
||||||
for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
|
for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
|
||||||
GrPaint paint;
|
GrPaint paint;
|
||||||
paint.setGammaCorrect(gammaCorrect);
|
paint.setGammaCorrect(gammaCorrect);
|
||||||
SkMatrix matrix;
|
SkMatrix matrix;
|
||||||
matrix.setIDiv(srcTexture->width(), srcTexture->height());
|
matrix.setIDiv(srcTexture->width(), srcTexture->height());
|
||||||
SkRect dstRect(srcRect);
|
SkIRect dstRect(srcRect);
|
||||||
if (srcBounds && i == 1) {
|
if (srcBounds && i == 1) {
|
||||||
SkRect domain;
|
SkRect domain;
|
||||||
matrix.mapRect(&domain, *srcBounds);
|
matrix.mapRect(&domain, SkRect::Make(*srcBounds));
|
||||||
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(
|
||||||
@ -269,10 +287,10 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
paint.addColorTextureProcessor(srcTexture.get(), 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,
|
shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY);
|
||||||
i < scaleFactorY ? 0.5f : 1.0f);
|
|
||||||
|
|
||||||
dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
|
dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(),
|
||||||
|
SkRect::Make(dstRect), SkRect::Make(srcRect));
|
||||||
|
|
||||||
srcDrawContext = dstDrawContext;
|
srcDrawContext = dstDrawContext;
|
||||||
srcRect = dstRect;
|
srcRect = dstRect;
|
||||||
@ -282,18 +300,15 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
srcRect = localDstBounds;
|
srcRect = localDstBounds;
|
||||||
|
scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
|
||||||
scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
|
|
||||||
srcRect.roundOut(&srcRect);
|
|
||||||
SkIRect srcIRect = srcRect.roundOut();
|
|
||||||
if (sigmaX > 0.0f) {
|
if (sigmaX > 0.0f) {
|
||||||
if (scaleFactorX > 1) {
|
if (scaleFactorX > 1) {
|
||||||
SkASSERT(srcDrawContext);
|
SkASSERT(srcDrawContext);
|
||||||
|
|
||||||
// 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.
|
||||||
clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
|
clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop,
|
||||||
radiusX, srcIRect.height());
|
radiusX, srcRect.height());
|
||||||
srcDrawContext->clear(&clearRect, 0x0, false);
|
srcDrawContext->clear(&clearRect, 0x0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,8 +329,8 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
|
|
||||||
// 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.
|
||||||
clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
|
clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom,
|
||||||
srcIRect.width(), radiusY);
|
srcRect.width(), radiusY);
|
||||||
srcDrawContext->clear(&clearRect, 0x0, false);
|
srcDrawContext->clear(&clearRect, 0x0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,13 +345,12 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
|
|
||||||
SkASSERT(srcDrawContext);
|
SkASSERT(srcDrawContext);
|
||||||
srcTexture = nullptr; // we don't use this from here on out
|
srcTexture = nullptr; // we don't use this from here on out
|
||||||
srcIRect = srcRect.roundOut();
|
|
||||||
|
|
||||||
if (scaleFactorX > 1 || scaleFactorY > 1) {
|
if (scaleFactorX > 1 || scaleFactorY > 1) {
|
||||||
// Clear one pixel to the right and below, to accommodate bilinear upsampling.
|
// Clear one pixel to the right and below, to accommodate bilinear upsampling.
|
||||||
clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect.width() + 1, 1);
|
clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
|
||||||
srcDrawContext->clear(&clearRect, 0x0, false);
|
srcDrawContext->clear(&clearRect, 0x0, false);
|
||||||
clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 1, srcIRect.height());
|
clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height());
|
||||||
srcDrawContext->clear(&clearRect, 0x0, false);
|
srcDrawContext->clear(&clearRect, 0x0, false);
|
||||||
|
|
||||||
SkMatrix matrix;
|
SkMatrix matrix;
|
||||||
@ -350,10 +364,11 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
paint.addColorTextureProcessor(tex.get(), matrix, params);
|
paint.addColorTextureProcessor(tex.get(), matrix, params);
|
||||||
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
|
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
|
||||||
|
|
||||||
SkRect dstRect(srcRect);
|
SkIRect dstRect(srcRect);
|
||||||
scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
|
scale_irect(&dstRect, scaleFactorX, scaleFactorY);
|
||||||
|
|
||||||
dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
|
dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(),
|
||||||
|
SkRect::Make(dstRect), SkRect::Make(srcRect));
|
||||||
|
|
||||||
srcDrawContext = dstDrawContext;
|
srcDrawContext = dstDrawContext;
|
||||||
srcRect = dstRect;
|
srcRect = dstRect;
|
||||||
@ -362,6 +377,8 @@ sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
|||||||
|
|
||||||
return srcDrawContext;
|
return srcDrawContext;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
@ -34,11 +34,10 @@ namespace SkGpuBlurUtils {
|
|||||||
sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
|
||||||
GrTexture* srcTexture,
|
GrTexture* srcTexture,
|
||||||
bool gammaCorrect,
|
bool gammaCorrect,
|
||||||
const SkRect& dstBounds,
|
const SkIRect& dstBounds,
|
||||||
const SkRect* srcBounds,
|
const SkIRect* srcBounds,
|
||||||
float sigmaX,
|
float sigmaX,
|
||||||
float sigmaY);
|
float sigmaY);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -28,11 +28,11 @@ static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& r
|
|||||||
static bool draw_mask(GrDrawContext* drawContext,
|
static bool draw_mask(GrDrawContext* drawContext,
|
||||||
const GrClip& clip,
|
const GrClip& clip,
|
||||||
const SkMatrix& viewMatrix,
|
const SkMatrix& viewMatrix,
|
||||||
const SkRect& maskRect,
|
const SkIRect& maskRect,
|
||||||
GrPaint* grp,
|
GrPaint* grp,
|
||||||
GrTexture* mask) {
|
GrTexture* mask) {
|
||||||
SkMatrix matrix;
|
SkMatrix matrix;
|
||||||
matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
|
matrix.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
|
||||||
matrix.postIDiv(mask->width(), mask->height());
|
matrix.postIDiv(mask->width(), mask->height());
|
||||||
|
|
||||||
grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(mask, matrix,
|
grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(mask, matrix,
|
||||||
@ -42,7 +42,8 @@ static bool draw_mask(GrDrawContext* drawContext,
|
|||||||
if (!viewMatrix.invert(&inverse)) {
|
if (!viewMatrix.invert(&inverse)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), maskRect, inverse);
|
drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(),
|
||||||
|
SkRect::Make(maskRect), inverse);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,23 +87,16 @@ static bool sw_draw_with_mask_filter(GrDrawContext* drawContext,
|
|||||||
texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
|
texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
|
||||||
dstM.fImage, dstM.fRowBytes);
|
dstM.fImage, dstM.fRowBytes);
|
||||||
|
|
||||||
SkRect maskRect = SkRect::Make(dstM.fBounds);
|
return draw_mask(drawContext, clipData, viewMatrix, dstM.fBounds, grp, texture);
|
||||||
|
|
||||||
return draw_mask(drawContext, clipData, viewMatrix, maskRect, grp, texture);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a mask of 'devPath' and place the result in 'mask'.
|
// Create a mask of 'devPath' and place the result in 'mask'.
|
||||||
static sk_sp<GrTexture> create_mask_GPU(GrContext* context,
|
static sk_sp<GrTexture> create_mask_GPU(GrContext* context,
|
||||||
SkRect* maskRect,
|
const SkIRect& maskRect,
|
||||||
const SkPath& devPath,
|
const SkPath& devPath,
|
||||||
SkStrokeRec::InitStyle fillOrHairline,
|
SkStrokeRec::InitStyle fillOrHairline,
|
||||||
bool doAA,
|
bool doAA,
|
||||||
int sampleCnt) {
|
int sampleCnt) {
|
||||||
// This mask will ultimately be drawn as a non-AA rect (see draw_mask).
|
|
||||||
// Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
|
|
||||||
// so the mask draws in a reproducible manner.
|
|
||||||
*maskRect = SkRect::Make(maskRect->roundOut());
|
|
||||||
|
|
||||||
if (!doAA) {
|
if (!doAA) {
|
||||||
// Don't need MSAA if mask isn't AA
|
// Don't need MSAA if mask isn't AA
|
||||||
sampleCnt = 0;
|
sampleCnt = 0;
|
||||||
@ -116,8 +110,8 @@ static sk_sp<GrTexture> create_mask_GPU(GrContext* context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
sk_sp<GrDrawContext> drawContext(context->newDrawContext(SkBackingFit::kApprox,
|
sk_sp<GrDrawContext> drawContext(context->newDrawContext(SkBackingFit::kApprox,
|
||||||
SkScalarCeilToInt(maskRect->width()),
|
maskRect.width(),
|
||||||
SkScalarCeilToInt(maskRect->height()),
|
maskRect.height(),
|
||||||
config,
|
config,
|
||||||
sampleCnt));
|
sampleCnt));
|
||||||
if (!drawContext) {
|
if (!drawContext) {
|
||||||
@ -131,13 +125,13 @@ static sk_sp<GrTexture> create_mask_GPU(GrContext* context,
|
|||||||
tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
|
tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
|
||||||
|
|
||||||
// setup new clip
|
// setup new clip
|
||||||
const SkRect clipRect = SkRect::MakeWH(maskRect->width(), maskRect->height());
|
const SkRect clipRect = SkRect::MakeIWH(maskRect.width(), maskRect.height());
|
||||||
GrClip clip(clipRect);
|
GrClip clip(clipRect);
|
||||||
|
|
||||||
// Draw the mask into maskTexture with the path's integerized top-left at
|
// Draw the mask into maskTexture with the path's integerized top-left at
|
||||||
// the origin using tempPaint.
|
// the origin using tempPaint.
|
||||||
SkMatrix translate;
|
SkMatrix translate;
|
||||||
translate.setTranslate(-maskRect->fLeft, -maskRect->fTop);
|
translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
|
||||||
drawContext->drawPath(clip, tempPaint, translate, devPath, GrStyle(fillOrHairline));
|
drawContext->drawPath(clip, tempPaint, translate, devPath, GrStyle(fillOrHairline));
|
||||||
return drawContext->asTexture();;
|
return drawContext->asTexture();;
|
||||||
}
|
}
|
||||||
@ -195,6 +189,9 @@ static void draw_path_with_mask_filter(GrContext* context,
|
|||||||
clipBounds,
|
clipBounds,
|
||||||
viewMatrix,
|
viewMatrix,
|
||||||
&maskRect)) {
|
&maskRect)) {
|
||||||
|
// This mask will ultimately be drawn as a non-AA rect (see draw_mask).
|
||||||
|
// Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
|
||||||
|
// so the mask draws in a reproducible manner.
|
||||||
SkIRect finalIRect;
|
SkIRect finalIRect;
|
||||||
maskRect.roundOut(&finalIRect);
|
maskRect.roundOut(&finalIRect);
|
||||||
if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
|
if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
|
||||||
@ -215,7 +212,7 @@ static void draw_path_with_mask_filter(GrContext* context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
sk_sp<GrTexture> mask(create_mask_GPU(context,
|
sk_sp<GrTexture> mask(create_mask_GPU(context,
|
||||||
&maskRect,
|
finalIRect,
|
||||||
*path,
|
*path,
|
||||||
fillOrHairline,
|
fillOrHairline,
|
||||||
paint->isAntiAlias(),
|
paint->isAntiAlias(),
|
||||||
@ -223,10 +220,10 @@ static void draw_path_with_mask_filter(GrContext* context,
|
|||||||
if (mask) {
|
if (mask) {
|
||||||
GrTexture* filtered;
|
GrTexture* filtered;
|
||||||
|
|
||||||
if (maskFilter->filterMaskGPU(mask.get(), viewMatrix, maskRect, &filtered, true)) {
|
if (maskFilter->filterMaskGPU(mask.get(), viewMatrix, finalIRect, &filtered, true)) {
|
||||||
// filterMaskGPU gives us ownership of a ref to the result
|
// filterMaskGPU gives us ownership of a ref to the result
|
||||||
SkAutoTUnref<GrTexture> atu(filtered);
|
SkAutoTUnref<GrTexture> atu(filtered);
|
||||||
if (draw_mask(drawContext, clip, viewMatrix, maskRect, paint, filtered)) {
|
if (draw_mask(drawContext, clip, viewMatrix, finalIRect, paint, filtered)) {
|
||||||
// This path is completely drawn
|
// This path is completely drawn
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user