content/canvas/src/WebGLMemoryTracker.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial