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, |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 149 | float sigmaY) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 150 | SkASSERT(context); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 151 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 152 | SkIRect clearRect; |
| 153 | int scaleFactorX, radiusX; |
| 154 | int scaleFactorY, radiusY; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 155 | int maxTextureSize = context->caps()->maxTextureSize(); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 156 | sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
| 157 | sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 158 | |
| 159 | SkRect srcRect(rect); |
| 160 | scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
reed | d02cf26 | 2014-11-18 18:06:45 -0800 | [diff] [blame] | 161 | srcRect.roundOut(&srcRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 162 | scale_rect(&srcRect, static_cast<float>(scaleFactorX), |
| 163 | static_cast<float>(scaleFactorY)); |
| 164 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 165 | // setup new clip |
| 166 | GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height())); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 167 | |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 168 | SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 169 | kRGBA_8888_GrPixelConfig == srcTexture->config() || |
| 170 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 171 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 172 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 173 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 174 | desc.fWidth = SkScalarFloorToInt(srcRect.width()); |
| 175 | desc.fHeight = SkScalarFloorToInt(srcRect.height()); |
| 176 | desc.fConfig = srcTexture->config(); |
| 177 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 178 | GrTexture* dstTexture; |
| 179 | GrTexture* tempTexture; |
| 180 | SkAutoTUnref<GrTexture> temp1, temp2; |
| 181 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 182 | temp1.reset(context->textureProvider()->createApproxTexture(desc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 183 | dstTexture = temp1.get(); |
| 184 | if (canClobberSrc) { |
| 185 | tempTexture = srcTexture; |
| 186 | } else { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 187 | temp2.reset(context->textureProvider()->createApproxTexture(desc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 188 | tempTexture = temp2.get(); |
| 189 | } |
| 190 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 191 | if (nullptr == dstTexture || nullptr == tempTexture) { |
| 192 | return nullptr; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 193 | } |
| 194 | |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 195 | SkAutoTUnref<GrDrawContext> srcDrawContext; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 196 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 197 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 198 | GrPaint paint; |
| 199 | SkMatrix matrix; |
| 200 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 201 | SkRect dstRect(srcRect); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 202 | if (cropToRect && i == 1) { |
| 203 | dstRect.offset(-dstRect.fLeft, -dstRect.fTop); |
| 204 | SkRect domain; |
| 205 | matrix.mapRect(&domain, rect); |
| 206 | domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f, |
| 207 | i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 208 | SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create( |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 209 | srcTexture, |
| 210 | matrix, |
| 211 | domain, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 212 | GrTextureDomain::kDecal_Mode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 213 | GrTextureParams::kBilerp_FilterMode)); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 214 | paint.addColorFragmentProcessor(fp); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 215 | } else { |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 216 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 217 | paint.addColorTextureProcessor(srcTexture, matrix, params); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 218 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 219 | scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, |
| 220 | i < scaleFactorY ? 0.5f : 1.0f); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 221 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 222 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 223 | context->drawContext(dstTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 224 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 225 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 226 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 227 | dstDrawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 228 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 229 | srcDrawContext.swap(dstDrawContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 230 | srcRect = dstRect; |
| 231 | srcTexture = dstTexture; |
| 232 | SkTSwap(dstTexture, tempTexture); |
| 233 | } |
| 234 | |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 235 | const SkIRect srcIRect = srcRect.roundOut(); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 236 | |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 237 | // 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] | 238 | // launch a single non separable kernel vs two launches |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 239 | if (sigmaX > 0.0f && sigmaY > 0.0f && |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 240 | (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
| 241 | // We shouldn't be scaling because this is a small size blur |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 242 | SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 243 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 244 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 245 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 246 | context->drawContext(dstTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 247 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 249 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 250 | convolve_gaussian_2d(dstDrawContext, clip, srcRect, dstRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 251 | srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 252 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 253 | srcDrawContext.swap(dstDrawContext); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 254 | srcRect = dstRect; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 255 | srcTexture = dstTexture; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 256 | SkTSwap(dstTexture, tempTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 257 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 258 | } else { |
| 259 | if (sigmaX > 0.0f) { |
| 260 | if (scaleFactorX > 1) { |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 261 | // TODO: if we pass in the source draw context we don't need this here |
| 262 | if (!srcDrawContext) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 263 | srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 264 | if (!srcDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 265 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 269 | // Clear out a radius to the right of the srcRect to prevent the |
| 270 | // X convolution from reading garbage. |
| 271 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 272 | radiusX, srcIRect.height()); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 273 | srcDrawContext->clear(&clearRect, 0x0, false); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 274 | } |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 275 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 276 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 277 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 278 | context->drawContext(dstTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 279 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 280 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 281 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 282 | convolve_gaussian(dstDrawContext, clip, srcRect, dstRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 283 | srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, |
| 284 | cropToRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 285 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 286 | srcDrawContext.swap(dstDrawContext); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 287 | srcTexture = dstTexture; |
| 288 | srcRect = dstRect; |
| 289 | SkTSwap(dstTexture, tempTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 290 | } |
| 291 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 292 | if (sigmaY > 0.0f) { |
| 293 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 294 | // TODO: if we pass in the source draw context we don't need this here |
| 295 | if (!srcDrawContext) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 296 | srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 297 | if (!srcDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 302 | // Clear out a radius below the srcRect to prevent the Y |
| 303 | // convolution from reading garbage. |
| 304 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 305 | srcIRect.width(), radiusY); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 306 | srcDrawContext->clear(&clearRect, 0x0, false); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 307 | } |
| 308 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 309 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 310 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 311 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 312 | context->drawContext(dstTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 313 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 314 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 315 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 316 | convolve_gaussian(dstDrawContext, clip, srcRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 317 | dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, |
| 318 | cropToRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 319 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 320 | srcDrawContext.swap(dstDrawContext); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 321 | srcTexture = dstTexture; |
| 322 | srcRect = dstRect; |
| 323 | SkTSwap(dstTexture, tempTexture); |
| 324 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 328 | SkASSERT(srcDrawContext); |
| 329 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 330 | // Clear one pixel to the right and below, to accommodate bilinear |
| 331 | // upsampling. |
| 332 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 333 | srcIRect.width() + 1, 1); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 334 | srcDrawContext->clear(&clearRect, 0x0, false); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 335 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 336 | 1, srcIRect.height()); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 337 | srcDrawContext->clear(&clearRect, 0x0, false); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 338 | SkMatrix matrix; |
| 339 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 340 | |
| 341 | GrPaint paint; |
| 342 | // FIXME: this should be mitchell, not bilinear. |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 343 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 344 | paint.addColorTextureProcessor(srcTexture, matrix, params); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 345 | |
| 346 | SkRect dstRect(srcRect); |
| 347 | scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 348 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 349 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 350 | context->drawContext(dstTexture->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 351 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 352 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 353 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame^] | 354 | dstDrawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 355 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 356 | srcDrawContext.swap(dstDrawContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 357 | srcRect = dstRect; |
| 358 | srcTexture = dstTexture; |
| 359 | SkTSwap(dstTexture, tempTexture); |
| 360 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 361 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 362 | return SkRef(srcTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 363 | } |
| 364 | #endif |
| 365 | |
robertphillips@google.com | cce4102 | 2013-07-15 15:47:10 +0000 | [diff] [blame] | 366 | } |