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" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 17 | #include "GrRenderTargetContext.h" |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 18 | #include "GrRenderTargetContextPriv.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 19 | #include "GrFixedClip.h" |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 20 | |
| 21 | #define MAX_BLUR_SIGMA 4.0f |
| 22 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 23 | static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) { |
| 24 | rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale)); |
| 25 | rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale)); |
| 26 | rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale)); |
| 27 | rect->fBottom = SkScalarCeilToInt(SkScalarMul(rect->fBottom, yScale)); |
| 28 | } |
| 29 | |
| 30 | static void scale_irect(SkIRect* rect, int xScale, int yScale) { |
| 31 | rect->fLeft *= xScale; |
| 32 | rect->fTop *= yScale; |
| 33 | rect->fRight *= xScale; |
| 34 | rect->fBottom *= yScale; |
| 35 | } |
| 36 | |
| 37 | #ifdef SK_DEBUG |
| 38 | static inline int is_even(int x) { return !(x & 1); } |
| 39 | #endif |
| 40 | |
| 41 | static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) { |
| 42 | if (xAxis) { |
| 43 | SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight)); |
| 44 | rect->fLeft /= 2; |
| 45 | rect->fRight /= 2; |
| 46 | } |
| 47 | if (yAxis) { |
| 48 | SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom)); |
| 49 | rect->fTop /= 2; |
| 50 | rect->fBottom /= 2; |
| 51 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 52 | } |
| 53 | |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 54 | 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] | 55 | *scaleFactor = 1; |
| 56 | while (sigma > MAX_BLUR_SIGMA) { |
| 57 | *scaleFactor *= 2; |
| 58 | sigma *= 0.5f; |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 59 | if (*scaleFactor > maxTextureSize) { |
| 60 | *scaleFactor = maxTextureSize; |
| 61 | sigma = MAX_BLUR_SIGMA; |
| 62 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 63 | } |
| 64 | *radius = static_cast<int>(ceilf(sigma * 3.0f)); |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 65 | SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 66 | return sigma; |
| 67 | } |
| 68 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 69 | static void convolve_gaussian_1d(GrRenderTargetContext* renderTargetContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 70 | const GrClip& clip, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 71 | const SkIRect& dstRect, |
| 72 | const SkIPoint& srcOffset, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 73 | GrTexture* texture, |
| 74 | Gr1DKernelEffect::Direction direction, |
| 75 | int radius, |
| 76 | float sigma, |
| 77 | bool useBounds, |
| 78 | float bounds[2]) { |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 79 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 80 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 81 | sk_sp<GrFragmentProcessor> conv(GrConvolutionEffect::MakeGaussian( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 82 | texture, direction, radius, sigma, useBounds, bounds)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 83 | paint.addColorFragmentProcessor(std::move(conv)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 84 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 85 | SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), |
| 86 | -SkIntToScalar(srcOffset.y())); |
Brian Salomon | 3944484 | 2016-12-09 14:06:38 -0500 | [diff] [blame^] | 87 | renderTargetContext->fillRectWithLocalMatrix(clip, paint, GrAA::kNo, SkMatrix::I(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 88 | SkRect::Make(dstRect), localMatrix); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 91 | static void convolve_gaussian_2d(GrRenderTargetContext* renderTargetContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 92 | const GrClip& clip, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 93 | const SkIRect& dstRect, |
| 94 | const SkIPoint& srcOffset, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 95 | GrTexture* texture, |
| 96 | int radiusX, |
| 97 | int radiusY, |
| 98 | SkScalar sigmaX, |
| 99 | SkScalar sigmaY, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 100 | const SkIRect* srcBounds) { |
| 101 | SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()), |
| 102 | -SkIntToScalar(srcOffset.y())); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 103 | SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); |
| 104 | SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); |
| 105 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 106 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 107 | SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect(); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 108 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 109 | sk_sp<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::MakeGaussian( |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 110 | texture, bounds, size, 1.0, 0.0, kernelOffset, |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 111 | srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 112 | true, sigmaX, sigmaY)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 113 | paint.addColorFragmentProcessor(std::move(conv)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 114 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 3944484 | 2016-12-09 14:06:38 -0500 | [diff] [blame^] | 115 | renderTargetContext->fillRectWithLocalMatrix(clip, paint, GrAA::kNo, SkMatrix::I(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 116 | SkRect::Make(dstRect), localMatrix); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 119 | static void convolve_gaussian(GrRenderTargetContext* renderTargetContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 120 | const GrClip& clip, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 121 | const SkIRect& srcRect, |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 122 | GrTexture* texture, |
| 123 | Gr1DKernelEffect::Direction direction, |
| 124 | int radius, |
| 125 | float sigma, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 126 | const SkIRect* srcBounds, |
| 127 | const SkIPoint& srcOffset) { |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 128 | float bounds[2] = { 0.0f, 1.0f }; |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 129 | SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 130 | if (!srcBounds) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 131 | convolve_gaussian_1d(renderTargetContext, clip, dstRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 132 | direction, radius, sigma, false, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 133 | return; |
| 134 | } |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 135 | SkIRect midRect = *srcBounds, leftRect, rightRect; |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 136 | midRect.offset(srcOffset); |
| 137 | SkIRect topRect, bottomRect; |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 138 | if (direction == Gr1DKernelEffect::kX_Direction) { |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 139 | bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width(); |
| 140 | bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width(); |
| 141 | topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top()); |
| 142 | bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom()); |
| 143 | midRect.inset(radius, 0); |
| 144 | leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 145 | rightRect = |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 146 | SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 147 | dstRect.fTop = midRect.top(); |
| 148 | dstRect.fBottom = midRect.bottom(); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 149 | } else { |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 150 | bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height(); |
| 151 | bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height(); |
| 152 | topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom()); |
| 153 | bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom()); |
| 154 | midRect.inset(0, radius); |
| 155 | leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 156 | rightRect = |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 157 | SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 158 | dstRect.fLeft = midRect.left(); |
| 159 | dstRect.fRight = midRect.right(); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 160 | } |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 161 | if (!topRect.isEmpty()) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 162 | renderTargetContext->clear(&topRect, 0, false); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (!bottomRect.isEmpty()) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 166 | renderTargetContext->clear(&bottomRect, 0, false); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 167 | } |
| 168 | if (midRect.isEmpty()) { |
| 169 | // Blur radius covers srcBounds; use bounds over entire draw |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 170 | convolve_gaussian_1d(renderTargetContext, clip, dstRect, srcOffset, texture, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 171 | direction, radius, sigma, true, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 172 | } else { |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 173 | // Draw right and left margins with bounds; middle without. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 174 | convolve_gaussian_1d(renderTargetContext, clip, leftRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 175 | direction, radius, sigma, true, bounds); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 176 | convolve_gaussian_1d(renderTargetContext, clip, rightRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 177 | direction, radius, sigma, true, bounds); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 178 | convolve_gaussian_1d(renderTargetContext, clip, midRect, srcOffset, texture, |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 179 | direction, radius, sigma, false, bounds); |
senorblanco@chromium.org | d71732a | 2013-07-30 21:11:05 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 183 | namespace SkGpuBlurUtils { |
| 184 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 185 | sk_sp<GrRenderTargetContext> GaussianBlur(GrContext* context, |
| 186 | GrTexture* origSrc, |
| 187 | sk_sp<SkColorSpace> colorSpace, |
| 188 | const SkIRect& dstBounds, |
| 189 | const SkIRect* srcBounds, |
| 190 | float sigmaX, |
| 191 | float sigmaY, |
| 192 | SkBackingFit fit) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 193 | SkASSERT(context); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 194 | SkIRect clearRect; |
| 195 | int scaleFactorX, radiusX; |
| 196 | int scaleFactorY, radiusY; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 197 | int maxTextureSize = context->caps()->maxTextureSize(); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 198 | sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
| 199 | sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 200 | SkASSERT(sigmaX || sigmaY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 201 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 202 | SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y()); |
| 203 | SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height()); |
| 204 | SkIRect localSrcBounds; |
| 205 | SkIRect srcRect; |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 206 | if (srcBounds) { |
| 207 | srcRect = localSrcBounds = *srcBounds; |
| 208 | srcRect.offset(srcOffset); |
| 209 | srcBounds = &localSrcBounds; |
| 210 | } else { |
| 211 | srcRect = localDstBounds; |
| 212 | } |
| 213 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 214 | scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
| 215 | scale_irect(&srcRect, scaleFactorX, scaleFactorY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 216 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 217 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 218 | GrFixedClip clip(localDstBounds); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 219 | |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 220 | sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc)); |
| 221 | |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 222 | SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 223 | kRGBA_8888_GrPixelConfig == srcTexture->config() || |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 224 | kSRGBA_8888_GrPixelConfig == srcTexture->config() || |
| 225 | kSBGRA_8888_GrPixelConfig == srcTexture->config() || |
brianosman | 950fc6c | 2016-09-19 06:59:28 -0700 | [diff] [blame] | 226 | kRGBA_half_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 227 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 228 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 229 | const int width = dstBounds.width(); |
| 230 | const int height = dstBounds.height(); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 231 | const GrPixelConfig config = srcTexture->config(); |
| 232 | |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 233 | sk_sp<GrRenderTargetContext> dstRenderTargetContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 234 | fit, width, height, config, colorSpace, 0, kDefault_GrSurfaceOrigin)); |
| 235 | if (!dstRenderTargetContext) { |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 236 | return nullptr; |
| 237 | } |
| 238 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 239 | // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just |
| 240 | // launch a single non separable kernel vs two launches |
| 241 | if (sigmaX > 0.0f && sigmaY > 0.0f && |
| 242 | (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
| 243 | // We shouldn't be scaling because this is a small size blur |
| 244 | SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
| 245 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 246 | convolve_gaussian_2d(dstRenderTargetContext.get(), clip, localDstBounds, srcOffset, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 247 | srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 248 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 249 | return dstRenderTargetContext; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 250 | } |
| 251 | |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 252 | sk_sp<GrRenderTargetContext> tmpRenderTargetContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 253 | fit, width, height, config, colorSpace, 0, kDefault_GrSurfaceOrigin)); |
| 254 | if (!tmpRenderTargetContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 255 | return nullptr; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 258 | sk_sp<GrRenderTargetContext> srcRenderTargetContext; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 259 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 260 | SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); |
| 261 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 262 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 263 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 264 | paint.setGammaCorrect(dstRenderTargetContext->isGammaCorrect()); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 265 | // TODO: this matrix relies on the final instantiated size of the texture. This |
| 266 | // will have to be deferred for TextureProxys |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 267 | SkMatrix matrix; |
| 268 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 269 | SkIRect dstRect(srcRect); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 270 | if (srcBounds && i == 1) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 271 | SkRect domain; |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 272 | matrix.mapRect(&domain, SkRect::Make(*srcBounds)); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 273 | domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, |
| 274 | (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 275 | sk_sp<GrFragmentProcessor> fp(GrTextureDomainEffect::Make( |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 276 | srcTexture.get(), |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 277 | nullptr, |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 278 | matrix, |
| 279 | domain, |
| 280 | GrTextureDomain::kDecal_Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 281 | GrSamplerParams::kBilerp_FilterMode)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 282 | paint.addColorFragmentProcessor(std::move(fp)); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 283 | srcRect.offset(-srcOffset); |
| 284 | srcOffset.set(0, 0); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 285 | } else { |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 286 | GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode); |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 287 | paint.addColorTextureProcessor(srcTexture.get(), nullptr, matrix, params); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 288 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 289 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 290 | shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 291 | |
Brian Salomon | 3944484 | 2016-12-09 14:06:38 -0500 | [diff] [blame^] | 292 | dstRenderTargetContext->fillRectToRect(clip, paint, GrAA::kNo, SkMatrix::I(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 293 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 294 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 295 | srcRenderTargetContext = dstRenderTargetContext; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 296 | srcRect = dstRect; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 297 | srcTexture = srcRenderTargetContext->asTexture(); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 298 | if (!srcTexture) { |
| 299 | return nullptr; |
| 300 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 301 | dstRenderTargetContext.swap(tmpRenderTargetContext); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 302 | localSrcBounds = srcRect; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 303 | } |
| 304 | |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 305 | srcRect = localDstBounds; |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 306 | scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 307 | if (sigmaX > 0.0f) { |
| 308 | if (scaleFactorX > 1) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 309 | SkASSERT(srcRenderTargetContext); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 310 | |
| 311 | // Clear out a radius to the right of the srcRect to prevent the |
| 312 | // X convolution from reading garbage. |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 313 | clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, |
| 314 | radiusX, srcRect.height()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 315 | srcRenderTargetContext->priv().absClear(&clearRect, 0x0); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 316 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 317 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 318 | convolve_gaussian(dstRenderTargetContext.get(), clip, srcRect, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 319 | srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 320 | srcBounds, srcOffset); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 321 | srcRenderTargetContext = dstRenderTargetContext; |
| 322 | srcTexture = srcRenderTargetContext->asTexture(); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 323 | if (!srcTexture) { |
| 324 | return nullptr; |
| 325 | } |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 326 | srcRect.offsetTo(0, 0); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 327 | dstRenderTargetContext.swap(tmpRenderTargetContext); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 328 | localSrcBounds = srcRect; |
| 329 | srcOffset.set(0, 0); |
| 330 | } |
| 331 | |
| 332 | if (sigmaY > 0.0f) { |
| 333 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 334 | SkASSERT(srcRenderTargetContext); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 335 | |
| 336 | // Clear out a radius below the srcRect to prevent the Y |
| 337 | // convolution from reading garbage. |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 338 | clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, |
| 339 | srcRect.width(), radiusY); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 340 | srcRenderTargetContext->priv().absClear(&clearRect, 0x0); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 343 | convolve_gaussian(dstRenderTargetContext.get(), clip, srcRect, |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 344 | srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 345 | srcBounds, srcOffset); |
robertphillips | 56a85e6 | 2016-05-06 07:17:49 -0700 | [diff] [blame] | 346 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 347 | srcRenderTargetContext = dstRenderTargetContext; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 348 | srcRect.offsetTo(0, 0); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 349 | dstRenderTargetContext.swap(tmpRenderTargetContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 350 | } |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame] | 351 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 352 | SkASSERT(srcRenderTargetContext); |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 353 | srcTexture = nullptr; // we don't use this from here on out |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 354 | |
| 355 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
robertphillips | 04c84af | 2016-05-12 11:13:00 -0700 | [diff] [blame] | 356 | // Clear one pixel to the right and below, to accommodate bilinear upsampling. |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 357 | // TODO: it seems like we should actually be clamping here rather than darkening |
| 358 | // the bottom right edges. |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 359 | clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 360 | srcRenderTargetContext->priv().absClear(&clearRect, 0x0); |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 361 | clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 362 | srcRenderTargetContext->priv().absClear(&clearRect, 0x0); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 363 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 364 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 365 | paint.setGammaCorrect(dstRenderTargetContext->isGammaCorrect()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 366 | // FIXME: this should be mitchell, not bilinear. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 367 | GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 368 | sk_sp<GrTexture> tex(srcRenderTargetContext->asTexture()); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 369 | if (!tex) { |
| 370 | return nullptr; |
| 371 | } |
| 372 | |
| 373 | // TODO: this matrix relies on the final instantiated size of the texture. This |
| 374 | // will have to be deferred for TextureProxys |
| 375 | SkMatrix matrix; |
| 376 | matrix.setIDiv(tex->width(), tex->height()); |
| 377 | |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 378 | paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 379 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 380 | |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 381 | SkIRect dstRect(srcRect); |
| 382 | scale_irect(&dstRect, scaleFactorX, scaleFactorY); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 383 | |
Brian Salomon | 3944484 | 2016-12-09 14:06:38 -0500 | [diff] [blame^] | 384 | dstRenderTargetContext->fillRectToRect(clip, paint, GrAA::kNo, SkMatrix::I(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 385 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 386 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 387 | srcRenderTargetContext = dstRenderTargetContext; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 388 | srcRect = dstRect; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 389 | dstRenderTargetContext.swap(tmpRenderTargetContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 390 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 391 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 392 | return srcRenderTargetContext; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 393 | } |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 394 | |
robertphillips@google.com | cce4102 | 2013-07-15 15:47:10 +0000 | [diff] [blame] | 395 | } |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 396 | |
| 397 | #endif |
| 398 | |