Bug 1547472 - Ref count the ProfilerScreenshots structure r=gerald,jesup a=jcristau
authorBarret Rennie <barret@brennie.ca>
Fri, 24 May 2019 23:53:28 +0000
changeset 533448 d674f0f91eb487be6b23ea00f607da8843d079b4
parent 533447 54457d76cd8cc055a48a4955822e937511e1b460
child 533449 21c421820c7cf589615a9fd52085d62ae128eae2
push id11323
push userarchaeopteryx@coole-files.de
push dateTue, 28 May 2019 12:44:46 +0000
treeherdermozilla-beta@d674f0f91eb4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgerald, jesup, jcristau
bugs1547472
milestone68.0
Bug 1547472 - Ref count the ProfilerScreenshots structure r=gerald,jesup a=jcristau Differential Revision: https://phabricator.services.mozilla.com/D32524
gfx/layers/ProfilerScreenshots.cpp
gfx/layers/ProfilerScreenshots.h
gfx/layers/composite/CompositorScreenshotGrabber.cpp
gfx/layers/mlgpu/MLGPUScreenshotGrabber.cpp
gfx/webrender_bindings/RendererScreenshotGrabber.cpp
gfx/webrender_bindings/RendererScreenshotGrabber.h
--- a/gfx/layers/ProfilerScreenshots.cpp
+++ b/gfx/layers/ProfilerScreenshots.cpp
@@ -79,20 +79,22 @@ void ProfilerScreenshots::SubmitScreensh
   }
 
   int sourceThread = profiler_current_thread_id();
   uintptr_t windowIdentifier = aWindowIdentifier;
   IntSize originalSize = aOriginalSize;
   IntSize scaledSize = aScaledSize;
   TimeStamp timeStamp = aTimeStamp;
 
