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