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