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/MemoryReporting.h"
19 struct SVGDrawingParameters
;
20 class SVGDocumentWrapper
;
21 class SVGRootRenderingObserver
;
22 class SVGLoadEventListener
;
23 class SVGParseCompleteListener
;
25 class VectorImage final
: public ImageResource
, public nsIStreamListener
{
28 NS_DECL_NSIREQUESTOBSERVER
29 NS_DECL_NSISTREAMLISTENER
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
;
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
;
73 explicit VectorImage(nsIURI
* aURI
= nullptr);
74 virtual ~VectorImage();
76 virtual nsresult
StartAnimation() override
;
77 virtual nsresult
StopAnimation() override
;
78 virtual bool ShouldAnimate() override
;
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
;
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
,
98 bool MaybeRestrictSVGContext(Maybe
<SVGImageContext
>& aNewSVGContext
,
99 const Maybe
<SVGImageContext
>& aSVGContext
,
102 /// Create a gfxDrawable which callbacks into the SVG document.
103 already_AddRefed
<gfxDrawable
> CreateSVGDrawable(
104 const SVGDrawingParameters
& aParams
);
106 /// Rasterize the SVG into a surface. aWillCache will be set to whether or
107 /// not the new surface was put into the cache.
108 already_AddRefed
<SourceSurface
> CreateSurface(
109 const SVGDrawingParameters
& aParams
, gfxDrawable
* aSVGDrawable
,
112 /// Send a frame complete notification if appropriate. Must be called only
113 /// after all drawing has been completed.
114 void SendFrameComplete(bool aDidCache
, uint32_t aFlags
);
116 void Show(gfxDrawable
* aDrawable
, const SVGDrawingParameters
& aParams
);
118 nsresult
Init(const char* aMimeType
, uint32_t aFlags
);
121 * In catastrophic circumstances like a GPU driver crash, we may lose our
122 * surfaces even if they're locked. RecoverFromLossOfSurfaces discards all
123 * existing surfaces, allowing us to recover.
125 void RecoverFromLossOfSurfaces();
127 void CancelAllListeners();
128 void SendInvalidationNotifications();
130 RefPtr
<SVGDocumentWrapper
> mSVGDocumentWrapper
;
131 RefPtr
<SVGRootRenderingObserver
> mRenderingObserver
;
132 RefPtr
<SVGLoadEventListener
> mLoadEventListener
;
133 RefPtr
<SVGParseCompleteListener
> mParseCompleteListener
;
135 /// Count of locks on this image (roughly correlated to visible instances).
138 // Stored result from the Necko load of the image, which we save in
139 // OnImageDataComplete if the underlying SVG document isn't loaded. If we save
140 // this, we actually notify this progress (and clear this value) in
141 // OnSVGDocumentLoaded or OnSVGDocumentError.
142 Maybe
<Progress
> mLoadProgress
;
144 bool mIsInitialized
; // Have we been initialized?
145 bool mDiscardable
; // Are we discardable?
146 bool mIsFullyLoaded
; // Has the SVG document finished
148 bool mIsDrawing
; // Are we currently drawing?
149 bool mHaveAnimations
; // Is our SVG content SMIL-animated?
150 // (Only set after mIsFullyLoaded.)
151 bool mHasPendingInvalidation
; // Invalidate observers next refresh
154 friend class ImageFactory
;
157 inline NS_IMETHODIMP
VectorImage::GetAnimationMode(uint16_t* aAnimationMode
) {
158 return GetAnimationModeInternal(aAnimationMode
);
161 inline NS_IMETHODIMP
VectorImage::SetAnimationMode(uint16_t aAnimationMode
) {
162 return SetAnimationModeInternal(aAnimationMode
);
166 } // namespace mozilla
168 #endif // mozilla_image_VectorImage_h