2018-09-25 16:13:51 +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 "ImageMemoryReporter.h"
|
|
|
|
#include "Image.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "base/process_util.h"
|
2018-09-25 16:13:51 +03:00
|
|
|
#include "mozilla/layers/SharedSurfacesParent.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
2018-09-25 16:13:51 +03:00
|
|
|
#include "nsIMemoryReporter.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
ImageMemoryReporter::WebRenderReporter* ImageMemoryReporter::sWrReporter;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-25 16:13:51 +03:00
|
|
|
class ImageMemoryReporter::WebRenderReporter final : public nsIMemoryReporter {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
WebRenderReporter() {}
|
|
|
|
|
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize) override {
|
|
|
|
layers::SharedSurfacesMemoryReport report;
|
|
|
|
layers::SharedSurfacesParent::AccumulateMemoryReport(report);
|
|
|
|
ReportSharedSurfaces(aHandleReport, aData, /* aIsForCompositor */ true,
|
|
|
|
report);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~WebRenderReporter() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ImageMemoryReporter::WebRenderReporter, nsIMemoryReporter)
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::InitForWebRender() {
|
2018-09-25 16:13:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() || XRE_IsGPUProcess());
|
|
|
|
if (!sWrReporter) {
|
|
|
|
sWrReporter = new WebRenderReporter();
|
|
|
|
RegisterStrongMemoryReporter(sWrReporter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::ShutdownForWebRender() {
|
2018-09-25 16:13:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() || XRE_IsGPUProcess());
|
|
|
|
if (sWrReporter) {
|
|
|
|
UnregisterStrongMemoryReporter(sWrReporter);
|
|
|
|
sWrReporter = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::ReportSharedSurfaces(
|
2018-09-25 16:13:51 +03:00
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
|
|
|
const layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
|
|
|
ReportSharedSurfaces(aHandleReport, aData,
|
|
|
|
/* aIsForCompositor */ false, aSharedSurfaces);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::ReportSharedSurfaces(
|
2018-09-25 16:13:51 +03:00
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
|
|
|
bool aIsForCompositor,
|
|
|
|
const layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
|
|
|
MOZ_ASSERT_IF(aIsForCompositor, XRE_IsParentProcess() || XRE_IsGPUProcess());
|
|
|
|
MOZ_ASSERT_IF(!aIsForCompositor,
|
|
|
|
XRE_IsParentProcess() || XRE_IsContentProcess());
|
|
|
|
|
|
|
|
for (auto i = aSharedSurfaces.mSurfaces.begin();
|
|
|
|
i != aSharedSurfaces.mSurfaces.end(); ++i) {
|
|
|
|
ReportSharedSurface(aHandleReport, aData, aIsForCompositor, i->first,
|
|
|
|
i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::ReportSharedSurface(
|
2018-09-25 16:13:51 +03:00
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
|
|
|
bool aIsForCompositor, uint64_t aExternalId,
|
|
|
|
const layers::SharedSurfacesMemoryReport::SurfaceEntry& aEntry) {
|
|
|
|
nsAutoCString path;
|
|
|
|
if (aIsForCompositor) {
|
|
|
|
path.AppendLiteral("gfx/webrender/images/mapped_from_owner/");
|
|
|
|
} else {
|
|
|
|
path.AppendLiteral("gfx/webrender/images/owner_cache_missing/");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIsForCompositor) {
|
|
|
|
path.AppendLiteral("pid=");
|
|
|
|
path.AppendInt(uint32_t(aEntry.mCreatorPid));
|
|
|
|
path.AppendLiteral("/");
|
|
|
|
}
|
|
|
|
|
2019-06-26 06:24:21 +03:00
|
|
|
if (StaticPrefs::image_mem_debug_reporting()) {
|
2018-09-25 16:13:51 +03:00
|
|
|
path.AppendInt(aExternalId, 16);
|
|
|
|
path.AppendLiteral("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
path.AppendLiteral("image(");
|
|
|
|
path.AppendInt(aEntry.mSize.width);
|
|
|
|
path.AppendLiteral("x");
|
|
|
|
path.AppendInt(aEntry.mSize.height);
|
|
|
|
path.AppendLiteral(", compositor_ref:");
|
|
|
|
path.AppendInt(aEntry.mConsumers);
|
|
|
|
path.AppendLiteral(", creator_ref:");
|
|
|
|
path.AppendInt(aEntry.mCreatorRef);
|
2020-07-17 03:02:13 +03:00
|
|
|
path.AppendLiteral(")/decoded-");
|
2018-09-25 16:13:51 +03:00
|
|
|
|
|
|
|
size_t surfaceSize = mozilla::ipc::SharedMemory::PageAlignedSize(
|
|
|
|
aEntry.mSize.height * aEntry.mStride);
|
|
|
|
|
|
|
|
// If this memory has already been reported elsewhere (e.g. as part of our
|
|
|
|
// explicit section in the surface cache), we don't want report it again as
|
2020-07-17 03:02:13 +03:00
|
|
|
// KIND_NONHEAP and have it counted again. The paths must be different if the
|
|
|
|
// kinds are different to avoid problems when diffing memory reports.
|
2018-09-25 16:13:51 +03:00
|
|
|
bool sameProcess = aEntry.mCreatorPid == base::GetCurrentProcId();
|
2020-07-17 03:02:13 +03:00
|
|
|
int32_t kind;
|
|
|
|
if (aIsForCompositor && !sameProcess) {
|
|
|
|
path.AppendLiteral("nonheap");
|
|
|
|
kind = nsIMemoryReporter::KIND_NONHEAP;
|
|
|
|
} else {
|
|
|
|
path.AppendLiteral("other");
|
|
|
|
kind = nsIMemoryReporter::KIND_OTHER;
|
|
|
|
}
|
2018-09-25 16:13:51 +03:00
|
|
|
|
|
|
|
constexpr auto desc = "Decoded image data stored in shared memory."_ns;
|
2020-09-23 18:17:15 +03:00
|
|
|
aHandleReport->Callback(""_ns, path, kind, nsIMemoryReporter::UNITS_BYTES,
|
|
|
|
surfaceSize, desc, aData);
|
2018-09-25 16:13:51 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::AppendSharedSurfacePrefix(
|
2018-09-25 16:13:51 +03:00
|
|
|
nsACString& aPathPrefix, const SurfaceMemoryCounter& aCounter,
|
|
|
|
layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
|
|
|
uint64_t extId = aCounter.Values().ExternalId();
|
|
|
|
if (extId) {
|
|
|
|
auto gpuEntry = aSharedSurfaces.mSurfaces.find(extId);
|
|
|
|
|
2019-06-26 06:24:21 +03:00
|
|
|
if (StaticPrefs::image_mem_debug_reporting()) {
|
2018-09-25 16:13:51 +03:00
|
|
|
aPathPrefix.AppendLiteral(", external_id:");
|
|
|
|
aPathPrefix.AppendInt(extId, 16);
|
|
|
|
if (gpuEntry != aSharedSurfaces.mSurfaces.end()) {
|
|
|
|
aPathPrefix.AppendLiteral(", compositor_ref:");
|
|
|
|
aPathPrefix.AppendInt(gpuEntry->second.mConsumers);
|
|
|
|
} else {
|
|
|
|
aPathPrefix.AppendLiteral(", compositor_ref:missing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gpuEntry != aSharedSurfaces.mSurfaces.end()) {
|
|
|
|
MOZ_ASSERT(gpuEntry->second.mCreatorRef);
|
|
|
|
aSharedSurfaces.mSurfaces.erase(gpuEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void ImageMemoryReporter::TrimSharedSurfaces(
|
2018-09-25 16:13:51 +03:00
|
|
|
const ImageMemoryCounter& aCounter,
|
|
|
|
layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
|
|
|
if (aSharedSurfaces.mSurfaces.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
|
|
|
|
uint64_t extId = counter.Values().ExternalId();
|
|
|
|
if (extId) {
|
|
|
|
auto gpuEntry = aSharedSurfaces.mSurfaces.find(extId);
|
|
|
|
if (gpuEntry != aSharedSurfaces.mSurfaces.end()) {
|
|
|
|
MOZ_ASSERT(gpuEntry->second.mCreatorRef);
|
|
|
|
aSharedSurfaces.mSurfaces.erase(gpuEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|