Backed out changeset 58dbd2146e24 (bug 944961) for bustage.
[gecko.git] / content / canvas / src / WebGLContextReporter.cpp
blob678c285f89b61a46a0420d2a2427c0e728582501
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/. */
6 #include "WebGLContext.h"
7 #include "WebGLMemoryTracker.h"
9 using namespace mozilla;
11 NS_IMPL_ISUPPORTS1(WebGLMemoryPressureObserver, nsIObserver)
13 NS_IMETHODIMP
14 WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
15 nsISupports* aData)
17 #define REPORT(_path, _kind, _units, _amount, _desc) \
18 do { \
19 nsresult rv; \
20 rv = aHandleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
21 _kind, _units, _amount, \
22 NS_LITERAL_CSTRING(_desc), aData); \
23 NS_ENSURE_SUCCESS(rv, rv); \
24 } while (0)
26 REPORT("webgl-texture-memory",
27 KIND_OTHER, UNITS_BYTES, GetTextureMemoryUsed(),
28 "Memory used by WebGL textures.The OpenGL"
29 " implementation is free to store these textures in either video"
30 " memory or main memory. This measurement is only a lower bound,"
31 " actual memory usage may be higher for example if the storage"
32 " is strided.");
34 REPORT("webgl-texture-count",
35 KIND_OTHER, UNITS_COUNT, GetTextureCount(),
36 "Number of WebGL textures.");
38 REPORT("webgl-buffer-memory",
39 KIND_OTHER, UNITS_BYTES, GetBufferMemoryUsed(),
40 "Memory used by WebGL buffers. The OpenGL"
41 " implementation is free to store these buffers in either video"
42 " memory or main memory. This measurement is only a lower bound,"
43 " actual memory usage may be higher for example if the storage"
44 " is strided.");
46 REPORT("explicit/webgl/buffer-cache-memory",
47 KIND_HEAP, UNITS_BYTES, GetBufferCacheMemoryUsed(),
48 "Memory used by WebGL buffer caches. The WebGL"
49 " implementation caches the contents of element array buffers"
50 " only.This adds up with the webgl-buffer-memory value, but"
51 " contrary to it, this one represents bytes on the heap,"
52 " not managed by OpenGL.");
54 REPORT("webgl-buffer-count",
55 KIND_OTHER, UNITS_COUNT, GetBufferCount(),
56 "Number of WebGL buffers.");
58 REPORT("webgl-renderbuffer-memory",
59 KIND_OTHER, UNITS_BYTES, GetRenderbufferMemoryUsed(),
60 "Memory used by WebGL renderbuffers. The OpenGL"
61 " implementation is free to store these renderbuffers in either"
62 " video memory or main memory. This measurement is only a lower"
63 " bound, actual memory usage may be higher for example if the"
64 " storage is strided.");
66 REPORT("webgl-renderbuffer-count",
67 KIND_OTHER, UNITS_COUNT, GetRenderbufferCount(),
68 "Number of WebGL renderbuffers.");
70 REPORT("explicit/webgl/shader",
71 KIND_HEAP, UNITS_BYTES, GetShaderSize(),
72 "Combined size of WebGL shader ASCII sources and translation"
73 " logs cached on the heap.");
75 REPORT("webgl-shader-count",
76 KIND_OTHER, UNITS_COUNT, GetShaderCount(),
77 "Number of WebGL shaders.");
79 REPORT("webgl-context-count",
80 KIND_OTHER, UNITS_COUNT, GetContextCount(),
81 "Number of WebGL contexts.");
83 #undef REPORT
85 return NS_OK;
88 NS_IMPL_ISUPPORTS_INHERITED0(WebGLMemoryTracker, MemoryMultiReporter)
90 StaticRefPtr<WebGLMemoryTracker> WebGLMemoryTracker::sUniqueInstance;
92 WebGLMemoryTracker* WebGLMemoryTracker::UniqueInstance()
94 if (!sUniqueInstance) {
95 sUniqueInstance = new WebGLMemoryTracker;
96 sUniqueInstance->InitMemoryReporter();
98 return sUniqueInstance;
101 WebGLMemoryTracker::WebGLMemoryTracker()
102 : MemoryMultiReporter("webgl")
106 void
107 WebGLMemoryTracker::InitMemoryReporter()
109 RegisterWeakMemoryReporter(this);
112 WebGLMemoryTracker::~WebGLMemoryTracker()
114 UnregisterWeakMemoryReporter(this);
117 NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(WebGLBufferMallocSizeOf)
119 int64_t
120 WebGLMemoryTracker::GetBufferCacheMemoryUsed() {
121 const ContextsArrayType & contexts = Contexts();
122 int64_t result = 0;
123 for(size_t i = 0; i < contexts.Length(); ++i) {
124 for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst();
125 buffer;
126 buffer = buffer->getNext())
128 if (buffer->Target() == LOCAL_GL_ELEMENT_ARRAY_BUFFER)
129 result += buffer->SizeOfIncludingThis(WebGLBufferMallocSizeOf);
132 return result;
135 NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(WebGLShaderMallocSizeOf)
137 int64_t
138 WebGLMemoryTracker::GetShaderSize() {
139 const ContextsArrayType & contexts = Contexts();
140 int64_t result = 0;
141 for(size_t i = 0; i < contexts.Length(); ++i) {
142 for (const WebGLShader *shader = contexts[i]->mShaders.getFirst();
143 shader;
144 shader = shader->getNext())
146 result += shader->SizeOfIncludingThis(WebGLShaderMallocSizeOf);
149 return result;