GfxTexturesReporter.h (2975B)
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 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 44 static void UpdateWasteAmount(int32_t delta) { 45 if (delta >= 0) { 46 sTileWasteAmount += static_cast<size_t>(delta); 47 } else { 48 sTileWasteAmount -= static_cast<size_t>(-delta); 49 } 50 } 51 52 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, 53 nsISupports* aData, bool aAnonymize) override { 54 MOZ_COLLECT_REPORT( 55 "gfx-tiles-waste", KIND_OTHER, UNITS_BYTES, int64_t(sTileWasteAmount), 56 "Memory lost due to tiles extending past content boundaries"); 57 58 MOZ_COLLECT_REPORT("gfx-textures", KIND_OTHER, UNITS_BYTES, 59 int64_t(sAmount), 60 "Memory used for storing GL textures."); 61 62 MOZ_COLLECT_REPORT("gfx-textures-peak", KIND_OTHER, UNITS_BYTES, 63 int64_t(sPeakAmount), 64 "Peak memory used for storing GL textures."); 65 66 return NS_OK; 67 } 68 69 private: 70 static Atomic<size_t> sAmount; 71 static Atomic<size_t> sPeakAmount; 72 // Count the amount of memory lost to tile waste 73 static Atomic<size_t> sTileWasteAmount; 74 }; 75 76 class GfxTextureWasteTracker { 77 public: 78 GfxTextureWasteTracker() : mBytes(0) { 79 MOZ_COUNT_CTOR(GfxTextureWasteTracker); 80 } 81 82 void Update(int32_t aPixelArea, int32_t aBytesPerPixel) { 83 GfxTexturesReporter::UpdateWasteAmount(-mBytes); 84 mBytes = aPixelArea * aBytesPerPixel; 85 GfxTexturesReporter::UpdateWasteAmount(mBytes); 86 } 87 88 ~GfxTextureWasteTracker() { 89 GfxTexturesReporter::UpdateWasteAmount(-mBytes); 90 MOZ_COUNT_DTOR(GfxTextureWasteTracker); 91 } 92 93 private: 94 GfxTextureWasteTracker(const GfxTextureWasteTracker& aRef); 95 96 int32_t mBytes; 97 }; 98 99 } // namespace gl 100 } // namespace mozilla 101 102 #endif // GFXTEXTURESREPORTER_H_