diff options
author | 2012-11-16 17:22:33 +0000 | |
---|---|---|
committer | 2012-11-16 17:22:33 +0000 | |
commit | c4381309649c5cf338dcf6a7fc8296451a686d6b (patch) | |
tree | b5b7ea1f091b646de5826b43ec5d0871813491f0 | |
parent | 59d968fb29b39cc1f4eb0ac687007c74618c6c1c (diff) |
Separable blur: subpixel blurring. We approximate intermediate values of blur for even kernel sizes by using a kernel of size N offset to the left in the first pass, a kernel of size N offset to the right in the second pass, and a centered kernel of size N + 1 in the third pass. This required adding support for asymmetrical radii to the box blur.
Since this can only be done in the 3-pass blur for separable blurs, we turn off the optimization that drops to low quality for blurs of < 3 pixels.
Review URL: https://codereview.appspot.com/6843072
git-svn-id: http://skia.googlecode.com/svn/trunk@6464 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | src/effects/SkBlurMask.cpp | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/src/effects/SkBlurMask.cpp b/src/effects/SkBlurMask.cpp index 41f4b5b992..e762ae6f65 100644 --- a/src/effects/SkBlurMask.cpp +++ b/src/effects/SkBlurMask.cpp @@ -20,12 +20,13 @@ * (width + radius * 2) * height bytes in size. */ static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst, - int radius, int width, int height, bool transpose) + int leftRadius, int rightRadius, int width, int height, + bool transpose) { - int kernelSize = radius * 2 + 1; - int border = SkMin32(width, radius * 2); + int kernelSize = leftRadius + rightRadius + 1; + int border = SkMin32(width, leftRadius + rightRadius); uint32_t scale = (1 << 24) / kernelSize; - int new_width = width + radius * 2; + int new_width = width + SkMax32(leftRadius, rightRadius) * 2; int dst_x_stride = transpose ? height : 1; int dst_y_stride = transpose ? 1 : new_width; for (int y = 0; y < height; ++y) { @@ -33,16 +34,19 @@ static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst, uint8_t* dptr = dst + y * dst_y_stride; const uint8_t* right = src + y * src_y_stride; const uint8_t* left = right; + for (int x = 0; x < leftRadius - rightRadius; x++) { + *dptr++ = 0; + } for (int x = 0; x < border; ++x) { sum += *right++; *dptr = (sum * scale) >> 24; dptr += dst_x_stride; } - for (int x = width; x < radius * 2; ++x) { + for (int x = width; x < leftRadius + rightRadius; ++x) { *dptr = (sum * scale) >> 24; dptr += dst_x_stride; } - for (int x = radius * 2; x < width; ++x) { + for (int x = leftRadius + rightRadius; x < width; ++x) { sum += *right++; *dptr = (sum * scale) >> 24; sum -= *left++; @@ -53,11 +57,22 @@ static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst, sum -= *left++; dptr += dst_x_stride; } + for (int x = 0; x < rightRadius - leftRadius; x++) { + *dptr++ = 0; + } SkASSERT(sum == 0); } return new_width; } +static void get_adjusted_radii(SkScalar passRadius, int *loRadius, int *hiRadius) +{ + *loRadius = *hiRadius = SkScalarCeil(passRadius); + if (SkIntToScalar(*hiRadius) - passRadius > SkFloatToScalar(0.5f)) { + *loRadius = *hiRadius - 1; + } +} + // Unrolling the integer blur kernel seems to give us a ~15% speedup on Windows, // breakeven on Mac, and ~15% slowdown on Linux. // Reading a word at a time when bulding the sum buffer seems to give @@ -606,7 +621,7 @@ bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, } // Force high quality off for small radii (performance) - if (radius < SkIntToScalar(3)) quality = kLow_Quality; + if (radius < SkIntToScalar(3) && !separable) quality = kLow_Quality; // highQuality: use three box blur passes as a cheap way to approximate a Gaussian blur int passCount = (quality == kHigh_Quality) ? 3 : 1; @@ -654,17 +669,19 @@ bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, int w = sw, h = sh; if (quality == kHigh_Quality) { + int loRadius, hiRadius; + get_adjusted_radii(passRadius, &loRadius, &hiRadius); // Do three X blurs, with a transpose on the final one. - w = boxBlur(sp, src.fRowBytes, tp, rx, w, h, false); - w = boxBlur(tp, w, dp, rx, w, h, false); - w = boxBlur(dp, w, tp, rx, w, h, true); + w = boxBlur(sp, src.fRowBytes, tp, loRadius, hiRadius, w, h, false); + w = boxBlur(tp, w, dp, hiRadius, loRadius, w, h, false); + w = boxBlur(dp, w, tp, hiRadius, hiRadius, w, h, true); // Do three Y blurs, with a transpose on the final one. - h = boxBlur(tp, h, dp, ry, h, w, false); - h = boxBlur(dp, h, tp, ry, h, w, false); - h = boxBlur(tp, h, dp, ry, h, w, true); + h = boxBlur(tp, h, dp, loRadius, hiRadius, h, w, false); + h = boxBlur(dp, h, tp, hiRadius, loRadius, h, w, false); + h = boxBlur(tp, h, dp, hiRadius, hiRadius, h, w, true); } else { - w = boxBlur(sp, src.fRowBytes, tp, rx, w, h, true); - h = boxBlur(tp, h, dp, ry, h, w, true); + w = boxBlur(sp, src.fRowBytes, tp, rx, rx, w, h, true); + h = boxBlur(tp, h, dp, ry, ry, h, w, true); } } else { const size_t storageW = sw + 2 * (passCount - 1) * rx + 1; |