1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include <string>
8 #include <sstream>
9 #include "nsExceptionHandler.h"
10 #include "GfxTexturesReporter.h"
11 #include "mozilla/StaticPrefs_gfx.h"
12 
13 using namespace mozilla::gl;
14 
15 NS_IMPL_ISUPPORTS(GfxTexturesReporter, nsIMemoryReporter)
16 
17 mozilla::Atomic<size_t> GfxTexturesReporter::sAmount(0);
18 mozilla::Atomic<size_t> GfxTexturesReporter::sPeakAmount(0);
19 mozilla::Atomic<size_t> GfxTexturesReporter::sTileWasteAmount(0);
20 
FormatBytes(size_t amount)21 static std::string FormatBytes(size_t amount) {
22   std::stringstream stream;
23   int depth = 0;
24   double val = amount;
25   while (val > 1024) {
26     val /= 1024;
27     depth++;
28   }
29 
30   const char* unit;
31   switch (depth) {
32     case 0:
33       unit = "bytes";
34       break;
35     case 1:
36       unit = "KB";
37       break;
38     case 2:
39       unit = "MB";
40       break;
41     case 3:
42       unit = "GB";
43       break;
44     default:
45       unit = "";
46       break;
47   }
48 
49   stream << val << " " << unit;
50   return stream.str();
51 }
52 
53 /* static */
UpdateAmount(MemoryUse action,size_t amount)54 void GfxTexturesReporter::UpdateAmount(MemoryUse action, size_t amount) {
55   if (action == MemoryFreed) {
56     MOZ_RELEASE_ASSERT(
57         amount <= sAmount,
58         "GFX: Current texture usage greater than update amount.");
59     sAmount -= amount;
60 
61     if (StaticPrefs::gfx_logging_texture_usage_enabled_AtStartup()) {
62       printf_stderr("Current texture usage: %s\n",
63                     FormatBytes(sAmount).c_str());
64     }
65   } else {
66     sAmount += amount;
67     if (sAmount > sPeakAmount) {
68       sPeakAmount.exchange(sAmount);
69       if (StaticPrefs::gfx_logging_peak_texture_usage_enabled_AtStartup()) {
70         printf_stderr("Peak texture usage: %s\n",
71                       FormatBytes(sPeakAmount).c_str());
72       }
73     }
74   }
75 
76   CrashReporter::AnnotateTexturesSize(sAmount);
77 }
78