2018-04-12 22:31:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
#include "ScreenshotGrabber.h"
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2021-03-22 19:29:52 +03:00
|
|
|
#include "mozilla/ProfilerMarkers.h"
|
2018-04-12 22:31:12 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2018-12-02 17:02:27 +03:00
|
|
|
#include "mozilla/layers/Compositor.h"
|
2018-04-12 22:31:12 +03:00
|
|
|
#include "mozilla/layers/ProfilerScreenshots.h"
|
2018-12-02 17:02:27 +03:00
|
|
|
#include "mozilla/layers/TextureHost.h"
|
2018-04-12 22:31:12 +03:00
|
|
|
#include "mozilla/gfx/Point.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
|
|
|
namespace layers {
|
2020-08-27 06:55:33 +03:00
|
|
|
namespace profiler_screenshots {
|
2018-04-12 22:31:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The actual implementation of screenshot grabbing.
|
2020-08-27 06:55:33 +03:00
|
|
|
* The ScreenshotGrabberImpl object is destroyed if the profiler is
|
2018-04-12 22:31:12 +03:00
|
|
|
* disabled and MaybeGrabScreenshot notices it.
|
|
|
|
*/
|
2020-08-27 06:55:33 +03:00
|
|
|
class ScreenshotGrabberImpl final {
|
2018-04-12 22:31:12 +03:00
|
|
|
public:
|
2020-08-27 06:55:33 +03:00
|
|
|
explicit ScreenshotGrabberImpl(const IntSize& aBufferSize);
|
|
|
|
~ScreenshotGrabberImpl();
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-09-15 23:47:19 +03:00
|
|
|
void GrabScreenshot(Window& aWindow, const IntSize& aWindowSize);
|
2018-04-12 22:31:12 +03:00
|
|
|
void ProcessQueue();
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct QueueItem final {
|
|
|
|
mozilla::TimeStamp mTimeStamp;
|
|
|
|
RefPtr<AsyncReadbackBuffer> mScreenshotBuffer;
|
2020-08-27 06:55:33 +03:00
|
|
|
IntSize mScreenshotSize;
|
|
|
|
IntSize mWindowSize;
|
2018-04-12 22:31:12 +03:00
|
|
|
};
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
RefPtr<RenderSource> ScaleDownWindowRenderSourceToSize(
|
|
|
|
Window& aWindow, const IntSize& aDestSize,
|
|
|
|
RenderSource* aWindowRenderSource, size_t aLevel);
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
already_AddRefed<AsyncReadbackBuffer> TakeNextBuffer(Window& aWindow);
|
2018-04-12 22:31:12 +03:00
|
|
|
void ReturnBuffer(AsyncReadbackBuffer* aBuffer);
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
nsTArray<RefPtr<DownscaleTarget>> mCachedLevels;
|
2018-04-12 22:31:12 +03:00
|
|
|
nsTArray<RefPtr<AsyncReadbackBuffer>> mAvailableBuffers;
|
|
|
|
Maybe<QueueItem> mCurrentFrameQueueItem;
|
|
|
|
nsTArray<QueueItem> mQueue;
|
2019-05-25 02:53:28 +03:00
|
|
|
RefPtr<ProfilerScreenshots> mProfilerScreenshots;
|
2018-04-12 22:31:12 +03:00
|
|
|
const IntSize mBufferSize;
|
|
|
|
};
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
} // namespace profiler_screenshots
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
ScreenshotGrabber::ScreenshotGrabber() = default;
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
ScreenshotGrabber::~ScreenshotGrabber() = default;
|
|
|
|
|
|
|
|
void ScreenshotGrabber::MaybeGrabScreenshot(
|
2020-09-15 23:47:19 +03:00
|
|
|
profiler_screenshots::Window& aWindow, const IntSize& aWindowSize) {
|
2018-04-12 22:31:12 +03:00
|
|
|
if (ProfilerScreenshots::IsEnabled()) {
|
|
|
|
if (!mImpl) {
|
2020-08-27 06:55:33 +03:00
|
|
|
mImpl = MakeUnique<profiler_screenshots::ScreenshotGrabberImpl>(
|
2018-04-12 22:31:12 +03:00
|
|
|
ProfilerScreenshots::ScreenshotSize());
|
|
|
|
}
|
2020-09-15 23:47:19 +03:00
|
|
|
mImpl->GrabScreenshot(aWindow, aWindowSize);
|
2018-04-12 22:31:12 +03:00
|
|
|
} else if (mImpl) {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
void ScreenshotGrabber::MaybeProcessQueue() {
|
2018-04-12 22:31:12 +03:00
|
|
|
if (ProfilerScreenshots::IsEnabled()) {
|
|
|
|
if (!mImpl) {
|
2020-08-27 06:55:33 +03:00
|
|
|
mImpl = MakeUnique<profiler_screenshots::ScreenshotGrabberImpl>(
|
2018-04-12 22:31:12 +03:00
|
|
|
ProfilerScreenshots::ScreenshotSize());
|
|
|
|
}
|
|
|
|
mImpl->ProcessQueue();
|
|
|
|
} else if (mImpl) {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
void ScreenshotGrabber::NotifyEmptyFrame() {
|
2020-09-10 06:02:36 +03:00
|
|
|
PROFILER_MARKER_UNTYPED("NoCompositorScreenshot because nothing changed",
|
|
|
|
GRAPHICS);
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
void ScreenshotGrabber::Destroy() { mImpl = nullptr; }
|
|
|
|
|
|
|
|
namespace profiler_screenshots {
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
ScreenshotGrabberImpl::ScreenshotGrabberImpl(const IntSize& aBufferSize)
|
2018-04-12 22:31:12 +03:00
|
|
|
: mBufferSize(aBufferSize) {}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
ScreenshotGrabberImpl::~ScreenshotGrabberImpl() {
|
2018-04-12 22:31:12 +03:00
|
|
|
// Any queue items in mQueue or mCurrentFrameQueueItem will be lost.
|
|
|
|
// That's ok: Either the profiler has stopped and we don't care about these
|
|
|
|
// screenshots, or the window is closing and we don't really need the last
|
|
|
|
// few frames from the window.
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
// Scale down aWindowRenderSource into a RenderSource of size
|
|
|
|
// mBufferSize * (1 << aLevel) and return that RenderSource.
|
2018-04-12 22:31:12 +03:00
|
|
|
// Don't scale down by more than a factor of 2 with a single scaling operation,
|
|
|
|
// because it'll look bad. If higher scales are needed, use another
|
|
|
|
// intermediate target by calling this function recursively with aLevel + 1.
|
2020-08-27 06:55:33 +03:00
|
|
|
RefPtr<RenderSource> ScreenshotGrabberImpl::ScaleDownWindowRenderSourceToSize(
|
|
|
|
Window& aWindow, const IntSize& aDestSize,
|
|
|
|
RenderSource* aWindowRenderSource, size_t aLevel) {
|
|
|
|
if (aLevel == mCachedLevels.Length()) {
|
|
|
|
mCachedLevels.AppendElement(
|
|
|
|
aWindow.CreateDownscaleTarget(mBufferSize * (1 << aLevel)));
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
2020-08-27 06:55:33 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aLevel < mCachedLevels.Length());
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
RefPtr<RenderSource> renderSource = aWindowRenderSource;
|
|
|
|
IntSize sourceSize = aWindowRenderSource->Size();
|
|
|
|
if (sourceSize.width > aDestSize.width * 2) {
|
2018-04-12 22:31:12 +03:00
|
|
|
sourceSize = aDestSize * 2;
|
2020-08-27 06:55:33 +03:00
|
|
|
renderSource = ScaleDownWindowRenderSourceToSize(
|
|
|
|
aWindow, sourceSize, aWindowRenderSource, aLevel + 1);
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
if (renderSource) {
|
|
|
|
if (mCachedLevels[aLevel]->DownscaleFrom(
|
|
|
|
renderSource, IntRect({}, sourceSize), IntRect({}, aDestSize))) {
|
|
|
|
return mCachedLevels[aLevel]->AsRenderSource();
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-09-15 23:47:19 +03:00
|
|
|
void ScreenshotGrabberImpl::GrabScreenshot(Window& aWindow,
|
|
|
|
const IntSize& aWindowSize) {
|
|
|
|
RefPtr<RenderSource> windowRenderSource =
|
|
|
|
aWindow.GetWindowContents(aWindowSize);
|
2018-04-12 22:31:12 +03:00
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
if (!windowRenderSource) {
|
2020-09-10 06:02:36 +03:00
|
|
|
PROFILER_MARKER_UNTYPED(
|
2018-04-12 22:31:12 +03:00
|
|
|
"NoCompositorScreenshot because of unsupported compositor "
|
2019-01-18 18:40:15 +03:00
|
|
|
"configuration",
|
|
|
|
GRAPHICS);
|
2018-04-12 22:31:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-15 23:47:19 +03:00
|
|
|
Size windowSize(aWindowSize);
|
2020-09-15 03:09:08 +03:00
|
|
|
float scale = std::min(mBufferSize.width / windowSize.width,
|
|
|
|
mBufferSize.height / windowSize.height);
|
|
|
|
IntSize scaledSize = IntSize::Round(windowSize * scale);
|
2020-08-27 06:55:33 +03:00
|
|
|
RefPtr<RenderSource> scaledTarget = ScaleDownWindowRenderSourceToSize(
|
|
|
|
aWindow, scaledSize, windowRenderSource, 0);
|
2018-04-12 22:31:12 +03:00
|
|
|
|
|
|
|
if (!scaledTarget) {
|
2020-09-10 06:02:36 +03:00
|
|
|
PROFILER_MARKER_UNTYPED(
|
2020-08-27 06:55:33 +03:00
|
|
|
"NoCompositorScreenshot because ScaleDownWindowRenderSourceToSize "
|
|
|
|
"failed",
|
2019-01-18 18:40:15 +03:00
|
|
|
GRAPHICS);
|
2018-04-12 22:31:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
RefPtr<AsyncReadbackBuffer> buffer = TakeNextBuffer(aWindow);
|
2018-04-12 22:31:12 +03:00
|
|
|
if (!buffer) {
|
2020-09-10 06:02:36 +03:00
|
|
|
PROFILER_MARKER_UNTYPED(
|
2019-01-18 18:40:15 +03:00
|
|
|
"NoCompositorScreenshot because AsyncReadbackBuffer creation failed",
|
|
|
|
GRAPHICS);
|
2018-04-12 22:31:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
buffer->CopyFrom(scaledTarget);
|
2018-04-12 22:31:12 +03:00
|
|
|
|
|
|
|
// This QueueItem will be added to the queue at the end of the next call to
|
|
|
|
// ProcessQueue(). This ensures that the buffer isn't mapped into main memory
|
|
|
|
// until the next frame. If we did it in this frame, we'd block on the GPU.
|
2020-08-27 06:55:33 +03:00
|
|
|
mCurrentFrameQueueItem =
|
|
|
|
Some(QueueItem{TimeStamp::Now(), std::move(buffer), scaledSize,
|
2021-07-28 20:21:09 +03:00
|
|
|
windowRenderSource->Size()});
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
already_AddRefed<AsyncReadbackBuffer> ScreenshotGrabberImpl::TakeNextBuffer(
|
|
|
|
Window& aWindow) {
|
2018-04-12 22:31:12 +03:00
|
|
|
if (!mAvailableBuffers.IsEmpty()) {
|
|
|
|
RefPtr<AsyncReadbackBuffer> buffer = mAvailableBuffers[0];
|
|
|
|
mAvailableBuffers.RemoveElementAt(0);
|
|
|
|
return buffer.forget();
|
|
|
|
}
|
2020-08-27 06:55:33 +03:00
|
|
|
return aWindow.CreateAsyncReadbackBuffer(mBufferSize);
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
void ScreenshotGrabberImpl::ReturnBuffer(AsyncReadbackBuffer* aBuffer) {
|
2018-04-12 22:31:12 +03:00
|
|
|
mAvailableBuffers.AppendElement(aBuffer);
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
void ScreenshotGrabberImpl::ProcessQueue() {
|
2018-04-12 22:31:12 +03:00
|
|
|
if (!mQueue.IsEmpty()) {
|
|
|
|
if (!mProfilerScreenshots) {
|
2019-05-25 02:53:28 +03:00
|
|
|
mProfilerScreenshots = new ProfilerScreenshots();
|
2018-04-12 22:31:12 +03:00
|
|
|
}
|
|
|
|
for (const auto& item : mQueue) {
|
|
|
|
mProfilerScreenshots->SubmitScreenshot(
|
2021-07-28 20:21:09 +03:00
|
|
|
item.mWindowSize, item.mScreenshotSize, item.mTimeStamp,
|
|
|
|
[&item](DataSourceSurface* aTargetSurface) {
|
2018-04-12 22:31:12 +03:00
|
|
|
return item.mScreenshotBuffer->MapAndCopyInto(aTargetSurface,
|
|
|
|
item.mScreenshotSize);
|
|
|
|
});
|
|
|
|
ReturnBuffer(item.mScreenshotBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mQueue.Clear();
|
|
|
|
|
|
|
|
if (mCurrentFrameQueueItem) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mQueue.AppendElement(std::move(*mCurrentFrameQueueItem));
|
2018-04-12 22:31:12 +03:00
|
|
|
mCurrentFrameQueueItem = Nothing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:55:33 +03:00
|
|
|
} // namespace profiler_screenshots
|
2018-04-12 22:31:12 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|