Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Side by Side Diff: src/effects/SkGpuBlurUtils.cpp

Issue 1962903003: Make SkGpuBlurUtils::GaussianBlur more drawContext centric (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix sk_sp bug Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/effects/SkGpuBlurUtils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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* origSrc,
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() );
183 SkRect localSrcBounds; 183 SkRect localSrcBounds;
184 SkRect srcRect; 184 SkRect srcRect;
185 if (srcBounds) { 185 if (srcBounds) {
186 srcRect = localSrcBounds = *srcBounds; 186 srcRect = localSrcBounds = *srcBounds;
187 srcRect.offset(srcOffset); 187 srcRect.offset(srcOffset);
188 srcBounds = &localSrcBounds; 188 srcBounds = &localSrcBounds;
189 } else { 189 } else {
190 srcRect = localDstBounds; 190 srcRect = localDstBounds;
191 } 191 }
192 192
193 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); 193 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
194 srcRect.roundOut(&srcRect); 194 srcRect.roundOut(&srcRect);
195 scale_rect(&srcRect, static_cast<float>(scaleFactorX), 195 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
196 static_cast<float>(scaleFactorY)); 196 static_cast<float>(scaleFactorY));
197 197
198 // setup new clip 198 // setup new clip
199 GrClip clip(localDstBounds); 199 GrClip clip(localDstBounds);
200 200
201 sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc));
202
201 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || 203 SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
202 kRGBA_8888_GrPixelConfig == srcTexture->config() || 204 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
203 kSRGBA_8888_GrPixelConfig == srcTexture->config() || 205 kSRGBA_8888_GrPixelConfig == srcTexture->config() ||
204 kSBGRA_8888_GrPixelConfig == srcTexture->config() || 206 kSBGRA_8888_GrPixelConfig == srcTexture->config() ||
205 kAlpha_8_GrPixelConfig == srcTexture->config()); 207 kAlpha_8_GrPixelConfig == srcTexture->config());
206 208
207 const int width = SkScalarFloorToInt(dstBounds.width()); 209 const int width = SkScalarFloorToInt(dstBounds.width());
208 const int height = SkScalarFloorToInt(dstBounds.height()); 210 const int height = SkScalarFloorToInt(dstBounds.height());
209 const GrPixelConfig config = srcTexture->config(); 211 const GrPixelConfig config = srcTexture->config();
210 212
211 const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0, 213 const SkSurfaceProps props(gammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0,
212 SkSurfaceProps::kLegacyFontHost_InitType); 214 SkSurfaceProps::kLegacyFontHost_InitType);
213 215
216 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kA pprox,
217 width, height, c onfig,
218 0, kDefault_GrSu rfaceOrigin,
219 &props));
220 if (!dstDrawContext) {
221 return nullptr;
222 }
223
214 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i s faster to just 224 // 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 225 // launch a single non separable kernel vs two launches
216 if (sigmaX > 0.0f && sigmaY > 0.0f && 226 if (sigmaX > 0.0f && sigmaY > 0.0f &&
217 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { 227 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
218 // We shouldn't be scaling because this is a small size blur 228 // We shouldn't be scaling because this is a small size blur
219 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); 229 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
220 230
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, 231 convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffs et,
229 srcTexture, radiusX, radiusY, sigmaX, sigmaY, srcBo unds); 232 srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds);
230 233
231 return dstDrawContext->asTexture().release(); 234 return dstDrawContext;
232 } 235 }
233 236
234 GrSurfaceDesc desc; 237 sk_sp<GrDrawContext> tmpDrawContext(context->newDrawContext(SkBackingFit::kA pprox,
235 desc.fFlags = kRenderTarget_GrSurfaceFlag; 238 width, height, c onfig,
236 desc.fWidth = width; 239 0, kDefault_GrSu rfaceOrigin,
237 desc.fHeight = height; 240 &props));
238 desc.fConfig = config; 241 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; 242 return nullptr;
251 } 243 }
252 244
253 sk_sp<GrDrawContext> srcDrawContext; 245 sk_sp<GrDrawContext> srcDrawContext;
254 246
255 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { 247 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
256 GrPaint paint; 248 GrPaint paint;
257 paint.setGammaCorrect(gammaCorrect); 249 paint.setGammaCorrect(gammaCorrect);
258 SkMatrix matrix; 250 SkMatrix matrix;
259 matrix.setIDiv(srcTexture->width(), srcTexture->height()); 251 matrix.setIDiv(srcTexture->width(), srcTexture->height());
260 SkRect dstRect(srcRect); 252 SkRect dstRect(srcRect);
261 if (srcBounds && i == 1) { 253 if (srcBounds && i == 1) {
262 SkRect domain; 254 SkRect domain;
263 matrix.mapRect(&domain, *srcBounds); 255 matrix.mapRect(&domain, *srcBounds);
264 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width( ) : 0.0f, 256 domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width( ) : 0.0f,
265 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height () : 0.0f); 257 (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height () : 0.0f);
266 sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create( 258 sk_sp<const GrFragmentProcessor> fp(GrTextureDomainEffect::Create(
267 srcTexture, 259 srcTexture.get(),
268 matrix, 260 matrix,
269 domain, 261 domain,
270 GrTextureDomain::kDecal_ Mode, 262 GrTextureDomain::kDecal_ Mode,
271 GrTextureParams::kBilerp _FilterMode)); 263 GrTextureParams::kBilerp _FilterMode));
272 paint.addColorFragmentProcessor(fp.get()); 264 paint.addColorFragmentProcessor(fp.get());
273 srcRect.offset(-srcOffset); 265 srcRect.offset(-srcOffset);
274 srcOffset.set(0, 0); 266 srcOffset.set(0, 0);
275 } else { 267 } else {
276 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::k Bilerp_FilterMode); 268 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::k Bilerp_FilterMode);
277 paint.addColorTextureProcessor(srcTexture, matrix, params); 269 paint.addColorTextureProcessor(srcTexture.get(), matrix, params);
278 } 270 }
279 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); 271 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
280 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, 272 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
281 i < scaleFactorY ? 0.5f : 1.0f); 273 i < scaleFactorY ? 0.5f : 1.0f);
282 274
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); 275 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR ect);
289 276
290 srcDrawContext.swap(dstDrawContext); 277 srcDrawContext = dstDrawContext;
291 srcRect = dstRect; 278 srcRect = dstRect;
292 srcTexture = dstTexture; 279 srcTexture = srcDrawContext->asTexture();
293 SkTSwap(dstTexture, tempTexture); 280 dstDrawContext.swap(tmpDrawContext);
294 localSrcBounds = srcRect; 281 localSrcBounds = srcRect;
295 } 282 }
296 283
297 srcRect = localDstBounds; 284 srcRect = localDstBounds;
298 285
299 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); 286 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
300 srcRect.roundOut(&srcRect); 287 srcRect.roundOut(&srcRect);
301 SkIRect srcIRect = srcRect.roundOut(); 288 SkIRect srcIRect = srcRect.roundOut();
302 if (sigmaX > 0.0f) { 289 if (sigmaX > 0.0f) {
303 if (scaleFactorX > 1) { 290 if (scaleFactorX > 1) {
304 // TODO: if we pass in the source draw context we don't need this he re 291 SkASSERT(srcDrawContext);
305 if (!srcDrawContext) {
306 srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRe nderTarget()));
307 if (!srcDrawContext) {
308 return nullptr;
309 }
310 }
311 292
312 // Clear out a radius to the right of the srcRect to prevent the 293 // Clear out a radius to the right of the srcRect to prevent the
313 // X convolution from reading garbage. 294 // X convolution from reading garbage.
314 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 295 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
315 radiusX, srcIRect.height()); 296 radiusX, srcIRect.height());
316 srcDrawContext->clear(&clearRect, 0x0, false); 297 srcDrawContext->clear(&clearRect, 0x0, false);
317 } 298 }
318 299
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, 300 convolve_gaussian(dstDrawContext.get(), clip, srcRect,
325 srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, s igmaX, 301 srcTexture.get(), Gr1DKernelEffect::kX_Direction, radi usX, sigmaX,
326 srcBounds, srcOffset); 302 srcBounds, srcOffset);
327 srcDrawContext.swap(dstDrawContext); 303 srcDrawContext = dstDrawContext;
328 srcTexture = dstTexture; 304 srcTexture = srcDrawContext->asTexture();
329 srcRect.offsetTo(0, 0); 305 srcRect.offsetTo(0, 0);
330 SkTSwap(dstTexture, tempTexture); 306 dstDrawContext.swap(tmpDrawContext);
331 localSrcBounds = srcRect; 307 localSrcBounds = srcRect;
332 srcOffset.set(0, 0); 308 srcOffset.set(0, 0);
333 } 309 }
334 310
335 if (sigmaY > 0.0f) { 311 if (sigmaY > 0.0f) {
336 if (scaleFactorY > 1 || sigmaX > 0.0f) { 312 if (scaleFactorY > 1 || sigmaX > 0.0f) {
337 // TODO: if we pass in the source draw context we don't need this he re 313 SkASSERT(srcDrawContext);
338 if (!srcDrawContext) {
339 srcDrawContext = context->drawContext(sk_ref_sp(srcTexture->asRe nderTarget()));
340 if (!srcDrawContext) {
341 return nullptr;
342 }
343 }
344 314
345 // Clear out a radius below the srcRect to prevent the Y 315 // Clear out a radius below the srcRect to prevent the Y
346 // convolution from reading garbage. 316 // convolution from reading garbage.
347 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, 317 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
348 srcIRect.width(), radiusY); 318 srcIRect.width(), radiusY);
349 srcDrawContext->clear(&clearRect, 0x0, false); 319 srcDrawContext->clear(&clearRect, 0x0, false);
350 } 320 }
351 321
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, 322 convolve_gaussian(dstDrawContext.get(), clip, srcRect,
358 srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, s igmaY, 323 srcTexture.get(), Gr1DKernelEffect::kY_Direction, radi usY, sigmaY,
359 srcBounds, srcOffset); 324 srcBounds, srcOffset);
360 325
361 srcDrawContext.swap(dstDrawContext); 326 srcDrawContext = dstDrawContext;
362 srcTexture = dstTexture;
363 srcRect.offsetTo(0, 0); 327 srcRect.offsetTo(0, 0);
364 SkTSwap(dstTexture, tempTexture); 328 dstDrawContext.swap(tmpDrawContext);
365 } 329 }
366 330
331 SkASSERT(srcDrawContext);
332 srcTexture = nullptr; // we don't use this from here on out
367 srcIRect = srcRect.roundOut(); 333 srcIRect = srcRect.roundOut();
368 334
369 if (scaleFactorX > 1 || scaleFactorY > 1) { 335 if (scaleFactorX > 1 || scaleFactorY > 1) {
370 SkASSERT(srcDrawContext); 336 // Clear one pixel to the right and below, to accommodate bilinear upsam pling.
337 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect .width() + 1, 1);
338 srcDrawContext->clear(&clearRect, 0x0, false);
339 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 1, srcIRec t.height());
340 srcDrawContext->clear(&clearRect, 0x0, false);
371 341
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; 342 SkMatrix matrix;
381 matrix.setIDiv(srcTexture->width(), srcTexture->height()); 343 matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height());
382 344
383 GrPaint paint; 345 GrPaint paint;
384 paint.setGammaCorrect(gammaCorrect); 346 paint.setGammaCorrect(gammaCorrect);
385 // FIXME: this should be mitchell, not bilinear. 347 // FIXME: this should be mitchell, not bilinear.
386 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile rp_FilterMode); 348 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile rp_FilterMode);
387 paint.addColorTextureProcessor(srcTexture, matrix, params); 349 sk_sp<GrTexture> tex(srcDrawContext->asTexture());
350 paint.addColorTextureProcessor(tex.get(), matrix, params);
388 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); 351 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
389 352
390 SkRect dstRect(srcRect); 353 SkRect dstRect(srcRect);
391 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); 354 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
392 355
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); 356 dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcR ect);
399 357
400 srcDrawContext.swap(dstDrawContext); 358 srcDrawContext = dstDrawContext;
401 srcRect = dstRect; 359 srcRect = dstRect;
402 srcTexture = dstTexture; 360 dstDrawContext.swap(tmpDrawContext);
403 SkTSwap(dstTexture, tempTexture);
404 } 361 }
405 362
406 return SkRef(srcTexture); 363 return srcDrawContext;
407 } 364 }
408 #endif 365 #endif
409 366
410 } 367 }
OLDNEW
« no previous file with comments | « src/effects/SkGpuBlurUtils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698