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