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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, | 176 convolve_gaussian_1d(drawContext, clip, midRect, srcOffset, texture, |
177 direction, radius, sigma, false, bounds); | 177 direction, radius, sigma, false, bounds); |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 namespace SkGpuBlurUtils { | 181 namespace SkGpuBlurUtils { |
182 | 182 |
183 sk_sp<GrDrawContext> GaussianBlur(GrContext* context, | 183 sk_sp<GrDrawContext> GaussianBlur(GrContext* context, |
184 GrTexture* origSrc, | 184 GrTexture* origSrc, |
185 sk_sp<SkColorSpace> colorSpace, | 185 sk_sp<SkColorSpace> colorSpace, |
186 bool gammaCorrect, | |
187 const SkIRect& dstBounds, | 186 const SkIRect& dstBounds, |
188 const SkIRect* srcBounds, | 187 const SkIRect* srcBounds, |
189 float sigmaX, | 188 float sigmaX, |
190 float sigmaY) { | 189 float sigmaY) { |
191 SkASSERT(context); | 190 SkASSERT(context); |
192 SkIRect clearRect; | 191 SkIRect clearRect; |
193 int scaleFactorX, radiusX; | 192 int scaleFactorX, radiusX; |
194 int scaleFactorY, radiusY; | 193 int scaleFactorY, radiusY; |
195 int maxTextureSize = context->caps()->maxTextureSize(); | 194 int maxTextureSize = context->caps()->maxTextureSize(); |
196 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); | 195 sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); |
(...skipping 23 matching lines...) Expand all Loading... |
220 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || | 219 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || |
221 kRGBA_8888_GrPixelConfig == srcTexture->config() || | 220 kRGBA_8888_GrPixelConfig == srcTexture->config() || |
222 kSRGBA_8888_GrPixelConfig == srcTexture->config() || | 221 kSRGBA_8888_GrPixelConfig == srcTexture->config() || |
223 kSBGRA_8888_GrPixelConfig == srcTexture->config() || | 222 kSBGRA_8888_GrPixelConfig == srcTexture->config() || |
224 kAlpha_8_GrPixelConfig == srcTexture->config()); | 223 kAlpha_8_GrPixelConfig == srcTexture->config()); |
225 | 224 |
226 const int width = dstBounds.width(); | 225 const int width = dstBounds.width(); |
227 const int height = dstBounds.height(); | 226 const int height = dstBounds.height(); |
228 const GrPixelConfig config = srcTexture->config(); | 227 const GrPixelConfig config = srcTexture->config(); |
229 | 228 |
230 const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag
: 0, | |
231 SkSurfaceProps::kLegacyFontHost_InitType); | |
232 | |
233 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kA
pprox, | 229 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kA
pprox, |
234 width, height, c
onfig, colorSpace, | 230 width, height, c
onfig, colorSpace, |
235 0, kDefault_GrSu
rfaceOrigin, | 231 0, kDefault_GrSu
rfaceOrigin)); |
236 &props)); | |
237 if (!dstDrawContext) { | 232 if (!dstDrawContext) { |
238 return nullptr; | 233 return nullptr; |
239 } | 234 } |
240 | 235 |
241 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i
s faster to just | 236 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i
s faster to just |
242 // launch a single non separable kernel vs two launches | 237 // launch a single non separable kernel vs two launches |
243 if (sigmaX > 0.0f && sigmaY > 0.0f && | 238 if (sigmaX > 0.0f && sigmaY > 0.0f && |
244 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { | 239 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { |
245 // We shouldn't be scaling because this is a small size blur | 240 // We shouldn't be scaling because this is a small size blur |
246 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); | 241 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); |
247 | 242 |
248 convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffs
et, | 243 convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffs
et, |
249 srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY,
srcBounds); | 244 srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY,
srcBounds); |
250 | 245 |
251 return dstDrawContext; | 246 return dstDrawContext; |
252 } | 247 } |
253 | 248 |
254 sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kA
pprox, | 249 sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kA
pprox, |
255 width, height, c
onfig, colorSpace, | 250 width, height, c
onfig, colorSpace, |
256 0, kDefault_GrSu
rfaceOrigin, | 251 0, kDefault_GrSu
rfaceOrigin)); |
257 &props)); | |
258 if (!tmpDrawContext) { | 252 if (!tmpDrawContext) { |
259 return nullptr; | 253 return nullptr; |
260 } | 254 } |
261 | 255 |
262 sk_sp<GrDrawContext> srcDrawContext; | 256 sk_sp<GrDrawContext> srcDrawContext; |
263 | 257 |
264 SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); | 258 SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); |
265 | 259 |
266 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { | 260 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { |
267 GrPaint paint; | 261 GrPaint paint; |
268 paint.setGammaCorrect(gammaCorrect); | 262 paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); |
269 SkMatrix matrix; | 263 SkMatrix matrix; |
270 matrix.setIDiv(srcTexture->width(), srcTexture->height()); | 264 matrix.setIDiv(srcTexture->width(), srcTexture->height()); |
271 SkIRect dstRect(srcRect); | 265 SkIRect dstRect(srcRect); |
272 if (srcBounds && i == 1) { | 266 if (srcBounds && i == 1) { |
273 SkRect domain; | 267 SkRect domain; |
274 matrix.mapRect(&domain, SkRect::Make(*srcBounds)); | 268 matrix.mapRect(&domain, SkRect::Make(*srcBounds)); |
275 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width(
) : 0.0f, | 269 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width(
) : 0.0f, |
276 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height
() : 0.0f); | 270 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height
() : 0.0f); |
277 sk_sp<GrFragmentProcessor> fp(GrTextureDomainEffect::Make( | 271 sk_sp<GrFragmentProcessor> fp(GrTextureDomainEffect::Make( |
278 srcTexture.get(), | 272 srcTexture.get(), |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 // Clear one pixel to the right and below, to accommodate bilinear upsam
pling. | 346 // Clear one pixel to the right and below, to accommodate bilinear upsam
pling. |
353 clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.wi
dth() + 1, 1); | 347 clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.wi
dth() + 1, 1); |
354 srcDrawContext->clear(&clearRect, 0x0, false); | 348 srcDrawContext->clear(&clearRect, 0x0, false); |
355 clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.h
eight()); | 349 clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.h
eight()); |
356 srcDrawContext->clear(&clearRect, 0x0, false); | 350 srcDrawContext->clear(&clearRect, 0x0, false); |
357 | 351 |
358 SkMatrix matrix; | 352 SkMatrix matrix; |
359 matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); | 353 matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); |
360 | 354 |
361 GrPaint paint; | 355 GrPaint paint; |
362 paint.setGammaCorrect(gammaCorrect); | 356 paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); |
363 // FIXME: this should be mitchell, not bilinear. | 357 // FIXME: this should be mitchell, not bilinear. |
364 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile
rp_FilterMode); | 358 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile
rp_FilterMode); |
365 sk_sp<GrTexture> tex(srcDrawContext->asTexture()); | 359 sk_sp<GrTexture> tex(srcDrawContext->asTexture()); |
366 paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params); | 360 paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params); |
367 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 361 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
368 | 362 |
369 SkIRect dstRect(srcRect); | 363 SkIRect dstRect(srcRect); |
370 scale_irect(&dstRect, scaleFactorX, scaleFactorY); | 364 scale_irect(&dstRect, scaleFactorX, scaleFactorY); |
371 | 365 |
372 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), | 366 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), |
373 SkRect::Make(dstRect), SkRect::Make(srcRe
ct)); | 367 SkRect::Make(dstRect), SkRect::Make(srcRe
ct)); |
374 | 368 |
375 srcDrawContext = dstDrawContext; | 369 srcDrawContext = dstDrawContext; |
376 srcRect = dstRect; | 370 srcRect = dstRect; |
377 dstDrawContext.swap(tmpDrawContext); | 371 dstDrawContext.swap(tmpDrawContext); |
378 } | 372 } |
379 | 373 |
380 return srcDrawContext; | 374 return srcDrawContext; |
381 } | 375 } |
382 | 376 |
383 } | 377 } |
384 | 378 |
385 #endif | 379 #endif |
386 | 380 |
OLD | NEW |