Bug 1116905 - part 3 - remove dependence on implicit conversion from T* to TemporaryRef<T>, gfx changes; r=jrmuizel
authorNathan Froyd <froydnj@mozilla.com>
Thu, 30 Apr 2015 15:20:30 -0400
changeset 266210 f1f569c211024cf1855f5fbedb17e8648a915ae5
parent 266209 43684b88e52e138611f58dfb632305681aa564f9
child 266211 b1c86100dbce2eef1686b57b59c76ff78cfbc376
push id2231
push usermichael.l.comella@gmail.com
push dateFri, 22 May 2015 20:04:59 +0000
reviewersjrmuizel
bugs1116905
milestone41.0a1
Bug 1116905 - part 3 - remove dependence on implicit conversion from T* to TemporaryRef<T>, gfx changes; r=jrmuizel
gfx/2d/DataSourceSurface.cpp
gfx/2d/DrawTargetCG.cpp
gfx/2d/DrawTargetCairo.cpp
gfx/2d/DrawTargetD2D.cpp
gfx/2d/DrawTargetD2D1.cpp
gfx/2d/DrawTargetDual.cpp
gfx/2d/DrawTargetDual.h
gfx/2d/DrawTargetRecording.cpp
gfx/2d/DrawTargetSkia.cpp
gfx/2d/DrawTargetTiled.cpp
gfx/2d/Factory.cpp
gfx/2d/FilterNodeD2D1.cpp
gfx/2d/FilterNodeSoftware.cpp
gfx/2d/PathCG.cpp
gfx/2d/PathCairo.cpp
gfx/2d/PathD2D.cpp
gfx/2d/PathRecording.cpp
gfx/2d/PathSkia.cpp
gfx/2d/ScaledFontBase.cpp
gfx/2d/ScaledFontMac.cpp
gfx/2d/SourceSurfaceCG.cpp
gfx/2d/SourceSurfaceCG.h
gfx/2d/SourceSurfaceCairo.cpp
gfx/2d/SourceSurfaceD2D1.cpp
gfx/gl/SharedSurface.cpp
gfx/layers/Effects.h
gfx/layers/ImageContainer.cpp
gfx/layers/ImageContainer.h
gfx/layers/basic/BasicImages.cpp
gfx/layers/client/CanvasClient.cpp
gfx/layers/client/ContentClient.cpp
gfx/layers/client/TextureClientRecycleAllocator.cpp
gfx/layers/composite/TextureHost.cpp
gfx/layers/d3d9/CompositorD3D9.cpp
gfx/layers/ipc/CompositorBench.cpp
gfx/thebes/gfxUtils.cpp
--- a/gfx/2d/DataSourceSurface.cpp
+++ b/gfx/2d/DataSourceSurface.cpp
@@ -7,13 +7,15 @@
 #include "DataSourceSurfaceWrapper.h"
 
 namespace mozilla {
 namespace gfx {
 
 TemporaryRef<DataSourceSurface>
 DataSourceSurface::GetDataSurface()
 {
-  return (GetType() == SurfaceType::DATA) ? this : new DataSourceSurfaceWrapper(this);
+  RefPtr<DataSourceSurface> surface =
+    (GetType() == SurfaceType::DATA) ? this : new DataSourceSurfaceWrapper(this);
+  return surface.forget();
 }
 
 }
 }
--- a/gfx/2d/DrawTargetCG.cpp
+++ b/gfx/2d/DrawTargetCG.cpp
@@ -186,17 +186,17 @@ DrawTargetCG::GetBackendType() const
   }
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetCG::Snapshot()
 {
   if (!mSnapshot) {
     if (GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE) {
-      return new SourceSurfaceCGIOSurfaceContext(this);
+      return MakeAndAddRef<SourceSurfaceCGIOSurfaceContext>(this);
     }
     Flush();
     mSnapshot = new SourceSurfaceCGBitmapContext(this);
   }
 
   return mSnapshot;
 }
 
