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
;
18 RendererScreenshotGrabber::RendererScreenshotGrabber() {
19 mMaxScreenshotSize
= ProfilerScreenshots::ScreenshotSize();
22 void RendererScreenshotGrabber::MaybeGrabScreenshot(
23 RendererOGL
* aRendererOGL
, const gfx::IntSize
& aWindowSize
) {
25 ProfilerScreenshots::IsEnabled() && aRendererOGL
->EnsureAsyncScreenshot();
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
) {
40 ProfilerScreenshots::IsEnabled() && aRendererOGL
->EnsureAsyncScreenshot();
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
) {
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
{
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
);
97 if (mCurrentFrameQueueItem
) {
98 mQueue
.AppendElement(*mCurrentFrameQueueItem
);
99 mCurrentFrameQueueItem
= Nothing();
104 } // namespace mozilla