Limit lifetime of GrDrawContext objects
authorrobertphillips <robertphillips@google.com>
Tue, 1 Sep 2015 15:34:28 +0000 (08:34 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 1 Sep 2015 15:34:28 +0000 (08:34 -0700)
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

17 files changed:
gm/texdata.cpp
src/core/SkImageFilter.cpp
src/effects/SkAlphaThresholdFilter.cpp
src/effects/SkBlurMaskFilter.cpp
src/effects/SkDisplacementMapEffect.cpp
src/effects/SkGpuBlurUtils.cpp
src/effects/SkLightingImageFilter.cpp
src/effects/SkMorphologyImageFilter.cpp
src/effects/SkXfermodeImageFilter.cpp
src/gpu/GrBlurUtils.cpp
src/gpu/GrContext.cpp
src/gpu/GrLayerCache.cpp
src/gpu/GrRenderTarget.cpp
src/gpu/SkGpuDevice.cpp
src/gpu/SkGr.cpp
src/gpu/effects/GrConfigConversionEffect.cpp
src/image/SkImage_Gpu.cpp

index 13a1c2a..1a807c7 100644 (file)
@@ -39,7 +39,7 @@ protected:
     void onDraw(SkCanvas* canvas) override {
         GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
         GrContext* ctx = canvas->getGrContext();
-        GrDrawContext* drawContext = ctx ? ctx->drawContext() : nullptr;
+        SkAutoTUnref<GrDrawContext> drawContext(ctx ? ctx->drawContext() : nullptr);
         if (drawContext && target) {
             SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
             static const int stride = 2 * S;
index cda3e05..3256294 100644 (file)
@@ -354,7 +354,7 @@ bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Cont
         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);
index b28a62b..066a91f 100644 (file)
@@ -277,7 +277,7 @@ bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp,
             return false;
         }
 
-        GrDrawContext* drawContext = context->drawContext();
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
         if (drawContext) {
             GrPaint grPaint;
             grPaint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
index 1bd708b..2ef86b0 100644 (file)
@@ -1226,7 +1226,7 @@ bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src,
             paint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op);
         }
 
-        GrDrawContext* drawContext = context->drawContext();
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
         if (!drawContext) {
             return false;
         }
index 4929071..e87a871 100644 (file)
@@ -453,7 +453,7 @@ bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src,
     matrix.setTranslate(-SkIntToScalar(colorBounds.x()),
                         -SkIntToScalar(colorBounds.y()));
 
-    GrDrawContext* drawContext = context->drawContext();
+    SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
     if (!drawContext) {
         return false;
     }
index 8b0e5fb..fd27cc3 100644 (file)
@@ -196,7 +196,7 @@ GrTexture* GaussianBlur(GrContext* context,
         return nullptr;
     }
 
-    GrDrawContext* srcDrawContext = nullptr;
+    SkAutoTUnref<GrDrawContext> srcDrawContext;
 
     for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
         GrPaint paint;
