SourceSurfaceSkia.h (2148B)
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_SOURCESURFACESKIA_H_ 8 #define MOZILLA_GFX_SOURCESURFACESKIA_H_ 9 10 #include "2D.h" 11 #include "mozilla/Mutex.h" 12 #include "skia/include/core/SkRefCnt.h" 13 14 class SkImage; 15 class SkSurface; 16 17 namespace mozilla { 18 19 namespace gfx { 20 21 class DrawTargetSkia; 22 class SnapshotLock; 23 24 class SourceSurfaceSkia : public DataSourceSurface { 25 public: 26 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceSkia, override) 27 28 SourceSurfaceSkia(); 29 virtual ~SourceSurfaceSkia(); 30 31 SurfaceType GetType() const override { return SurfaceType::SKIA; } 32 IntSize GetSize() const override; 33 SurfaceFormat GetFormat() const override; 34 35 void GiveSurface(SkSurface* aSurface); 36 37 sk_sp<SkImage> GetImage(Maybe<MutexAutoLock>* aLock); 38 39 bool InitFromData(unsigned char* aData, const IntSize& aSize, int32_t aStride, 40 SurfaceFormat aFormat); 41 42 bool InitFromImage(const sk_sp<SkImage>& aImage, 43 SurfaceFormat aFormat = SurfaceFormat::UNKNOWN, 44 DrawTargetSkia* aOwner = nullptr); 45 46 already_AddRefed<SourceSurface> ExtractSubrect(const IntRect& aRect) override; 47 48 uint8_t* GetData() override; 49 50 /** 51 * The caller is responsible for ensuring aMappedSurface is not null. 52 */ 53 bool Map(MapType, MappedSurface* aMappedSurface) override; 54 55 void Unmap() override; 56 57 int32_t Stride() override { return mStride; } 58 59 private: 60 friend class DrawTargetSkia; 61 62 void DrawTargetWillChange(); 63 64 sk_sp<SkImage> mImage; 65 // This keeps a surface alive if needed because its DrawTarget has gone away. 66 sk_sp<SkSurface> mSurface; 67 SurfaceFormat mFormat; 68 IntSize mSize; 69 int32_t mStride; 70 Atomic<DrawTargetSkia*> mDrawTarget; 71 Mutex mChangeMutex MOZ_UNANNOTATED; 72 bool mIsMapped; 73 }; 74 75 } // namespace gfx 76 } // namespace mozilla 77 78 #endif /* MOZILLA_GFX_SOURCESURFACESKIA_H_ */