Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef MOZILLA_GFX_SOURCESURFACERAWDATA_H_
7 : #define MOZILLA_GFX_SOURCESURFACERAWDATA_H_
8 :
9 : #include "2D.h"
10 : #include "Tools.h"
11 : #include "mozilla/Atomics.h"
12 :
13 : namespace mozilla {
14 : namespace gfx {
15 :
16 : class SourceSurfaceRawData : public DataSourceSurface
17 : {
18 : public:
19 923 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceRawData, override)
20 :
21 83 : SourceSurfaceRawData()
22 83 : : mRawData(0)
23 : , mStride(0)
24 : , mFormat(SurfaceFormat::UNKNOWN)
25 : , mMapCount(0)
26 : , mOwnData(false)
27 : , mDeallocator(nullptr)
28 83 : , mClosure(nullptr)
29 : {
30 83 : }
31 :
32 68 : virtual ~SourceSurfaceRawData()
33 68 : {
34 34 : if (mDeallocator) {
35 28 : mDeallocator(mClosure);
36 6 : } else if (mOwnData) {
37 : // The buffer is created from GuaranteePersistance().
38 0 : delete [] mRawData;
39 : }
40 :
41 34 : MOZ_ASSERT(mMapCount == 0);
42 102 : }
43 :
44 377 : virtual uint8_t *GetData() override { return mRawData; }
45 186 : virtual int32_t Stride() override { return mStride; }
46 :
47 225 : virtual SurfaceType GetType() const override { return SurfaceType::DATA; }
48 529 : virtual IntSize GetSize() const override { return mSize; }
49 333 : virtual SurfaceFormat GetFormat() const override { return mFormat; }
50 :
51 : virtual void GuaranteePersistance() override;
52 :
53 : // Althought Map (and Moz2D in general) isn't normally threadsafe,
54 : // we want to allow it for SourceSurfaceRawData since it should
55 : // always be fine (for reading at least).
56 : //
57 : // This is the same as the base class implementation except using
58 : // mMapCount instead of mIsMapped since that breaks for multithread.
59 : //
60 : // Once mfbt supports Monitors we should implement proper read/write
61 : // locking to prevent write races.
62 0 : virtual bool Map(MapType, MappedSurface *aMappedSurface) override
63 : {
64 0 : aMappedSurface->mData = GetData();
65 0 : aMappedSurface->mStride = Stride();
66 0 : bool success = !!aMappedSurface->mData;
67 0 : if (success) {
68 0 : mMapCount++;
69 : }
70 0 : return success;
71 : }
72 :
73 0 : virtual void Unmap() override
74 : {
75 0 : mMapCount--;
76 0 : MOZ_ASSERT(mMapCount >= 0);
77 0 : }
78 :
79 : private:
80 : friend class Factory;
81 :
82 : // If we have a custom deallocator, the |aData| will be released using the
83 : // custom deallocator and |aClosure| in dtor. The assumption is that the
84 : // caller will check for valid size and stride before making this call.
85 : void InitWrappingData(unsigned char *aData,
86 : const IntSize &aSize,
87 : int32_t aStride,
88 : 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 : Atomic<int32_t> mMapCount;
97 :
98 : bool mOwnData;
99 : Factory::SourceSurfaceDeallocator mDeallocator;
100 : void* mClosure;
101 : };
102 :
103 : class SourceSurfaceAlignedRawData : public DataSourceSurface
104 : {
105 : public:
106 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceAlignedRawData, override)
107 0 : SourceSurfaceAlignedRawData()
108 0 : : mStride(0)
109 : , mFormat(SurfaceFormat::UNKNOWN)
110 0 : , mMapCount(0)
111 0 : {}
112 0 : ~SourceSurfaceAlignedRawData()
113 0 : {
114 0 : MOZ_ASSERT(mMapCount == 0);
115 0 : }
116 :
117 0 : virtual uint8_t* GetData() override { return mArray; }
118 0 : virtual int32_t Stride() override { return mStride; }
119 :
120 0 : virtual SurfaceType GetType() const override { return SurfaceType::DATA; }
121 0 : virtual IntSize GetSize() const override { return mSize; }
122 0 : virtual SurfaceFormat GetFormat() const override { return mFormat; }
123 :
124 0 : virtual bool Map(MapType, MappedSurface *aMappedSurface) override
125 : {
126 0 : aMappedSurface->mData = GetData();
127 0 : aMappedSurface->mStride = Stride();
128 0 : bool success = !!aMappedSurface->mData;
129 0 : if (success) {
130 0 : mMapCount++;
131 : }
132 0 : return success;
133 : }
134 :
135 0 : virtual void Unmap() override
136 : {
137 0 : mMapCount--;
138 0 : MOZ_ASSERT(mMapCount >= 0);
139 0 : }
140 :
141 : private:
142 : friend class Factory;
143 :
144 : bool Init(const IntSize &aSize,
145 : SurfaceFormat aFormat,
146 : bool aClearMem,
147 : uint8_t aClearValue,
148 : int32_t aStride = 0);
149 :
150 : AlignedArray<uint8_t> mArray;
151 : int32_t mStride;
152 : SurfaceFormat mFormat;
153 : IntSize mSize;
154 : Atomic<int32_t> mMapCount;
155 : };
156 :
157 : } // namespace gfx
158 : } // namespace mozilla
159 :
160 : #endif /* MOZILLA_GFX_SOURCESURFACERAWDATA_H_ */
|