blob: ad268d6961ef69f919e9472f11b5347cc3854328 [file] [log] [blame]
robertphillips@google.com736dd032013-07-15 15:06:54 +00001/*
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"
joshualitt5acfea72014-08-11 13:55:34 -070014#include "effects/GrMatrixConvolutionEffect.h"
robertphillips@google.com736dd032013-07-15 15:06:54 +000015#include "GrContext.h"
bsalomon76228632015-05-29 08:02:10 -070016#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070017#include "GrDrawContext.h"
robertphillips@google.com736dd032013-07-15 15:06:54 +000018#endif
19
20namespace SkGpuBlurUtils {
21
22#if SK_SUPPORT_GPU
23
24#define MAX_BLUR_SIGMA 4.0f
25
26static void scale_rect(SkRect* rect, float xScale, float yScale) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000027 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.com736dd032013-07-15 15:06:54 +000031}
32
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +000033static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000034 *scaleFactor = 1;
35 while (sigma > MAX_BLUR_SIGMA) {
36 *scaleFactor *= 2;
37 sigma *= 0.5f;
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +000038 if (*scaleFactor > maxTextureSize) {
39 *scaleFactor = maxTextureSize;
40 sigma = MAX_BLUR_SIGMA;
41 }
robertphillips@google.com736dd032013-07-15 15:06:54 +000042 }
43 *radius = static_cast<int>(ceilf(sigma * 3.0f));
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +000044 SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius);
robertphillips@google.com736dd032013-07-15 15:06:54 +000045 return sigma;
46}
47
robertphillipsea461502015-05-26 11:38:03 -070048static void convolve_gaussian_1d(GrDrawContext* drawContext,
joshualitt25d9c152015-02-18 12:29:52 -080049 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080050 const GrClip& clip,
joshualitt5acfea72014-08-11 13:55:34 -070051 const SkRect& srcRect,
52 const SkRect& dstRect,
53 GrTexture* texture,
54 Gr1DKernelEffect::Direction direction,
55 int radius,
56 float sigma,
57 bool useBounds,
58 float bounds[2]) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000059 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -070060 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian(
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +000061 texture, direction, radius, sigma, useBounds, bounds));
joshualittb0a8a372014-09-23 09:50:21 -070062 paint.addColorProcessor(conv);
robertphillipsea461502015-05-26 11:38:03 -070063 drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +000064}
65
robertphillipsea461502015-05-26 11:38:03 -070066static void convolve_gaussian_2d(GrDrawContext* drawContext,
joshualitt25d9c152015-02-18 12:29:52 -080067 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080068 const GrClip& clip,
joshualitt5acfea72014-08-11 13:55:34 -070069 const SkRect& srcRect,
70 const SkRect& dstRect,
71 GrTexture* texture,
72 int radiusX,
73 int radiusY,
74 SkScalar sigmaX,
75 SkScalar sigmaY,
76 bool useBounds,
77 SkIRect bounds) {
78 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
79 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
80 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -070081 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
joshualitt5acfea72014-08-11 13:55:34 -070082 texture, bounds, size, 1.0, 0.0, kernelOffset,
83 useBounds ? GrTextureDomain::kClamp_Mode : GrTextureDomain::kIgnore_Mode,
84 true, sigmaX, sigmaY));
joshualittb0a8a372014-09-23 09:50:21 -070085 paint.addColorProcessor(conv);
robertphillipsea461502015-05-26 11:38:03 -070086 drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), dstRect, srcRect);
joshualitt5acfea72014-08-11 13:55:34 -070087}
88
robertphillipsea461502015-05-26 11:38:03 -070089static void convolve_gaussian(GrDrawContext* drawContext,
joshualitt25d9c152015-02-18 12:29:52 -080090 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080091 const GrClip& clip,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +000092 const SkRect& srcRect,
93 const SkRect& dstRect,
94 GrTexture* texture,
95 Gr1DKernelEffect::Direction direction,
96 int radius,
97 float sigma,
98 bool cropToSrcRect) {
99 float bounds[2] = { 0.0f, 1.0f };
100 if (!cropToSrcRect) {
robertphillipsea461502015-05-26 11:38:03 -0700101 convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700102 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000103 return;
104 }
105 SkRect lowerSrcRect = srcRect, lowerDstRect = dstRect;
106 SkRect middleSrcRect = srcRect, middleDstRect = dstRect;
107 SkRect upperSrcRect = srcRect, upperDstRect = dstRect;
108 SkScalar size;
109 SkScalar rad = SkIntToScalar(radius);
110 if (direction == Gr1DKernelEffect::kX_Direction) {
111 bounds[0] = SkScalarToFloat(srcRect.left()) / texture->width();
112 bounds[1] = SkScalarToFloat(srcRect.right()) / texture->width();
113 size = srcRect.width();
114 lowerSrcRect.fRight = srcRect.left() + rad;
115 lowerDstRect.fRight = dstRect.left() + rad;
116 upperSrcRect.fLeft = srcRect.right() - rad;
117 upperDstRect.fLeft = dstRect.right() - rad;
118 middleSrcRect.inset(rad, 0);
119 middleDstRect.inset(rad, 0);
120 } else {
121 bounds[0] = SkScalarToFloat(srcRect.top()) / texture->height();
122 bounds[1] = SkScalarToFloat(srcRect.bottom()) / texture->height();
123 size = srcRect.height();
124 lowerSrcRect.fBottom = srcRect.top() + rad;
125 lowerDstRect.fBottom = dstRect.top() + rad;
126 upperSrcRect.fTop = srcRect.bottom() - rad;
127 upperDstRect.fTop = dstRect.bottom() - rad;
128 middleSrcRect.inset(0, rad);
129 middleDstRect.inset(0, rad);
130 }
131 if (radius >= size * SK_ScalarHalf) {
132 // Blur radius covers srcRect; use bounds over entire draw
robertphillipsea461502015-05-26 11:38:03 -0700133 convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700134 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000135 } else {
136 // Draw upper and lower margins with bounds; middle without.
robertphillipsea461502015-05-26 11:38:03 -0700137 convolve_gaussian_1d(drawContext, rt, clip, lowerSrcRect, lowerDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700138 direction, radius, sigma, true, bounds);
robertphillipsea461502015-05-26 11:38:03 -0700139 convolve_gaussian_1d(drawContext, rt, clip, upperSrcRect, upperDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700140 direction, radius, sigma, true, bounds);
robertphillipsea461502015-05-26 11:38:03 -0700141 convolve_gaussian_1d(drawContext, rt, clip, middleSrcRect, middleDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700142 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000143 }
144}
145
robertphillips@google.com736dd032013-07-15 15:06:54 +0000146GrTexture* GaussianBlur(GrContext* context,
147 GrTexture* srcTexture,
148 bool canClobberSrc,
149 const SkRect& rect,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000150 bool cropToRect,
skia.committer@gmail.com977409a2013-07-16 07:00:56 +0000151 float sigmaX,
robertphillips@google.com736dd032013-07-15 15:06:54 +0000152 float sigmaY) {
bsalomon49f085d2014-09-05 13:34:00 -0700153 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000154
robertphillips@google.com736dd032013-07-15 15:06:54 +0000155 SkIRect clearRect;
156 int scaleFactorX, radiusX;
157 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700158 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000159 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
160 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000161
162 SkRect srcRect(rect);
163 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800164 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000165 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
166 static_cast<float>(scaleFactorY));
167
joshualitt570d2f82015-02-25 13:19:48 -0800168 // setup new clip
169 GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height()));
robertphillips@google.com736dd032013-07-15 15:06:54 +0000170
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000171 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000172 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
173 kAlpha_8_GrPixelConfig == srcTexture->config());
174
bsalomonf2703d82014-10-28 14:33:06 -0700175 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800176 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000177 desc.fWidth = SkScalarFloorToInt(srcRect.width());
178 desc.fHeight = SkScalarFloorToInt(srcRect.height());
179 desc.fConfig = srcTexture->config();
180
bsalomone3059732014-10-14 11:47:22 -0700181 GrTexture* dstTexture;
182 GrTexture* tempTexture;
183 SkAutoTUnref<GrTexture> temp1, temp2;
184
bsalomond309e7a2015-04-30 14:18:54 -0700185 temp1.reset(context->textureProvider()->refScratchTexture(
186 desc, GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700187 dstTexture = temp1.get();
188 if (canClobberSrc) {
189 tempTexture = srcTexture;
190 } else {
bsalomond309e7a2015-04-30 14:18:54 -0700191 temp2.reset(context->textureProvider()->refScratchTexture(
192 desc, GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700193 tempTexture = temp2.get();
194 }
195
robertphillips@google.com736dd032013-07-15 15:06:54 +0000196 if (NULL == dstTexture || NULL == tempTexture) {
197 return NULL;
198 }
199
robertphillipsea461502015-05-26 11:38:03 -0700200 GrDrawContext* drawContext = context->drawContext();
201 if (!drawContext) {
202 return NULL;
203 }
204
robertphillips@google.com736dd032013-07-15 15:06:54 +0000205 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
206 GrPaint paint;
207 SkMatrix matrix;
208 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000209 SkRect dstRect(srcRect);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000210 if (cropToRect && i == 1) {
211 dstRect.offset(-dstRect.fLeft, -dstRect.fTop);
212 SkRect domain;
213 matrix.mapRect(&domain, rect);
214 domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f,
215 i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bsalomon9f876a32014-12-09 10:51:07 -0800216 SkAutoTUnref<GrFragmentProcessor> fp( GrTextureDomainEffect::Create(
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000217 srcTexture,
218 matrix,
219 domain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000220 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000221 GrTextureParams::kBilerp_FilterMode));
joshualittb0a8a372014-09-23 09:50:21 -0700222 paint.addColorProcessor(fp);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000223 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000224 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700225 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000226 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000227 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
228 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsea461502015-05-26 11:38:03 -0700229 drawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, SkMatrix::I(),
230 dstRect, srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000231 srcRect = dstRect;
232 srcTexture = dstTexture;
233 SkTSwap(dstTexture, tempTexture);
234 }
235
reedb07a94f2014-11-19 05:03:18 -0800236 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000237
joshualitt5acfea72014-08-11 13:55:34 -0700238 // For really small blurs(Certainly no wider than 5x5 on desktop gpus) it is faster to just
239 // launch a single non separable kernel vs two launches
240 if (sigmaX > 0.0f && sigmaY > 0 &&
241 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
242 // We shouldn't be scaling because this is a small size blur
243 SkASSERT((scaleFactorX == scaleFactorY) == 1);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000244 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsea461502015-05-26 11:38:03 -0700245 convolve_gaussian_2d(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800246 srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000247 srcTexture = dstTexture;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000248 srcRect = dstRect;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000249 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000250
joshualitt5acfea72014-08-11 13:55:34 -0700251 } else {
252 if (sigmaX > 0.0f) {
253 if (scaleFactorX > 1) {
254 // Clear out a radius to the right of the srcRect to prevent the
255 // X convolution from reading garbage.
256 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
257 radiusX, srcIRect.height());
robertphillipsea461502015-05-26 11:38:03 -0700258 drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700259 }
joshualitt5acfea72014-08-11 13:55:34 -0700260 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsea461502015-05-26 11:38:03 -0700261 convolve_gaussian(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800262 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
263 cropToRect);
joshualitt5acfea72014-08-11 13:55:34 -0700264 srcTexture = dstTexture;
265 srcRect = dstRect;
266 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000267 }
268
joshualitt5acfea72014-08-11 13:55:34 -0700269 if (sigmaY > 0.0f) {
270 if (scaleFactorY > 1 || sigmaX > 0.0f) {
271 // Clear out a radius below the srcRect to prevent the Y
272 // convolution from reading garbage.
273 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
274 srcIRect.width(), radiusY);
robertphillipsea461502015-05-26 11:38:03 -0700275 drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700276 }
277
joshualitt5acfea72014-08-11 13:55:34 -0700278 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsea461502015-05-26 11:38:03 -0700279 convolve_gaussian(drawContext, dstTexture->asRenderTarget(), clip, srcRect,
joshualitt570d2f82015-02-25 13:19:48 -0800280 dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
281 cropToRect);
joshualitt5acfea72014-08-11 13:55:34 -0700282 srcTexture = dstTexture;
283 srcRect = dstRect;
284 SkTSwap(dstTexture, tempTexture);
285 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000286 }
287
288 if (scaleFactorX > 1 || scaleFactorY > 1) {
289 // Clear one pixel to the right and below, to accommodate bilinear
290 // upsampling.
291 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
292 srcIRect.width() + 1, 1);
robertphillipsea461502015-05-26 11:38:03 -0700293 drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000294 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
295 1, srcIRect.height());
robertphillipsea461502015-05-26 11:38:03 -0700296 drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000297 SkMatrix matrix;
298 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000299
300 GrPaint paint;
301 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000302 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700303 paint.addColorTextureProcessor(srcTexture, matrix, params);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000304
305 SkRect dstRect(srcRect);
306 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
robertphillipsea461502015-05-26 11:38:03 -0700307 drawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
308 SkMatrix::I(), dstRect, srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000309 srcRect = dstRect;
310 srcTexture = dstTexture;
311 SkTSwap(dstTexture, tempTexture);
312 }
bsalomone3059732014-10-14 11:47:22 -0700313 return SkRef(srcTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000314}
315#endif
316
robertphillips@google.comcce41022013-07-15 15:47:10 +0000317}