Bug 1521243 - Show a warning for invalid declarations and filter icon for overridden...
[gecko.git] / gfx / layers / SourceSurfaceVolatileData.h
blobb7d45de2d56f02f1987d2b2685c6002847efa274
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"
14 namespace mozilla {
15 namespace gfx {
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.
26 class SourceSurfaceVolatileData : public DataSourceSurface {
27 public:
28 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceVolatileData, override)
30 SourceSurfaceVolatileData()
31 : mMutex("SourceSurfaceVolatileData"),
32 mStride(0),
33 mFormat(SurfaceFormat::UNKNOWN),
34 mWasPurged(false) {}
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);
61 if (mWasPurged) {
62 return false;
64 if (mMapCount == 0) {
65 mVBufPtr = mVBuf;
67 if (mVBufPtr.WasBufferPurged()) {
68 mWasPurged = true;
69 return false;
71 aMappedSurface->mData = mVBufPtr;
72 aMappedSurface->mStride = mStride;
73 ++mMapCount;
74 return true;
77 void Unmap() override {
78 MutexAutoLock lock(mMutex);
79 MOZ_ASSERT(mMapCount > 0);
80 MOZ_ASSERT(!mWasPurged);
81 if (--mMapCount == 0) {
82 mVBufPtr = nullptr;
86 private:
87 ~SourceSurfaceVolatileData() override {}
89 Mutex mMutex;
90 int32_t mStride;
91 IntSize mSize;
92 RefPtr<VolatileBuffer> mVBuf;
93 VolatileBufferPtr<uint8_t> mVBufPtr;
94 SurfaceFormat mFormat;
95 bool mWasPurged;
98 } // namespace gfx
99 } // namespace mozilla
101 #endif /* MOZILLA_GFX_SOURCESURFACEVOLATILEDATA_H_ */