blob: 0ebbfe45bede645e75bec1de4aea7022776084fb [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,
joshualitt25d9c152015-02-18 12:29:52 -080049 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080050 const GrClip& clip,
joshualitt5acfea72014-08-11 13:55:34 -070051 const SkRect& srcRect,
52 const SkRect& dstRect,
53 GrTexture* texture,
54 Gr1DKernelEffect::Direction direction,
55 int radius,
56 float sigma,
57 bool useBounds,
58 float bounds[2]) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000059 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -070060 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian(
bsalomon4a339522015-10-06 08:40:50 -070061 texture, direction, radius, sigma, useBounds, bounds));
bsalomonac856c92015-08-27 06:30:17 -070062 paint.addColorFragmentProcessor(conv);
robertphillipsea461502015-05-26 11:38:03 -070063 drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +000064}
65
robertphillipsea461502015-05-26 11:38:03 -070066static void convolve_gaussian_2d(GrDrawContext* drawContext,
joshualitt25d9c152015-02-18 12:29:52 -080067 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080068 const GrClip& clip,
joshualitt5acfea72014-08-11 13:55:34 -070069 const SkRect& srcRect,
70 const SkRect& dstRect,
71 GrTexture* texture,
72 int radiusX,
73 int radiusY,
74 SkScalar sigmaX,
75 SkScalar sigmaY,
76 bool useBounds,
77 SkIRect bounds) {
78 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
79 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
80 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -070081 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaussian(
joshualitt5acfea72014-08-11 13:55:34 -070082 texture, bounds, size, 1.0, 0.0, kernelOffset,
83 useBounds ? GrTextureDomain::kClamp_Mode : GrTextureDomain::kIgnore_Mode,
84 true, sigmaX, sigmaY));
bsalomonac856c92015-08-27 06:30:17 -070085 paint.addColorFragmentProcessor(conv);
robertphillipsea461502015-05-26 11:38:03 -070086 drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), dstRect, srcRect);
joshualitt5acfea72014-08-11 13:55:34 -070087}
88
robertphillipsea461502015-05-26 11:38:03 -070089static void convolve_gaussian(GrDrawContext* drawContext,
joshualitt25d9c152015-02-18 12:29:52 -080090 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080091 const GrClip& clip,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +000092 const SkRect& srcRect,
93 const SkRect& dstRect,
94 GrTexture* texture,
95 Gr1DKernelEffect::Direction direction,
96 int radius,
97 float sigma,
98 bool cropToSrcRect) {
99 float bounds[2] = { 0.0f, 1.0f };
100 if (!cropToSrcRect) {
robertphillipsea461502015-05-26 11:38:03 -0700101 convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700102 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000103 return;
104 }
105 SkRect lowerSrcRect = srcRect, lowerDstRect = dstRect;
106 SkRect middleSrcRect = srcRect, middleDstRect = dstRect;
107 SkRect upperSrcRect = srcRect, upperDstRect = dstRect;
108 SkScalar size;
109 SkScalar rad = SkIntToScalar(radius);
110 if (direction == Gr1DKernelEffect::kX_Direction) {
111 bounds[0] = SkScalarToFloat(srcRect.left()) / texture->width();
112 bounds[1] = SkScalarToFloat(srcRect.right()) / texture->width();
113 size = srcRect.width();
114 lowerSrcRect.fRight = srcRect.left() + rad;
115 lowerDstRect.fRight = dstRect.left() + rad;
116 upperSrcRect.fLeft = srcRect.right() - rad;
117 upperDstRect.fLeft = dstRect.right() - rad;
118 middleSrcRect.inset(rad, 0);
119 middleDstRect.inset(rad, 0);
120 } else {
121 bounds[0] = SkScalarToFloat(srcRect.top()) / texture->height();
122 bounds[1] = SkScalarToFloat(srcRect.bottom()) / texture->height();
123 size = srcRect.height();
124 lowerSrcRect.fBottom = srcRect.top() + rad;
125 lowerDstRect.fBottom = dstRect.top() + rad;
126 upperSrcRect.fTop = srcRect.bottom() - rad;
127 upperDstRect.fTop = dstRect.bottom() - rad;
128 middleSrcRect.inset(0, rad);
129 middleDstRect.inset(0, rad);
130 }
131 if (radius >= size * SK_ScalarHalf) {
132 // Blur radius covers srcRect; use bounds over entire draw
robertphillipsea461502015-05-26 11:38:03 -0700133 convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700134 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000135 } else {
136 // Draw upper and lower margins with bounds; middle without.
robertphillipsea461502015-05-26 11:38:03 -0700137 convolve_gaussian_1d(drawContext, rt, clip, lowerSrcRect, lowerDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700138 direction, radius, sigma, true, bounds);
robertphillipsea461502015-05-26 11:38:03 -0700139 convolve_gaussian_1d(drawContext, rt, clip, upperSrcRect, upperDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700140 direction, radius, sigma, true, bounds);
robertphillipsea461502015-05-26 11:38:03 -0700141 convolve_gaussian_1d(drawContext, rt, clip, middleSrcRect, middleDstRect, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700142 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000143 }
144}
145
robertphillips@google.com736dd032013-07-15 15:06:54 +0000146GrTexture* GaussianBlur(GrContext* context,
147 GrTexture* srcTexture,
148 bool canClobberSrc,
149 const SkRect& rect,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000150 bool cropToRect,
skia.committer@gmail.com977409a2013-07-16 07:00:56 +0000151 float sigmaX,
robertphillips@google.com736dd032013-07-15 15:06:54 +0000152 float sigmaY) {
bsalomon49f085d2014-09-05 13:34:00 -0700153 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000154
robertphillips@google.com736dd032013-07-15 15:06:54 +0000155 SkIRect clearRect;
156 int scaleFactorX, radiusX;
157 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700158 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000159 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
160 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000161
162 SkRect srcRect(rect);
163 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800164 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000165 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
166 static_cast<float>(scaleFactorY));
167
joshualitt570d2f82015-02-25 13:19:48 -0800168 // setup new clip
169 GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height()));
robertphillips@google.com736dd032013-07-15 15:06:54 +0000170
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000171 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000172 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
173 kAlpha_8_GrPixelConfig == srcTexture->config());
174
bsalomonf2703d82014-10-28 14:33:06 -0700175 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800176 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000177 desc.fWidth = SkScalarFloorToInt(srcRect.width());
178 desc.fHeight = SkScalarFloorToInt(srcRect.height());
179 desc.fConfig = srcTexture->config();
180
bsalomone3059732014-10-14 11:47:22 -0700181 GrTexture* dstTexture;
182 GrTexture* tempTexture;
183 SkAutoTUnref<GrTexture> temp1, temp2;
184
bsalomoneae62002015-07-31 13:59:30 -0700185 temp1.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700186 dstTexture = temp1.get();
187 if (canClobberSrc) {
188 tempTexture = srcTexture;
189 } else {
bsalomoneae62002015-07-31 13:59:30 -0700190 temp2.reset(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700191 tempTexture = temp2.get();
192 }
193
halcanary96fcdcc2015-08-27 07:41:13 -0700194 if (nullptr == dstTexture || nullptr == tempTexture) {
195 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000196 }
197
robertphillipsc9a37062015-09-01 08:34:28 -0700198 SkAutoTUnref<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700199
robertphillips@google.com736dd032013-07-15 15:06:54 +0000200 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
201 GrPaint paint;
202 SkMatrix matrix;
203 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000204 SkRect dstRect(srcRect);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000205 if (cropToRect && i == 1) {
206 dstRect.offset(-dstRect.fLeft, -dstRect.fTop);
207 SkRect domain;
208 matrix.mapRect(&domain, rect);
209 domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f,
210 i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bsalomon4a339522015-10-06 08:40:50 -0700211 SkAutoTUnref<GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000212 srcTexture,
213 matrix,
214 domain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000215 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000216 GrTextureParams::kBilerp_FilterMode));
bsalomonac856c92015-08-27 06:30:17 -0700217 paint.addColorFragmentProcessor(fp);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000218 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000219 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700220 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000221 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000222 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
223 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700224
robertphillipsc9a37062015-09-01 08:34:28 -0700225 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700226 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700227 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700228 }
229 dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
230 SkMatrix::I(), dstRect, srcRect);
231
bungeman77a53de2015-10-01 12:28:49 -0700232 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000233 srcRect = dstRect;
234 srcTexture = dstTexture;
235 SkTSwap(dstTexture, tempTexture);
236 }
237
reedb07a94f2014-11-19 05:03:18 -0800238 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000239
robertphillipsff0ca5e2015-07-22 11:54:44 -0700240 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
joshualitt5acfea72014-08-11 13:55:34 -0700241 // launch a single non separable kernel vs two launches
robertphillipsff0ca5e2015-07-22 11:54:44 -0700242 if (sigmaX > 0.0f && sigmaY > 0.0f &&
joshualitt5acfea72014-08-11 13:55:34 -0700243 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
244 // We shouldn't be scaling because this is a small size blur
robertphillipsff0ca5e2015-07-22 11:54:44 -0700245 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000246 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700247
robertphillipsc9a37062015-09-01 08:34:28 -0700248 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700249 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700251 }
252 convolve_gaussian_2d(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800253 srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700254
bungeman77a53de2015-10-01 12:28:49 -0700255 srcDrawContext.swap(dstDrawContext);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000256 srcRect = dstRect;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700257 srcTexture = dstTexture;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000258 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000259
joshualitt5acfea72014-08-11 13:55:34 -0700260 } else {
261 if (sigmaX > 0.0f) {
262 if (scaleFactorX > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700263 // TODO: if we pass in the source draw context we don't need this here
264 if (!srcDrawContext) {
robertphillipsc9a37062015-09-01 08:34:28 -0700265 srcDrawContext.reset(context->drawContext());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700266 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700267 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700268 }
269 }
270
joshualitt5acfea72014-08-11 13:55:34 -0700271 // Clear out a radius to the right of the srcRect to prevent the
272 // X convolution from reading garbage.
273 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
274 radiusX, srcIRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700275 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700276 }
joshualitt5acfea72014-08-11 13:55:34 -0700277 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700278
robertphillipsc9a37062015-09-01 08:34:28 -0700279 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
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 }
283 convolve_gaussian(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800284 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
285 cropToRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700286
bungeman77a53de2015-10-01 12:28:49 -0700287 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700288 srcTexture = dstTexture;
289 srcRect = dstRect;
290 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000291 }
292
joshualitt5acfea72014-08-11 13:55:34 -0700293 if (sigmaY > 0.0f) {
294 if (scaleFactorY > 1 || sigmaX > 0.0f) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700295 // TODO: if we pass in the source draw context we don't need this here
296 if (!srcDrawContext) {
robertphillipsc9a37062015-09-01 08:34:28 -0700297 srcDrawContext.reset(context->drawContext());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700298 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700299 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700300 }
301 }
302
joshualitt5acfea72014-08-11 13:55:34 -0700303 // Clear out a radius below the srcRect to prevent the Y
304 // convolution from reading garbage.
305 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
306 srcIRect.width(), radiusY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700307 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700308 }
309
joshualitt5acfea72014-08-11 13:55:34 -0700310 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700311
robertphillipsc9a37062015-09-01 08:34:28 -0700312 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
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 }
316 convolve_gaussian(dstDrawContext, dstTexture->asRenderTarget(), 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);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700334 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000335 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
336 1, srcIRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700337 srcDrawContext->clear(srcTexture->asRenderTarget(), &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
robertphillipsc9a37062015-09-01 08:34:28 -0700349 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700350 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700351 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700352 }
353 dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
354 SkMatrix::I(), dstRect, srcRect);
355
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}