michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef WEBGLMEMORYTRACKER_H_ michael@0: #define WEBGLMEMORYTRACKER_H_ michael@0: michael@0: #include "WebGLContext.h" michael@0: #include "WebGLBuffer.h" michael@0: #include "WebGLVertexAttribData.h" michael@0: #include "WebGLShader.h" michael@0: #include "WebGLProgram.h" michael@0: #include "WebGLUniformLocation.h" michael@0: #include "WebGLTexture.h" michael@0: #include "WebGLRenderbuffer.h" michael@0: #include "mozilla/StaticPtr.h" michael@0: #include "nsIMemoryReporter.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class WebGLMemoryTracker : public nsIMemoryReporter michael@0: { michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIMEMORYREPORTER michael@0: michael@0: WebGLMemoryTracker(); michael@0: virtual ~WebGLMemoryTracker(); michael@0: static StaticRefPtr sUniqueInstance; michael@0: michael@0: // Here we store plain pointers, not RefPtrs: we don't want the michael@0: // WebGLMemoryTracker unique instance to keep alive all michael@0: // WebGLContexts ever created. michael@0: typedef nsTArray ContextsArrayType; michael@0: ContextsArrayType mContexts; michael@0: michael@0: void InitMemoryReporter(); michael@0: michael@0: static WebGLMemoryTracker* UniqueInstance(); michael@0: michael@0: static ContextsArrayType & Contexts() { return UniqueInstance()->mContexts; } michael@0: michael@0: friend class WebGLContext; michael@0: michael@0: public: michael@0: michael@0: static void AddWebGLContext(const WebGLContext* c) { michael@0: Contexts().AppendElement(c); michael@0: } michael@0: michael@0: static void RemoveWebGLContext(const WebGLContext* c) { michael@0: ContextsArrayType & contexts = Contexts(); michael@0: contexts.RemoveElement(c); michael@0: if (contexts.IsEmpty()) { michael@0: sUniqueInstance = nullptr; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: static int64_t GetTextureMemoryUsed() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLTexture *texture = contexts[i]->mTextures.getFirst(); michael@0: texture; michael@0: texture = texture->getNext()) michael@0: { michael@0: result += texture->MemoryUsage(); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetTextureCount() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLTexture *texture = contexts[i]->mTextures.getFirst(); michael@0: texture; michael@0: texture = texture->getNext()) michael@0: { michael@0: result++; michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetBufferMemoryUsed() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst(); michael@0: buffer; michael@0: buffer = buffer->getNext()) michael@0: { michael@0: result += buffer->ByteLength(); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetBufferCacheMemoryUsed(); michael@0: michael@0: static int64_t GetBufferCount() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst(); michael@0: buffer; michael@0: buffer = buffer->getNext()) michael@0: { michael@0: result++; michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetRenderbufferMemoryUsed() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLRenderbuffer *rb = contexts[i]->mRenderbuffers.getFirst(); michael@0: rb; michael@0: rb = rb->getNext()) michael@0: { michael@0: result += rb->MemoryUsage(); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetRenderbufferCount() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLRenderbuffer *rb = contexts[i]->mRenderbuffers.getFirst(); michael@0: rb; michael@0: rb = rb->getNext()) michael@0: { michael@0: result++; michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetShaderSize(); michael@0: michael@0: static int64_t GetShaderCount() { michael@0: const ContextsArrayType & contexts = Contexts(); michael@0: int64_t result = 0; michael@0: for(size_t i = 0; i < contexts.Length(); ++i) { michael@0: for (const WebGLShader *shader = contexts[i]->mShaders.getFirst(); michael@0: shader; michael@0: shader = shader->getNext()) michael@0: { michael@0: result++; michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static int64_t GetContextCount() { michael@0: return Contexts().Length(); michael@0: } michael@0: }; michael@0: michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif