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