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/. */
7 #include "SourceSurfaceRawData.h"
9 #include "DataSurfaceHelpers.h"
11 #include "mozilla/Types.h" // for decltype
12 #include "nsIMemoryReporter.h"
17 class SourceSurfaceAlignedRawDataReporter final
: public nsIMemoryReporter
{
18 ~SourceSurfaceAlignedRawDataReporter() = default;
23 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
24 nsISupports
* aData
, bool aAnonymize
) override
{
25 MOZ_COLLECT_REPORT("explicit/gfx/source-surface-aligned-raw-data",
26 KIND_HEAP
, UNITS_BYTES
, sTotalDataBytes
,
27 "Total memory used by source surface aligned raw data.");
32 friend class SourceSurfaceAlignedRawData
;
34 static Atomic
<size_t> sTotalDataBytes
;
37 NS_IMPL_ISUPPORTS(SourceSurfaceAlignedRawDataReporter
, nsIMemoryReporter
)
40 Atomic
<size_t> SourceSurfaceAlignedRawDataReporter::sTotalDataBytes(0);
43 void SourceSurfaceAlignedRawData::RegisterMemoryReporter() {
44 RegisterStrongMemoryReporter(new SourceSurfaceAlignedRawDataReporter
);
47 void SourceSurfaceRawData::InitWrappingData(
48 uint8_t* aData
, const IntSize
& aSize
, int32_t aStride
,
49 SurfaceFormat aFormat
, Factory::SourceSurfaceDeallocator aDeallocator
,
55 mDeallocator
= aDeallocator
;
59 void SourceSurfaceRawData::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
,
60 SizeOfInfo
& aInfo
) const {
61 aInfo
.AddType(SurfaceType::DATA
);
63 aInfo
.mUnknownBytes
= mStride
* mSize
.height
;
67 SourceSurfaceAlignedRawData::SourceSurfaceAlignedRawData()
68 : mStride(0), mFormat(SurfaceFormat::UNKNOWN
) {}
70 SourceSurfaceAlignedRawData::~SourceSurfaceAlignedRawData() {
71 size_t bufLen
= BufferSizeFromStrideAndHeight(mStride
, mSize
.height
);
72 SourceSurfaceAlignedRawDataReporter::sTotalDataBytes
-= bufLen
;
75 bool SourceSurfaceAlignedRawData::Init(const IntSize
& aSize
,
76 SurfaceFormat aFormat
, bool aClearMem
,
77 uint8_t aClearValue
, int32_t aStride
) {
79 mStride
= aStride
? aStride
80 : GetAlignedStride
<16>(aSize
.width
, BytesPerPixel(aFormat
));
82 size_t bufLen
= BufferSizeFromStrideAndHeight(mStride
, aSize
.height
);
84 bool zeroMem
= aClearMem
&& !aClearValue
;
85 static_assert(sizeof(decltype(mArray
[0])) == 1,
86 "mArray.Realloc() takes an object count, so its objects must "
87 "be 1-byte sized if we use bufLen");
89 // AlignedArray uses cmalloc to zero mem for a fast path.
90 mArray
.Realloc(/* actually an object count */ bufLen
, zeroMem
);
93 SourceSurfaceAlignedRawDataReporter::sTotalDataBytes
+= bufLen
;
95 if (mArray
&& aClearMem
&& aClearValue
) {
96 memset(mArray
, aClearValue
, mStride
* aSize
.height
);
103 return mArray
!= nullptr;
106 void SourceSurfaceAlignedRawData::SizeOfExcludingThis(
107 MallocSizeOf aMallocSizeOf
, SizeOfInfo
& aInfo
) const {
108 aInfo
.AddType(SurfaceType::DATA_ALIGNED
);
109 // This memory is accounted for in aggregate by sTotalDataBytes. We return
110 // zero here to prevent double-counting.
111 aInfo
.mHeapBytes
= 0;
115 } // namespace mozilla