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