OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkGpuBlurUtils.h" | 8 #include "SkGpuBlurUtils.h" |
9 | 9 |
10 #include "SkRect.h" | 10 #include "SkRect.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 const GrClip& clip, | 49 const GrClip& clip, |
50 const SkRect& dstRect, | 50 const SkRect& dstRect, |
51 const SkPoint& srcOffset, | 51 const SkPoint& srcOffset, |
52 GrTexture* texture, | 52 GrTexture* texture, |
53 Gr1DKernelEffect::Direction direction, | 53 Gr1DKernelEffect::Direction direction, |
54 int radius, | 54 int radius, |
55 float sigma, | 55 float sigma, |
56 bool useBounds, | 56 bool useBounds, |
57 float bounds[2]) { | 57 float bounds[2]) { |
58 GrPaint paint; | 58 GrPaint paint; |
59 paint.setAllowSRGBInputs(drawContext->allowSRGBInputs()); | |
59 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian( | 60 SkAutoTUnref<GrFragmentProcessor> conv(GrConvolutionEffect::CreateGaussian( |
60 texture, direction, radius, sigma, useBounds, bounds)); | 61 texture, direction, radius, sigma, useBounds, bounds)); |
61 paint.addColorFragmentProcessor(conv); | 62 paint.addColorFragmentProcessor(conv); |
62 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 63 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
63 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y()); | 64 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y()); |
64 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, lo calMatrix); | 65 drawContext->fillRectWithLocalMatrix(clip, paint, SkMatrix::I(), dstRect, lo calMatrix); |
65 } | 66 } |
66 | 67 |
67 static void convolve_gaussian_2d(GrDrawContext* drawContext, | 68 static void convolve_gaussian_2d(GrDrawContext* drawContext, |
68 const GrClip& clip, | 69 const GrClip& clip, |
69 const SkRect& dstRect, | 70 const SkRect& dstRect, |
70 const SkPoint& srcOffset, | 71 const SkPoint& srcOffset, |
71 GrTexture* texture, | 72 GrTexture* texture, |
72 int radiusX, | 73 int radiusX, |
73 int radiusY, | 74 int radiusY, |
74 SkScalar sigmaX, | 75 SkScalar sigmaX, |
75 SkScalar sigmaY, | 76 SkScalar sigmaY, |
76 const SkRect* srcBounds) { | 77 const SkRect* srcBounds) { |
77 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y()); | 78 SkMatrix localMatrix = SkMatrix::MakeTrans(-srcOffset.x(), -srcOffset.y()); |
78 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); | 79 SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1); |
79 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); | 80 SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY); |
80 GrPaint paint; | 81 GrPaint paint; |
82 paint.setAllowSRGBInputs(drawContext->allowSRGBInputs()); | |
81 SkIRect bounds; | 83 SkIRect bounds; |
82 if (srcBounds) { | 84 if (srcBounds) { |
83 srcBounds->roundOut(&bounds); | 85 srcBounds->roundOut(&bounds); |
84 } else { | 86 } else { |
85 bounds.setEmpty(); | 87 bounds.setEmpty(); |
86 } | 88 } |
87 | 89 |
88 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaus sian( | 90 SkAutoTUnref<GrFragmentProcessor> conv(GrMatrixConvolutionEffect::CreateGaus sian( |
89 texture, bounds, size, 1.0, 0.0, kernelOffset, | 91 texture, bounds, size, 1.0, 0.0, kernelOffset, |
90 srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_ Mode, | 92 srcBounds ? GrTextureDomain::kDecal_Mode : GrTextureDomain::kIgnore_ Mode, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, | 159 convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, |
158 direction, radius, sigma, true, bounds); | 160 direction, radius, sigma, true, bounds); |
159 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, | 161 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, |
160 direction, radius, sigma, false, bounds); | 162 direction, radius, sigma, false, bounds); |
161 } | 163 } |
162 } | 164 } |
163 | 165 |
164 GrTexture* GaussianBlur(GrContext* context, | 166 GrTexture* GaussianBlur(GrContext* context, |
165 GrTexture* srcTexture, | 167 GrTexture* srcTexture, |
166 bool canClobberSrc, | 168 bool canClobberSrc, |
169 bool allowSRGBInputs, | |
167 const SkRect& dstBounds, | 170 const SkRect& dstBounds, |
168 const SkRect* srcBounds, | 171 const SkRect* srcBounds, |
169 float sigmaX, | 172 float sigmaX, |
170 float sigmaY) { | 173 float sigmaY) { |
171 SkASSERT(context); | 174 SkASSERT(context); |
172 SkIRect clearRect; | 175 SkIRect clearRect; |
173 int scaleFactorX, radiusX; | 176 int scaleFactorX, radiusX; |
174 int scaleFactorY, radiusY; | 177 int scaleFactorY, radiusY; |
175 int maxTextureSize = context->caps()->maxTextureSize(); | 178 int maxTextureSize = context->caps()->maxTextureSize(); |
176 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); | 179 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 } | 225 } |
223 | 226 |
224 if (nullptr == dstTexture || nullptr == tempTexture) { | 227 if (nullptr == dstTexture || nullptr == tempTexture) { |
225 return nullptr; | 228 return nullptr; |
226 } | 229 } |
227 | 230 |
228 SkAutoTUnref<GrDrawContext> srcDrawContext; | 231 SkAutoTUnref<GrDrawContext> srcDrawContext; |
229 | 232 |
230 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { | 233 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
231 GrPaint paint; | 234 GrPaint paint; |
235 paint.setAllowSRGBInputs(allowSRGBInputs); | |
232 SkMatrix matrix; | 236 SkMatrix matrix; |
233 matrix.setIDiv(srcTexture->width(), srcTexture->height()); | 237 matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
234 SkRect dstRect(srcRect); | 238 SkRect dstRect(srcRect); |
235 if (srcBounds && i == 1) { | 239 if (srcBounds && i == 1) { |
236 SkRect domain; | 240 SkRect domain; |
237 matrix.mapRect(&domain, *srcBounds); | 241 matrix.mapRect(&domain, *srcBounds); |
238 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width( ) : 0.0f, | 242 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width( ) : 0.0f, |
239 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height () : 0.0f); | 243 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height () : 0.0f); |
240 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Cr eate( | 244 SkAutoTUnref<const GrFragmentProcessor> fp(GrTextureDomainEffect::Cr eate( |
241 srcTexture, | 245 srcTexture, |
(...skipping 19 matching lines...) Expand all Loading... | |
261 } | 265 } |
262 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR ect); | 266 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR ect); |
263 | 267 |
264 srcDrawContext.swap(dstDrawContext); | 268 srcDrawContext.swap(dstDrawContext); |
265 srcRect = dstRect; | 269 srcRect = dstRect; |
266 srcTexture = dstTexture; | 270 srcTexture = dstTexture; |
267 SkTSwap(dstTexture, tempTexture); | 271 SkTSwap(dstTexture, tempTexture); |
268 localSrcBounds = srcRect; | 272 localSrcBounds = srcRect; |
269 } | 273 } |
270 | 274 |
275 SkSurfaceProps props(allowSRGBInputs ? SkSurfaceProps::kAllowSRGBInputs_Flag : 0, | |
276 SkSurfaceProps::kLegacyFontHost_InitType); | |
277 | |
271 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i s faster to just | 278 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i s faster to just |
272 // launch a single non separable kernel vs two launches | 279 // launch a single non separable kernel vs two launches |
273 srcRect = localDstBounds; | 280 srcRect = localDstBounds; |
274 if (sigmaX > 0.0f && sigmaY > 0.0f && | 281 if (sigmaX > 0.0f && sigmaY > 0.0f && |
275 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { | 282 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
276 // We shouldn't be scaling because this is a small size blur | 283 // We shouldn't be scaling because this is a small size blur |
277 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); | 284 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
278 | 285 |
279 SkAutoTUnref<GrDrawContext> dstDrawContext( | 286 SkAutoTUnref<GrDrawContext> dstDrawContext( |
robertphillips
2016/04/05 12:54:34
I would probably move this move left and put it on
Brian Osman
2016/04/05 15:36:41
Acknowledged.
| |
280 context->drawContext(dstTexture->as RenderTarget())); | 287 context->drawContext(dstTexture->as RenderTarget(), |
288 &props)); | |
281 if (!dstDrawContext) { | 289 if (!dstDrawContext) { |
282 return nullptr; | 290 return nullptr; |
283 } | 291 } |
284 convolve_gaussian_2d(dstDrawContext, clip, srcRect, srcOffset, | 292 convolve_gaussian_2d(dstDrawContext, clip, srcRect, srcOffset, |
285 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBo unds); | 293 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBo unds); |
286 | 294 |
287 srcDrawContext.swap(dstDrawContext); | 295 srcDrawContext.swap(dstDrawContext); |
288 srcRect.offsetTo(0, 0); | 296 srcRect.offsetTo(0, 0); |
289 srcTexture = dstTexture; | 297 srcTexture = dstTexture; |
290 SkTSwap(dstTexture, tempTexture); | 298 SkTSwap(dstTexture, tempTexture); |
(...skipping 12 matching lines...) Expand all Loading... | |
303 } | 311 } |
304 } | 312 } |
305 | 313 |
306 // Clear out a radius to the right of the srcRect to prevent the | 314 // Clear out a radius to the right of the srcRect to prevent the |
307 // X convolution from reading garbage. | 315 // X convolution from reading garbage. |
308 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, | 316 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
309 radiusX, srcIRect.height()); | 317 radiusX, srcIRect.height()); |
310 srcDrawContext->clear(&clearRect, 0x0, false); | 318 srcDrawContext->clear(&clearRect, 0x0, false); |
311 } | 319 } |
312 | 320 |
313 SkAutoTUnref<GrDrawContext> dstDrawContext( | 321 SkAutoTUnref<GrDrawContext> dstDrawContext( |
robertphillips
2016/04/05 12:54:34
here too
Brian Osman
2016/04/05 15:36:41
Acknowledged.
| |
314 context->drawContext(dstTexture->as RenderTarget())); | 322 context->drawContext(dstTexture->as RenderTarget(), |
323 &props)); | |
315 if (!dstDrawContext) { | 324 if (!dstDrawContext) { |
316 return nullptr; | 325 return nullptr; |
317 } | 326 } |
318 convolve_gaussian(dstDrawContext, clip, srcRect, | 327 convolve_gaussian(dstDrawContext, clip, srcRect, |
319 srcTexture, Gr1DKernelEffect::kX_Direction, radius X, sigmaX, | 328 srcTexture, Gr1DKernelEffect::kX_Direction, radius X, sigmaX, |
320 srcBounds, srcOffset); | 329 srcBounds, srcOffset); |
321 srcDrawContext.swap(dstDrawContext); | 330 srcDrawContext.swap(dstDrawContext); |
322 srcTexture = dstTexture; | 331 srcTexture = dstTexture; |
323 srcRect.offsetTo(0, 0); | 332 srcRect.offsetTo(0, 0); |
324 SkTSwap(dstTexture, tempTexture); | 333 SkTSwap(dstTexture, tempTexture); |
(...skipping 11 matching lines...) Expand all Loading... | |
336 } | 345 } |
337 } | 346 } |
338 | 347 |
339 // Clear out a radius below the srcRect to prevent the Y | 348 // Clear out a radius below the srcRect to prevent the Y |
340 // convolution from reading garbage. | 349 // convolution from reading garbage. |
341 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, | 350 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
342 srcIRect.width(), radiusY); | 351 srcIRect.width(), radiusY); |
343 srcDrawContext->clear(&clearRect, 0x0, false); | 352 srcDrawContext->clear(&clearRect, 0x0, false); |
344 } | 353 } |
345 | 354 |
346 SkAutoTUnref<GrDrawContext> dstDrawContext( | 355 SkAutoTUnref<GrDrawContext> dstDrawContext( |
robertphillips
2016/04/05 12:54:34
3
Brian Osman
2016/04/05 15:36:41
Acknowledged.
| |
347 context->drawContext(dstTexture-> asRenderTarget())); | 356 context->drawContext(dstTexture-> asRenderTarget(), |
357 &props)); | |
348 if (!dstDrawContext) { | 358 if (!dstDrawContext) { |
349 return nullptr; | 359 return nullptr; |
350 } | 360 } |
351 convolve_gaussian(dstDrawContext, clip, srcRect, | 361 convolve_gaussian(dstDrawContext, clip, srcRect, |
352 srcTexture, Gr1DKernelEffect::kY_Direction, radius Y, sigmaY, | 362 srcTexture, Gr1DKernelEffect::kY_Direction, radius Y, sigmaY, |
353 srcBounds, srcOffset); | 363 srcBounds, srcOffset); |
354 | 364 |
355 srcDrawContext.swap(dstDrawContext); | 365 srcDrawContext.swap(dstDrawContext); |
356 srcTexture = dstTexture; | 366 srcTexture = dstTexture; |
357 srcRect.offsetTo(0, 0); | 367 srcRect.offsetTo(0, 0); |
(...skipping 10 matching lines...) Expand all Loading... | |
368 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, | 378 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
369 srcIRect.width() + 1, 1); | 379 srcIRect.width() + 1, 1); |
370 srcDrawContext->clear(&clearRect, 0x0, false); | 380 srcDrawContext->clear(&clearRect, 0x0, false); |
371 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, | 381 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
372 1, srcIRect.height()); | 382 1, srcIRect.height()); |
373 srcDrawContext->clear(&clearRect, 0x0, false); | 383 srcDrawContext->clear(&clearRect, 0x0, false); |
374 SkMatrix matrix; | 384 SkMatrix matrix; |
375 matrix.setIDiv(srcTexture->width(), srcTexture->height()); | 385 matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
376 | 386 |
377 GrPaint paint; | 387 GrPaint paint; |
388 paint.setAllowSRGBInputs(allowSRGBInputs); | |
378 // FIXME: this should be mitchell, not bilinear. | 389 // FIXME: this should be mitchell, not bilinear. |
379 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile rp_FilterMode); | 390 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile rp_FilterMode); |
380 paint.addColorTextureProcessor(srcTexture, matrix, params); | 391 paint.addColorTextureProcessor(srcTexture, matrix, params); |
381 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 392 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
382 | 393 |
383 SkRect dstRect(srcRect); | 394 SkRect dstRect(srcRect); |
384 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); | 395 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
385 | 396 |
386 SkAutoTUnref<GrDrawContext> dstDrawContext( | 397 SkAutoTUnref<GrDrawContext> dstDrawContext( |
387 context->drawContext(dstTexture->asRenderTarget( ))); | 398 context->drawContext(dstTexture->asRenderTarget( ))); |
388 if (!dstDrawContext) { | 399 if (!dstDrawContext) { |
389 return nullptr; | 400 return nullptr; |
390 } | 401 } |
391 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR ect); | 402 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR ect); |
392 | 403 |
393 srcDrawContext.swap(dstDrawContext); | 404 srcDrawContext.swap(dstDrawContext); |
394 srcRect = dstRect; | 405 srcRect = dstRect; |
395 srcTexture = dstTexture; | 406 srcTexture = dstTexture; |
396 SkTSwap(dstTexture, tempTexture); | 407 SkTSwap(dstTexture, tempTexture); |
397 } | 408 } |
398 | 409 |
399 return SkRef(srcTexture); | 410 return SkRef(srcTexture); |
400 } | 411 } |
401 #endif | 412 #endif |
402 | 413 |
403 } | 414 } |
OLD | NEW |