Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim: set ts=8 sts=4 et sw=4 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 "GfxTexturesReporter.h"
10 : #include "gfxPrefs.h"
11 :
12 : #ifdef MOZ_CRASHREPORTER
13 : #include "nsExceptionHandler.h"
14 : #endif
15 :
16 : using namespace mozilla;
17 : using namespace mozilla::gl;
18 :
19 39 : NS_IMPL_ISUPPORTS(GfxTexturesReporter, nsIMemoryReporter)
20 :
21 : Atomic<size_t> GfxTexturesReporter::sAmount(0);
22 : Atomic<size_t> GfxTexturesReporter::sPeakAmount(0);
23 : Atomic<size_t> GfxTexturesReporter::sTileWasteAmount(0);
24 :
25 : std::string
26 0 : FormatBytes(size_t amount)
27 : {
28 0 : std::stringstream stream;
29 0 : int depth = 0;
30 0 : double val = amount;
31 0 : while (val > 1024) {
32 0 : val /= 1024;
33 0 : depth++;
34 : }
35 :
36 : const char* unit;
37 0 : switch(depth) {
38 : case 0:
39 0 : unit = "bytes";
40 0 : break;
41 : case 1:
42 0 : unit = "KB";
43 0 : break;
44 : case 2:
45 0 : unit = "MB";
46 0 : break;
47 : case 3:
48 0 : unit = "GB";
49 0 : break;
50 : default:
51 0 : unit = "";
52 0 : break;
53 : }
54 :
55 0 : stream << val << " " << unit;
56 0 : return stream.str();
57 : }
58 :
59 : /* static */ void
60 0 : GfxTexturesReporter::UpdateAmount(MemoryUse action, size_t amount)
61 : {
62 0 : if (action == MemoryFreed) {
63 0 : MOZ_RELEASE_ASSERT(amount <= sAmount, "GFX: Current texture usage greater than update amount.");
64 0 : sAmount -= amount;
65 :
66 0 : if (gfxPrefs::GfxLoggingTextureUsageEnabled()) {
67 0 : printf_stderr("Current texture usage: %s\n", FormatBytes(sAmount).c_str());
68 : }
69 : } else {
70 0 : sAmount += amount;
71 0 : if (sAmount > sPeakAmount) {
72 0 : sPeakAmount.exchange(sAmount);
73 0 : if (gfxPrefs::GfxLoggingPeakTextureUsageEnabled()) {
74 0 : printf_stderr("Peak texture usage: %s\n", FormatBytes(sPeakAmount).c_str());
75 : }
76 : }
77 : }
78 :
79 : #ifdef MOZ_CRASHREPORTER
80 0 : CrashReporter::AnnotateTexturesSize(sAmount);
81 : #endif
82 9 : }
|