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