WebGLMemoryTracker.cpp (5366B)
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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/. */ 5 6 #include "WebGLMemoryTracker.h" 7 8 #include "HostWebGLContext.h" 9 #include "WebGLBuffer.h" 10 #include "WebGLRenderbuffer.h" 11 #include "WebGLShader.h" 12 #include "WebGLTexture.h" 13 14 namespace mozilla { 15 namespace webgl { 16 MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf) 17 } // namespace webgl 18 19 void WebGLMemoryTracker::EnsureRegistered() { 20 static bool sIsRegistered = []() { 21 RegisterStrongMemoryReporter(new WebGLMemoryTracker); 22 return true; 23 }(); 24 (void)sIsRegistered; 25 } 26 27 NS_IMETHODIMP 28 WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport, 29 nsISupports* aData, bool) { 30 const auto locked = HostWebGLContext::OutstandingContexts(); 31 const auto& contexts = locked->contexts; 32 33 const auto contextCount = contexts.size(); 34 35 size_t bufferCount = 0; 36 int64_t bufferGpuSize = 0; 37 int64_t bufferCacheSize = 0; 38 39 size_t rbCount = 0; 40 int64_t rbGpuSize = 0; 41 42 size_t shaderCount = 0; 43 int64_t shaderCpuSize = 0; 44 45 size_t texCount = 0; 46 size_t texHeapOverhead = 0; 47 int64_t texGpuSize = 0; 48 49 for (const auto& context : contexts) { 50 bufferCount += context->mBufferMap.size(); 51 for (const auto& pair : context->mBufferMap) { 52 const auto& buffer = *pair.second; 53 bufferGpuSize += buffer.mByteLength; 54 55 if (buffer.mIndexCache) { 56 bufferCacheSize += buffer.mByteLength; 57 } 58 bufferCacheSize += buffer.mIndexRanges.size() * 59 sizeof(decltype(buffer.mIndexRanges)::value_type); 60 } 61 62 // - 63 64 rbCount += context->mRenderbufferMap.size(); 65 for (const auto& pair : context->mRenderbufferMap) { 66 const auto& rb = pair.second; 67 rbGpuSize += rb->MemoryUsage(); 68 } 69 70 // - 71 72 shaderCount += context->mShaderMap.size(); 73 for (const auto& pair : context->mShaderMap) { 74 const auto& shader = pair.second; 75 shaderCpuSize += shader->SizeOfIncludingThis(webgl::MallocSizeOf); 76 } 77 78 // - 79 80 texCount += context->mTextureMap.size(); 81 for (const auto& pair : context->mTextureMap) { 82 const auto& texture = pair.second; 83 texGpuSize += texture->MemoryUsage(); 84 texHeapOverhead += texture->SizeOfIncludingThis(webgl::MallocSizeOf); 85 } 86 } 87 88 // - 89 90 MOZ_COLLECT_REPORT( 91 "webgl-texture-memory", KIND_OTHER, UNITS_BYTES, texGpuSize, 92 "Memory used by WebGL textures. The OpenGL implementation is free to " 93 "store these textures in either video memory or main memory. This " 94 "measurement is only a lower bound, actual memory usage may be higher " 95 "for example if the storage is strided."); 96 97 MOZ_COLLECT_REPORT("webgl-texture-count", KIND_OTHER, UNITS_COUNT, 98 static_cast<int64_t>(texCount), 99 "Number of WebGL textures."); 100 101 MOZ_COLLECT_REPORT( 102 "webgl-buffer-memory", KIND_OTHER, UNITS_BYTES, bufferGpuSize, 103 "Memory used by WebGL buffers. The OpenGL implementation is free to " 104 "store these buffers in either video memory or main memory. This " 105 "measurement is only a lower bound, actual memory usage may be higher " 106 "for example if the storage is strided."); 107 108 MOZ_COLLECT_REPORT( 109 "explicit/webgl/buffer-cache-memory", KIND_HEAP, UNITS_BYTES, 110 bufferCacheSize, 111 "Memory used by WebGL buffer caches. The WebGL implementation caches " 112 "the contents of element array buffers only. This adds up with the " 113 "'webgl-buffer-memory' value, but contrary to it, this one represents " 114 "bytes on the heap, not managed by OpenGL."); 115 116 MOZ_COLLECT_REPORT("webgl-buffer-count", KIND_OTHER, UNITS_COUNT, 117 static_cast<int64_t>(bufferCount), 118 "Number of WebGL buffers."); 119 120 MOZ_COLLECT_REPORT( 121 "webgl-renderbuffer-memory", KIND_OTHER, UNITS_BYTES, rbGpuSize, 122 "Memory used by WebGL renderbuffers. The OpenGL implementation is free " 123 "to store these renderbuffers in either video memory or main memory. " 124 "This measurement is only a lower bound, actual memory usage may be " 125 "higher, for example if the storage is strided."); 126 127 MOZ_COLLECT_REPORT("webgl-renderbuffer-count", KIND_OTHER, UNITS_COUNT, 128 static_cast<int64_t>(rbCount), 129 "Number of WebGL renderbuffers."); 130 131 MOZ_COLLECT_REPORT( 132 "explicit/webgl/shaders", KIND_HEAP, UNITS_BYTES, shaderCpuSize, 133 "Combined size of WebGL shader ASCII sources and translation logs " 134 "cached on the heap."); 135 136 MOZ_COLLECT_REPORT("explicit/webgl/textures", KIND_HEAP, UNITS_BYTES, 137 texHeapOverhead, 138 "WebGLTexture implementation detail overhead."); 139 140 MOZ_COLLECT_REPORT("webgl-shader-count", KIND_OTHER, UNITS_COUNT, 141 static_cast<int64_t>(shaderCount), 142 "Number of WebGL shaders."); 143 144 MOZ_COLLECT_REPORT("webgl-context-count", KIND_OTHER, UNITS_COUNT, 145 static_cast<int64_t>(contextCount), 146 "Number of WebGL contexts."); 147 148 return NS_OK; 149 } 150 151 NS_IMPL_ISUPPORTS(WebGLMemoryTracker, nsIMemoryReporter) 152 153 } // namespace mozilla