SourceSurfaceRawData.h (4166B)
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 MOZILLA_GFX_SOURCESURFACERAWDATA_H_ 8 #define MOZILLA_GFX_SOURCESURFACERAWDATA_H_ 9 10 #include "2D.h" 11 #include "Tools.h" 12 13 namespace mozilla { 14 namespace gfx { 15 16 class SourceSurfaceMappedData final : public DataSourceSurface { 17 public: 18 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceMappedData, final) 19 20 SourceSurfaceMappedData(ScopedMap&& aMap, const IntSize& aSize, 21 SurfaceFormat aFormat) 22 : mMap(std::move(aMap)), mSize(aSize), mFormat(aFormat) {} 23 24 ~SourceSurfaceMappedData() final = default; 25 26 uint8_t* GetData() final { return mMap.GetData(); } 27 int32_t Stride() final { return mMap.GetStride(); } 28 29 SurfaceType GetType() const final { return SurfaceType::DATA_MAPPED; } 30 IntSize GetSize() const final { return mSize; } 31 SurfaceFormat GetFormat() const final { return mFormat; } 32 33 void SizeOfExcludingThis(MallocSizeOf aMallocSizeOf, 34 SizeOfInfo& aInfo) const override { 35 aInfo.AddType(SurfaceType::DATA_MAPPED); 36 mMap.GetSurface()->SizeOfExcludingThis(aMallocSizeOf, aInfo); 37 } 38 39 const DataSourceSurface* GetScopedSurface() const { 40 return mMap.GetSurface(); 41 } 42 43 private: 44 ScopedMap mMap; 45 IntSize mSize; 46 SurfaceFormat mFormat; 47 }; 48 49 class SourceSurfaceRawData : public DataSourceSurface { 50 public: 51 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceRawData, override) 52 53 SourceSurfaceRawData() 54 : mRawData(0), 55 mStride(0), 56 mFormat(SurfaceFormat::UNKNOWN), 57 mDeallocator(nullptr), 58 mClosure(nullptr) {} 59 60 virtual ~SourceSurfaceRawData() { 61 if (mDeallocator) { 62 mDeallocator(mClosure); 63 } 64 } 65 66 virtual uint8_t* GetData() override { return mRawData; } 67 virtual int32_t Stride() override { return mStride; } 68 69 virtual SurfaceType GetType() const override { return SurfaceType::DATA; } 70 virtual IntSize GetSize() const override { return mSize; } 71 virtual SurfaceFormat GetFormat() const override { return mFormat; } 72 73 void SizeOfExcludingThis(MallocSizeOf aMallocSizeOf, 74 SizeOfInfo& aInfo) const override; 75 76 private: 77 friend class Factory; 78 79 // If we have a custom deallocator, the |aData| will be released using the 80 // custom deallocator and |aClosure| in dtor. The assumption is that the 81 // caller will check for valid size and stride before making this call. 82 void InitWrappingData(unsigned char* aData, const IntSize& aSize, 83 int32_t aStride, SurfaceFormat aFormat, 84 Factory::SourceSurfaceDeallocator aDeallocator, 85 void* aClosure); 86 87 uint8_t* mRawData; 88 int32_t mStride; 89 SurfaceFormat mFormat; 90 IntSize mSize; 91 92 Factory::SourceSurfaceDeallocator mDeallocator; 93 void* mClosure; 94 }; 95 96 class SourceSurfaceAlignedRawData : public DataSourceSurface { 97 public: 98 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceAlignedRawData, 99 override) 100 SourceSurfaceAlignedRawData(); 101 virtual ~SourceSurfaceAlignedRawData() override; 102 103 bool Init(const IntSize& aSize, SurfaceFormat aFormat, bool aClearMem, 104 uint8_t aClearValue, int32_t aStride = 0); 105 106 uint8_t* GetData() override { return mArray; } 107 int32_t Stride() override { return mStride; } 108 109 SurfaceType GetType() const override { return SurfaceType::DATA_ALIGNED; } 110 IntSize GetSize() const override { return mSize; } 111 SurfaceFormat GetFormat() const override { return mFormat; } 112 113 void SizeOfExcludingThis(MallocSizeOf aMallocSizeOf, 114 SizeOfInfo& aInfo) const override; 115 116 private: 117 friend class Factory; 118 119 AlignedArray<uint8_t> mArray; 120 int32_t mStride; 121 SurfaceFormat mFormat; 122 IntSize mSize; 123 124 static void RegisterMemoryReporter(); 125 }; 126 127 } // namespace gfx 128 } // namespace mozilla 129 130 #endif /* MOZILLA_GFX_SOURCESURFACERAWDATA_H_ */