/src/mozilla-central/gfx/layers/SourceSurfaceVolatileData.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_SOURCESURFACEVOLATILEDATA_H_ |
8 | | #define MOZILLA_GFX_SOURCESURFACEVOLATILEDATA_H_ |
9 | | |
10 | | #include "mozilla/gfx/2D.h" |
11 | | #include "mozilla/Mutex.h" |
12 | | #include "mozilla/VolatileBuffer.h" |
13 | | |
14 | | namespace mozilla { |
15 | | namespace gfx { |
16 | | |
17 | | /** |
18 | | * This class is used to wrap volatile data buffers used for source surfaces. |
19 | | * The Map and Unmap semantics are used to guarantee that the volatile data |
20 | | * buffer is not freed by the operating system while the surface is in active |
21 | | * use. If GetData is expected to return a non-null value without a |
22 | | * corresponding Map call (and verification of the result), the surface data |
23 | | * should be wrapped in a temporary SourceSurfaceRawData with a ScopedMap |
24 | | * closure. |
25 | | */ |
26 | | class SourceSurfaceVolatileData : public DataSourceSurface |
27 | | { |
28 | | public: |
29 | | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceVolatileData, override) |
30 | | |
31 | | SourceSurfaceVolatileData() |
32 | | : mMutex("SourceSurfaceVolatileData") |
33 | | , mStride(0) |
34 | | , mMapCount(0) |
35 | | , mFormat(SurfaceFormat::UNKNOWN) |
36 | | , mWasPurged(false) |
37 | | { |
38 | | } |
39 | | |
40 | | bool Init(const IntSize &aSize, |
41 | | int32_t aStride, |
42 | | SurfaceFormat aFormat); |
43 | | |
44 | 0 | uint8_t *GetData() override { return mVBufPtr; } |
45 | 0 | int32_t Stride() override { return mStride; } |
46 | | |
47 | 0 | SurfaceType GetType() const override { return SurfaceType::DATA; } |
48 | 0 | IntSize GetSize() const override { return mSize; } |
49 | 0 | SurfaceFormat GetFormat() const override { return mFormat; } |
50 | | |
51 | | void GuaranteePersistance() override; |
52 | | |
53 | | void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf, |
54 | | size_t& aHeapSizeOut, |
55 | | size_t& aNonHeapSizeOut, |
56 | | size_t& aExtHandlesOut) const override; |
57 | | |
58 | | bool OnHeap() const override |
59 | 0 | { |
60 | 0 | return mVBuf->OnHeap(); |
61 | 0 | } |
62 | | |
63 | | // Althought Map (and Moz2D in general) isn't normally threadsafe, |
64 | | // we want to allow it for SourceSurfaceVolatileData since it should |
65 | | // always be fine (for reading at least). |
66 | | // |
67 | | // This is the same as the base class implementation except using |
68 | | // mMapCount instead of mIsMapped since that breaks for multithread. |
69 | | bool Map(MapType, MappedSurface *aMappedSurface) override |
70 | 0 | { |
71 | 0 | MutexAutoLock lock(mMutex); |
72 | 0 | if (mWasPurged) { |
73 | 0 | return false; |
74 | 0 | } |
75 | 0 | if (mMapCount == 0) { |
76 | 0 | mVBufPtr = mVBuf; |
77 | 0 | } |
78 | 0 | if (mVBufPtr.WasBufferPurged()) { |
79 | 0 | mWasPurged = true; |
80 | 0 | return false; |
81 | 0 | } |
82 | 0 | aMappedSurface->mData = mVBufPtr; |
83 | 0 | aMappedSurface->mStride = mStride; |
84 | 0 | ++mMapCount; |
85 | 0 | return true; |
86 | 0 | } |
87 | | |
88 | | void Unmap() override |
89 | 0 | { |
90 | 0 | MutexAutoLock lock(mMutex); |
91 | 0 | MOZ_ASSERT(mMapCount > 0); |
92 | 0 | MOZ_ASSERT(!mWasPurged); |
93 | 0 | if (--mMapCount == 0) { |
94 | 0 | mVBufPtr = nullptr; |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | private: |
99 | | ~SourceSurfaceVolatileData() override |
100 | 0 | { |
101 | 0 | MOZ_ASSERT(mMapCount == 0); |
102 | 0 | } |
103 | | |
104 | | Mutex mMutex; |
105 | | int32_t mStride; |
106 | | int32_t mMapCount; |
107 | | IntSize mSize; |
108 | | RefPtr<VolatileBuffer> mVBuf; |
109 | | VolatileBufferPtr<uint8_t> mVBufPtr; |
110 | | SurfaceFormat mFormat; |
111 | | bool mWasPurged; |
112 | | }; |
113 | | |
114 | | } // namespace gfx |
115 | | } // namespace mozilla |
116 | | |
117 | | #endif /* MOZILLA_GFX_SOURCESURFACEVOLATILEDATA_H_ */ |