blob: ec37505d3eb041255ca547dbdda0981a109978ea [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& srcRect,
51 const SkRect& dstRect,
52 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);
robertphillips2e1e51f2015-10-15 08:01:48 -070062 drawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +000063}
64
robertphillipsea461502015-05-26 11:38:03 -070065static void convolve_gaussian_2d(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080066 const GrClip& clip,
joshualitt5acfea72014-08-11 13:55:34 -070067 const SkRect& srcRect,
68 const SkRect& dstRect,
69 GrTexture* texture,
70 int radiusX,
71 int radiusY,
72 SkScalar sigmaX,
73 SkScalar sigmaY,
74 bool useBounds,
75 SkIRect bounds) {
76 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
77 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
78 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -070079 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
joshualitt5acfea72014-08-11 13:55:34 -070080 texture, bounds, size, 1.0, 0.0, kernelOffset,
81 useBounds ? GrTextureDomain::kClamp_Mode : GrTextureDomain::kIgnore_Mode,
82 true, sigmaX, sigmaY));
bsalomonac856c92015-08-27 06:30:17 -070083 paint.addColorFragmentProcessor(conv);
robertphillips2e1e51f2015-10-15 08:01:48 -070084 drawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
joshualitt5acfea72014-08-11 13:55:34 -070085}
86
robertphillipsea461502015-05-26 11:38:03 -070087static void convolve_gaussian(GrDrawContext* drawContext,
joshualitt570d2f82015-02-25 13:19:48 -080088 const GrClip& clip,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +000089 const SkRect& srcRect,
90 const SkRect& dstRect,
91 GrTexture* texture,
92 Gr1DKernelEffect::Direction direction,
93 int radius,
94 float sigma,
95 bool cropToSrcRect) {
96 float bounds[2] = { 0.0f, 1.0f };
97 if (!cropToSrcRect) {
robertphillips2e1e51f2015-10-15 08:01:48 -070098 convolve_gaussian_1d(drawContext, clip, srcRect, dstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -070099 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000100 return;
101 }
102 SkRect lowerSrcRect = srcRect, lowerDstRect = dstRect;
103 SkRect middleSrcRect = srcRect, middleDstRect = dstRect;
104 SkRect upperSrcRect = srcRect, upperDstRect = dstRect;
105 SkScalar size;
106 SkScalar rad = SkIntToScalar(radius);
107 if (direction == Gr1DKernelEffect::kX_Direction) {
108 bounds[0] = SkScalarToFloat(srcRect.left()) / texture->width();
109 bounds[1] = SkScalarToFloat(srcRect.right()) / texture->width();
110 size = srcRect.width();
111 lowerSrcRect.fRight = srcRect.left() + rad;
112 lowerDstRect.fRight = dstRect.left() + rad;
113 upperSrcRect.fLeft = srcRect.right() - rad;
114 upperDstRect.fLeft = dstRect.right() - rad;
115 middleSrcRect.inset(rad, 0);
116 middleDstRect.inset(rad, 0);
117 } else {
118 bounds[0] = SkScalarToFloat(srcRect.top()) / texture->height();
119 bounds[1] = SkScalarToFloat(srcRect.bottom()) / texture->height();
120 size = srcRect.height();
121 lowerSrcRect.fBottom = srcRect.top() + rad;
122 lowerDstRect.fBottom = dstRect.top() + rad;
123 upperSrcRect.fTop = srcRect.bottom() - rad;
124 upperDstRect.fTop = dstRect.bottom() - rad;
125 middleSrcRect.inset(0, rad);
126 middleDstRect.inset(0, rad);
127 }
128 if (radius >= size * SK_ScalarHalf) {
129 // Blur radius covers srcRect; use bounds over entire draw
robertphillips2e1e51f2015-10-15 08:01:48 -0700130 convolve_gaussian_1d(drawContext, clip, srcRect, dstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700131 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000132 } else {
133 // Draw upper and lower margins with bounds; middle without.
robertphillips2e1e51f2015-10-15 08:01:48 -0700134 convolve_gaussian_1d(drawContext, clip, lowerSrcRect, lowerDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700135 direction, radius, sigma, true, bounds);
robertphillips2e1e51f2015-10-15 08:01:48 -0700136 convolve_gaussian_1d(drawContext, clip, upperSrcRect, upperDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700137 direction, radius, sigma, true, bounds);
robertphillips2e1e51f2015-10-15 08:01:48 -0700138 convolve_gaussian_1d(drawContext, clip, middleSrcRect, middleDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700139 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000140 }
141}
142
robertphillips@google.com736dd032013-07-15 15:06:54 +0000143GrTexture* GaussianBlur(GrContext* context,
144 GrTexture* srcTexture,
145 bool canClobberSrc,
146 const SkRect& rect,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000147 bool cropToRect,
skia.committer@gmail.com977409a2013-07-16 07:00:56 +0000148 float sigmaX,
robertphillips@google.com736dd032013-07-15 15:06:54 +0000149 float sigmaY) {
bsalomon49f085d2014-09-05 13:34:00 -0700150 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000151
robertphillips@google.com736dd032013-07-15 15:06:54 +0000152 SkIRect clearRect;
153 int scaleFactorX, radiusX;
154 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700155 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000156 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
157 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000158
159 SkRect srcRect(rect);
160 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800161 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000162 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
163 static_cast<float>(scaleFactorY));
164
joshualitt570d2f82015-02-25 13:19:48 -0800165 // setup new clip
166 GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height()));
robertphillips@google.com736dd032013-07-15 15:06:54 +0000167
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000168 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000169 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
170 kAlpha_8_GrPixelConfig == srcTexture->config());
171
bsalomonf2703d82014-10-28 14:33:06 -0700172 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800173 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000174 desc.fWidth = SkScalarFloorToInt(srcRect.width());
175 desc.fHeight = SkScalarFloorToInt(srcRect.height());
176 desc.fConfig = srcTexture->config();
177
bsalomone3059732014-10-14 11:47:22 -0700178 GrTexture* dstTexture;
179 GrTexture* tempTexture;
180 SkAutoTUnref<GrTexture> temp1, temp2;
181
bsalomoneae62002015-07-31 13:59:30 -0700182 temp1.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700183 dstTexture = temp1.get();
184 if (canClobberSrc) {
185 tempTexture = srcTexture;
186 } else {
bsalomoneae62002015-07-31 13:59:30 -0700187 temp2.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700188 tempTexture = temp2.get();
189 }
190
halcanary96fcdcc2015-08-27 07:41:13 -0700191 if (nullptr == dstTexture || nullptr == tempTexture) {
192 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000193 }
194
robertphillipsc9a37062015-09-01 08:34:28 -0700195 SkAutoTUnref<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700196
robertphillips@google.com736dd032013-07-15 15:06:54 +0000197 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
198 GrPaint paint;
199 SkMatrix matrix;
200 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000201 SkRect dstRect(srcRect);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000202 if (cropToRect && i == 1) {
203 dstRect.offset(-dstRect.fLeft, -dstRect.fTop);
204 SkRect domain;
205 matrix.mapRect(&domain, rect);
206 domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f,
207 i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bsalomon0ba8c242015-10-07 09:20:28 -0700208 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000209 srcTexture,
210 matrix,
211 domain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000212 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000213 GrTextureParams::kBilerp_FilterMode));
bsalomonac856c92015-08-27 06:30:17 -0700214 paint.addColorFragmentProcessor(fp);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000215 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000216 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700217 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000218 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000219 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
220 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700221
robertphillips2e1e51f2015-10-15 08:01:48 -0700222 SkAutoTUnref<GrDrawContext> dstDrawContext(
223 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700224 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700225 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700226 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700227 dstDrawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700228
bungeman77a53de2015-10-01 12:28:49 -0700229 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000230 srcRect = dstRect;
231 srcTexture = dstTexture;
232 SkTSwap(dstTexture, tempTexture);
233 }
234
reedb07a94f2014-11-19 05:03:18 -0800235 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000236
robertphillipsff0ca5e2015-07-22 11:54:44 -0700237 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
joshualitt5acfea72014-08-11 13:55:34 -0700238 // launch a single non separable kernel vs two launches
robertphillipsff0ca5e2015-07-22 11:54:44 -0700239 if (sigmaX > 0.0f && sigmaY > 0.0f &&
joshualitt5acfea72014-08-11 13:55:34 -0700240 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
241 // We shouldn't be scaling because this is a small size blur
robertphillipsff0ca5e2015-07-22 11:54:44 -0700242 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000243 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700244
robertphillips2e1e51f2015-10-15 08:01:48 -0700245 SkAutoTUnref<GrDrawContext> dstDrawContext(
246 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700247 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700248 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700249 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700250 convolve_gaussian_2d(dstDrawContext, clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800251 srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700252
bungeman77a53de2015-10-01 12:28:49 -0700253 srcDrawContext.swap(dstDrawContext);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000254 srcRect = dstRect;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700255 srcTexture = dstTexture;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000256 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000257
joshualitt5acfea72014-08-11 13:55:34 -0700258 } else {
259 if (sigmaX > 0.0f) {
260 if (scaleFactorX > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700261 // TODO: if we pass in the source draw context we don't need this here
262 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700263 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700264 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700265 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700266 }
267 }
268
joshualitt5acfea72014-08-11 13:55:34 -0700269 // Clear out a radius to the right of the srcRect to prevent the
270 // X convolution from reading garbage.
271 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
272 radiusX, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700273 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700274 }
joshualitt5acfea72014-08-11 13:55:34 -0700275 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
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 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700282 convolve_gaussian(dstDrawContext, clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800283 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
284 cropToRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700285
bungeman77a53de2015-10-01 12:28:49 -0700286 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700287 srcTexture = dstTexture;
288 srcRect = dstRect;
289 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000290 }
291
joshualitt5acfea72014-08-11 13:55:34 -0700292 if (sigmaY > 0.0f) {
293 if (scaleFactorY > 1 || sigmaX > 0.0f) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700294 // TODO: if we pass in the source draw context we don't need this here
295 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700296 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700297 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700298 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700299 }
300 }
301
joshualitt5acfea72014-08-11 13:55:34 -0700302 // Clear out a radius below the srcRect to prevent the Y
303 // convolution from reading garbage.
304 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
305 srcIRect.width(), radiusY);
robertphillips2e1e51f2015-10-15 08:01:48 -0700306 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700307 }
308
joshualitt5acfea72014-08-11 13:55:34 -0700309 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700310
robertphillips2e1e51f2015-10-15 08:01:48 -0700311 SkAutoTUnref<GrDrawContext> dstDrawContext(
312 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 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700316 convolve_gaussian(dstDrawContext, clip, srcRect,
joshualitt570d2f82015-02-25 13:19:48 -0800317 dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
318 cropToRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700319
bungeman77a53de2015-10-01 12:28:49 -0700320 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700321 srcTexture = dstTexture;
322 srcRect = dstRect;
323 SkTSwap(dstTexture, tempTexture);
324 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000325 }
326
327 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700328 SkASSERT(srcDrawContext);
329
robertphillips@google.com736dd032013-07-15 15:06:54 +0000330 // Clear one pixel to the right and below, to accommodate bilinear
331 // upsampling.
332 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
333 srcIRect.width() + 1, 1);
robertphillips2e1e51f2015-10-15 08:01:48 -0700334 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000335 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
336 1, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700337 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000338 SkMatrix matrix;
339 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000340
341 GrPaint paint;
342 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000343 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700344 paint.addColorTextureProcessor(srcTexture, matrix, params);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000345
346 SkRect dstRect(srcRect);
347 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700348
robertphillips2e1e51f2015-10-15 08:01:48 -0700349 SkAutoTUnref<GrDrawContext> dstDrawContext(
350 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700351 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700352 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700353 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700354 dstDrawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700355
bungeman77a53de2015-10-01 12:28:49 -0700356 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000357 srcRect = dstRect;
358 srcTexture = dstTexture;
359 SkTSwap(dstTexture, tempTexture);
360 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700361
bsalomone3059732014-10-14 11:47:22 -0700362 return SkRef(srcTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000363}
364#endif
365
robertphillips@google.comcce41022013-07-15 15:47:10 +0000366}