Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "WebGLBuffer.h"
9 : #include "WebGLContext.h"
10 : #include "WebGLVertexAttribData.h"
11 : #include "WebGLProgram.h"
12 : #include "WebGLRenderbuffer.h"
13 : #include "WebGLShader.h"
14 : #include "WebGLTexture.h"
15 : #include "WebGLUniformLocation.h"
16 :
17 : namespace mozilla {
18 :
19 : NS_IMETHODIMP
20 0 : WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
21 : nsISupports* aData, bool)
22 : {
23 0 : MOZ_COLLECT_REPORT(
24 : "webgl-texture-memory", KIND_OTHER, UNITS_BYTES, GetTextureMemoryUsed(),
25 : "Memory used by WebGL textures. The OpenGL implementation is free to "
26 : "store these textures in either video memory or main memory. This "
27 : "measurement is only a lower bound, actual memory usage may be higher "
28 0 : "for example if the storage is strided.");
29 :
30 0 : MOZ_COLLECT_REPORT(
31 : "webgl-texture-count", KIND_OTHER, UNITS_COUNT, GetTextureCount(),
32 0 : "Number of WebGL textures.");
33 :
34 0 : MOZ_COLLECT_REPORT(
35 : "webgl-buffer-memory", KIND_OTHER, UNITS_BYTES, GetBufferMemoryUsed(),
36 : "Memory used by WebGL buffers. The OpenGL implementation is free to "
37 : "store these buffers in either video memory or main memory. This "
38 : "measurement is only a lower bound, actual memory usage may be higher "
39 0 : "for example if the storage is strided.");
40 :
41 0 : MOZ_COLLECT_REPORT(
42 : "explicit/webgl/buffer-cache-memory", KIND_HEAP, UNITS_BYTES,
43 : GetBufferCacheMemoryUsed(),
44 : "Memory used by WebGL buffer caches. The WebGL implementation caches "
45 : "the contents of element array buffers only. This adds up with the "
46 : "'webgl-buffer-memory' value, but contrary to it, this one represents "
47 0 : "bytes on the heap, not managed by OpenGL.");
48 :
49 0 : MOZ_COLLECT_REPORT(
50 : "webgl-buffer-count", KIND_OTHER, UNITS_COUNT, GetBufferCount(),
51 0 : "Number of WebGL buffers.");
52 :
53 0 : MOZ_COLLECT_REPORT(
54 : "webgl-renderbuffer-memory", KIND_OTHER, UNITS_BYTES,
55 : GetRenderbufferMemoryUsed(),
56 : "Memory used by WebGL renderbuffers. The OpenGL implementation is free "
57 : "to store these renderbuffers in either video memory or main memory. "
58 : "This measurement is only a lower bound, actual memory usage may be "
59 0 : "higher, for example if the storage is strided.");
60 :
61 0 : MOZ_COLLECT_REPORT(
62 : "webgl-renderbuffer-count", KIND_OTHER, UNITS_COUNT,
63 : GetRenderbufferCount(),
64 0 : "Number of WebGL renderbuffers.");
65 :
66 0 : MOZ_COLLECT_REPORT(
67 : "explicit/webgl/shader", KIND_HEAP, UNITS_BYTES, GetShaderSize(),
68 : "Combined size of WebGL shader ASCII sources and translation logs "
69 0 : "cached on the heap.");
70 :
71 0 : MOZ_COLLECT_REPORT(
72 : "webgl-shader-count", KIND_OTHER, UNITS_COUNT, GetShaderCount(),
73 0 : "Number of WebGL shaders.");
74 :
75 0 : MOZ_COLLECT_REPORT(
76 : "webgl-context-count", KIND_OTHER, UNITS_COUNT, GetContextCount(),
77 0 : "Number of WebGL contexts.");
78 :
79 0 : return NS_OK;
80 : }
81 :
82 0 : NS_IMPL_ISUPPORTS(WebGLMemoryTracker, nsIMemoryReporter)
83 :
84 3 : StaticRefPtr<WebGLMemoryTracker> WebGLMemoryTracker::sUniqueInstance;
85 :
86 : WebGLMemoryTracker*
87 0 : WebGLMemoryTracker::UniqueInstance()
88 : {
89 0 : if (!sUniqueInstance) {
90 0 : sUniqueInstance = new WebGLMemoryTracker;
91 0 : sUniqueInstance->InitMemoryReporter();
92 : }
93 0 : return sUniqueInstance;
94 : }
95 :
96 0 : WebGLMemoryTracker::WebGLMemoryTracker()
97 : {
98 0 : }
99 :
100 : void
101 0 : WebGLMemoryTracker::InitMemoryReporter()
102 : {
103 0 : RegisterWeakMemoryReporter(this);
104 0 : }
105 :
106 0 : WebGLMemoryTracker::~WebGLMemoryTracker()
107 : {
108 0 : UnregisterWeakMemoryReporter(this);
109 0 : }
110 :
111 0 : MOZ_DEFINE_MALLOC_SIZE_OF(WebGLBufferMallocSizeOf)
112 :
113 : int64_t
114 0 : WebGLMemoryTracker::GetBufferCacheMemoryUsed()
115 : {
116 0 : const ContextsArrayType& contexts = Contexts();
117 0 : int64_t result = 0;
118 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
119 0 : for (const WebGLBuffer* buffer = contexts[i]->mBuffers.getFirst();
120 0 : buffer;
121 0 : buffer = buffer->getNext())
122 : {
123 0 : if (buffer->Content() == WebGLBuffer::Kind::ElementArray) {
124 0 : result += buffer->SizeOfIncludingThis(WebGLBufferMallocSizeOf);
125 : }
126 : }
127 : }
128 0 : return result;
129 : }
130 :
131 0 : MOZ_DEFINE_MALLOC_SIZE_OF(WebGLShaderMallocSizeOf)
132 :
133 : int64_t
134 0 : WebGLMemoryTracker::GetShaderSize()
135 : {
136 0 : const ContextsArrayType& contexts = Contexts();
137 0 : int64_t result = 0;
138 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
139 0 : for (const WebGLShader* shader = contexts[i]->mShaders.getFirst();
140 0 : shader;
141 0 : shader = shader->getNext())
142 : {
143 0 : result += shader->SizeOfIncludingThis(WebGLShaderMallocSizeOf);
144 : }
145 : }
146 0 : return result;
147 : }
148 :
149 : /*static*/ int64_t
150 0 : WebGLMemoryTracker::GetTextureMemoryUsed()
151 : {
152 0 : const ContextsArrayType & contexts = Contexts();
153 0 : int64_t result = 0;
154 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
155 0 : for (const WebGLTexture* texture = contexts[i]->mTextures.getFirst();
156 0 : texture;
157 0 : texture = texture->getNext())
158 : {
159 0 : result += texture->MemoryUsage();
160 : }
161 : }
162 0 : return result;
163 : }
164 :
165 : /*static*/ int64_t
166 0 : WebGLMemoryTracker::GetTextureCount()
167 : {
168 0 : const ContextsArrayType & contexts = Contexts();
169 0 : int64_t result = 0;
170 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
171 0 : for (const WebGLTexture* texture = contexts[i]->mTextures.getFirst();
172 0 : texture;
173 0 : texture = texture->getNext())
174 : {
175 0 : result++;
176 : }
177 : }
178 0 : return result;
179 : }
180 :
181 : /*static*/ int64_t
182 0 : WebGLMemoryTracker::GetBufferMemoryUsed()
183 : {
184 0 : const ContextsArrayType & contexts = Contexts();
185 0 : int64_t result = 0;
186 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
187 0 : for (const WebGLBuffer* buffer = contexts[i]->mBuffers.getFirst();
188 0 : buffer;
189 0 : buffer = buffer->getNext())
190 : {
191 0 : result += buffer->ByteLength();
192 : }
193 : }
194 0 : return result;
195 : }
196 :
197 : /*static*/ int64_t
198 0 : WebGLMemoryTracker::GetBufferCount()
199 : {
200 0 : const ContextsArrayType & contexts = Contexts();
201 0 : int64_t result = 0;
202 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
203 0 : for (const WebGLBuffer* buffer = contexts[i]->mBuffers.getFirst();
204 0 : buffer;
205 0 : buffer = buffer->getNext())
206 : {
207 0 : result++;
208 : }
209 : }
210 0 : return result;
211 : }
212 :
213 : /*static*/ int64_t
214 0 : WebGLMemoryTracker::GetRenderbufferMemoryUsed()
215 : {
216 0 : const ContextsArrayType & contexts = Contexts();
217 0 : int64_t result = 0;
218 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
219 0 : for (const WebGLRenderbuffer* rb = contexts[i]->mRenderbuffers.getFirst();
220 0 : rb;
221 0 : rb = rb->getNext())
222 : {
223 0 : result += rb->MemoryUsage();
224 : }
225 : }
226 0 : return result;
227 : }
228 :
229 : /*static*/ int64_t
230 0 : WebGLMemoryTracker::GetRenderbufferCount()
231 : {
232 0 : const ContextsArrayType & contexts = Contexts();
233 0 : int64_t result = 0;
234 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
235 0 : for (const WebGLRenderbuffer* rb = contexts[i]->mRenderbuffers.getFirst();
236 0 : rb;
237 0 : rb = rb->getNext())
238 : {
239 0 : result++;
240 : }
241 : }
242 0 : return result;
243 : }
244 :
245 : /*static*/ int64_t
246 0 : WebGLMemoryTracker::GetShaderCount()
247 : {
248 0 : const ContextsArrayType & contexts = Contexts();
249 0 : int64_t result = 0;
250 0 : for(size_t i = 0; i < contexts.Length(); ++i) {
251 0 : for (const WebGLShader* shader = contexts[i]->mShaders.getFirst();
252 0 : shader;
253 0 : shader = shader->getNext())
254 : {
255 0 : result++;
256 : }
257 : }
258 0 : return result;
259 : }
260 :
261 : } // namespace mozilla
|