VectorImage.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef mozilla_image_VectorImage_h
  6. #define mozilla_image_VectorImage_h
  7. #include "Image.h"
  8. #include "nsIStreamListener.h"
  9. #include "mozilla/MemoryReporting.h"
  10. class nsIRequest;
  11. class gfxDrawable;
  12. namespace mozilla {
  13. namespace image {
  14. struct SVGDrawingParameters;
  15. class SVGDocumentWrapper;
  16. class SVGRootRenderingObserver;
  17. class SVGLoadEventListener;
  18. class SVGParseCompleteListener;
  19. class VectorImage final : public ImageResource,
  20. public nsIStreamListener
  21. {
  22. public:
  23. NS_DECL_ISUPPORTS
  24. NS_DECL_NSIREQUESTOBSERVER
  25. NS_DECL_NSISTREAMLISTENER
  26. NS_DECL_IMGICONTAINER
  27. // (no public constructor - use ImageFactory)
  28. // Methods inherited from Image
  29. virtual size_t SizeOfSourceWithComputedFallback(MallocSizeOf aMallocSizeOf)
  30. const override;
  31. virtual void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
  32. MallocSizeOf aMallocSizeOf) const override;
  33. virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
  34. nsISupports* aContext,
  35. nsIInputStream* aInStr,
  36. uint64_t aSourceOffset,
  37. uint32_t aCount) override;
  38. virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
  39. nsISupports* aContext,
  40. nsresult aResult,
  41. bool aLastPart) override;
  42. virtual void OnSurfaceDiscarded() override;
  43. /**
  44. * Callback for SVGRootRenderingObserver.
  45. *
  46. * This just sets a dirty flag that we check in VectorImage::RequestRefresh,
  47. * which is called under the ticks of the refresh driver of any observing
  48. * documents that we may have. Only then (after all animations in this image
  49. * have been updated) do we send out "frame changed" notifications,
  50. */
  51. void InvalidateObserversOnNextRefreshDriverTick();
  52. // Callback for SVGParseCompleteListener.
  53. void OnSVGDocumentParsed();
  54. // Callbacks for SVGLoadEventListener.
  55. void OnSVGDocumentLoaded();
  56. void OnSVGDocumentError();
  57. protected:
  58. explicit VectorImage(ImageURL* aURI = nullptr);
  59. virtual ~VectorImage();
  60. virtual nsresult StartAnimation() override;
  61. virtual nsresult StopAnimation() override;
  62. virtual bool ShouldAnimate() override;
  63. private:
  64. /// Attempt to find a cached surface matching @aParams in the SurfaceCache.
  65. already_AddRefed<gfxDrawable>
  66. LookupCachedSurface(const SVGDrawingParameters& aParams);
  67. void CreateSurfaceAndShow(const SVGDrawingParameters& aParams,
  68. gfx::BackendType aBackend);
  69. void Show(gfxDrawable* aDrawable, const SVGDrawingParameters& aParams);
  70. nsresult Init(const char* aMimeType, uint32_t aFlags);
  71. /**
  72. * In catastrophic circumstances like a GPU driver crash, we may lose our
  73. * surfaces even if they're locked. RecoverFromLossOfSurfaces discards all
  74. * existing surfaces, allowing us to recover.
  75. */
  76. void RecoverFromLossOfSurfaces();
  77. void CancelAllListeners();
  78. void SendInvalidationNotifications();
  79. RefPtr<SVGDocumentWrapper> mSVGDocumentWrapper;
  80. RefPtr<SVGRootRenderingObserver> mRenderingObserver;
  81. RefPtr<SVGLoadEventListener> mLoadEventListener;
  82. RefPtr<SVGParseCompleteListener> mParseCompleteListener;
  83. /// Count of locks on this image (roughly correlated to visible instances).
  84. uint32_t mLockCount;
  85. // Stored result from the Necko load of the image, which we save in
  86. // OnImageDataComplete if the underlying SVG document isn't loaded. If we save
  87. // this, we actually notify this progress (and clear this value) in
  88. // OnSVGDocumentLoaded or OnSVGDocumentError.
  89. Maybe<Progress> mLoadProgress;
  90. bool mIsInitialized; // Have we been initialized?
  91. bool mDiscardable; // Are we discardable?
  92. bool mIsFullyLoaded; // Has the SVG document finished
  93. // loading?
  94. bool mIsDrawing; // Are we currently drawing?
  95. bool mHaveAnimations; // Is our SVG content SMIL-animated?
  96. // (Only set after mIsFullyLoaded.)
  97. bool mHasPendingInvalidation; // Invalidate observers next refresh
  98. // driver tick.
  99. friend class ImageFactory;
  100. };
  101. inline NS_IMETHODIMP VectorImage::GetAnimationMode(uint16_t* aAnimationMode) {
  102. return GetAnimationModeInternal(aAnimationMode);
  103. }
  104. inline NS_IMETHODIMP VectorImage::SetAnimationMode(uint16_t aAnimationMode) {
  105. return SetAnimationModeInternal(aAnimationMode);
  106. }
  107. } // namespace image
  108. } // namespace mozilla
  109. #endif // mozilla_image_VectorImage_h