1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGLMemoryTracker.h"
8 #include "HostWebGLContext.h"
9 #include "WebGLBuffer.h"
10 #include "WebGLRenderbuffer.h"
11 #include "WebGLShader.h"
12 #include "WebGLTexture.h"
16 MOZ_DEFINE_MALLOC_SIZE_OF(WebGLShaderMallocSizeOf
)
18 void WebGLMemoryTracker::EnsureRegistered() {
19 static bool sIsRegistered
= []() {
20 RegisterStrongMemoryReporter(new WebGLMemoryTracker
);
27 WebGLMemoryTracker::CollectReports(nsIHandleReportCallback
* aHandleReport
,
28 nsISupports
* aData
, bool) {
29 const auto locked
= HostWebGLContext::OutstandingContexts();
30 const auto& contexts
= locked
->contexts
;
32 const auto contextCount
= contexts
.size();
34 size_t bufferCount
= 0;
35 int64_t bufferGpuSize
= 0;
36 int64_t bufferCacheSize
= 0;
39 int64_t rbGpuSize
= 0;
41 size_t shaderCount
= 0;
42 int64_t shaderCpuSize
= 0;
45 int64_t texGpuSize
= 0;
47 for (const auto& context
: contexts
) {
48 bufferCount
+= context
->mBufferMap
.size();
49 for (const auto& pair
: context
->mBufferMap
) {
50 const auto& buffer
= *pair
.second
;
51 bufferGpuSize
+= buffer
.mByteLength
;
53 if (buffer
.mIndexCache
) {
54 bufferCacheSize
+= buffer
.mByteLength
;
56 bufferCacheSize
+= buffer
.mIndexRanges
.size() *
57 sizeof(decltype(buffer
.mIndexRanges
)::value_type
);
62 rbCount
+= context
->mRenderbufferMap
.size();
63 for (const auto& pair
: context
->mRenderbufferMap
) {
64 const auto& rb
= pair
.second
;
65 rbGpuSize
+= rb
->MemoryUsage();
70 shaderCount
+= context
->mShaderMap
.size();
71 for (const auto& pair
: context
->mShaderMap
) {
72 const auto& shader
= pair
.second
;
73 shaderCpuSize
+= shader
->SizeOfIncludingThis(WebGLShaderMallocSizeOf
);
78 texCount
+= context
->mTextureMap
.size();
79 for (const auto& pair
: context
->mTextureMap
) {
80 const auto& texture
= pair
.second
;
81 texGpuSize
+= texture
->MemoryUsage();
88 "webgl-texture-memory", KIND_OTHER
, UNITS_BYTES
, texGpuSize
,
89 "Memory used by WebGL textures. The OpenGL implementation is free to "
90 "store these textures in either video memory or main memory. This "
91 "measurement is only a lower bound, actual memory usage may be higher "
92 "for example if the storage is strided.");
94 MOZ_COLLECT_REPORT("webgl-texture-count", KIND_OTHER
, UNITS_COUNT
,
95 static_cast<int64_t>(texCount
),
96 "Number of WebGL textures.");
99 "webgl-buffer-memory", KIND_OTHER
, UNITS_BYTES
, bufferGpuSize
,
100 "Memory used by WebGL buffers. The OpenGL implementation is free to "
101 "store these buffers in either video memory or main memory. This "
102 "measurement is only a lower bound, actual memory usage may be higher "
103 "for example if the storage is strided.");
106 "explicit/webgl/buffer-cache-memory", KIND_HEAP
, UNITS_BYTES
,
108 "Memory used by WebGL buffer caches. The WebGL implementation caches "
109 "the contents of element array buffers only. This adds up with the "
110 "'webgl-buffer-memory' value, but contrary to it, this one represents "
111 "bytes on the heap, not managed by OpenGL.");
113 MOZ_COLLECT_REPORT("webgl-buffer-count", KIND_OTHER
, UNITS_COUNT
,
114 static_cast<int64_t>(bufferCount
),
115 "Number of WebGL buffers.");
118 "webgl-renderbuffer-memory", KIND_OTHER
, UNITS_BYTES
, rbGpuSize
,
119 "Memory used by WebGL renderbuffers. The OpenGL implementation is free "
120 "to store these renderbuffers in either video memory or main memory. "
121 "This measurement is only a lower bound, actual memory usage may be "
122 "higher, for example if the storage is strided.");
124 MOZ_COLLECT_REPORT("webgl-renderbuffer-count", KIND_OTHER
, UNITS_COUNT
,
125 static_cast<int64_t>(rbCount
),
126 "Number of WebGL renderbuffers.");
129 "explicit/webgl/shader", KIND_HEAP
, UNITS_BYTES
, shaderCpuSize
,
130 "Combined size of WebGL shader ASCII sources and translation logs "
131 "cached on the heap.");
133 MOZ_COLLECT_REPORT("webgl-shader-count", KIND_OTHER
, UNITS_COUNT
,
134 static_cast<int64_t>(shaderCount
),
135 "Number of WebGL shaders.");
137 MOZ_COLLECT_REPORT("webgl-context-count", KIND_OTHER
, UNITS_COUNT
,
138 static_cast<int64_t>(contextCount
),
139 "Number of WebGL contexts.");
144 NS_IMPL_ISUPPORTS(WebGLMemoryTracker
, nsIMemoryReporter
)
146 } // namespace mozilla