@@ -257,17 +257,18 @@ GetRetainedImageFromSourceSurface(Source
                            data->Stride(), data->GetFormat());
     }
   }
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetCG::OptimizeSourceSurface(SourceSurface *aSurface) const
 {
-  return aSurface;
+  RefPtr<SourceSurface> surface(aSurface);
+  return surface.forget();
 }
 
 class UnboundnessFixer
 {
     CGRect mClipBounds;
     CGLayerRef mLayer;
     CGContextRef mLayerCg;
   public:
@@ -460,17 +461,17 @@ class GradientStopsCG : public GradientS
   ExtendMode mExtend;
 };
 
 TemporaryRef<GradientStops>
 DrawTargetCG::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
                                   ExtendMode aExtendMode) const
 {
   std::vector<GradientStop> stops(aStops, aStops+aNumStops);
-  return new GradientStopsCG(mColorSpace, stops, aExtendMode);
+  return MakeAndAddRef<GradientStopsCG>(mColorSpace, stops, aExtendMode);
 }
 
 static void
 UpdateLinearParametersToIncludePoint(double *min_t, double *max_t,
                                      CGPoint *start,
                                      double dx, double dy,
                                      double x, double y)
 {
@@ -1900,17 +1901,17 @@ DrawTargetCG::Init(BackendType aType, co
   
   // Calling Init with aData == nullptr will allocate.
   return Init(aType, nullptr, aSize, stride, aFormat);
 }
 
 TemporaryRef<PathBuilder>
 DrawTargetCG::CreatePathBuilder(FillRule aFillRule) const
 {
-  return new PathBuilderCG(aFillRule);
+  return MakeAndAddRef<PathBuilderCG>(aFillRule);
 }
 
 void*
 DrawTargetCG::GetNativeSurface(NativeSurfaceType aType)
 {
   if ((aType == NativeSurfaceType::CGCONTEXT && GetContextType(mCg) == CG_CONTEXT_TYPE_BITMAP) ||
       (aType == NativeSurfaceType::CGCONTEXT_ACCELERATED && GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE)) {
     return mCg;
--- a/gfx/2d/DrawTargetCairo.cpp
+++ b/gfx/2d/DrawTargetCairo.cpp
@@ -1321,17 +1321,17 @@ DrawTargetCairo::PopClip()
 
   MOZ_ASSERT(cairo_status(mContext) || GetTransform() == Matrix(mat.xx, mat.yx, mat.xy, mat.yy, mat.x0, mat.y0),
              "Transforms are out of sync");
 }
 
 TemporaryRef<PathBuilder>
 DrawTargetCairo::CreatePathBuilder(FillRule aFillRule /* = FillRule::FILL_WINDING */) const
 {
-  return new PathBuilderCairo(aFillRule);
+  return MakeAndAddRef<PathBuilderCairo>(aFillRule);
 }
 
 void
 DrawTargetCairo::ClearSurfaceForUnboundedSource(const CompositionOp &aOperator)
 {
   if (aOperator != CompositionOp::OP_SOURCE)
     return;
   cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
@@ -1341,17 +1341,17 @@ DrawTargetCairo::ClearSurfaceForUnbounde
   cairo_paint(mContext);
 }
 
 
 TemporaryRef<GradientStops>
 DrawTargetCairo::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
                                      ExtendMode aExtendMode) const
 {
-  return new GradientStopsCairo(aStops, aNumStops, aExtendMode);
+  return MakeAndAddRef<GradientStopsCairo>(aStops, aNumStops, aExtendMode);
 }
 
 TemporaryRef<FilterNode>
 DrawTargetCairo::CreateFilter(FilterType aType)
 {
   return FilterNodeSoftware::Create(aType);
 }
 
@@ -1395,41 +1395,42 @@ DestroyPixmap(void *data)
 {
   delete static_cast<DestroyPixmapClosure*>(data);
 }
 #endif
 
 TemporaryRef<SourceSurface>
 DrawTargetCairo::OptimizeSourceSurface(SourceSurface *aSurface) const
 {
+  RefPtr<SourceSurface> surface(aSurface);
 #ifdef CAIRO_HAS_XLIB_SURFACE
   cairo_surface_type_t ctype = cairo_surface_get_type(mSurface);
   if (aSurface->GetType() == SurfaceType::CAIRO &&
       cairo_surface_get_type(
         static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface()) == ctype) {
-    return aSurface;
+    return surface.forget();
   }
 
   if (ctype != CAIRO_SURFACE_TYPE_XLIB) {
-    return aSurface;
+    return surface.forget();
   }
 
   IntSize size = aSurface->GetSize();
   if (!size.width || !size.height) {
-    return aSurface;
+    return surface.forget();
   }
 
   // Although the dimension parameters in the xCreatePixmapReq wire protocol are
   // 16-bit unsigned integers, the server's CreatePixmap returns BadAlloc if
   // either dimension cannot be represented by a 16-bit *signed* integer.
   #define XLIB_IMAGE_SIDE_SIZE_LIMIT 0x7fff
 
   if (size.width > XLIB_IMAGE_SIDE_SIZE_LIMIT ||
       size.height > XLIB_IMAGE_SIDE_SIZE_LIMIT) {
-    return aSurface;
+    return surface.forget();
   }
 
   SurfaceFormat format = aSurface->GetFormat();
   Screen *screen = cairo_xlib_surface_get_screen(mSurface);
   Display *dpy = DisplayOfScreen(screen);
   XRenderPictFormat* xrenderFormat = nullptr;
   switch (format) {
   case SurfaceFormat::B8G8R8A8:
@@ -1437,70 +1438,70 @@ DrawTargetCairo::OptimizeSourceSurface(S
     break;
   case SurfaceFormat::B8G8R8X8:
     xrenderFormat = XRenderFindStandardFormat(dpy, PictStandardRGB24);
     break;
   case SurfaceFormat::A8:
     xrenderFormat = XRenderFindStandardFormat(dpy, PictStandardA8);
     break;
   default:
-    return aSurface;
+    return surface.forget();
   }
   if (!xrenderFormat) {
-    return aSurface;
+    return surface.forget();
   }
 
   Drawable pixmap = XCreatePixmap(dpy, RootWindowOfScreen(screen),
                                   size.width, size.height,
                                   xrenderFormat->depth);
   if (!pixmap) {
-    return aSurface;
+    return surface.forget();
   }
 
   ScopedDeletePtr<DestroyPixmapClosure> closure(
     new DestroyPixmapClosure(pixmap, screen));
 
   ScopedCairoSurface csurf(
     cairo_xlib_surface_create_with_xrender_format(dpy, pixmap,
                                                   screen, xrenderFormat,
                                                   size.width, size.height));
   if (!csurf || cairo_surface_status(csurf)) {
-    return aSurface;
+    return surface.forget();
   }
 
   cairo_surface_set_user_data(csurf, &gDestroyPixmapKey,
                               closure.forget(), DestroyPixmap);
 
   RefPtr<DrawTargetCairo> dt = new DrawTargetCairo();
   if (!dt->Init(csurf, size, &format)) {
-    return aSurface;
+    return surface.forget();
   }
 
   dt->CopySurface(aSurface,
                   IntRect(0, 0, size.width, size.height),
                   IntPoint(0, 0));
   dt->Flush();
 
-  return new SourceSurfaceCairo(csurf, size, format);
+  surface = new SourceSurfaceCairo(csurf, size, format);
 #endif
 
-  return aSurface;
+  return surface.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetCairo::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
 {
   if (aSurface.mType == NativeSurfaceType::CAIRO_SURFACE) {
     if (aSurface.mSize.width <= 0 ||
         aSurface.mSize.height <= 0) {
       gfxWarning() << "Can't create a SourceSurface without a valid size";
       return nullptr;
     }
     cairo_surface_t* surf = static_cast<cairo_surface_t*>(aSurface.mSurface);
-    return new SourceSurfaceCairo(surf, aSurface.mSize, aSurface.mFormat);
+    return MakeAndAddRef<SourceSurfaceCairo>(surf, aSurface.mSize, aSurface.mFormat);
   }
 
   return nullptr;
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetCairo::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
--- a/gfx/2d/DrawTargetD2D.cpp
+++ b/gfx/2d/DrawTargetD2D.cpp
@@ -1202,17 +1202,18 @@ DrawTargetD2D::CreateSourceSurfaceFromDa
   return newSurf.forget();
 }
 
 TemporaryRef<SourceSurface> 
 DrawTargetD2D::OptimizeSourceSurface(SourceSurface *aSurface) const
 {
   if (aSurface->GetType() == SurfaceType::D2D1_BITMAP ||
       aSurface->GetType() == SurfaceType::D2D1_DRAWTARGET) {
-    return aSurface;
+    RefPtr<SourceSurface> surface(aSurface);
+    return surface.forget();
   }
 
   RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
 
   DataSourceSurface::MappedSurface map;
   if (!data->Map(DataSourceSurface::MapType::READ, &map)) {
     return nullptr;
   }
@@ -1279,17 +1280,17 @@ DrawTargetD2D::CreatePathBuilder(FillRul
     gfxWarning() << "Failed to access Direct2D Path Geometry. Code: " << hexa(hr);
     return nullptr;
   }
 
   if (aFillRule == FillRule::FILL_WINDING) {
     sink->SetFillMode(D2D1_FILL_MODE_WINDING);
   }
 
-  return new PathBuilderD2D(sink, path, aFillRule, BackendType::DIRECT2D);
+  return MakeAndAddRef<PathBuilderD2D>(sink, path, aFillRule, BackendType::DIRECT2D);
 }
 
 TemporaryRef<GradientStops>
 DrawTargetD2D::CreateGradientStops(GradientStop *rawStops, uint32_t aNumStops, ExtendMode aExtendMode) const
 {
   D2D1_GRADIENT_STOP *stops = new D2D1_GRADIENT_STOP[aNumStops];
 
   for (uint32_t i = 0; i < aNumStops; i++) {
@@ -1305,17 +1306,17 @@ DrawTargetD2D::CreateGradientStops(Gradi
                                       byRef(stopCollection));
   delete [] stops;
 
   if (FAILED(hr)) {
     gfxWarning() << "Failed to create GradientStopCollection. Code: " << hexa(hr);
     return nullptr;
   }
 
-  return new GradientStopsD2D(stopCollection, Factory::GetDirect3D11Device());
+  return MakeAndAddRef<GradientStopsD2D>(stopCollection, Factory::GetDirect3D11Device());
 }
 
 TemporaryRef<FilterNode>
 DrawTargetD2D::CreateFilter(FilterType aType)
 {
   RefPtr<ID2D1DeviceContext> dc;
   HRESULT hr = mRT->QueryInterface((ID2D1DeviceContext**)byRef(dc));
 
--- a/gfx/2d/DrawTargetD2D1.cpp
+++ b/gfx/2d/DrawTargetD2D1.cpp
@@ -652,17 +652,17 @@ DrawTargetD2D1::CreateSourceSurfaceFromD
                                  D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE, D2DPixelFormat(aFormat)),
                                  byRef(bitmap));
 
   if (FAILED(hr) || !bitmap) {
     gfxCriticalError(CriticalLog::DefaultOptions(Factory::ReasonableSurfaceSize(aSize))) << "[D2D1.1] 1CreateBitmap failure " << aSize << " Code: " << hexa(hr);
     return nullptr;
   }
 
-  return new SourceSurfaceD2D1(bitmap.get(), mDC, aFormat, aSize);
+  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(), mDC, aFormat, aSize);
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetD2D1::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   RefPtr<DrawTargetD2D1> dt = new DrawTargetD2D1();
 
   if (!dt->Init(aSize, aFormat)) {
@@ -689,17 +689,17 @@ DrawTargetD2D1::CreatePathBuilder(FillRu
     gfxWarning() << *this << ": Failed to access Direct2D Path Geometry. Code: " << hexa(hr);
     return nullptr;
   }
 
   if (aFillRule == FillRule::FILL_WINDING) {
     sink->SetFillMode(D2D1_FILL_MODE_WINDING);
   }
 
-  return new PathBuilderD2D(sink, path, aFillRule, BackendType::DIRECT2D1_1);
+  return MakeAndAddRef<PathBuilderD2D>(sink, path, aFillRule, BackendType::DIRECT2D1_1);
 }
 
 TemporaryRef<GradientStops>
 DrawTargetD2D1::CreateGradientStops(GradientStop *rawStops, uint32_t aNumStops, ExtendMode aExtendMode) const
 {
   if (aNumStops == 0) {
     gfxWarning() << *this << ": Failed to create GradientStopCollection with no stops.";
     return nullptr;
@@ -720,17 +720,17 @@ DrawTargetD2D1::CreateGradientStops(Grad
                                       byRef(stopCollection));
   delete [] stops;
 
   if (FAILED(hr)) {
     gfxWarning() << *this << ": Failed to create GradientStopCollection. Code: " << hexa(hr);
     return nullptr;
   }
 
-  return new GradientStopsD2D(stopCollection, Factory::GetDirect3D11Device());
+  return MakeAndAddRef<GradientStopsD2D>(stopCollection, Factory::GetDirect3D11Device());
 }
 
 TemporaryRef<FilterNode>
 DrawTargetD2D1::CreateFilter(FilterType aType)
 {
   return FilterNodeD2D1::Create(mDC, aType);
 }
 
@@ -1454,17 +1454,18 @@ DrawTargetD2D1::GetImageForSurface(Sourc
 
   return image.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetD2D1::OptimizeSourceSurface(SourceSurface* aSurface) const
 {
   if (aSurface->GetType() == SurfaceType::D2D1_1_IMAGE) {
-    return aSurface;
+    RefPtr<SourceSurface> surface(aSurface);
+    return surface.forget();
   }
 
   RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
 
   DataSourceSurface::MappedSurface map;
   if (!data->Map(DataSourceSurface::MapType::READ, &map)) {
     return nullptr;
   }
@@ -1479,17 +1480,17 @@ DrawTargetD2D1::OptimizeSourceSurface(So
   }
 
   data->Unmap();
 
   if (!bitmap) {
     return data.forget();
   }
 
-  return new SourceSurfaceD2D1(bitmap.get(), mDC, data->GetFormat(), data->GetSize());
+  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(), mDC, data->GetFormat(), data->GetSize());
 }
 
 void
 DrawTargetD2D1::PushD2DLayer(ID2D1DeviceContext *aDC, ID2D1Geometry *aGeometry, const D2D1_MATRIX_3X2_F &aTransform)
 {
   D2D1_LAYER_OPTIONS1 options = D2D1_LAYER_OPTIONS1_NONE;
 
   if (aDC->GetPixelFormat().alphaMode == D2D1_ALPHA_MODE_IGNORE) {
--- a/gfx/2d/DrawTargetDual.cpp
+++ b/gfx/2d/DrawTargetDual.cpp
@@ -187,13 +187,13 @@ DrawTargetDual::CreateSimilarDrawTarget(
   RefPtr<DrawTarget> dtA = mA->CreateSimilarDrawTarget(aSize, aFormat);
   RefPtr<DrawTarget> dtB = mB->CreateSimilarDrawTarget(aSize, aFormat);
 
   if (!dtA || !dtB) {
     gfxWarning() << "Failure to allocate a similar DrawTargetDual. Size: " << aSize;
     return nullptr;
   }
 
-  return new DrawTargetDual(dtA, dtB);
+  return MakeAndAddRef<DrawTargetDual>(dtA, dtB);
 }
 
 }
 }
--- a/gfx/2d/DrawTargetDual.h
+++ b/gfx/2d/DrawTargetDual.h
@@ -40,17 +40,19 @@ public:
     : mA(aA)
     , mB(aB)
   { 
     mFormat = aA->GetFormat();
   }
      
   virtual DrawTargetType GetType() const override { return mA->GetType(); }
   virtual BackendType GetBackendType() const override { return mA->GetBackendType(); }
-  virtual TemporaryRef<SourceSurface> Snapshot() override { return new SourceSurfaceDual(mA, mB); }
+  virtual TemporaryRef<SourceSurface> Snapshot() override {
+    return MakeAndAddRef<SourceSurfaceDual>(mA, mB);
+  }
   virtual IntSize GetSize() override { return mA->GetSize(); }
      
   FORWARD_FUNCTION(Flush)
   FORWARD_FUNCTION1(PushClip, const Path *, aPath)
   FORWARD_FUNCTION1(PushClipRect, const Rect &, aRect)
   FORWARD_FUNCTION(PopClip)
   FORWARD_FUNCTION1(ClearRect, const Rect &, aRect)
 
--- a/gfx/2d/DrawTargetRecording.cpp
+++ b/gfx/2d/DrawTargetRecording.cpp
@@ -536,24 +536,24 @@ DrawTargetRecording::CreateSourceSurface
 
   return retSurf.forget();
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetRecording::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   RefPtr<DrawTarget> dt = mFinalDT->CreateSimilarDrawTarget(aSize, aFormat);
-  return new DrawTargetRecording(mRecorder.get(), dt);
+  return MakeAndAddRef<DrawTargetRecording>(mRecorder.get(), dt);
 }
 
 TemporaryRef<PathBuilder>
 DrawTargetRecording::CreatePathBuilder(FillRule aFillRule) const
 {
   RefPtr<PathBuilder> builder = mFinalDT->CreatePathBuilder(aFillRule);
-  return new PathBuilderRecording(builder, aFillRule);
+  return MakeAndAddRef<PathBuilderRecording>(builder, aFillRule);
 }
 
 TemporaryRef<GradientStops>
 DrawTargetRecording::CreateGradientStops(GradientStop *aStops,
                                          uint32_t aNumStops,
                                          ExtendMode aExtendMode) const
 {
   RefPtr<GradientStops> stops = mFinalDT->CreateGradientStops(aStops, aNumStops, aExtendMode);
--- a/gfx/2d/DrawTargetSkia.cpp
+++ b/gfx/2d/DrawTargetSkia.cpp
@@ -699,17 +699,18 @@ DrawTargetSkia::UsingSkiaGPU() const
   return false;
 #endif
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetSkia::OptimizeSourceSurface(SourceSurface *aSurface) const
 {
   if (aSurface->GetType() == SurfaceType::SKIA) {
-    return aSurface;
+    RefPtr<SourceSurface> surface(aSurface);
+    return surface.forget();
   }
 
   if (!UsingSkiaGPU()) {
     // If we're not using skia-gl then drawing doesn't require any
     // uploading, so any data surface is fine. Call GetDataSurface
     // to trigger any required readback so that it only happens
     // once.
     return aSurface->GetDataSurface();
@@ -736,23 +737,23 @@ DrawTargetSkia::CreateSourceSurfaceFromN
 {
   if (aSurface.mType == NativeSurfaceType::CAIRO_SURFACE) {
     if (aSurface.mSize.width <= 0 ||
         aSurface.mSize.height <= 0) {
       gfxWarning() << "Can't create a SourceSurface without a valid size";
       return nullptr;
     }
     cairo_surface_t* surf = static_cast<cairo_surface_t*>(aSurface.mSurface);
-    return new SourceSurfaceCairo(surf, aSurface.mSize, aSurface.mFormat);
+    return MakeAndAddRef<SourceSurfaceCairo>(surf, aSurface.mSize, aSurface.mFormat);
 #if USE_SKIA_GPU
   } else if (aSurface.mType == NativeSurfaceType::OPENGL_TEXTURE && UsingSkiaGPU()) {
     RefPtr<SourceSurfaceSkia> newSurf = new SourceSurfaceSkia();
     unsigned int texture = (unsigned int)((uintptr_t)aSurface.mSurface);
     if (newSurf->InitFromTexture((DrawTargetSkia*)this, texture, aSurface.mSize, aSurface.mFormat)) {
-      return newSurf;
+      return newSurf.forget();
     }
     return nullptr;
 #endif
   }
 
   return nullptr;
 }
 
@@ -923,17 +924,17 @@ DrawTargetSkia::GetNativeSurface(NativeS
 #endif
   return nullptr;
 }
 
 
 TemporaryRef<PathBuilder>
 DrawTargetSkia::CreatePathBuilder(FillRule aFillRule) const
 {
-  return new PathBuilderSkia(aFillRule);
+  return MakeAndAddRef<PathBuilderSkia>(aFillRule);
 }
 
 void
 DrawTargetSkia::ClearRect(const Rect &aRect)
 {
   MarkChanged();
   SkPaint paint;
   mCanvas->save();
@@ -976,17 +977,17 @@ DrawTargetSkia::CreateGradientStops(Grad
 {
   std::vector<GradientStop> stops;
   stops.resize(aNumStops);
   for (uint32_t i = 0; i < aNumStops; i++) {
     stops[i] = aStops[i];
   }
   std::stable_sort(stops.begin(), stops.end());
 
-  return new GradientStopsSkia(stops, aNumStops, aExtendMode);
+  return MakeAndAddRef<GradientStopsSkia>(stops, aNumStops, aExtendMode);
 }
 
 TemporaryRef<FilterNode>
 DrawTargetSkia::CreateFilter(FilterType aType)
 {
   return FilterNodeSoftware::Create(aType);
 }
 
--- a/gfx/2d/DrawTargetTiled.cpp
+++ b/gfx/2d/DrawTargetTiled.cpp
@@ -46,17 +46,17 @@ DrawTargetTiled::Init(const TileSet& aTi
   }
   mFormat = mTiles[0].mDrawTarget->GetFormat();
   return true;
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetTiled::Snapshot()
 {
-  return new SnapshotTiled(mTiles, mRect);
+  return MakeAndAddRef<SnapshotTiled>(mTiles, mRect);
 }
 
 // Skip the mClippedOut check since this is only used for Flush() which
 // should happen even if we're clipped.
 #define TILED_COMMAND(command) \
   void \
   DrawTargetTiled::command() \
   { \
--- a/gfx/2d/Factory.cpp
+++ b/gfx/2d/Factory.cpp
@@ -332,31 +332,31 @@ Factory::CreateDrawTarget(BackendType aB
     }
 #endif
   default:
     gfxDebug() << "Invalid draw target type specified.";
     return nullptr;
   }
 
   if (mRecorder && retVal) {
-    return new DrawTargetRecording(mRecorder, retVal);
+    return MakeAndAddRef<DrawTargetRecording>(mRecorder, retVal);
   }
 
   if (!retVal) {
     // Failed
     gfxCriticalError(LoggerOptionsBasedOnSize(aSize)) << "Failed to create DrawTarget, Type: " << int(aBackend) << " Size: " << aSize;
   }
 
   return retVal.forget();
 }
 
 TemporaryRef<DrawTarget>
 Factory::CreateRecordingDrawTarget(DrawEventRecorder *aRecorder, DrawTarget *aDT)
 {
-  return new DrawTargetRecording(aRecorder, aDT);
+  return MakeAndAddRef<DrawTargetRecording>(aRecorder, aDT);
 }
 
 TemporaryRef<DrawTarget>
 Factory::CreateDrawTargetForData(BackendType aBackend,
                                  unsigned char *aData,
                                  const IntSize &aSize,
                                  int32_t aStride,
                                  SurfaceFormat aFormat)
@@ -401,17 +401,17 @@ Factory::CreateDrawTargetForData(Backend
     }
 #endif
   default:
     gfxDebug() << "Invalid draw target type specified.";
     return nullptr;
   }
 
   if (mRecorder && retVal) {
-    return new DrawTargetRecording(mRecorder, retVal, true);
+    return MakeAndAddRef<DrawTargetRecording>(mRecorder, retVal, true);
   }
 
   if (!retVal) {
     gfxDebug() << "Failed to create DrawTarget, Type: " << int(aBackend) << " Size: " << aSize;
   }
 
   return retVal.forget();
 }
@@ -473,35 +473,35 @@ Factory::GetMaxSurfaceSize(BackendType a
 
 TemporaryRef<ScaledFont>
 Factory::CreateScaledFontForNativeFont(const NativeFont &aNativeFont, Float aSize)
 {
   switch (aNativeFont.mType) {
 #ifdef WIN32
   case NativeFontType::DWRITE_FONT_FACE:
     {
-      return new ScaledFontDWrite(static_cast<IDWriteFontFace*>(aNativeFont.mFont), aSize);
+      return MakeAndAddRef<ScaledFontDWrite>(static_cast<IDWriteFontFace*>(aNativeFont.mFont), aSize);
     }
 #if defined(USE_CAIRO) || defined(USE_SKIA)
   case NativeFontType::GDI_FONT_FACE:
     {
-      return new ScaledFontWin(static_cast<LOGFONT*>(aNativeFont.mFont), aSize);
+      return MakeAndAddRef<ScaledFontWin>(static_cast<LOGFONT*>(aNativeFont.mFont), aSize);
     }
 #endif
 #endif
 #ifdef XP_MACOSX
   case NativeFontType::MAC_FONT_FACE:
     {
-      return new ScaledFontMac(static_cast<CGFontRef>(aNativeFont.mFont), aSize);
+      return MakeAndAddRef<ScaledFontMac>(static_cast<CGFontRef>(aNativeFont.mFont), aSize);
     }
 #endif
 #if defined(USE_CAIRO) || defined(USE_SKIA_FREETYPE)
   case NativeFontType::CAIRO_FONT_FACE:
     {
-      return new ScaledFontCairo(static_cast<cairo_scaled_font_t*>(aNativeFont.mFont), aSize);
+      return MakeAndAddRef<ScaledFontCairo>(static_cast<cairo_scaled_font_t*>(aNativeFont.mFont), aSize);
     }
 #endif
   default:
     gfxWarning() << "Invalid native font type specified.";
     return nullptr;
   }
 }
 
@@ -509,17 +509,17 @@ TemporaryRef<ScaledFont>
 Factory::CreateScaledFontForTrueTypeData(uint8_t *aData, uint32_t aSize,
                                          uint32_t aFaceIndex, Float aGlyphSize,
                                          FontType aType)
 {
   switch (aType) {
 #ifdef WIN32
   case FontType::DWRITE:
     {
-      return new ScaledFontDWrite(aData, aSize, aFaceIndex, aGlyphSize);
+      return MakeAndAddRef<ScaledFontDWrite>(aData, aSize, aFaceIndex, aGlyphSize);
     }
 #endif
   default:
     gfxWarning() << "Unable to create requested font type from truetype data";
     return nullptr;
   }
 }
 
@@ -701,17 +701,17 @@ bool
 Factory::SupportsD2D1()
 {
   return !!D2DFactory1();
 }
 
 TemporaryRef<GlyphRenderingOptions>
 Factory::CreateDWriteGlyphRenderingOptions(IDWriteRenderingParams *aParams)
 {
-  return new GlyphRenderingOptionsDWrite(aParams);
+  return MakeAndAddRef<GlyphRenderingOptionsDWrite>(aParams);
 }
 
 uint64_t
 Factory::GetD2DVRAMUsageDrawTarget()
 {
   return DrawTargetD2D::mVRAMUsageDT;
 }
 
@@ -775,18 +775,17 @@ Factory::CreateDrawTargetForCairoSurface
 #ifdef USE_CAIRO
   RefPtr<DrawTargetCairo> newTarget = new DrawTargetCairo();
 
   if (newTarget->Init(aSurface, aSize, aFormat)) {
     retVal = newTarget;
   }
 
   if (mRecorder && retVal) {
-    RefPtr<DrawTarget> recordDT = new DrawTargetRecording(mRecorder, retVal, true);
-    return recordDT.forget();
+    return MakeAndAddRef<DrawTargetRecording>(mRecorder, retVal, true);
   }
 #endif
   return retVal.forget();
 }
 
 #ifdef XP_MACOSX
 TemporaryRef<DrawTarget>
 Factory::CreateDrawTargetForCairoCGContext(CGContextRef cg, const IntSize& aSize)
@@ -795,25 +794,25 @@ Factory::CreateDrawTargetForCairoCGConte
 
   RefPtr<DrawTargetCG> newTarget = new DrawTargetCG();
 
   if (newTarget->Init(cg, aSize)) {
     retVal = newTarget;
   }
 
   if (mRecorder && retVal) {
-    return new DrawTargetRecording(mRecorder, retVal);
+    return MakeAndAddRef<DrawTargetRecording>(mRecorder, retVal);
   }
   return retVal.forget();
 }
 
 TemporaryRef<GlyphRenderingOptions>
 Factory::CreateCGGlyphRenderingOptions(const Color &aFontSmoothingBackgroundColor)
 {
-  return new GlyphRenderingOptionsCG(aFontSmoothingBackgroundColor);
+  return MakeAndAddRef<GlyphRenderingOptionsCG>(aFontSmoothingBackgroundColor);
 }
 #endif
 
 TemporaryRef<DataSourceSurface>
 Factory::CreateWrappingDataSourceSurface(uint8_t *aData, int32_t aStride,
                                          const IntSize &aSize,
                                          SurfaceFormat aFormat)
 {
@@ -868,17 +867,17 @@ Factory::CreateDataSourceSurfaceWithStri
 
   gfxCriticalError(LoggerOptionsBasedOnSize(aSize)) << "CreateDataSourceSurfaceWithStride failed to initialize " << aSize << ", " << aFormat << ", " << aStride << ", " << aZero;
   return nullptr;
 }
 
 TemporaryRef<DrawEventRecorder>
 Factory::CreateEventRecorderForFile(const char *aFilename)
 {
-  return new DrawEventRecorderFile(aFilename);
+  return MakeAndAddRef<DrawEventRecorderFile>(aFilename);
 }
 
 void
 Factory::SetGlobalEventRecorder(DrawEventRecorder *aRecorder)
 {
   mRecorder = aRecorder;
 }
 
--- a/gfx/2d/FilterNodeD2D1.cpp
+++ b/gfx/2d/FilterNodeD2D1.cpp
@@ -536,17 +536,17 @@ IsTransferFilterType(FilterType aType)
   }
 }
 
 /* static */
 TemporaryRef<FilterNode>
 FilterNodeD2D1::Create(ID2D1DeviceContext *aDC, FilterType aType)
 {
   if (aType == FilterType::CONVOLVE_MATRIX) {
-    return new FilterNodeConvolveD2D1(aDC);
+    return MakeAndAddRef<FilterNodeConvolveD2D1>(aDC);
   }
 
   RefPtr<ID2D1Effect> effect;
   HRESULT hr;
 
   hr = aDC->CreateEffect(GetCLDIDForFilterType(aType), byRef(effect));
 
   if (FAILED(hr)) {
--- a/gfx/2d/FilterNodeSoftware.cpp
+++ b/gfx/2d/FilterNodeSoftware.cpp
@@ -1297,17 +1297,18 @@ FilterNodeColorMatrixSoftware::SetAttrib
   mAlphaMode = (AlphaMode)aAlphaMode;
   Invalidate();
 }
 
 static TemporaryRef<DataSourceSurface>
 Premultiply(DataSourceSurface* aSurface)
 {
   if (aSurface->GetFormat() == SurfaceFormat::A8) {
-    return aSurface;
+    RefPtr<DataSourceSurface> surface(aSurface);
+    return surface.forget();
   }
 
   IntSize size = aSurface->GetSize();
   RefPtr<DataSourceSurface> target =
     Factory::CreateDataSourceSurface(size, SurfaceFormat::B8G8R8A8);
   if (MOZ2D_WARN_IF(!target)) {
     return nullptr;
   }
@@ -1322,17 +1323,18 @@ Premultiply(DataSourceSurface* aSurface)
 
   return target.forget();
 }
 
 static TemporaryRef<DataSourceSurface>
 Unpremultiply(DataSourceSurface* aSurface)
 {
   if (aSurface->GetFormat() == SurfaceFormat::A8) {
-    return aSurface;
+    RefPtr<DataSourceSurface> surface(aSurface);
+    return surface.forget();
   }
 
   IntSize size = aSurface->GetSize();
   RefPtr<DataSourceSurface> target =
     Factory::CreateDataSourceSurface(size, SurfaceFormat::B8G8R8A8);
   if (MOZ2D_WARN_IF(!target)) {
     return nullptr;
   }
--- a/gfx/2d/PathCG.cpp
+++ b/gfx/2d/PathCG.cpp
@@ -125,24 +125,24 @@ PathBuilderCG::CurrentPoint() const
 void
 PathBuilderCG::EnsureActive(const Point &aPoint)
 {
 }
 
 TemporaryRef<Path>
 PathBuilderCG::Finish()
 {
-  return new PathCG(mCGPath, mFillRule);
+  return MakeAndAddRef<PathCG>(mCGPath, mFillRule);
 }
 
 TemporaryRef<PathBuilder>
 PathCG::CopyToBuilder(FillRule aFillRule) const
 {
   CGMutablePathRef path = CGPathCreateMutableCopy(mPath);
-  return new PathBuilderCG(path, aFillRule);
+  return MakeAndAddRef<PathBuilderCG>(path, aFillRule);
 }
 
 
 
 TemporaryRef<PathBuilder>
 PathCG::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
 {
   // 10.7 adds CGPathCreateMutableCopyByTransformingPath it might be faster than doing
@@ -192,17 +192,17 @@ PathCG::TransformedCopyToBuilder(const M
     }
   };
 
   TransformApplier ta;
   ta.path = CGPathCreateMutable();
   ta.transform = GfxMatrixToCGAffineTransform(aTransform);
 
   CGPathApply(mPath, &ta, TransformApplier::TranformCGPathApplierFunc);
-  return new PathBuilderCG(ta.path, aFillRule);
+  return MakeAndAddRef<PathBuilderCG>(ta.path, aFillRule);
 }
 
 static void
 StreamPathToSinkApplierFunc(void *vinfo, const CGPathElement *element)
 {
   PathSink *sink = reinterpret_cast<PathSink*>(vinfo);
   switch (element->type) {
     case kCGPathElementMoveToPoint:
--- a/gfx/2d/PathCairo.cpp
+++ b/gfx/2d/PathCairo.cpp
@@ -120,17 +120,17 @@ Point
 PathBuilderCairo::CurrentPoint() const
 {
   return mCurrentPoint;
 }
 
 TemporaryRef<Path>
 PathBuilderCairo::Finish()
 {
-  return new PathCairo(mFillRule, mPathData, mCurrentPoint);
+  return MakeAndAddRef<PathCairo>(mFillRule, mPathData, mCurrentPoint);
 }
 
 PathCairo::PathCairo(FillRule aFillRule, std::vector<cairo_path_data_t> &aPathData, const Point &aCurrentPoint)
   : mFillRule(aFillRule)
   , mContainingContext(nullptr)
   , mCurrentPoint(aCurrentPoint)
 {
   mPathData.swap(aPathData);
--- a/gfx/2d/PathD2D.cpp
+++ b/gfx/2d/PathD2D.cpp
@@ -298,17 +298,17 @@ PathBuilderD2D::Finish()
   }
 
   HRESULT hr = mSink->Close();
   if (FAILED(hr)) {
     gfxDebug() << "Failed to close PathSink. Code: " << hexa(hr);
     return nullptr;
   }
 
-  return new PathD2D(mGeometry, mFigureActive, mCurrentPoint, mFillRule, mBackendType);
+  return MakeAndAddRef<PathD2D>(mGeometry, mFigureActive, mCurrentPoint, mFillRule, mBackendType);
 }
 
 TemporaryRef<PathBuilder>
 PathD2D::CopyToBuilder(FillRule aFillRule) const
 {
   return TransformedCopyToBuilder(Matrix(), aFillRule);
 }
 
--- a/gfx/2d/PathRecording.cpp
+++ b/gfx/2d/PathRecording.cpp
@@ -68,17 +68,17 @@ PathBuilderRecording::CurrentPoint() con
 {
   return mPathBuilder->CurrentPoint();
 }
 
 TemporaryRef<Path>
 PathBuilderRecording::Finish()
 {
   RefPtr<Path> path = mPathBuilder->Finish();
-  return new PathRecording(path, mPathOps, mFillRule);
+  return MakeAndAddRef<PathRecording>(path, mPathOps, mFillRule);
 }
 
 PathRecording::~PathRecording()
 {
   for (size_t i = 0; i < mStoredRecorders.size(); i++) {
     mStoredRecorders[i]->RemoveStoredPath(this);
     mStoredRecorders[i]->RecordEvent(RecordedPathDestruction(this));
   }
--- a/gfx/2d/PathSkia.cpp
+++ b/gfx/2d/PathSkia.cpp
@@ -100,17 +100,17 @@ PathBuilderSkia::CurrentPoint() const
   }
   SkPoint point = mPath.getPoint(pointCount - 1);
   return Point(SkScalarToFloat(point.fX), SkScalarToFloat(point.fY));
 }
 
 TemporaryRef<Path>
 PathBuilderSkia::Finish()
 {
-  return new PathSkia(mPath, mFillRule);
+  return MakeAndAddRef<PathSkia>(mPath, mFillRule);
 }
 
 void
 PathBuilderSkia::AppendPath(const SkPath &aPath)
 {
   mPath.addPath(aPath);
 }
 
@@ -118,17 +118,17 @@ TemporaryRef<PathBuilder>
 PathSkia::CopyToBuilder(FillRule aFillRule) const
 {
   return TransformedCopyToBuilder(Matrix(), aFillRule);
 }
 
 TemporaryRef<PathBuilder>
 PathSkia::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
 {
-  return new PathBuilderSkia(aTransform, mPath, aFillRule);
+  return MakeAndAddRef<PathBuilderSkia>(aTransform, mPath, aFillRule);
 }
 
 bool
 PathSkia::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const
 {
   Matrix inverse = aTransform;
   inverse.Invert();
   Point transformed = inverse * aPoint;
--- a/gfx/2d/ScaledFontBase.cpp
+++ b/gfx/2d/ScaledFontBase.cpp
@@ -75,17 +75,17 @@ ScaledFontBase::GetSkiaPathForGlyphs(con
 #endif
 
 TemporaryRef<Path>
 ScaledFontBase::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
 {
 #ifdef USE_SKIA
   if (aTarget->GetBackendType() == BackendType::SKIA) {
     SkPath path = GetSkiaPathForGlyphs(aBuffer);
-    return new PathSkia(path, FillRule::FILL_WINDING);
+    return MakeAndAddRef<PathSkia>(path, FillRule::FILL_WINDING);
   }
 #endif
 #ifdef USE_CAIRO
   if (aTarget->GetBackendType() == BackendType::CAIRO) {
     MOZ_ASSERT(mScaledFont);
 
     DrawTarget *dt = const_cast<DrawTarget*>(aTarget);
     cairo_t *ctx = static_cast<cairo_t*>(dt->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT));
--- a/gfx/2d/ScaledFontMac.cpp
+++ b/gfx/2d/ScaledFontMac.cpp
@@ -89,19 +89,19 @@ ScaledFontMac::GetPathForGlyphs(const Gl
           CGPathRef glyphPath = ::CGFontGetGlyphPath(mFont, &flip, 0, aBuffer.mGlyphs[i].mIndex);
 
           CGAffineTransform matrix = CGAffineTransformMake(mSize, 0, 0, mSize,
                                                            aBuffer.mGlyphs[i].mPosition.x,
                                                            aBuffer.mGlyphs[i].mPosition.y);
           CGPathAddPath(path, &matrix, glyphPath);
           CGPathRelease(glyphPath);
       }
-      TemporaryRef<Path> ret = new PathCG(path, FillRule::FILL_WINDING);
+      RefPtr<Path> ret = new PathCG(path, FillRule::FILL_WINDING);
       CGPathRelease(path);
-      return ret;
+      return ret.forget();
   }
   return ScaledFontBase::GetPathForGlyphs(aBuffer, aTarget);
 }
 
 void
 ScaledFontMac::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, BackendType aBackendType, const Matrix *aTransformHint)
 {
   if (!(aBackendType == BackendType::COREGRAPHICS || aBackendType == BackendType::COREGRAPHICS_ACCELERATED)) {
--- a/gfx/2d/SourceSurfaceCG.cpp
+++ b/gfx/2d/SourceSurfaceCG.cpp
@@ -40,17 +40,17 @@ TemporaryRef<DataSourceSurface>
 SourceSurfaceCG::GetDataSurface()
 {
   //XXX: we should be more disciplined about who takes a reference and where
   CGImageRetain(mImage);
   RefPtr<DataSourceSurface> dataSurf = new DataSourceSurfaceCG(mImage);
 
   // We also need to make sure that the returned surface has
   // surface->GetType() == SurfaceType::DATA.
-  return new DataSourceSurfaceWrapper(dataSurf);
+  return MakeAndAddRef<DataSourceSurfaceWrapper>(dataSurf);
 }
 
 static void releaseCallback(void *info, const void *data, size_t size) {
   free(info);
 }
 
 CGImageRef
 CreateCGImage(void *aInfo,
--- a/gfx/2d/SourceSurfaceCG.h
+++ b/gfx/2d/SourceSurfaceCG.h
@@ -122,17 +122,18 @@ public:
     // getting deleted before the CGImageRef it belongs to.
     //
     // Another reason we need a local copy of the data is that the data in
     // mDrawTarget could change when someone touches the original DrawTargetCG
     // object.  But a SourceSurface object should be immutable.
     //
     // For more information see bug 925448.
     DrawTargetWillChange();
-    return this;
+    RefPtr<DataSourceSurface> copy(this);
+    return copy.forget();
   }
 
   CGImageRef GetImage() { EnsureImage(); return mImage; }
 
   virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData); }
 
   virtual int32_t Stride() { return mStride; }
 
--- a/gfx/2d/SourceSurfaceCairo.cpp
+++ b/gfx/2d/SourceSurfaceCairo.cpp
@@ -76,17 +76,17 @@ SourceSurfaceCairo::GetDataSurface()
     cairo_destroy(ctx);
 
     dataSurf = new DataSourceSurfaceCairo(imageSurf);
     cairo_surface_destroy(imageSurf);
   }
 
   // We also need to make sure that the returned surface has
   // surface->GetType() == SurfaceType::DATA.
-  return new DataSourceSurfaceWrapper(dataSurf);
+  return MakeAndAddRef<DataSourceSurfaceWrapper>(dataSurf);
 }
 
 cairo_surface_t*
 SourceSurfaceCairo::GetSurface() const
 {
   return mSurface;
 }
 
