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

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

Issue 1321353002: Limit lifetime of GrDrawContext objects (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update to ToT Created 5 years, 3 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
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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 tempTexture = srcTexture; 189 tempTexture = srcTexture;
190 } else { 190 } else {
191 temp2.reset(context->textureProvider()->createApproxTexture(desc)); 191 temp2.reset(context->textureProvider()->createApproxTexture(desc));
192 tempTexture = temp2.get(); 192 tempTexture = temp2.get();
193 } 193 }
194 194
195 if (nullptr == dstTexture || nullptr == tempTexture) { 195 if (nullptr == dstTexture || nullptr == tempTexture) {
196 return nullptr; 196 return nullptr;
197 } 197 }
198 198
199 GrDrawContext* srcDrawContext = nullptr; 199 SkAutoTUnref<GrDrawContext> srcDrawContext;
200 200
201 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { 201 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
202 GrPaint paint; 202 GrPaint paint;
203 SkMatrix matrix; 203 SkMatrix matrix;
204 matrix.setIDiv(srcTexture->width(), srcTexture->height()); 204 matrix.setIDiv(srcTexture->width(), srcTexture->height());
205 SkRect dstRect(srcRect); 205 SkRect dstRect(srcRect);
206 if (cropToRect && i == 1) { 206 if (cropToRect && i == 1) {
207 dstRect.offset(-dstRect.fLeft, -dstRect.fTop); 207 dstRect.offset(-dstRect.fLeft, -dstRect.fTop);
208 SkRect domain; 208 SkRect domain;
209 matrix.mapRect(&domain, rect); 209 matrix.mapRect(&domain, rect);
210 domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f, 210 domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f,
211 i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f); 211 i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f);
212 SkAutoTUnref<GrFragmentProcessor> fp( GrTextureDomainEffect::Creat e( 212 SkAutoTUnref<GrFragmentProcessor> fp( GrTextureDomainEffect::Creat e(
213 paint.getProcessorDataManager(), 213 paint.getProcessorDataManager(),
214 srcTexture, 214 srcTexture,
215 matrix, 215 matrix,
216 domain, 216 domain,
217 GrTextureDomain::kDecal_Mode, 217 GrTextureDomain::kDecal_Mode,
218 GrTextureParams::kBilerp_FilterMode)); 218 GrTextureParams::kBilerp_FilterMode));
219 paint.addColorFragmentProcessor(fp); 219 paint.addColorFragmentProcessor(fp);
220 } else { 220 } else {
221 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::k Bilerp_FilterMode); 221 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::k Bilerp_FilterMode);
222 paint.addColorTextureProcessor(srcTexture, matrix, params); 222 paint.addColorTextureProcessor(srcTexture, matrix, params);
223 } 223 }
224 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, 224 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
225 i < scaleFactorY ? 0.5f : 1.0f); 225 i < scaleFactorY ? 0.5f : 1.0f);
226 226
227 GrDrawContext* dstDrawContext = context->drawContext(); 227 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
228 if (!dstDrawContext) { 228 if (!dstDrawContext) {
229 return nullptr; 229 return nullptr;
230 } 230 }
231 dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, 231 dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
232 SkMatrix::I(), dstRect, srcRect); 232 SkMatrix::I(), dstRect, srcRect);
233 233
234 srcDrawContext = dstDrawContext; 234 srcDrawContext.swap(&dstDrawContext);
235 srcRect = dstRect; 235 srcRect = dstRect;
236 srcTexture = dstTexture; 236 srcTexture = dstTexture;
237 SkTSwap(dstTexture, tempTexture); 237 SkTSwap(dstTexture, tempTexture);
238 } 238 }
239 239
240 const SkIRect srcIRect = srcRect.roundOut(); 240 const SkIRect srcIRect = srcRect.roundOut();
241 241
242 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i s faster to just 242 // For really small blurs (certainly no wider than 5x5 on desktop gpus) it i s faster to just
243 // launch a single non separable kernel vs two launches 243 // launch a single non separable kernel vs two launches
244 if (sigmaX > 0.0f && sigmaY > 0.0f && 244 if (sigmaX > 0.0f && sigmaY > 0.0f &&
245 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { 245 (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
246 // We shouldn't be scaling because this is a small size blur 246 // We shouldn't be scaling because this is a small size blur
247 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); 247 SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
248 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); 248 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
249 249
250 GrDrawContext* dstDrawContext = context->drawContext(); 250 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
251 if (!dstDrawContext) { 251 if (!dstDrawContext) {
252 return nullptr; 252 return nullptr;
253 } 253 }
254 convolve_gaussian_2d(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect, 254 convolve_gaussian_2d(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
255 srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropT oRect, srcIRect); 255 srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropT oRect, srcIRect);
256 256
257 srcDrawContext = dstDrawContext; 257 srcDrawContext.swap(&dstDrawContext);
258 srcRect = dstRect; 258 srcRect = dstRect;
259 srcTexture = dstTexture; 259 srcTexture = dstTexture;
260 SkTSwap(dstTexture, tempTexture); 260 SkTSwap(dstTexture, tempTexture);
261 261
262 } else { 262 } else {
263 if (sigmaX > 0.0f) { 263 if (sigmaX > 0.0f) {
264 if (scaleFactorX > 1) { 264 if (scaleFactorX > 1) {
265 // TODO: if we pass in the source draw context we don't need thi s here 265 // TODO: if we pass in the source draw context we don't need thi s here
266 if (!srcDrawContext) { 266 if (!srcDrawContext) {
267 srcDrawContext = context->drawContext(); 267 srcDrawContext.reset(context->drawContext());
268 if (!srcDrawContext) { 268 if (!srcDrawContext) {
269 return nullptr; 269 return nullptr;
270 } 270 }
271 } 271 }
272 272
273 // Clear out a radius to the right of the srcRect to prevent the 273 // Clear out a radius to the right of the srcRect to prevent the
274 // X convolution from reading garbage. 274 // X convolution from reading garbage.
275 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 275 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
276 radiusX, srcIRect.height()); 276 radiusX, srcIRect.height());
277 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); 277 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
278 } 278 }
279 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); 279 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
280 280
281 GrDrawContext* dstDrawContext = context->drawContext(); 281 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
282 if (!dstDrawContext) { 282 if (!dstDrawContext) {
283 return nullptr; 283 return nullptr;
284 } 284 }
285 convolve_gaussian(dstDrawContext, dstTexture->asRenderTarget(), clip , srcRect, dstRect, 285 convolve_gaussian(dstDrawContext, dstTexture->asRenderTarget(), clip , srcRect, dstRect,
286 srcTexture, Gr1DKernelEffect::kX_Direction, radius X, sigmaX, 286 srcTexture, Gr1DKernelEffect::kX_Direction, radius X, sigmaX,
287 cropToRect); 287 cropToRect);
288 288
289 srcDrawContext = dstDrawContext; 289 srcDrawContext.swap(&dstDrawContext);
290 srcTexture = dstTexture; 290 srcTexture = dstTexture;
291 srcRect = dstRect; 291 srcRect = dstRect;
292 SkTSwap(dstTexture, tempTexture); 292 SkTSwap(dstTexture, tempTexture);
293 } 293 }
294 294
295 if (sigmaY > 0.0f) { 295 if (sigmaY > 0.0f) {
296 if (scaleFactorY > 1 || sigmaX > 0.0f) { 296 if (scaleFactorY > 1 || sigmaX > 0.0f) {
297 // TODO: if we pass in the source draw context we don't need thi s here 297 // TODO: if we pass in the source draw context we don't need thi s here
298 if (!srcDrawContext) { 298 if (!srcDrawContext) {
299 srcDrawContext = context->drawContext(); 299 srcDrawContext.reset(context->drawContext());
300 if (!srcDrawContext) { 300 if (!srcDrawContext) {
301 return nullptr; 301 return nullptr;
302 } 302 }
303 } 303 }
304 304
305 // Clear out a radius below the srcRect to prevent the Y 305 // Clear out a radius below the srcRect to prevent the Y
306 // convolution from reading garbage. 306 // convolution from reading garbage.
307 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, 307 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
308 srcIRect.width(), radiusY); 308 srcIRect.width(), radiusY);
309 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); 309 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false);
310 } 310 }
311 311
312 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); 312 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
313 313
314 GrDrawContext* dstDrawContext = context->drawContext(); 314 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
315 if (!dstDrawContext) { 315 if (!dstDrawContext) {
316 return nullptr; 316 return nullptr;
317 } 317 }
318 convolve_gaussian(dstDrawContext, dstTexture->asRenderTarget(), clip , srcRect, 318 convolve_gaussian(dstDrawContext, dstTexture->asRenderTarget(), clip , srcRect,
319 dstRect, srcTexture, Gr1DKernelEffect::kY_Directio n, radiusY, sigmaY, 319 dstRect, srcTexture, Gr1DKernelEffect::kY_Directio n, radiusY, sigmaY,
320 cropToRect); 320 cropToRect);
321 321
322 srcDrawContext = dstDrawContext; 322 srcDrawContext.swap(&dstDrawContext);
323 srcTexture = dstTexture; 323 srcTexture = dstTexture;
324 srcRect = dstRect; 324 srcRect = dstRect;
325 SkTSwap(dstTexture, tempTexture); 325 SkTSwap(dstTexture, tempTexture);
326 } 326 }
327 } 327 }
328 328
329 if (scaleFactorX > 1 || scaleFactorY > 1) { 329 if (scaleFactorX > 1 || scaleFactorY > 1) {
330 SkASSERT(srcDrawContext); 330 SkASSERT(srcDrawContext);
331 331
332 // Clear one pixel to the right and below, to accommodate bilinear 332 // Clear one pixel to the right and below, to accommodate bilinear
333 // upsampling. 333 // upsampling.
334 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, 334 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
335 srcIRect.width() + 1, 1); 335 srcIRect.width() + 1, 1);
336 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, fal se); 336 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, fal se);
337 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 337 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
338 1, srcIRect.height()); 338 1, srcIRect.height());
339 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, fal se); 339 srcDrawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, fal se);
340 SkMatrix matrix; 340 SkMatrix matrix;
341 matrix.setIDiv(srcTexture->width(), srcTexture->height()); 341 matrix.setIDiv(srcTexture->width(), srcTexture->height());
342 342
343 GrPaint paint; 343 GrPaint paint;
344 // FIXME: this should be mitchell, not bilinear. 344 // FIXME: this should be mitchell, not bilinear.
345 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile rp_FilterMode); 345 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBile rp_FilterMode);
346 paint.addColorTextureProcessor(srcTexture, matrix, params); 346 paint.addColorTextureProcessor(srcTexture, matrix, params);
347 347
348 SkRect dstRect(srcRect); 348 SkRect dstRect(srcRect);
349 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); 349 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
350 350
351 GrDrawContext* dstDrawContext = context->drawContext(); 351 SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
352 if (!dstDrawContext) { 352 if (!dstDrawContext) {
353 return nullptr; 353 return nullptr;
354 } 354 }
355 dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, 355 dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
356 SkMatrix::I(), dstRect, srcRect); 356 SkMatrix::I(), dstRect, srcRect);
357 357
358 srcDrawContext = dstDrawContext; 358 srcDrawContext.swap(&dstDrawContext);
359 srcRect = dstRect; 359 srcRect = dstRect;
360 srcTexture = dstTexture; 360 srcTexture = dstTexture;
361 SkTSwap(dstTexture, tempTexture); 361 SkTSwap(dstTexture, tempTexture);
362 } 362 }
363 363
364 return SkRef(srcTexture); 364 return SkRef(srcTexture);
365 } 365 }
366 #endif 366 #endif
367 367
368 } 368 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698