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