Bug 1024983 - Stop addrefing and releasing excessively in the Moz2D code in functions that return a TemporaryRef. r=mstange
authorJonathan Watt <jwatt@jwatt.org>
Fri, 13 Jun 2014 17:09:23 +0100
changeset 210583 038f7887f26d9e2bfc7f95129b994174c5eab380
parent 210582 07d708291fed003292a28530188ea0616b07bbb2
child 210584 c0efa7f1e54c2dfbc9dfa77c49bf0411bc8b891d
push idunknown
push userunknown
push dateunknown
reviewersmstange
bugs1024983
milestone33.0a1
Bug 1024983 - Stop addrefing and releasing excessively in the Moz2D code in functions that return a TemporaryRef. r=mstange
gfx/2d/DataSourceSurface.cpp
gfx/2d/DrawTargetCG.cpp
gfx/2d/DrawTargetCairo.cpp
gfx/2d/DrawTargetD2D.cpp
gfx/2d/DrawTargetD2D1.cpp
gfx/2d/DrawTargetRecording.cpp
gfx/2d/DrawTargetSkia.cpp
gfx/2d/DrawTargetTiled.cpp
gfx/2d/Factory.cpp
gfx/2d/FilterNodeSoftware.cpp
gfx/2d/FilterProcessing.cpp
gfx/2d/FilterProcessingScalar.cpp
gfx/2d/HelpersD2D.h
gfx/2d/PathCG.cpp
gfx/2d/PathCairo.cpp
gfx/2d/PathD2D.cpp
gfx/2d/PathRecording.cpp
gfx/2d/PathSkia.cpp
gfx/2d/RadialGradientEffectD2D1.cpp
gfx/2d/ScaledFontBase.cpp
gfx/2d/ScaledFontMac.cpp
gfx/2d/SourceSurfaceCG.cpp
gfx/2d/SourceSurfaceCairo.cpp
gfx/2d/SourceSurfaceD2D.cpp
gfx/2d/SourceSurfaceD2DTarget.cpp
--- a/gfx/2d/DataSourceSurface.cpp
+++ b/gfx/2d/DataSourceSurface.cpp
@@ -7,19 +7,13 @@
 #include "DataSourceSurfaceWrapper.h"
 
 namespace mozilla {
 namespace gfx {
 
 TemporaryRef<DataSourceSurface>
 DataSourceSurface::GetDataSurface()
 {
-  RefPtr<DataSourceSurface> temp;
-  if (GetType() == SurfaceType::DATA) {
-    temp = this;
-  } else {
-    temp = new DataSourceSurfaceWrapper(this);
-  }
-  return temp;
+  return (GetType() == SurfaceType::DATA) ? this : new DataSourceSurfaceWrapper(this);
 }
 
 }
 }