+  RefPtr<ProfilerScreenshots> self = this;
+
   mThread->Dispatch(NS_NewRunnableFunction(
       "ProfilerScreenshots::SubmitScreenshot",
-      [this, backingSurface, sourceThread, windowIdentifier, originalSize,
-       scaledSize, timeStamp]() {
+      [self{std::move(self)}, backingSurface, sourceThread, windowIdentifier,
+       originalSize, scaledSize, timeStamp]() {
         // Create a new surface that wraps backingSurface's data but has the
         // correct size.
         {
           DataSourceSurface::ScopedMap scopedMap(backingSurface,
                                                  DataSourceSurface::READ);
           RefPtr<DataSourceSurface> surf =
               Factory::CreateWrappingDataSourceSurface(
                   scopedMap.GetData(), scopedMap.GetStride(), scaledSize,
@@ -109,17 +111,17 @@ void ProfilerScreenshots::SubmitScreensh
                 sourceThread, JS::ProfilingCategoryPair::GRAPHICS,
                 "CompositorScreenshot",
                 MakeUnique<ScreenshotPayload>(timeStamp, std::move(dataURL),
                                               originalSize, windowIdentifier));
           }
         }
 
         // Return backingSurface back to the surface pool.
-        ReturnSurface(backingSurface);
+        self->ReturnSurface(backingSurface);
       }));
 #endif
 }
 
 already_AddRefed<DataSourceSurface> ProfilerScreenshots::TakeNextSurface() {
   MutexAutoLock mon(mMutex);
   if (!mAvailableSurfaces.IsEmpty()) {
     RefPtr<DataSourceSurface> surf = mAvailableSurfaces[0];
--- a/gfx/layers/ProfilerScreenshots.h
+++ b/gfx/layers/ProfilerScreenshots.h
@@ -29,19 +29,20 @@ namespace layers {
  * Screenshots have a fixed bounding size. The user of this class will usually
  * scale down the window contents first, ideally on the GPU, then read back the
  * small scaled down image into main memory, and then call SubmitScreenshot to
  * pass the data to the profiler.
  * This class encodes each screenshot to a JPEG data URL, on a separate thread.
  * This class manages that thread and recycles memory buffers.
  */
 class ProfilerScreenshots final {
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ProfilerScreenshots)
+
  public:
   ProfilerScreenshots();
-  ~ProfilerScreenshots();
 
   /**
    * Returns whether the profiler is currently active and is running with the
    * "screenshots" feature enabled.
    */
   static bool IsEnabled();
 
   /**
@@ -70,16 +71,18 @@ class ProfilerScreenshots final {
    *   supplied to the callback. Called zero or one times, synchronously.
    */
   void SubmitScreenshot(
       uintptr_t aWindowIdentifier, const gfx::IntSize& aOriginalSize,
       const gfx::IntSize& aScaledSize, const TimeStamp& aTimeStamp,
       const std::function<bool(gfx::DataSourceSurface*)>& aPopulateSurface);
 
  private:
+  ~ProfilerScreenshots();
+
   /**
    * Recycle a surface from mAvailableSurfaces or create a new one if all
    * surfaces are currently in use, up to some maximum limit.
    * Returns null if the limit is reached.
    * Can be called on any thread.
    */
   already_AddRefed<DataSourceSurface> TakeNextSurface();
 
--- a/gfx/layers/composite/CompositorScreenshotGrabber.cpp
+++ b/gfx/layers/composite/CompositorScreenshotGrabber.cpp
@@ -48,17 +48,17 @@ class CompositorScreenshotGrabberImpl fi
 
   already_AddRefed<AsyncReadbackBuffer> TakeNextBuffer(Compositor* aCompositor);
   void ReturnBuffer(AsyncReadbackBuffer* aBuffer);
 
   nsTArray<RefPtr<CompositingRenderTarget>> mTargets;
   nsTArray<RefPtr<AsyncReadbackBuffer>> mAvailableBuffers;
   Maybe<QueueItem> mCurrentFrameQueueItem;
   nsTArray<QueueItem> mQueue;
-  UniquePtr<ProfilerScreenshots> mProfilerScreenshots;
+  RefPtr<ProfilerScreenshots> mProfilerScreenshots;
   const IntSize mBufferSize;
 };
 
 CompositorScreenshotGrabber::CompositorScreenshotGrabber() {}
 
 CompositorScreenshotGrabber::~CompositorScreenshotGrabber() {}
 
 void CompositorScreenshotGrabber::MaybeGrabScreenshot(Compositor* aCompositor) {
@@ -200,17 +200,17 @@ CompositorScreenshotGrabberImpl::TakeNex
 void CompositorScreenshotGrabberImpl::ReturnBuffer(
     AsyncReadbackBuffer* aBuffer) {
   mAvailableBuffers.AppendElement(aBuffer);
 }
 
 void CompositorScreenshotGrabberImpl::ProcessQueue() {
   if (!mQueue.IsEmpty()) {
     if (!mProfilerScreenshots) {
-      mProfilerScreenshots = MakeUnique<ProfilerScreenshots>();
+      mProfilerScreenshots = new ProfilerScreenshots();
     }
     for (const auto& item : mQueue) {
       mProfilerScreenshots->SubmitScreenshot(
           item.mWindowIdentifier, item.mWindowSize, item.mScreenshotSize,
           item.mTimeStamp, [&item](DataSourceSurface* aTargetSurface) {
             return item.mScreenshotBuffer->MapAndCopyInto(aTargetSurface,
                                                           item.mScreenshotSize);
           });
--- a/gfx/layers/mlgpu/MLGPUScreenshotGrabber.cpp
+++ b/gfx/layers/mlgpu/MLGPUScreenshotGrabber.cpp
@@ -63,17 +63,17 @@ class MLGPUScreenshotGrabberImpl final {
 
   already_AddRefed<MLGTexture> TakeNextReadbackTexture(MLGDevice* aCompositor);
   void ReturnReadbackTexture(MLGTexture* aReadbackTexture);
 
   nsTArray<CachedLevel> mCachedLevels;
   nsTArray<RefPtr<MLGTexture>> mAvailableReadbackTextures;
   Maybe<QueueItem> mCurrentFrameQueueItem;
   nsTArray<QueueItem> mQueue;
-  UniquePtr<ProfilerScreenshots> mProfilerScreenshots;
+  RefPtr<ProfilerScreenshots> mProfilerScreenshots;
   const IntSize mReadbackTextureSize;
 };
 
 MLGPUScreenshotGrabber::~MLGPUScreenshotGrabber() {}
 
 void MLGPUScreenshotGrabber::MaybeGrabScreenshot(MLGDevice* aDevice,
                                                  MLGTexture* aTexture) {
   if (ProfilerScreenshots::IsEnabled()) {
@@ -292,17 +292,17 @@ MLGPUScreenshotGrabberImpl::TakeNextRead
 void MLGPUScreenshotGrabberImpl::ReturnReadbackTexture(
     MLGTexture* aReadbackTexture) {
   mAvailableReadbackTextures.AppendElement(aReadbackTexture);
 }
 
 void MLGPUScreenshotGrabberImpl::ProcessQueue() {
   if (!mQueue.IsEmpty()) {
     if (!mProfilerScreenshots) {
-      mProfilerScreenshots = MakeUnique<ProfilerScreenshots>();
+      mProfilerScreenshots = new ProfilerScreenshots();
     }
     for (const auto& item : mQueue) {
       mProfilerScreenshots->SubmitScreenshot(
           item.mWindowIdentifier, item.mWindowSize, item.mScreenshotSize,
           item.mTimeStamp, [&item](DataSourceSurface* aTargetSurface) {
             MLGMappedResource map;
             if (!item.mDevice->Map(item.mScreenshotReadbackTexture,
                                    MLGMapType::READ, &map)) {
--- a/gfx/webrender_bindings/RendererScreenshotGrabber.cpp
+++ b/gfx/webrender_bindings/RendererScreenshotGrabber.cpp
@@ -8,29 +8,29 @@ namespace wr {
 RendererScreenshotGrabber::RendererScreenshotGrabber() {
   mMaxScreenshotSize = ProfilerScreenshots::ScreenshotSize();
 }
 
 void RendererScreenshotGrabber::MaybeGrabScreenshot(
     Renderer* aRenderer, const gfx::IntSize& aWindowSize) {
   if (ProfilerScreenshots::IsEnabled()) {
     if (!mProfilerScreenshots) {
-      mProfilerScreenshots = MakeUnique<ProfilerScreenshots>();
+      mProfilerScreenshots = new ProfilerScreenshots();
     }
 
     GrabScreenshot(aRenderer, aWindowSize);
   } else if (mProfilerScreenshots) {
     Destroy(aRenderer);
   }
 }
 
 void RendererScreenshotGrabber::MaybeProcessQueue(Renderer* aRenderer) {
   if (ProfilerScreenshots::IsEnabled()) {
     if (!mProfilerScreenshots) {
-      mProfilerScreenshots = MakeUnique<ProfilerScreenshots>();
+      mProfilerScreenshots = new ProfilerScreenshots();
     }
 
     ProcessQueue(aRenderer);
   } else if (mProfilerScreenshots) {
     Destroy(aRenderer);
   }
 }
 
--- a/gfx/webrender_bindings/RendererScreenshotGrabber.h
+++ b/gfx/webrender_bindings/RendererScreenshotGrabber.h
@@ -84,15 +84,15 @@ class RendererScreenshotGrabber final {
    * after a call to |MaybeProcessQueue| so it will be not be processed until
    * the next frame.
    */
   Maybe<QueueItem> mCurrentFrameQueueItem;
 
   /**
    * Our handle to the profiler screenshots object.
    */
-  UniquePtr<mozilla::layers::ProfilerScreenshots> mProfilerScreenshots;
+  RefPtr<mozilla::layers::ProfilerScreenshots> mProfilerScreenshots;
 };
 
 }  // namespace wr
 }  // namespace mozilla
 
 #endif  // mozilla_layers_RendererScreenshotGrabber_h