blob: 3437ab0d0198b6499d11eb42af9b9b304510a3cd [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,
reedc9b5f8b2015-10-22 13:20:20 -0700149 float sigmaY,
150 GrTextureProvider::SizeConstraint constraint) {
bsalomon49f085d2014-09-05 13:34:00 -0700151 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000152
robertphillips@google.com736dd032013-07-15 15:06:54 +0000153 SkIRect clearRect;
154 int scaleFactorX, radiusX;
155 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700156 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000157 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
158 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000159
160 SkRect srcRect(rect);
161 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
reedd02cf262014-11-18 18:06:45 -0800162 srcRect.roundOut(&srcRect);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000163 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
164 static_cast<float>(scaleFactorY));
165
joshualitt570d2f82015-02-25 13:19:48 -0800166 // setup new clip
167 GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height()));
robertphillips@google.com736dd032013-07-15 15:06:54 +0000168
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000169 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000170 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
171 kAlpha_8_GrPixelConfig == srcTexture->config());
172
bsalomonf2703d82014-10-28 14:33:06 -0700173 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800174 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000175 desc.fWidth = SkScalarFloorToInt(srcRect.width());
176 desc.fHeight = SkScalarFloorToInt(srcRect.height());
177 desc.fConfig = srcTexture->config();
178
bsalomone3059732014-10-14 11:47:22 -0700179 GrTexture* dstTexture;
180 GrTexture* tempTexture;
181 SkAutoTUnref<GrTexture> temp1, temp2;
182
reedc9b5f8b2015-10-22 13:20:20 -0700183 temp1.reset(context->textureProvider()->createTexture(desc, constraint));
bsalomone3059732014-10-14 11:47:22 -0700184 dstTexture = temp1.get();
185 if (canClobberSrc) {
186 tempTexture = srcTexture;
187 } else {
reedc9b5f8b2015-10-22 13:20:20 -0700188 temp2.reset(context->textureProvider()->createTexture(desc, constraint));
bsalomone3059732014-10-14 11:47:22 -0700189 tempTexture = temp2.get();
190 }
191
halcanary96fcdcc2015-08-27 07:41:13 -0700192 if (nullptr == dstTexture || nullptr == tempTexture) {
193 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000194 }
195
robertphillipsc9a37062015-09-01 08:34:28 -0700196 SkAutoTUnref<GrDrawContext> srcDrawContext;
robertphillipsea461502015-05-26 11:38:03 -0700197
robertphillips@google.com736dd032013-07-15 15:06:54 +0000198 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
199 GrPaint paint;
200 SkMatrix matrix;
201 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000202 SkRect dstRect(srcRect);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000203 if (cropToRect && i == 1) {
204 dstRect.offset(-dstRect.fLeft, -dstRect.fTop);
205 SkRect domain;
206 matrix.mapRect(&domain, rect);
207 domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f,
208 i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bsalomon0ba8c242015-10-07 09:20:28 -0700209 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000210 srcTexture,
211 matrix,
212 domain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000213 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000214 GrTextureParams::kBilerp_FilterMode));
bsalomonac856c92015-08-27 06:30:17 -0700215 paint.addColorFragmentProcessor(fp);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000216 } else {
humper@google.comb86add12013-07-25 18:49:07 +0000217 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700218 paint.addColorTextureProcessor(srcTexture, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000219 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000220 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
221 i < scaleFactorY ? 0.5f : 1.0f);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700222
robertphillips2e1e51f2015-10-15 08:01:48 -0700223 SkAutoTUnref<GrDrawContext> dstDrawContext(
224 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700225 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700226 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700227 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700228 dstDrawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700229
bungeman77a53de2015-10-01 12:28:49 -0700230 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000231 srcRect = dstRect;
232 srcTexture = dstTexture;
233 SkTSwap(dstTexture, tempTexture);
234 }
235
reedb07a94f2014-11-19 05:03:18 -0800236 const SkIRect srcIRect = srcRect.roundOut();
robertphillips@google.com736dd032013-07-15 15:06:54 +0000237
robertphillipsff0ca5e2015-07-22 11:54:44 -0700238 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
joshualitt5acfea72014-08-11 13:55:34 -0700239 // launch a single non separable kernel vs two launches
robertphillipsff0ca5e2015-07-22 11:54:44 -0700240 if (sigmaX > 0.0f && sigmaY > 0.0f &&
joshualitt5acfea72014-08-11 13:55:34 -0700241 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
242 // We shouldn't be scaling because this is a small size blur
robertphillipsff0ca5e2015-07-22 11:54:44 -0700243 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000244 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700245
robertphillips2e1e51f2015-10-15 08:01:48 -0700246 SkAutoTUnref<GrDrawContext> dstDrawContext(
247 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700248 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700250 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700251 convolve_gaussian_2d(dstDrawContext, clip, srcRect, dstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800252 srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700253
bungeman77a53de2015-10-01 12:28:49 -0700254 srcDrawContext.swap(dstDrawContext);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000255 srcRect = dstRect;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700256 srcTexture = dstTexture;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000257 SkTSwap(dstTexture, tempTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000258
joshualitt5acfea72014-08-11 13:55:34 -0700259 } else {
260 if (sigmaX > 0.0f) {
261 if (scaleFactorX > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700262 // TODO: if we pass in the source draw context we don't need this here
263 if (!srcDrawContext) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700264 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700265 if (!srcDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700266 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700267 }
268 }
269
joshualitt5acfea72014-08-11 13:55:34 -0700270 // Clear out a radius to the right of the srcRect to prevent the
271 // X convolution from reading garbage.
272 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
273 radiusX, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700274 srcDrawContext->clear(&clearRect, 0x0, false);
joshualitt5acfea72014-08-11 13:55:34 -0700275 }
joshualitt5acfea72014-08-11 13:55:34 -0700276 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
robertphillipsff0ca5e2015-07-22 11:54:44 -0700277
robertphillips2e1e51f2015-10-15 08:01:48 -0700278 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700279 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 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700283 convolve_gaussian(dstDrawContext, 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) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700297 srcDrawContext.reset(context->drawContext(srcTexture->asRenderTarget()));
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);
robertphillips2e1e51f2015-10-15 08:01:48 -0700307 srcDrawContext->clear(&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
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 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700317 convolve_gaussian(dstDrawContext, clip, srcRect,
joshualitt570d2f82015-02-25 13:19:48 -0800318 dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
319 cropToRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700320
bungeman77a53de2015-10-01 12:28:49 -0700321 srcDrawContext.swap(dstDrawContext);
joshualitt5acfea72014-08-11 13:55:34 -0700322 srcTexture = dstTexture;
323 srcRect = dstRect;
324 SkTSwap(dstTexture, tempTexture);
325 }
robertphillips@google.com736dd032013-07-15 15:06:54 +0000326 }
327
328 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700329 SkASSERT(srcDrawContext);
330
robertphillips@google.com736dd032013-07-15 15:06:54 +0000331 // Clear one pixel to the right and below, to accommodate bilinear
332 // upsampling.
333 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
334 srcIRect.width() + 1, 1);
robertphillips2e1e51f2015-10-15 08:01:48 -0700335 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000336 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
337 1, srcIRect.height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700338 srcDrawContext->clear(&clearRect, 0x0, false);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000339 SkMatrix matrix;
340 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000341
342 GrPaint paint;
343 // FIXME: this should be mitchell, not bilinear.
humper@google.comb86add12013-07-25 18:49:07 +0000344 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700345 paint.addColorTextureProcessor(srcTexture, matrix, params);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000346
347 SkRect dstRect(srcRect);
348 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700349
robertphillips2e1e51f2015-10-15 08:01:48 -0700350 SkAutoTUnref<GrDrawContext> dstDrawContext(
robertphillips77a2e522015-10-17 07:43:27 -0700351 context->drawContext(dstTexture->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700352 if (!dstDrawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700353 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700354 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700355 dstDrawContext->drawNonAARectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700356
bungeman77a53de2015-10-01 12:28:49 -0700357 srcDrawContext.swap(dstDrawContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000358 srcRect = dstRect;
359 srcTexture = dstTexture;
360 SkTSwap(dstTexture, tempTexture);
361 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700362
bsalomone3059732014-10-14 11:47:22 -0700363 return SkRef(srcTexture);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000364}
365#endif
366
robertphillips@google.comcce41022013-07-15 15:47:10 +0000367}