NativeFontResource.cpp (1940B)
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 "2D.h" 8 #include "nsIMemoryReporter.h" 9 10 namespace mozilla { 11 namespace gfx { 12 13 static Atomic<size_t> gTotalNativeFontResourceData; 14 15 NativeFontResource::NativeFontResource(size_t aDataLength) 16 : mDataLength(aDataLength) { 17 gTotalNativeFontResourceData += mDataLength; 18 } 19 20 NativeFontResource::~NativeFontResource() { 21 gTotalNativeFontResourceData -= mDataLength; 22 } 23 24 // Memory reporter that estimates the amount of memory that is currently being 25 // allocated internally by various native font APIs for native font resources. 26 // The sanest way to do this, given that NativeFontResources can be created and 27 // used in many different threads or processes and given that such memory is 28 // implicitly allocated by the native APIs, is just to maintain a global atomic 29 // counter and report this value as such. 30 class NativeFontResourceDataMemoryReporter final : public nsIMemoryReporter { 31 ~NativeFontResourceDataMemoryReporter() = default; 32 33 public: 34 NS_DECL_ISUPPORTS 35 36 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, 37 nsISupports* aData, bool aAnonymize) override { 38 MOZ_COLLECT_REPORT("explicit/gfx/native-font-resource-data", KIND_HEAP, 39 UNITS_BYTES, gTotalNativeFontResourceData, 40 "Total memory used by native font API resource data."); 41 return NS_OK; 42 } 43 }; 44 45 NS_IMPL_ISUPPORTS(NativeFontResourceDataMemoryReporter, nsIMemoryReporter) 46 47 void NativeFontResource::RegisterMemoryReporter() { 48 RegisterStrongMemoryReporter(new NativeFontResourceDataMemoryReporter); 49 } 50 51 } // namespace gfx 52 } // namespace mozilla