content/canvas/src/WebGLMemoryTracker.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/src/WebGLMemoryTracker.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef WEBGLMEMORYTRACKER_H_
    1.10 +#define WEBGLMEMORYTRACKER_H_
    1.11 +
    1.12 +#include "WebGLContext.h"
    1.13 +#include "WebGLBuffer.h"
    1.14 +#include "WebGLVertexAttribData.h"
    1.15 +#include "WebGLShader.h"
    1.16 +#include "WebGLProgram.h"
    1.17 +#include "WebGLUniformLocation.h"
    1.18 +#include "WebGLTexture.h"
    1.19 +#include "WebGLRenderbuffer.h"
    1.20 +#include "mozilla/StaticPtr.h"
    1.21 +#include "nsIMemoryReporter.h"
    1.22 +
    1.23 +namespace mozilla {
    1.24 +
    1.25 +class WebGLMemoryTracker : public nsIMemoryReporter
    1.26 +{
    1.27 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.28 +    NS_DECL_NSIMEMORYREPORTER
    1.29 +
    1.30 +    WebGLMemoryTracker();
    1.31 +    virtual ~WebGLMemoryTracker();
    1.32 +    static StaticRefPtr<WebGLMemoryTracker> sUniqueInstance;
    1.33 +
    1.34 +    // Here we store plain pointers, not RefPtrs: we don't want the
    1.35 +    // WebGLMemoryTracker unique instance to keep alive all
    1.36 +    // WebGLContexts ever created.
    1.37 +    typedef nsTArray<const WebGLContext*> ContextsArrayType;
    1.38 +    ContextsArrayType mContexts;
    1.39 +
    1.40 +    void InitMemoryReporter();
    1.41 +
    1.42 +    static WebGLMemoryTracker* UniqueInstance();
    1.43 +
    1.44 +    static ContextsArrayType & Contexts() { return UniqueInstance()->mContexts; }
    1.45 +
    1.46 +    friend class WebGLContext;
    1.47 +
    1.48 +  public:
    1.49 +
    1.50 +    static void AddWebGLContext(const WebGLContext* c) {
    1.51 +        Contexts().AppendElement(c);
    1.52 +    }
    1.53 +
    1.54 +    static void RemoveWebGLContext(const WebGLContext* c) {
    1.55 +        ContextsArrayType & contexts = Contexts();
    1.56 +        contexts.RemoveElement(c);
    1.57 +        if (contexts.IsEmpty()) {
    1.58 +            sUniqueInstance = nullptr;
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +  private:
    1.63 +    static int64_t GetTextureMemoryUsed() {
    1.64 +        const ContextsArrayType & contexts = Contexts();
    1.65 +        int64_t result = 0;
    1.66 +        for(size_t i = 0; i < contexts.Length(); ++i) {
    1.67 +            for (const WebGLTexture *texture = contexts[i]->mTextures.getFirst();
    1.68 +                 texture;
    1.69 +                 texture = texture->getNext())
    1.70 +            {
    1.71 +                result += texture->MemoryUsage();
    1.72 +            }
    1.73 +        }
    1.74 +        return result;
    1.75 +    }
    1.76 +
    1.77 +    static int64_t GetTextureCount() {
    1.78 +        const ContextsArrayType & contexts = Contexts();
    1.79 +        int64_t result = 0;
    1.80 +        for(size_t i = 0; i < contexts.Length(); ++i) {
    1.81 +            for (const WebGLTexture *texture = contexts[i]->mTextures.getFirst();
    1.82 +                 texture;
    1.83 +                 texture = texture->getNext())
    1.84 +            {
    1.85 +                result++;
    1.86 +            }
    1.87 +        }
    1.88 +        return result;
    1.89 +    }
    1.90 +
    1.91 +    static int64_t GetBufferMemoryUsed() {
    1.92 +        const ContextsArrayType & contexts = Contexts();
    1.93 +        int64_t result = 0;
    1.94 +        for(size_t i = 0; i < contexts.Length(); ++i) {
    1.95 +            for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst();
    1.96 +                 buffer;
    1.97 +                 buffer = buffer->getNext())
    1.98 +            {
    1.99 +                result += buffer->ByteLength();
   1.100 +            }
   1.101 +        }
   1.102 +        return result;
   1.103 +    }
   1.104 +
   1.105 +    static int64_t GetBufferCacheMemoryUsed();
   1.106 +
   1.107 +    static int64_t GetBufferCount() {
   1.108 +        const ContextsArrayType & contexts = Contexts();
   1.109 +        int64_t result = 0;
   1.110 +        for(size_t i = 0; i < contexts.Length(); ++i) {
   1.111 +            for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst();
   1.112 +                 buffer;
   1.113 +                 buffer = buffer->getNext())
   1.114 +            {
   1.115 +                result++;
   1.116 +            }
   1.117 +        }
   1.118 +        return result;
   1.119 +    }
   1.120 +
   1.121 +    static int64_t GetRenderbufferMemoryUsed() {
   1.122 +        const ContextsArrayType & contexts = Contexts();
   1.123 +        int64_t result = 0;
   1.124 +        for(size_t i = 0; i < contexts.Length(); ++i) {
   1.125 +            for (const WebGLRenderbuffer *rb = contexts[i]->mRenderbuffers.getFirst();
   1.126 +                 rb;
   1.127 +                 rb = rb->getNext())
   1.128 +            {
   1.129 +                result += rb->MemoryUsage();
   1.130 +            }
   1.131 +        }
   1.132 +        return result;
   1.133 +    }
   1.134 +
   1.135 +    static int64_t GetRenderbufferCount() {
   1.136 +        const ContextsArrayType & contexts = Contexts();
   1.137 +        int64_t result = 0;
   1.138 +        for(size_t i = 0; i < contexts.Length(); ++i) {
   1.139 +            for (const WebGLRenderbuffer *rb = contexts[i]->mRenderbuffers.getFirst();
   1.140 +                 rb;
   1.141 +                 rb = rb->getNext())
   1.142 +            {
   1.143 +                result++;
   1.144 +            }
   1.145 +        }
   1.146 +        return result;
   1.147 +    }
   1.148 +
   1.149 +    static int64_t GetShaderSize();
   1.150 +
   1.151 +    static int64_t GetShaderCount() {
   1.152 +        const ContextsArrayType & contexts = Contexts();
   1.153 +        int64_t result = 0;
   1.154 +        for(size_t i = 0; i < contexts.Length(); ++i) {
   1.155 +            for (const WebGLShader *shader = contexts[i]->mShaders.getFirst();
   1.156 +                 shader;
   1.157 +                 shader = shader->getNext())
   1.158 +            {
   1.159 +                result++;
   1.160 +            }
   1.161 +        }
   1.162 +        return result;
   1.163 +    }
   1.164 +
   1.165 +    static int64_t GetContextCount() {
   1.166 +        return Contexts().Length();
   1.167 +    }
   1.168 +};
   1.169 +
   1.170 +
   1.171 +} // namespace mozilla
   1.172 +
   1.173 +#endif

mercurial