Bug 1686495 [wpt PR 27132] - Add tests for proposed WebDriver Shadow DOM support...
[gecko.git] / image / VectorImage.h
blobd0a8ab42d232de3eb744e387fdf510c9239a3a7a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_image_VectorImage_h
7 #define mozilla_image_VectorImage_h
9 #include "Image.h"
10 #include "nsIStreamListener.h"
11 #include "mozilla/gfx/Point.h"
12 #include "mozilla/MemoryReporting.h"
14 class nsIRequest;
15 class gfxDrawable;
17 namespace mozilla {
18 struct MediaFeatureChange;
20 namespace image {
22 struct SVGDrawingParameters;
23 class SVGDocumentWrapper;
24 class SVGRootRenderingObserver;
25 class SVGLoadEventListener;
26 class SVGParseCompleteListener;
28 class VectorImage final : public ImageResource, public nsIStreamListener {
29 public:
30 NS_DECL_ISUPPORTS
31 NS_DECL_NSIREQUESTOBSERVER
32 NS_DECL_NSISTREAMLISTENER
33 NS_DECL_IMGICONTAINER
35 // (no public constructor - use ImageFactory)
37 // Methods inherited from Image
38 void MediaFeatureValuesChangedAllDocuments(const MediaFeatureChange&) final;
39 nsresult GetNativeSizes(nsTArray<gfx::IntSize>& aNativeSizes) const override;
40 size_t GetNativeSizesLength() const override;
41 virtual size_t SizeOfSourceWithComputedFallback(
42 SizeOfState& aState) const override;
43 virtual void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
44 MallocSizeOf aMallocSizeOf) const override;
46 virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
47 nsISupports* aContext,
48 nsIInputStream* aInStr,
49 uint64_t aSourceOffset,
50 uint32_t aCount) override;
51 virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
52 nsISupports* aContext, nsresult aResult,
53 bool aLastPart) override;
55 virtual void OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) override;
57 /**
58 * Callback for SVGRootRenderingObserver.
60 * This just sets a dirty flag that we check in VectorImage::RequestRefresh,
61 * which is called under the ticks of the refresh driver of any observing
62 * documents that we may have. Only then (after all animations in this image
63 * have been updated) do we send out "frame changed" notifications,
65 void InvalidateObserversOnNextRefreshDriverTick();
67 // Callback for SVGParseCompleteListener.
68 void OnSVGDocumentParsed();
70 // Callbacks for SVGLoadEventListener.
71 void OnSVGDocumentLoaded();
72 void OnSVGDocumentError();
74 protected:
75 explicit VectorImage(nsIURI* aURI = nullptr);
76 virtual ~VectorImage();
78 virtual nsresult StartAnimation() override;
79 virtual nsresult StopAnimation() override;
80 virtual bool ShouldAnimate() override;
82 private:
83 Tuple<ImgDrawResult, gfx::IntSize, RefPtr<gfx::SourceSurface>>
84 GetFrameInternal(const gfx::IntSize& aSize,
85 const Maybe<SVGImageContext>& aSVGContext,
86 uint32_t aWhichFrame, uint32_t aFlags) override;
88 Tuple<ImgDrawResult, gfx::IntSize> GetImageContainerSize(
89 layers::LayerManager* aManager, const gfx::IntSize& aSize,
90 uint32_t aFlags) override;
92 /**
93 * Attempt to find a matching cached surface in the SurfaceCache. Returns the
94 * cached surface, if found, and the size to rasterize at, if applicable.
95 * If we cannot rasterize, it will be the requested size to draw at (aSize).
97 Tuple<RefPtr<gfx::SourceSurface>, gfx::IntSize> LookupCachedSurface(
98 const gfx::IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
99 uint32_t aFlags);
101 bool MaybeRestrictSVGContext(Maybe<SVGImageContext>& aNewSVGContext,
102 const Maybe<SVGImageContext>& aSVGContext,
103 uint32_t aFlags);
105 /// Create a gfxDrawable which callbacks into the SVG document.
106 already_AddRefed<gfxDrawable> CreateSVGDrawable(
107 const SVGDrawingParameters& aParams);
109 /// Rasterize the SVG into a surface. aWillCache will be set to whether or
110 /// not the new surface was put into the cache.
111 already_AddRefed<gfx::SourceSurface> CreateSurface(
112 const SVGDrawingParameters& aParams, gfxDrawable* aSVGDrawable,
113 bool& aWillCache);
115 /// Send a frame complete notification if appropriate. Must be called only
116 /// after all drawing has been completed.
117 void SendFrameComplete(bool aDidCache, uint32_t aFlags);
119 void Show(gfxDrawable* aDrawable, const SVGDrawingParameters& aParams);
121 nsresult Init(const char* aMimeType, uint32_t aFlags);
124 * In catastrophic circumstances like a GPU driver crash, we may lose our
125 * surfaces even if they're locked. RecoverFromLossOfSurfaces discards all
126 * existing surfaces, allowing us to recover.
128 void RecoverFromLossOfSurfaces();
130 void CancelAllListeners();
131 void SendInvalidationNotifications();
133 void ReportDocumentUseCounters();
135 RefPtr<SVGDocumentWrapper> mSVGDocumentWrapper;
136 RefPtr<SVGRootRenderingObserver> mRenderingObserver;
137 RefPtr<SVGLoadEventListener> mLoadEventListener;
138 RefPtr<SVGParseCompleteListener> mParseCompleteListener;
140 /// Count of locks on this image (roughly correlated to visible instances).
141 uint32_t mLockCount;
143 // Stored result from the Necko load of the image, which we save in
144 // OnImageDataComplete if the underlying SVG document isn't loaded. If we save
145 // this, we actually notify this progress (and clear this value) in
146 // OnSVGDocumentLoaded or OnSVGDocumentError.
147 Maybe<Progress> mLoadProgress;
149 bool mIsInitialized; // Have we been initialized?
150 bool mDiscardable; // Are we discardable?
151 bool mIsFullyLoaded; // Has the SVG document finished
152 // loading?
153 bool mIsDrawing; // Are we currently drawing?
154 bool mHaveAnimations; // Is our SVG content SMIL-animated?
155 // (Only set after mIsFullyLoaded.)
156 bool mHasPendingInvalidation; // Invalidate observers next refresh
157 // driver tick.
159 friend class ImageFactory;
162 inline NS_IMETHODIMP VectorImage::GetAnimationMode(uint16_t* aAnimationMode) {
163 return GetAnimationModeInternal(aAnimationMode);
166 inline NS_IMETHODIMP VectorImage::SetAnimationMode(uint16_t aAnimationMode) {
167 return SetAnimationModeInternal(aAnimationMode);
170 } // namespace image
171 } // namespace mozilla
173 #endif // mozilla_image_VectorImage_h