blob: e3463a4b7856a166dc70f087ba8ce538d4e96d33 [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"
Brian Osman11052242016-10-27 14:47:55 -040017#include "GrRenderTargetContext.h"
Robert Phillips784b7bf2016-12-09 13:35:02 -050018#include "GrRenderTargetContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070019#include "GrFixedClip.h"
robertphillips@google.com736dd032013-07-15 15:06:54 +000020
21#define MAX_BLUR_SIGMA 4.0f
22
robertphillipsf054b172016-05-13 05:06:19 -070023static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) {
24 rect->fLeft = SkScalarFloorToInt(SkScalarMul(rect->fLeft, xScale));
25 rect->fTop = SkScalarFloorToInt(SkScalarMul(rect->fTop, yScale));
26 rect->fRight = SkScalarCeilToInt(SkScalarMul(rect->fRight, xScale));
27 rect->fBottom = SkScalarCeilToInt(SkScalarMul(rect->fBottom, yScale));
28}
29
30static void scale_irect(SkIRect* rect, int xScale, int yScale) {
31 rect->fLeft *= xScale;
32 rect->fTop *= yScale;
33 rect->fRight *= xScale;
34 rect->fBottom *= yScale;
35}
36
37#ifdef SK_DEBUG
38static inline int is_even(int x) { return !(x & 1); }
39#endif
40
41static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) {
42 if (xAxis) {
43 SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight));
44 rect->fLeft /= 2;
45 rect->fRight /= 2;
46 }
47 if (yAxis) {
48 SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom));
49 rect->fTop /= 2;
50 rect->fBottom /= 2;
51 }
robertphillips@google.com736dd032013-07-15 15:06:54 +000052}
53
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +000054static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000055 *scaleFactor = 1;
56 while (sigma > MAX_BLUR_SIGMA) {
57 *scaleFactor *= 2;
58 sigma *= 0.5f;
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +000059 if (*scaleFactor > maxTextureSize) {
60 *scaleFactor = maxTextureSize;
61 sigma = MAX_BLUR_SIGMA;
62 }
robertphillips@google.com736dd032013-07-15 15:06:54 +000063 }
64 *radius = static_cast<int>(ceilf(sigma * 3.0f));
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +000065 SkASSERT(*radius <= GrConvolutionEffect::kMaxKernelRadius);
robertphillips@google.com736dd032013-07-15 15:06:54 +000066 return sigma;
67}
68
Brian Osman11052242016-10-27 14:47:55 -040069static void convolve_gaussian_1d(GrRenderTargetContext* renderTargetContext,
joshualitt570d2f82015-02-25 13:19:48 -080070 const GrClip& clip,
robertphillipsf054b172016-05-13 05:06:19 -070071 const SkIRect& dstRect,
72 const SkIPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070073 GrTexture* texture,
74 Gr1DKernelEffect::Direction direction,
75 int radius,
76 float sigma,
77 bool useBounds,
78 float bounds[2]) {
robertphillips@google.com736dd032013-07-15 15:06:54 +000079 GrPaint paint;
Brian Osman11052242016-10-27 14:47:55 -040080 paint.setGammaCorrect(renderTargetContext->isGammaCorrect());
bungeman06ca8ec2016-06-09 08:01:03 -070081 sk_sp<GrFragmentProcessor> conv(GrConvolutionEffect::MakeGaussian(
bsalomon4a339522015-10-06 08:40:50 -070082 texture, direction, radius, sigma, useBounds, bounds));
bungeman06ca8ec2016-06-09 08:01:03 -070083 paint.addColorFragmentProcessor(std::move(conv));
reed374772b2016-10-05 17:33:02 -070084 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
robertphillipsf054b172016-05-13 05:06:19 -070085 SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
86 -SkIntToScalar(srcOffset.y()));
Brian Salomon39444842016-12-09 14:06:38 -050087 renderTargetContext->fillRectWithLocalMatrix(clip, paint, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -040088 SkRect::Make(dstRect), localMatrix);
robertphillips@google.com736dd032013-07-15 15:06:54 +000089}
90
Brian Osman11052242016-10-27 14:47:55 -040091static void convolve_gaussian_2d(GrRenderTargetContext* renderTargetContext,
joshualitt570d2f82015-02-25 13:19:48 -080092 const GrClip& clip,
robertphillipsf054b172016-05-13 05:06:19 -070093 const SkIRect& dstRect,
94 const SkIPoint& srcOffset,
joshualitt5acfea72014-08-11 13:55:34 -070095 GrTexture* texture,
96 int radiusX,
97 int radiusY,
98 SkScalar sigmaX,
99 SkScalar sigmaY,
robertphillipsf054b172016-05-13 05:06:19 -0700100 const SkIRect* srcBounds) {
101 SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
102 -SkIntToScalar(srcOffset.y()));
joshualitt5acfea72014-08-11 13:55:34 -0700103 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
104 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
105 GrPaint paint;
Brian Osman11052242016-10-27 14:47:55 -0400106 paint.setGammaCorrect(renderTargetContext->isGammaCorrect());
robertphillipsf054b172016-05-13 05:06:19 -0700107 SkIRect bounds = srcBounds ? *srcBounds : SkIRect::EmptyIRect();
senorblanco07d56b12015-11-10 07:32:37 -0800108
bungeman06ca8ec2016-06-09 08:01:03 -0700109 sk_sp<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::MakeGaussian(
joshualitt5acfea72014-08-11 13:55:34 -0700110 texture, bounds, size, 1.0, 0.0, kernelOffset,
senorblanco07d56b12015-11-10 07:32:37 -0800111 srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_Mode,
joshualitt5acfea72014-08-11 13:55:34 -0700112 true, sigmaX, sigmaY));
bungeman06ca8ec2016-06-09 08:01:03 -0700113 paint.addColorFragmentProcessor(std::move(conv));
reed374772b2016-10-05 17:33:02 -0700114 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon39444842016-12-09 14:06:38 -0500115 renderTargetContext->fillRectWithLocalMatrix(clip, paint, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -0400116 SkRect::Make(dstRect), localMatrix);
joshualitt5acfea72014-08-11 13:55:34 -0700117}
118
Brian Osman11052242016-10-27 14:47:55 -0400119static void convolve_gaussian(GrRenderTargetContext* renderTargetContext,
joshualitt570d2f82015-02-25 13:19:48 -0800120 const GrClip& clip,
robertphillipsf054b172016-05-13 05:06:19 -0700121 const SkIRect& srcRect,
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000122 GrTexture* texture,
123 Gr1DKernelEffect::Direction direction,
124 int radius,
125 float sigma,
robertphillipsf054b172016-05-13 05:06:19 -0700126 const SkIRect* srcBounds,
127 const SkIPoint& srcOffset) {
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000128 float bounds[2] = { 0.0f, 1.0f };
robertphillipsf054b172016-05-13 05:06:19 -0700129 SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height());
senorblanco07d56b12015-11-10 07:32:37 -0800130 if (!srcBounds) {
Brian Osman11052242016-10-27 14:47:55 -0400131 convolve_gaussian_1d(renderTargetContext, clip, dstRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700132 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000133 return;
134 }
robertphillipsf054b172016-05-13 05:06:19 -0700135 SkIRect midRect = *srcBounds, leftRect, rightRect;
senorblanco07d56b12015-11-10 07:32:37 -0800136 midRect.offset(srcOffset);
137 SkIRect topRect, bottomRect;
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000138 if (direction == Gr1DKernelEffect::kX_Direction) {
robertphillipsf054b172016-05-13 05:06:19 -0700139 bounds[0] = SkIntToFloat(srcBounds->left()) / texture->width();
140 bounds[1] = SkIntToFloat(srcBounds->right()) / texture->width();
141 topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top());
142 bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom());
143 midRect.inset(radius, 0);
144 leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom());
senorblanco07d56b12015-11-10 07:32:37 -0800145 rightRect =
robertphillipsf054b172016-05-13 05:06:19 -0700146 SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom());
senorblanco07d56b12015-11-10 07:32:37 -0800147 dstRect.fTop = midRect.top();
148 dstRect.fBottom = midRect.bottom();
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000149 } else {
robertphillipsf054b172016-05-13 05:06:19 -0700150 bounds[0] = SkIntToFloat(srcBounds->top()) / texture->height();
151 bounds[1] = SkIntToFloat(srcBounds->bottom()) / texture->height();
152 topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom());
153 bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom());
154 midRect.inset(0, radius);
155 leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top());
senorblanco07d56b12015-11-10 07:32:37 -0800156 rightRect =
robertphillipsf054b172016-05-13 05:06:19 -0700157 SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height());
senorblanco07d56b12015-11-10 07:32:37 -0800158 dstRect.fLeft = midRect.left();
159 dstRect.fRight = midRect.right();
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000160 }
senorblanco07d56b12015-11-10 07:32:37 -0800161 if (!topRect.isEmpty()) {
Brian Osman11052242016-10-27 14:47:55 -0400162 renderTargetContext->clear(&topRect, 0, false);
senorblanco07d56b12015-11-10 07:32:37 -0800163 }
164
165 if (!bottomRect.isEmpty()) {
Brian Osman11052242016-10-27 14:47:55 -0400166 renderTargetContext->clear(&bottomRect, 0, false);
senorblanco07d56b12015-11-10 07:32:37 -0800167 }
168 if (midRect.isEmpty()) {
169 // Blur radius covers srcBounds; use bounds over entire draw
Brian Osman11052242016-10-27 14:47:55 -0400170 convolve_gaussian_1d(renderTargetContext, clip, dstRect, srcOffset, texture,
robertphillipsf054b172016-05-13 05:06:19 -0700171 direction, radius, sigma, true, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000172 } else {
senorblanco07d56b12015-11-10 07:32:37 -0800173 // Draw right and left margins with bounds; middle without.
Brian Osman11052242016-10-27 14:47:55 -0400174 convolve_gaussian_1d(renderTargetContext, clip, leftRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700175 direction, radius, sigma, true, bounds);
Brian Osman11052242016-10-27 14:47:55 -0400176 convolve_gaussian_1d(renderTargetContext, clip, rightRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700177 direction, radius, sigma, true, bounds);
Brian Osman11052242016-10-27 14:47:55 -0400178 convolve_gaussian_1d(renderTargetContext, clip, midRect, srcOffset, texture,
joshualitt5acfea72014-08-11 13:55:34 -0700179 direction, radius, sigma, false, bounds);
senorblanco@chromium.orgd71732a2013-07-30 21:11:05 +0000180 }
181}
182
robertphillipsf054b172016-05-13 05:06:19 -0700183namespace SkGpuBlurUtils {
184
Brian Osman11052242016-10-27 14:47:55 -0400185sk_sp<GrRenderTargetContext> GaussianBlur(GrContext* context,
186 GrTexture* origSrc,
187 sk_sp<SkColorSpace> colorSpace,
188 const SkIRect& dstBounds,
189 const SkIRect* srcBounds,
190 float sigmaX,
191 float sigmaY,
192 SkBackingFit fit) {
bsalomon49f085d2014-09-05 13:34:00 -0700193 SkASSERT(context);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000194 SkIRect clearRect;
195 int scaleFactorX, radiusX;
196 int scaleFactorY, radiusY;
bsalomon76228632015-05-29 08:02:10 -0700197 int maxTextureSize = context->caps()->maxTextureSize();
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000198 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
199 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
robertphillipsf054b172016-05-13 05:06:19 -0700200 SkASSERT(sigmaX || sigmaY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000201
robertphillipsf054b172016-05-13 05:06:19 -0700202 SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y());
203 SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height());
204 SkIRect localSrcBounds;
205 SkIRect srcRect;
senorblanco07d56b12015-11-10 07:32:37 -0800206 if (srcBounds) {
207 srcRect = localSrcBounds = *srcBounds;
208 srcRect.offset(srcOffset);
209 srcBounds = &localSrcBounds;
210 } else {
211 srcRect = localDstBounds;
212 }
213
robertphillipsf054b172016-05-13 05:06:19 -0700214 scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
215 scale_irect(&srcRect, scaleFactorX, scaleFactorY);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000216
joshualitt570d2f82015-02-25 13:19:48 -0800217 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700218 GrFixedClip clip(localDstBounds);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000219
robertphillips04c84af2016-05-12 11:13:00 -0700220 sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc));
221
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000222 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000223 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
brianosmana6359362016-03-21 06:55:37 -0700224 kSRGBA_8888_GrPixelConfig == srcTexture->config() ||
225 kSBGRA_8888_GrPixelConfig == srcTexture->config() ||
brianosman950fc6c2016-09-19 06:59:28 -0700226 kRGBA_half_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com736dd032013-07-15 15:06:54 +0000227 kAlpha_8_GrPixelConfig == srcTexture->config());
228
robertphillipsf054b172016-05-13 05:06:19 -0700229 const int width = dstBounds.width();
230 const int height = dstBounds.height();
robertphillipsa8966a82016-05-09 06:45:37 -0700231 const GrPixelConfig config = srcTexture->config();
232
robertphillipsd728f0c2016-11-21 11:05:03 -0800233 sk_sp<GrRenderTargetContext> dstRenderTargetContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400234 fit, width, height, config, colorSpace, 0, kDefault_GrSurfaceOrigin));
235 if (!dstRenderTargetContext) {
robertphillips04c84af2016-05-12 11:13:00 -0700236 return nullptr;
237 }
238
robertphillipsa8966a82016-05-09 06:45:37 -0700239 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
240 // launch a single non separable kernel vs two launches
241 if (sigmaX > 0.0f && sigmaY > 0.0f &&
242 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
243 // We shouldn't be scaling because this is a small size blur
244 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
245
Brian Osman11052242016-10-27 14:47:55 -0400246 convolve_gaussian_2d(dstRenderTargetContext.get(), clip, localDstBounds, srcOffset,
robertphillips04c84af2016-05-12 11:13:00 -0700247 srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds);
robertphillipsa8966a82016-05-09 06:45:37 -0700248
Brian Osman11052242016-10-27 14:47:55 -0400249 return dstRenderTargetContext;
robertphillipsa8966a82016-05-09 06:45:37 -0700250 }
251
robertphillipsd728f0c2016-11-21 11:05:03 -0800252 sk_sp<GrRenderTargetContext> tmpRenderTargetContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400253 fit, width, height, config, colorSpace, 0, kDefault_GrSurfaceOrigin));
254 if (!tmpRenderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700255 return nullptr;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000256 }
257
Brian Osman11052242016-10-27 14:47:55 -0400258 sk_sp<GrRenderTargetContext> srcRenderTargetContext;
robertphillipsea461502015-05-26 11:38:03 -0700259
robertphillipsf054b172016-05-13 05:06:19 -0700260 SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY));
261
robertphillips@google.com736dd032013-07-15 15:06:54 +0000262 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
263 GrPaint paint;
Brian Osman11052242016-10-27 14:47:55 -0400264 paint.setGammaCorrect(dstRenderTargetContext->isGammaCorrect());
robertphillipsd728f0c2016-11-21 11:05:03 -0800265 // TODO: this matrix relies on the final instantiated size of the texture. This
266 // will have to be deferred for TextureProxys
robertphillips@google.com736dd032013-07-15 15:06:54 +0000267 SkMatrix matrix;
268 matrix.setIDiv(srcTexture->width(), srcTexture->height());
robertphillipsf054b172016-05-13 05:06:19 -0700269 SkIRect dstRect(srcRect);
senorblanco07d56b12015-11-10 07:32:37 -0800270 if (srcBounds && i == 1) {
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000271 SkRect domain;
robertphillipsf054b172016-05-13 05:06:19 -0700272 matrix.mapRect(&domain, SkRect::Make(*srcBounds));
senorblanco07d56b12015-11-10 07:32:37 -0800273 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f,
274 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f);
bungeman06ca8ec2016-06-09 08:01:03 -0700275 sk_sp<GrFragmentProcessor> fp(GrTextureDomainEffect::Make(
robertphillips04c84af2016-05-12 11:13:00 -0700276 srcTexture.get(),
brianosman54f30c12016-07-18 10:53:52 -0700277 nullptr,
robertphillipsa8966a82016-05-09 06:45:37 -0700278 matrix,
279 domain,
280 GrTextureDomain::kDecal_Mode,
Brian Salomon514baff2016-11-17 15:17:07 -0500281 GrSamplerParams::kBilerp_FilterMode));
bungeman06ca8ec2016-06-09 08:01:03 -0700282 paint.addColorFragmentProcessor(std::move(fp));
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 {
Brian Salomon514baff2016-11-17 15:17:07 -0500286 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
brianosman54f30c12016-07-18 10:53:52 -0700287 paint.addColorTextureProcessor(srcTexture.get(), nullptr, matrix, params);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000288 }
reed374772b2016-10-05 17:33:02 -0700289 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
robertphillipsf054b172016-05-13 05:06:19 -0700290 shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700291
Brian Salomon39444842016-12-09 14:06:38 -0500292 dstRenderTargetContext->fillRectToRect(clip, paint, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -0400293 SkRect::Make(dstRect), SkRect::Make(srcRect));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700294
Brian Osman11052242016-10-27 14:47:55 -0400295 srcRenderTargetContext = dstRenderTargetContext;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000296 srcRect = dstRect;
Brian Osman11052242016-10-27 14:47:55 -0400297 srcTexture = srcRenderTargetContext->asTexture();
robertphillipsd728f0c2016-11-21 11:05:03 -0800298 if (!srcTexture) {
299 return nullptr;
300 }
Brian Osman11052242016-10-27 14:47:55 -0400301 dstRenderTargetContext.swap(tmpRenderTargetContext);
senorblanco07d56b12015-11-10 07:32:37 -0800302 localSrcBounds = srcRect;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000303 }
304
senorblancoc834ab12015-12-17 08:10:17 -0800305 srcRect = localDstBounds;
robertphillipsf054b172016-05-13 05:06:19 -0700306 scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
robertphillipsa8966a82016-05-09 06:45:37 -0700307 if (sigmaX > 0.0f) {
308 if (scaleFactorX > 1) {
Brian Osman11052242016-10-27 14:47:55 -0400309 SkASSERT(srcRenderTargetContext);
robertphillipsa8966a82016-05-09 06:45:37 -0700310
311 // Clear out a radius to the right of the srcRect to prevent the
312 // X convolution from reading garbage.
robertphillipsf054b172016-05-13 05:06:19 -0700313 clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop,
314 radiusX, srcRect.height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500315 srcRenderTargetContext->priv().absClear(&clearRect, 0x0);
robertphillipsa8966a82016-05-09 06:45:37 -0700316 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700317
Brian Osman11052242016-10-27 14:47:55 -0400318 convolve_gaussian(dstRenderTargetContext.get(), clip, srcRect,
robertphillips04c84af2016-05-12 11:13:00 -0700319 srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
robertphillipsa8966a82016-05-09 06:45:37 -0700320 srcBounds, srcOffset);
Brian Osman11052242016-10-27 14:47:55 -0400321 srcRenderTargetContext = dstRenderTargetContext;
322 srcTexture = srcRenderTargetContext->asTexture();
robertphillipsd728f0c2016-11-21 11:05:03 -0800323 if (!srcTexture) {
324 return nullptr;
325 }
robertphillipsa8966a82016-05-09 06:45:37 -0700326 srcRect.offsetTo(0, 0);
Brian Osman11052242016-10-27 14:47:55 -0400327 dstRenderTargetContext.swap(tmpRenderTargetContext);
robertphillipsa8966a82016-05-09 06:45:37 -0700328 localSrcBounds = srcRect;
329 srcOffset.set(0, 0);
330 }
331
332 if (sigmaY > 0.0f) {
333 if (scaleFactorY > 1 || sigmaX > 0.0f) {
Brian Osman11052242016-10-27 14:47:55 -0400334 SkASSERT(srcRenderTargetContext);
robertphillipsa8966a82016-05-09 06:45:37 -0700335
336 // Clear out a radius below the srcRect to prevent the Y
337 // convolution from reading garbage.
robertphillipsf054b172016-05-13 05:06:19 -0700338 clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom,
339 srcRect.width(), radiusY);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500340 srcRenderTargetContext->priv().absClear(&clearRect, 0x0);
robertphillipsa8966a82016-05-09 06:45:37 -0700341 }
342
Brian Osman11052242016-10-27 14:47:55 -0400343 convolve_gaussian(dstRenderTargetContext.get(), clip, srcRect,
robertphillips04c84af2016-05-12 11:13:00 -0700344 srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
robertphillipsa8966a82016-05-09 06:45:37 -0700345 srcBounds, srcOffset);
robertphillips56a85e62016-05-06 07:17:49 -0700346
Brian Osman11052242016-10-27 14:47:55 -0400347 srcRenderTargetContext = dstRenderTargetContext;
robertphillipsa8966a82016-05-09 06:45:37 -0700348 srcRect.offsetTo(0, 0);
Brian Osman11052242016-10-27 14:47:55 -0400349 dstRenderTargetContext.swap(tmpRenderTargetContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000350 }
robertphillipsa8966a82016-05-09 06:45:37 -0700351
Brian Osman11052242016-10-27 14:47:55 -0400352 SkASSERT(srcRenderTargetContext);
robertphillips04c84af2016-05-12 11:13:00 -0700353 srcTexture = nullptr; // we don't use this from here on out
robertphillips@google.com736dd032013-07-15 15:06:54 +0000354
355 if (scaleFactorX > 1 || scaleFactorY > 1) {
robertphillips04c84af2016-05-12 11:13:00 -0700356 // Clear one pixel to the right and below, to accommodate bilinear upsampling.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500357 // TODO: it seems like we should actually be clamping here rather than darkening
358 // the bottom right edges.
robertphillipsf054b172016-05-13 05:06:19 -0700359 clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500360 srcRenderTargetContext->priv().absClear(&clearRect, 0x0);
robertphillipsf054b172016-05-13 05:06:19 -0700361 clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500362 srcRenderTargetContext->priv().absClear(&clearRect, 0x0);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700363
robertphillips@google.com736dd032013-07-15 15:06:54 +0000364 GrPaint paint;
Brian Osman11052242016-10-27 14:47:55 -0400365 paint.setGammaCorrect(dstRenderTargetContext->isGammaCorrect());
robertphillips@google.com736dd032013-07-15 15:06:54 +0000366 // FIXME: this should be mitchell, not bilinear.
Brian Salomon514baff2016-11-17 15:17:07 -0500367 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
Brian Osman11052242016-10-27 14:47:55 -0400368 sk_sp<GrTexture> tex(srcRenderTargetContext->asTexture());
robertphillipsd728f0c2016-11-21 11:05:03 -0800369 if (!tex) {
370 return nullptr;
371 }
372
373 // TODO: this matrix relies on the final instantiated size of the texture. This
374 // will have to be deferred for TextureProxys
375 SkMatrix matrix;
376 matrix.setIDiv(tex->width(), tex->height());
377
brianosman54f30c12016-07-18 10:53:52 -0700378 paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params);
reed374772b2016-10-05 17:33:02 -0700379 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000380
robertphillipsf054b172016-05-13 05:06:19 -0700381 SkIRect dstRect(srcRect);
382 scale_irect(&dstRect, scaleFactorX, scaleFactorY);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700383
Brian Salomon39444842016-12-09 14:06:38 -0500384 dstRenderTargetContext->fillRectToRect(clip, paint, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -0400385 SkRect::Make(dstRect), SkRect::Make(srcRect));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700386
Brian Osman11052242016-10-27 14:47:55 -0400387 srcRenderTargetContext = dstRenderTargetContext;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000388 srcRect = dstRect;
Brian Osman11052242016-10-27 14:47:55 -0400389 dstRenderTargetContext.swap(tmpRenderTargetContext);
robertphillips@google.com736dd032013-07-15 15:06:54 +0000390 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700391
Brian Osman11052242016-10-27 14:47:55 -0400392 return srcRenderTargetContext;
robertphillips@google.com736dd032013-07-15 15:06:54 +0000393}
robertphillips@google.com736dd032013-07-15 15:06:54 +0000394
robertphillips@google.comcce41022013-07-15 15:47:10 +0000395}
robertphillipsf054b172016-05-13 05:06:19 -0700396
397#endif
398