Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/2d/SourceSurfaceRawData.h
Line
Count
Source (jump to first uncovered line)
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 SourceSurfaceRawData : public DataSourceSurface
18
{
19
public:
20
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceRawData, override)
21
22
  SourceSurfaceRawData()
23
    : mRawData(0)
24
    , mStride(0)
25
    , mFormat(SurfaceFormat::UNKNOWN)
26
    , mMapCount(0)
27
    , mOwnData(false)
28
    , mDeallocator(nullptr)
29
    , mClosure(nullptr)
30
0
  {
31
0
  }
32
33
  virtual ~SourceSurfaceRawData()
34
0
  {
35
0
    if (mDeallocator) {
36
0
      mDeallocator(mClosure);
37
0
    } else if (mOwnData) {
38
0
      // The buffer is created from GuaranteePersistance().
39
0
      delete [] mRawData;
40
0
    }
41
0
42
0
    MOZ_ASSERT(mMapCount == 0);
43
0
  }
44
45
0
  virtual uint8_t *GetData() override { return mRawData; }
46
0
  virtual int32_t Stride() override { return mStride; }
47
48
0
  virtual SurfaceType GetType() const override { return SurfaceType::DATA; }
49
0
  virtual IntSize GetSize() const override { return mSize; }
50
0
  virtual SurfaceFormat GetFormat() const override { return mFormat; }
51
52
  virtual void GuaranteePersistance() override;
53
54
  // Althought Map (and Moz2D in general) isn't normally threadsafe,
55
  // we want to allow it for SourceSurfaceRawData since it should
56
  // always be fine (for reading at least).
57
  //
58
  // This is the same as the base class implementation except using
59
  // mMapCount instead of mIsMapped since that breaks for multithread.
60
  //
61
  // Once mfbt supports Monitors we should implement proper read/write
62
  // locking to prevent write races.
63
  virtual bool Map(MapType, MappedSurface *aMappedSurface) override
64
0
  {
65
0
    aMappedSurface->mData = GetData();
66
0
    aMappedSurface->mStride = Stride();
67
0
    bool success = !!aMappedSurface->mData;
68
0
    if (success) {
69
0
      mMapCount++;
70
0
    }
71
0
    return success;
72
0
  }
73
74
  virtual void Unmap() override
75
0
  {
76
0
    mMapCount--;
77
0
    MOZ_ASSERT(mMapCount >= 0);
78
0
  }
79
80
private:
81
  friend class Factory;
82
83
  // If we have a custom deallocator, the |aData| will be released using the
84
  // custom deallocator and |aClosure| in dtor.  The assumption is that the
85
  // caller will check for valid size and stride before making this call.
86
  void InitWrappingData(unsigned char *aData,
87
                        const IntSize &aSize,
88
                        int32_t aStride,
89
                        SurfaceFormat aFormat,
90
                        Factory::SourceSurfaceDeallocator aDeallocator,
91
                        void* aClosure);
92
93
  uint8_t *mRawData;
94
  int32_t mStride;
95
  SurfaceFormat mFormat;
96
  IntSize mSize;
97
  Atomic<int32_t> mMapCount;
98
99
  bool mOwnData;
100
  Factory::SourceSurfaceDeallocator mDeallocator;
101
  void* mClosure;
102
};
103
104
class SourceSurfaceAlignedRawData : public DataSourceSurface
105
{
106
public:
107
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceAlignedRawData, override)
108
  SourceSurfaceAlignedRawData()
109
    : mStride(0)
110
    , mFormat(SurfaceFormat::UNKNOWN)
111
    , mMapCount(0)
112
0
  {}
113
  ~SourceSurfaceAlignedRawData()
114
0
  {
115
0
    MOZ_ASSERT(mMapCount == 0);
116
0
  }
117
118
  bool Init(const IntSize &aSize,
119
            SurfaceFormat aFormat,
120
            bool aClearMem,
121
            uint8_t aClearValue,
122
            int32_t aStride = 0);
123
124
0
  virtual uint8_t* GetData() override { return mArray; }
125
0
  virtual int32_t Stride() override { return mStride; }
126
127
0
  virtual SurfaceType GetType() const override { return SurfaceType::DATA; }
128
0
  virtual IntSize GetSize() const override { return mSize; }
129
0
  virtual SurfaceFormat GetFormat() const override { return mFormat; }
130
131
  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
132
                              size_t& aHeapSizeOut,
133
                              size_t& aNonHeapSizeOut,
134
                              size_t& aExtHandlesOut) const override;
135
136
  virtual bool Map(MapType, MappedSurface *aMappedSurface) override
137
0
  {
138
0
    aMappedSurface->mData = GetData();
139
0
    aMappedSurface->mStride = Stride();
140
0
    bool success = !!aMappedSurface->mData;
141
0
    if (success) {
142
0
      mMapCount++;
143
0
    }
144
0
    return success;
145
0
  }
146
147
  virtual void Unmap() override
148
0
  {
149
0
    mMapCount--;
150
0
    MOZ_ASSERT(mMapCount >= 0);
151
0
  }
152
153
private:
154
  friend class Factory;
155
156
  AlignedArray<uint8_t> mArray;
157
  int32_t mStride;
158
  SurfaceFormat mFormat;
159
  IntSize mSize;
160
  Atomic<int32_t> mMapCount;
161
};
162
163
} // namespace gfx
164
} // namespace mozilla
165
166
#endif /* MOZILLA_GFX_SOURCESURFACERAWDATA_H_ */