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 #ifndef GFXTEXTURESREPORTER_H_
8 #define GFXTEXTURESREPORTER_H_
9 
10 #include "mozilla/Atomics.h"
11 #include "nsIMemoryReporter.h"
12 #include "GLTypes.h"
13 
14 namespace mozilla {
15 namespace gl {
16 
17 class GfxTexturesReporter final : public nsIMemoryReporter {
18   ~GfxTexturesReporter() = default;
19 
20  public:
21   NS_DECL_ISUPPORTS
22 
GfxTexturesReporter()23   GfxTexturesReporter() {
24 #ifdef DEBUG
25     // There must be only one instance of this class, due to |sAmount|
26     // being static.  Assert this.
27     static bool hasRun = false;
28     MOZ_ASSERT(!hasRun);
29     hasRun = true;
30 #endif
31   }
32 
33   enum MemoryUse {
34     // when memory being allocated is reported to a memory reporter
35     MemoryAllocated,
36     // when memory being freed is reported to a memory reporter
37     MemoryFreed
38   };
39 
40   // When memory is used/freed for tile textures, call this method to update
41   // the value reported by this memory reporter.
42   static void UpdateAmount(MemoryUse action, size_t amount);
43 
UpdateWasteAmount(size_t delta)44   static void UpdateWasteAmount(size_t delta) { sTileWasteAmount += delta; }
45 
CollectReports(nsIHandleReportCallback * aHandleReport,nsISupports * aData,bool aAnonymize)46   NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
47                             nsISupports* aData, bool aAnonymize) override {
48     MOZ_COLLECT_REPORT(
49         "gfx-tiles-waste", KIND_OTHER, UNITS_BYTES, int64_t(sTileWasteAmount),
50         "Memory lost due to tiles extending past content boundaries");
51 
52     MOZ_COLLECT_REPORT("gfx-textures", KIND_OTHER, UNITS_BYTES,
53                        int64_t(sAmount),
54                        "Memory used for storing GL textures.");
55 
56     MOZ_COLLECT_REPORT("gfx-textures-peak", KIND_OTHER, UNITS_BYTES,
57                        int64_t(sPeakAmount),
58                        "Peak memory used for storing GL textures.");
59 
60     return NS_OK;
61   }
62 
63  private:
64   static Atomic<size_t> sAmount;
65   static Atomic<size_t> sPeakAmount;
66   // Count the amount of memory lost to tile waste
67   static Atomic<size_t> sTileWasteAmount;
68 };
69 
70 class GfxTextureWasteTracker {
71  public:
GfxTextureWasteTracker()72   GfxTextureWasteTracker() : mBytes(0) {
73     MOZ_COUNT_CTOR(GfxTextureWasteTracker);
74   }
75 
Update(int32_t aPixelArea,int32_t aBytesPerPixel)76   void Update(int32_t aPixelArea, int32_t aBytesPerPixel) {
77     GfxTexturesReporter::UpdateWasteAmount(-mBytes);
78     mBytes = aPixelArea * aBytesPerPixel;
79     GfxTexturesReporter::UpdateWasteAmount(mBytes);
80   }
81 
~GfxTextureWasteTracker()82   ~GfxTextureWasteTracker() {
83     GfxTexturesReporter::UpdateWasteAmount(-mBytes);
84     MOZ_COUNT_DTOR(GfxTextureWasteTracker);
85   }
86 
87  private:
88   GfxTextureWasteTracker(const GfxTextureWasteTracker& aRef);
89 
90   int32_t mBytes;
91 };
92 
93 }  // namespace gl
94 }  // namespace mozilla
95 
96 #endif  // GFXTEXTURESREPORTER_H_
97