--- a/gfx/2d/DrawTargetCG.cpp
+++ b/gfx/2d/DrawTargetCG.cpp
@@ -165,50 +165,48 @@ DrawTargetCG::GetType() const
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetCG::Snapshot()
 {
   if (!mSnapshot) {
     if (GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE) {
       return new SourceSurfaceCGIOSurfaceContext(this);
-    } else {
-      mSnapshot = new SourceSurfaceCGBitmapContext(this);
     }
+    mSnapshot = new SourceSurfaceCGBitmapContext(this);
   }
 
   return mSnapshot;
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetCG::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   // XXX: in thebes we use CGLayers to do this kind of thing. It probably makes sense
   // to add that in somehow, but at a higher level
   RefPtr<DrawTargetCG> newTarget = new DrawTargetCG();
   if (newTarget->Init(GetType(), aSize, aFormat)) {
-    return newTarget;
-  } else {
-    return nullptr;
+    return newTarget.forget();
   }
+  return nullptr;
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetCG::CreateSourceSurfaceFromData(unsigned char *aData,
                                            const IntSize &aSize,
                                            int32_t aStride,
                                            SurfaceFormat aFormat) const
 {
   RefPtr<SourceSurfaceCG> newSurf = new SourceSurfaceCG();
 
- if (!newSurf->InitFromData(aData, aSize, aStride, aFormat)) {
+  if (!newSurf->InitFromData(aData, aSize, aStride, aFormat)) {
     return nullptr;
   }
 
-  return newSurf;
+  return newSurf.forget();
 }
 
 // This function returns a retained CGImage that needs to be released after
 // use. The reason for this is that we want to either reuse an existing CGImage
 // or create a new one.
 static CGImageRef
 GetRetainedImageFromSourceSurface(SourceSurface *aSurface)
 {
@@ -1442,18 +1440,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
 {
-  RefPtr<PathBuilderCG> pb = new PathBuilderCG(aFillRule);
-  return pb;
+  return new 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
@@ -971,19 +971,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
 {
-  RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
-
-  return builder;
+  return new PathBuilderCairo(aFillRule);
 }
 
 void
 DrawTargetCairo::ClearSurfaceForUnboundedSource(const CompositionOp &aOperator)
 {
   if (aOperator != CompositionOp::OP_SOURCE)
     return;
   cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
@@ -993,19 +991,17 @@ DrawTargetCairo::ClearSurfaceForUnbounde
   cairo_paint(mContext);
 }
 
 
 TemporaryRef<GradientStops>
 DrawTargetCairo::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
                                      ExtendMode aExtendMode) const
 {
-  RefPtr<GradientStopsCairo> stops = new GradientStopsCairo(aStops, aNumStops,
-                                                            aExtendMode);
-  return stops;
+  return new GradientStopsCairo(aStops, aNumStops, aExtendMode);
 }
 
 TemporaryRef<FilterNode>
 DrawTargetCairo::CreateFilter(FilterType aType)
 {
   return FilterNodeSoftware::Create(aType);
 }
 
@@ -1054,17 +1050,17 @@ DrawTargetCairo::CreateSourceSurfaceFrom
     return nullptr;
   }
 
   CopyDataToCairoSurface(surf, aData, aSize, aStride, BytesPerPixel(aFormat));
 
   RefPtr<SourceSurfaceCairo> source_surf = new SourceSurfaceCairo(surf, aSize, aFormat);
   cairo_surface_destroy(surf);
 
-  return source_surf;
+  return source_surf.forget();
 }
 
 #ifdef CAIRO_HAS_XLIB_SURFACE
 static cairo_user_data_key_t gDestroyPixmapKey;
 
 struct DestroyPixmapClosure {
   DestroyPixmapClosure(Drawable d, Screen *s) : mPixmap(d), mScreen(s) {}
   ~DestroyPixmapClosure() {
@@ -1151,54 +1147,49 @@ DrawTargetCairo::OptimizeSourceSurface(S
     return aSurface;
   }
 
   dt->CopySurface(aSurface,
                   IntRect(0, 0, size.width, size.height),
                   IntPoint(0, 0));
   dt->Flush();
 
-  RefPtr<SourceSurface> surf =
-    new SourceSurfaceCairo(csurf, size, format);
-
-  return surf;
+  return new SourceSurfaceCairo(csurf, size, format);
 #endif
 
   return aSurface;
 }
 
 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);
-    RefPtr<SourceSurfaceCairo> source =
-      new SourceSurfaceCairo(surf, aSurface.mSize, aSurface.mFormat);
-    return source;
+    return new SourceSurfaceCairo(surf, aSurface.mSize, aSurface.mFormat);
   }
 
   return nullptr;
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetCairo::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   cairo_surface_t* similar = cairo_surface_create_similar(cairo_get_target(mContext),
                                                           GfxFormatToCairoContent(aFormat),
                                                           aSize.width, aSize.height);
 
   if (!cairo_surface_status(similar)) {
     RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
     target->InitAlreadyReferenced(similar, aSize);
-    return target;
+    return target.forget();
   }
 
   return nullptr;
 }
 
 bool
 DrawTargetCairo::InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
 {
@@ -1229,17 +1220,17 @@ DrawTargetCairo::CreateShadowDrawTarget(
     return nullptr;
   }
 
   // If we don't have a blur then we can use the RGBA mask and keep all the
   // operations in graphics memory.
   if (aSigma == 0.0F) {
     RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
     target->InitAlreadyReferenced(similar, aSize);
-    return target;
+    return target.forget();
   }
 
   cairo_surface_t* blursurf = cairo_image_surface_create(CAIRO_FORMAT_A8,
                                                          aSize.width,
                                                          aSize.height);
 
   if (cairo_surface_status(blursurf)) {
     return nullptr;
@@ -1252,17 +1243,17 @@ DrawTargetCairo::CreateShadowDrawTarget(
     return nullptr;
   }
 
   cairo_tee_surface_add(tee, similar);
   cairo_surface_destroy(similar);
 
   RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
   target->InitAlreadyReferenced(tee, aSize);
-  return target;
+  return target.forget();
 }
 
 bool
 DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
 {
   cairo_surface_reference(aSurface);
   return InitAlreadyReferenced(aSurface, aSize, aFormat);
 }
--- a/gfx/2d/DrawTargetD2D.cpp
+++ b/gfx/2d/DrawTargetD2D.cpp
@@ -1193,17 +1193,17 @@ DrawTargetD2D::CreateSourceSurfaceFromDa
                                            SurfaceFormat aFormat) const
 {
   RefPtr<SourceSurfaceD2D> newSurf = new SourceSurfaceD2D();
 
   if (!newSurf->InitFromData(aData, aSize, aStride, aFormat, mRT)) {
     return nullptr;
   }
 
-  return newSurf;
+  return newSurf.forget();
 }
 
 TemporaryRef<SourceSurface> 
 DrawTargetD2D::OptimizeSourceSurface(SourceSurface *aSurface) const
 {
   if (aSurface->GetType() == SurfaceType::D2D1_BITMAP ||
       aSurface->GetType() == SurfaceType::D2D1_DRAWTARGET) {
     return aSurface;
@@ -1217,19 +1217,19 @@ DrawTargetD2D::OptimizeSourceSurface(Sou
   }
 
   RefPtr<SourceSurfaceD2D> newSurf = new SourceSurfaceD2D();
   bool success = newSurf->InitFromData(map.mData, data->GetSize(), map.mStride, data->GetFormat(), mRT);
 
   data->Unmap();
 
   if (!success) {
-    return data;
+    return data.forget();
   }
-  return newSurf;
+  return newSurf.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetD2D::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
 {
   if (aSurface.mType != NativeSurfaceType::D3D10_TEXTURE) {
     gfxDebug() << *this << ": Failure to create source surface from non-D3D10 texture native surface.";
     return nullptr;
@@ -1239,31 +1239,31 @@ DrawTargetD2D::CreateSourceSurfaceFromNa
   if (!newSurf->InitFromTexture(static_cast<ID3D10Texture2D*>(aSurface.mSurface),
                                 aSurface.mFormat,
                                 mRT))
   {
     gfxWarning() << *this << ": Failed to create SourceSurface from texture.";
     return nullptr;
   }
 
-  return newSurf;
+  return newSurf.forget();
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetD2D::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   RefPtr<DrawTargetD2D> newTarget =
     new DrawTargetD2D();
 
   if (!newTarget->Init(aSize, aFormat)) {
     gfxDebug() << *this << ": Failed to create optimal draw target. Size: " << aSize;
     return nullptr;
   }
 
-  return newTarget;
+  return newTarget.forget();
 }
 
 TemporaryRef<PathBuilder>
 DrawTargetD2D::CreatePathBuilder(FillRule aFillRule) const
 {
   RefPtr<ID2D1PathGeometry> path;
   HRESULT hr = factory()->CreatePathGeometry(byRef(path));
 
@@ -1493,17 +1493,17 @@ DrawTargetD2D::GetCachedLayer()
       mVRAMUsageDT += GetByteSize();
     }
     layer = mCachedLayers[mCurrentCachedLayer];
   } else {
     mRT->CreateLayer(byRef(layer));
   }
 
   mCurrentCachedLayer++;
-  return layer;
+  return layer.forget();
 }
 
 void
 DrawTargetD2D::PopCachedLayer(ID2D1RenderTarget *aRT)
 {
   aRT->PopLayer();
   mCurrentCachedLayer--;
 }
@@ -1977,17 +1977,17 @@ DrawTargetD2D::CreateRTForTexture(ID3D10
     D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(desc.Format, alphaMode));
   hr = factory()->CreateDxgiSurfaceRenderTarget(surface, props, byRef(rt));
 
   if (FAILED(hr)) {
     gfxWarning() << "Failed to create D2D render target for texture.";
     return nullptr;
   }
 
-  return rt;
+  return rt.forget();
 }
 
 void
 DrawTargetD2D::EnsureViews()
 {
   if (mTempTexture && mSRView && mRTView) {
     return;
   }
@@ -2277,28 +2277,29 @@ DrawTargetD2D::FillGlyphsManual(ScaledFo
 }
 
 TemporaryRef<ID2D1Brush>
 DrawTargetD2D::CreateBrushForPattern(const Pattern &aPattern, Float aAlpha)
 {
   if (!IsPatternSupportedByD2D(aPattern)) {
     RefPtr<ID2D1SolidColorBrush> colBrush;
     mRT->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f), byRef(colBrush));
-    return colBrush;
+    return colBrush.forget();
   }
 
   if (aPattern.GetType() == PatternType::COLOR) {
     RefPtr<ID2D1SolidColorBrush> colBrush;
     Color color = static_cast<const ColorPattern*>(&aPattern)->mColor;
     mRT->CreateSolidColorBrush(D2D1::ColorF(color.r, color.g,
                                             color.b, color.a),
                                D2D1::BrushProperties(aAlpha),
                                byRef(colBrush));
-    return colBrush;
-  } else if (aPattern.GetType() == PatternType::LINEAR_GRADIENT) {
+    return colBrush.forget();
+  }
+  if (aPattern.GetType() == PatternType::LINEAR_GRADIENT) {
     RefPtr<ID2D1LinearGradientBrush> gradBrush;
     const LinearGradientPattern *pat =
       static_cast<const LinearGradientPattern*>(&aPattern);
 
     GradientStopsD2D *stops = static_cast<GradientStopsD2D*>(pat->mStops.get());
 
     if (!stops) {
       gfxDebug() << "No stops specified for gradient pattern.";
@@ -2308,26 +2309,27 @@ DrawTargetD2D::CreateBrushForPattern(con
     if (pat->mBegin == pat->mEnd) {
       RefPtr<ID2D1SolidColorBrush> colBrush;
       uint32_t stopCount = stops->mStopCollection->GetGradientStopCount();
       vector<D2D1_GRADIENT_STOP> d2dStops(stopCount);
       stops->mStopCollection->GetGradientStops(&d2dStops.front(), stopCount);
       mRT->CreateSolidColorBrush(d2dStops.back().color,
                                  D2D1::BrushProperties(aAlpha),
                                  byRef(colBrush));
-      return colBrush;
+      return colBrush.forget();
     }
 
     mRT->CreateLinearGradientBrush(D2D1::LinearGradientBrushProperties(D2DPoint(pat->mBegin),
                                                                        D2DPoint(pat->mEnd)),
                                    D2D1::BrushProperties(aAlpha, D2DMatrix(pat->mMatrix)),
                                    stops->mStopCollection,
                                    byRef(gradBrush));
-    return gradBrush;
-  } else if (aPattern.GetType() == PatternType::RADIAL_GRADIENT) {
+    return gradBrush.forget();
+  }
+  if (aPattern.GetType() == PatternType::RADIAL_GRADIENT) {
     RefPtr<ID2D1RadialGradientBrush> gradBrush;
     const RadialGradientPattern *pat =
       static_cast<const RadialGradientPattern*>(&aPattern);
 
     GradientStopsD2D *stops = static_cast<GradientStopsD2D*>(pat->mStops.get());
 
     if (!stops) {
       gfxDebug() << "No stops specified for gradient pattern.";
@@ -2338,18 +2340,19 @@ DrawTargetD2D::CreateBrushForPattern(con
     mRT->CreateRadialGradientBrush(
       D2D1::RadialGradientBrushProperties(D2DPoint(pat->mCenter2),
                                           D2DPoint(pat->mCenter1 - pat->mCenter2),
                                           pat->mRadius2, pat->mRadius2),
       D2D1::BrushProperties(aAlpha, D2DMatrix(pat->mMatrix)),
       stops->mStopCollection,
       byRef(gradBrush));
 
-    return gradBrush;
-  } else if (aPattern.GetType() == PatternType::SURFACE) {
+    return gradBrush.forget();
+  }
+  if (aPattern.GetType() == PatternType::SURFACE) {
     RefPtr<ID2D1BitmapBrush> bmBrush;
     const SurfacePattern *pat =
       static_cast<const SurfacePattern*>(&aPattern);
 
     if (!pat->mSurface) {
       gfxDebug() << "No source surface specified for surface pattern";
       return nullptr;
     }
@@ -2396,17 +2399,17 @@ DrawTargetD2D::CreateBrushForPattern(con
     
     mRT->CreateBitmapBrush(bitmap,
                            D2D1::BitmapBrushProperties(D2DExtend(pat->mExtendMode),
                                                        D2DExtend(pat->mExtendMode),
                                                        D2DFilter(pat->mFilter)),
                            D2D1::BrushProperties(aAlpha, D2DMatrix(mat)),
                            byRef(bmBrush));
 
-    return bmBrush;
+    return bmBrush.forget();
   }
 
   gfxWarning() << "Invalid pattern type detected.";
   return nullptr;
 }
 
 TemporaryRef<ID3D10Texture2D>
 DrawTargetD2D::CreateGradientTexture(const GradientStopsD2D *aStops)
@@ -2472,17 +2475,17 @@ DrawTargetD2D::CreateGradientTexture(con
 
   D3D10_SUBRESOURCE_DATA data;
   data.pSysMem = &textureData.front();
   data.SysMemPitch = 4096 * 4;
 
   RefPtr<ID3D10Texture2D> tex;
   mDevice->CreateTexture2D(&desc, &data, byRef(tex));
 
-  return tex;
+  return tex.forget();
 }
 
 TemporaryRef<ID3D10Texture2D>
 DrawTargetD2D::CreateTextureForAnalysis(IDWriteGlyphRunAnalysis *aAnalysis, const IntRect &aBounds)
 {
   HRESULT hr;
 
   uint32_t bufferSize = aBounds.width * aBounds.height * 3;
@@ -2537,17 +2540,17 @@ DrawTargetD2D::CreateTextureForAnalysis(
 	
   delete [] alignedTextureData;
   delete [] texture;
 
   if (FAILED(hr)) {
     return nullptr;
   }
 
-  return tex;
+  return tex.forget();
 }
 
 void
 DrawTargetD2D::SetupEffectForRadialGradient(const RadialGradientPattern *aPattern)
 {
   mPrivateData->mEffect->GetTechniqueByName("SampleRadialGradient")->GetPassByIndex(0)->Apply(0);
   mPrivateData->mEffect->GetVariableByName("MaskTexCoords")->AsVector()->
     SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f));
--- a/gfx/2d/DrawTargetD2D1.cpp
+++ b/gfx/2d/DrawTargetD2D1.cpp
@@ -528,17 +528,17 @@ TemporaryRef<DrawTarget>
 DrawTargetD2D1::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   RefPtr<DrawTargetD2D1> dt = new DrawTargetD2D1();
 
   if (!dt->Init(aSize, aFormat)) {
     return nullptr;
   }
 
-  return dt;
+  return dt.forget();
 }
 
 TemporaryRef<PathBuilder>
 DrawTargetD2D1::CreatePathBuilder(FillRule aFillRule) const
 {
   RefPtr<ID2D1PathGeometry> path;
   HRESULT hr = factory()->CreatePathGeometry(byRef(path));
 
@@ -792,28 +792,29 @@ DrawTargetD2D1::PopClipsFromDC(ID2D1Devi
 }
 
 TemporaryRef<ID2D1Brush>
 DrawTargetD2D1::CreateBrushForPattern(const Pattern &aPattern, Float aAlpha)
 {
   if (!IsPatternSupportedByD2D(aPattern)) {
     RefPtr<ID2D1SolidColorBrush> colBrush;
     mDC->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f), byRef(colBrush));
-    return colBrush;
+    return colBrush.forget();
   }
 
   if (aPattern.GetType() == PatternType::COLOR) {
     RefPtr<ID2D1SolidColorBrush> colBrush;
     Color color = static_cast<const ColorPattern*>(&aPattern)->mColor;
     mDC->CreateSolidColorBrush(D2D1::ColorF(color.r, color.g,
                                             color.b, color.a),
                                D2D1::BrushProperties(aAlpha),
                                byRef(colBrush));
-    return colBrush;
-  } else if (aPattern.GetType() == PatternType::LINEAR_GRADIENT) {
+    return colBrush.forget();
+  }
+  if (aPattern.GetType() == PatternType::LINEAR_GRADIENT) {
     RefPtr<ID2D1LinearGradientBrush> gradBrush;
     const LinearGradientPattern *pat =
       static_cast<const LinearGradientPattern*>(&aPattern);
 
     GradientStopsD2D *stops = static_cast<GradientStopsD2D*>(pat->mStops.get());
 
     if (!stops) {
       gfxDebug() << "No stops specified for gradient pattern.";
@@ -823,26 +824,27 @@ DrawTargetD2D1::CreateBrushForPattern(co
     if (pat->mBegin == pat->mEnd) {
       RefPtr<ID2D1SolidColorBrush> colBrush;
       uint32_t stopCount = stops->mStopCollection->GetGradientStopCount();
       vector<D2D1_GRADIENT_STOP> d2dStops(stopCount);
       stops->mStopCollection->GetGradientStops(&d2dStops.front(), stopCount);
       mDC->CreateSolidColorBrush(d2dStops.back().color,
                                  D2D1::BrushProperties(aAlpha),
                                  byRef(colBrush));
-      return colBrush;
+      return colBrush.forget();
     }
 
     mDC->CreateLinearGradientBrush(D2D1::LinearGradientBrushProperties(D2DPoint(pat->mBegin),
                                                                        D2DPoint(pat->mEnd)),
                                    D2D1::BrushProperties(aAlpha, D2DMatrix(pat->mMatrix)),
                                    stops->mStopCollection,
                                    byRef(gradBrush));
-    return gradBrush;
-  } else if (aPattern.GetType() == PatternType::RADIAL_GRADIENT) {
+    return gradBrush.forget();
+  }
+  if (aPattern.GetType() == PatternType::RADIAL_GRADIENT) {
     RefPtr<ID2D1RadialGradientBrush> gradBrush;
     const RadialGradientPattern *pat =
       static_cast<const RadialGradientPattern*>(&aPattern);
 
     GradientStopsD2D *stops = static_cast<GradientStopsD2D*>(pat->mStops.get());
 
     if (!stops) {
       gfxDebug() << "No stops specified for gradient pattern.";
@@ -850,21 +852,22 @@ DrawTargetD2D1::CreateBrushForPattern(co
     }
 
     // This will not be a complex radial gradient brush.
     mDC->CreateRadialGradientBrush(
       D2D1::RadialGradientBrushProperties(D2DPoint(pat->mCenter2),
                                           D2DPoint(pat->mCenter1 - pat->mCenter2),
                                           pat->mRadius2, pat->mRadius2),
       D2D1::BrushProperties(aAlpha, D2DMatrix(pat->mMatrix)),
-      stops->mStopCollection,
-      byRef(gradBrush));
+                            stops->mStopCollection,
+                            byRef(gradBrush));
 
-    return gradBrush;
-  } else if (aPattern.GetType() == PatternType::SURFACE) {
+    return gradBrush.forget();
+  }
+  if (aPattern.GetType() == PatternType::SURFACE) {
     const SurfacePattern *pat =
       static_cast<const SurfacePattern*>(&aPattern);
 
     if (!pat->mSurface) {
       gfxDebug() << "No source surface specified for surface pattern";
       return nullptr;
     }
 
@@ -876,17 +879,17 @@ DrawTargetD2D1::CreateBrushForPattern(co
     mDC->CreateImageBrush(image,
                           D2D1::ImageBrushProperties(D2D1::RectF(0, 0,
                                                                   Float(pat->mSurface->GetSize().width),
                                                                   Float(pat->mSurface->GetSize().height)),
                                   D2DExtend(pat->mExtendMode), D2DExtend(pat->mExtendMode),
                                   D2DInterpolationMode(pat->mFilter)),
                           D2D1::BrushProperties(aAlpha, D2DMatrix(mat)),
                           byRef(imageBrush));
-    return imageBrush;
+    return imageBrush.forget();
   }
 
   gfxWarning() << "Invalid pattern type detected.";
   return nullptr;
 }
 
 TemporaryRef<ID2D1Image>
 DrawTargetD2D1::GetImageForSurface(SourceSurface *aSurface, Matrix &aSourceTransform,
@@ -904,26 +907,23 @@ DrawTargetD2D1::GetImageForSurface(Sourc
     break;
   default:
     {
       RefPtr<DataSourceSurface> dataSurf = aSurface->GetDataSurface();
       if (!dataSurf) {
         gfxWarning() << "Invalid surface type.";
         return nullptr;
       }
-
-      image = CreatePartialBitmapForSurface(dataSurf, mTransform, mSize, aExtendMode,
-                                            aSourceTransform, mDC); 
-
-      return image;
+      return CreatePartialBitmapForSurface(dataSurf, mTransform, mSize, aExtendMode,
+                                           aSourceTransform, mDC);
     }
     break;
   }
 
-  return image;
+  return image.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetD2D1::OptimizeSourceSurface(SourceSurface* aSurface) const
 {
   if (aSurface->GetType() == SurfaceType::D2D1_1_IMAGE) {
     return aSurface;
   }
@@ -938,17 +938,17 @@ DrawTargetD2D1::OptimizeSourceSurface(So
   RefPtr<ID2D1Bitmap1> bitmap;
   HRESULT hr = mDC->CreateBitmap(D2DIntSize(data->GetSize()), map.mData, map.mStride,
                                  D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE, D2DPixelFormat(data->GetFormat())),
                                  byRef(bitmap));
 
   data->Unmap();
 
   if (!bitmap) {
-    return data;
+    return data.forget();
   }
 
   return new SourceSurfaceD2D1(bitmap.get(), mDC, data->GetFormat(), data->GetSize());
 }
 
 void
 DrawTargetD2D1::PushD2DLayer(ID2D1DeviceContext *aDC, ID2D1Geometry *aGeometry, const D2D1_MATRIX_3X2_F &aTransform)
 {
--- a/gfx/2d/DrawTargetRecording.cpp
+++ b/gfx/2d/DrawTargetRecording.cpp
@@ -366,17 +366,17 @@ TemporaryRef<SourceSurface>
 DrawTargetRecording::Snapshot()
 {
   RefPtr<SourceSurface> surf = mFinalDT->Snapshot();
 
   RefPtr<SourceSurface> retSurf = new SourceSurfaceRecording(surf, mRecorder);
 
   mRecorder->RecordEvent(RecordedSnapshot(retSurf, this));
 
-  return retSurf;
+  return retSurf.forget();
 }
 
 void
 DrawTargetRecording::DrawSurface(SourceSurface *aSurface,
                                  const Rect &aDest,
                                  const Rect &aSource,
                                  const DrawSurfaceOptions &aSurfOptions,
                                  const DrawOptions &aOptions)
@@ -411,17 +411,17 @@ TemporaryRef<FilterNode>
 DrawTargetRecording::CreateFilter(FilterType aType)
 {
   RefPtr<FilterNode> node = mFinalDT->CreateFilter(aType);
 
   RefPtr<FilterNode> retNode = new FilterNodeRecording(node, mRecorder);
 
   mRecorder->RecordEvent(RecordedFilterNodeCreation(retNode, aType));
 
-  return retNode;
+  return retNode.forget();
 }
 
 void
 DrawTargetRecording::ClearRect(const Rect &aRect)
 {
   mRecorder->RecordEvent(RecordedClearRect(this, aRect));
   mFinalDT->ClearRect(aRect);
 }
@@ -465,17 +465,17 @@ DrawTargetRecording::CreateSourceSurface
                                                  SurfaceFormat aFormat) const
 {
   RefPtr<SourceSurface> surf = mFinalDT->CreateSourceSurfaceFromData(aData, aSize, aStride, aFormat);
 
   RefPtr<SourceSurface> retSurf = new SourceSurfaceRecording(surf, mRecorder);
 
   mRecorder->RecordEvent(RecordedSourceSurfaceCreation(retSurf, aData, aStride, aSize, aFormat));
 
-  return retSurf;
+  return retSurf.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetRecording::OptimizeSourceSurface(SourceSurface *aSurface) const
 {
   RefPtr<SourceSurface> surf = mFinalDT->OptimizeSourceSurface(aSurface);
 
   RefPtr<SourceSurface> retSurf = new SourceSurfaceRecording(surf, mRecorder);
@@ -498,17 +498,17 @@ DrawTargetRecording::OptimizeSourceSurfa
                                     surf->GetSize(), surf->GetFormat()));
     delete [] sourceData;
   } else {
     mRecorder->RecordEvent(
       RecordedSourceSurfaceCreation(retSurf, dataSurf->GetData(), dataSurf->Stride(),
                                     dataSurf->GetSize(), dataSurf->GetFormat()));
   }
 
-  return retSurf;
+  return retSurf.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetRecording::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
 {
   RefPtr<SourceSurface> surf = mFinalDT->CreateSourceSurfaceFromNativeSurface(aSurface);
 
   RefPtr<SourceSurface> retSurf = new SourceSurfaceRecording(surf, mRecorder);
@@ -526,27 +526,24 @@ DrawTargetRecording::CreateSourceSurface
                                     surf->GetSize(), surf->GetFormat()));
     delete [] sourceData;
   } else {
     mRecorder->RecordEvent(
       RecordedSourceSurfaceCreation(retSurf, dataSurf->GetData(), dataSurf->Stride(),
                                     dataSurf->GetSize(), dataSurf->GetFormat()));
   }
 
-  return retSurf;
+  return retSurf.forget();
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetRecording::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   RefPtr<DrawTarget> dt = mFinalDT->CreateSimilarDrawTarget(aSize, aFormat);
-
-  RefPtr<DrawTarget> retDT = new DrawTargetRecording(mRecorder.get(), dt);
-
-  return retDT;
+  return new DrawTargetRecording(mRecorder.get(), dt);
 }
 
 TemporaryRef<PathBuilder>
 DrawTargetRecording::CreatePathBuilder(FillRule aFillRule) const
 {
   RefPtr<PathBuilder> builder = mFinalDT->CreatePathBuilder(aFillRule);
   return new PathBuilderRecording(builder, aFillRule);
 }
@@ -557,17 +554,17 @@ DrawTargetRecording::CreateGradientStops
                                          ExtendMode aExtendMode) const
 {
   RefPtr<GradientStops> stops = mFinalDT->CreateGradientStops(aStops, aNumStops, aExtendMode);
 
   RefPtr<GradientStops> retStops = new GradientStopsRecording(stops, mRecorder);
 
   mRecorder->RecordEvent(RecordedGradientStopsCreation(retStops, aStops, aNumStops, aExtendMode));
 
-  return retStops;
+  return retStops.forget();
 }
 
 void
 DrawTargetRecording::SetTransform(const Matrix &aTransform)
 {
   mRecorder->RecordEvent(RecordedSetTransform(this, aTransform));
   DrawTarget::SetTransform(aTransform);
   mFinalDT->SetTransform(aTransform);
--- a/gfx/2d/DrawTargetSkia.cpp
+++ b/gfx/2d/DrawTargetSkia.cpp
@@ -130,17 +130,17 @@ DrawTargetSkia::Snapshot()
   RefPtr<SourceSurfaceSkia> snapshot = mSnapshot;
   if (!snapshot) {
     snapshot = new SourceSurfaceSkia();
     mSnapshot = snapshot;
     if (!snapshot->InitFromCanvas(mCanvas.get(), mFormat, this))
       return nullptr;
   }
 
-  return snapshot;
+  return snapshot.forget();
 }
 
 static void
 SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern, TempBitmap& aTmpBitmap,
                 Float aAlpha = 1.0)
 {
   switch (aPattern.GetType()) {
     case PatternType::COLOR: {
@@ -582,27 +582,27 @@ DrawTargetSkia::CreateSourceSurfaceFromD
 {
   RefPtr<SourceSurfaceSkia> newSurf = new SourceSurfaceSkia();
 
   if (!newSurf->InitFromData(aData, aSize, aStride, aFormat)) {
     gfxDebug() << *this << ": Failure to create source surface from data. Size: " << aSize;
     return nullptr;
   }
     
-  return newSurf;
+  return newSurf.forget();
 }
 
 TemporaryRef<DrawTarget>
 DrawTargetSkia::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
 {
   RefPtr<DrawTargetSkia> target = new DrawTargetSkia();
   if (!target->Init(aSize, aFormat)) {
     return nullptr;
   }
-  return target;
+  return target.forget();
 }
 
 bool
 DrawTargetSkia::UsingSkiaGPU() const
 {
 #ifdef USE_SKIA_GPU
   return !!mTexture;
 #else
@@ -633,17 +633,17 @@ DrawTargetSkia::OptimizeSourceSurface(So
     return nullptr;
   }
 
   RefPtr<SourceSurface> result = CreateSourceSurfaceFromData(map.mData,
                                                              dataSurf->GetSize(),
                                                              map.mStride,
                                                              dataSurf->GetFormat());
   dataSurf->Unmap();
-  return result;
+  return result.forget();
 }
 
 TemporaryRef<SourceSurface>
 DrawTargetSkia::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
 {
   return nullptr;
 }
 
@@ -803,18 +803,17 @@ DrawTargetSkia::GetNativeSurface(NativeS
 
   return nullptr;  
 }
 
 
 TemporaryRef<PathBuilder> 
 DrawTargetSkia::CreatePathBuilder(FillRule aFillRule) const
 {
-  RefPtr<PathBuilderSkia> pb = new PathBuilderSkia(aFillRule);
-  return pb;
+  return new PathBuilderSkia(aFillRule);
 }
 
 void
 DrawTargetSkia::ClearRect(const Rect &aRect)
 {
   MarkChanged();
   SkPaint paint;
   mCanvas->save();
--- a/gfx/2d/DrawTargetTiled.cpp
+++ b/gfx/2d/DrawTargetTiled.cpp
@@ -71,17 +71,17 @@ public:
 
       for (size_t i = 0; i < mSnapshots.size(); i++) {
         RefPtr<DataSourceSurface> dataSurf = mSnapshots[i]->GetDataSurface();
         dt->CopySurface(dataSurf, IntRect(IntPoint(0, 0), mSnapshots[i]->GetSize()), mOrigins[i]);
       }
     }
     surf->Unmap();
 
-    return surf;
+    return surf.forget();
   }
 private:
   vector<RefPtr<SourceSurface>> mSnapshots;
   vector<IntPoint> mOrigins;
   IntRect mRect;
 };
 
 TemporaryRef<SourceSurface>
--- a/gfx/2d/Factory.cpp
+++ b/gfx/2d/Factory.cpp
@@ -294,27 +294,25 @@ Factory::CreateDrawTarget(BackendType aB
     }
 #endif
   default:
     gfxDebug() << "Invalid draw target type specified.";
     return nullptr;
   }
 
   if (mRecorder && retVal) {
-    RefPtr<DrawTarget> recordDT;
-    recordDT = new DrawTargetRecording(mRecorder, retVal);
-    return recordDT;
+    return new DrawTargetRecording(mRecorder, retVal);
   }
 
   if (!retVal) {
     // Failed
     gfxDebug() << "Failed to create DrawTarget, Type: " << int(aBackend) << " Size: " << aSize;
   }
   
-  return retVal;
+  return retVal.forget();
 }
 
 TemporaryRef<DrawTarget>
 Factory::CreateRecordingDrawTarget(DrawEventRecorder *aRecorder, DrawTarget *aDT)
 {
   return new DrawTargetRecording(aRecorder, aDT);
 }
 
@@ -342,58 +340,57 @@ Factory::CreateDrawTargetForData(Backend
       break;
     }
 #endif
 #ifdef XP_MACOSX
   case BackendType::COREGRAPHICS:
     {
       RefPtr<DrawTargetCG> newTarget = new DrawTargetCG();
       if (newTarget->Init(aBackend, aData, aSize, aStride, aFormat))
-        return newTarget;
+        return newTarget.forget();
       break;
     }
 #endif
 #ifdef USE_CAIRO
   case BackendType::CAIRO:
     {
       RefPtr<DrawTargetCairo> newTarget;
       newTarget = new DrawTargetCairo();
       if (newTarget->Init(aData, aSize, aStride, aFormat)) {
-        retVal = newTarget;
+        retVal = newTarget.forget();
       }
       break;
     }
 #endif
   default:
     gfxDebug() << "Invalid draw target type specified.";
     return nullptr;
   }
 
   if (mRecorder && retVal) {
-    RefPtr<DrawTarget> recordDT = new DrawTargetRecording(mRecorder, retVal, true);
-    return recordDT;
+    return new DrawTargetRecording(mRecorder, retVal, true);
   }
 
   if (!retVal) {
     gfxDebug() << "Failed to create DrawTarget, Type: " << int(aBackend) << " Size: " << aSize;
   }
 
