Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / image / BlobSurfaceProvider.h
blob6515bb2af4489c573ebcf0f11791e2d88d68a4a6
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_IMAGE_BLOBSURFACEPROVIDER_H_
8 #define MOZILLA_IMAGE_BLOBSURFACEPROVIDER_H_
10 #include "mozilla/Maybe.h"
11 #include "mozilla/SVGImageContext.h"
12 #include "mozilla/gfx/2D.h"
13 #include "mozilla/layers/WebRenderLayerManager.h"
14 #include "ImageRegion.h"
15 #include "ISurfaceProvider.h"
17 #include <vector>
19 namespace mozilla {
20 namespace image {
22 class BlobImageKeyData final {
23 public:
24 BlobImageKeyData(layers::WebRenderLayerManager* aManager,
25 const wr::BlobImageKey& aBlobKey,
26 std::vector<RefPtr<gfx::ScaledFont>>&& aScaledFonts,
27 std::vector<RefPtr<gfx::SourceSurface>>&& aExternalSurfaces)
28 : mManager(aManager),
29 mBlobKey(aBlobKey),
30 mScaledFonts(std::move(aScaledFonts)),
31 mExternalSurfaces(std::move(aExternalSurfaces)),
32 mDirty(false) {}
34 BlobImageKeyData(BlobImageKeyData&& aOther) noexcept
35 : mManager(std::move(aOther.mManager)),
36 mBlobKey(aOther.mBlobKey),
37 mScaledFonts(std::move(aOther.mScaledFonts)),
38 mExternalSurfaces(std::move(aOther.mExternalSurfaces)),
39 mDirty(aOther.mDirty) {}
41 BlobImageKeyData& operator=(BlobImageKeyData&& aOther) noexcept {
42 mManager = std::move(aOther.mManager);
43 mBlobKey = aOther.mBlobKey;
44 mScaledFonts = std::move(aOther.mScaledFonts);
45 mExternalSurfaces = std::move(aOther.mExternalSurfaces);
46 mDirty = aOther.mDirty;
47 return *this;
50 BlobImageKeyData(const BlobImageKeyData&) = delete;
51 BlobImageKeyData& operator=(const BlobImageKeyData&) = delete;
53 RefPtr<layers::WebRenderLayerManager> mManager;
54 wr::BlobImageKey mBlobKey;
55 std::vector<RefPtr<gfx::ScaledFont>> mScaledFonts;
56 std::vector<RefPtr<gfx::SourceSurface>> mExternalSurfaces;
57 bool mDirty;
60 } // namespace image
61 } // namespace mozilla
63 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(mozilla::image::BlobImageKeyData);
65 namespace mozilla {
66 namespace wr {
67 class IpcResourceUpdateQueue;
68 } // namespace wr
70 namespace image {
71 class SVGDocumentWrapper;
73 /**
74 * An ISurfaceProvider that manages blob recordings of SVG images. Unlike the
75 * rasterized ISurfaceProviders, it only provides a recording which may be
76 * replayed in the compositor process by WebRender. It may be invalidated
77 * directly in order to reuse the resource ids and underlying buffers when the
78 * SVG image has changed (e.g. it is animated).
80 class BlobSurfaceProvider final : public ISurfaceProvider {
81 public:
82 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BlobSurfaceProvider, override)
84 BlobSurfaceProvider(ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
85 SVGDocumentWrapper* aSVGDocumentWrapper,
86 uint32_t aImageFlags);
88 bool IsFinished() const override { return true; }
90 size_t LogicalSizeInBytes() const override {
91 const gfx::IntSize& size = GetSurfaceKey().Size();
92 return size.width * size.height * sizeof(uint32_t);
95 nsresult UpdateKey(layers::RenderRootStateManager* aManager,
96 wr::IpcResourceUpdateQueue& aResources,
97 wr::ImageKey& aKey) override;
99 void InvalidateRecording() override;
101 void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
102 const AddSizeOfCb& aCallback) override {
103 AddSizeOfCbData metadata;
104 metadata.mFinished = true;
105 metadata.mHeapBytes += mKeys.ShallowSizeOfExcludingThis(aMallocSizeOf);
107 gfx::SourceSurface::SizeOfInfo info;
108 info.AddType(gfx::SurfaceType::BLOB_IMAGE);
109 metadata.Accumulate(info);
111 aCallback(metadata);
114 protected:
115 DrawableFrameRef DrawableRef(size_t aFrame) override {
116 MOZ_ASSERT_UNREACHABLE("BlobSurfaceProvider::DrawableRef not supported!");
117 return DrawableFrameRef();
119 bool IsLocked() const override { return true; }
120 void SetLocked(bool) override {}
122 private:
123 ~BlobSurfaceProvider() override;
125 Maybe<BlobImageKeyData> RecordDrawing(layers::WebRenderLayerManager* aManager,
126 wr::IpcResourceUpdateQueue& aResources,
127 Maybe<wr::BlobImageKey> aBlobKey);
129 static void DestroyKeys(const AutoTArray<BlobImageKeyData, 1>& aKeys);
131 AutoTArray<BlobImageKeyData, 1> mKeys;
133 RefPtr<image::SVGDocumentWrapper> mSVGDocumentWrapper;
134 uint32_t mImageFlags;
137 } // namespace image
138 } // namespace mozilla
140 #endif /* MOZILLA_IMAGE_BLOBSURFACEPROVIDER_H_ */