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/. */
7 #ifndef MOZILLA_GFX_SOURCESURFACEVOLATILEDATA_H_
8 #define MOZILLA_GFX_SOURCESURFACEVOLATILEDATA_H_
10 #include "mozilla/gfx/2D.h"
11 #include "mozilla/Mutex.h"
12 #include "mozilla/VolatileBuffer.h"
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
26 class SourceSurfaceVolatileData
: public DataSourceSurface
{
28 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceVolatileData
, override
)
30 SourceSurfaceVolatileData()
31 : mMutex("SourceSurfaceVolatileData"),
33 mFormat(SurfaceFormat::UNKNOWN
),
36 bool Init(const IntSize
& aSize
, int32_t aStride
, SurfaceFormat aFormat
);
38 uint8_t* GetData() override
{ return mVBufPtr
; }
39 int32_t Stride() override
{ return mStride
; }
41 SurfaceType
GetType() const override
{ return SurfaceType::DATA
; }
42 IntSize
GetSize() const override
{ return mSize
; }
43 SurfaceFormat
GetFormat() const override
{ return mFormat
; }
45 void GuaranteePersistance() override
;
47 void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf
, size_t& aHeapSizeOut
,
48 size_t& aNonHeapSizeOut
, size_t& aExtHandlesOut
,
49 uint64_t& aExtIdOut
) const override
;
51 bool OnHeap() const override
{ return mVBuf
->OnHeap(); }
53 // Althought Map (and Moz2D in general) isn't normally threadsafe,
54 // we want to allow it for SourceSurfaceVolatileData since it should
55 // always be fine (for reading at least).
57 // This is the same as the base class implementation except using
58 // mMapCount instead of mIsMapped since that breaks for multithread.
59 bool Map(MapType
, MappedSurface
* aMappedSurface
) override
{
60 MutexAutoLock
lock(mMutex
);
67 if (mVBufPtr
.WasBufferPurged()) {
71 aMappedSurface
->mData
= mVBufPtr
;
72 aMappedSurface
->mStride
= mStride
;
77 void Unmap() override
{
78 MutexAutoLock
lock(mMutex
);
79 MOZ_ASSERT(mMapCount
> 0);
80 MOZ_ASSERT(!mWasPurged
);
81 if (--mMapCount
== 0) {
87 ~SourceSurfaceVolatileData() override
{}
92 RefPtr
<VolatileBuffer
> mVBuf
;
93 VolatileBufferPtr
<uint8_t> mVBufPtr
;
94 SurfaceFormat mFormat
;
99 } // namespace mozilla
101 #endif /* MOZILLA_GFX_SOURCESURFACEVOLATILEDATA_H_ */