| 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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 // Draw right and left margins with bounds; middle without. | 156 // Draw right and left margins with bounds; middle without. |
| 157 convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture, | 157 convolve_gaussian_1d(drawContext, clip, leftRect, srcOffset, texture, |
| 158 direction, radius, sigma, true, bounds); | 158 direction, radius, sigma, true, bounds); |
| 159 convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, | 159 convolve_gaussian_1d(drawContext, clip, rightRect, srcOffset, texture, |
| 160 direction, radius, sigma, true, bounds); | 160 direction, radius, sigma, true, bounds); |
| 161 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, | 161 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, |
| 162 direction, radius, sigma, false, bounds); | 162 direction, radius, sigma, false, bounds); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 GrTexture* GaussianBlur(GrContext* context, | 166 sk_sp<GrDrawContext> GaussianBlur(GrContext* context, |
| 167 GrTexture* srcTexture, | 167 GrTexture* srcTexture, |
| 168 bool gammaCorrect, | 168 bool gammaCorrect, |
| 169 const SkRect& dstBounds, | 169 const SkRect& dstBounds, |
| 170 const SkRect* srcBounds, | 170 const SkRect* srcBounds, |
| 171 float sigmaX, | 171 float sigmaX, |
| 172 float sigmaY) { | 172 float sigmaY) { |
| 173 SkASSERT(context); | 173 SkASSERT(context); |
| 174 SkIRect clearRect; | 174 SkIRect clearRect; |
| 175 int scaleFactorX, radiusX; | 175 int scaleFactorX, radiusX; |
| 176 int scaleFactorY, radiusY; | 176 int scaleFactorY, radiusY; |
| 177 int maxTextureSize = context->caps()->maxTextureSize(); | 177 int maxTextureSize = context->caps()->maxTextureSize(); |
| 178 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); | 178 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
| 179 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); | 179 sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); |
| 180 | 180 |
| 181 SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y()); | 181 SkPoint srcOffset = SkPoint::Make(-dstBounds.x(), -dstBounds.y()); |
| 182 SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height()
); | 182 SkRect localDstBounds = SkRect::MakeWH(dstBounds.width(), dstBounds.height()
); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 204 kSBGRA_8888_GrPixelConfig == srcTexture->config() || | 204 kSBGRA_8888_GrPixelConfig == srcTexture->config() || |
| 205 kAlpha_8_GrPixelConfig == srcTexture->config()); | 205 kAlpha_8_GrPixelConfig == srcTexture->config()); |
| 206 | 206 |
| 207 const int width = SkScalarFloorToInt(dstBounds.width()); | 207 const int width = SkScalarFloorToInt(dstBounds.width()); |
| 208 const int height = SkScalarFloorToInt(dstBounds.height()); | 208 const int height = SkScalarFloorToInt(dstBounds.height()); |
| 209 const GrPixelConfig config = srcTexture->config(); | 209 const GrPixelConfig config = srcTexture->config(); |
| 210 | 210 |
| 211 const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag
: 0, | 211 const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag
: 0, |
| 212 SkSurfaceProps::kLegacyFontHost_InitType); | 212 SkSurfaceProps::kLegacyFontHost_InitType); |
| 213 | 213 |
| 214 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kA
pprox, |
| 215 width, height, c
onfig, |
| 216 0, kDefault_GrSu
rfaceOrigin, |
| 217 &props)); |
| 218 if (!dstDrawContext) { |
| 219 return nullptr; |
| 220 } |
| 221 |
| 214 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i
s faster to just | 222 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i
s faster to just |
| 215 // launch a single non separable kernel vs two launches | 223 // launch a single non separable kernel vs two launches |
| 216 if (sigmaX > 0.0f && sigmaY > 0.0f && | 224 if (sigmaX > 0.0f && sigmaY > 0.0f && |
| 217 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { | 225 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
| 218 // We shouldn't be scaling because this is a small size blur | 226 // We shouldn't be scaling because this is a small size blur |
| 219 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); | 227 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
| 220 | 228 |
| 221 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit
::kApprox, | |
| 222 width, heigh
t, config, | |
| 223 0, kDefault_
GrSurfaceOrigin, | |
| 224 &props)); | |
| 225 if (!dstDrawContext) { | |
| 226 return nullptr; | |
| 227 } | |
| 228 convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffs
et, | 229 convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffs
et, |
| 229 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBo
unds); | 230 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBo
unds); |
| 230 | 231 |
| 231 return dstDrawContext->asTexture().release(); | 232 return dstDrawContext; |
| 232 } | 233 } |
| 233 | 234 |
| 234 GrSurfaceDesc desc; | 235 sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kA
pprox, |
| 235 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 236 width, height, c
onfig, |
| 236 desc.fWidth = width; | 237 0, kDefault_GrSu
rfaceOrigin, |
| 237 desc.fHeight = height; | 238 &props)); |
| 238 desc.fConfig = config; | 239 if (!tmpDrawContext) { |
| 239 | |
| 240 GrTexture* dstTexture; | |
| 241 GrTexture* tempTexture; | |
| 242 SkAutoTUnref<GrTexture> temp1, temp2; | |
| 243 | |
| 244 temp1.reset(context->textureProvider()->createApproxTexture(desc)); | |
| 245 dstTexture = temp1.get(); | |
| 246 temp2.reset(context->textureProvider()->createApproxTexture(desc)); | |
| 247 tempTexture = temp2.get(); | |
| 248 | |
| 249 if (!dstTexture || !tempTexture) { | |
| 250 return nullptr; | 240 return nullptr; |
| 251 } | 241 } |
| 252 | 242 |
| 253 sk_sp<GrDrawContext> srcDrawContext; | 243 sk_sp<GrDrawContext> srcDrawContext; |
| 254 | 244 |
| 255 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { | 245 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
| 256 GrPaint paint; | 246 GrPaint paint; |
| 257 paint.setGammaCorrect(gammaCorrect); | 247 paint.setGammaCorrect(gammaCorrect); |
| 258 SkMatrix matrix; | 248 SkMatrix matrix; |
| 259 matrix.setIDiv(srcTexture->width(), srcTexture->height()); | 249 matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 273 srcRect.offset(-srcOffset); | 263 srcRect.offset(-srcOffset); |
| 274 srcOffset.set(0, 0); | 264 srcOffset.set(0, 0); |
| 275 } else { | 265 } else { |
| 276 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::k
Bilerp_FilterMode); | 266 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::k
Bilerp_FilterMode); |
| 277 paint.addColorTextureProcessor(srcTexture, matrix, params); | 267 paint.addColorTextureProcessor(srcTexture, matrix, params); |
| 278 } | 268 } |
| 279 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 269 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
| 280 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, | 270 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, |
| 281 i < scaleFactorY ? 0.5f : 1.0f); | 271 i < scaleFactorY ? 0.5f : 1.0f); |
| 282 | 272 |
| 283 sk_sp<GrDrawContext> dstDrawContext( | |
| 284 context->drawContext(sk_ref_sp(dstTexture->as
RenderTarget()))); | |
| 285 if (!dstDrawContext) { | |
| 286 return nullptr; | |
| 287 } | |
| 288 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR
ect); | 273 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR
ect); |
| 289 | 274 |
| 290 srcDrawContext.swap(dstDrawContext); | 275 srcDrawContext = dstDrawContext; |
| 291 srcRect = dstRect; | 276 srcRect = dstRect; |
| 292 srcTexture = dstTexture; | 277 srcTexture = srcDrawContext->asTexture().release(); |
| 293 SkTSwap(dstTexture, tempTexture); | 278 SkTSwap(dstDrawContext, tmpDrawContext); |
| 294 localSrcBounds = srcRect; | 279 localSrcBounds = srcRect; |
| 295 } | 280 } |
| 296 | 281 |
| 297 srcRect = localDstBounds; | 282 srcRect = localDstBounds; |
| 298 | 283 |
| 299 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); | 284 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); |
| 300 srcRect.roundOut(&srcRect); | 285 srcRect.roundOut(&srcRect); |
| 301 SkIRect srcIRect = srcRect.roundOut(); | 286 SkIRect srcIRect = srcRect.roundOut(); |
| 302 if (sigmaX > 0.0f) { | 287 if (sigmaX > 0.0f) { |
| 303 if (scaleFactorX > 1) { | 288 if (scaleFactorX > 1) { |
| 304 // TODO: if we pass in the source draw context we don't need this he
re | 289 SkASSERT(srcDrawContext); |
| 305 if (!srcDrawContext) { | |
| 306 srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRe
nderTarget())); | |
| 307 if (!srcDrawContext) { | |
| 308 return nullptr; | |
| 309 } | |
| 310 } | |
| 311 | 290 |
| 312 // Clear out a radius to the right of the srcRect to prevent the | 291 // Clear out a radius to the right of the srcRect to prevent the |
| 313 // X convolution from reading garbage. | 292 // X convolution from reading garbage. |
| 314 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, | 293 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, |
| 315 radiusX, srcIRect.height()); | 294 radiusX, srcIRect.height()); |
| 316 srcDrawContext->clear(&clearRect, 0x0, false); | 295 srcDrawContext->clear(&clearRect, 0x0, false); |
| 317 } | 296 } |
| 318 | 297 |
| 319 sk_sp<GrDrawContext> dstDrawContext( | |
| 320 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props
)); | |
| 321 if (!dstDrawContext) { | |
| 322 return nullptr; | |
| 323 } | |
| 324 convolve_gaussian(dstDrawContext.get(), clip, srcRect, | 298 convolve_gaussian(dstDrawContext.get(), clip, srcRect, |
| 325 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, s
igmaX, | 299 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, s
igmaX, |
| 326 srcBounds, srcOffset); | 300 srcBounds, srcOffset); |
| 327 srcDrawContext.swap(dstDrawContext); | 301 srcDrawContext = dstDrawContext; |
| 328 srcTexture = dstTexture; | 302 srcTexture = srcDrawContext->asTexture().release(); |
| 329 srcRect.offsetTo(0, 0); | 303 srcRect.offsetTo(0, 0); |
| 330 SkTSwap(dstTexture, tempTexture); | 304 SkTSwap(dstDrawContext, tmpDrawContext); |
| 331 localSrcBounds = srcRect; | 305 localSrcBounds = srcRect; |
| 332 srcOffset.set(0, 0); | 306 srcOffset.set(0, 0); |
| 333 } | 307 } |
| 334 | 308 |
| 335 if (sigmaY > 0.0f) { | 309 if (sigmaY > 0.0f) { |
| 336 if (scaleFactorY > 1 || sigmaX > 0.0f) { | 310 if (scaleFactorY > 1 || sigmaX > 0.0f) { |
| 337 // TODO: if we pass in the source draw context we don't need this he
re | 311 SkASSERT(srcDrawContext); |
| 338 if (!srcDrawContext) { | |
| 339 srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRe
nderTarget())); | |
| 340 if (!srcDrawContext) { | |
| 341 return nullptr; | |
| 342 } | |
| 343 } | |
| 344 | 312 |
| 345 // Clear out a radius below the srcRect to prevent the Y | 313 // Clear out a radius below the srcRect to prevent the Y |
| 346 // convolution from reading garbage. | 314 // convolution from reading garbage. |
| 347 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, | 315 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, |
| 348 srcIRect.width(), radiusY); | 316 srcIRect.width(), radiusY); |
| 349 srcDrawContext->clear(&clearRect, 0x0, false); | 317 srcDrawContext->clear(&clearRect, 0x0, false); |
| 350 } | 318 } |
| 351 | 319 |
| 352 sk_sp<GrDrawContext> dstDrawContext( | |
| 353 context->drawContext(sk_ref_sp(dstTexture->asRenderTarget()), &props
)); | |
| 354 if (!dstDrawContext) { | |
| 355 return nullptr; | |
| 356 } | |
| 357 convolve_gaussian(dstDrawContext.get(), clip, srcRect, | 320 convolve_gaussian(dstDrawContext.get(), clip, srcRect, |
| 358 srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, s
igmaY, | 321 srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, s
igmaY, |
| 359 srcBounds, srcOffset); | 322 srcBounds, srcOffset); |
| 360 | 323 |
| 361 srcDrawContext.swap(dstDrawContext); | 324 srcDrawContext = dstDrawContext; |
| 362 srcTexture = dstTexture; | |
| 363 srcRect.offsetTo(0, 0); | 325 srcRect.offsetTo(0, 0); |
| 364 SkTSwap(dstTexture, tempTexture); | 326 SkTSwap(dstDrawContext, tmpDrawContext); |
| 365 } | 327 } |
| 366 | 328 |
| 329 SkASSERT(srcDrawContext); |
| 330 srcTexture = nullptr; // we don't use this from here on out |
| 367 srcIRect = srcRect.roundOut(); | 331 srcIRect = srcRect.roundOut(); |
| 368 | 332 |
| 369 if (scaleFactorX > 1 || scaleFactorY > 1) { | 333 if (scaleFactorX > 1 || scaleFactorY > 1) { |
| 370 SkASSERT(srcDrawContext); | 334 // Clear one pixel to the right and below, to accommodate bilinear upsam
pling. |
| 335 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect
.width() + 1, 1); |
| 336 srcDrawContext->clear(&clearRect, 0x0, false); |
| 337 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 1, srcIRec
t.height()); |
| 338 srcDrawContext->clear(&clearRect, 0x0, false); |
| 371 | 339 |
| 372 // Clear one pixel to the right and below, to accommodate bilinear | |
| 373 // upsampling. | |
| 374 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, | |
| 375 srcIRect.width() + 1, 1); | |
| 376 srcDrawContext->clear(&clearRect, 0x0, false); | |
| 377 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, | |
| 378 1, srcIRect.height()); | |
| 379 srcDrawContext->clear(&clearRect, 0x0, false); | |
| 380 SkMatrix matrix; | 340 SkMatrix matrix; |
| 381 matrix.setIDiv(srcTexture->width(), srcTexture->height()); | 341 matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); |
| 382 | 342 |
| 383 GrPaint paint; | 343 GrPaint paint; |
| 384 paint.setGammaCorrect(gammaCorrect); | 344 paint.setGammaCorrect(gammaCorrect); |
| 385 // FIXME: this should be mitchell, not bilinear. | 345 // FIXME: this should be mitchell, not bilinear. |
| 386 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile
rp_FilterMode); | 346 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile
rp_FilterMode); |
| 387 paint.addColorTextureProcessor(srcTexture, matrix, params); | 347 paint.addColorTextureProcessor(srcDrawContext->asTexture().release(), ma
trix, params); |
| 388 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 348 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
| 389 | 349 |
| 390 SkRect dstRect(srcRect); | 350 SkRect dstRect(srcRect); |
| 391 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); | 351 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); |
| 392 | 352 |
| 393 sk_sp<GrDrawContext> dstDrawContext( | |
| 394 context->drawContext(sk_ref_sp(dstTexture->asRen
derTarget()))); | |
| 395 if (!dstDrawContext) { | |
| 396 return nullptr; | |
| 397 } | |
| 398 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR
ect); | 353 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR
ect); |
| 399 | 354 |
| 400 srcDrawContext.swap(dstDrawContext); | 355 srcDrawContext = dstDrawContext; |
| 401 srcRect = dstRect; | 356 srcRect = dstRect; |
| 402 srcTexture = dstTexture; | 357 SkTSwap(dstDrawContext, tmpDrawContext); |
| 403 SkTSwap(dstTexture, tempTexture); | |
| 404 } | 358 } |
| 405 | 359 |
| 406 return SkRef(srcTexture); | 360 return srcDrawContext; |
| 407 } | 361 } |
| 408 #endif | 362 #endif |
| 409 | 363 |
| 410 } | 364 } |
| OLD | NEW |