Backed out 4 changesets (bug 1879154) for causing bustage on nsUserCharacteristics...
[gecko.git] / gfx / webrender_bindings / RendererScreenshotGrabber.cpp
blob1d001d47275ee8ca4ed9d9b77b776dd16aa80b35
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "RendererScreenshotGrabber.h"
9 #include "RendererOGL.h"
11 #include "mozilla/gfx/2D.h"
13 using mozilla::layers::ProfilerScreenshots;
15 namespace mozilla {
16 namespace wr {
18 RendererScreenshotGrabber::RendererScreenshotGrabber() {
19 mMaxScreenshotSize = ProfilerScreenshots::ScreenshotSize();
22 void RendererScreenshotGrabber::MaybeGrabScreenshot(
23 RendererOGL* aRendererOGL, const gfx::IntSize& aWindowSize) {
24 bool isEnabled =
25 ProfilerScreenshots::IsEnabled() && aRendererOGL->EnsureAsyncScreenshot();
27 if (isEnabled) {
28 if (!mProfilerScreenshots) {
29 mProfilerScreenshots = new ProfilerScreenshots();
32 GrabScreenshot(aRendererOGL->GetRenderer(), aWindowSize);
33 } else if (mProfilerScreenshots) {
34 Destroy(aRendererOGL->GetRenderer());
38 void RendererScreenshotGrabber::MaybeProcessQueue(RendererOGL* aRendererOGL) {
39 bool isEnabled =
40 ProfilerScreenshots::IsEnabled() && aRendererOGL->EnsureAsyncScreenshot();
42 if (isEnabled) {
43 if (!mProfilerScreenshots) {
44 mProfilerScreenshots = new ProfilerScreenshots();
47 ProcessQueue(aRendererOGL->GetRenderer());
48 } else if (mProfilerScreenshots) {
49 Destroy(aRendererOGL->GetRenderer());
53 void RendererScreenshotGrabber::Destroy(Renderer* aRenderer) {
54 mQueue.Clear();
55 mCurrentFrameQueueItem = Nothing();
56 mProfilerScreenshots = nullptr;
58 wr_renderer_release_profiler_structures(aRenderer);
61 void RendererScreenshotGrabber::GrabScreenshot(
62 Renderer* aRenderer, const gfx::IntSize& aWindowSize) {
63 gfx::IntSize screenshotSize;
65 AsyncScreenshotHandle handle = wr_renderer_get_screenshot_async(
66 aRenderer, 0, 0, aWindowSize.width, aWindowSize.height,
67 mMaxScreenshotSize.width, mMaxScreenshotSize.height, ImageFormat::BGRA8,
68 &screenshotSize.width, &screenshotSize.height);
70 mCurrentFrameQueueItem = Some(QueueItem{
71 TimeStamp::Now(),
72 handle,
73 screenshotSize,
74 aWindowSize,
75 });
78 void RendererScreenshotGrabber::ProcessQueue(Renderer* aRenderer) {
79 for (const auto& item : mQueue) {
80 mProfilerScreenshots->SubmitScreenshot(
81 item.mWindowSize, item.mScreenshotSize, item.mTimeStamp,
82 [&item, aRenderer](gfx::DataSourceSurface* aTargetSurface) {
83 gfx::DataSourceSurface::ScopedMap map(aTargetSurface,
84 gfx::DataSourceSurface::WRITE);
85 int32_t destStride = map.GetStride();
87 bool success = wr_renderer_map_and_recycle_screenshot(
88 aRenderer, item.mHandle, map.GetData(),
89 destStride * aTargetSurface->GetSize().height, destStride);
91 return success;
92 });
95 mQueue.Clear();
97 if (mCurrentFrameQueueItem) {
98 mQueue.AppendElement(*mCurrentFrameQueueItem);
99 mCurrentFrameQueueItem = Nothing();
103 } // namespace wr
104 } // namespace mozilla