SourceSurfaceWebgl.h (2902B)
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_SOURCESURFACEWEBGL_H_ 8 #define MOZILLA_GFX_SOURCESURFACEWEBGL_H_ 9 10 #include "mozilla/WeakPtr.h" 11 #include "mozilla/gfx/2D.h" 12 13 namespace mozilla { 14 class WebGLBuffer; 15 } // namespace mozilla 16 17 namespace mozilla::gfx { 18 19 class DrawTargetWebgl; 20 class SharedContextWebgl; 21 class TextureHandle; 22 23 // SourceSurfaceWebgl holds WebGL resources that can be used to efficiently 24 // copy snapshot data between multiple DrawTargetWebgls. It also takes care 25 // of copy-on-write behavior when the owner target is modified or destructs. 26 class SourceSurfaceWebgl : public DataSourceSurface { 27 public: 28 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceWebgl, override) 29 30 explicit SourceSurfaceWebgl(DrawTargetWebgl* aDT); 31 virtual ~SourceSurfaceWebgl(); 32 33 SurfaceType GetType() const override { return SurfaceType::WEBGL; } 34 IntSize GetSize() const override { return mSize; } 35 SurfaceFormat GetFormat() const override { return mFormat; } 36 37 uint8_t* GetData() override; 38 int32_t Stride() override; 39 40 bool Map(MapType aType, MappedSurface* aMappedSurface) override; 41 void Unmap() override; 42 43 bool HasReadData() const { return !!mData; } 44 45 already_AddRefed<SourceSurface> ExtractSubrect(const IntRect& aRect) override; 46 47 bool ReadDataInto(uint8_t* aData, int32_t aStride) override; 48 49 private: 50 friend class DrawTargetWebgl; 51 friend class SharedContextWebgl; 52 53 explicit SourceSurfaceWebgl(const RefPtr<SharedContextWebgl>& aSharedContext); 54 55 bool EnsureData(bool aForce = true, uint8_t* aData = nullptr, 56 int32_t aStride = 0); 57 bool ForceReadFromPBO(); 58 59 void DrawTargetWillChange(bool aNeedHandle); 60 61 void GiveTexture(RefPtr<TextureHandle> aHandle); 62 63 void SetHandle(TextureHandle* aHandle); 64 65 void OnUnlinkTexture(SharedContextWebgl* aContext, TextureHandle* aHandle, 66 bool aForce); 67 68 DrawTargetWebgl* GetTarget() const { return mDT.get(); } 69 70 SurfaceFormat mFormat = SurfaceFormat::UNKNOWN; 71 IntSize mSize; 72 RefPtr<WebGLBuffer> mReadBuffer; 73 // Any data that has been read back from the WebGL context for mapping. 74 RefPtr<DataSourceSurface> mData; 75 // Whether the data is safe to modify 76 bool mOwnsData = true; 77 // The draw target that currently owns the texture for this surface. 78 WeakPtr<DrawTargetWebgl> mDT; 79 // The actual shared context that any WebGL resources belong to. 80 WeakPtr<SharedContextWebgl> mSharedContext; 81 // If this snapshot has been copied into a cached texture handle. 82 RefPtr<TextureHandle> mHandle; 83 }; 84 85 } // namespace mozilla::gfx 86 87 #endif /* MOZILLA_GFX_SOURCESURFACEWEBGL_H_ */