--- a/gfx/2d/SourceSurfaceD2D1.cpp
+++ b/gfx/2d/SourceSurfaceD2D1.cpp
@@ -61,17 +61,17 @@ SourceSurfaceD2D1::GetDataSurface()
   
   hr = softwareBitmap->CopyFromBitmap(&point, mRealizedBitmap, &rect);
 
   if (FAILED(hr)) {
     gfxWarning() << "Failed to readback into software bitmap. Code: " << hexa(hr);
     return nullptr;
   }
 
-  return new DataSourceSurfaceD2D1(softwareBitmap, mFormat);
+  return MakeAndAddRef<DataSourceSurfaceD2D1>(softwareBitmap, mFormat);
 }
 
 void
 SourceSurfaceD2D1::EnsureRealizedBitmap()
 {
   if (mRealizedBitmap) {
     return;
   }
--- a/gfx/gl/SharedSurface.cpp
+++ b/gfx/gl/SharedSurface.cpp
@@ -342,17 +342,17 @@ SurfaceFactory::NewShSurfHandle(const gf
     auto surf = NewSharedSurface(size);
     if (!surf)
         return nullptr;
 
     // Before next use, wait until SharedSurface's buffer
     // is no longer being used.
     surf->WaitForBufferOwnership();
 
-    return new ShSurfHandle(this, Move(surf));
+    return MakeAndAddRef<ShSurfHandle>(this, Move(surf));
 }
 
 // Auto-deletes surfs of the wrong type.
 void
 SurfaceFactory::Recycle(UniquePtr<SharedSurface> surf)
 {
     MOZ_ASSERT(surf);
 
--- a/gfx/layers/Effects.h
+++ b/gfx/layers/Effects.h
@@ -282,17 +282,17 @@ CreateTexturedEffect(TextureSource* aSou
                      TextureSource* aSourceOnWhite,
                      const gfx::Filter& aFilter,
                      bool isAlphaPremultiplied)
 {
   MOZ_ASSERT(aSource);
   if (aSourceOnWhite) {
     MOZ_ASSERT(aSource->GetFormat() == gfx::SurfaceFormat::R8G8B8X8 ||
                aSourceOnWhite->GetFormat() == gfx::SurfaceFormat::B8G8R8X8);
-    return new EffectComponentAlpha(aSource, aSourceOnWhite, aFilter);
+    return MakeAndAddRef<EffectComponentAlpha>(aSource, aSourceOnWhite, aFilter);
   }
 
   return CreateTexturedEffect(aSource->GetFormat(),
                               aSource,
                               aFilter,
                               isAlphaPremultiplied);
 }
 
--- a/gfx/layers/ImageContainer.cpp
+++ b/gfx/layers/ImageContainer.cpp
@@ -453,17 +453,18 @@ PlanarYCbCrImage::AllocateAndGetNewBuffe
   }
   return mBuffer;
 }
 
 TemporaryRef<gfx::SourceSurface>
 PlanarYCbCrImage::GetAsSourceSurface()
 {
   if (mSourceSurface) {
-    return mSourceSurface.get();
+    RefPtr<gfx::SourceSurface> surface(mSourceSurface);
+    return surface.forget();
   }
 
   gfx::IntSize size(mSize);
   gfx::SurfaceFormat format = gfx::ImageFormatToSurfaceFormat(GetOffscreenFormat());
   gfx::GetYCbCrToRGBDestFormatAndSize(mData, format, size);
   if (mSize.width > PlanarYCbCrImage::MAX_DIMENSION ||
       mSize.height > PlanarYCbCrImage::MAX_DIMENSION) {
     NS_ERROR("Illegal image dest width or height");
--- a/gfx/layers/ImageContainer.h
+++ b/gfx/layers/ImageContainer.h
@@ -799,17 +799,18 @@ public:
   void SetData(const Data& aData)
   {
     mSize = aData.mSize;
     mSourceSurface = aData.mSourceSurface;
   }
 
   virtual TemporaryRef<gfx::SourceSurface> GetAsSourceSurface() override
   {
-    return mSourceSurface.get();
+    RefPtr<gfx::SourceSurface> surface(mSourceSurface);
+    return surface.forget();
   }
 
   virtual TextureClient* GetTextureClient(CompositableClient* aClient) override;
 
   virtual gfx::IntSize GetSize() override { return mSize; }
 
   CairoImage();
   ~CairoImage();
--- a/gfx/layers/basic/BasicImages.cpp
+++ b/gfx/layers/basic/BasicImages.cpp
@@ -131,17 +131,18 @@ BasicPlanarYCbCrImage::SetData(const Dat
 }
 
 TemporaryRef<gfx::SourceSurface>
 BasicPlanarYCbCrImage::GetAsSourceSurface()
 {
   NS_ASSERTION(NS_IsMainThread(), "Must be main thread");
 
   if (mSourceSurface) {
-    return mSourceSurface.get();
+    RefPtr<gfx::SourceSurface> surface(mSourceSurface);
+    return surface.forget();
   }
 
   if (!mDecodedBuffer) {
     return PlanarYCbCrImage::GetAsSourceSurface();
   }
 
   gfxImageFormat format = GetOffscreenFormat();
 
@@ -160,17 +161,17 @@ BasicPlanarYCbCrImage::GetAsSourceSurfac
     }
 
     surface = drawTarget->Snapshot();
   }
 
   mRecycleBin->RecycleBuffer(mDecodedBuffer.forget(), mSize.height * mStride);
 
   mSourceSurface = surface;
-  return mSourceSurface.get();
+  return surface.forget();
 }
 
 
 ImageFactory*
 BasicLayerManager::GetImageFactory()
 {
   if (!mFactory) {
     mFactory = new BasicImageFactory();
--- a/gfx/layers/client/CanvasClient.cpp
+++ b/gfx/layers/client/CanvasClient.cpp
@@ -35,26 +35,28 @@ namespace layers {
 /* static */ TemporaryRef<CanvasClient>
 CanvasClient::CreateCanvasClient(CanvasClientType aType,
                                  CompositableForwarder* aForwarder,
                                  TextureFlags aFlags)
 {
 #ifndef MOZ_WIDGET_GONK
   if (XRE_GetProcessType() != GeckoProcessType_Default) {
     NS_WARNING("Most platforms still need an optimized way to share GL cross process.");
-    return new CanvasClient2D(aForwarder, aFlags);
+    return MakeAndAddRef<CanvasClient2D>(aForwarder, aFlags);
   }
 #endif
 
   switch (aType) {
   case CanvasClientTypeShSurf:
-    return new CanvasClientSharedSurface(aForwarder, aFlags);
+    return MakeAndAddRef<CanvasClientSharedSurface>(aForwarder, aFlags);
+    break;
 
   default:
-    return new CanvasClient2D(aForwarder, aFlags);
+    return MakeAndAddRef<CanvasClient2D>(aForwarder, aFlags);
+    break;
   }
 }
 
 void
 CanvasClient2D::Update(gfx::IntSize aSize, ClientCanvasLayer* aLayer)
 {
   AutoRemoveTexture autoRemove(this);
   if (mBuffer &&
@@ -162,17 +164,17 @@ TexClientFromShSurf(ISurfaceAllocator* a
       return nullptr;
 
 #ifdef MOZ_WIDGET_GONK
     case SharedSurfaceType::Gralloc:
       return GrallocTextureClientOGL::FromSharedSurface(surf, flags);
 #endif
 
     default:
-      return new SharedSurfaceTextureClient(aAllocator, flags, surf);
+      return MakeAndAddRef<SharedSurfaceTextureClient>(aAllocator, flags, surf);
   }
 }
 
 ////////////////////////////////////////
 // Readback
 
 // For formats compatible with R8G8B8A8.
 static inline void SwapRB_R8G8B8A8(uint8_t* pixel) {
--- a/gfx/layers/client/ContentClient.cpp
+++ b/gfx/layers/client/ContentClient.cpp
@@ -86,19 +86,19 @@ ContentClient::CreateContentClient(Compo
 #endif
   {
     useDoubleBuffering = (LayerManagerComposite::SupportsDirectTexturing() &&
                          backend != LayersBackend::LAYERS_D3D9) ||
                          backend == LayersBackend::LAYERS_BASIC;
   }
 
   if (useDoubleBuffering || PR_GetEnv("MOZ_FORCE_DOUBLE_BUFFERING")) {
-    return new ContentClientDoubleBuffered(aForwarder);
+    return MakeAndAddRef<ContentClientDoubleBuffered>(aForwarder);
   }
-  return new ContentClientSingleBuffered(aForwarder);
+  return MakeAndAddRef<ContentClientSingleBuffered>(aForwarder);
 }
 
 void
 ContentClient::EndPaint(nsTArray<ReadbackProcessor::Update>* aReadbackUpdates)
 {
   // It is very important that this is called after any overridden EndPaint behaviour,
   // because destroying textures is a three stage process:
   // 1. We are done with the buffer and move it to ContentClient::mOldTextures,
--- a/gfx/layers/client/TextureClientRecycleAllocator.cpp
+++ b/gfx/layers/client/TextureClientRecycleAllocator.cpp
@@ -193,17 +193,18 @@ TextureClientRecycleAllocatorImp::Create
 
   {
     MutexAutoLock lock(mLock);
     MOZ_ASSERT(mInUseClients.find(textureHolder->GetTextureClient()) == mInUseClients.end());
     // Register TextureClient
     mInUseClients[textureHolder->GetTextureClient()] = textureHolder;
   }
   textureHolder->GetTextureClient()->SetRecycleCallback(TextureClientRecycleAllocatorImp::RecycleCallback, this);
-  return textureHolder->GetTextureClient();
+  RefPtr<TextureClient> client(textureHolder->GetTextureClient());
+  return client.forget();
 }
 
 void
 TextureClientRecycleAllocatorImp::Destroy()
 {
   MutexAutoLock lock(mLock);
   if (mDestroyed) {
     return;
--- a/gfx/layers/composite/TextureHost.cpp
+++ b/gfx/layers/composite/TextureHost.cpp
@@ -183,17 +183,17 @@ TextureHost::Create(const SurfaceDescrip
       return CreateBackendIndependentTextureHost(aDesc, aDeallocator, aFlags);
 
     case SurfaceDescriptor::TEGLImageDescriptor:
     case SurfaceDescriptor::TNewSurfaceDescriptorGralloc:
     case SurfaceDescriptor::TSurfaceTextureDescriptor:
       return CreateTextureHostOGL(aDesc, aDeallocator, aFlags);
 
     case SurfaceDescriptor::TSharedSurfaceDescriptor:
-      return new SharedSurfaceTextureHost(aFlags, aDesc.get_SharedSurfaceDescriptor());
+      return MakeAndAddRef<SharedSurfaceTextureHost>(aFlags, aDesc.get_SharedSurfaceDescriptor());
 
     case SurfaceDescriptor::TSurfaceDescriptorMacIOSurface:
       if (Compositor::GetBackend() == LayersBackend::LAYERS_OPENGL) {
         return CreateTextureHostOGL(aDesc, aDeallocator, aFlags);
       } else {
         return CreateTextureHostBasic(aDesc, aDeallocator, aFlags);
       }
 
--- a/gfx/layers/d3d9/CompositorD3D9.cpp
+++ b/gfx/layers/d3d9/CompositorD3D9.cpp
@@ -92,17 +92,17 @@ int32_t
 CompositorD3D9::GetMaxTextureSize() const
 {
   return mDeviceManager ? mDeviceManager->GetMaxTextureSize() : INT32_MAX;
 }
 
 TemporaryRef<DataTextureSource>
 CompositorD3D9::CreateDataTextureSource(TextureFlags aFlags)
 {
-  return new DataTextureSourceD3D9(SurfaceFormat::UNKNOWN, this, aFlags);
+  return MakeAndAddRef<DataTextureSourceD3D9>(SurfaceFormat::UNKNOWN, this, aFlags);
 }
 
 TemporaryRef<CompositingRenderTarget>
 CompositorD3D9::CreateRenderTarget(const gfx::IntRect &aRect,
                                    SurfaceInitMode aInit)
 {
   MOZ_ASSERT(aRect.width != 0 && aRect.height != 0, "Trying to create a render target of invalid size");
 
--- a/gfx/layers/ipc/CompositorBench.cpp
+++ b/gfx/layers/ipc/CompositorBench.cpp
@@ -130,20 +130,20 @@ public:
   }
 
   TemporaryRef<Effect> CreateEffect(size_t i) {
       float red;
       float tmp;
       red = modf(i * 0.03f, &tmp);
       EffectChain effects;
       gfxRGBA color(red, 0.4f, 0.4f, 1.0f);
-      return new EffectSolidColor(gfx::Color(color.r,
-                                             color.g,
-                                             color.b,
-                                             color.a));
+      return MakeAndAddRef<EffectSolidColor>(gfx::Color(color.r,
+                                                        color.g,
+                                                        color.b,
+                                                        color.a));
   }
 };
 
 class EffectSolidColorStressBench : public BenchTest {
 public:
   EffectSolidColorStressBench()
     : BenchTest("EffectSolidColorStressBench (10s various EffectSolidColor)")
   {}
@@ -156,20 +156,20 @@ public:
   }
 
   TemporaryRef<Effect> CreateEffect(size_t i) {
       float red;
       float tmp;
       red = modf(i * 0.03f, &tmp);
       EffectChain effects;
       gfxRGBA color(red, 0.4f, 0.4f, 1.0f);
-      return new EffectSolidColor(gfx::Color(color.r,
-                                             color.g,
-                                             color.b,
-                                             color.a));
+      return MakeAndAddRef<EffectSolidColor>(gfx::Color(color.r,
+                                                        color.g,
+                                                        color.b,
+                                                        color.a));
   }
 };
 
 class UploadBench : public BenchTest {
 public:
   UploadBench()
     : BenchTest("Upload Bench (10s 256x256 upload)")
   {}
--- a/gfx/thebes/gfxUtils.cpp
+++ b/gfx/thebes/gfxUtils.cpp
@@ -326,17 +326,18 @@ MapSrcAndCreateMappedDest(DataSourceSurf
 TemporaryRef<DataSourceSurface>
 gfxUtils::CreatePremultipliedDataSurface(DataSourceSurface* srcSurf)
 {
     RefPtr<DataSourceSurface> destSurf;
     DataSourceSurface::MappedSurface srcMap;
     DataSourceSurface::MappedSurface destMap;
     if (!MapSrcAndCreateMappedDest(srcSurf, &destSurf, &srcMap, &destMap)) {
         MOZ_ASSERT(false, "MapSrcAndCreateMappedDest failed.");
-        return srcSurf;
+        RefPtr<DataSourceSurface> surface(srcSurf);
+        return surface.forget();
     }
 
     PremultiplyData(srcMap.mData, srcMap.mStride,
                     destMap.mData, destMap.mStride,
                     srcSurf->GetSize().width,
                     srcSurf->GetSize().height);
 
     UnmapSrcDest(srcSurf, destSurf);
@@ -346,17 +347,18 @@ gfxUtils::CreatePremultipliedDataSurface
 TemporaryRef<DataSourceSurface>
 gfxUtils::CreateUnpremultipliedDataSurface(DataSourceSurface* srcSurf)
 {
     RefPtr<DataSourceSurface> destSurf;
     DataSourceSurface::MappedSurface srcMap;
     DataSourceSurface::MappedSurface destMap;
     if (!MapSrcAndCreateMappedDest(srcSurf, &destSurf, &srcMap, &destMap)) {
         MOZ_ASSERT(false, "MapSrcAndCreateMappedDest failed.");
-        return srcSurf;
+        RefPtr<DataSourceSurface> surface(srcSurf);
+        return surface.forget();
     }
 
     UnpremultiplyData(srcMap.mData, srcMap.mStride,
                       destMap.mData, destMap.mStride,
                       srcSurf->GetSize().width,
                       srcSurf->GetSize().height);
 
     UnmapSrcDest(srcSurf, destSurf);