Bug 1516095 [wpt PR 14643] - [Resource-Timing] Flakiness reduction, a=testonly
[gecko.git] / image / VectorImage.h
blob02c7402b1cea59df22280292d80ecbcdaac5cf43
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/MemoryReporting.h"
13 class nsIRequest;
14 class gfxDrawable;
16 namespace mozilla {
17 namespace image {
19 struct SVGDrawingParameters;
20 class SVGDocumentWrapper;
21 class SVGRootRenderingObserver;
22 class SVGLoadEventListener;
23 class SVGParseCompleteListener;
25 class VectorImage final : public ImageResource, public nsIStreamListener {
26 public:
27 NS_DECL_ISUPPORTS
28 NS_DECL_NSIREQUESTOBSERVER
29 NS_DECL_NSISTREAMLISTENER
30 NS_DECL_IMGICONTAINER
32 // (no public constructor - use ImageFactory)
34 // Methods inherited from Image
35 nsresult GetNativeSizes(nsTArray<gfx::IntSize>& aNativeSizes) const override;
36 size_t GetNativeSizesLength() const override;
37 virtual size_t SizeOfSourceWithComputedFallback(
38 SizeOfState& aState) const override;
39 virtual void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
40 MallocSizeOf aMallocSizeOf) const override;
42 virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
43 nsISupports* aContext,
44 nsIInputStream* aInStr,
45 uint64_t aSourceOffset,
46 uint32_t aCount) override;
47 virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
48 nsISupports* aContext, nsresult aResult,
49 bool aLastPart) override;
51 virtual void OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) override;
53 /**
54 * Callback for SVGRootRenderingObserver.
56 * This just sets a dirty flag that we check in VectorImage::RequestRefresh,
57 * which is called under the ticks of the refresh driver of any observing
58 * documents that we may have. Only then (after all animations in this image
59 * have been updated) do we send out "frame changed" notifications,
61 void InvalidateObserversOnNextRefreshDriverTick();
63 // Callback for SVGParseCompleteListener.
64 void OnSVGDocumentParsed();
66 // Callbacks for SVGLoadEventListener.
67 void OnSVGDocumentLoaded();
68 void OnSVGDocumentError();
70 virtual void ReportUseCounters() override;
72 protected:
73 explicit VectorImage(nsIURI* aURI = nullptr);
74 virtual ~VectorImage();
76 virtual nsresult StartAnimation() override;
77 virtual nsresult StopAnimation() override;
78 virtual bool ShouldAnimate() override;
80 private:
81 Tuple<ImgDrawResult, IntSize, RefPtr<SourceSurface>> GetFrameInternal(
82 const IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
83 uint32_t aWhichFrame, uint32_t aFlags) override;
85 Tuple<ImgDrawResult, IntSize> GetImageContainerSize(
86 layers::LayerManager* aManager, const IntSize& aSize,
87 uint32_t aFlags) override;
89 /**
90 * Attempt to find a matching cached surface in the SurfaceCache. Returns the
91 * cached surface, if found, and the size to rasterize at, if applicable.
92 * If we cannot rasterize, it will be the requested size to draw at (aSize).
94 Tuple<RefPtr<SourceSurface>, IntSize> LookupCachedSurface(
95 const IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
96 uint32_t aFlags);
98 bool MaybeRestrictSVGContext(Maybe<SVGImageContext>& aNewSVGContext,
99 const Maybe<SVGImageContext>& aSVGContext,
100 uint32_t aFlags);
102 /// Create a gfxDrawable which callbacks into the SVG document.
103 already_AddRefed<gfxDrawable> CreateSVGDrawable(
104 const SVGDrawingParameters& aParams);
106 /// Returns true if we use the surface cache to store rasterized copies.
107 bool UseSurfaceCacheForSize(const IntSize& aSize) const;
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<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 RefPtr<SVGDocumentWrapper> mSVGDocumentWrapper;
134 RefPtr<SVGRootRenderingObserver> mRenderingObserver;
135 RefPtr<SVGLoadEventListener> mLoadEventListener;
136 RefPtr<SVGParseCompleteListener> mParseCompleteListener;
138 /// Count of locks on this image (roughly correlated to visible instances).
139 uint32_t mLockCount;
141 // Stored result from the Necko load of the image, which we save in
142 // OnImageDataComplete if the underlying SVG document isn't loaded. If we save
143 // this, we actually notify this progress (and clear this value) in
144 // OnSVGDocumentLoaded or OnSVGDocumentError.
145 Maybe<Progress> mLoadProgress;
147 bool mIsInitialized; // Have we been initialized?
148 bool mDiscardable; // Are we discardable?
149 bool mIsFullyLoaded; // Has the SVG document finished
150 // loading?
151 bool mIsDrawing; // Are we currently drawing?
152 bool mHaveAnimations; // Is our SVG content SMIL-animated?
153 // (Only set after mIsFullyLoaded.)
154 bool mHasPendingInvalidation; // Invalidate observers next refresh
155 // driver tick.
157 friend class ImageFactory;
160 inline NS_IMETHODIMP VectorImage::GetAnimationMode(uint16_t* aAnimationMode) {
161 return GetAnimationModeInternal(aAnimationMode);
164 inline NS_IMETHODIMP VectorImage::SetAnimationMode(uint16_t aAnimationMode) {
165 return SetAnimationModeInternal(aAnimationMode);
168 } // namespace image
169 } // namespace mozilla
171 #endif // mozilla_image_VectorImage_h