blob: de19d4372c498dd3dd4840c8bfdc5cbb146b0dd0 [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
19#define MAX_BLUR_SIGMA 4.0f
20
robertphillipsf054b172016-05-13 05:06:19 -070021static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) {
22 rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale));
23 rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale));
24 rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale));
25 rect->fBottom = SkScalarCeilToInt(SkScalarMul(rect->fBottom, yScale));
26}
27
28static void scale_irect(SkIRect* rect, int xScale, int yScale) {
29 rect->fLeft *= xScale;
30 rect->fTop *= yScale;
31 rect->fRight *= xScale;
32 rect->fBottom *= yScale;
33}
34
35#ifdef SK_DEBUG
36static inline int is_even(int x) { return !(x & 1); }
37#endif
38
39static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) {
40 if (xAxis) {
41 SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight));
42 rect->fLeft /= 2;
43 rect->fRight /= 2;
44 }
45 if (yAxis) {
46 SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom));
47 rect->fTop /= 2;
48 rect->fBottom /= 2;
49 }
robertphillips@google.com736dd032013-07-15 15:06:54 +000050}
51
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +000052static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000053 *scaleFactor = 1;
54 while (sigma > MAX_BLUR_SIGMA) {
55 *scaleFactor *= 2;
56 sigma *= 0.5f;
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +000057 if (*scaleFactor > maxTextureSize) {
58 *scaleFactor = maxTextureSize;
59 sigma = MAX_BLUR_SIGMA;
60 }
robertphillips@google.com736dd032013-07-15 15:06:54 +000061 }
62 *radius = static_cast<int>(ceilf(sigma * 3.0f));
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +000063 SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius);
robertphillips@google.com736dd032013-07-15 15:06:54 +000064 return sigma;
65}
66
robertphillipsea461502015-05-26 11:38:03 -070067static void convolve_gaussian_1d(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080068 const GrClip& clip,
robertphillipsf054b172016-05-13 05:06:19 -070069 const SkIRect& dstRect,
70 const SkIPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070071 GrTexture* texture,
72 Gr1DKernelEffect::Direction direction,
73 int radius,
74 float sigma,
75 bool useBounds,
76 float bounds[2]) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000077 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -070078 paint.setGammaCorrect(drawContext->isGammaCorrect());
joshualittb0a8a372014-09-23 09:50:21 -070079 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian(
bsalomon4a339522015-10-06 08:40:50 -070080 texture, direction, radius, sigma, useBounds, bounds));
bsalomonac856c92015-08-27 06:30:17 -070081 paint.addColorFragmentProcessor(conv);
egdanielc4b72722015-11-23 13:20:41 -080082 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsf054b172016-05-13 05:06:19 -070083 SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
84 -SkIntToScalar(srcOffset.y()));
85 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(),
86 SkRect::Make(dstRect), localMatrix);
robertphillips@google.com736dd032013-07-15 15:06:54 +000087}
88
robertphillipsea461502015-05-26 11:38:03 -070089static void convolve_gaussian_2d(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080090 const GrClip& clip,
robertphillipsf054b172016-05-13 05:06:19 -070091 const SkIRect& dstRect,
92 const SkIPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070093 GrTexture* texture,
94 int radiusX,
95 int radiusY,
96 SkScalar sigmaX,
97 SkScalar sigmaY,
robertphillipsf054b172016-05-13 05:06:19 -070098 const SkIRect* srcBounds) {
99 SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
100 -SkIntToScalar(srcOffset.y()));
joshualitt5acfea72014-08-11 13:55:34 -0700101 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
102 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
103 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -0700104 paint.setGammaCorrect(drawContext->isGammaCorrect());
robertphillipsf054b172016-05-13 05:06:19 -0700105 SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect();
senorblanco07d56b12015-11-10 07:32:37 -0800106
joshualittb0a8a372014-09-23 09:50:21 -0700107 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
joshualitt5acfea72014-08-11 13:55:34 -0700108 texture, bounds, size, 1.0, 0.0, kernelOffset,
senorblanco07d56b12015-11-10 07:32:37 -0800109 srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode,
joshualitt5acfea72014-08-11 13:55:34 -0700110 true, sigmaX, sigmaY));
bsalomonac856c92015-08-27 06:30:17 -0700111 paint.addColorFragmentProcessor(conv);
egdanielc4b72722015-11-23 13:20:41 -0800112 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsf054b172016-05-13 05:06:19 -0700113 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(),
114 SkRect::Make(dstRect), localMatrix);
joshualitt5acfea72014-08-11 13:55:34 -0700115}
116
robertphillipsea461502015-05-26 11:38:03 -0700117static void convolve_gaussian(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -0800118 const GrClip& clip,
robertphillipsf054b172016-05-13 05:06:19 -0700119 const SkIRect& srcRect,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000120 GrTexture* texture,
121 Gr1DKernelEffect::Direction direction,
122 int radius,
123 float sigma,
robertphillipsf054b172016-05-13 05:06:19 -0700124 const SkIRect* srcBounds,
125 const SkIPoint& srcOffset) {
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000126 float bounds[2] = { 0.0f, 1.0f };
robertphillipsf054b172016-05-13 05:06:19 -0700127 SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height());
senorblanco07d56b12015-11-10 07:32:37 -0800128 if (!srcBounds) {
senorblanco48343ee2015-11-03 05:07:43 -0800129 convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700130 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000131 return;
132 }
robertphillipsf054b172016-05-13 05:06:19 -0700133 SkIRect midRect = *srcBounds, leftRect, rightRect;
senorblanco07d56b12015-11-10 07:32:37 -0800134 midRect.offset(srcOffset);
135 SkIRect topRect, bottomRect;
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000136 if (direction == Gr1DKernelEffect::kX_Direction) {
robertphillipsf054b172016-05-13 05:06:19 -0700137 bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width();
138 bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width();
139 topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top());
140 bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom());
141 midRect.inset(radius, 0);
142 leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom());
senorblanco07d56b12015-11-10 07:32:37 -0800143 rightRect =
robertphillipsf054b172016-05-13 05:06:19 -0700144 SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom());
senorblanco07d56b12015-11-10 07:32:37 -0800145 dstRect.fTop = midRect.top();
146 dstRect.fBottom = midRect.bottom();
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000147 } else {
robertphillipsf054b172016-05-13 05:06:19 -0700148 bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height();
149 bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height();
150 topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom());
151 bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom());
152 midRect.inset(0, radius);
153 leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top());
senorblanco07d56b12015-11-10 07:32:37 -0800154 rightRect =
robertphillipsf054b172016-05-13 05:06:19 -0700155 SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height());
senorblanco07d56b12015-11-10 07:32:37 -0800156 dstRect.fLeft = midRect.left();
157 dstRect.fRight = midRect.right();
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000158 }
senorblanco07d56b12015-11-10 07:32:37 -0800159 if (!topRect.isEmpty()) {
160 drawContext->clear(&topRect, 0, false);
161 }
162
163 if (!bottomRect.isEmpty()) {
164 drawContext->clear(&bottomRect, 0, false);
165 }
166 if (midRect.isEmpty()) {
167 // Blur radius covers srcBounds; use bounds over entire draw
senorblancoc834ab12015-12-17 08:10:17 -0800168 convolve_gaussian_1d(drawContext, clip, dstRect, srcOffset, texture,
robertphillipsf054b172016-05-13 05:06:19 -0700169 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000170 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800171 // Draw right and left margins with bounds; middle without.
senorblancoc834ab12015-12-17 08:10:17 -0800172 convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700173 direction, radius, sigma, true, bounds);
senorblancoc834ab12015-12-17 08:10:17 -0800174 convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700175 direction, radius, sigma, true, bounds);
senorblancoc834ab12015-12-17 08:10:17 -0800176 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700177 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000178 }
179}
180
robertphillipsf054b172016-05-13 05:06:19 -0700181namespace SkGpuBlurUtils {
182
robertphillips04c84af2016-05-12 11:13:00 -0700183sk_sp<GrDrawContext> GaussianBlur(GrContext* context,
184 GrTexture* origSrc,
185 bool gammaCorrect,
robertphillipsf054b172016-05-13 05:06:19 -0700186 const SkIRect& dstBounds,
187 const SkIRect* srcBounds,
robertphillips04c84af2016-05-12 11:13:00 -0700188 float sigmaX,
189 float sigmaY) {
bsalomon49f085d2014-09-05 13:34:00 -0700190 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000191 SkIRect clearRect;
192 int scaleFactorX, radiusX;
193 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700194 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000195 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
196 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillipsf054b172016-05-13 05:06:19 -0700197 SkASSERT(sigmaX || sigmaY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000198
robertphillipsf054b172016-05-13 05:06:19 -0700199 SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y());
200 SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height());
201 SkIRect localSrcBounds;
202 SkIRect srcRect;
senorblanco07d56b12015-11-10 07:32:37 -0800203 if (srcBounds) {
204 srcRect = localSrcBounds = *srcBounds;
205 srcRect.offset(srcOffset);
206 srcBounds = &localSrcBounds;
207 } else {
208 srcRect = localDstBounds;
209 }
210
robertphillipsf054b172016-05-13 05:06:19 -0700211 scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
212 scale_irect(&srcRect, scaleFactorX, scaleFactorY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000213
joshualitt570d2f82015-02-25 13:19:48 -0800214 // setup new clip
senorblanco07d56b12015-11-10 07:32:37 -0800215 GrClip clip(localDstBounds);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000216
robertphillips04c84af2016-05-12 11:13:00 -0700217 sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc));
218
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000219 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000220 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
brianosmana6359362016-03-21 06:55:37 -0700221 kSRGBA_8888_GrPixelConfig == srcTexture->config() ||
222 kSBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000223 kAlpha_8_GrPixelConfig == srcTexture->config());
224
robertphillipsf054b172016-05-13 05:06:19 -0700225 const int width = dstBounds.width();
226 const int height = dstBounds.height();
robertphillipsa8966a82016-05-09 06:45:37 -0700227 const GrPixelConfig config = srcTexture->config();
228
229 const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0,
230 SkSurfaceProps::kLegacyFontHost_InitType);
231
robertphillips04c84af2016-05-12 11:13:00 -0700232 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox,
233 width, height, config,
234 0, kDefault_GrSurfaceOrigin,
235 &props));
236 if (!dstDrawContext) {
237 return nullptr;
238 }
239
robertphillipsa8966a82016-05-09 06:45:37 -0700240 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
241 // launch a single non separable kernel vs two launches
242 if (sigmaX > 0.0f && sigmaY > 0.0f &&
243 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
244 // We shouldn't be scaling because this is a small size blur
245 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
246
robertphillipsa8966a82016-05-09 06:45:37 -0700247 convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset,
robertphillips04c84af2016-05-12 11:13:00 -0700248 srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds);
robertphillipsa8966a82016-05-09 06:45:37 -0700249
robertphillips04c84af2016-05-12 11:13:00 -0700250 return dstDrawContext;
robertphillipsa8966a82016-05-09 06:45:37 -0700251 }
252
robertphillips04c84af2016-05-12 11:13:00 -0700253 sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kApprox,
254 width, height, config,
255 0, kDefault_GrSurfaceOrigin,
256 &props));
257 if (!tmpDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000259 }
260
robertphillips6c7e3252016-04-27 10:47:51 -0700261 sk_sp<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700262
robertphillipsf054b172016-05-13 05:06:19 -0700263 SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY));
264
robertphillips@google.com736dd032013-07-15 15:06:54 +0000265 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
266 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -0700267 paint.setGammaCorrect(gammaCorrect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000268 SkMatrix matrix;
269 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillipsf054b172016-05-13 05:06:19 -0700270 SkIRect dstRect(srcRect);
senorblanco07d56b12015-11-10 07:32:37 -0800271 if (srcBounds && i == 1) {
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000272 SkRect domain;
robertphillipsf054b172016-05-13 05:06:19 -0700273 matrix.mapRect(&domain, SkRect::Make(*srcBounds));
senorblanco07d56b12015-11-10 07:32:37 -0800274 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f,
275 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f);
robertphillipsa8966a82016-05-09 06:45:37 -0700276 sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
robertphillips04c84af2016-05-12 11:13:00 -0700277 srcTexture.get(),
robertphillipsa8966a82016-05-09 06:45:37 -0700278 matrix,
279 domain,
280 GrTextureDomain::kDecal_Mode,
281 GrTextureParams::kBilerp_FilterMode));
282 paint.addColorFragmentProcessor(fp.get());
senorblanco07d56b12015-11-10 07:32:37 -0800283 srcRect.offset(-srcOffset);
284 srcOffset.set(0, 0);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000285 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000286 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
robertphillips04c84af2016-05-12 11:13:00 -0700287 paint.addColorTextureProcessor(srcTexture.get(), matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000288 }
egdanielc4b72722015-11-23 13:20:41 -0800289 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsf054b172016-05-13 05:06:19 -0700290 shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700291
robertphillipsf054b172016-05-13 05:06:19 -0700292 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(),
293 SkRect::Make(dstRect), SkRect::Make(srcRect));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700294
robertphillips04c84af2016-05-12 11:13:00 -0700295 srcDrawContext = dstDrawContext;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000296 srcRect = dstRect;
robertphillips04c84af2016-05-12 11:13:00 -0700297 srcTexture = srcDrawContext->asTexture();
298 dstDrawContext.swap(tmpDrawContext);
senorblanco07d56b12015-11-10 07:32:37 -0800299 localSrcBounds = srcRect;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000300 }
301
senorblancoc834ab12015-12-17 08:10:17 -0800302 srcRect = localDstBounds;
robertphillipsf054b172016-05-13 05:06:19 -0700303 scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
robertphillipsa8966a82016-05-09 06:45:37 -0700304 if (sigmaX > 0.0f) {
305 if (scaleFactorX > 1) {
robertphillips04c84af2016-05-12 11:13:00 -0700306 SkASSERT(srcDrawContext);
robertphillipsa8966a82016-05-09 06:45:37 -0700307
308 // Clear out a radius to the right of the srcRect to prevent the
309 // X convolution from reading garbage.
robertphillipsf054b172016-05-13 05:06:19 -0700310 clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop,
311 radiusX, srcRect.height());
robertphillipsa8966a82016-05-09 06:45:37 -0700312 srcDrawContext->clear(&clearRect, 0x0, false);
313 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700314
robertphillipsa8966a82016-05-09 06:45:37 -0700315 convolve_gaussian(dstDrawContext.get(), clip, srcRect,
robertphillips04c84af2016-05-12 11:13:00 -0700316 srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
robertphillipsa8966a82016-05-09 06:45:37 -0700317 srcBounds, srcOffset);
robertphillips04c84af2016-05-12 11:13:00 -0700318 srcDrawContext = dstDrawContext;
319 srcTexture = srcDrawContext->asTexture();
robertphillipsa8966a82016-05-09 06:45:37 -0700320 srcRect.offsetTo(0, 0);
robertphillips04c84af2016-05-12 11:13:00 -0700321 dstDrawContext.swap(tmpDrawContext);
robertphillipsa8966a82016-05-09 06:45:37 -0700322 localSrcBounds = srcRect;
323 srcOffset.set(0, 0);
324 }
325
326 if (sigmaY > 0.0f) {
327 if (scaleFactorY > 1 || sigmaX > 0.0f) {
robertphillips04c84af2016-05-12 11:13:00 -0700328 SkASSERT(srcDrawContext);
robertphillipsa8966a82016-05-09 06:45:37 -0700329
330 // Clear out a radius below the srcRect to prevent the Y
331 // convolution from reading garbage.
robertphillipsf054b172016-05-13 05:06:19 -0700332 clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom,
333 srcRect.width(), radiusY);
robertphillipsa8966a82016-05-09 06:45:37 -0700334 srcDrawContext->clear(&clearRect, 0x0, false);
335 }
336
robertphillipsa8966a82016-05-09 06:45:37 -0700337 convolve_gaussian(dstDrawContext.get(), clip, srcRect,
robertphillips04c84af2016-05-12 11:13:00 -0700338 srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
robertphillipsa8966a82016-05-09 06:45:37 -0700339 srcBounds, srcOffset);
robertphillips56a85e62016-05-06 07:17:49 -0700340
robertphillips04c84af2016-05-12 11:13:00 -0700341 srcDrawContext = dstDrawContext;
robertphillipsa8966a82016-05-09 06:45:37 -0700342 srcRect.offsetTo(0, 0);
robertphillips04c84af2016-05-12 11:13:00 -0700343 dstDrawContext.swap(tmpDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000344 }
robertphillipsa8966a82016-05-09 06:45:37 -0700345
robertphillips04c84af2016-05-12 11:13:00 -0700346 SkASSERT(srcDrawContext);
347 srcTexture = nullptr; // we don't use this from here on out
robertphillips@google.com736dd032013-07-15 15:06:54 +0000348
349 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillips04c84af2016-05-12 11:13:00 -0700350 // Clear one pixel to the right and below, to accommodate bilinear upsampling.
robertphillipsf054b172016-05-13 05:06:19 -0700351 clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
robertphillips04c84af2016-05-12 11:13:00 -0700352 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillipsf054b172016-05-13 05:06:19 -0700353 clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height());
robertphillips04c84af2016-05-12 11:13:00 -0700354 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700355
robertphillips@google.com736dd032013-07-15 15:06:54 +0000356 SkMatrix matrix;
robertphillips04c84af2016-05-12 11:13:00 -0700357 matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000358
359 GrPaint paint;
brianosmanb461d342016-04-13 13:10:14 -0700360 paint.setGammaCorrect(gammaCorrect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000361 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000362 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
robertphillips04c84af2016-05-12 11:13:00 -0700363 sk_sp<GrTexture> tex(srcDrawContext->asTexture());
364 paint.addColorTextureProcessor(tex.get(), matrix, params);
egdanielc4b72722015-11-23 13:20:41 -0800365 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000366
robertphillipsf054b172016-05-13 05:06:19 -0700367 SkIRect dstRect(srcRect);
368 scale_irect(&dstRect, scaleFactorX, scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700369
robertphillipsf054b172016-05-13 05:06:19 -0700370 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(),
371 SkRect::Make(dstRect), SkRect::Make(srcRect));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700372
robertphillips04c84af2016-05-12 11:13:00 -0700373 srcDrawContext = dstDrawContext;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000374 srcRect = dstRect;
robertphillips04c84af2016-05-12 11:13:00 -0700375 dstDrawContext.swap(tmpDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000376 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700377
robertphillips04c84af2016-05-12 11:13:00 -0700378 return srcDrawContext;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000379}
robertphillips@google.com736dd032013-07-15 15:06:54 +0000380
robertphillips@google.comcce41022013-07-15 15:47:10 +0000381}
robertphillipsf054b172016-05-13 05:06:19 -0700382
383#endif
384