From edd61684e61665fed6d812a66c0802aa4e0e89aa Mon Sep 17 00:00:00 2001 From: Daniel Holbert Date: Mon, 11 May 2026 16:17:32 +0000 Subject: [PATCH] Bug 2037700: Use MakeRefPtr and MakeAndAddRef instead of 'new' to allocate gfxDrawable instances. r=media-playback-reviewers,layout-reviewers,emilio,padenot I'm using MakeRefPtr nearly everywhere, except when the old code was immediately returning foo.forget() - in those spots, I'm using MakeAndAddRef for brevity (since that function does the forget() internally). Differential Revision: https://phabricator.services.mozilla.com/D299013 --- dom/base/nsContentUtils.cpp | 3 +-- dom/media/mediacontrol/MediaControlUtils.h | 3 +-- gfx/thebes/gfxDrawable.cpp | 4 +--- image/ClippedImage.cpp | 3 +-- image/OrientedImage.cpp | 2 +- image/VectorImage.cpp | 6 ++---- layout/painting/nsImageRenderer.cpp | 9 ++++----- .../cocoa/MediaHardwareKeysEventSourceMacMediaCenter.mm | 2 +- 8 files changed, 12 insertions(+), 20 deletions(-) diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 42a8a512a00a..a3462dc12bf5 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -9632,8 +9632,7 @@ already_AddRefed nsContentUtils::IPCImageToImage( return nullptr; } - RefPtr drawable = - new gfxSurfaceDrawable(surface, surface->GetSize()); + auto drawable = MakeRefPtr(surface, surface->GetSize()); nsCOMPtr imageContainer = image::ImageOps::CreateFromDrawable(drawable); return imageContainer.forget(); diff --git a/dom/media/mediacontrol/MediaControlUtils.h b/dom/media/mediacontrol/MediaControlUtils.h index 4994e9656094..75fad7c68168 100644 --- a/dom/media/mediacontrol/MediaControlUtils.h +++ b/dom/media/mediacontrol/MediaControlUtils.h @@ -105,8 +105,7 @@ inline nsresult GetEncodedImageBuffer(gfx::DataSourceSurface* aSurface, return NS_ERROR_FAILURE; } - RefPtr drawable = - new gfxSurfaceDrawable(aSurface, aSurface->GetSize()); + auto drawable = MakeRefPtr(aSurface, aSurface->GetSize()); nsCOMPtr image = image::ImageOps::CreateFromDrawable(drawable); nsCOMPtr inputStream; diff --git a/gfx/thebes/gfxDrawable.cpp b/gfx/thebes/gfxDrawable.cpp index 39f46e1f48a6..29c582fe3162 100644 --- a/gfx/thebes/gfxDrawable.cpp +++ b/gfx/thebes/gfxDrawable.cpp @@ -112,9 +112,7 @@ already_AddRefed gfxCallbackDrawable::MakeSurfaceDrawable( RefPtr surface = dt->Snapshot(); if (surface) { - RefPtr drawable = - new gfxSurfaceDrawable(surface, mSize); - return drawable.forget(); + return MakeAndAddRef(surface, mSize); } return nullptr; } diff --git a/image/ClippedImage.cpp b/image/ClippedImage.cpp index 6a4e2a175059..a8e6731b3859 100644 --- a/image/ClippedImage.cpp +++ b/image/ClippedImage.cpp @@ -356,8 +356,7 @@ ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize, } // Create a drawable from that surface. - RefPtr drawable = - new gfxSurfaceDrawable(surface, aSize); + auto drawable = MakeRefPtr(surface, aSize); // Draw. gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion, diff --git a/image/OrientedImage.cpp b/image/OrientedImage.cpp index b488022c5382..fdef4084d867 100644 --- a/image/OrientedImage.cpp +++ b/image/OrientedImage.cpp @@ -88,7 +88,7 @@ already_AddRefed OrientedImage::OrientSurface( } // Create our drawable. - RefPtr drawable = new gfxSurfaceDrawable(aSurface, originalSize); + auto drawable = MakeRefPtr(aSurface, originalSize); // Determine an appropriate format for the surface. gfx::SurfaceFormat surfaceFormat = IsOpaque(aSurface->GetFormat()) diff --git a/image/VectorImage.cpp b/image/VectorImage.cpp index 9a34895a83fb..54d6fd95ce0f 100644 --- a/image/VectorImage.cpp +++ b/image/VectorImage.cpp @@ -956,8 +956,7 @@ VectorImage::Draw(gfxContext* aContext, const nsIntSize& aSize, std::tie(sourceSurface, params.size) = LookupCachedSurface(aSize, params.svgContext, aFlags); if (sourceSurface) { - RefPtr drawable = - new gfxSurfaceDrawable(sourceSurface, params.size); + auto drawable = MakeRefPtr(sourceSurface, params.size); Show(drawable, params); return ImgDrawResult::SUCCESS; } @@ -980,8 +979,7 @@ VectorImage::Draw(gfxContext* aContext, const nsIntSize& aSize, return ImgDrawResult::SUCCESS; } - RefPtr drawable = - new gfxSurfaceDrawable(sourceSurface, params.size); + auto drawable = MakeRefPtr(sourceSurface, params.size); Show(drawable, params); SendFrameComplete(didCache, params.flags); return ImgDrawResult::SUCCESS; diff --git a/layout/painting/nsImageRenderer.cpp b/layout/painting/nsImageRenderer.cpp index eb7b1b46ed53..00c28ad48d97 100644 --- a/layout/painting/nsImageRenderer.cpp +++ b/layout/painting/nsImageRenderer.cpp @@ -109,7 +109,7 @@ static already_AddRefed GetSymbolicIconImage(nsAtom* aName, if (NS_WARN_IF(!surface)) { return nullptr; } - RefPtr drawable = new gfxSurfaceDrawable(surface, surface->GetSize()); + auto drawable = MakeRefPtr(surface, surface->GetSize()); nsCOMPtr container = ImageOps::CreateFromDrawable(drawable); MOZ_ASSERT(container); lookup.Set(SymbolicImageEntry{std::move(key), std::move(container)}); @@ -809,10 +809,9 @@ already_AddRefed nsImageRenderer::DrawableForElement( } NS_ASSERTION(mImageElementSurface.GetSourceSurface(), "Surface should be ready."); - RefPtr drawable = - new gfxSurfaceDrawable(mImageElementSurface.GetSourceSurface().get(), - mImageElementSurface.mSize); - return drawable.forget(); + return MakeAndAddRef( + mImageElementSurface.GetSourceSurface().get(), + mImageElementSurface.mSize); } ImgDrawResult nsImageRenderer::DrawLayer( diff --git a/widget/cocoa/MediaHardwareKeysEventSourceMacMediaCenter.mm b/widget/cocoa/MediaHardwareKeysEventSourceMacMediaCenter.mm index e4e7701e3a38..0c312fa9712f 100644 --- a/widget/cocoa/MediaHardwareKeysEventSourceMacMediaCenter.mm +++ b/widget/cocoa/MediaHardwareKeysEventSourceMacMediaCenter.mm @@ -215,7 +215,7 @@ void MediaHardwareKeysEventSourceMacMediaCenter::SetMediaMetadata( break; } - RefPtr drawable = new gfxSurfaceDrawable( + auto drawable = MakeRefPtr( imageData.mDataSurface, imageData.mDataSurface->GetSize()); nsCOMPtr imageContainer = image::ImageOps::CreateFromDrawable(drawable);