robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkGpuBlurUtils.h" |
| 9 | |
| 10 | #include "SkRect.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | #include "effects/GrConvolutionEffect.h" |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 14 | #include "effects/GrMatrixConvolutionEffect.h" |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 16 | #include "GrCaps.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 17 | #include "GrDrawContext.h" |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 18 | |
| 19 | #define MAX_BLUR_SIGMA 4.0f |
| 20 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 21 | static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) { |
| 22 | rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale)); |
| 23 | rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale)); |
| 24 | rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale)); |
| 25 | rect->fBottom = SkScalarCeilToInt(SkScalarMul(rect->fBottom, yScale)); |
| 26 | } |
| 27 | |
| 28 | static void scale_irect(SkIRect* rect, int xScale, int yScale) { |
| 29 | rect->fLeft *= xScale; |
| 30 | rect->fTop *= yScale; |
| 31 | rect->fRight *= xScale; |
| 32 | rect->fBottom *= yScale; |
| 33 | } |
| 34 | |
| 35 | #ifdef SK_DEBUG |
| 36 | static inline int is_even(int x) { return !(x & 1); } |
| 37 | #endif |
| 38 | |
| 39 | static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) { |
| 40 | if (xAxis) { |
| 41 | SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight)); |
| 42 | rect->fLeft /= 2; |
| 43 | rect->fRight /= 2; |
| 44 | } |
| 45 | if (yAxis) { |
| 46 | SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom)); |
| 47 | rect->fTop /= 2; |
| 48 | rect->fBottom /= 2; |
| 49 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 50 | } |
| 51 | |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 52 | static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) { |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 53 | *scaleFactor = 1; |
| 54 | while (sigma > MAX_BLUR_SIGMA) { |
| 55 | *scaleFactor *= 2; |
| 56 | sigma *= 0.5f; |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 57 | if (*scaleFactor > maxTextureSize) { |
| 58 | *scaleFactor = maxTextureSize; |
| 59 | sigma = MAX_BLUR_SIGMA; |
| 60 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 61 | } |
| 62 | *radius = static_cast<int>(ceilf(sigma * 3.0f)); |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 63 | SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 64 | return sigma; |
| 65 | } |
| 66 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 67 | static void convolve_gaussian_1d(GrDrawContext* drawContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 68 | const GrClip& clip, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 69 | const SkIRect& dstRect, |
| 70 | const SkIPoint& srcOffset, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 71 | GrTexture* texture, |
| 72 | Gr1DKernelEffect::Direction direction, |
| 73 | int radius, |
| 74 | float sigma, |
| 75 | bool useBounds, |
| 76 | float bounds[2]) { |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 77 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 78 | paint.setGammaCorrect(drawContext->isGammaCorrect()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 79 | SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 80 | texture, direction, radius, sigma, useBounds, bounds)); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 81 | paint.addColorFragmentProcessor(conv); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 82 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 83 | SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), |
| 84 | -SkIntToScalar(srcOffset.y())); |
| 85 | drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), |
| 86 | SkRect::Make(dstRect), localMatrix); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 87 | } |
| 88 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 89 | static void convolve_gaussian_2d(GrDrawContext* drawContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 90 | const GrClip& clip, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 91 | const SkIRect& dstRect, |
| 92 | const SkIPoint& srcOffset, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 93 | GrTexture* texture, |
| 94 | int radiusX, |
| 95 | int radiusY, |
| 96 | SkScalar sigmaX, |
| 97 | SkScalar sigmaY, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 98 | const SkIRect* srcBounds) { |
| 99 | SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), |
| 100 | -SkIntToScalar(srcOffset.y())); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 101 | SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); |
| 102 | SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); |
| 103 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 104 | paint.setGammaCorrect(drawContext->isGammaCorrect()); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 105 | SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect(); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 106 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 107 | SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian( |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 108 | texture, bounds, size, 1.0, 0.0, kernelOffset, |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 109 | srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 110 | true, sigmaX, sigmaY)); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 111 | paint.addColorFragmentProcessor(conv); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 112 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 113 | drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), |
| 114 | SkRect::Make(dstRect), localMatrix); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 115 | } |
| 116 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 117 | static void convolve_gaussian(GrDrawContext* drawContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 118 | const GrClip& clip, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 119 | const SkIRect& srcRect, |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 120 | GrTexture* texture, |
| 121 | Gr1DKernelEffect::Direction direction, |
| 122 | int radius, |
| 123 | float sigma, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 124 | const SkIRect* srcBounds, |
| 125 | const SkIPoint& srcOffset) { |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 126 | float bounds[2] = { 0.0f, 1.0f }; |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 127 | SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 128 | if (!srcBounds) { |
senorblanco | 48343ee | 2015-11-03 05:07:43 -0800 | [diff] [blame] | 129 | convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 130 | direction, radius, sigma, false, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 133 | SkIRect midRect = *srcBounds, leftRect, rightRect; |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 134 | midRect.offset(srcOffset); |
| 135 | SkIRect topRect, bottomRect; |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 136 | if (direction == Gr1DKernelEffect::kX_Direction) { |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 137 | bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width(); |
| 138 | bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width(); |
| 139 | topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top()); |
| 140 | bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom()); |
| 141 | midRect.inset(radius, 0); |
| 142 | leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 143 | rightRect = |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 144 | SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 145 | dstRect.fTop = midRect.top(); |
| 146 | dstRect.fBottom = midRect.bottom(); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 147 | } else { |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 148 | bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height(); |
| 149 | bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height(); |
| 150 | topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom()); |
| 151 | bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom()); |
| 152 | midRect.inset(0, radius); |
| 153 | leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 154 | rightRect = |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 155 | SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 156 | dstRect.fLeft = midRect.left(); |
| 157 | dstRect.fRight = midRect.right(); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 158 | } |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 159 | if (!topRect.isEmpty()) { |
| 160 | drawContext->clear(&topRect, 0, false); |
| 161 | } |
| 162 | |
| 163 | if (!bottomRect.isEmpty()) { |
| 164 | drawContext->clear(&bottomRect, 0, false); |
| 165 | } |
| 166 | if (midRect.isEmpty()) { |
| 167 | // Blur radius covers srcBounds; use bounds over entire draw |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 168 | convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 169 | direction, radius, sigma, true, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 170 | } else { |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 171 | // Draw right and left margins with bounds; middle without. |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 172 | convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 173 | direction, radius, sigma, true, bounds); |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 174 | convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 175 | direction, radius, sigma, true, bounds); |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 176 | convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 177 | direction, radius, sigma, false, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 181 | namespace SkGpuBlurUtils { |
| 182 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 183 | sk_sp<GrDrawContext> GaussianBlur(GrContext* context, |
| 184 | GrTexture* origSrc, |
| 185 | bool gammaCorrect, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 186 | const SkIRect& dstBounds, |
| 187 | const SkIRect* srcBounds, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 188 | float sigmaX, |
| 189 | float sigmaY) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 190 | SkASSERT(context); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 191 | SkIRect clearRect; |
| 192 | int scaleFactorX, radiusX; |
| 193 | int scaleFactorY, radiusY; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 194 | int maxTextureSize = context->caps()->maxTextureSize(); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 195 | sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
| 196 | sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 197 | SkASSERT(sigmaX || sigmaY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 198 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 199 | SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y()); |
| 200 | SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height()); |
| 201 | SkIRect localSrcBounds; |
| 202 | SkIRect srcRect; |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 203 | if (srcBounds) { |
| 204 | srcRect = localSrcBounds = *srcBounds; |
| 205 | srcRect.offset(srcOffset); |
| 206 | srcBounds = &localSrcBounds; |
| 207 | } else { |
| 208 | srcRect = localDstBounds; |
| 209 | } |
| 210 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 211 | scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
| 212 | scale_irect(&srcRect, scaleFactorX, scaleFactorY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 213 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 214 | // setup new clip |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 215 | GrClip clip(localDstBounds); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 216 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 217 | sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc)); |
| 218 | |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 219 | SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 220 | kRGBA_8888_GrPixelConfig == srcTexture->config() || |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 221 | kSRGBA_8888_GrPixelConfig == srcTexture->config() || |
| 222 | kSBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 223 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 224 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 225 | const int width = dstBounds.width(); |
| 226 | const int height = dstBounds.height(); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 227 | const GrPixelConfig config = srcTexture->config(); |
| 228 | |
| 229 | const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0, |
| 230 | SkSurfaceProps::kLegacyFontHost_InitType); |
| 231 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 232 | sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox, |
| 233 | width, height, config, |
| 234 | 0, kDefault_GrSurfaceOrigin, |
| 235 | &props)); |
| 236 | if (!dstDrawContext) { |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 240 | // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just |
| 241 | // launch a single non separable kernel vs two launches |
| 242 | if (sigmaX > 0.0f && sigmaY > 0.0f && |
| 243 | (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
| 244 | // We shouldn't be scaling because this is a small size blur |
| 245 | SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
| 246 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 247 | convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 248 | srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 249 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 250 | return dstDrawContext; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 251 | } |
| 252 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 253 | sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kApprox, |
| 254 | width, height, config, |
| 255 | 0, kDefault_GrSurfaceOrigin, |
| 256 | &props)); |
| 257 | if (!tmpDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | return nullptr; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 259 | } |
| 260 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 261 | sk_sp<GrDrawContext> srcDrawContext; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 262 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 263 | SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); |
| 264 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 265 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 266 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 267 | paint.setGammaCorrect(gammaCorrect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 268 | SkMatrix matrix; |
| 269 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 270 | SkIRect dstRect(srcRect); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 271 | if (srcBounds && i == 1) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 272 | SkRect domain; |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 273 | matrix.mapRect(&domain, SkRect::Make(*srcBounds)); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 274 | domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, |
| 275 | (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 276 | sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create( |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 277 | srcTexture.get(), |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 278 | matrix, |
| 279 | domain, |
| 280 | GrTextureDomain::kDecal_Mode, |
| 281 | GrTextureParams::kBilerp_FilterMode)); |
| 282 | paint.addColorFragmentProcessor(fp.get()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 283 | srcRect.offset(-srcOffset); |
| 284 | srcOffset.set(0, 0); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 285 | } else { |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 286 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 287 | paint.addColorTextureProcessor(srcTexture.get(), matrix, params); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 288 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 289 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 290 | shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 291 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 292 | dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), |
| 293 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 294 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 295 | srcDrawContext = dstDrawContext; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 296 | srcRect = dstRect; |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 297 | srcTexture = srcDrawContext->asTexture(); |
| 298 | dstDrawContext.swap(tmpDrawContext); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 299 | localSrcBounds = srcRect; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 300 | } |
| 301 | |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 302 | srcRect = localDstBounds; |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 303 | scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 304 | if (sigmaX > 0.0f) { |
| 305 | if (scaleFactorX > 1) { |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 306 | SkASSERT(srcDrawContext); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 307 | |
| 308 | // Clear out a radius to the right of the srcRect to prevent the |
| 309 | // X convolution from reading garbage. |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 310 | clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, |
| 311 | radiusX, srcRect.height()); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 312 | srcDrawContext->clear(&clearRect, 0x0, false); |
| 313 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 314 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 315 | convolve_gaussian(dstDrawContext.get(), clip, srcRect, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 316 | srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 317 | srcBounds, srcOffset); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 318 | srcDrawContext = dstDrawContext; |
| 319 | srcTexture = srcDrawContext->asTexture(); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 320 | srcRect.offsetTo(0, 0); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 321 | dstDrawContext.swap(tmpDrawContext); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 322 | localSrcBounds = srcRect; |
| 323 | srcOffset.set(0, 0); |
| 324 | } |
| 325 | |
| 326 | if (sigmaY > 0.0f) { |
| 327 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 328 | SkASSERT(srcDrawContext); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 329 | |
| 330 | // Clear out a radius below the srcRect to prevent the Y |
| 331 | // convolution from reading garbage. |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 332 | clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, |
| 333 | srcRect.width(), radiusY); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 334 | srcDrawContext->clear(&clearRect, 0x0, false); |
| 335 | } |
| 336 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 337 | convolve_gaussian(dstDrawContext.get(), clip, srcRect, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 338 | srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 339 | srcBounds, srcOffset); |
robertphillips | 56a85e6 | 2016-05-06 07:17:49 -0700 | [diff] [blame] | 340 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 341 | srcDrawContext = dstDrawContext; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 342 | srcRect.offsetTo(0, 0); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 343 | dstDrawContext.swap(tmpDrawContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 344 | } |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 345 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 346 | SkASSERT(srcDrawContext); |
| 347 | srcTexture = nullptr; // we don't use this from here on out |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 348 | |
| 349 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 350 | // Clear one pixel to the right and below, to accommodate bilinear upsampling. |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 351 | clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 352 | srcDrawContext->clear(&clearRect, 0x0, false); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 353 | clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height()); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 354 | srcDrawContext->clear(&clearRect, 0x0, false); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 355 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 356 | SkMatrix matrix; |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 357 | matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 358 | |
| 359 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 360 | paint.setGammaCorrect(gammaCorrect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 361 | // FIXME: this should be mitchell, not bilinear. |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 362 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 363 | sk_sp<GrTexture> tex(srcDrawContext->asTexture()); |
| 364 | paint.addColorTextureProcessor(tex.get(), matrix, params); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 365 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 366 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 367 | SkIRect dstRect(srcRect); |
| 368 | scale_irect(&dstRect, scaleFactorX, scaleFactorY); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 369 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 370 | dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), |
| 371 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 372 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 373 | srcDrawContext = dstDrawContext; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 374 | srcRect = dstRect; |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 375 | dstDrawContext.swap(tmpDrawContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 376 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 377 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 378 | return srcDrawContext; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 379 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 380 | |
robertphillips@google.com | cce4102 | 2013-07-15 15:47:10 +0000 | [diff] [blame] | 381 | } |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 382 | |
| 383 | #endif |
| 384 | |