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