|
1 /* -*- Mode: C++; tab-width: 4; 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 #ifndef WEBGLMEMORYTRACKER_H_ |
|
7 #define WEBGLMEMORYTRACKER_H_ |
|
8 |
|
9 #include "WebGLContext.h" |
|
10 #include "WebGLBuffer.h" |
|
11 #include "WebGLVertexAttribData.h" |
|
12 #include "WebGLShader.h" |
|
13 #include "WebGLProgram.h" |
|
14 #include "WebGLUniformLocation.h" |
|
15 #include "WebGLTexture.h" |
|
16 #include "WebGLRenderbuffer.h" |
|
17 #include "mozilla/StaticPtr.h" |
|
18 #include "nsIMemoryReporter.h" |
|
19 |
|
20 namespace mozilla { |
|
21 |
|
22 class WebGLMemoryTracker : public nsIMemoryReporter |
|
23 { |
|
24 NS_DECL_THREADSAFE_ISUPPORTS |
|
25 NS_DECL_NSIMEMORYREPORTER |
|
26 |
|
27 WebGLMemoryTracker(); |
|
28 virtual ~WebGLMemoryTracker(); |
|
29 static StaticRefPtr<WebGLMemoryTracker> sUniqueInstance; |
|
30 |
|
31 // Here we store plain pointers, not RefPtrs: we don't want the |
|
32 // WebGLMemoryTracker unique instance to keep alive all |
|
33 // WebGLContexts ever created. |
|
34 typedef nsTArray<const WebGLContext*> ContextsArrayType; |
|
35 ContextsArrayType mContexts; |
|
36 |
|
37 void InitMemoryReporter(); |
|
38 |
|
39 static WebGLMemoryTracker* UniqueInstance(); |
|
40 |
|
41 static ContextsArrayType & Contexts() { return UniqueInstance()->mContexts; } |
|
42 |
|
43 friend class WebGLContext; |
|
44 |
|
45 public: |
|
46 |
|
47 static void AddWebGLContext(const WebGLContext* c) { |
|
48 Contexts().AppendElement(c); |
|
49 } |
|
50 |
|
51 static void RemoveWebGLContext(const WebGLContext* c) { |
|
52 ContextsArrayType & contexts = Contexts(); |
|
53 contexts.RemoveElement(c); |
|
54 if (contexts.IsEmpty()) { |
|
55 sUniqueInstance = nullptr; |
|
56 } |
|
57 } |
|
58 |
|
59 private: |
|
60 static int64_t GetTextureMemoryUsed() { |
|
61 const ContextsArrayType & contexts = Contexts(); |
|
62 int64_t result = 0; |
|
63 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
64 for (const WebGLTexture *texture = contexts[i]->mTextures.getFirst(); |
|
65 texture; |
|
66 texture = texture->getNext()) |
|
67 { |
|
68 result += texture->MemoryUsage(); |
|
69 } |
|
70 } |
|
71 return result; |
|
72 } |
|
73 |
|
74 static int64_t GetTextureCount() { |
|
75 const ContextsArrayType & contexts = Contexts(); |
|
76 int64_t result = 0; |
|
77 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
78 for (const WebGLTexture *texture = contexts[i]->mTextures.getFirst(); |
|
79 texture; |
|
80 texture = texture->getNext()) |
|
81 { |
|
82 result++; |
|
83 } |
|
84 } |
|
85 return result; |
|
86 } |
|
87 |
|
88 static int64_t GetBufferMemoryUsed() { |
|
89 const ContextsArrayType & contexts = Contexts(); |
|
90 int64_t result = 0; |
|
91 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
92 for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst(); |
|
93 buffer; |
|
94 buffer = buffer->getNext()) |
|
95 { |
|
96 result += buffer->ByteLength(); |
|
97 } |
|
98 } |
|
99 return result; |
|
100 } |
|
101 |
|
102 static int64_t GetBufferCacheMemoryUsed(); |
|
103 |
|
104 static int64_t GetBufferCount() { |
|
105 const ContextsArrayType & contexts = Contexts(); |
|
106 int64_t result = 0; |
|
107 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
108 for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst(); |
|
109 buffer; |
|
110 buffer = buffer->getNext()) |
|
111 { |
|
112 result++; |
|
113 } |
|
114 } |
|
115 return result; |
|
116 } |
|
117 |
|
118 static int64_t GetRenderbufferMemoryUsed() { |
|
119 const ContextsArrayType & contexts = Contexts(); |
|
120 int64_t result = 0; |
|
121 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
122 for (const WebGLRenderbuffer *rb = contexts[i]->mRenderbuffers.getFirst(); |
|
123 rb; |
|
124 rb = rb->getNext()) |
|
125 { |
|
126 result += rb->MemoryUsage(); |
|
127 } |
|
128 } |
|
129 return result; |
|
130 } |
|
131 |
|
132 static int64_t GetRenderbufferCount() { |
|
133 const ContextsArrayType & contexts = Contexts(); |
|
134 int64_t result = 0; |
|
135 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
136 for (const WebGLRenderbuffer *rb = contexts[i]->mRenderbuffers.getFirst(); |
|
137 rb; |
|
138 rb = rb->getNext()) |
|
139 { |
|
140 result++; |
|
141 } |
|
142 } |
|
143 return result; |
|
144 } |
|
145 |
|
146 static int64_t GetShaderSize(); |
|
147 |
|
148 static int64_t GetShaderCount() { |
|
149 const ContextsArrayType & contexts = Contexts(); |
|
150 int64_t result = 0; |
|
151 for(size_t i = 0; i < contexts.Length(); ++i) { |
|
152 for (const WebGLShader *shader = contexts[i]->mShaders.getFirst(); |
|
153 shader; |
|
154 shader = shader->getNext()) |
|
155 { |
|
156 result++; |
|
157 } |
|
158 } |
|
159 return result; |
|
160 } |
|
161 |
|
162 static int64_t GetContextCount() { |
|
163 return Contexts().Length(); |
|
164 } |
|
165 }; |
|
166 |
|
167 |
|
168 } // namespace mozilla |
|
169 |
|
170 #endif |