Bug 1655184 [wpt PR 24743] - [LCP] Ignore paints with opacity 0, a=testonly
[gecko.git] / image / VectorImage.h
blobad096db56f878e547262374e33b9574d72f10a6a
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 struct MediaFeatureChange;
19 namespace image {
21 struct SVGDrawingParameters;
22 class SVGDocumentWrapper;
23 class SVGRootRenderingObserver;
24 class SVGLoadEventListener;
25 class SVGParseCompleteListener;
27 class VectorImage final : public ImageResource, public nsIStreamListener {
28 public:
29 NS_DECL_ISUPPORTS
30 NS_DECL_NSIREQUESTOBSERVER
31 NS_DECL_NSISTREAMLISTENER
32 NS_DECL_IMGICONTAINER
34 // (no public constructor - use ImageFactory)
36 // Methods inherited from Image
37 void MediaFeatureValuesChangedAllDocuments(const MediaFeatureChange&) final;
38 nsresult GetNativeSizes(nsTArray<gfx::IntSize>& aNativeSizes) const override;
39 size_t GetNativeSizesLength() const override;
40 virtual size_t SizeOfSourceWithComputedFallback(
41 SizeOfState& aState) const override;
42 virtual void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
43 MallocSizeOf aMallocSizeOf) const override;
45 virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
46 nsISupports* aContext,
47 nsIInputStream* aInStr,
48 uint64_t aSourceOffset,
49 uint32_t aCount) override;
50 virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
51 nsISupports* aContext, nsresult aResult,
52 bool aLastPart) override;
54 virtual void OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) override;
56 /**
57 * Callback for SVGRootRenderingObserver.
59 * This just sets a dirty flag that we check in VectorImage::RequestRefresh,
60 * which is called under the ticks of the refresh driver of any observing
61 * documents that we may have. Only then (after all animations in this image
62 * have been updated) do we send out "frame changed" notifications,
64 void InvalidateObserversOnNextRefreshDriverTick();
66 // Callback for SVGParseCompleteListener.
67 void OnSVGDocumentParsed();
69 // Callbacks for SVGLoadEventListener.
70 void OnSVGDocumentLoaded();
71 void OnSVGDocumentError();
73 protected:
74 explicit VectorImage(nsIURI* aURI = nullptr);
75 virtual ~VectorImage();
77 virtual nsresult StartAnimation() override;
78 virtual nsresult StopAnimation() override;
79 virtual bool ShouldAnimate() override;
81 private:
82 Tuple<ImgDrawResult, IntSize, RefPtr<SourceSurface>> GetFrameInternal(
83 const IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
84 uint32_t aWhichFrame, uint32_t aFlags) override;
86 Tuple<ImgDrawResult, IntSize> GetImageContainerSize(
87 layers::LayerManager* aManager, const IntSize& aSize,
88 uint32_t aFlags) override;
90 /**
91 * Attempt to find a matching cached surface in the SurfaceCache. Returns the
92 * cached surface, if found, and the size to rasterize at, if applicable.
93 * If we cannot rasterize, it will be the requested size to draw at (aSize).
95 Tuple<RefPtr<SourceSurface>, IntSize> LookupCachedSurface(
96 const IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
97 uint32_t aFlags);
99 bool MaybeRestrictSVGContext(Maybe<SVGImageContext>& aNewSVGContext,
100 const Maybe<SVGImageContext>& aSVGContext,
101 uint32_t aFlags);
103 /// Create a gfxDrawable which callbacks into the SVG document.
104 already_AddRefed<gfxDrawable> CreateSVGDrawable(
105 const SVGDrawingParameters& aParams);
107 /// Rasterize the SVG into a surface. aWillCache will be set to whether or
108 /// not the new surface was put into the cache.
109 already_AddRefed<SourceSurface> CreateSurface(
110 const SVGDrawingParameters& aParams, gfxDrawable* aSVGDrawable,
111 bool& aWillCache);
113 /// Send a frame complete notification if appropriate. Must be called only
114 /// after all drawing has been completed.
115 void SendFrameComplete(bool aDidCache, uint32_t aFlags);
117 void Show(gfxDrawable* aDrawable, const SVGDrawingParameters& aParams);
119 nsresult Init(const char* aMimeType, uint32_t aFlags);
122 * In catastrophic circumstances like a GPU driver crash, we may lose our
123 * surfaces even if they're locked. RecoverFromLossOfSurfaces discards all
124 * existing surfaces, allowing us to recover.
126 void RecoverFromLossOfSurfaces();
128 void CancelAllListeners();
129 void SendInvalidationNotifications();
131 RefPtr<SVGDocumentWrapper> mSVGDocumentWrapper;
132 RefPtr<SVGRootRenderingObserver> mRenderingObserver;
133 RefPtr<SVGLoadEventListener> mLoadEventListener;
134 RefPtr<SVGParseCompleteListener> mParseCompleteListener;
136 /// Count of locks on this image (roughly correlated to visible instances).
137 uint32_t mLockCount;
139 // Stored result from the Necko load of the image, which we save in
140 // OnImageDataComplete if the underlying SVG document isn't loaded. If we save
141 // this, we actually notify this progress (and clear this value) in
142 // OnSVGDocumentLoaded or OnSVGDocumentError.
143 Maybe<Progress> mLoadProgress;
145 bool mIsInitialized; // Have we been initialized?
146 bool mDiscardable; // Are we discardable?
147 bool mIsFullyLoaded; // Has the SVG document finished
148 // loading?
149 bool mIsDrawing; // Are we currently drawing?
150 bool mHaveAnimations; // Is our SVG content SMIL-animated?
151 // (Only set after mIsFullyLoaded.)
152 bool mHasPendingInvalidation; // Invalidate observers next refresh
153 // driver tick.
155 friend class ImageFactory;
158 inline NS_IMETHODIMP VectorImage::GetAnimationMode(uint16_t* aAnimationMode) {
159 return GetAnimationModeInternal(aAnimationMode);
162 inline NS_IMETHODIMP VectorImage::SetAnimationMode(uint16_t aAnimationMode) {
163 return SetAnimationModeInternal(aAnimationMode);
166 } // namespace image
167 } // namespace mozilla
169 #endif // mozilla_image_VectorImage_h