From: mtklein Date: Mon, 22 Aug 2016 16:25:59 +0000 (-0700) Subject: Revert of Moving SkBlurImageFilter into core (patchset #6 id:100001 of https://codere... X-Git-Tag: submit/tizen/20180928.044319~116^2~77 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2ec417257b5866b653af1af6ef5e91388cc40f49;p=platform%2Fupstream%2FlibSkiaSharp.git Revert of Moving SkBlurImageFilter into core (patchset #6 id:100001 of https://codereview.chromium.org/2255803003/ ) Reason for revert: It looks like this breaks our roll into Google3: https://test.corp.google.com/ui#id=OCL:130943857:BASE:130944046:1471881622765:61dbdd3 Original issue's description: > Moving SkBlurImageFilter into core > > BUG=skia: > GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2255803003 > > Committed: https://skia.googlesource.com/skia/+/dd3259eb95c3b47e11eefa3b176365a112a32b48 TBR=reed@google.com,fmalita@chromium.org,vjiaoblack@google.com # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=skia: Review-Url: https://codereview.chromium.org/2263283002 --- diff --git a/gyp/core.gypi b/gyp/core.gypi index 0a30618da3..8d1dc8d177 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -60,7 +60,6 @@ '<(skia_src_path)/core/SkBlitter_PM4f.cpp', '<(skia_src_path)/core/SkBlitter_RGB16.cpp', '<(skia_src_path)/core/SkBlitter_Sprite.cpp', - '<(skia_src_path)/core/SkBlurImageFilter.cpp', '<(skia_src_path)/core/SkBuffer.cpp', '<(skia_src_path)/core/SkCachedData.cpp', '<(skia_src_path)/core/SkCanvas.cpp', @@ -140,8 +139,6 @@ '<(skia_src_path)/core/SkGlyphCache.cpp', '<(skia_src_path)/core/SkGlyphCache.h', '<(skia_src_path)/core/SkGlyphCache_Globals.h', - '<(skia_src_path)/core/SkGpuBlurUtils.h', - '<(skia_src_path)/core/SkGpuBlurUtils.cpp', '<(skia_src_path)/core/SkGraphics.cpp', '<(skia_src_path)/core/SkHalf.cpp', '<(skia_src_path)/core/SkHalf.h', diff --git a/gyp/effects.gypi b/gyp/effects.gypi index 62c6bfdec4..45eced88e2 100644 --- a/gyp/effects.gypi +++ b/gyp/effects.gypi @@ -26,6 +26,7 @@ '<(skia_src_path)/effects/SkBlurDrawLooper.cpp', '<(skia_src_path)/effects/SkBlurMask.cpp', '<(skia_src_path)/effects/SkBlurMask.h', + '<(skia_src_path)/effects/SkBlurImageFilter.cpp', '<(skia_src_path)/effects/SkBlurMaskFilter.cpp', '<(skia_src_path)/effects/SkColorCubeFilter.cpp', '<(skia_src_path)/effects/SkColorFilterImageFilter.cpp', @@ -44,6 +45,8 @@ '<(skia_src_path)/effects/SkImageSource.cpp', '<(skia_src_path)/effects/SkGammaColorFilter.cpp', '<(skia_src_path)/effects/SkGaussianEdgeShader.cpp', + '<(skia_src_path)/effects/SkGpuBlurUtils.h', + '<(skia_src_path)/effects/SkGpuBlurUtils.cpp', '<(skia_src_path)/effects/SkLayerDrawLooper.cpp', '<(skia_src_path)/effects/SkLayerRasterizer.cpp', '<(skia_src_path)/effects/SkLightingImageFilter.cpp', diff --git a/include/core/SkImageFilter.h b/include/core/SkImageFilter.h index 97f84dc596..6210073bf4 100644 --- a/include/core/SkImageFilter.h +++ b/include/core/SkImageFilter.h @@ -152,10 +152,6 @@ public: return this->isColorFilterNode(filterPtr); } - static sk_sp MakeBlur(SkScalar sigmaX, SkScalar sigmaY, - sk_sp input, - const CropRect* cropRect = nullptr); - /** * Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely * replaced by the returned colorfilter. i.e. the two effects will affect drawing in the @@ -233,7 +229,6 @@ public: SK_TO_STRING_PUREVIRT() SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter) - SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() protected: class Common { diff --git a/include/effects/SkBlurImageFilter.h b/include/effects/SkBlurImageFilter.h index 085a13b5b3..9bfa19b875 100644 --- a/include/effects/SkBlurImageFilter.h +++ b/include/effects/SkBlurImageFilter.h @@ -9,14 +9,40 @@ #define SkBlurImageFilter_DEFINED #include "SkImageFilter.h" +#include "SkSize.h" -class SK_API SkBlurImageFilter { +class SK_API SkBlurImageFilter : public SkImageFilter { public: static sk_sp Make(SkScalar sigmaX, SkScalar sigmaY, sk_sp input, - const SkImageFilter::CropRect* cropRect = nullptr) { - return SkImageFilter::MakeBlur(sigmaX, sigmaY, input, cropRect); + const CropRect* cropRect = nullptr); + + SkRect computeFastBounds(const SkRect&) const override; + + SK_TO_STRING_OVERRIDE() + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurImageFilter) + +#ifdef SK_SUPPORT_LEGACY_IMAGEFILTER_PTR + static SkImageFilter* Create(SkScalar sigmaX, SkScalar sigmaY, SkImageFilter* input = nullptr, + const CropRect* cropRect = nullptr) { + return Make(sigmaX, sigmaY, sk_ref_sp(input), cropRect).release(); } +#endif + +protected: + void flatten(SkWriteBuffer&) const override; + sk_sp onFilterImage(SkSpecialImage* source, const Context&, + SkIPoint* offset) const override; + SkIRect onFilterNodeBounds(const SkIRect& src, const SkMatrix&, MapDirection) const override; + +private: + SkBlurImageFilter(SkScalar sigmaX, + SkScalar sigmaY, + sk_sp input, + const CropRect* cropRect); + + SkSize fSigma; + typedef SkImageFilter INHERITED; }; #endif diff --git a/src/core/SkBlurImageFilter.cpp b/src/core/SkBlurImageFilter.cpp deleted file mode 100644 index b190554209..0000000000 --- a/src/core/SkBlurImageFilter.cpp +++ /dev/null @@ -1,295 +0,0 @@ -/* - * Copyright 2011 The Android Open Source Project - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "SkAutoPixmapStorage.h" -#include "SkColorPriv.h" -#include "SkGpuBlurUtils.h" -#include "SkOpts.h" -#include "SkReadBuffer.h" -#include "SkSpecialImage.h" -#include "SkWriteBuffer.h" - -#if SK_SUPPORT_GPU -#include "GrContext.h" -#include "SkGr.h" -#endif - -class SkBlurImageFilterImpl : public SkImageFilter { -public: - SkBlurImageFilterImpl(SkScalar sigmaX, - SkScalar sigmaY, - sk_sp input, - const CropRect* cropRect); - - SkRect computeFastBounds(const SkRect&) const override; - - SK_TO_STRING_OVERRIDE() - SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurImageFilterImpl) - -#ifdef SK_SUPPORT_LEGACY_IMAGEFILTER_PTR - static SkImageFilter* Create(SkScalar sigmaX, SkScalar sigmaY, SkImageFilter* input = nullptr, - const CropRect* cropRect = nullptr) { - return Make(sigmaX, sigmaY, sk_ref_sp(input), cropRect).release(); - } -#endif - -protected: - void flatten(SkWriteBuffer&) const override; - sk_sp onFilterImage(SkSpecialImage* source, const Context&, - SkIPoint* offset) const override; - SkIRect onFilterNodeBounds(const SkIRect& src, const SkMatrix&, MapDirection) const override; - -private: - SkSize fSigma; - typedef SkImageFilter INHERITED; - - friend class SkImageFilter; -}; - -SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkImageFilter) - SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurImageFilterImpl) -SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END - -/////////////////////////////////////////////////////////////////////////////// - -sk_sp SkImageFilter::MakeBlur(SkScalar sigmaX, SkScalar sigmaY, - sk_sp input, - const CropRect* cropRect) { - if (0 == sigmaX && 0 == sigmaY && !cropRect) { - return input; - } - return sk_sp(new SkBlurImageFilterImpl(sigmaX, sigmaY, input, cropRect)); -} - -// This rather arbitrary-looking value results in a maximum box blur kernel size -// of 1000 pixels on the raster path, which matches the WebKit and Firefox -// implementations. Since the GPU path does not compute a box blur, putting -// the limit on sigma ensures consistent behaviour between the GPU and -// raster paths. -#define MAX_SIGMA SkIntToScalar(532) - -static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) { - SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height()); - ctm.mapVectors(&sigma, 1); - sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA); - sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA); - return sigma; -} - -SkBlurImageFilterImpl::SkBlurImageFilterImpl(SkScalar sigmaX, - SkScalar sigmaY, - sk_sp input, - const CropRect* cropRect) - : INHERITED(&input, 1, cropRect) - , fSigma(SkSize::Make(sigmaX, sigmaY)) { -} - -sk_sp SkBlurImageFilterImpl::CreateProc(SkReadBuffer& buffer) { - SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); - SkScalar sigmaX = buffer.readScalar(); - SkScalar sigmaY = buffer.readScalar(); - return SkImageFilter::MakeBlur(sigmaX, sigmaY, common.getInput(0), &common.cropRect()); -} - -void SkBlurImageFilterImpl::flatten(SkWriteBuffer& buffer) const { - this->INHERITED::flatten(buffer); - buffer.writeScalar(fSigma.fWidth); - buffer.writeScalar(fSigma.fHeight); -} - -static void get_box3_params(SkScalar s, int *kernelSize, int* kernelSize3, int *lowOffset, - int *highOffset) { - float pi = SkScalarToFloat(SK_ScalarPI); - int d = static_cast(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi) / 4.0f + 0.5f)); - *kernelSize = d; - if (d % 2 == 1) { - *lowOffset = *highOffset = (d - 1) / 2; - *kernelSize3 = d; - } else { - *highOffset = d / 2; - *lowOffset = *highOffset - 1; - *kernelSize3 = d + 1; - } -} - -sk_sp SkBlurImageFilterImpl::onFilterImage(SkSpecialImage* source, - const Context& ctx, - SkIPoint* offset) const { - SkIPoint inputOffset = SkIPoint::Make(0, 0); - - sk_sp input(this->filterInput(0, source, ctx, &inputOffset)); - if (!input) { - return nullptr; - } - - SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY, - input->width(), input->height()); - - SkIRect dstBounds; - if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) { - return nullptr; - } - if (!inputBounds.intersect(dstBounds)) { - return nullptr; - } - - const SkVector sigma = map_sigma(fSigma, ctx.ctm()); - -#if SK_SUPPORT_GPU - if (source->isTextureBacked()) { - GrContext* context = source->getContext(); - sk_sp inputTexture(input->asTextureRef(context)); - SkASSERT(inputTexture); - - if (0 == sigma.x() && 0 == sigma.y()) { - offset->fX = inputBounds.x(); - offset->fY = inputBounds.y(); - return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), - -inputOffset.y())); - } - - offset->fX = dstBounds.fLeft; - offset->fY = dstBounds.fTop; - inputBounds.offset(-inputOffset); - dstBounds.offset(-inputOffset); - sk_sp drawContext(SkGpuBlurUtils::GaussianBlur( - context, - inputTexture.get(), - sk_ref_sp(source->getColorSpace()), - dstBounds, - &inputBounds, - sigma.x(), - sigma.y())); - if (!drawContext) { - return nullptr; - } - - // TODO: Get the colorSpace from the drawContext (once it has one) - return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(dstBounds.width(), dstBounds.height()), - kNeedNewImageUniqueID_SpecialImage, - drawContext->asTexture(), - sk_ref_sp(input->getColorSpace()), &source->props()); - } -#endif - - int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; - int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; - get_box3_params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffsetX); - get_box3_params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffsetY); - - if (kernelSizeX < 0 || kernelSizeY < 0) { - return nullptr; - } - - if (kernelSizeX == 0 && kernelSizeY == 0) { - offset->fX = inputBounds.x(); - offset->fY = inputBounds.y(); - return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), - -inputOffset.y())); - } - - SkBitmap inputBM; - - if (!input->getROPixels(&inputBM)) { - return nullptr; - } - - if (inputBM.colorType() != kN32_SkColorType) { - return nullptr; - } - - SkImageInfo info = SkImageInfo::Make(dstBounds.width(), dstBounds.height(), - inputBM.colorType(), inputBM.alphaType()); - - SkBitmap tmp, dst; - if (!tmp.tryAllocPixels(info) || !dst.tryAllocPixels(info)) { - return nullptr; - } - - SkAutoLockPixels inputLock(inputBM), tmpLock(tmp), dstLock(dst); - - offset->fX = dstBounds.fLeft; - offset->fY = dstBounds.fTop; - SkPMColor* t = tmp.getAddr32(0, 0); - SkPMColor* d = dst.getAddr32(0, 0); - int w = dstBounds.width(), h = dstBounds.height(); - const SkPMColor* s = inputBM.getAddr32(inputBounds.x() - inputOffset.x(), - inputBounds.y() - inputOffset.y()); - inputBounds.offset(-dstBounds.x(), -dstBounds.y()); - dstBounds.offset(-dstBounds.x(), -dstBounds.y()); - SkIRect inputBoundsT = SkIRect::MakeLTRB(inputBounds.top(), inputBounds.left(), - inputBounds.bottom(), inputBounds.right()); - SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); - int sw = int(inputBM.rowBytes() >> 2); - - /** - * - * In order to make memory accesses cache-friendly, we reorder the passes to - * use contiguous memory reads wherever possible. - * - * For example, the 6 passes of the X-and-Y blur case are rewritten as - * follows. Instead of 3 passes in X and 3 passes in Y, we perform - * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, - * then 1 pass in X transposed to Y on write. - * - * +----+ +----+ +----+ +---+ +---+ +---+ +----+ - * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | -----> | AB | - * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blurXY +----+ - * +---+ +---+ +---+ - * - * In this way, two of the y-blurs become x-blurs applied to transposed - * images, and all memory reads are contiguous. - */ - if (kernelSizeX > 0 && kernelSizeY > 0) { - SkOpts::box_blur_xx(s, sw, inputBounds, t, kernelSizeX, lowOffsetX, highOffsetX, w, h); - SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX, lowOffsetX, w, h); - SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX, highOffsetX, w, h); - SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); - SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); - SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); - } else if (kernelSizeX > 0) { - SkOpts::box_blur_xx(s, sw, inputBounds, d, kernelSizeX, lowOffsetX, highOffsetX, w, h); - SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX, lowOffsetX, w, h); - SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX, highOffsetX, w, h); - } else if (kernelSizeY > 0) { - SkOpts::box_blur_yx(s, sw, inputBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); - SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); - SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); - } - - return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(), - dstBounds.height()), - dst, &source->props()); -} - - -SkRect SkBlurImageFilterImpl::computeFastBounds(const SkRect& src) const { - SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; - bounds.outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)), - SkScalarMul(fSigma.height(), SkIntToScalar(3))); - return bounds; -} - -SkIRect SkBlurImageFilterImpl::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, - MapDirection) const { - SkVector sigma = map_sigma(fSigma, ctm); - return src.makeOutset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar(3))), - SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar(3)))); -} - -#ifndef SK_IGNORE_TO_STRING -void SkBlurImageFilterImpl::toString(SkString* str) const { - str->appendf("SkBlurImageFilterImpl: ("); - str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight); - - if (this->getInput(0)) { - this->getInput(0)->toString(str); - } - - str->append("))"); -} -#endif diff --git a/src/core/SkGpuBlurUtils.cpp b/src/core/SkGpuBlurUtils.cpp deleted file mode 100644 index ca762e88e3..0000000000 --- a/src/core/SkGpuBlurUtils.cpp +++ /dev/null @@ -1,382 +0,0 @@ -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "SkGpuBlurUtils.h" - -#include "SkRect.h" - -#if SK_SUPPORT_GPU -#include "effects/GrConvolutionEffect.h" -#include "effects/GrMatrixConvolutionEffect.h" -#include "GrContext.h" -#include "GrCaps.h" -#include "GrDrawContext.h" -#include "GrFixedClip.h" - -#define MAX_BLUR_SIGMA 4.0f - -static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) { - rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale)); - rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale)); - rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale)); - 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) { - *scaleFactor = 1; - while (sigma > MAX_BLUR_SIGMA) { - *scaleFactor *= 2; - sigma *= 0.5f; - if (*scaleFactor > maxTextureSize) { - *scaleFactor = maxTextureSize; - sigma = MAX_BLUR_SIGMA; - } - } - *radius = static_cast(ceilf(sigma * 3.0f)); - SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius); - return sigma; -} - -static void convolve_gaussian_1d(GrDrawContext* drawContext, - const GrClip& clip, - const SkIRect& dstRect, - const SkIPoint& srcOffset, - GrTexture* texture, - Gr1DKernelEffect::Direction direction, - int radius, - float sigma, - bool useBounds, - float bounds[2]) { - GrPaint paint; - paint.setGammaCorrect(drawContext->isGammaCorrect()); - sk_sp conv(GrConvolutionEffect::MakeGaussian( - texture, direction, radius, sigma, useBounds, bounds)); - paint.addColorFragmentProcessor(std::move(conv)); - paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); - SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), - -SkIntToScalar(srcOffset.y())); - drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), - SkRect::Make(dstRect), localMatrix); -} - -static void convolve_gaussian_2d(GrDrawContext* drawContext, - const GrClip& clip, - const SkIRect& dstRect, - const SkIPoint& srcOffset, - GrTexture* texture, - int radiusX, - int radiusY, - SkScalar sigmaX, - SkScalar sigmaY, - const SkIRect* srcBounds) { - SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), - -SkIntToScalar(srcOffset.y())); - SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); - SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); - GrPaint paint; - paint.setGammaCorrect(drawContext->isGammaCorrect()); - SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect(); - - sk_sp conv(GrMatrixConvolutionEffect::MakeGaussian( - texture, bounds, size, 1.0, 0.0, kernelOffset, - srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode, - true, sigmaX, sigmaY)); - paint.addColorFragmentProcessor(std::move(conv)); - paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); - drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), - SkRect::Make(dstRect), localMatrix); -} - -static void convolve_gaussian(GrDrawContext* drawContext, - const GrClip& clip, - const SkIRect& srcRect, - GrTexture* texture, - Gr1DKernelEffect::Direction direction, - int radius, - float sigma, - const SkIRect* srcBounds, - const SkIPoint& srcOffset) { - float bounds[2] = { 0.0f, 1.0f }; - SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height()); - if (!srcBounds) { - convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture, - direction, radius, sigma, false, bounds); - return; - } - SkIRect midRect = *srcBounds, leftRect, rightRect; - midRect.offset(srcOffset); - SkIRect topRect, bottomRect; - if (direction == Gr1DKernelEffect::kX_Direction) { - bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width(); - bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width(); - topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top()); - bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom()); - midRect.inset(radius, 0); - leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom()); - rightRect = - SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom()); - dstRect.fTop = midRect.top(); - dstRect.fBottom = midRect.bottom(); - } else { - bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height(); - bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height(); - topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom()); - bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom()); - midRect.inset(0, radius); - leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top()); - rightRect = - SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height()); - dstRect.fLeft = midRect.left(); - dstRect.fRight = midRect.right(); - } - if (!topRect.isEmpty()) { - drawContext->clear(&topRect, 0, false); - } - - if (!bottomRect.isEmpty()) { - drawContext->clear(&bottomRect, 0, false); - } - if (midRect.isEmpty()) { - // Blur radius covers srcBounds; use bounds over entire draw - convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture, - direction, radius, sigma, true, bounds); - } else { - // Draw right and left margins with bounds; middle without. - convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture, - direction, radius, sigma, true, bounds); - convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, - direction, radius, sigma, true, bounds); - convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, - direction, radius, sigma, false, bounds); - } -} - -namespace SkGpuBlurUtils { - -sk_sp GaussianBlur(GrContext* context, - GrTexture* origSrc, - sk_sp colorSpace, - const SkIRect& dstBounds, - const SkIRect* srcBounds, - float sigmaX, - float sigmaY, - SkBackingFit fit) { - SkASSERT(context); - SkIRect clearRect; - int scaleFactorX, radiusX; - int scaleFactorY, radiusY; - int maxTextureSize = context->caps()->maxTextureSize(); - sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); - sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); - SkASSERT(sigmaX || sigmaY); - - SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y()); - SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height()); - SkIRect localSrcBounds; - SkIRect srcRect; - if (srcBounds) { - srcRect = localSrcBounds = *srcBounds; - srcRect.offset(srcOffset); - srcBounds = &localSrcBounds; - } else { - srcRect = localDstBounds; - } - - scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); - scale_irect(&srcRect, scaleFactorX, scaleFactorY); - - // setup new clip - GrFixedClip clip(localDstBounds); - - sk_sp srcTexture(sk_ref_sp(origSrc)); - - SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || - kRGBA_8888_GrPixelConfig == srcTexture->config() || - kSRGBA_8888_GrPixelConfig == srcTexture->config() || - kSBGRA_8888_GrPixelConfig == srcTexture->config() || - kAlpha_8_GrPixelConfig == srcTexture->config()); - - const int width = dstBounds.width(); - const int height = dstBounds.height(); - const GrPixelConfig config = srcTexture->config(); - - sk_sp dstDrawContext(context->makeDrawContext(fit, - width, height, config, colorSpace, - 0, kDefault_GrSurfaceOrigin)); - if (!dstDrawContext) { - return nullptr; - } - - // 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 - if (sigmaX > 0.0f && sigmaY > 0.0f && - (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { - // We shouldn't be scaling because this is a small size blur - SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); - - convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset, - srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds); - - return dstDrawContext; - } - - sk_sp tmpDrawContext(context->makeDrawContext(fit, - width, height, config, colorSpace, - 0, kDefault_GrSurfaceOrigin)); - if (!tmpDrawContext) { - return nullptr; - } - - sk_sp srcDrawContext; - - SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); - - for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { - GrPaint paint; - paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); - SkMatrix matrix; - matrix.setIDiv(srcTexture->width(), srcTexture->height()); - SkIRect dstRect(srcRect); - if (srcBounds && i == 1) { - SkRect domain; - matrix.mapRect(&domain, SkRect::Make(*srcBounds)); - domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, - (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); - sk_sp fp(GrTextureDomainEffect::Make( - srcTexture.get(), - nullptr, - matrix, - domain, - GrTextureDomain::kDecal_Mode, - GrTextureParams::kBilerp_FilterMode)); - paint.addColorFragmentProcessor(std::move(fp)); - srcRect.offset(-srcOffset); - srcOffset.set(0, 0); - } else { - GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); - paint.addColorTextureProcessor(srcTexture.get(), nullptr, matrix, params); - } - paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); - shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY); - - dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), - SkRect::Make(dstRect), SkRect::Make(srcRect)); - - srcDrawContext = dstDrawContext; - srcRect = dstRect; - srcTexture = srcDrawContext->asTexture(); - dstDrawContext.swap(tmpDrawContext); - localSrcBounds = srcRect; - } - - srcRect = localDstBounds; - scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); - if (sigmaX > 0.0f) { - if (scaleFactorX > 1) { - SkASSERT(srcDrawContext); - - // Clear out a radius to the right of the srcRect to prevent the - // X convolution from reading garbage. - clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, - radiusX, srcRect.height()); - srcDrawContext->clear(&clearRect, 0x0, false); - } - - convolve_gaussian(dstDrawContext.get(), clip, srcRect, - srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, - srcBounds, srcOffset); - srcDrawContext = dstDrawContext; - srcTexture = srcDrawContext->asTexture(); - srcRect.offsetTo(0, 0); - dstDrawContext.swap(tmpDrawContext); - localSrcBounds = srcRect; - srcOffset.set(0, 0); - } - - if (sigmaY > 0.0f) { - if (scaleFactorY > 1 || sigmaX > 0.0f) { - SkASSERT(srcDrawContext); - - // Clear out a radius below the srcRect to prevent the Y - // convolution from reading garbage. - clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, - srcRect.width(), radiusY); - srcDrawContext->clear(&clearRect, 0x0, false); - } - - convolve_gaussian(dstDrawContext.get(), clip, srcRect, - srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, - srcBounds, srcOffset); - - srcDrawContext = dstDrawContext; - srcRect.offsetTo(0, 0); - dstDrawContext.swap(tmpDrawContext); - } - - SkASSERT(srcDrawContext); - srcTexture = nullptr; // we don't use this from here on out - - if (scaleFactorX > 1 || scaleFactorY > 1) { - // Clear one pixel to the right and below, to accommodate bilinear upsampling. - clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); - srcDrawContext->clear(&clearRect, 0x0, false); - clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height()); - srcDrawContext->clear(&clearRect, 0x0, false); - - SkMatrix matrix; - matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); - - GrPaint paint; - paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); - // FIXME: this should be mitchell, not bilinear. - GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); - sk_sp tex(srcDrawContext->asTexture()); - paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params); - paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); - - SkIRect dstRect(srcRect); - scale_irect(&dstRect, scaleFactorX, scaleFactorY); - - dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), - SkRect::Make(dstRect), SkRect::Make(srcRect)); - - srcDrawContext = dstDrawContext; - srcRect = dstRect; - dstDrawContext.swap(tmpDrawContext); - } - - return srcDrawContext; -} - -} - -#endif - diff --git a/src/core/SkGpuBlurUtils.h b/src/core/SkGpuBlurUtils.h deleted file mode 100644 index a12a08873c..0000000000 --- a/src/core/SkGpuBlurUtils.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef SkGpuBlurUtils_DEFINED -#define SkGpuBlurUtils_DEFINED - -#if SK_SUPPORT_GPU -#include "GrDrawContext.h" - -class GrContext; -class GrTexture; - -struct SkRect; - -namespace SkGpuBlurUtils { - /** - * 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 srcTexture The source texture to be blurred. - * @param colorSpace Color space of the source (used for the drawContext result, too). - * @param dstBounds The destination bounds, relative to the source texture. - * @param srcBounds The source bounds, relative to the source texture. If non-null, - * no pixels will be sampled outside of this rectangle. - * @param sigmaX The blur's standard deviation in X. - * @param sigmaY The blur's standard deviation in Y. - * @param fit backing fit for the returned draw context - * @return The drawContext containing the blurred result. - */ - sk_sp GaussianBlur(GrContext* context, - GrTexture* srcTexture, - sk_sp colorSpace, - const SkIRect& dstBounds, - const SkIRect* srcBounds, - float sigmaX, - float sigmaY, - SkBackingFit fit = SkBackingFit::kApprox); -}; - -#endif -#endif diff --git a/src/effects/SkBlurImageFilter.cpp b/src/effects/SkBlurImageFilter.cpp new file mode 100644 index 0000000000..37584ab3ad --- /dev/null +++ b/src/effects/SkBlurImageFilter.cpp @@ -0,0 +1,259 @@ +/* + * Copyright 2011 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkBlurImageFilter.h" + +#include "SkAutoPixmapStorage.h" +#include "SkColorPriv.h" +#include "SkGpuBlurUtils.h" +#include "SkOpts.h" +#include "SkReadBuffer.h" +#include "SkSpecialImage.h" +#include "SkWriteBuffer.h" + +#if SK_SUPPORT_GPU +#include "GrContext.h" +#include "SkGr.h" +#endif + +sk_sp SkBlurImageFilter::Make(SkScalar sigmaX, SkScalar sigmaY, + sk_sp input, + const CropRect* cropRect) { + if (0 == sigmaX && 0 == sigmaY && !cropRect) { + return input; + } + return sk_sp(new SkBlurImageFilter(sigmaX, sigmaY, input, cropRect)); +} + +// This rather arbitrary-looking value results in a maximum box blur kernel size +// of 1000 pixels on the raster path, which matches the WebKit and Firefox +// implementations. Since the GPU path does not compute a box blur, putting +// the limit on sigma ensures consistent behaviour between the GPU and +// raster paths. +#define MAX_SIGMA SkIntToScalar(532) + +static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) { + SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height()); + ctm.mapVectors(&sigma, 1); + sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA); + sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA); + return sigma; +} + +SkBlurImageFilter::SkBlurImageFilter(SkScalar sigmaX, + SkScalar sigmaY, + sk_sp input, + const CropRect* cropRect) + : INHERITED(&input, 1, cropRect) + , fSigma(SkSize::Make(sigmaX, sigmaY)) { +} + +sk_sp SkBlurImageFilter::CreateProc(SkReadBuffer& buffer) { + SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); + SkScalar sigmaX = buffer.readScalar(); + SkScalar sigmaY = buffer.readScalar(); + return Make(sigmaX, sigmaY, common.getInput(0), &common.cropRect()); +} + +void SkBlurImageFilter::flatten(SkWriteBuffer& buffer) const { + this->INHERITED::flatten(buffer); + buffer.writeScalar(fSigma.fWidth); + buffer.writeScalar(fSigma.fHeight); +} + +static void get_box3_params(SkScalar s, int *kernelSize, int* kernelSize3, int *lowOffset, + int *highOffset) { + float pi = SkScalarToFloat(SK_ScalarPI); + int d = static_cast(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi) / 4.0f + 0.5f)); + *kernelSize = d; + if (d % 2 == 1) { + *lowOffset = *highOffset = (d - 1) / 2; + *kernelSize3 = d; + } else { + *highOffset = d / 2; + *lowOffset = *highOffset - 1; + *kernelSize3 = d + 1; + } +} + +sk_sp SkBlurImageFilter::onFilterImage(SkSpecialImage* source, + const Context& ctx, + SkIPoint* offset) const { + SkIPoint inputOffset = SkIPoint::Make(0, 0); + + sk_sp input(this->filterInput(0, source, ctx, &inputOffset)); + if (!input) { + return nullptr; + } + + SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY, + input->width(), input->height()); + + SkIRect dstBounds; + if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) { + return nullptr; + } + if (!inputBounds.intersect(dstBounds)) { + return nullptr; + } + + const SkVector sigma = map_sigma(fSigma, ctx.ctm()); + +#if SK_SUPPORT_GPU + if (source->isTextureBacked()) { + GrContext* context = source->getContext(); + sk_sp inputTexture(input->asTextureRef(context)); + SkASSERT(inputTexture); + + if (0 == sigma.x() && 0 == sigma.y()) { + offset->fX = inputBounds.x(); + offset->fY = inputBounds.y(); + return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), + -inputOffset.y())); + } + + offset->fX = dstBounds.fLeft; + offset->fY = dstBounds.fTop; + inputBounds.offset(-inputOffset); + dstBounds.offset(-inputOffset); + sk_sp drawContext(SkGpuBlurUtils::GaussianBlur( + context, + inputTexture.get(), + sk_ref_sp(source->getColorSpace()), + dstBounds, + &inputBounds, + sigma.x(), + sigma.y())); + if (!drawContext) { + return nullptr; + } + + // TODO: Get the colorSpace from the drawContext (once it has one) + return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(dstBounds.width(), dstBounds.height()), + kNeedNewImageUniqueID_SpecialImage, + drawContext->asTexture(), + sk_ref_sp(input->getColorSpace()), &source->props()); + } +#endif + + int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; + int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; + get_box3_params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffsetX); + get_box3_params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffsetY); + + if (kernelSizeX < 0 || kernelSizeY < 0) { + return nullptr; + } + + if (kernelSizeX == 0 && kernelSizeY == 0) { + offset->fX = inputBounds.x(); + offset->fY = inputBounds.y(); + return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), + -inputOffset.y())); + } + + SkBitmap inputBM; + + if (!input->getROPixels(&inputBM)) { + return nullptr; + } + + if (inputBM.colorType() != kN32_SkColorType) { + return nullptr; + } + + SkImageInfo info = SkImageInfo::Make(dstBounds.width(), dstBounds.height(), + inputBM.colorType(), inputBM.alphaType()); + + SkBitmap tmp, dst; + if (!tmp.tryAllocPixels(info) || !dst.tryAllocPixels(info)) { + return nullptr; + } + + SkAutoLockPixels inputLock(inputBM), tmpLock(tmp), dstLock(dst); + + offset->fX = dstBounds.fLeft; + offset->fY = dstBounds.fTop; + SkPMColor* t = tmp.getAddr32(0, 0); + SkPMColor* d = dst.getAddr32(0, 0); + int w = dstBounds.width(), h = dstBounds.height(); + const SkPMColor* s = inputBM.getAddr32(inputBounds.x() - inputOffset.x(), + inputBounds.y() - inputOffset.y()); + inputBounds.offset(-dstBounds.x(), -dstBounds.y()); + dstBounds.offset(-dstBounds.x(), -dstBounds.y()); + SkIRect inputBoundsT = SkIRect::MakeLTRB(inputBounds.top(), inputBounds.left(), + inputBounds.bottom(), inputBounds.right()); + SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); + int sw = int(inputBM.rowBytes() >> 2); + + /** + * + * In order to make memory accesses cache-friendly, we reorder the passes to + * use contiguous memory reads wherever possible. + * + * For example, the 6 passes of the X-and-Y blur case are rewritten as + * follows. Instead of 3 passes in X and 3 passes in Y, we perform + * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, + * then 1 pass in X transposed to Y on write. + * + * +----+ +----+ +----+ +---+ +---+ +---+ +----+ + * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | -----> | AB | + * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blurXY +----+ + * +---+ +---+ +---+ + * + * In this way, two of the y-blurs become x-blurs applied to transposed + * images, and all memory reads are contiguous. + */ + if (kernelSizeX > 0 && kernelSizeY > 0) { + SkOpts::box_blur_xx(s, sw, inputBounds, t, kernelSizeX, lowOffsetX, highOffsetX, w, h); + SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX, lowOffsetX, w, h); + SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX, highOffsetX, w, h); + SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); + SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); + SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); + } else if (kernelSizeX > 0) { + SkOpts::box_blur_xx(s, sw, inputBounds, d, kernelSizeX, lowOffsetX, highOffsetX, w, h); + SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX, lowOffsetX, w, h); + SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX, highOffsetX, w, h); + } else if (kernelSizeY > 0) { + SkOpts::box_blur_yx(s, sw, inputBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); + SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); + SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); + } + + return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(), + dstBounds.height()), + dst, &source->props()); +} + + +SkRect SkBlurImageFilter::computeFastBounds(const SkRect& src) const { + SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; + bounds.outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)), + SkScalarMul(fSigma.height(), SkIntToScalar(3))); + return bounds; +} + +SkIRect SkBlurImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, + MapDirection) const { + SkVector sigma = map_sigma(fSigma, ctm); + return src.makeOutset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar(3))), + SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar(3)))); +} + +#ifndef SK_IGNORE_TO_STRING +void SkBlurImageFilter::toString(SkString* str) const { + str->appendf("SkBlurImageFilter: ("); + str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight); + + if (this->getInput(0)) { + this->getInput(0)->toString(str); + } + + str->append("))"); +} +#endif diff --git a/src/effects/SkGpuBlurUtils.cpp b/src/effects/SkGpuBlurUtils.cpp new file mode 100644 index 0000000000..ca762e88e3 --- /dev/null +++ b/src/effects/SkGpuBlurUtils.cpp @@ -0,0 +1,382 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkGpuBlurUtils.h" + +#include "SkRect.h" + +#if SK_SUPPORT_GPU +#include "effects/GrConvolutionEffect.h" +#include "effects/GrMatrixConvolutionEffect.h" +#include "GrContext.h" +#include "GrCaps.h" +#include "GrDrawContext.h" +#include "GrFixedClip.h" + +#define MAX_BLUR_SIGMA 4.0f + +static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) { + rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale)); + rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale)); + rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale)); + 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) { + *scaleFactor = 1; + while (sigma > MAX_BLUR_SIGMA) { + *scaleFactor *= 2; + sigma *= 0.5f; + if (*scaleFactor > maxTextureSize) { + *scaleFactor = maxTextureSize; + sigma = MAX_BLUR_SIGMA; + } + } + *radius = static_cast(ceilf(sigma * 3.0f)); + SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius); + return sigma; +} + +static void convolve_gaussian_1d(GrDrawContext* drawContext, + const GrClip& clip, + const SkIRect& dstRect, + const SkIPoint& srcOffset, + GrTexture* texture, + Gr1DKernelEffect::Direction direction, + int radius, + float sigma, + bool useBounds, + float bounds[2]) { + GrPaint paint; + paint.setGammaCorrect(drawContext->isGammaCorrect()); + sk_sp conv(GrConvolutionEffect::MakeGaussian( + texture, direction, radius, sigma, useBounds, bounds)); + paint.addColorFragmentProcessor(std::move(conv)); + paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); + SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), + -SkIntToScalar(srcOffset.y())); + drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), + SkRect::Make(dstRect), localMatrix); +} + +static void convolve_gaussian_2d(GrDrawContext* drawContext, + const GrClip& clip, + const SkIRect& dstRect, + const SkIPoint& srcOffset, + GrTexture* texture, + int radiusX, + int radiusY, + SkScalar sigmaX, + SkScalar sigmaY, + const SkIRect* srcBounds) { + SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), + -SkIntToScalar(srcOffset.y())); + SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); + SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); + GrPaint paint; + paint.setGammaCorrect(drawContext->isGammaCorrect()); + SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect(); + + sk_sp conv(GrMatrixConvolutionEffect::MakeGaussian( + texture, bounds, size, 1.0, 0.0, kernelOffset, + srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode, + true, sigmaX, sigmaY)); + paint.addColorFragmentProcessor(std::move(conv)); + paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); + drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), + SkRect::Make(dstRect), localMatrix); +} + +static void convolve_gaussian(GrDrawContext* drawContext, + const GrClip& clip, + const SkIRect& srcRect, + GrTexture* texture, + Gr1DKernelEffect::Direction direction, + int radius, + float sigma, + const SkIRect* srcBounds, + const SkIPoint& srcOffset) { + float bounds[2] = { 0.0f, 1.0f }; + SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height()); + if (!srcBounds) { + convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture, + direction, radius, sigma, false, bounds); + return; + } + SkIRect midRect = *srcBounds, leftRect, rightRect; + midRect.offset(srcOffset); + SkIRect topRect, bottomRect; + if (direction == Gr1DKernelEffect::kX_Direction) { + bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width(); + bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width(); + topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top()); + bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom()); + midRect.inset(radius, 0); + leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom()); + rightRect = + SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom()); + dstRect.fTop = midRect.top(); + dstRect.fBottom = midRect.bottom(); + } else { + bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height(); + bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height(); + topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom()); + bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom()); + midRect.inset(0, radius); + leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top()); + rightRect = + SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height()); + dstRect.fLeft = midRect.left(); + dstRect.fRight = midRect.right(); + } + if (!topRect.isEmpty()) { + drawContext->clear(&topRect, 0, false); + } + + if (!bottomRect.isEmpty()) { + drawContext->clear(&bottomRect, 0, false); + } + if (midRect.isEmpty()) { + // Blur radius covers srcBounds; use bounds over entire draw + convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture, + direction, radius, sigma, true, bounds); + } else { + // Draw right and left margins with bounds; middle without. + convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture, + direction, radius, sigma, true, bounds); + convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, + direction, radius, sigma, true, bounds); + convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, + direction, radius, sigma, false, bounds); + } +} + +namespace SkGpuBlurUtils { + +sk_sp GaussianBlur(GrContext* context, + GrTexture* origSrc, + sk_sp colorSpace, + const SkIRect& dstBounds, + const SkIRect* srcBounds, + float sigmaX, + float sigmaY, + SkBackingFit fit) { + SkASSERT(context); + SkIRect clearRect; + int scaleFactorX, radiusX; + int scaleFactorY, radiusY; + int maxTextureSize = context->caps()->maxTextureSize(); + sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); + sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); + SkASSERT(sigmaX || sigmaY); + + SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y()); + SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height()); + SkIRect localSrcBounds; + SkIRect srcRect; + if (srcBounds) { + srcRect = localSrcBounds = *srcBounds; + srcRect.offset(srcOffset); + srcBounds = &localSrcBounds; + } else { + srcRect = localDstBounds; + } + + scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); + scale_irect(&srcRect, scaleFactorX, scaleFactorY); + + // setup new clip + GrFixedClip clip(localDstBounds); + + sk_sp srcTexture(sk_ref_sp(origSrc)); + + SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || + kRGBA_8888_GrPixelConfig == srcTexture->config() || + kSRGBA_8888_GrPixelConfig == srcTexture->config() || + kSBGRA_8888_GrPixelConfig == srcTexture->config() || + kAlpha_8_GrPixelConfig == srcTexture->config()); + + const int width = dstBounds.width(); + const int height = dstBounds.height(); + const GrPixelConfig config = srcTexture->config(); + + sk_sp dstDrawContext(context->makeDrawContext(fit, + width, height, config, colorSpace, + 0, kDefault_GrSurfaceOrigin)); + if (!dstDrawContext) { + return nullptr; + } + + // 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 + if (sigmaX > 0.0f && sigmaY > 0.0f && + (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { + // We shouldn't be scaling because this is a small size blur + SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); + + convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset, + srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds); + + return dstDrawContext; + } + + sk_sp tmpDrawContext(context->makeDrawContext(fit, + width, height, config, colorSpace, + 0, kDefault_GrSurfaceOrigin)); + if (!tmpDrawContext) { + return nullptr; + } + + sk_sp srcDrawContext; + + SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); + + for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { + GrPaint paint; + paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); + SkMatrix matrix; + matrix.setIDiv(srcTexture->width(), srcTexture->height()); + SkIRect dstRect(srcRect); + if (srcBounds && i == 1) { + SkRect domain; + matrix.mapRect(&domain, SkRect::Make(*srcBounds)); + domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, + (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); + sk_sp fp(GrTextureDomainEffect::Make( + srcTexture.get(), + nullptr, + matrix, + domain, + GrTextureDomain::kDecal_Mode, + GrTextureParams::kBilerp_FilterMode)); + paint.addColorFragmentProcessor(std::move(fp)); + srcRect.offset(-srcOffset); + srcOffset.set(0, 0); + } else { + GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); + paint.addColorTextureProcessor(srcTexture.get(), nullptr, matrix, params); + } + paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); + shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY); + + dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), + SkRect::Make(dstRect), SkRect::Make(srcRect)); + + srcDrawContext = dstDrawContext; + srcRect = dstRect; + srcTexture = srcDrawContext->asTexture(); + dstDrawContext.swap(tmpDrawContext); + localSrcBounds = srcRect; + } + + srcRect = localDstBounds; + scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); + if (sigmaX > 0.0f) { + if (scaleFactorX > 1) { + SkASSERT(srcDrawContext); + + // Clear out a radius to the right of the srcRect to prevent the + // X convolution from reading garbage. + clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, + radiusX, srcRect.height()); + srcDrawContext->clear(&clearRect, 0x0, false); + } + + convolve_gaussian(dstDrawContext.get(), clip, srcRect, + srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, + srcBounds, srcOffset); + srcDrawContext = dstDrawContext; + srcTexture = srcDrawContext->asTexture(); + srcRect.offsetTo(0, 0); + dstDrawContext.swap(tmpDrawContext); + localSrcBounds = srcRect; + srcOffset.set(0, 0); + } + + if (sigmaY > 0.0f) { + if (scaleFactorY > 1 || sigmaX > 0.0f) { + SkASSERT(srcDrawContext); + + // Clear out a radius below the srcRect to prevent the Y + // convolution from reading garbage. + clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, + srcRect.width(), radiusY); + srcDrawContext->clear(&clearRect, 0x0, false); + } + + convolve_gaussian(dstDrawContext.get(), clip, srcRect, + srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, + srcBounds, srcOffset); + + srcDrawContext = dstDrawContext; + srcRect.offsetTo(0, 0); + dstDrawContext.swap(tmpDrawContext); + } + + SkASSERT(srcDrawContext); + srcTexture = nullptr; // we don't use this from here on out + + if (scaleFactorX > 1 || scaleFactorY > 1) { + // Clear one pixel to the right and below, to accommodate bilinear upsampling. + clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); + srcDrawContext->clear(&clearRect, 0x0, false); + clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height()); + srcDrawContext->clear(&clearRect, 0x0, false); + + SkMatrix matrix; + matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); + + GrPaint paint; + paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); + // FIXME: this should be mitchell, not bilinear. + GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); + sk_sp tex(srcDrawContext->asTexture()); + paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params); + paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); + + SkIRect dstRect(srcRect); + scale_irect(&dstRect, scaleFactorX, scaleFactorY); + + dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), + SkRect::Make(dstRect), SkRect::Make(srcRect)); + + srcDrawContext = dstDrawContext; + srcRect = dstRect; + dstDrawContext.swap(tmpDrawContext); + } + + return srcDrawContext; +} + +} + +#endif + diff --git a/src/effects/SkGpuBlurUtils.h b/src/effects/SkGpuBlurUtils.h new file mode 100644 index 0000000000..a12a08873c --- /dev/null +++ b/src/effects/SkGpuBlurUtils.h @@ -0,0 +1,46 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkGpuBlurUtils_DEFINED +#define SkGpuBlurUtils_DEFINED + +#if SK_SUPPORT_GPU +#include "GrDrawContext.h" + +class GrContext; +class GrTexture; + +struct SkRect; + +namespace SkGpuBlurUtils { + /** + * 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 srcTexture The source texture to be blurred. + * @param colorSpace Color space of the source (used for the drawContext result, too). + * @param dstBounds The destination bounds, relative to the source texture. + * @param srcBounds The source bounds, relative to the source texture. If non-null, + * no pixels will be sampled outside of this rectangle. + * @param sigmaX The blur's standard deviation in X. + * @param sigmaY The blur's standard deviation in Y. + * @param fit backing fit for the returned draw context + * @return The drawContext containing the blurred result. + */ + sk_sp GaussianBlur(GrContext* context, + GrTexture* srcTexture, + sk_sp colorSpace, + const SkIRect& dstBounds, + const SkIRect* srcBounds, + float sigmaX, + float sigmaY, + SkBackingFit fit = SkBackingFit::kApprox); +}; + +#endif +#endif diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp index fdafd9609a..cc697ce0ba 100644 --- a/src/ports/SkGlobalInitialization_default.cpp +++ b/src/ports/SkGlobalInitialization_default.cpp @@ -102,7 +102,7 @@ void SkFlattenable::PrivateInitializer::InitEffects() { SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSumPathEffect) // ImageFilter - SkImageFilter::InitializeFlattenables(); + SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDilateImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDisplacementMapEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDropShadowImageFilter)