blob: c3681faca55f0b0a96d22a1835f882d2ff324070 [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,
joshualitt570d2f82015-02-25 13:19:48 -080049 const GrClip& clip,
joshualitt5acfea72014-08-11 13:55:34 -070050 const SkRect& dstRect,
senorblanco48343ee2015-11-03 05:07:43 -080051 const SkPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070052 GrTexture* texture,
53 Gr1DKernelEffect::Direction direction,
54 int radius,
55 float sigma,
56 bool useBounds,
57 float bounds[2]) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000058 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -070059 paint.setGammaCorrect(drawContext->isGammaCorrect());
joshualittb0a8a372014-09-23 09:50:21 -070060 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian(
bsalomon4a339522015-10-06 08:40:50 -070061 texture, direction, radius, sigma, useBounds, bounds));
bsalomonac856c92015-08-27 06:30:17 -070062 paint.addColorFragmentProcessor(conv);
egdanielc4b72722015-11-23 13:20:41 -080063 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
senorblancoc834ab12015-12-17 08:10:17 -080064 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y());
bsalomona2e69fc2015-11-05 10:41:43 -080065 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, localMatrix);
robertphillips@google.com736dd032013-07-15 15:06:54 +000066}
67
robertphillipsea461502015-05-26 11:38:03 -070068static void convolve_gaussian_2d(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080069 const GrClip& clip,
senorblancoc834ab12015-12-17 08:10:17 -080070 const SkRect& dstRect,
71 const SkPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070072 GrTexture* texture,
73 int radiusX,
74 int radiusY,
75 SkScalar sigmaX,
76 SkScalar sigmaY,
senorblanco07d56b12015-11-10 07:32:37 -080077 const SkRect* srcBounds) {
senorblancoc834ab12015-12-17 08:10:17 -080078 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y());
joshualitt5acfea72014-08-11 13:55:34 -070079 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
80 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
81 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -070082 paint.setGammaCorrect(drawContext->isGammaCorrect());
senorblanco07d56b12015-11-10 07:32:37 -080083 SkIRect bounds;
84 if (srcBounds) {
85 srcBounds->roundOut(&bounds);
86 } else {
87 bounds.setEmpty();
88 }
89
joshualittb0a8a372014-09-23 09:50:21 -070090 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
joshualitt5acfea72014-08-11 13:55:34 -070091 texture, bounds, size, 1.0, 0.0, kernelOffset,
senorblanco07d56b12015-11-10 07:32:37 -080092 srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode,
joshualitt5acfea72014-08-11 13:55:34 -070093 true, sigmaX, sigmaY));
bsalomonac856c92015-08-27 06:30:17 -070094 paint.addColorFragmentProcessor(conv);
egdanielc4b72722015-11-23 13:20:41 -080095 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -080096 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, localMatrix);
joshualitt5acfea72014-08-11 13:55:34 -070097}
98
robertphillipsea461502015-05-26 11:38:03 -070099static void convolve_gaussian(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -0800100 const GrClip& clip,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000101 const SkRect& srcRect,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000102 GrTexture* texture,
103 Gr1DKernelEffect::Direction direction,
104 int radius,
105 float sigma,
senorblanco07d56b12015-11-10 07:32:37 -0800106 const SkRect* srcBounds,
107 const SkPoint& srcOffset) {
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000108 float bounds[2] = { 0.0f, 1.0f };
senorblanco48343ee2015-11-03 05:07:43 -0800109 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
senorblanco07d56b12015-11-10 07:32:37 -0800110 if (!srcBounds) {
senorblanco48343ee2015-11-03 05:07:43 -0800111 convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700112 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000113 return;
114 }
senorblanco07d56b12015-11-10 07:32:37 -0800115 SkRect midRect = *srcBounds, leftRect, rightRect;
116 midRect.offset(srcOffset);
117 SkIRect topRect, bottomRect;
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000118 SkScalar rad = SkIntToScalar(radius);
119 if (direction == Gr1DKernelEffect::kX_Direction) {
senorblanco07d56b12015-11-10 07:32:37 -0800120 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.orgd71732a2013-07-30 21:11:05 +0000131 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800132 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.orgd71732a2013-07-30 21:11:05 +0000143 }
senorblanco07d56b12015-11-10 07:32:37 -0800144 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
senorblancoc834ab12015-12-17 08:10:17 -0800153 convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700154 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000155 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800156 // Draw right and left margins with bounds; middle without.
senorblancoc834ab12015-12-17 08:10:17 -0800157 convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700158 direction, radius, sigma, true, bounds);
senorblancoc834ab12015-12-17 08:10:17 -0800159 convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700160 direction, radius, sigma, true, bounds);
senorblancoc834ab12015-12-17 08:10:17 -0800161 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700162 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000163 }
164}
165
robertphillips@google.com736dd032013-07-15 15:06:54 +0000166GrTexture* GaussianBlur(GrContext* context,
167 GrTexture* srcTexture,
jvanverth67a58dc2016-05-06 13:05:09 -0700168 bool canClobberSrc,
brianosmanb461d342016-04-13 13:10:14 -0700169 bool gammaCorrect,
senorblanco07d56b12015-11-10 07:32:37 -0800170 const SkRect& dstBounds,
171 const SkRect* srcBounds,
skia.committer@gmail.com977409a2013-07-16 07:00:56 +0000172 float sigmaX,
reed4e23cda2016-01-11 10:56:59 -0800173 float sigmaY) {
bsalomon49f085d2014-09-05 13:34:00 -0700174 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000175 SkIRect clearRect;
176 int scaleFactorX, radiusX;
177 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700178 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000179 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
180 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000181
senorblanco07d56b12015-11-10 07:32:37 -0800182 SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y());
183 SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height());
184 SkRect localSrcBounds;
185 SkRect srcRect;
186 if (srcBounds) {
187 srcRect = localSrcBounds = *srcBounds;
188 srcRect.offset(srcOffset);
189 srcBounds = &localSrcBounds;
190 } else {
191 srcRect = localDstBounds;
192 }
193
robertphillips@google.com736dd032013-07-15 15:06:54 +0000194 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800195 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000196 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
197 static_cast<float>(scaleFactorY));
198
joshualitt570d2f82015-02-25 13:19:48 -0800199 // setup new clip
senorblanco07d56b12015-11-10 07:32:37 -0800200 GrClip clip(localDstBounds);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000201
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000202 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000203 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
brianosmana6359362016-03-21 06:55:37 -0700204 kSRGBA_8888_GrPixelConfig == srcTexture->config() ||
205 kSBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000206 kAlpha_8_GrPixelConfig == srcTexture->config());
207
bsalomonf2703d82014-10-28 14:33:06 -0700208 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800209 desc.fFlags = kRenderTarget_GrSurfaceFlag;
jvanverth67a58dc2016-05-06 13:05:09 -0700210 desc.fWidth = SkScalarFloorToInt(dstBounds.width());
211 desc.fHeight = SkScalarFloorToInt(dstBounds.height());
212 desc.fConfig = srcTexture->config();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000213
bsalomone3059732014-10-14 11:47:22 -0700214 GrTexture* dstTexture;
215 GrTexture* tempTexture;
216 SkAutoTUnref<GrTexture> temp1, temp2;
217
reed4e23cda2016-01-11 10:56:59 -0800218 temp1.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700219 dstTexture = temp1.get();
jvanverth67a58dc2016-05-06 13:05:09 -0700220 if (canClobberSrc) {
221 tempTexture = srcTexture;
222 } else {
223 temp2.reset(context->textureProvider()->createApproxTexture(desc));
224 tempTexture = temp2.get();
225 }
bsalomone3059732014-10-14 11:47:22 -0700226
jvanverth67a58dc2016-05-06 13:05:09 -0700227 if (nullptr == dstTexture || nullptr == tempTexture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700228 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000229 }
230
robertphillips6c7e3252016-04-27 10:47:51 -0700231 sk_sp<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700232
robertphillips@google.com736dd032013-07-15 15:06:54 +0000233 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
234 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -0700235 paint.setGammaCorrect(gammaCorrect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000236 SkMatrix matrix;
237 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000238 SkRect dstRect(srcRect);
senorblanco07d56b12015-11-10 07:32:37 -0800239 if (srcBounds && i == 1) {
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000240 SkRect domain;
senorblanco07d56b12015-11-10 07:32:37 -0800241 matrix.mapRect(&domain, *srcBounds);
242 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f,
243 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f);
jvanverth67a58dc2016-05-06 13:05:09 -0700244 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
245 srcTexture,
246 matrix,
247 domain,
248 GrTextureDomain::kDecal_Mode,
249 GrTextureParams::kBilerp_FilterMode));
250 paint.addColorFragmentProcessor(fp);
senorblanco07d56b12015-11-10 07:32:37 -0800251 srcRect.offset(-srcOffset);
252 srcOffset.set(0, 0);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000253 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000254 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700255 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000256 }
egdanielc4b72722015-11-23 13:20:41 -0800257 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000258 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
259 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700260
robertphillips6c7e3252016-04-27 10:47:51 -0700261 sk_sp<GrDrawContext> dstDrawContext(
262 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget())));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700263 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700264 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700265 }
bsalomona2e69fc2015-11-05 10:41:43 -0800266 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700267
bungeman77a53de2015-10-01 12:28:49 -0700268 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000269 srcRect = dstRect;
270 srcTexture = dstTexture;
271 SkTSwap(dstTexture, tempTexture);
senorblanco07d56b12015-11-10 07:32:37 -0800272 localSrcBounds = srcRect;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000273 }
274
jvanverth67a58dc2016-05-06 13:05:09 -0700275 SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0,
276 SkSurfaceProps::kLegacyFontHost_InitType);
277
278 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
279 // launch a single non separable kernel vs two launches
senorblancoc834ab12015-12-17 08:10:17 -0800280 srcRect = localDstBounds;
jvanverth67a58dc2016-05-06 13:05:09 -0700281 if (sigmaX > 0.0f && sigmaY > 0.0f &&
282 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
283 // We shouldn't be scaling because this is a small size blur
284 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700285
robertphillips6c7e3252016-04-27 10:47:51 -0700286 sk_sp<GrDrawContext> dstDrawContext(
287 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700288 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700289 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700290 }
jvanverth67a58dc2016-05-06 13:05:09 -0700291 convolve_gaussian_2d(dstDrawContext.get(), clip, srcRect, srcOffset,
292 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBounds);
robertphillips56a85e62016-05-06 07:17:49 -0700293
jvanverth67a58dc2016-05-06 13:05:09 -0700294 srcDrawContext.swap(dstDrawContext);
295 srcRect.offsetTo(0, 0);
296 srcTexture = dstTexture;
297 SkTSwap(dstTexture, tempTexture);
298
299 } else {
300 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
301 srcRect.roundOut(&srcRect);
302 const SkIRect srcIRect = srcRect.roundOut();
303 if (sigmaX > 0.0f) {
304 if (scaleFactorX > 1) {
305 // TODO: if we pass in the source draw context we don't need this here
robertphillips56a85e62016-05-06 07:17:49 -0700306 if (!srcDrawContext) {
jvanverth67a58dc2016-05-06 13:05:09 -0700307 srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRenderTarget()));
308 if (!srcDrawContext) {
309 return nullptr;
310 }
robertphillips56a85e62016-05-06 07:17:49 -0700311 }
jvanverth67a58dc2016-05-06 13:05:09 -0700312
313 // Clear out a radius to the right of the srcRect to prevent the
314 // X convolution from reading garbage.
315 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
316 radiusX, srcIRect.height());
317 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips56a85e62016-05-06 07:17:49 -0700318 }
319
jvanverth67a58dc2016-05-06 13:05:09 -0700320 sk_sp<GrDrawContext> dstDrawContext(
321 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props));
322 if (!dstDrawContext) {
323 return nullptr;
324 }
325 convolve_gaussian(dstDrawContext.get(), clip, srcRect,
326 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
327 srcBounds, srcOffset);
328 srcDrawContext.swap(dstDrawContext);
329 srcTexture = dstTexture;
330 srcRect.offsetTo(0, 0);
331 SkTSwap(dstTexture, tempTexture);
332 localSrcBounds = srcRect;
333 srcOffset.set(0, 0);
robertphillips56a85e62016-05-06 07:17:49 -0700334 }
335
jvanverth67a58dc2016-05-06 13:05:09 -0700336 if (sigmaY > 0.0f) {
337 if (scaleFactorY > 1 || sigmaX > 0.0f) {
338 // TODO: if we pass in the source draw context we don't need this here
339 if (!srcDrawContext) {
340 srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRenderTarget()));
341 if (!srcDrawContext) {
342 return nullptr;
343 }
344 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700345
jvanverth67a58dc2016-05-06 13:05:09 -0700346 // Clear out a radius below the srcRect to prevent the Y
347 // convolution from reading garbage.
348 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
349 srcIRect.width(), radiusY);
350 srcDrawContext->clear(&clearRect, 0x0, false);
351 }
352
353 sk_sp<GrDrawContext> dstDrawContext(
354 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props));
355 if (!dstDrawContext) {
356 return nullptr;
357 }
358 convolve_gaussian(dstDrawContext.get(), clip, srcRect,
359 srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
360 srcBounds, srcOffset);
361
362 srcDrawContext.swap(dstDrawContext);
363 srcTexture = dstTexture;
364 srcRect.offsetTo(0, 0);
365 SkTSwap(dstTexture, tempTexture);
366 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000367 }
jvanverth67a58dc2016-05-06 13:05:09 -0700368 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000369
370 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700371 SkASSERT(srcDrawContext);
372
robertphillips@google.com736dd032013-07-15 15:06:54 +0000373 // Clear one pixel to the right and below, to accommodate bilinear
374 // upsampling.
375 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
376 srcIRect.width() + 1, 1);
robertphillips2e1e51f2015-10-15 08:01:48 -0700377 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000378 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
379 1, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700380 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000381 SkMatrix matrix;
382 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000383
384 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -0700385 paint.setGammaCorrect(gammaCorrect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000386 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000387 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700388 paint.addColorTextureProcessor(srcTexture, matrix, params);
egdanielc4b72722015-11-23 13:20:41 -0800389 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000390
391 SkRect dstRect(srcRect);
392 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700393
robertphillips6c7e3252016-04-27 10:47:51 -0700394 sk_sp<GrDrawContext> dstDrawContext(
395 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget())));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700396 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700397 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700398 }
bsalomona2e69fc2015-11-05 10:41:43 -0800399 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700400
bungeman77a53de2015-10-01 12:28:49 -0700401 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000402 srcRect = dstRect;
403 srcTexture = dstTexture;
404 SkTSwap(dstTexture, tempTexture);
405 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700406
bsalomone3059732014-10-14 11:47:22 -0700407 return SkRef(srcTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000408}
409#endif
410
robertphillips@google.comcce41022013-07-15 15:47:10 +0000411}