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, |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 168 | bool gammaCorrect, |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 169 | const SkRect& dstBounds, |
| 170 | const SkRect* srcBounds, |
skia.committer@gmail.com | 977409a | 2013-07-16 07:00:56 +0000 | [diff] [blame] | 171 | float sigmaX, |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 172 | float sigmaY) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 173 | SkASSERT(context); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 174 | SkIRect clearRect; |
| 175 | int scaleFactorX, radiusX; |
| 176 | int scaleFactorY, radiusY; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 177 | int maxTextureSize = context->caps()->maxTextureSize(); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 178 | sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
| 179 | sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 180 | |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 181 | SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y()); |
| 182 | SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height()); |
| 183 | SkRect localSrcBounds; |
| 184 | SkRect srcRect; |
| 185 | if (srcBounds) { |
| 186 | srcRect = localSrcBounds = *srcBounds; |
| 187 | srcRect.offset(srcOffset); |
| 188 | srcBounds = &localSrcBounds; |
| 189 | } else { |
| 190 | srcRect = localDstBounds; |
| 191 | } |
| 192 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 193 | scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
reed | d02cf26 | 2014-11-18 18:06:45 -0800 | [diff] [blame] | 194 | srcRect.roundOut(&srcRect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 195 | scale_rect(&srcRect, static_cast<float>(scaleFactorX), |
| 196 | static_cast<float>(scaleFactorY)); |
| 197 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 198 | // setup new clip |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 199 | GrClip clip(localDstBounds); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 200 | |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 201 | SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 202 | kRGBA_8888_GrPixelConfig == srcTexture->config() || |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 203 | kSRGBA_8888_GrPixelConfig == srcTexture->config() || |
| 204 | kSBGRA_8888_GrPixelConfig == srcTexture->config() || |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 205 | kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 206 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 207 | const int width = SkScalarFloorToInt(dstBounds.width()); |
| 208 | const int height = SkScalarFloorToInt(dstBounds.height()); |
| 209 | const GrPixelConfig config = srcTexture->config(); |
| 210 | |
| 211 | const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0, |
| 212 | SkSurfaceProps::kLegacyFontHost_InitType); |
| 213 | |
| 214 | // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just |
| 215 | // launch a single non separable kernel vs two launches |
| 216 | if (sigmaX > 0.0f && sigmaY > 0.0f && |
| 217 | (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
| 218 | // We shouldn't be scaling because this is a small size blur |
| 219 | SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
| 220 | |
| 221 | sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox, |
| 222 | width, height, config, |
| 223 | 0, kDefault_GrSurfaceOrigin, |
| 224 | &props)); |
| 225 | if (!dstDrawContext) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset, |
| 229 | srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBounds); |
| 230 | |
| 231 | return dstDrawContext->asTexture().release(); |
| 232 | } |
| 233 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 234 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 235 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 236 | desc.fWidth = width; |
| 237 | desc.fHeight = height; |
| 238 | desc.fConfig = config; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 239 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 240 | GrTexture* dstTexture; |
| 241 | GrTexture* tempTexture; |
| 242 | SkAutoTUnref<GrTexture> temp1, temp2; |
| 243 | |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 244 | temp1.reset(context->textureProvider()->createApproxTexture(desc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 245 | dstTexture = temp1.get(); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 246 | temp2.reset(context->textureProvider()->createApproxTexture(desc)); |
| 247 | tempTexture = temp2.get(); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 248 | |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 249 | if (!dstTexture || !tempTexture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 250 | return nullptr; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 251 | } |
| 252 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 253 | sk_sp<GrDrawContext> srcDrawContext; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 254 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 255 | for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 256 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 257 | paint.setGammaCorrect(gammaCorrect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 258 | SkMatrix matrix; |
| 259 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 260 | SkRect dstRect(srcRect); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 261 | if (srcBounds && i == 1) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 262 | SkRect domain; |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 263 | matrix.mapRect(&domain, *srcBounds); |
| 264 | domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, |
| 265 | (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 266 | sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create( |
| 267 | srcTexture, |
| 268 | matrix, |
| 269 | domain, |
| 270 | GrTextureDomain::kDecal_Mode, |
| 271 | GrTextureParams::kBilerp_FilterMode)); |
| 272 | paint.addColorFragmentProcessor(fp.get()); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 273 | srcRect.offset(-srcOffset); |
| 274 | srcOffset.set(0, 0); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 275 | } else { |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 276 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 277 | paint.addColorTextureProcessor(srcTexture, matrix, params); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 278 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 279 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 280 | scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, |
| 281 | i < scaleFactorY ? 0.5f : 1.0f); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 282 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 283 | sk_sp<GrDrawContext> dstDrawContext( |
| 284 | context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()))); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 285 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 287 | } |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 288 | dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 289 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 290 | srcDrawContext.swap(dstDrawContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 291 | srcRect = dstRect; |
| 292 | srcTexture = dstTexture; |
| 293 | SkTSwap(dstTexture, tempTexture); |
senorblanco | 07d56b1 | 2015-11-10 07:32:37 -0800 | [diff] [blame] | 294 | localSrcBounds = srcRect; |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 295 | } |
| 296 | |
senorblanco | c834ab1 | 2015-12-17 08:10:17 -0800 | [diff] [blame] | 297 | srcRect = localDstBounds; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 298 | |
| 299 | scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
| 300 | srcRect.roundOut(&srcRect); |
| 301 | SkIRect srcIRect = srcRect.roundOut(); |
| 302 | if (sigmaX > 0.0f) { |
| 303 | if (scaleFactorX > 1) { |
| 304 | // TODO: if we pass in the source draw context we don't need this here |
| 305 | if (!srcDrawContext) { |
| 306 | srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRenderTarget())); |
| 307 | if (!srcDrawContext) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Clear out a radius to the right of the srcRect to prevent the |
| 313 | // X convolution from reading garbage. |
| 314 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 315 | radiusX, srcIRect.height()); |
| 316 | srcDrawContext->clear(&clearRect, 0x0, false); |
| 317 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 318 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 319 | sk_sp<GrDrawContext> dstDrawContext( |
| 320 | context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props)); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 321 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 322 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 323 | } |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 324 | convolve_gaussian(dstDrawContext.get(), clip, srcRect, |
| 325 | srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, |
| 326 | srcBounds, srcOffset); |
| 327 | srcDrawContext.swap(dstDrawContext); |
| 328 | srcTexture = dstTexture; |
| 329 | srcRect.offsetTo(0, 0); |
| 330 | SkTSwap(dstTexture, tempTexture); |
| 331 | localSrcBounds = srcRect; |
| 332 | srcOffset.set(0, 0); |
| 333 | } |
| 334 | |
| 335 | if (sigmaY > 0.0f) { |
| 336 | if (scaleFactorY > 1 || sigmaX > 0.0f) { |
| 337 | // TODO: if we pass in the source draw context we don't need this here |
| 338 | if (!srcDrawContext) { |
| 339 | srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRenderTarget())); |
| 340 | if (!srcDrawContext) { |
| 341 | return nullptr; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Clear out a radius below the srcRect to prevent the Y |
| 346 | // convolution from reading garbage. |
| 347 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 348 | srcIRect.width(), radiusY); |
| 349 | srcDrawContext->clear(&clearRect, 0x0, false); |
| 350 | } |
| 351 | |
| 352 | sk_sp<GrDrawContext> dstDrawContext( |
| 353 | context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props)); |
| 354 | if (!dstDrawContext) { |
| 355 | return nullptr; |
| 356 | } |
| 357 | convolve_gaussian(dstDrawContext.get(), clip, srcRect, |
| 358 | srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, |
| 359 | srcBounds, srcOffset); |
robertphillips | 56a85e6 | 2016-05-06 07:17:49 -0700 | [diff] [blame] | 360 | |
jvanverth | 67a58dc | 2016-05-06 13:05:09 -0700 | [diff] [blame] | 361 | srcDrawContext.swap(dstDrawContext); |
jvanverth | 67a58dc | 2016-05-06 13:05:09 -0700 | [diff] [blame] | 362 | srcTexture = dstTexture; |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 363 | srcRect.offsetTo(0, 0); |
jvanverth | 67a58dc | 2016-05-06 13:05:09 -0700 | [diff] [blame] | 364 | SkTSwap(dstTexture, tempTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 365 | } |
robertphillips | a8966a8 | 2016-05-09 06:45:37 -0700 | [diff] [blame^] | 366 | |
| 367 | srcIRect = srcRect.roundOut(); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 368 | |
| 369 | if (scaleFactorX > 1 || scaleFactorY > 1) { |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 370 | SkASSERT(srcDrawContext); |
| 371 | |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 372 | // Clear one pixel to the right and below, to accommodate bilinear |
| 373 | // upsampling. |
| 374 | clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 375 | srcIRect.width() + 1, 1); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 376 | srcDrawContext->clear(&clearRect, 0x0, false); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 377 | clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 378 | 1, srcIRect.height()); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 379 | srcDrawContext->clear(&clearRect, 0x0, false); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 380 | SkMatrix matrix; |
| 381 | matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 382 | |
| 383 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 384 | paint.setGammaCorrect(gammaCorrect); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 385 | // FIXME: this should be mitchell, not bilinear. |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 386 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 387 | paint.addColorTextureProcessor(srcTexture, matrix, params); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 388 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 389 | |
| 390 | SkRect dstRect(srcRect); |
| 391 | scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 392 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 393 | sk_sp<GrDrawContext> dstDrawContext( |
| 394 | context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()))); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 395 | if (!dstDrawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 396 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 397 | } |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 398 | dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 399 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 400 | srcDrawContext.swap(dstDrawContext); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 401 | srcRect = dstRect; |
| 402 | srcTexture = dstTexture; |
| 403 | SkTSwap(dstTexture, tempTexture); |
| 404 | } |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 405 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 406 | return SkRef(srcTexture); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 407 | } |
| 408 | #endif |
| 409 | |
robertphillips@google.com | cce4102 | 2013-07-15 15:47:10 +0000 | [diff] [blame] | 410 | } |