SourceSurfaceRawData.cpp (3805B)
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 "SourceSurfaceRawData.h" 8 9 #include "DataSurfaceHelpers.h" 10 #include "Logging.h" 11 #include "nsIMemoryReporter.h" 12 13 namespace mozilla { 14 namespace gfx { 15 16 class SourceSurfaceAlignedRawDataReporter final : public nsIMemoryReporter { 17 ~SourceSurfaceAlignedRawDataReporter() = default; 18 19 public: 20 NS_DECL_ISUPPORTS 21 22 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, 23 nsISupports* aData, bool aAnonymize) override { 24 MOZ_COLLECT_REPORT("explicit/gfx/source-surface-aligned-raw-data", 25 KIND_HEAP, UNITS_BYTES, sTotalDataBytes, 26 "Total memory used by source surface aligned raw data."); 27 return NS_OK; 28 } 29 30 private: 31 friend class SourceSurfaceAlignedRawData; 32 33 static Atomic<size_t> sTotalDataBytes; 34 }; 35 36 NS_IMPL_ISUPPORTS(SourceSurfaceAlignedRawDataReporter, nsIMemoryReporter) 37 38 /* static */ 39 Atomic<size_t> SourceSurfaceAlignedRawDataReporter::sTotalDataBytes(0); 40 41 /* static */ 42 void SourceSurfaceAlignedRawData::RegisterMemoryReporter() { 43 RegisterStrongMemoryReporter(new SourceSurfaceAlignedRawDataReporter); 44 } 45 46 void SourceSurfaceRawData::InitWrappingData( 47 uint8_t* aData, const IntSize& aSize, int32_t aStride, 48 SurfaceFormat aFormat, Factory::SourceSurfaceDeallocator aDeallocator, 49 void* aClosure) { 50 mRawData = aData; 51 mSize = aSize; 52 mStride = aStride; 53 mFormat = aFormat; 54 mDeallocator = aDeallocator; 55 mClosure = aClosure; 56 } 57 58 void SourceSurfaceRawData::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf, 59 SizeOfInfo& aInfo) const { 60 aInfo.AddType(SurfaceType::DATA); 61 if (mDeallocator) { 62 aInfo.mUnknownBytes = mStride * mSize.height; 63 } 64 } 65 66 SourceSurfaceAlignedRawData::SourceSurfaceAlignedRawData() 67 : mStride(0), mFormat(SurfaceFormat::UNKNOWN) {} 68 69 SourceSurfaceAlignedRawData::~SourceSurfaceAlignedRawData() { 70 size_t bufLen = BufferSizeFromStrideAndHeight(mStride, mSize.height); 71 SourceSurfaceAlignedRawDataReporter::sTotalDataBytes -= bufLen; 72 } 73 74 bool SourceSurfaceAlignedRawData::Init(const IntSize& aSize, 75 SurfaceFormat aFormat, bool aClearMem, 76 uint8_t aClearValue, int32_t aStride) { 77 mFormat = aFormat; 78 mStride = aStride ? aStride 79 : GetAlignedStride<16>(aSize.width, BytesPerPixel(aFormat)); 80 81 size_t bufLen = BufferSizeFromStrideAndHeight(mStride, aSize.height); 82 if (bufLen > 0) { 83 bool zeroMem = aClearMem && !aClearValue; 84 static_assert(sizeof(decltype(mArray[0])) == 1, 85 "mArray.Realloc() takes an object count, so its objects must " 86 "be 1-byte sized if we use bufLen"); 87 88 // AlignedArray uses cmalloc to zero mem for a fast path. 89 mArray.Realloc(/* actually an object count */ bufLen, zeroMem); 90 mSize = aSize; 91 92 SourceSurfaceAlignedRawDataReporter::sTotalDataBytes += bufLen; 93 94 if (mArray && aClearMem && aClearValue) { 95 memset(mArray, aClearValue, mStride * aSize.height); 96 } 97 } else { 98 mArray.Dealloc(); 99 mSize.SizeTo(0, 0); 100 } 101 102 return mArray != nullptr; 103 } 104 105 void SourceSurfaceAlignedRawData::SizeOfExcludingThis( 106 MallocSizeOf aMallocSizeOf, SizeOfInfo& aInfo) const { 107 aInfo.AddType(SurfaceType::DATA_ALIGNED); 108 // This memory is accounted for in aggregate by sTotalDataBytes. We return 109 // zero here to prevent double-counting. 110 aInfo.mHeapBytes = 0; 111 } 112 113 } // namespace gfx 114 } // namespace mozilla