content/canvas/src/WebGLContextReporter.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/src/WebGLContextReporter.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; 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 +#include "WebGLContext.h"
    1.10 +#include "WebGLMemoryTracker.h"
    1.11 +
    1.12 +using namespace mozilla;
    1.13 +
    1.14 +NS_IMPL_ISUPPORTS(WebGLMemoryPressureObserver, nsIObserver)
    1.15 +
    1.16 +NS_IMETHODIMP
    1.17 +WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
    1.18 +                                   nsISupports* aData)
    1.19 +{
    1.20 +#define REPORT(_path, _kind, _units, _amount, _desc)                          \
    1.21 +    do {                                                                      \
    1.22 +      nsresult rv;                                                            \
    1.23 +      rv = aHandleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
    1.24 +                                   _kind, _units, _amount,                    \
    1.25 +                                   NS_LITERAL_CSTRING(_desc), aData);         \
    1.26 +      NS_ENSURE_SUCCESS(rv, rv);                                              \
    1.27 +    } while (0)
    1.28 +
    1.29 +    REPORT("webgl-texture-memory",
    1.30 +           KIND_OTHER, UNITS_BYTES, GetTextureMemoryUsed(),
    1.31 +           "Memory used by WebGL textures.The OpenGL"
    1.32 +           " implementation is free to store these textures in either video"
    1.33 +           " memory or main memory. This measurement is only a lower bound,"
    1.34 +           " actual memory usage may be higher for example if the storage"
    1.35 +           " is strided.");
    1.36 +
    1.37 +    REPORT("webgl-texture-count",
    1.38 +           KIND_OTHER, UNITS_COUNT, GetTextureCount(),
    1.39 +           "Number of WebGL textures.");
    1.40 +
    1.41 +    REPORT("webgl-buffer-memory",
    1.42 +           KIND_OTHER, UNITS_BYTES, GetBufferMemoryUsed(),
    1.43 +           "Memory used by WebGL buffers. The OpenGL"
    1.44 +           " implementation is free to store these buffers in either video"
    1.45 +           " memory or main memory. This measurement is only a lower bound,"
    1.46 +           " actual memory usage may be higher for example if the storage"
    1.47 +           " is strided.");
    1.48 +
    1.49 +    REPORT("explicit/webgl/buffer-cache-memory",
    1.50 +           KIND_HEAP, UNITS_BYTES, GetBufferCacheMemoryUsed(),
    1.51 +           "Memory used by WebGL buffer caches. The WebGL"
    1.52 +           " implementation caches the contents of element array buffers"
    1.53 +           " only.This adds up with the webgl-buffer-memory value, but"
    1.54 +           " contrary to it, this one represents bytes on the heap,"
    1.55 +           " not managed by OpenGL.");
    1.56 +
    1.57 +    REPORT("webgl-buffer-count",
    1.58 +           KIND_OTHER, UNITS_COUNT, GetBufferCount(),
    1.59 +           "Number of WebGL buffers.");
    1.60 +
    1.61 +    REPORT("webgl-renderbuffer-memory",
    1.62 +           KIND_OTHER, UNITS_BYTES, GetRenderbufferMemoryUsed(),
    1.63 +           "Memory used by WebGL renderbuffers. The OpenGL"
    1.64 +           " implementation is free to store these renderbuffers in either"
    1.65 +           " video memory or main memory. This measurement is only a lower"
    1.66 +           " bound, actual memory usage may be higher for example if the"
    1.67 +           " storage is strided.");
    1.68 +
    1.69 +    REPORT("webgl-renderbuffer-count",
    1.70 +           KIND_OTHER, UNITS_COUNT, GetRenderbufferCount(),
    1.71 +           "Number of WebGL renderbuffers.");
    1.72 +
    1.73 +    REPORT("explicit/webgl/shader",
    1.74 +           KIND_HEAP, UNITS_BYTES, GetShaderSize(),
    1.75 +           "Combined size of WebGL shader ASCII sources and translation"
    1.76 +           " logs cached on the heap.");
    1.77 +
    1.78 +    REPORT("webgl-shader-count",
    1.79 +           KIND_OTHER, UNITS_COUNT, GetShaderCount(),
    1.80 +           "Number of WebGL shaders.");
    1.81 +
    1.82 +    REPORT("webgl-context-count",
    1.83 +           KIND_OTHER, UNITS_COUNT, GetContextCount(),
    1.84 +           "Number of WebGL contexts.");
    1.85 +
    1.86 +#undef REPORT
    1.87 +
    1.88 +    return NS_OK;
    1.89 +}
    1.90 +
    1.91 +NS_IMPL_ISUPPORTS(WebGLMemoryTracker, nsIMemoryReporter)
    1.92 +
    1.93 +StaticRefPtr<WebGLMemoryTracker> WebGLMemoryTracker::sUniqueInstance;
    1.94 +
    1.95 +WebGLMemoryTracker* WebGLMemoryTracker::UniqueInstance()
    1.96 +{
    1.97 +    if (!sUniqueInstance) {
    1.98 +        sUniqueInstance = new WebGLMemoryTracker;
    1.99 +        sUniqueInstance->InitMemoryReporter();
   1.100 +    }
   1.101 +    return sUniqueInstance;
   1.102 +}
   1.103 +
   1.104 +WebGLMemoryTracker::WebGLMemoryTracker()
   1.105 +{
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +WebGLMemoryTracker::InitMemoryReporter()
   1.110 +{
   1.111 +    RegisterWeakMemoryReporter(this);
   1.112 +}
   1.113 +
   1.114 +WebGLMemoryTracker::~WebGLMemoryTracker()
   1.115 +{
   1.116 +    UnregisterWeakMemoryReporter(this);
   1.117 +}
   1.118 +
   1.119 +MOZ_DEFINE_MALLOC_SIZE_OF(WebGLBufferMallocSizeOf)
   1.120 +
   1.121 +int64_t
   1.122 +WebGLMemoryTracker::GetBufferCacheMemoryUsed() {
   1.123 +    const ContextsArrayType & contexts = Contexts();
   1.124 +    int64_t result = 0;
   1.125 +    for(size_t i = 0; i < contexts.Length(); ++i) {
   1.126 +        for (const WebGLBuffer *buffer = contexts[i]->mBuffers.getFirst();
   1.127 +             buffer;
   1.128 +             buffer = buffer->getNext())
   1.129 +        {
   1.130 +            if (buffer->Target() == LOCAL_GL_ELEMENT_ARRAY_BUFFER)
   1.131 +                result += buffer->SizeOfIncludingThis(WebGLBufferMallocSizeOf);
   1.132 +        }
   1.133 +    }
   1.134 +    return result;
   1.135 +}
   1.136 +
   1.137 +MOZ_DEFINE_MALLOC_SIZE_OF(WebGLShaderMallocSizeOf)
   1.138 +
   1.139 +int64_t
   1.140 +WebGLMemoryTracker::GetShaderSize() {
   1.141 +    const ContextsArrayType & contexts = Contexts();
   1.142 +    int64_t result = 0;
   1.143 +    for(size_t i = 0; i < contexts.Length(); ++i) {
   1.144 +        for (const WebGLShader *shader = contexts[i]->mShaders.getFirst();
   1.145 +             shader;
   1.146 +             shader = shader->getNext())
   1.147 +        {
   1.148 +            result += shader->SizeOfIncludingThis(WebGLShaderMallocSizeOf);
   1.149 +        }
   1.150 +    }
   1.151 +    return result;
   1.152 +}

mercurial