blob: 77a850187788874df96455265709d922d66a0bcb [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;
joshualittb0a8a372014-09-23 09:50:21 -070059 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian(
bsalomon4a339522015-10-06 08:40:50 -070060 texture, direction, radius, sigma, useBounds, bounds));
bsalomonac856c92015-08-27 06:30:17 -070061 paint.addColorFragmentProcessor(conv);
egdanielc4b72722015-11-23 13:20:41 -080062 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
senorblancoc834ab12015-12-17 08:10:17 -080063 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y());
bsalomona2e69fc2015-11-05 10:41:43 -080064 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, localMatrix);
robertphillips@google.com736dd032013-07-15 15:06:54 +000065}
66
robertphillipsea461502015-05-26 11:38:03 -070067static void convolve_gaussian_2d(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080068 const GrClip& clip,
senorblancoc834ab12015-12-17 08:10:17 -080069 const SkRect& dstRect,
70 const SkPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070071 GrTexture* texture,
72 int radiusX,
73 int radiusY,
74 SkScalar sigmaX,
75 SkScalar sigmaY,
senorblanco07d56b12015-11-10 07:32:37 -080076 const SkRect* srcBounds) {
senorblancoc834ab12015-12-17 08:10:17 -080077 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y());
joshualitt5acfea72014-08-11 13:55:34 -070078 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
79 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
80 GrPaint paint;
senorblanco07d56b12015-11-10 07:32:37 -080081 SkIRect bounds;
82 if (srcBounds) {
83 srcBounds->roundOut(&bounds);
84 } else {
85 bounds.setEmpty();
86 }
87
joshualittb0a8a372014-09-23 09:50:21 -070088 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
joshualitt5acfea72014-08-11 13:55:34 -070089 texture, bounds, size, 1.0, 0.0, kernelOffset,
senorblanco07d56b12015-11-10 07:32:37 -080090 srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode,
joshualitt5acfea72014-08-11 13:55:34 -070091 true, sigmaX, sigmaY));
bsalomonac856c92015-08-27 06:30:17 -070092 paint.addColorFragmentProcessor(conv);
egdanielc4b72722015-11-23 13:20:41 -080093 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -080094 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, localMatrix);
joshualitt5acfea72014-08-11 13:55:34 -070095}
96
robertphillipsea461502015-05-26 11:38:03 -070097static void convolve_gaussian(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080098 const GrClip& clip,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +000099 const SkRect& srcRect,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000100 GrTexture* texture,
101 Gr1DKernelEffect::Direction direction,
102 int radius,
103 float sigma,
senorblanco07d56b12015-11-10 07:32:37 -0800104 const SkRect* srcBounds,
105 const SkPoint& srcOffset) {
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000106 float bounds[2] = { 0.0f, 1.0f };
senorblanco48343ee2015-11-03 05:07:43 -0800107 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
senorblanco07d56b12015-11-10 07:32:37 -0800108 if (!srcBounds) {
senorblanco48343ee2015-11-03 05:07:43 -0800109 convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700110 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000111 return;
112 }
senorblanco07d56b12015-11-10 07:32:37 -0800113 SkRect midRect = *srcBounds, leftRect, rightRect;
114 midRect.offset(srcOffset);
115 SkIRect topRect, bottomRect;
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000116 SkScalar rad = SkIntToScalar(radius);
117 if (direction == Gr1DKernelEffect::kX_Direction) {
senorblanco07d56b12015-11-10 07:32:37 -0800118 bounds[0] = SkScalarToFloat(srcBounds->left()) / texture->width();
119 bounds[1] = SkScalarToFloat(srcBounds->right()) / texture->width();
120 SkRect::MakeLTRB(0, 0, dstRect.right(), midRect.top()).roundOut(&topRect);
121 SkRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom())
122 .roundOut(&bottomRect);
123 midRect.inset(rad, 0);
124 leftRect = SkRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom());
125 rightRect =
126 SkRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom());
127 dstRect.fTop = midRect.top();
128 dstRect.fBottom = midRect.bottom();
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000129 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800130 bounds[0] = SkScalarToFloat(srcBounds->top()) / texture->height();
131 bounds[1] = SkScalarToFloat(srcBounds->bottom()) / texture->height();
132 SkRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom()).roundOut(&topRect);
133 SkRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom())
134 .roundOut(&bottomRect);;
135 midRect.inset(0, rad);
136 leftRect = SkRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top());
137 rightRect =
138 SkRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height());
139 dstRect.fLeft = midRect.left();
140 dstRect.fRight = midRect.right();
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000141 }
senorblanco07d56b12015-11-10 07:32:37 -0800142 if (!topRect.isEmpty()) {
143 drawContext->clear(&topRect, 0, false);
144 }
145
146 if (!bottomRect.isEmpty()) {
147 drawContext->clear(&bottomRect, 0, false);
148 }
149 if (midRect.isEmpty()) {
150 // Blur radius covers srcBounds; use bounds over entire draw
senorblancoc834ab12015-12-17 08:10:17 -0800151 convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700152 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000153 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800154 // Draw right and left margins with bounds; middle without.
senorblancoc834ab12015-12-17 08:10:17 -0800155 convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700156 direction, radius, sigma, true, bounds);
senorblancoc834ab12015-12-17 08:10:17 -0800157 convolve_gaussian_1d(drawContext, clip, rightRect, 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, midRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700160 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000161 }
162}
163
robertphillips@google.com736dd032013-07-15 15:06:54 +0000164GrTexture* GaussianBlur(GrContext* context,
165 GrTexture* srcTexture,
166 bool canClobberSrc,
senorblanco07d56b12015-11-10 07:32:37 -0800167 const SkRect& dstBounds,
168 const SkRect* srcBounds,
skia.committer@gmail.com977409a2013-07-16 07:00:56 +0000169 float sigmaX,
reed4e23cda2016-01-11 10:56:59 -0800170 float sigmaY) {
bsalomon49f085d2014-09-05 13:34:00 -0700171 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000172 SkIRect clearRect;
173 int scaleFactorX, radiusX;
174 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700175 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000176 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
177 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000178
senorblanco07d56b12015-11-10 07:32:37 -0800179 SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y());
180 SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height());
181 SkRect localSrcBounds;
182 SkRect srcRect;
183 if (srcBounds) {
184 srcRect = localSrcBounds = *srcBounds;
185 srcRect.offset(srcOffset);
186 srcBounds = &localSrcBounds;
187 } else {
188 srcRect = localDstBounds;
189 }
190
robertphillips@google.com736dd032013-07-15 15:06:54 +0000191 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800192 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000193 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
194 static_cast<float>(scaleFactorY));
195
joshualitt570d2f82015-02-25 13:19:48 -0800196 // setup new clip
senorblanco07d56b12015-11-10 07:32:37 -0800197 GrClip clip(localDstBounds);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000198
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000199 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000200 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
201 kAlpha_8_GrPixelConfig == srcTexture->config());
202
bsalomonf2703d82014-10-28 14:33:06 -0700203 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800204 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco07d56b12015-11-10 07:32:37 -0800205 desc.fWidth = SkScalarFloorToInt(dstBounds.width());
206 desc.fHeight = SkScalarFloorToInt(dstBounds.height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000207 desc.fConfig = srcTexture->config();
208
bsalomone3059732014-10-14 11:47:22 -0700209 GrTexture* dstTexture;
210 GrTexture* tempTexture;
211 SkAutoTUnref<GrTexture> temp1, temp2;
212
reed4e23cda2016-01-11 10:56:59 -0800213 temp1.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700214 dstTexture = temp1.get();
215 if (canClobberSrc) {
216 tempTexture = srcTexture;
217 } else {
reed4e23cda2016-01-11 10:56:59 -0800218 temp2.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700219 tempTexture = temp2.get();
220 }
221
halcanary96fcdcc2015-08-27 07:41:13 -0700222 if (nullptr == dstTexture || nullptr == tempTexture) {
223 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000224 }
225
robertphillipsc9a37062015-09-01 08:34:28 -0700226 SkAutoTUnref<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700227
robertphillips@google.com736dd032013-07-15 15:06:54 +0000228 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
229 GrPaint paint;
230 SkMatrix matrix;
231 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000232 SkRect dstRect(srcRect);
senorblanco07d56b12015-11-10 07:32:37 -0800233 if (srcBounds && i == 1) {
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000234 SkRect domain;
senorblanco07d56b12015-11-10 07:32:37 -0800235 matrix.mapRect(&domain, *srcBounds);
236 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f,
237 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bsalomon0ba8c242015-10-07 09:20:28 -0700238 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000239 srcTexture,
240 matrix,
241 domain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000242 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000243 GrTextureParams::kBilerp_FilterMode));
bsalomonac856c92015-08-27 06:30:17 -0700244 paint.addColorFragmentProcessor(fp);
senorblanco07d56b12015-11-10 07:32:37 -0800245 srcRect.offset(-srcOffset);
246 srcOffset.set(0, 0);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000247 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000248 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700249 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000250 }
egdanielc4b72722015-11-23 13:20:41 -0800251 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000252 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
253 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700254
robertphillips2e1e51f2015-10-15 08:01:48 -0700255 SkAutoTUnref<GrDrawContext> dstDrawContext(
256 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700257 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700259 }
bsalomona2e69fc2015-11-05 10:41:43 -0800260 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700261
bungeman77a53de2015-10-01 12:28:49 -0700262 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000263 srcRect = dstRect;
264 srcTexture = dstTexture;
265 SkTSwap(dstTexture, tempTexture);
senorblanco07d56b12015-11-10 07:32:37 -0800266 localSrcBounds = srcRect;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000267 }
268
robertphillipsff0ca5e2015-07-22 11:54:44 -0700269 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
joshualitt5acfea72014-08-11 13:55:34 -0700270 // launch a single non separable kernel vs two launches
senorblancoc834ab12015-12-17 08:10:17 -0800271 srcRect = localDstBounds;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700272 if (sigmaX > 0.0f && sigmaY > 0.0f &&
joshualitt5acfea72014-08-11 13:55:34 -0700273 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
274 // We shouldn't be scaling because this is a small size blur
robertphillipsff0ca5e2015-07-22 11:54:44 -0700275 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700276
robertphillips2e1e51f2015-10-15 08:01:48 -0700277 SkAutoTUnref<GrDrawContext> dstDrawContext(
278 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700279 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700280 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700281 }
senorblancoc834ab12015-12-17 08:10:17 -0800282 convolve_gaussian_2d(dstDrawContext, clip, srcRect, srcOffset,
senorblanco07d56b12015-11-10 07:32:37 -0800283 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBounds);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700284
bungeman77a53de2015-10-01 12:28:49 -0700285 srcDrawContext.swap(dstDrawContext);
senorblanco48343ee2015-11-03 05:07:43 -0800286 srcRect.offsetTo(0, 0);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700287 srcTexture = dstTexture;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000288 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000289
joshualitt5acfea72014-08-11 13:55:34 -0700290 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800291 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
292 srcRect.roundOut(&srcRect);
293 const SkIRect srcIRect = srcRect.roundOut();
joshualitt5acfea72014-08-11 13:55:34 -0700294 if (sigmaX > 0.0f) {
295 if (scaleFactorX > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700296 // TODO: if we pass in the source draw context we don't need this here
297 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700298 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700299 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700300 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700301 }
302 }
303
joshualitt5acfea72014-08-11 13:55:34 -0700304 // Clear out a radius to the right of the srcRect to prevent the
305 // X convolution from reading garbage.
306 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
307 radiusX, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700308 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700309 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700310
robertphillips2e1e51f2015-10-15 08:01:48 -0700311 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700312 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700313 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700314 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700315 }
senorblanco48343ee2015-11-03 05:07:43 -0800316 convolve_gaussian(dstDrawContext, clip, srcRect,
joshualitt570d2f82015-02-25 13:19:48 -0800317 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
senorblanco07d56b12015-11-10 07:32:37 -0800318 srcBounds, srcOffset);
bungeman77a53de2015-10-01 12:28:49 -0700319 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700320 srcTexture = dstTexture;
senorblanco48343ee2015-11-03 05:07:43 -0800321 srcRect.offsetTo(0, 0);
joshualitt5acfea72014-08-11 13:55:34 -0700322 SkTSwap(dstTexture, tempTexture);
senorblanco07d56b12015-11-10 07:32:37 -0800323 localSrcBounds = srcRect;
324 srcOffset.set(0, 0);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000325 }
326
joshualitt5acfea72014-08-11 13:55:34 -0700327 if (sigmaY > 0.0f) {
328 if (scaleFactorY > 1 || sigmaX > 0.0f) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700329 // TODO: if we pass in the source draw context we don't need this here
330 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700331 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700332 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700333 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700334 }
335 }
336
joshualitt5acfea72014-08-11 13:55:34 -0700337 // Clear out a radius below the srcRect to prevent the Y
338 // convolution from reading garbage.
339 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
340 srcIRect.width(), radiusY);
robertphillips2e1e51f2015-10-15 08:01:48 -0700341 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700342 }
343
robertphillips2e1e51f2015-10-15 08:01:48 -0700344 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700345 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700346 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700347 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700348 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700349 convolve_gaussian(dstDrawContext, clip, srcRect,
senorblanco48343ee2015-11-03 05:07:43 -0800350 srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
senorblanco07d56b12015-11-10 07:32:37 -0800351 srcBounds, srcOffset);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700352
bungeman77a53de2015-10-01 12:28:49 -0700353 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700354 srcTexture = dstTexture;
senorblanco48343ee2015-11-03 05:07:43 -0800355 srcRect.offsetTo(0, 0);
joshualitt5acfea72014-08-11 13:55:34 -0700356 SkTSwap(dstTexture, tempTexture);
357 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000358 }
senorblanco07d56b12015-11-10 07:32:37 -0800359 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000360
361 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700362 SkASSERT(srcDrawContext);
363
robertphillips@google.com736dd032013-07-15 15:06:54 +0000364 // Clear one pixel to the right and below, to accommodate bilinear
365 // upsampling.
366 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
367 srcIRect.width() + 1, 1);
robertphillips2e1e51f2015-10-15 08:01:48 -0700368 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000369 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
370 1, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700371 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000372 SkMatrix matrix;
373 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000374
375 GrPaint paint;
376 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000377 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700378 paint.addColorTextureProcessor(srcTexture, matrix, params);
egdanielc4b72722015-11-23 13:20:41 -0800379 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000380
381 SkRect dstRect(srcRect);
382 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700383
robertphillips2e1e51f2015-10-15 08:01:48 -0700384 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700385 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700386 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700387 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700388 }
bsalomona2e69fc2015-11-05 10:41:43 -0800389 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700390
bungeman77a53de2015-10-01 12:28:49 -0700391 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000392 srcRect = dstRect;
393 srcTexture = dstTexture;
394 SkTSwap(dstTexture, tempTexture);
395 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700396
bsalomone3059732014-10-14 11:47:22 -0700397 return SkRef(srcTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000398}
399#endif
400
robertphillips@google.comcce41022013-07-15 15:47:10 +0000401}