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
10 #include "nsIStreamListener.h"
11 #include "mozilla/gfx/Point.h"
12 #include "mozilla/MemoryReporting.h"
18 struct MediaFeatureChange
;
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
{
32 NS_DECL_NSIREQUESTOBSERVER
33 NS_DECL_NSISTREAMLISTENER
36 // (no public constructor - use ImageFactory)
38 // Methods inherited from Image
39 virtual size_t SizeOfSourceWithComputedFallback(
40 SizeOfState
& aState
) const override
;
42 virtual nsresult
OnImageDataAvailable(nsIRequest
* aRequest
,
43 nsIInputStream
* aInStr
,
44 uint64_t aSourceOffset
,
45 uint32_t aCount
) override
;
46 virtual nsresult
OnImageDataComplete(nsIRequest
* aRequest
, nsresult aResult
,
47 bool aLastPart
) override
;
49 virtual void OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) override
;
52 * Callback for SVGRootRenderingObserver.
54 * This just sets a dirty flag that we check in VectorImage::RequestRefresh,
55 * which is called under the ticks of the refresh driver of any observing
56 * documents that we may have. Only then (after all animations in this image
57 * have been updated) do we send out "frame changed" notifications,
59 void InvalidateObserversOnNextRefreshDriverTick();
61 // Callback for SVGParseCompleteListener.
62 void OnSVGDocumentParsed();
64 // Callbacks for SVGLoadEventListener.
65 void OnSVGDocumentLoaded();
66 void OnSVGDocumentError();
69 explicit VectorImage(nsIURI
* aURI
= nullptr);
70 virtual ~VectorImage();
72 virtual nsresult
StartAnimation() override
;
73 virtual nsresult
StopAnimation() override
;
74 virtual bool ShouldAnimate() override
;
77 friend class SourceSurfaceBlobImage
;
80 * Attempt to find a matching cached surface in the SurfaceCache. Returns the
81 * cached surface, if found, and the size to rasterize at, if applicable.
82 * If we cannot rasterize, it will be the requested size to draw at (aSize).
84 std::tuple
<RefPtr
<gfx::SourceSurface
>, gfx::IntSize
> LookupCachedSurface(
85 const gfx::IntSize
& aSize
, const SVGImageContext
& aSVGContext
,
88 bool MaybeRestrictSVGContext(SVGImageContext
& aSVGContext
, uint32_t aFlags
);
90 /// Create a gfxDrawable which callbacks into the SVG document.
91 already_AddRefed
<gfxDrawable
> CreateSVGDrawable(
92 const SVGDrawingParameters
& aParams
);
94 /// Rasterize the SVG into a surface. aWillCache will be set to whether or
95 /// not the new surface was put into the cache.
96 already_AddRefed
<gfx::SourceSurface
> CreateSurface(
97 const SVGDrawingParameters
& aParams
, gfxDrawable
* aSVGDrawable
,
100 /// Send a frame complete notification if appropriate. Must be called only
101 /// after all drawing has been completed.
102 void SendFrameComplete(bool aDidCache
, uint32_t aFlags
);
104 void Show(gfxDrawable
* aDrawable
, const SVGDrawingParameters
& aParams
);
106 nsresult
Init(const char* aMimeType
, uint32_t aFlags
);
109 * In catastrophic circumstances like a GPU driver crash, we may lose our
110 * surfaces even if they're locked. RecoverFromLossOfSurfaces discards all
111 * existing surfaces, allowing us to recover.
113 void RecoverFromLossOfSurfaces();
115 void CancelAllListeners();
116 void SendInvalidationNotifications();
118 void ReportDocumentUseCounters();
120 RefPtr
<SVGDocumentWrapper
> mSVGDocumentWrapper
;
121 RefPtr
<SVGRootRenderingObserver
> mRenderingObserver
;
122 RefPtr
<SVGLoadEventListener
> mLoadEventListener
;
123 RefPtr
<SVGParseCompleteListener
> mParseCompleteListener
;
125 /// Count of locks on this image (roughly correlated to visible instances).
128 // Stored result from the Necko load of the image, which we save in
129 // OnImageDataComplete if the underlying SVG document isn't loaded. If we save
130 // this, we actually notify this progress (and clear this value) in
131 // OnSVGDocumentLoaded or OnSVGDocumentError.
132 Maybe
<Progress
> mLoadProgress
;
134 bool mIsInitialized
; // Have we been initialized?
135 bool mDiscardable
; // Are we discardable?
136 bool mIsFullyLoaded
; // Has the SVG document finished
138 bool mHaveAnimations
; // Is our SVG content SMIL-animated?
139 // (Only set after mIsFullyLoaded.)
140 bool mHasPendingInvalidation
; // Invalidate observers next refresh
143 friend class ImageFactory
;
146 inline NS_IMETHODIMP
VectorImage::GetAnimationMode(uint16_t* aAnimationMode
) {
147 return GetAnimationModeInternal(aAnimationMode
);
150 inline NS_IMETHODIMP
VectorImage::SetAnimationMode(uint16_t aAnimationMode
) {
151 return SetAnimationModeInternal(aAnimationMode
);
155 } // namespace mozilla
157 #endif // mozilla_image_VectorImage_h