content/canvas/src/WebGLMemoryTracker.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     6 #ifndef WEBGLMEMORYTRACKER_H_
     7 #define WEBGLMEMORYTRACKER_H_
     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"
    20 namespace mozilla {
    22 class WebGLMemoryTracker : public nsIMemoryReporter
    23 {
    24     NS_DECL_THREADSAFE_ISUPPORTS
    25     NS_DECL_NSIMEMORYREPORTER
    27     WebGLMemoryTracker();
    28     virtual ~WebGLMemoryTracker();
    29     static StaticRefPtr<WebGLMemoryTracker> sUniqueInstance;
    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;
    37     void InitMemoryReporter();
    39     static WebGLMemoryTracker* UniqueInstance();
    41     static ContextsArrayType & Contexts() { return UniqueInstance()->mContexts; }
    43     friend class WebGLContext;
    45   public:
    47     static void AddWebGLContext(const WebGLContext* c) {
    48         Contexts().AppendElement(c);
    49     }
    51     static void RemoveWebGLContext(const WebGLContext* c) {
    52         ContextsArrayType & contexts = Contexts();
    53         contexts.RemoveElement(c);
    54         if (contexts.IsEmpty()) {
    55             sUniqueInstance = nullptr;
    56         }
    57     }
    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     }
    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     }
    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     }
   102     static int64_t GetBufferCacheMemoryUsed();
   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     }
   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     }
   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     }
   146     static int64_t GetShaderSize();
   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     }
   162     static int64_t GetContextCount() {
   163         return Contexts().Length();
   164     }
   165 };
   168 } // namespace mozilla
   170 #endif

mercurial