-  return retVal;
+  return retVal.forget();
 }
 
 TemporaryRef<DrawTarget>
 Factory::CreateTiledDrawTarget(const TileSet& aTileSet)
 {
   RefPtr<DrawTargetTiled> dt = new DrawTargetTiled();
 
   if (!dt->Init(aTileSet)) {
     return nullptr;
   }
 
-  return dt;
+  return dt.forget();
 }
 
 TemporaryRef<ScaledFont>
 Factory::CreateScaledFontForNativeFont(const NativeFont &aNativeFont, Float aSize)
 {
   switch (aNativeFont.mType) {
 #ifdef WIN32
   case NativeFontType::DWRITE_FONT_FACE:
@@ -448,17 +445,17 @@ Factory::CreateScaledFontWithCairo(const
 {
 #ifdef USE_CAIRO
   // In theory, we could pull the NativeFont out of the cairo_scaled_font_t*,
   // but that would require a lot of code that would be otherwise repeated in
   // various backends.
   // Therefore, we just reuse CreateScaledFontForNativeFont's implementation.
   RefPtr<ScaledFont> font = CreateScaledFontForNativeFont(aNativeFont, aSize);
   static_cast<ScaledFontBase*>(font.get())->SetCairoScaledFont(aScaledFont);
-  return font;
+  return font.forget();
 #else
   return nullptr;
 #endif
 }
 
 TemporaryRef<DrawTarget>
 Factory::CreateDualDrawTarget(DrawTarget *targetA, DrawTarget *targetB)
 {
@@ -466,17 +463,17 @@ Factory::CreateDualDrawTarget(DrawTarget
     new DrawTargetDual(targetA, targetB);
 
   RefPtr<DrawTarget> retVal = newTarget;
 
   if (mRecorder) {
     retVal = new DrawTargetRecording(mRecorder, retVal);
   }
 
-  return retVal;
+  return retVal.forget();
 }
 
 
 #ifdef WIN32
 TemporaryRef<DrawTarget>
 Factory::CreateDrawTargetForD3D10Texture(ID3D10Texture2D *aTexture, SurfaceFormat aFormat)
 {
   RefPtr<DrawTargetD2D> newTarget;
@@ -484,17 +481,17 @@ Factory::CreateDrawTargetForD3D10Texture
   newTarget = new DrawTargetD2D();
   if (newTarget->Init(aTexture, aFormat)) {
     RefPtr<DrawTarget> retVal = newTarget;
 
     if (mRecorder) {
       retVal = new DrawTargetRecording(mRecorder, retVal, true);
     }
 
-    return retVal;
+    return retVal.forget();
   }
 
   gfxWarning() << "Failed to create draw target for D3D10 texture.";
 
   // Failed
   return nullptr;
 }
 
@@ -522,17 +519,17 @@ Factory::CreateDualDrawTargetForD3D10Tex
     new DrawTargetDual(newTargetA, newTargetB);
 
   RefPtr<DrawTarget> retVal = newTarget;
 
   if (mRecorder) {
     retVal = new DrawTargetRecording(mRecorder, retVal);
   }
 
-  return retVal;
+  return retVal.forget();
 }
 
 void
 Factory::SetDirect3D10Device(ID3D10Device1 *aDevice)
 {
   mD3D10Device = aDevice;
 }
 
@@ -566,20 +563,17 @@ Factory::GetD2D1Device()
 {
   return mD2D1Device;
 }
 #endif
 
 TemporaryRef<GlyphRenderingOptions>
 Factory::CreateDWriteGlyphRenderingOptions(IDWriteRenderingParams *aParams)
 {
-  RefPtr<GlyphRenderingOptions> options =
-    new GlyphRenderingOptionsDWrite(aParams);
-
-  return options;
+  return new GlyphRenderingOptionsDWrite(aParams);
 }
 
 uint64_t
 Factory::GetD2DVRAMUsageDrawTarget()
 {
   return DrawTargetD2D::mVRAMUsageDT;
 }
 
@@ -602,17 +596,17 @@ TemporaryRef<DrawTarget>
 Factory::CreateDrawTargetSkiaWithGrContext(GrContext* aGrContext,
                                            const IntSize &aSize,
                                            SurfaceFormat aFormat)
 {
   RefPtr<DrawTarget> newTarget = new DrawTargetSkia();
   if (!newTarget->InitWithGrContext(aGrContext, aSize, aFormat)) {
     return nullptr;
   }
-  return newTarget;
+  return newTarget.forget();
 }
 
 #endif // USE_SKIA_GPU
 
 void
 Factory::PurgeAllCaches()
 {
 }
@@ -621,17 +615,17 @@ Factory::PurgeAllCaches()
 TemporaryRef<GlyphRenderingOptions>
 Factory::CreateCairoGlyphRenderingOptions(FontHinting aHinting, bool aAutoHinting)
 {
   RefPtr<GlyphRenderingOptionsCairo> options =
     new GlyphRenderingOptionsCairo();
 
   options->SetHinting(aHinting);
   options->SetAutoHinting(aAutoHinting);
-  return options;
+  return options.forget();
 }
 #endif
 
 TemporaryRef<DrawTarget>
 Factory::CreateDrawTargetForCairoSurface(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
 {
   RefPtr<DrawTarget> retVal;
 
@@ -639,88 +633,87 @@ Factory::CreateDrawTargetForCairoSurface
   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;
+    return recordDT.forget();
   }
 #endif
-  return retVal;
+  return retVal.forget();
 }
 
 #ifdef XP_MACOSX
 TemporaryRef<DrawTarget>
 Factory::CreateDrawTargetForCairoCGContext(CGContextRef cg, const IntSize& aSize)
 {
   RefPtr<DrawTarget> retVal;
 
   RefPtr<DrawTargetCG> newTarget = new DrawTargetCG();
 
   if (newTarget->Init(cg, aSize)) {
     retVal = newTarget;
   }
 
   if (mRecorder && retVal) {
-    RefPtr<DrawTarget> recordDT = new DrawTargetRecording(mRecorder, retVal);
-    return recordDT;
+    return new DrawTargetRecording(mRecorder, retVal);
   }
-  return retVal;
+  return retVal.forget();
 }
 #endif
 
 TemporaryRef<DataSourceSurface>
 Factory::CreateWrappingDataSourceSurface(uint8_t *aData, int32_t aStride,
                                          const IntSize &aSize,
                                          SurfaceFormat aFormat)
 {
   if (aSize.width <= 0 || aSize.height <= 0) {
     return nullptr;
   }
 
   RefPtr<SourceSurfaceRawData> newSurf = new SourceSurfaceRawData();
 
   if (newSurf->InitWrappingData(aData, aSize, aStride, aFormat, false)) {
-    return newSurf;
+    return newSurf.forget();
   }
 
   return nullptr;
 }
 
 TemporaryRef<DataSourceSurface>
 Factory::CreateDataSourceSurface(const IntSize &aSize,
                                  SurfaceFormat aFormat)
 {
   if (!CheckSurfaceSize(aSize)) {
     return nullptr;
   }
 
   RefPtr<SourceSurfaceAlignedRawData> newSurf = new SourceSurfaceAlignedRawData();
   if (newSurf->Init(aSize, aFormat)) {
-    return newSurf;
+    return newSurf.forget();
   }
 
   return nullptr;
 }
 
 TemporaryRef<DataSourceSurface>
 Factory::CreateDataSourceSurfaceWithStride(const IntSize &aSize,
                                            SurfaceFormat aFormat,
                                            int32_t aStride)
 {
   if (aStride < aSize.width * BytesPerPixel(aFormat)) {
     return nullptr;
   }
 
   RefPtr<SourceSurfaceAlignedRawData> newSurf = new SourceSurfaceAlignedRawData();
   if (newSurf->InitWithStride(aSize, aFormat, aStride)) {
-    return newSurf;
+    return newSurf.forget();
   }
 
   return nullptr;
 }
 
 TemporaryRef<DrawEventRecorder>
 Factory::CreateEventRecorderForFile(const char *aFilename)
 {
--- a/gfx/2d/FilterNodeSoftware.cpp
+++ b/gfx/2d/FilterNodeSoftware.cpp
@@ -277,17 +277,17 @@ CopyRect(DataSourceSurface* aSrc, DataSo
 TemporaryRef<DataSourceSurface>
 CloneAligned(DataSourceSurface* aSource)
 {
   RefPtr<DataSourceSurface> copy =
     Factory::CreateDataSourceSurface(aSource->GetSize(), aSource->GetFormat());
   if (copy) {
     CopyRect(aSource, copy, IntRect(IntPoint(), aSource->GetSize()), IntPoint());
   }
-  return copy;
+  return copy.forget();
 }
 
 static void
 FillRectWithPixel(DataSourceSurface *aSurface, const IntRect &aFillRect, IntPoint aPixelPos)
 {
   MOZ_ASSERT(!IntRectOverflows(aFillRect));
   MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aFillRect),
              "aFillRect needs to be completely inside the surface");
@@ -513,35 +513,35 @@ GetDataSurfaceInRect(SourceSurface *aSur
     return nullptr;
   }
 
   if (aEdgeMode == EDGE_MODE_NONE && !aSurfaceRect.Contains(aDestRect)) {
     ClearDataSourceSurface(target);
   }
 
   if (!aSurface) {
-    return target;
+    return target.forget();
   }
 
   RefPtr<DataSourceSurface> dataSource = aSurface->GetDataSurface();
   MOZ_ASSERT(dataSource);
 
   if (aEdgeMode == EDGE_MODE_WRAP) {
     TileSurface(dataSource, target, intersectInDestSpace.TopLeft());
-    return target;
+    return target.forget();
   }
 
   CopyRect(dataSource, target, intersectInSourceSpace,
            intersectInDestSpace.TopLeft());
 
   if (aEdgeMode == EDGE_MODE_DUPLICATE) {
     DuplicateEdges(target, intersectInDestSpace);
   }
 
-  return target;
+  return target.forget();
 }
 
 /* static */ TemporaryRef<FilterNode>
 FilterNodeSoftware::Create(FilterType aType)
 {
   RefPtr<FilterNodeSoftware> filter;
   switch (aType) {
     case FilterType::BLEND:
@@ -618,17 +618,17 @@ FilterNodeSoftware::Create(FilterType aT
       break;
     case FilterType::DISTANT_DIFFUSE:
       filter = new FilterNodeLightingSoftware<DistantLightSoftware, DiffuseLightingSoftware>("FilterNodeLightingSoftware<DistantLight, DiffuseLighting>");
       break;
     case FilterType::DISTANT_SPECULAR:
       filter = new FilterNodeLightingSoftware<DistantLightSoftware, SpecularLightingSoftware>("FilterNodeLightingSoftware<DistantLight, SpecularLighting>");
       break;
   }
-  return filter;
+  return filter.forget();
 }
 
 void
 FilterNodeSoftware::Draw(DrawTarget* aDrawTarget,
                          const Rect &aSourceRect,
                          const Point &aDestPoint,
                          const DrawOptions &aOptions)
 {
@@ -852,17 +852,17 @@ FilterNodeSoftware::GetInputDataSourceSu
   }
 
 #ifdef DEBUG_DUMP_SURFACES
   printf("<img src='"); DumpAsPNG(result); printf("'></section>");
 #endif
 
   MOZ_ASSERT(!result || result->GetSize() == aRect.Size(), "wrong surface size");
 
-  return result;
+  return result.forget();
 }
 
 IntRect
 FilterNodeSoftware::GetInputRectInRect(uint32_t aInputEnumIndex,
                                        const IntRect &aInRect)
 {
   if (IntRectOverflows(aInRect)) {
     return IntRect();
@@ -1017,17 +1017,17 @@ FilterNodeBlendSoftware::Render(const In
 
   // Second case: both are non-transparent.
   if (input1 && input2) {
     // Apply normal filtering.
     return FilterProcessing::ApplyBlending(input1, input2, mBlendMode);
   }
 
   // Third case: one of them is transparent. Return the non-transparent one.
-  return input1 ? input1 : input2;
+  return input1 ? input1.forget() : input2.forget();
 }
 
 void
 FilterNodeBlendSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   RequestInputRect(IN_BLEND_IN, aRect);
   RequestInputRect(IN_BLEND_IN2, aRect);
 }
@@ -1099,32 +1099,32 @@ FilterNodeTransformSoftware::Render(cons
 
   if (!input) {
     return nullptr;
   }
 
   Matrix transform = Matrix::Translation(srcRect.x, srcRect.y) * mMatrix *
                      Matrix::Translation(-aRect.x, -aRect.y);
   if (transform.IsIdentity() && srcRect.Size() == aRect.Size()) {
-    return input;
+    return input.forget();
   }
 
   RefPtr<DrawTarget> dt =
     Factory::CreateDrawTarget(BackendType::CAIRO, aRect.Size(), input->GetFormat());
   if (!dt) {
     return nullptr;
   }
 
   Rect r(0, 0, srcRect.width, srcRect.height);
   dt->SetTransform(transform);
   dt->DrawSurface(input, r, r, DrawSurfaceOptions(mFilter));
 
   RefPtr<SourceSurface> result = dt->Snapshot();
   RefPtr<DataSourceSurface> resultData = result->GetDataSurface();
-  return resultData;
+  return resultData.forget();
 }
 
 void
 FilterNodeTransformSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   RequestInputRect(IN_TRANSFORM_IN, SourceRectForOutputRect(aRect));
 }
 
@@ -1224,17 +1224,17 @@ ApplyMorphology(const IntRect& aSourceRe
 
     int32_t destStride = dest->Stride();
     uint8_t* destData = dest->GetData();
 
     FilterProcessing::ApplyMorphologyVertical(
       tmpData, tmpStride, destData, destStride, destRect, ry, aOperator);
   }
 
-  return dest;
+  return dest.forget();
 }
 
 TemporaryRef<DataSourceSurface>
 FilterNodeMorphologySoftware::Render(const IntRect& aRect)
 {
   IntRect srcRect = aRect;
   srcRect.Inflate(mRadii);
 
@@ -1243,17 +1243,17 @@ FilterNodeMorphologySoftware::Render(con
   if (!input) {
     return nullptr;
   }
 
   int32_t rx = mRadii.width;
   int32_t ry = mRadii.height;
 
   if (rx == 0 && ry == 0) {
-    return input;
+    return input.forget();
   }
 
   return ApplyMorphology(srcRect, input, aRect, rx, ry, mOperator);
 }
 
 void
 FilterNodeMorphologySoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
@@ -1320,17 +1320,17 @@ Premultiply(DataSourceSurface* aSurface)
   uint8_t* inputData = aSurface->GetData();
   int32_t inputStride = aSurface->Stride();
   uint8_t* targetData = target->GetData();
   int32_t targetStride = target->Stride();
 
   FilterProcessing::DoPremultiplicationCalculation(
     size, targetData, targetStride, inputData, inputStride);
 
-  return target;
+  return target.forget();
 }
 
 static TemporaryRef<DataSourceSurface>
 Unpremultiply(DataSourceSurface* aSurface)
 {
   if (aSurface->GetFormat() == SurfaceFormat::A8) {
     return aSurface;
   }
@@ -1345,17 +1345,17 @@ Unpremultiply(DataSourceSurface* aSurfac
   uint8_t* inputData = aSurface->GetData();
   int32_t inputStride = aSurface->Stride();
   uint8_t* targetData = target->GetData();
   int32_t targetStride = target->Stride();
 
   FilterProcessing::DoUnpremultiplicationCalculation(
     size, targetData, targetStride, inputData, inputStride);
 
-  return target;
+  return target.forget();
 }
 
 TemporaryRef<DataSourceSurface>
 FilterNodeColorMatrixSoftware::Render(const IntRect& aRect)
 {
   RefPtr<DataSourceSurface> input =
     GetInputDataSourceSurface(IN_COLOR_MATRIX_IN, aRect, NEED_COLOR_CHANNELS);
   if (!input) {
@@ -1368,17 +1368,17 @@ FilterNodeColorMatrixSoftware::Render(co
 
   RefPtr<DataSourceSurface> result =
     FilterProcessing::ApplyColorMatrix(input, mMatrix);
 
   if (mAlphaMode == ALPHA_MODE_PREMULTIPLIED) {
     result = Premultiply(result);
   }
 
-  return result;
+  return result.forget();
 }
 
 void
 FilterNodeColorMatrixSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   RequestInputRect(IN_COLOR_MATRIX_IN, aRect);
 }
 
@@ -1447,17 +1447,17 @@ FilterNodeFloodSoftware::Render(const In
         targetData[x] = alpha;
       }
       targetData += stride;
     }
   } else {
     MOZ_CRASH();
   }
 
-  return target;
+  return target.forget();
 }
 
 // Override GetOutput to get around caching. Rendering simple floods is
 // comparatively fast.
 TemporaryRef<DataSourceSurface>
 FilterNodeFloodSoftware::GetOutput(const IntRect& aRect)
 {
   return Render(aRect);
@@ -1559,17 +1559,17 @@ FilterNodeTileSoftware::Render(const Int
         }
       }
       MOZ_ASSERT(input->GetFormat() == target->GetFormat(), "different surface formats from the same input?");
 
       CopyRect(input, target, srcRect - srcRect.TopLeft(), destRect.TopLeft() - aRect.TopLeft());
     }
   }
 
-  return target;
+  return target.forget();
 }
 
 void
 FilterNodeTileSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   // Do not request anything.
   // Source rects for the tile filter can be discontinuous with large gaps
   // between them. Requesting those from our input filter might cause it to
@@ -1701,32 +1701,32 @@ FilterNodeComponentTransferSoftware::Ren
 
     if (colorChannelsBecomeBlack) {
       input = FilterProcessing::ExtractAlpha(input);
     }
   }
 
   SurfaceFormat format = input->GetFormat();
   if (format == SurfaceFormat::A8 && mDisableA) {
-    return input;
+    return input.forget();
   }
 
   RefPtr<DataSourceSurface> target =
     Factory::CreateDataSourceSurface(aRect.Size(), format);
   if (!target) {
     return nullptr;
   }
 
   if (format == SurfaceFormat::A8) {
     TransferComponents<1>(input, target, &lookupTables[B8G8R8A8_COMPONENT_BYTEOFFSET_A]);
   } else {
     TransferComponents<4>(input, target, lookupTables);
   }
 
-  return target;
+  return target.forget();
 }
 
 void
 FilterNodeComponentTransferSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   RequestInputRect(IN_TRANSFER_IN, aRect);
 }
 
@@ -2421,17 +2421,17 @@ FilterNodeConvolveMatrixSoftware::DoRend
                     aRect.width, aRect.height, sourceStride, targetStride,
                     x, y, intKernel, bias, shiftL, shiftR, mPreserveAlpha,
                     mKernelSize.width, mKernelSize.height, mTarget.x, mTarget.y,
                     aKernelUnitLengthX, aKernelUnitLengthY);
     }
   }
   delete[] intKernel;
 
-  return target;
+  return target.forget();
 }
 
 void
 FilterNodeConvolveMatrixSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   RequestInputRect(IN_CONVOLVE_MATRIX_IN, InflatedSourceRect(aRect));
 }
 
@@ -2562,17 +2562,17 @@ FilterNodeDisplacementMapSoftware::Rende
         scaleOver255 * mapData[mapIndex + xChannel] + scaleAdjustment;
       int32_t sourceY = y +
         scaleOver255 * mapData[mapIndex + yChannel] + scaleAdjustment;
       *(uint32_t*)(targetData + targIndex) =
         ColorAtPoint(sourceData, sourceStride, sourceX, sourceY);
     }
   }
 
-  return target;
+  return target.forget();
 }
 
 void
 FilterNodeDisplacementMapSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   RequestInputRect(IN_DISPLACEMENT_MAP_IN, InflatedSourceOrDestRect(aRect));
   RequestInputRect(IN_DISPLACEMENT_MAP_IN2, aRect);
 }
@@ -2819,17 +2819,17 @@ FilterNodeCompositeSoftware::Render(cons
           break;
         case COMPOSITE_OPERATOR_IN:
           // Transparency always wins. We're completely transparent now and
           // no additional input can get rid of that transparency.
           return nullptr;
       }
     }
   }
