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
)
19 WebGLMemoryTracker::CollectReports(nsIHandleReportCallback
* aHandleReport
,
20 nsISupports
* aData
, bool) {
21 const auto locked
= HostWebGLContext::OutstandingContexts();
22 const auto& contexts
= locked
->contexts
;
24 const auto contextCount
= contexts
.size();
26 size_t bufferCount
= 0;
27 int64_t bufferGpuSize
= 0;
28 int64_t bufferCacheSize
= 0;
31 int64_t rbGpuSize
= 0;
33 size_t shaderCount
= 0;
34 int64_t shaderCpuSize
= 0;
37 int64_t texGpuSize
= 0;
39 for (const auto& context
: contexts
) {
40 bufferCount
+= context
->mBufferMap
.size();
41 for (const auto& pair
: context
->mBufferMap
) {
42 const auto& buffer
= *pair
.second
;
43 bufferGpuSize
+= buffer
.mByteLength
;
45 if (buffer
.mIndexCache
) {
46 bufferCacheSize
+= buffer
.mByteLength
;
48 bufferCacheSize
+= buffer
.mIndexRanges
.size() *
49 sizeof(decltype(buffer
.mIndexRanges
)::value_type
);
54 rbCount
+= context
->mRenderbufferMap
.size();
55 for (const auto& pair
: context
->mRenderbufferMap
) {
56 const auto& rb
= pair
.second
;
57 rbGpuSize
+= rb
->MemoryUsage();
62 shaderCount
+= context
->mShaderMap
.size();
63 for (const auto& pair
: context
->mShaderMap
) {
64 const auto& shader
= pair
.second
;
65 shaderCpuSize
+= shader
->SizeOfIncludingThis(WebGLShaderMallocSizeOf
);
70 texCount
+= context
->mTextureMap
.size();
71 for (const auto& pair
: context
->mTextureMap
) {
72 const auto& texture
= pair
.second
;
73 texGpuSize
+= texture
->MemoryUsage();
80 "webgl-texture-memory", KIND_OTHER
, UNITS_BYTES
, texGpuSize
,
81 "Memory used by WebGL textures. The OpenGL implementation is free to "
82 "store these textures in either video memory or main memory. This "
83 "measurement is only a lower bound, actual memory usage may be higher "
84 "for example if the storage is strided.");
86 MOZ_COLLECT_REPORT("webgl-texture-count", KIND_OTHER
, UNITS_COUNT
,
87 static_cast<int64_t>(texCount
),
88 "Number of WebGL textures.");
91 "webgl-buffer-memory", KIND_OTHER
, UNITS_BYTES
, bufferGpuSize
,
92 "Memory used by WebGL buffers. The OpenGL implementation is free to "
93 "store these buffers in either video memory or main memory. This "
94 "measurement is only a lower bound, actual memory usage may be higher "
95 "for example if the storage is strided.");
98 "explicit/webgl/buffer-cache-memory", KIND_HEAP
, UNITS_BYTES
,
100 "Memory used by WebGL buffer caches. The WebGL implementation caches "
101 "the contents of element array buffers only. This adds up with the "
102 "'webgl-buffer-memory' value, but contrary to it, this one represents "
103 "bytes on the heap, not managed by OpenGL.");
105 MOZ_COLLECT_REPORT("webgl-buffer-count", KIND_OTHER
, UNITS_COUNT
,
106 static_cast<int64_t>(bufferCount
),
107 "Number of WebGL buffers.");
110 "webgl-renderbuffer-memory", KIND_OTHER
, UNITS_BYTES
, rbGpuSize
,
111 "Memory used by WebGL renderbuffers. The OpenGL implementation is free "
112 "to store these renderbuffers in either video memory or main memory. "
113 "This measurement is only a lower bound, actual memory usage may be "
114 "higher, for example if the storage is strided.");
116 MOZ_COLLECT_REPORT("webgl-renderbuffer-count", KIND_OTHER
, UNITS_COUNT
,
117 static_cast<int64_t>(rbCount
),
118 "Number of WebGL renderbuffers.");
121 "explicit/webgl/shader", KIND_HEAP
, UNITS_BYTES
, shaderCpuSize
,
122 "Combined size of WebGL shader ASCII sources and translation logs "
123 "cached on the heap.");
125 MOZ_COLLECT_REPORT("webgl-shader-count", KIND_OTHER
, UNITS_COUNT
,
126 static_cast<int64_t>(shaderCount
),
127 "Number of WebGL shaders.");
129 MOZ_COLLECT_REPORT("webgl-context-count", KIND_OTHER
, UNITS_COUNT
,
130 static_cast<int64_t>(contextCount
),
131 "Number of WebGL contexts.");
136 NS_IMPL_ISUPPORTS(WebGLMemoryTracker
, nsIMemoryReporter
)
138 } // namespace mozilla