Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / gfx / 2d / SourceSurfaceRawData.cpp
blobe8182f6c8fd4ed42fd45587012569be1ce4b7acf
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"
10 #include "Logging.h"
11 #include "mozilla/Types.h" // for decltype
12 #include "nsIMemoryReporter.h"
14 namespace mozilla {
15 namespace gfx {
17 class SourceSurfaceAlignedRawDataReporter final : public nsIMemoryReporter {
18 ~SourceSurfaceAlignedRawDataReporter() = default;
20 public:
21 NS_DECL_ISUPPORTS
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.");
28 return NS_OK;
31 private:
32 friend class SourceSurfaceAlignedRawData;
34 static Atomic<size_t> sTotalDataBytes;
37 NS_IMPL_ISUPPORTS(SourceSurfaceAlignedRawDataReporter, nsIMemoryReporter)
39 /* static */
40 Atomic<size_t> SourceSurfaceAlignedRawDataReporter::sTotalDataBytes(0);
42 /* static */
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,
50 void* aClosure) {
51 mRawData = aData;
52 mSize = aSize;
53 mStride = aStride;
54 mFormat = aFormat;
55 mDeallocator = aDeallocator;
56 mClosure = aClosure;
59 void SourceSurfaceRawData::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
60 SizeOfInfo& aInfo) const {
61 aInfo.AddType(SurfaceType::DATA);
62 if (mDeallocator) {
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) {
78 mFormat = aFormat;
79 mStride = aStride ? aStride
80 : GetAlignedStride<16>(aSize.width, BytesPerPixel(aFormat));
82 size_t bufLen = BufferSizeFromStrideAndHeight(mStride, aSize.height);
83 if (bufLen > 0) {
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);
91 mSize = aSize;
93 SourceSurfaceAlignedRawDataReporter::sTotalDataBytes += bufLen;
95 if (mArray && aClearMem && aClearValue) {
96 memset(mArray, aClearValue, mStride * aSize.height);
98 } else {
99 mArray.Dealloc();
100 mSize.SizeTo(0, 0);
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;
114 } // namespace gfx
115 } // namespace mozilla