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