Coverage Report

Created: 2018-09-25 14:53

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