2018-03-27 06:08:33 +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/. */
|
|
|
|
|
|
|
|
#include "mozilla/layers/ProfilerScreenshots.h"
|
|
|
|
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#include "gfxUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2018-12-02 17:02:27 +03:00
|
|
|
using namespace mozilla::gfx;
|
2018-03-27 06:08:33 +03:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
2021-11-08 01:43:12 +03:00
|
|
|
struct ScreenshotMarker {
|
|
|
|
static constexpr mozilla::Span<const char> MarkerTypeName() {
|
|
|
|
return mozilla::MakeStringSpan("CompositorScreenshot");
|
|
|
|
}
|
|
|
|
static void StreamJSONMarkerData(
|
|
|
|
mozilla::baseprofiler::SpliceableJSONWriter& aWriter,
|
|
|
|
const mozilla::ProfilerString8View& aScreenshotDataURL,
|
|
|
|
const mozilla::gfx::IntSize& aWindowSize, uint32_t aWindowIdentifier) {
|
|
|
|
if (aScreenshotDataURL.Length() != 0) {
|
|
|
|
aWriter.UniqueStringProperty("url", aScreenshotDataURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
aWriter.IntProperty("windowID", aWindowIdentifier);
|
|
|
|
|
|
|
|
if (!aWindowSize.IsEmpty()) {
|
|
|
|
aWriter.DoubleProperty("windowWidth", aWindowSize.width);
|
|
|
|
aWriter.DoubleProperty("windowHeight", aWindowSize.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static mozilla::MarkerSchema MarkerTypeDisplay() {
|
|
|
|
return mozilla::MarkerSchema::SpecialFrontendLocation{};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-28 20:21:09 +03:00
|
|
|
uint32_t ProfilerScreenshots::sWindowCounter = 0;
|
2018-03-27 06:08:33 +03:00
|
|
|
|
2021-07-28 20:21:09 +03:00
|
|
|
ProfilerScreenshots::ProfilerScreenshots()
|
|
|
|
: mMutex("ProfilerScreenshots::mMutex"),
|
|
|
|
mLiveSurfaceCount(0),
|
|
|
|
mWindowIdentifier(++sWindowCounter) {}
|
|
|
|
|
|
|
|
ProfilerScreenshots::~ProfilerScreenshots() {
|
|
|
|
if (mWindowIdentifier) {
|
|
|
|
profiler_add_marker("CompositorScreenshotWindowDestroyed",
|
2021-11-24 13:47:59 +03:00
|
|
|
geckoprofiler::category::GRAPHICS,
|
|
|
|
MarkerThreadId::MainThread(), ScreenshotMarker{},
|
|
|
|
/* aScreenshotDataURL */ "", mozilla::gfx::IntSize{},
|
|
|
|
mWindowIdentifier);
|
2021-07-28 20:21:09 +03:00
|
|
|
}
|
|
|
|
}
|
2018-03-27 06:08:33 +03:00
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
bool ProfilerScreenshots::IsEnabled() {
|
2018-03-27 06:08:33 +03:00
|
|
|
return profiler_feature_active(ProfilerFeature::Screenshots);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfilerScreenshots::SubmitScreenshot(
|
2021-07-28 20:21:09 +03:00
|
|
|
const gfx::IntSize& aOriginalSize, const IntSize& aScaledSize,
|
|
|
|
const TimeStamp& aTimeStamp,
|
2018-03-27 06:08:33 +03:00
|
|
|
const std::function<bool(DataSourceSurface*)>& aPopulateSurface) {
|
|
|
|
RefPtr<DataSourceSurface> backingSurface = TakeNextSurface();
|
|
|
|
if (!backingSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(aScaledSize <= backingSurface->GetSize());
|
|
|
|
|
|
|
|
bool succeeded = aPopulateSurface(backingSurface);
|
|
|
|
|
|
|
|
if (!succeeded) {
|
2020-09-10 06:02:36 +03:00
|
|
|
PROFILER_MARKER_UNTYPED(
|
2019-01-18 18:40:15 +03:00
|
|
|
"NoCompositorScreenshot because aPopulateSurface callback failed",
|
|
|
|
GRAPHICS);
|
2018-03-27 06:08:33 +03:00
|
|
|
ReturnSurface(backingSurface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-06 00:19:18 +03:00
|
|
|
NS_DispatchBackgroundTask(NS_NewRunnableFunction(
|
2018-03-27 06:08:33 +03:00
|
|
|
"ProfilerScreenshots::SubmitScreenshot",
|
2021-11-08 01:43:12 +03:00
|
|
|
[self = RefPtr<ProfilerScreenshots>{this},
|
|
|
|
backingSurface = std::move(backingSurface),
|
|
|
|
windowIdentifier = mWindowIdentifier, originalSize = aOriginalSize,
|
|
|
|
scaledSize = aScaledSize, timeStamp = aTimeStamp]() {
|
2018-03-27 06:08:33 +03:00
|
|
|
// Create a new surface that wraps backingSurface's data but has the
|
|
|
|
// correct size.
|
2021-11-24 13:47:59 +03:00
|
|
|
DataSourceSurface::ScopedMap scopedMap(backingSurface,
|
|
|
|
DataSourceSurface::READ);
|
|
|
|
RefPtr<DataSourceSurface> surf =
|
|
|
|
Factory::CreateWrappingDataSourceSurface(
|
|
|
|
scopedMap.GetData(), scopedMap.GetStride(), scaledSize,
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
|
|
|
|
|
|
|
// Encode surf to a JPEG data URL.
|
|
|
|
nsCString dataURL;
|
|
|
|
nsresult rv = gfxUtils::EncodeSourceSurface(
|
|
|
|
surf, ImageType::JPEG, u"quality=85"_ns, gfxUtils::eDataURIEncode,
|
|
|
|
nullptr, &dataURL);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Add a marker with the data URL.
|
|
|
|
profiler_add_marker(
|
|
|
|
"CompositorScreenshot", geckoprofiler::category::GRAPHICS,
|
|
|
|
{MarkerThreadId::MainThread(),
|
|
|
|
MarkerTiming::InstantAt(timeStamp)},
|
|
|
|
ScreenshotMarker{}, dataURL, originalSize, windowIdentifier);
|
2018-03-27 06:08:33 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-27 06:08:33 +03:00
|
|
|
// Return backingSurface back to the surface pool.
|
2019-05-25 02:53:28 +03:00
|
|
|
self->ReturnSurface(backingSurface);
|
2018-03-27 06:08:33 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DataSourceSurface> ProfilerScreenshots::TakeNextSurface() {
|
|
|
|
MutexAutoLock mon(mMutex);
|
|
|
|
if (!mAvailableSurfaces.IsEmpty()) {
|
|
|
|
RefPtr<DataSourceSurface> surf = mAvailableSurfaces[0];
|
|
|
|
mAvailableSurfaces.RemoveElementAt(0);
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
if (mLiveSurfaceCount >= 8) {
|
|
|
|
NS_WARNING(
|
|
|
|
"already 8 surfaces in flight, skipping capture for this composite");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mLiveSurfaceCount++;
|
|
|
|
return Factory::CreateDataSourceSurface(ScreenshotSize(),
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfilerScreenshots::ReturnSurface(DataSourceSurface* aSurface) {
|
|
|
|
MutexAutoLock mon(this->mMutex);
|
|
|
|
mAvailableSurfaces.AppendElement(aSurface);
|
|
|
|
}
|