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