gfx/webrender_bindings/RendererScreenshotGrabber.cpp
author Barret Rennie <barret@brennie.ca>
Thu, 21 Mar 2019 19:30:34 +0000
changeset 466228 13c0d857bf3c7cb5b21f25e2c35e1f2f3a57a4e2
parent 466227 d2c85db549ea25fb84c832df0a9654d084779736
child 475509 cc4f2327ff1941b6fbe29fee50df3172a1a80256
permissions -rw-r--r--
Bug 1444434 - Free the Gecko profiler screenshots structures in the WebRender renderer when profiling stops r=kvark Differential Revision: https://phabricator.services.mozilla.com/D23964

#include "RendererScreenshotGrabber.h"

using mozilla::layers::ProfilerScreenshots;

namespace mozilla {
namespace wr {

RendererScreenshotGrabber::RendererScreenshotGrabber() {
  mMaxScreenshotSize = ProfilerScreenshots::ScreenshotSize();
}

void RendererScreenshotGrabber::MaybeGrabScreenshot(
    Renderer* aRenderer, const gfx::IntSize& aWindowSize) {
  if (ProfilerScreenshots::IsEnabled()) {
    if (!mProfilerScreenshots) {
      mProfilerScreenshots = MakeUnique<ProfilerScreenshots>();
    }

    GrabScreenshot(aRenderer, aWindowSize);
  } else if (mProfilerScreenshots) {
    Destroy(aRenderer);
  }
}

void RendererScreenshotGrabber::MaybeProcessQueue(Renderer* aRenderer) {
  if (ProfilerScreenshots::IsEnabled()) {
    if (!mProfilerScreenshots) {
      mProfilerScreenshots = MakeUnique<ProfilerScreenshots>();
    }

    ProcessQueue(aRenderer);
  } else if (mProfilerScreenshots) {
    Destroy(aRenderer);
  }
}

void RendererScreenshotGrabber::Destroy(Renderer* aRenderer) {
  mQueue.Clear();
  mCurrentFrameQueueItem = Nothing();
  mProfilerScreenshots = nullptr;

  wr_renderer_release_profiler_structures(aRenderer);
}

void RendererScreenshotGrabber::GrabScreenshot(
    Renderer* aRenderer, const gfx::IntSize& aWindowSize) {
  gfx::IntSize screenshotSize;

  AsyncScreenshotHandle handle = wr_renderer_get_screenshot_async(
      aRenderer, 0, 0, aWindowSize.width, aWindowSize.height,
      mMaxScreenshotSize.width, mMaxScreenshotSize.height, ImageFormat::BGRA8,
      &screenshotSize.width, &screenshotSize.height);

  mCurrentFrameQueueItem = Some(QueueItem{
      TimeStamp::Now(),
      handle,
      screenshotSize,
      aWindowSize,
      reinterpret_cast<uintptr_t>(this),
  });
}

void RendererScreenshotGrabber::ProcessQueue(Renderer* aRenderer) {
  for (const auto& item : mQueue) {
    mProfilerScreenshots->SubmitScreenshot(
        item.mWindowIdentifier, item.mWindowSize, item.mScreenshotSize,
        item.mTimeStamp, [&item, aRenderer](DataSourceSurface* aTargetSurface) {
          DataSourceSurface::ScopedMap map(aTargetSurface,
                                           DataSourceSurface::WRITE);
          int32_t destStride = map.GetStride();

          bool success = wr_renderer_map_and_recycle_screenshot(
              aRenderer, item.mHandle, map.GetData(),
              destStride * aTargetSurface->GetSize().height, destStride);

          return success;
        });
  }

  mQueue.Clear();

  if (mCurrentFrameQueueItem) {
    mQueue.AppendElement(*mCurrentFrameQueueItem);
    mCurrentFrameQueueItem = Nothing();
  }
}

}  // namespace wr
}  // namespace mozilla