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 | #endif |
| 19 | |
| 20 | namespace SkGpuBlurUtils { |
| 21 | |
| 22 | #if SK_SUPPORT_GPU |
| 23 | |
| 24 | #define MAX_BLUR_SIGMA 4.0f |
| 25 | |
| 26 | static void scale_rect(SkRect* rect, float xScale, float yScale) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 27 | rect->fLeft = SkScalarMul(rect->fLeft, xScale); |
| 28 | rect->fTop = SkScalarMul(rect->fTop, yScale); |
| 29 | rect->fRight = SkScalarMul(rect->fRight, xScale); |
| 30 | rect->fBottom = SkScalarMul(rect->fBottom, yScale); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 31 | } |
| 32 | |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 33 | 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] | 34 | *scaleFactor = 1; |
| 35 | while (sigma > MAX_BLUR_SIGMA) { |
| 36 | *scaleFactor *= 2; |
| 37 | sigma *= 0.5f; |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 38 | if (*scaleFactor > maxTextureSize) { |
| 39 | *scaleFactor = maxTextureSize; |
| 40 | sigma = MAX_BLUR_SIGMA; |
| 41 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 42 | } |
| 43 | *radius = static_cast<int>(ceilf(sigma * 3.0f)); |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 44 | SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 45 | return sigma; |
| 46 | } |
| 47 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 48 | static void convolve_gaussian_1d(GrDrawContext* drawContext, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 49 | GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 50 | const GrClip& clip, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 51 | const SkRect& srcRect, |
| 52 | const SkRect& dstRect, |
| 53 | GrTexture* texture, |
| 54 | Gr1DKernelEffect::Direction direction, |
| 55 | int radius, |
| 56 | float sigma, |
| 57 | bool useBounds, |
| 58 | float bounds[2]) { |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 59 | GrPaint paint; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian( |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 61 | texture, direction, radius, sigma, useBounds, bounds)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 62 | paint.addColorProcessor(conv); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 63 | drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 64 | } |
| 65 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 66 | static void convolve_gaussian_2d(GrDrawContext* drawContext, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 67 | GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 68 | const GrClip& clip, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 69 | const SkRect& srcRect, |
| 70 | const SkRect& dstRect, |
| 71 | GrTexture* texture, |
| 72 | int radiusX, |
| 73 | int radiusY, |
| 74 | SkScalar sigmaX, |
| 75 | SkScalar sigmaY, |
| 76 | bool useBounds, |
| 77 | SkIRect bounds) { |
| 78 | SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); |
| 79 | SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); |
| 80 | GrPaint paint; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 81 | SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian( |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 82 | texture, bounds, size, 1.0, 0.0, kernelOffset, |
| 83 | useBounds ? GrTextureDomain::kClamp_Mode : GrTextureDomain::kIgnore_Mode, |
| 84 | true, sigmaX, sigmaY)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | paint.addColorProcessor(conv); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 86 | drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), dstRect, srcRect); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 87 | } |
| 88 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 89 | static void convolve_gaussian(GrDrawContext* drawContext, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 90 | GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 91 | const GrClip& clip, |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 92 | const SkRect& srcRect, |
| 93 | const SkRect& dstRect, |
| 94 | GrTexture* texture, |
| 95 | Gr1DKernelEffect::Direction direction, |
| 96 | int radius, |
| 97 | float sigma, |
| 98 | bool cropToSrcRect) { |
| 99 | float bounds[2] = { 0.0f, 1.0f }; |
| 100 | if (!cropToSrcRect) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 101 | convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 102 | direction, radius, sigma, false, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | SkRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
| 106 | SkRect middleSrcRect = srcRect, middleDstRect = dstRect; |
| 107 | SkRect upperSrcRect = srcRect, upperDstRect = dstRect; |
| 108 | SkScalar size; |
| 109 | SkScalar rad = SkIntToScalar(radius); |
| 110 | if (direction == Gr1DKernelEffect::kX_Direction) { |
| 111 | bounds[0] = SkScalarToFloat(srcRect.left()) / texture->width(); |
| 112 | bounds[1] = SkScalarToFloat(srcRect.right()) / texture->width(); |
| 113 | size = srcRect.width(); |
| 114 | lowerSrcRect.fRight = srcRect.left() + rad; |
| 115 | lowerDstRect.fRight = dstRect.left() + rad; |
| 116 | upperSrcRect.fLeft = srcRect.right() - rad; |
| 117 | upperDstRect.fLeft = dstRect.right() - rad; |
| 118 | middleSrcRect.inset(rad, 0); |
| 119 | middleDstRect.inset(rad, 0); |
| 120 | } else { |
| 121 | bounds[0] = SkScalarToFloat(srcRect.top()) / texture->height(); |
| 122 | bounds[1] = SkScalarToFloat(srcRect.bottom()) / texture->height(); |
| 123 | size = srcRect.height(); |
| 124 | lowerSrcRect.fBottom = srcRect.top() + rad; |
| 125 | lowerDstRect.fBottom = dstRect.top() + rad; |
| 126 | upperSrcRect.fTop = srcRect.bottom() - rad; |
| 127 | upperDstRect.fTop = dstRect.bottom() - rad; |
| 128 | middleSrcRect.inset(0, rad); |
| 129 | middleDstRect.inset(0, rad); |
| 130 | } |
| 131 | if (radius >= size * SK_ScalarHalf) { |
| 132 | // Blur radius covers srcRect; use bounds over entire draw |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 133 | convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 134 | direction, radius, sigma, true, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 135 | } else { |
| 136 | // Draw upper and lower margins with bounds; middle without. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 137 | convolve_gaussian_1d(drawContext, rt, clip, lowerSrcRect, lowerDstRect, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 138 | direction, radius, sigma, true, bounds); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 139 | convolve_gaussian_1d(drawContext, rt, clip, upperSrcRect, upperDstRect, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 140 | direction, radius, sigma, true, bounds); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 141 | convolve_gaussian_1d(drawContext, rt, clip, middleSrcRect, middleDstRect, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 142 | direction, radius, sigma, false, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 146 | GrTexture* GaussianBlur(GrContext* context, |
| 147 | GrTexture* srcTexture, |
| 148 | bool canClobberSrc, |
| 149 | const SkRect& rect, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 150 | bool cropToRect, |
skia.committer@gmail.com | 977409a | 2013-07-16 07:00:56 +0000 | [diff] [blame] | 151 | float sigmaX, |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 152 | float sigmaY) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 153 | SkASSERT(context); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 154 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 155 | SkIRect clearRect; |
| 156 | int scaleFactorX, radiusX; |
| 157 | int scaleFactorY, radiusY; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame^] | 158 | int maxTextureSize = context->caps()->maxTextureSize(); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 159 | sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
| 160 | sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 161 | |
| 162 | SkRect srcRect(rect); |
| 163 | scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
reed | d02cf26 | 2014-11-18 18:06:45 -0800 | [diff] [blame] | 164 | srcRect.roundOut(&srcRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 165 | scale_rect(&srcRect, static_cast<float>(scaleFactorX), |
| 166 | static_cast<float>(scaleFactorY)); |
| 167 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 168 | // setup new clip |
| 169 | GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height())); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 170 | |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 171 | SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 172 | kRGBA_8888_GrPixelConfig == srcTexture->config() || |
| 173 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 174 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 175 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 176 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 177 | desc.fWidth = SkScalarFloorToInt(srcRect.width()); |
| 178 | desc.fHeight = SkScalarFloorToInt(srcRect.height()); |
| 179 | desc.fConfig = srcTexture->config(); |
| 180 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 181 | GrTexture* dstTexture; |
| 182 | GrTexture* tempTexture; |
| 183 | SkAutoTUnref<GrTexture> temp1, temp2; |
| 184 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 185 | temp1.reset(context->textureProvider()->refScratchTexture( |
| 186 | desc, GrTextureProvider::kApprox_ScratchTexMatch)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 187 | dstTexture = temp1.get(); |
| 188 | if (canClobberSrc) { |
| 189 | tempTexture = srcTexture; |
| 190 | } else { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 191 | temp2.reset(context->textureProvider()->refScratchTexture( |
| 192 | desc, GrTextureProvider::kApprox_ScratchTexMatch)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 193 | tempTexture = temp2.get(); |
| 194 | } |
| 195 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 196 | if (NULL == dstTexture || NULL == tempTexture) { |
| 197 | return NULL; |
| 198 | } |
| 199 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 200 | GrDrawContext* drawContext = context->drawContext(); |
| 201 | if (!drawContext) { |
| 202 | return NULL; |
| 203 | } |
| 204 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 205 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 206 | GrPaint paint; |
| 207 | SkMatrix matrix; |
| 208 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 209 | SkRect dstRect(srcRect); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 210 | if (cropToRect && i == 1) { |
| 211 | dstRect.offset(-dstRect.fLeft, -dstRect.fTop); |
| 212 | SkRect domain; |
| 213 | matrix.mapRect(&domain, rect); |
| 214 | domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f, |
| 215 | i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f); |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 216 | SkAutoTUnref<GrFragmentProcessor> fp( GrTextureDomainEffect::Create( |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 217 | srcTexture, |
| 218 | matrix, |
| 219 | domain, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 220 | GrTextureDomain::kDecal_Mode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 221 | GrTextureParams::kBilerp_FilterMode)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 222 | paint.addColorProcessor(fp); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 223 | } else { |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 224 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 225 | paint.addColorTextureProcessor(srcTexture, matrix, params); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 226 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 227 | scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, |
| 228 | i < scaleFactorY ? 0.5f : 1.0f); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 229 | drawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, SkMatrix::I(), |
| 230 | dstRect, srcRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 231 | srcRect = dstRect; |
| 232 | srcTexture = dstTexture; |
| 233 | SkTSwap(dstTexture, tempTexture); |
| 234 | } |
| 235 | |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 236 | const SkIRect srcIRect = srcRect.roundOut(); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 237 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 238 | // For really small blurs(Certainly no wider than 5x5 on desktop gpus) it is faster to just |
| 239 | // launch a single non separable kernel vs two launches |
| 240 | if (sigmaX > 0.0f && sigmaY > 0 && |
| 241 | (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
| 242 | // We shouldn't be scaling because this is a small size blur |
| 243 | SkASSERT((scaleFactorX == scaleFactorY) == 1); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 244 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 245 | convolve_gaussian_2d(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 246 | srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 247 | srcTexture = dstTexture; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 248 | srcRect = dstRect; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 249 | SkTSwap(dstTexture, tempTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 250 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 251 | } else { |
| 252 | if (sigmaX > 0.0f) { |
| 253 | if (scaleFactorX > 1) { |
| 254 | // Clear out a radius to the right of the srcRect to prevent the |
| 255 | // X convolution from reading garbage. |
| 256 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 257 | radiusX, srcIRect.height()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 258 | drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 259 | } |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 260 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 261 | convolve_gaussian(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 262 | srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, |
| 263 | cropToRect); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 264 | srcTexture = dstTexture; |
| 265 | srcRect = dstRect; |
| 266 | SkTSwap(dstTexture, tempTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 267 | } |
| 268 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 269 | if (sigmaY > 0.0f) { |
| 270 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
| 271 | // Clear out a radius below the srcRect to prevent the Y |
| 272 | // convolution from reading garbage. |
| 273 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 274 | srcIRect.width(), radiusY); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 275 | drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 276 | } |
| 277 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 278 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 279 | convolve_gaussian(drawContext, dstTexture->asRenderTarget(), clip, srcRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 280 | dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, |
| 281 | cropToRect); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 282 | srcTexture = dstTexture; |
| 283 | srcRect = dstRect; |
| 284 | SkTSwap(dstTexture, tempTexture); |
| 285 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
| 289 | // Clear one pixel to the right and below, to accommodate bilinear |
| 290 | // upsampling. |
| 291 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 292 | srcIRect.width() + 1, 1); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 293 | drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 294 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 295 | 1, srcIRect.height()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 296 | drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 297 | SkMatrix matrix; |
| 298 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 299 | |
| 300 | GrPaint paint; |
| 301 | // FIXME: this should be mitchell, not bilinear. |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 302 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 303 | paint.addColorTextureProcessor(srcTexture, matrix, params); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 304 | |
| 305 | SkRect dstRect(srcRect); |
| 306 | scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 307 | drawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, |
| 308 | SkMatrix::I(), dstRect, srcRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 309 | srcRect = dstRect; |
| 310 | srcTexture = dstTexture; |
| 311 | SkTSwap(dstTexture, tempTexture); |
| 312 | } |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 313 | return SkRef(srcTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 314 | } |
| 315 | #endif |
| 316 | |
robertphillips@google.com | cce4102 | 2013-07-15 15:47:10 +0000 | [diff] [blame] | 317 | } |