-  return dest;
+  return dest.forget();
 }
 
 void
 FilterNodeCompositeSoftware::RequestFromInputsForRect(const IntRect &aRect)
 {
   for (size_t inputIndex = 0; inputIndex < NumberOfSetInputs(); inputIndex++) {
     RequestInputRect(IN_COMPOSITE_IN_START + inputIndex, aRect);
   }
@@ -3424,17 +3424,17 @@ FilterNodeLightingSoftware<LightType, Li
       Point3D pt(pointInFilterSpace.x, pointInFilterSpace.y, Z);
       Point3D rayDir = mLight.GetVectorToLight(pt);
       uint32_t color = mLight.GetColor(lightColor, rayDir);
 
       *(uint32_t*)(targetData + targetIndex) = mLighting.LightPixel(normal, rayDir, color);
     }
   }
 
-  return target;
+  return target.forget();
 }
 
 DiffuseLightingSoftware::DiffuseLightingSoftware()
  : mDiffuseConstant(0)
 {
 }
 
 bool
--- a/gfx/2d/FilterProcessing.cpp
+++ b/gfx/2d/FilterProcessing.cpp
@@ -21,17 +21,17 @@ FilterProcessing::ExtractAlpha(DataSourc
   if (Factory::HasSSE2()) {
 #ifdef USE_SSE2
     ExtractAlpha_SSE2(size, sourceData, sourceStride, alphaData, alphaStride);
 #endif
   } else {
     ExtractAlpha_Scalar(size, sourceData, sourceStride, alphaData, alphaStride);
   }
 
-  return alpha;
+  return alpha.forget();
 }
 
 TemporaryRef<DataSourceSurface>
 FilterProcessing::ConvertToB8G8R8A8(SourceSurface* aSurface)
 {
   if (Factory::HasSSE2()) {
 #ifdef USE_SSE2
     return ConvertToB8G8R8A8_SSE2(aSurface);
@@ -157,17 +157,17 @@ FilterProcessing::CombineColorChannels(D
   if (Factory::HasSSE2()) {
 #ifdef USE_SSE2
     CombineColorChannels_SSE2(size, resultStride, resultData, channelStride, channel0Data, channel1Data, channel2Data, channel3Data);
 #endif
   } else {
     CombineColorChannels_Scalar(size, resultStride, resultData, channelStride, channel0Data, channel1Data, channel2Data, channel3Data);
   }
 
-  return result;
+  return result.forget();
 }
 
 void
 FilterProcessing::DoPremultiplicationCalculation(const IntSize& aSize,
                                                  uint8_t* aTargetData, int32_t aTargetStride,
                                                  uint8_t* aSourceData, int32_t aSourceStride)
 {
   if (Factory::HasSSE2()) {
--- a/gfx/2d/FilterProcessingScalar.cpp
+++ b/gfx/2d/FilterProcessingScalar.cpp
@@ -80,17 +80,17 @@ ApplyBlending_Scalar(DataSourceSurface* 
         targetData[targetIndex + i] = static_cast<uint8_t>(val);
       }
       uint32_t alpha = 255 * 255 - (255 - qa) * (255 - qb);
       targetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A] =
         FilterProcessing::FastDivideBy255<uint8_t>(alpha);
     }
   }
 
-  return target;
+  return target.forget();
 }
 
 TemporaryRef<DataSourceSurface>
 FilterProcessing::ApplyBlending_Scalar(DataSourceSurface* aInput1, DataSourceSurface* aInput2,
                                        BlendMode aBlendMode)
 {
   switch (aBlendMode) {
     case BLEND_MODE_MULTIPLY:
--- a/gfx/2d/HelpersD2D.h
+++ b/gfx/2d/HelpersD2D.h
@@ -350,30 +350,30 @@ GetTransformedGeometry(ID2D1Geometry *aG
 {
   RefPtr<ID2D1PathGeometry> tmpGeometry;
   D2DFactory()->CreatePathGeometry(byRef(tmpGeometry));
   RefPtr<ID2D1GeometrySink> currentSink;
   tmpGeometry->Open(byRef(currentSink));
   aGeometry->Simplify(D2D1_GEOMETRY_SIMPLIFICATION_OPTION_CUBICS_AND_LINES,
                       aTransform, currentSink);
   currentSink->Close();
-  return tmpGeometry;
+  return tmpGeometry.forget();
 }
 
 static TemporaryRef<ID2D1Geometry>
 IntersectGeometry(ID2D1Geometry *aGeometryA, ID2D1Geometry *aGeometryB)
 {
   RefPtr<ID2D1PathGeometry> pathGeom;
   D2DFactory()->CreatePathGeometry(byRef(pathGeom));
   RefPtr<ID2D1GeometrySink> sink;
   pathGeom->Open(byRef(sink));
   aGeometryA->CombineWithGeometry(aGeometryB, D2D1_COMBINE_MODE_INTERSECT, nullptr, sink);
   sink->Close();
 
-  return pathGeom;
+  return pathGeom.forget();
 }
 
 static TemporaryRef<ID2D1StrokeStyle>
 CreateStrokeStyleForOptions(const StrokeOptions &aStrokeOptions)
 {
   RefPtr<ID2D1StrokeStyle> style;
 
   D2D1_CAP_STYLE capStyle;
@@ -437,17 +437,17 @@ CreateStrokeStyleForOptions(const Stroke
                                   aStrokeOptions.mMiterLimit),
       nullptr, 0, byRef(style));
   }
 
   if (FAILED(hr)) {
     gfxWarning() << "Failed to create Direct2D stroke style.";
   }
 
-  return style;
+  return style.forget();
 }
 
 // This creates a (partially) uploaded bitmap for a DataSourceSurface. It
 // uploads the minimum requirement and possibly downscales. It adjusts the
 // input Matrix to compensate.
 static TemporaryRef<ID2D1Bitmap>
 CreatePartialBitmapForSurface(DataSourceSurface *aSurface, const Matrix &aDestinationTransform,
                               const IntSize &aDestinationSize, ExtendMode aExtendMode,
@@ -520,17 +520,17 @@ CreatePartialBitmapForSurface(DataSource
     aRT->CreateBitmap(D2D1::SizeU(uint32_t(uploadRect.width), uint32_t(uploadRect.height)),
                       aSurface->GetData() + int(uploadRect.x) * 4 + int(uploadRect.y) * stride,
                       stride,
                       D2D1::BitmapProperties(D2DPixelFormat(aSurface->GetFormat())),
                       byRef(bitmap));
 
     aSourceTransform.Translate(uploadRect.x, uploadRect.y);
 
-    return bitmap;
+    return bitmap.forget();
   } else {
     int Bpp = BytesPerPixel(aSurface->GetFormat());
 
     if (Bpp != 4) {
       // This shouldn't actually happen in practice!
       MOZ_ASSERT(false);
       return nullptr;
     }
@@ -566,16 +566,16 @@ CreatePartialBitmapForSurface(DataSource
     
     aRT->CreateBitmap(D2D1::SizeU(newSize.width, newSize.height),
                       scaler.GetScaledData(), scaler.GetStride(),
                       D2D1::BitmapProperties(D2DPixelFormat(aSurface->GetFormat())),
                       byRef(bitmap));
 
     aSourceTransform.Scale(Float(size.width / newSize.width),
                            Float(size.height / newSize.height));
-    return bitmap;
+    return bitmap.forget();
   }
 }
 
 }
 }
 
 #endif /* MOZILLA_GFX_HELPERSD2D_H_ */
