blob: 13b6b0a3bc165ffbe58f597856ea042c4e7db106 [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,
reedc9b5f8b2015-10-22 13:20:20 -0700170 float sigmaY,
171 GrTextureProvider::SizeConstraint constraint) {
bsalomon49f085d2014-09-05 13:34:00 -0700172 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000173 SkIRect clearRect;
174 int scaleFactorX, radiusX;
175 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700176 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000177 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
178 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000179
senorblanco07d56b12015-11-10 07:32:37 -0800180 SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y());
181 SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height());
182 SkRect localSrcBounds;
183 SkRect srcRect;
184 if (srcBounds) {
185 srcRect = localSrcBounds = *srcBounds;
186 srcRect.offset(srcOffset);
187 srcBounds = &localSrcBounds;
188 } else {
189 srcRect = localDstBounds;
190 }
191
robertphillips@google.com736dd032013-07-15 15:06:54 +0000192 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800193 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000194 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
195 static_cast<float>(scaleFactorY));
196
joshualitt570d2f82015-02-25 13:19:48 -0800197 // setup new clip
senorblanco07d56b12015-11-10 07:32:37 -0800198 GrClip clip(localDstBounds);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000199
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000200 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000201 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
202 kAlpha_8_GrPixelConfig == srcTexture->config());
203
bsalomonf2703d82014-10-28 14:33:06 -0700204 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800205 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco07d56b12015-11-10 07:32:37 -0800206 desc.fWidth = SkScalarFloorToInt(dstBounds.width());
207 desc.fHeight = SkScalarFloorToInt(dstBounds.height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000208 desc.fConfig = srcTexture->config();
209
bsalomone3059732014-10-14 11:47:22 -0700210 GrTexture* dstTexture;
211 GrTexture* tempTexture;
212 SkAutoTUnref<GrTexture> temp1, temp2;
213
reedc9b5f8b2015-10-22 13:20:20 -0700214 temp1.reset(context->textureProvider()->createTexture(desc, constraint));
bsalomone3059732014-10-14 11:47:22 -0700215 dstTexture = temp1.get();
216 if (canClobberSrc) {
217 tempTexture = srcTexture;
218 } else {
reedc9b5f8b2015-10-22 13:20:20 -0700219 temp2.reset(context->textureProvider()->createTexture(desc, constraint));
bsalomone3059732014-10-14 11:47:22 -0700220 tempTexture = temp2.get();
221 }
222
halcanary96fcdcc2015-08-27 07:41:13 -0700223 if (nullptr == dstTexture || nullptr == tempTexture) {
224 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000225 }
226
robertphillipsc9a37062015-09-01 08:34:28 -0700227 SkAutoTUnref<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700228
robertphillips@google.com736dd032013-07-15 15:06:54 +0000229 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
230 GrPaint paint;
231 SkMatrix matrix;
232 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000233 SkRect dstRect(srcRect);
senorblanco07d56b12015-11-10 07:32:37 -0800234 if (srcBounds && i == 1) {
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000235 SkRect domain;
senorblanco07d56b12015-11-10 07:32:37 -0800236 matrix.mapRect(&domain, *srcBounds);
237 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f,
238 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bsalomon0ba8c242015-10-07 09:20:28 -0700239 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000240 srcTexture,
241 matrix,
242 domain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000243 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000244 GrTextureParams::kBilerp_FilterMode));
bsalomonac856c92015-08-27 06:30:17 -0700245 paint.addColorFragmentProcessor(fp);
senorblanco07d56b12015-11-10 07:32:37 -0800246 srcRect.offset(-srcOffset);
247 srcOffset.set(0, 0);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000248 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000249 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700250 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000251 }
egdanielc4b72722015-11-23 13:20:41 -0800252 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000253 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
254 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700255
robertphillips2e1e51f2015-10-15 08:01:48 -0700256 SkAutoTUnref<GrDrawContext> dstDrawContext(
257 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700258 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700259 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700260 }
bsalomona2e69fc2015-11-05 10:41:43 -0800261 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700262
bungeman77a53de2015-10-01 12:28:49 -0700263 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000264 srcRect = dstRect;
265 srcTexture = dstTexture;
266 SkTSwap(dstTexture, tempTexture);
senorblanco07d56b12015-11-10 07:32:37 -0800267 localSrcBounds = srcRect;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000268 }
269
robertphillipsff0ca5e2015-07-22 11:54:44 -0700270 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
joshualitt5acfea72014-08-11 13:55:34 -0700271 // launch a single non separable kernel vs two launches
senorblancoc834ab12015-12-17 08:10:17 -0800272 srcRect = localDstBounds;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700273 if (sigmaX > 0.0f && sigmaY > 0.0f &&
joshualitt5acfea72014-08-11 13:55:34 -0700274 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
275 // We shouldn't be scaling because this is a small size blur
robertphillipsff0ca5e2015-07-22 11:54:44 -0700276 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700277
robertphillips2e1e51f2015-10-15 08:01:48 -0700278 SkAutoTUnref<GrDrawContext> dstDrawContext(
279 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700280 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700282 }
senorblancoc834ab12015-12-17 08:10:17 -0800283 convolve_gaussian_2d(dstDrawContext, clip, srcRect, srcOffset,
senorblanco07d56b12015-11-10 07:32:37 -0800284 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBounds);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700285
bungeman77a53de2015-10-01 12:28:49 -0700286 srcDrawContext.swap(dstDrawContext);
senorblanco48343ee2015-11-03 05:07:43 -0800287 srcRect.offsetTo(0, 0);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700288 srcTexture = dstTexture;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000289 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000290
joshualitt5acfea72014-08-11 13:55:34 -0700291 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800292 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
293 srcRect.roundOut(&srcRect);
294 const SkIRect srcIRect = srcRect.roundOut();
joshualitt5acfea72014-08-11 13:55:34 -0700295 if (sigmaX > 0.0f) {
296 if (scaleFactorX > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700297 // TODO: if we pass in the source draw context we don't need this here
298 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700299 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700300 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700301 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700302 }
303 }
304
joshualitt5acfea72014-08-11 13:55:34 -0700305 // Clear out a radius to the right of the srcRect to prevent the
306 // X convolution from reading garbage.
307 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
308 radiusX, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700309 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700310 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700311
robertphillips2e1e51f2015-10-15 08:01:48 -0700312 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700313 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700314 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700315 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700316 }
senorblanco48343ee2015-11-03 05:07:43 -0800317 convolve_gaussian(dstDrawContext, clip, srcRect,
joshualitt570d2f82015-02-25 13:19:48 -0800318 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
senorblanco07d56b12015-11-10 07:32:37 -0800319 srcBounds, srcOffset);
bungeman77a53de2015-10-01 12:28:49 -0700320 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700321 srcTexture = dstTexture;
senorblanco48343ee2015-11-03 05:07:43 -0800322 srcRect.offsetTo(0, 0);
joshualitt5acfea72014-08-11 13:55:34 -0700323 SkTSwap(dstTexture, tempTexture);
senorblanco07d56b12015-11-10 07:32:37 -0800324 localSrcBounds = srcRect;
325 srcOffset.set(0, 0);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000326 }
327
joshualitt5acfea72014-08-11 13:55:34 -0700328 if (sigmaY > 0.0f) {
329 if (scaleFactorY > 1 || sigmaX > 0.0f) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700330 // TODO: if we pass in the source draw context we don't need this here
331 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700332 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700333 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700334 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700335 }
336 }
337
joshualitt5acfea72014-08-11 13:55:34 -0700338 // Clear out a radius below the srcRect to prevent the Y
339 // convolution from reading garbage.
340 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
341 srcIRect.width(), radiusY);
robertphillips2e1e51f2015-10-15 08:01:48 -0700342 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700343 }
344
robertphillips2e1e51f2015-10-15 08:01:48 -0700345 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700346 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700347 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700348 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700349 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700350 convolve_gaussian(dstDrawContext, clip, srcRect,
senorblanco48343ee2015-11-03 05:07:43 -0800351 srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
senorblanco07d56b12015-11-10 07:32:37 -0800352 srcBounds, srcOffset);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700353
bungeman77a53de2015-10-01 12:28:49 -0700354 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700355 srcTexture = dstTexture;
senorblanco48343ee2015-11-03 05:07:43 -0800356 srcRect.offsetTo(0, 0);
joshualitt5acfea72014-08-11 13:55:34 -0700357 SkTSwap(dstTexture, tempTexture);
358 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000359 }
senorblanco07d56b12015-11-10 07:32:37 -0800360 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000361
362 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700363 SkASSERT(srcDrawContext);
364
robertphillips@google.com736dd032013-07-15 15:06:54 +0000365 // Clear one pixel to the right and below, to accommodate bilinear
366 // upsampling.
367 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
368 srcIRect.width() + 1, 1);
robertphillips2e1e51f2015-10-15 08:01:48 -0700369 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000370 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
371 1, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700372 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000373 SkMatrix matrix;
374 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000375
376 GrPaint paint;
377 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000378 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700379 paint.addColorTextureProcessor(srcTexture, matrix, params);
egdanielc4b72722015-11-23 13:20:41 -0800380 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000381
382 SkRect dstRect(srcRect);
383 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700384
robertphillips2e1e51f2015-10-15 08:01:48 -0700385 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700386 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700387 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700388 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700389 }
bsalomona2e69fc2015-11-05 10:41:43 -0800390 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700391
bungeman77a53de2015-10-01 12:28:49 -0700392 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000393 srcRect = dstRect;
394 srcTexture = dstTexture;
395 SkTSwap(dstTexture, tempTexture);
396 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700397
bsalomone3059732014-10-14 11:47:22 -0700398 return SkRef(srcTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000399}
400#endif
401
robertphillips@google.comcce41022013-07-15 15:47:10 +0000402}