Bug 781380 - Fix cairo surface memory leak in DrawTargetCairo::CreateSimilarDrawTarget(). r=roc
authorAnthony Jones <ajones@mozilla.com>
Thu, 23 Aug 2012 19:50:59 -0400
changeset 105287 c97a0ffcf50061f24a924001ca46064f18ef3857
parent 105286 14f0f28ad1a99989b4885d376dbd0ea6794d57ee
child 105288 e137f28dfe7043a5ab7531fd2989c610092826a1
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersroc
bugs781380
milestone17.0a1
Bug 781380 - Fix cairo surface memory leak in DrawTargetCairo::CreateSimilarDrawTarget(). r=roc
gfx/2d/DrawTargetCairo.cpp
gfx/2d/DrawTargetCairo.h
--- a/gfx/2d/DrawTargetCairo.cpp
+++ b/gfx/2d/DrawTargetCairo.cpp
@@ -805,27 +805,33 @@ DrawTargetCairo::CreateSimilarDrawTarget
     target->Init(similar, aSize);
     return target;
   }
 
   return nullptr;
 }
 
 bool
-DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize)
+DrawTargetCairo::InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize)
 {
   mContext = cairo_create(aSurface);
   mSurface = aSurface;
-  cairo_surface_reference(mSurface);
   mSize = aSize;
   mFormat = CairoContentToGfxFormat(cairo_surface_get_content(aSurface));
 
   return true;
 }
 
+bool
+DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize)
+{
+  cairo_surface_reference(aSurface);
+  return InitAlreadyReferenced(aSurface, aSize);
+}
+
 void *
 DrawTargetCairo::GetNativeSurface(NativeSurfaceType aType)
 {
   if (aType == NATIVE_SURFACE_CAIRO_SURFACE) {
     return cairo_get_target(mContext);
   }
 
   return nullptr;
--- a/gfx/2d/DrawTargetCairo.h
+++ b/gfx/2d/DrawTargetCairo.h
@@ -130,16 +130,19 @@ public:
   virtual void SetTransform(const Matrix& aTransform);
 
   // Call to set up aContext for drawing (with the current transform, etc).
   // Pass the path you're going to be using if you have one.
   // Implicitly calls WillChange(aPath).
   void PrepareForDrawing(cairo_t* aContext, const Path* aPath = nullptr);
 
 private: // methods
+  // Init cairo surface without doing a cairo_surface_reference() call.
+  bool InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize);
+
   enum DrawPatternType { DRAW_FILL, DRAW_STROKE };
   void DrawPattern(const Pattern& aPattern,
                    const StrokeOptions& aStrokeOptions,
                    const DrawOptions& aOptions,
                    DrawPatternType aDrawType);
 
   // Copy-on-write support for snapshot surfaces.
   friend class SourceSurfaceCairo;