@@ -224,14 +224,14 @@ GrTexture* GaussianBlur(GrContext* context,
         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 @@ GrTexture* GaussianBlur(GrContext* context,
         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 @@ GrTexture* GaussianBlur(GrContext* context,
             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 @@ GrTexture* GaussianBlur(GrContext* context,
             }
             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 @@ GrTexture* GaussianBlur(GrContext* context,
                               srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
                               cropToRect);
 
-            srcDrawContext = dstDrawContext;
+            srcDrawContext.swap(&dstDrawContext);
             srcTexture = dstTexture;
             srcRect = dstRect;
             SkTSwap(dstTexture, tempTexture);
@@ -296,7 +296,7 @@ GrTexture* GaussianBlur(GrContext* context,
             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 @@ GrTexture* GaussianBlur(GrContext* context,
 
             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 @@ GrTexture* GaussianBlur(GrContext* context,
                               dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
                               cropToRect);
 
-            srcDrawContext = dstDrawContext;
+            srcDrawContext.swap(&dstDrawContext);
             srcTexture = dstTexture;
             srcRect = dstRect;
             SkTSwap(dstTexture, tempTexture);
@@ -348,14 +348,14 @@ GrTexture* GaussianBlur(GrContext* context,
         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);
index 240a693..c46cc67 100644 (file)
@@ -408,7 +408,7 @@ bool SkLightingImageFilterInternal::filterImageGPU(Proxy* proxy,
     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;
     }
index 2c99af9..440d88d 100644 (file)
@@ -573,7 +573,7 @@ bool apply_morphology(const SkBitmap& input,
         if (nullptr == scratch) {
             return false;
         }
-        GrDrawContext* dstDrawContext = context->drawContext();
+        SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
         if (!dstDrawContext) {
             return false;
         }
@@ -596,7 +596,7 @@ bool apply_morphology(const SkBitmap& input,
         if (nullptr == scratch) {
             return false;
         }
-        GrDrawContext* dstDrawContext = context->drawContext();
+        SkAutoTUnref<GrDrawContext> dstDrawContext(context->drawContext());
         if (!dstDrawContext) {
             return false;
         }
index 7c4a94b..9ffbe62 100644 (file)
@@ -192,7 +192,7 @@ bool SkXfermodeImageFilter::filterImageGPU(Proxy* proxy,
     paint.addColorFragmentProcessor(foregroundDomain.get());
     paint.addColorFragmentProcessor(xferProcessor)->unref();
 
-    GrDrawContext* drawContext = context->drawContext();
+    SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
     if (!drawContext) {
         return false;
     }
index a6eeae6..039a12c 100644 (file)
@@ -122,7 +122,7 @@ static GrTexture* create_mask_GPU(GrContext* context,
 
     SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
 
-    GrDrawContext* drawContext = context->drawContext();
+    SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
     if (!drawContext) {
         return nullptr;
     }
index 6dae8c7..4e8060b 100644 (file)
@@ -124,7 +124,8 @@ GrDrawContext* GrContext::DrawingMgr::drawContext(const SkSurfaceProps* surfaceP
                 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 @@ bool GrContext::writeSurfacePixels(GrSurface* surface,
             }
             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 @@ bool GrContext::readSurfacePixels(GrSurface* src,
             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 @@ void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRe
         return;
     }
 
-    GrDrawContext* drawContext = this->drawContext();
+    SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
     if (!drawContext) {
         return;
     }
index f33384c..27cb55b 100644 (file)
@@ -467,7 +467,7 @@ void GrLayerCache::purgeAll() {
 
     SkASSERT(0 == fPictureHash.count());
 
-    GrDrawContext* drawContext = fContext->drawContext();
+    SkAutoTUnref<GrDrawContext> drawContext(fContext->drawContext());
 
     if (drawContext) {
         drawContext->discard(fAtlas->getTexture()->asRenderTarget());
index 0908349..5cb34e8 100644 (file)
 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;
     }
index b9c2f7c..fd03acc 100644 (file)
@@ -196,7 +196,7 @@ SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
     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 @@ void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
     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()));
 }
 
 ///////////////////////////////////////////////////////////////////////////////
index 4293194..29c4572 100644 (file)
@@ -312,7 +312,7 @@ GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
     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 @@ static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKe
     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;
     }
index 3f68268..042e5f8 100644 (file)
@@ -226,7 +226,7 @@ void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context
         paint1.addColorFragmentProcessor(pmToUPM1);
 
 
-        GrDrawContext* readDrawContext = context->drawContext();
+        SkAutoTUnref<GrDrawContext> readDrawContext(context->drawContext());
         if (!readDrawContext) {
             failed = true;
             break;
@@ -243,7 +243,7 @@ void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context
 
         paint2.addColorFragmentProcessor(upmToPM);
 
-        GrDrawContext* tempDrawContext = context->drawContext();
+        SkAutoTUnref<GrDrawContext> tempDrawContext(context->drawContext());
         if (!tempDrawContext) {
             failed = true;
             break;
@@ -257,7 +257,7 @@ void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context
 
         paint3.addColorFragmentProcessor(pmToUPM2);
 
-        readDrawContext = context->drawContext();
+        readDrawContext.reset(context->drawContext());
         if (!readDrawContext) {
             failed = true;
             break;
index 6655e39..fdcb871 100644 (file)
@@ -254,7 +254,11 @@ SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorS
 
     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,