Limit lifetime of GrDrawContext objects
GrDrawContext's are about to become real allocated objects. This CL sets up the machinery so they won't leak.
Review URL: https://codereview.chromium.org/1321353002
diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp
index cda3e05..3256294 100644
--- a/src/core/SkImageFilter.cpp
+++ b/src/core/SkImageFilter.cpp
@@ -354,7 +354,7 @@
SkASSERT(fp);
paint.addColorFragmentProcessor(fp)->unref();
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (drawContext) {
drawContext->drawNonAARectToRect(dst->asRenderTarget(), clip, paint, SkMatrix::I(),
dstRect, srcRect);
diff --git a/src/effects/SkAlphaThresholdFilter.cpp b/src/effects/SkAlphaThresholdFilter.cpp
index b28a62b..066a91f 100644
--- a/src/effects/SkAlphaThresholdFilter.cpp
+++ b/src/effects/SkAlphaThresholdFilter.cpp
@@ -277,7 +277,7 @@
return false;
}
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (drawContext) {
GrPaint grPaint;
grPaint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
diff --git a/src/effects/SkBlurMaskFilter.cpp b/src/effects/SkBlurMaskFilter.cpp
index 1bd708b..2ef86b0 100644
--- a/src/effects/SkBlurMaskFilter.cpp
+++ b/src/effects/SkBlurMaskFilter.cpp
@@ -1226,7 +1226,7 @@
paint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op);
}
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return false;
}
diff --git a/src/effects/SkDisplacementMapEffect.cpp b/src/effects/SkDisplacementMapEffect.cpp
index 4929071..e87a871 100644
--- a/src/effects/SkDisplacementMapEffect.cpp
+++ b/src/effects/SkDisplacementMapEffect.cpp
@@ -453,7 +453,7 @@
matrix.setTranslate(-SkIntToScalar(colorBounds.x()),
-SkIntToScalar(colorBounds.y()));
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return false;
}
diff --git a/src/effects/SkGpuBlurUtils.cpp b/src/effects/SkGpuBlurUtils.cpp
index 8b0e5fb..fd27cc3 100644
--- a/src/effects/SkGpuBlurUtils.cpp
+++ b/src/effects/SkGpuBlurUtils.cpp
@@ -196,7 +196,7 @@
return nullptr;
}
- GrDrawContext* srcDrawContext = nullptr;
+ SkAutoTUnref<GrDrawContext> srcDrawContext;
for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
GrPaint paint;
@@ -224,14 +224,14 @@
scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
i < scaleFactorY ? 0.5f : 1.0f);
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return nullptr;
}
dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
SkMatrix::I(), dstRect, srcRect);
- srcDrawContext = dstDrawContext;
+ srcDrawContext.swap(&dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
@@ -247,14 +247,14 @@
SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return nullptr;
}
convolve_gaussian_2d(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
- srcDrawContext = dstDrawContext;
+ srcDrawContext.swap(&dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
@@ -264,7 +264,7 @@
if (scaleFactorX > 1) {
// TODO: if we pass in the source draw context we don't need this here
if (!srcDrawContext) {
- srcDrawContext = context->drawContext();
+ srcDrawContext.reset(context->drawContext());
if (!srcDrawContext) {
return nullptr;
}
@@ -278,7 +278,7 @@
}
SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return nullptr;
}
@@ -286,7 +286,7 @@
srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
cropToRect);
- srcDrawContext = dstDrawContext;
+ srcDrawContext.swap(&dstDrawContext);
srcTexture = dstTexture;
srcRect = dstRect;
SkTSwap(dstTexture, tempTexture);
@@ -296,7 +296,7 @@
if (scaleFactorY > 1 || sigmaX > 0.0f) {
// TODO: if we pass in the source draw context we don't need this here
if (!srcDrawContext) {
- srcDrawContext = context->drawContext();
+ srcDrawContext.reset(context->drawContext());
if (!srcDrawContext) {
return nullptr;
}
@@ -311,7 +311,7 @@
SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return nullptr;
}
@@ -319,7 +319,7 @@
dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
cropToRect);
- srcDrawContext = dstDrawContext;
+ srcDrawContext.swap(&dstDrawContext);
srcTexture = dstTexture;
srcRect = dstRect;
SkTSwap(dstTexture, tempTexture);
@@ -348,14 +348,14 @@
SkRect dstRect(srcRect);
scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return nullptr;
}
dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
SkMatrix::I(), dstRect, srcRect);
- srcDrawContext = dstDrawContext;
+ srcDrawContext.swap(&dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
diff --git a/src/effects/SkLightingImageFilter.cpp b/src/effects/SkLightingImageFilter.cpp
index 240a693..c46cc67 100644
--- a/src/effects/SkLightingImageFilter.cpp
+++ b/src/effects/SkLightingImageFilter.cpp
@@ -408,7 +408,7 @@
SkRect bottom = SkRect::MakeXYWH(1, dstRect.height() - 1, dstRect.width() - 2, 1);
SkRect bottomRight = SkRect::MakeXYWH(dstRect.width() - 1, dstRect.height() - 1, 1, 1);
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return false;
}
diff --git a/src/effects/SkMorphologyImageFilter.cpp b/src/effects/SkMorphologyImageFilter.cpp
index 2c99af9..440d88d 100644
--- a/src/effects/SkMorphologyImageFilter.cpp
+++ b/src/effects/SkMorphologyImageFilter.cpp
@@ -573,7 +573,7 @@
if (nullptr == scratch) {
return false;
}
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return false;
}
@@ -596,7 +596,7 @@
if (nullptr == scratch) {
return false;
}
- GrDrawContext* dstDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
if (!dstDrawContext) {
return false;
}
diff --git a/src/effects/SkXfermodeImageFilter.cpp b/src/effects/SkXfermodeImageFilter.cpp
index 7c4a94b..9ffbe62 100644
--- a/src/effects/SkXfermodeImageFilter.cpp
+++ b/src/effects/SkXfermodeImageFilter.cpp
@@ -192,7 +192,7 @@
paint.addColorFragmentProcessor(foregroundDomain.get());
paint.addColorFragmentProcessor(xferProcessor)->unref();
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return false;
}
diff --git a/src/gpu/GrBlurUtils.cpp b/src/gpu/GrBlurUtils.cpp
index a6eeae6..039a12c 100644
--- a/src/gpu/GrBlurUtils.cpp
+++ b/src/gpu/GrBlurUtils.cpp
@@ -122,7 +122,7 @@
SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return nullptr;
}
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 6dae8c7..4e8060b 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -124,7 +124,8 @@
new GrDrawContext(fContext, fDrawTarget, props);
}
- return fDrawContext[props.pixelGeometry()][props.isUseDeviceIndependentFonts()];
+ // For now, everyone gets a faux creation ref
+ return SkRef(fDrawContext[props.pixelGeometry()][props.isUseDeviceIndependentFonts()]);
}
////////////////////////////////////////////////////////////////////////////////
@@ -422,7 +423,7 @@
}
SkMatrix matrix;
matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
- GrDrawContext* drawContext = this->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
if (!drawContext) {
return false;
}
@@ -533,7 +534,7 @@
if (fp) {
paint.addColorFragmentProcessor(fp);
SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
- GrDrawContext* drawContext = this->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
drawContext->drawRect(temp->asRenderTarget(), GrClip::WideOpen(), paint,
SkMatrix::I(), rect, nullptr);
surfaceToRead.reset(SkRef(temp.get()));
@@ -609,7 +610,7 @@
return;
}
- GrDrawContext* drawContext = this->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
if (!drawContext) {
return;
}
diff --git a/src/gpu/GrLayerCache.cpp b/src/gpu/GrLayerCache.cpp
index f33384c..27cb55b 100644
--- a/src/gpu/GrLayerCache.cpp
+++ b/src/gpu/GrLayerCache.cpp
@@ -467,7 +467,7 @@
SkASSERT(0 == fPictureHash.count());
- GrDrawContext* drawContext = fContext->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(fContext->drawContext());
if (drawContext) {
drawContext->discard(fAtlas->getTexture()->asRenderTarget());
diff --git a/src/gpu/GrRenderTarget.cpp b/src/gpu/GrRenderTarget.cpp
index 0908349..5cb34e8 100644
--- a/src/gpu/GrRenderTarget.cpp
+++ b/src/gpu/GrRenderTarget.cpp
@@ -18,7 +18,11 @@
void GrRenderTarget::discard() {
// go through context so that all necessary flushing occurs
GrContext* context = this->getContext();
- GrDrawContext* drawContext = context ? context->drawContext() : nullptr;
+ if (!context) {
+ return;
+ }
+
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return;
}
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index b9c2f7c..fd03acc 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -196,7 +196,7 @@
fLegacyBitmap.setInfo(info);
fLegacyBitmap.setPixelRef(pr)->unref();
- fDrawContext.reset(SkRef(fContext->drawContext(&this->surfaceProps())));
+ fDrawContext.reset(fContext->drawContext(&this->surfaceProps()));
}
GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
@@ -373,7 +373,7 @@
SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
fLegacyBitmap.setPixelRef(pr)->unref();
- fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext(&this->surfaceProps())));
+ fDrawContext.reset(fRenderTarget->getContext()->drawContext(&this->surfaceProps()));
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 4293194..29c4572 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -312,7 +312,7 @@
SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
SkRect localRect = SkRect::MakeWH(1.f, 1.f);
- GrDrawContext* drawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return nullptr;
}
@@ -473,7 +473,7 @@
SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
SkIntToScalar(yuvInfo.fSize[0].fHeight));
- GrDrawContext* drawContext = ctx->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext());
if (!drawContext) {
return nullptr;
}
diff --git a/src/gpu/effects/GrConfigConversionEffect.cpp b/src/gpu/effects/GrConfigConversionEffect.cpp
index 3f68268..042e5f8 100644
--- a/src/gpu/effects/GrConfigConversionEffect.cpp
+++ b/src/gpu/effects/GrConfigConversionEffect.cpp
@@ -226,7 +226,7 @@
paint1.addColorFragmentProcessor(pmToUPM1);
- GrDrawContext* readDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> readDrawContext(context->drawContext());
if (!readDrawContext) {
failed = true;
break;
@@ -243,7 +243,7 @@
paint2.addColorFragmentProcessor(upmToPM);
- GrDrawContext* tempDrawContext = context->drawContext();
+ SkAutoTUnref<GrDrawContext> tempDrawContext(context->drawContext());
if (!tempDrawContext) {
failed = true;
break;
@@ -257,7 +257,7 @@
paint3.addColorFragmentProcessor(pmToUPM2);
- readDrawContext = context->drawContext();
+ readDrawContext.reset(context->drawContext());
if (!readDrawContext) {
failed = true;
break;
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index 6655e39..fdcb871 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -254,7 +254,11 @@
const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
SkIntToScalar(dstDesc.fHeight));
- GrDrawContext* drawContext = ctx->drawContext();
+ SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext());
+ if (!drawContext) {
+ return nullptr;
+ }
+
drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
ctx->flushSurfaceWrites(dst);
return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,