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/. */
5 
6 #include "WebGLMemoryTracker.h"
7 
8 #include "HostWebGLContext.h"
9 #include "WebGLBuffer.h"
10 #include "WebGLRenderbuffer.h"
11 #include "WebGLShader.h"
12 #include "WebGLTexture.h"
13 
14 namespace mozilla {
15 
MOZ_DEFINE_MALLOC_SIZE_OF(WebGLShaderMallocSizeOf)16 MOZ_DEFINE_MALLOC_SIZE_OF(WebGLShaderMallocSizeOf)
17 
18 NS_IMETHODIMP
19 WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
20                                    nsISupports* aData, bool) {
21   const auto locked = HostWebGLContext::OutstandingContexts();
22   const auto& contexts = locked->contexts;
23 
24   const auto contextCount = contexts.size();
25 
26   size_t bufferCount = 0;
27   int64_t bufferGpuSize = 0;
28   int64_t bufferCacheSize = 0;
29 
30   size_t rbCount = 0;
31   int64_t rbGpuSize = 0;
32 
33   size_t shaderCount = 0;
34   int64_t shaderCpuSize = 0;
35 
36   size_t texCount = 0;
37   int64_t texGpuSize = 0;
38 
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;
44 
45       if (buffer.mIndexCache) {
46         bufferCacheSize += buffer.mByteLength;
47       }
48       bufferCacheSize += buffer.mIndexRanges.size() *
49                          sizeof(decltype(buffer.mIndexRanges)::value_type);
50     }
51 
52     // -
53 
54     rbCount += context->mRenderbufferMap.size();
55     for (const auto& pair : context->mRenderbufferMap) {
56       const auto& rb = pair.second;
57       rbGpuSize += rb->MemoryUsage();
58     }
59 
60     // -
61 
62     shaderCount += context->mShaderMap.size();
63     for (const auto& pair : context->mShaderMap) {
64       const auto& shader = pair.second;
65       shaderCpuSize += shader->SizeOfIncludingThis(WebGLShaderMallocSizeOf);
66     }
67 
68     // -
69 
70     texCount += context->mTextureMap.size();
71     for (const auto& pair : context->mTextureMap) {
72       const auto& texture = pair.second;
73       texGpuSize += texture->MemoryUsage();
74     }
75   }
76 
77   // -
78 
79   MOZ_COLLECT_REPORT(
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.");
85 
86   MOZ_COLLECT_REPORT("webgl-texture-count", KIND_OTHER, UNITS_COUNT,
87                      static_cast<int64_t>(texCount),
88                      "Number of WebGL textures.");
89 
90   MOZ_COLLECT_REPORT(
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.");
96 
97   MOZ_COLLECT_REPORT(
98       "explicit/webgl/buffer-cache-memory", KIND_HEAP, UNITS_BYTES,
99       bufferCacheSize,
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.");
104 
105   MOZ_COLLECT_REPORT("webgl-buffer-count", KIND_OTHER, UNITS_COUNT,
106                      static_cast<int64_t>(bufferCount),
107                      "Number of WebGL buffers.");
108 
109   MOZ_COLLECT_REPORT(
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.");
115 
116   MOZ_COLLECT_REPORT("webgl-renderbuffer-count", KIND_OTHER, UNITS_COUNT,
117                      static_cast<int64_t>(rbCount),
118                      "Number of WebGL renderbuffers.");
119 
120   MOZ_COLLECT_REPORT(
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.");
124 
125   MOZ_COLLECT_REPORT("webgl-shader-count", KIND_OTHER, UNITS_COUNT,
126                      static_cast<int64_t>(shaderCount),
127                      "Number of WebGL shaders.");
128 
129   MOZ_COLLECT_REPORT("webgl-context-count", KIND_OTHER, UNITS_COUNT,
130                      static_cast<int64_t>(contextCount),
131                      "Number of WebGL contexts.");
132 
133   return NS_OK;
134 }
135 
136 NS_IMPL_ISUPPORTS(WebGLMemoryTracker, nsIMemoryReporter)
137 
138 }  // namespace mozilla
139