--- a/gfx/2d/PathCG.cpp
+++ b/gfx/2d/PathCG.cpp
@@ -95,26 +95,24 @@ PathBuilderCG::CurrentPoint() const
 void
 PathBuilderCG::EnsureActive(const Point &aPoint)
 {
 }
 
 TemporaryRef<Path>
 PathBuilderCG::Finish()
 {
-  RefPtr<PathCG> path = new PathCG(mCGPath, mFillRule);
-  return path;
+  return new PathCG(mCGPath, mFillRule);
 }
 
 TemporaryRef<PathBuilder>
 PathCG::CopyToBuilder(FillRule aFillRule) const
 {
   CGMutablePathRef path = CGPathCreateMutableCopy(mPath);
-  RefPtr<PathBuilderCG> builder = new PathBuilderCG(path, aFillRule);
-  return builder;
+  return new PathBuilderCG(path, aFillRule);
 }
 
 
 
 TemporaryRef<PathBuilder>
 PathCG::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
 {
   // 10.7 adds CGPathCreateMutableCopyByTransformingPath it might be faster than doing
@@ -164,18 +162,17 @@ PathCG::TransformedCopyToBuilder(const M
     }
   };
 
   TransformApplier ta;
   ta.path = CGPathCreateMutable();
   ta.transform = GfxMatrixToCGAffineTransform(aTransform);
 
   CGPathApply(mPath, &ta, TransformApplier::TranformCGPathApplierFunc);
-  RefPtr<PathBuilderCG> builder = new PathBuilderCG(ta.path, aFillRule);
-  return builder;
+  return new 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
@@ -162,28 +162,28 @@ PathCairo::~PathCairo()
 TemporaryRef<PathBuilder>
 PathCairo::CopyToBuilder(FillRule aFillRule) const
 {
   RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
 
   builder->mPathData = mPathData;
   builder->mCurrentPoint = mCurrentPoint;
 
-  return builder;
+  return builder.forget();
 }
 
 TemporaryRef<PathBuilder>
 PathCairo::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
 {
   RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
 
   AppendPathToBuilder(builder, &aTransform);
   builder->mCurrentPoint = aTransform * mCurrentPoint;
 
-  return builder;
+  return builder.forget();
 }
 
 bool
 PathCairo::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const
 {
   Matrix inverse = aTransform;
   inverse.Invert();
   Point transformed = inverse * aPoint;
--- a/gfx/2d/PathD2D.cpp
+++ b/gfx/2d/PathD2D.cpp
@@ -348,17 +348,17 @@ PathD2D::TransformedCopyToBuilder(const 
   RefPtr<PathBuilderD2D> pathBuilder = new PathBuilderD2D(sink, path, aFillRule);
   
   pathBuilder->mCurrentPoint = aTransform * mEndPoint;
   
   if (mEndedActive) {
     pathBuilder->mFigureActive = true;
   }
 
-  return pathBuilder;
+  return pathBuilder.forget();
 }
 
 void
 PathD2D::StreamToSink(PathSink *aSink) const
 {
   HRESULT hr;
 
   StreamingGeometrySink sink(aSink);
--- a/gfx/2d/PathRecording.cpp
+++ b/gfx/2d/PathRecording.cpp
@@ -85,17 +85,17 @@ PathRecording::~PathRecording()
 }
 
 TemporaryRef<PathBuilder>
 PathRecording::CopyToBuilder(FillRule aFillRule) const
 {
   RefPtr<PathBuilder> pathBuilder = mPath->CopyToBuilder(aFillRule);
   RefPtr<PathBuilderRecording> recording = new PathBuilderRecording(pathBuilder, aFillRule);
   recording->mPathOps = mPathOps;
-  return recording;
+  return recording.forget();
 }
 
 TemporaryRef<PathBuilder>
 PathRecording::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
 {
   RefPtr<PathBuilder> pathBuilder = mPath->TransformedCopyToBuilder(aTransform, aFillRule);
   RefPtr<PathBuilderRecording> recording = new PathBuilderRecording(pathBuilder, aFillRule);
   typedef std::vector<PathOp> pathOpVec;
@@ -108,13 +108,13 @@ PathRecording::TransformedCopyToBuilder(
     if (sPointCount[newPathOp.mType] >= 2) {
       newPathOp.mP2 = aTransform * iter->mP2;
     }
     if (sPointCount[newPathOp.mType] >= 3) {
       newPathOp.mP3 = aTransform * iter->mP3;
     }
     recording->mPathOps.push_back(newPathOp);
   }
-  return recording;
+  return recording.forget();
 }
 
 }
 }
--- a/gfx/2d/PathSkia.cpp
+++ b/gfx/2d/PathSkia.cpp
@@ -100,18 +100,17 @@ PathBuilderSkia::CurrentPoint() const
   }
   SkPoint point = mPath.getPoint(pointCount - 1);
   return Point(SkScalarToFloat(point.fX), SkScalarToFloat(point.fY));
 }
 
 TemporaryRef<Path>
 PathBuilderSkia::Finish()
 {
-  RefPtr<PathSkia> path = new PathSkia(mPath, mFillRule);
-  return path;
+  return new PathSkia(mPath, mFillRule);
 }
 
 void
 PathBuilderSkia::AppendPath(const SkPath &aPath)
 {
   mPath.addPath(aPath);
 }
 
@@ -119,18 +118,17 @@ TemporaryRef<PathBuilder>
 PathSkia::CopyToBuilder(FillRule aFillRule) const
 {
   return TransformedCopyToBuilder(Matrix(), aFillRule);
 }
 
 TemporaryRef<PathBuilder>
 PathSkia::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
 {
-  RefPtr<PathBuilderSkia> builder = new PathBuilderSkia(aTransform, mPath, aFillRule);
-  return builder;
+  return new 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/RadialGradientEffectD2D1.cpp
+++ b/gfx/2d/RadialGradientEffectD2D1.cpp
@@ -374,13 +374,13 @@ RadialGradientEffectD2D1::CreateGradient
   props.extendModes = &extendMode;
 
   HRESULT hr = mEffectContext->CreateResourceTexture(nullptr, &props, &textureData.front(), &stride, 4096 * 4, byRef(tex));
 
   if (FAILED(hr)) {
     gfxWarning() << "Failed to create resource texture: " << hr;
   }
 
-  return tex;
+  return tex.forget();
 }
 
 }
 }
--- a/gfx/2d/ScaledFontBase.cpp
+++ b/gfx/2d/ScaledFontBase.cpp
@@ -113,17 +113,17 @@ ScaledFontBase::GetPathForGlyphs(const G
 
     cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);
 
     RefPtr<PathCairo> newPath = new PathCairo(ctx);
     if (isNewContext) {
       cairo_destroy(ctx);
     }
 
-    return newPath;
+    return newPath.forget();
   }
 #endif
   return nullptr;
 }
 
 void
 ScaledFontBase::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, BackendType aBackendType, const Matrix *aTransformHint)
 {
--- a/gfx/2d/ScaledFontMac.cpp
+++ b/gfx/2d/ScaledFontMac.cpp
@@ -91,19 +91,18 @@ ScaledFontMac::GetPathForGlyphs(const Gl
                                                            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);
       CGPathRelease(path);
       return ret;
-  } else {
-      return ScaledFontBase::GetPathForGlyphs(aBuffer, aTarget);
   }
+  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)) {
     ScaledFontBase::CopyGlyphsToBuilder(aBuffer, aBuilder, aBackendType, aTransformHint);
     return;
--- a/gfx/2d/SourceSurfaceCG.cpp
+++ b/gfx/2d/SourceSurfaceCG.cpp
@@ -38,19 +38,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.
-  dataSurf = new DataSourceSurfaceWrapper(dataSurf);
-
-  return dataSurf;
+  return new DataSourceSurfaceWrapper(dataSurf);
 }
 
 static void releaseCallback(void *info, const void *data, size_t size) {
   free(info);
 }
 
 CGImageRef
 CreateCGImage(void *aInfo,
--- a/gfx/2d/SourceSurfaceCairo.cpp
+++ b/gfx/2d/SourceSurfaceCairo.cpp
@@ -76,19 +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.
-  dataSurf = new DataSourceSurfaceWrapper(dataSurf);
-
-  return dataSurf;
+  return new DataSourceSurfaceWrapper(dataSurf);
 }
 
 cairo_surface_t*
 SourceSurfaceCairo::GetSurface() const
 {
   return mSurface;
 }
 
--- a/gfx/2d/SourceSurfaceD2D.cpp
+++ b/gfx/2d/SourceSurfaceD2D.cpp
@@ -40,17 +40,17 @@ SourceSurfaceD2D::IsValid() const
   return mDevice == Factory::GetDirect3D10Device();
 }
 
 TemporaryRef<DataSourceSurface>
 SourceSurfaceD2D::GetDataSurface()
 {
   RefPtr<DataSourceSurfaceD2D> result = new DataSourceSurfaceD2D(this);
   if (result->IsValid()) {
-    return result;
+    return result.forget();
   }
   return nullptr;
 }
 
 bool
 SourceSurfaceD2D::InitFromData(unsigned char *aData,
                                const IntSize &aSize,
                                int32_t aStride,
--- a/gfx/2d/SourceSurfaceD2DTarget.cpp
+++ b/gfx/2d/SourceSurfaceD2DTarget.cpp
@@ -67,17 +67,17 @@ SourceSurfaceD2DTarget::GetDataSurface()
   HRESULT hr = Factory::GetDirect3D10Device()->CreateTexture2D(&desc, nullptr, byRef(dataSurf->mTexture));
 
   if (FAILED(hr)) {
     gfxDebug() << "Failed to create staging texture for SourceSurface. Code: " << hr;
     return nullptr;
   }
   Factory::GetDirect3D10Device()->CopyResource(dataSurf->mTexture, mTexture);
 
-  return dataSurf;
+  return dataSurf.forget();
 }
 
 void*
 SourceSurfaceD2DTarget::GetNativeSurface(NativeSurfaceType aType)
 {
   if (aType == NativeSurfaceType::D3D10_TEXTURE) {
     return static_cast<void*>(mTexture.get());
   }